Browse Source

Fix #1446: Add indentation options to UI.

pull/1471/head
Siegfried Pammer 6 years ago
parent
commit
6d89c4fbbb
  1. 1
      ICSharpCode.Decompiler/Output/ITextOutput.cs
  2. 15
      ICSharpCode.Decompiler/Output/PlainTextOutput.cs
  3. 11
      ILSpy/DecompilationOptions.cs
  4. 2
      ILSpy/Languages/CSharpILMixedLanguage.cs
  5. 1
      ILSpy/Languages/CSharpLanguage.cs
  6. 1
      ILSpy/Languages/ILLanguage.cs
  7. 2
      ILSpy/Options/DecompilerSettingsPanel.xaml
  8. 39
      ILSpy/Options/DisplaySettings.cs
  9. 16
      ILSpy/Options/DisplaySettingsPanel.xaml
  10. 31
      ILSpy/Options/DisplaySettingsPanel.xaml.cs

1
ICSharpCode.Decompiler/Output/ITextOutput.cs

@ -26,6 +26,7 @@ namespace ICSharpCode.Decompiler @@ -26,6 +26,7 @@ namespace ICSharpCode.Decompiler
{
public interface ITextOutput
{
string IndentationString { get; set; }
void Indent();
void Unindent();
void Write(char ch);

15
ICSharpCode.Decompiler/Output/PlainTextOutput.cs

@ -35,7 +35,9 @@ namespace ICSharpCode.Decompiler @@ -35,7 +35,9 @@ namespace ICSharpCode.Decompiler
int line = 1;
int column = 1;
public string IndentationString { get; set; } = "\t";
public PlainTextOutput(TextWriter writer)
{
if (writer == null)
@ -74,7 +76,7 @@ namespace ICSharpCode.Decompiler @@ -74,7 +76,7 @@ namespace ICSharpCode.Decompiler
if (needsIndent) {
needsIndent = false;
for (int i = 0; i < indent; i++) {
writer.Write('\t');
writer.Write(IndentationString);
}
column += indent;
}
@ -154,6 +156,15 @@ namespace ICSharpCode.Decompiler @@ -154,6 +156,15 @@ namespace ICSharpCode.Decompiler
this.actions = new List<Action<ITextOutput>>();
}
string ITextOutput.IndentationString {
get {
return target.IndentationString;
}
set {
target.IndentationString = value;
}
}
public void Commit()
{
foreach (var action in actions) {

11
ILSpy/DecompilationOptions.cs

@ -86,6 +86,17 @@ namespace ICSharpCode.ILSpy @@ -86,6 +86,17 @@ namespace ICSharpCode.ILSpy
newSettings.ExpandUsingDeclarations = displaySettings.ExpandUsingDeclarations;
newSettings.FoldBraces = displaySettings.FoldBraces;
newSettings.ShowDebugInfo = displaySettings.ShowDebugInfo;
newSettings.CSharpFormattingOptions.IndentationString = GetIndentationString(displaySettings);
}
private string GetIndentationString(DisplaySettings displaySettings)
{
if (displaySettings.IndentationUseTabs) {
int numberOfTabs = displaySettings.IndentationSize / displaySettings.IndentationTabSize;
int numberOfSpaces = displaySettings.IndentationSize % displaySettings.IndentationTabSize;
return new string('\t', numberOfTabs) + new string(' ', numberOfSpaces);
}
return new string(' ', displaySettings.IndentationSize);
}
}
}

2
ILSpy/Languages/CSharpILMixedLanguage.cs

@ -65,7 +65,7 @@ namespace ICSharpCode.ILSpy @@ -65,7 +65,7 @@ namespace ICSharpCode.ILSpy
static void WriteCode(TextWriter output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
{
syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
TokenWriter tokenWriter = new TextWriterTokenWriter(output);
TokenWriter tokenWriter = new TextWriterTokenWriter(output) { IndentationString = settings.CSharpFormattingOptions.IndentationString };
tokenWriter = TokenWriter.WrapInWriterThatSetsLocationsInAST(tokenWriter);
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions));
}

1
ILSpy/Languages/CSharpLanguage.cs

@ -121,6 +121,7 @@ namespace ICSharpCode.ILSpy @@ -121,6 +121,7 @@ namespace ICSharpCode.ILSpy
void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem)
{
syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
output.IndentationString = settings.CSharpFormattingOptions.IndentationString;
TokenWriter tokenWriter = new TextTokenWriter(output, settings, typeSystem);
if (output is ISmartTextOutput highlightingOutput) {
tokenWriter = new CSharpHighlightingTokenWriter(tokenWriter, highlightingOutput);

1
ILSpy/Languages/ILLanguage.cs

@ -52,6 +52,7 @@ namespace ICSharpCode.ILSpy @@ -52,6 +52,7 @@ namespace ICSharpCode.ILSpy
protected virtual ReflectionDisassembler CreateDisassembler(ITextOutput output, DecompilationOptions options)
{
output.IndentationString = options.DecompilerSettings.CSharpFormattingOptions.IndentationString;
return new ReflectionDisassembler(output, options.CancellationToken) {
DetectControlStructure = detectControlStructure,
ShowSequencePoints = options.DecompilerSettings.ShowDebugInfo,

2
ILSpy/Options/DecompilerSettingsPanel.xaml

@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Margin="3" Grid.ColumnSpan="3" TextWrapping="Wrap" Text="The settings selected below are applied to the decompiler output in addition to the setting selected in the language drop-down. Note that some settings implicitly depend on each other, e.g.: LINQ expressions cannot be introduced without first transforming static calls to extension method calls." />
<TextBlock Margin="3" Grid.ColumnSpan="3" TextWrapping="Wrap" Text="The settings selected below are applied to the decompiler output in combination with the selection in the language drop-down. Selecting a lower language version in the drop-down will deactivate all selected options of the higher versions. Note that some settings implicitly depend on each other, e.g.: LINQ expressions cannot be introduced without first transforming static calls to extension method calls." />
<ListBox Grid.Row="1" ItemsSource="{Binding Source={StaticResource SettingsCollection}}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">

39
ILSpy/Options/DisplaySettings.cs

@ -179,6 +179,42 @@ namespace ICSharpCode.ILSpy.Options @@ -179,6 +179,42 @@ namespace ICSharpCode.ILSpy.Options
}
}
bool indentationUseTabs = true;
public bool IndentationUseTabs {
get { return indentationUseTabs; }
set {
if (indentationUseTabs != value) {
indentationUseTabs = value;
OnPropertyChanged();
}
}
}
int indentationTabSize = 4;
public int IndentationTabSize {
get { return indentationTabSize; }
set {
if (indentationTabSize != value) {
indentationTabSize = value;
OnPropertyChanged();
}
}
}
int indentationSize = 4;
public int IndentationSize {
get { return indentationSize; }
set {
if (indentationSize != value) {
indentationSize = value;
OnPropertyChanged();
}
}
}
public void CopyValues(DisplaySettings s)
{
this.SelectedFont = s.selectedFont;
@ -192,6 +228,9 @@ namespace ICSharpCode.ILSpy.Options @@ -192,6 +228,9 @@ namespace ICSharpCode.ILSpy.Options
this.FoldBraces = s.foldBraces;
this.ExpandMemberDefinitions = s.expandMemberDefinitions;
this.ExpandUsingDeclarations = s.expandUsingDeclarations;
this.IndentationUseTabs = s.indentationUseTabs;
this.IndentationTabSize = s.indentationTabSize;
this.IndentationSize = s.indentationSize;
}
}
}

16
ILSpy/Options/DisplaySettingsPanel.xaml

@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
@ -57,7 +58,20 @@ @@ -57,7 +58,20 @@
</Border>
</Grid>
</GroupBox>
<GroupBox Header="Other options" Grid.Row="1">
<GroupBox Header="Indentation" Grid.Row="1">
<StackPanel Margin="3">
<CheckBox IsChecked="{Binding IndentationUseTabs}">Use tabs instead of spaces</CheckBox>
<StackPanel Orientation="Horizontal">
<Label>Tab size:</Label>
<TextBox x:Name="tabSizeTextBox" Width="50" Margin="3" Text="{Binding IndentationTabSize}" PreviewTextInput="TextBox_PreviewTextInput" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Indent size:</Label>
<TextBox x:Name="indentSizeTextBox" Width="50" Margin="3" Text="{Binding IndentationSize}" PreviewTextInput="TextBox_PreviewTextInput" />
</StackPanel>
</StackPanel>
</GroupBox>
<GroupBox Header="Other options" Grid.Row="2">
<StackPanel Margin="3">
<CheckBox IsChecked="{Binding ShowLineNumbers}">Show line numbers</CheckBox>
<CheckBox IsChecked="{Binding ShowMetadataTokens}">Show metadata tokens</CheckBox>

31
ILSpy/Options/DisplaySettingsPanel.xaml.cs

@ -37,7 +37,10 @@ namespace ICSharpCode.ILSpy.Options @@ -37,7 +37,10 @@ namespace ICSharpCode.ILSpy.Options
public DisplaySettingsPanel()
{
InitializeComponent();
DataObject.AddPastingHandler(tabSizeTextBox, OnPaste);
DataObject.AddPastingHandler(indentSizeTextBox, OnPaste);
Task<FontFamily[]> task = new Task<FontFamily[]>(FontLoader);
task.Start();
task.ContinueWith(
@ -57,7 +60,7 @@ namespace ICSharpCode.ILSpy.Options @@ -57,7 +60,7 @@ namespace ICSharpCode.ILSpy.Options
}
);
}
public void Load(ILSpySettings settings)
{
this.DataContext = LoadDisplaySettings(settings);
@ -108,6 +111,9 @@ namespace ICSharpCode.ILSpy.Options @@ -108,6 +111,9 @@ namespace ICSharpCode.ILSpy.Options
s.FoldBraces = (bool?)e.Attribute("FoldBraces") ?? false;
s.ExpandMemberDefinitions = (bool?)e.Attribute("ExpandMemberDefinitions") ?? false;
s.ExpandUsingDeclarations = (bool?)e.Attribute("ExpandUsingDeclarations") ?? false;
s.IndentationUseTabs = (bool?)e.Attribute("IndentationUseTabs") ?? true;
s.IndentationSize = (int?)e.Attribute("IndentationSize") ?? 4;
s.IndentationTabSize = (int?)e.Attribute("IndentationTabSize") ?? 4;
return s;
}
@ -127,7 +133,9 @@ namespace ICSharpCode.ILSpy.Options @@ -127,7 +133,9 @@ namespace ICSharpCode.ILSpy.Options
section.SetAttributeValue("SortResults", s.SortResults);
section.SetAttributeValue("FoldBraces", s.FoldBraces);
section.SetAttributeValue("ExpandMemberDefinitions", s.ExpandMemberDefinitions);
section.SetAttributeValue("ExpandUsingDeclarations", s.ExpandUsingDeclarations);
section.SetAttributeValue("IndentationUseTabs", s.IndentationUseTabs);
section.SetAttributeValue("IndentationSize", s.IndentationSize);
section.SetAttributeValue("IndentationTabSize", s.IndentationTabSize);
XElement existingElement = root.Element("DisplaySettings");
if (existingElement != null)
@ -138,8 +146,23 @@ namespace ICSharpCode.ILSpy.Options @@ -138,8 +146,23 @@ namespace ICSharpCode.ILSpy.Options
if (currentDisplaySettings != null)
currentDisplaySettings.CopyValues(s);
}
private void TextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
if (!e.Text.All(char.IsDigit))
e.Handled = true;
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
if (!e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true))
return;
var text = (string)e.SourceDataObject.GetData(DataFormats.UnicodeText, true) ?? string.Empty;
if (!text.All(char.IsDigit))
e.CancelCommand();
}
}
public class FontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

Loading…
Cancel
Save