From d07a18dc432f114b2f8c6416d5495d96d1344684 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 5 Feb 2011 20:29:35 +0100 Subject: [PATCH] Add folding support. --- ILSpy/Disassembler/ReflectionDisassembler.cs | 26 +++++++++++--------- ILSpy/ITextOutput.cs | 4 +-- ILSpy/TextView/DecompilerTextView.cs | 5 ++++ ILSpy/TextView/ILAsm-Mode.xshd | 3 --- ILSpy/TextView/SmartTextOutput.cs | 5 ++-- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/ILSpy/Disassembler/ReflectionDisassembler.cs b/ILSpy/Disassembler/ReflectionDisassembler.cs index f66254859..8992240ae 100644 --- a/ILSpy/Disassembler/ReflectionDisassembler.cs +++ b/ILSpy/Disassembler/ReflectionDisassembler.cs @@ -18,6 +18,7 @@ namespace ICSharpCode.ILSpy.Disassembler ITextOutput output; CancellationToken cancellationToken; bool detectControlStructure; + bool isInType; // whether we are currently disassembling a whole type (-> defaultCollapsed for foldings) MethodBodyDisassembler methodBodyDisassembler; public ReflectionDisassembler(ITextOutput output, bool detectControlStructure, CancellationToken cancellationToken) @@ -123,9 +124,8 @@ namespace ICSharpCode.ILSpy.Disassembler output.Write("unmanaged "); WriteFlags(method.ImplAttributes & ~(MethodImplAttributes.CodeTypeMask | MethodImplAttributes.ManagedMask), methodImpl); - output.WriteLine(); output.Unindent(); - OpenBlock(); + OpenBlock(isInType); WriteAttributes(method.CustomAttributes); if (method.HasBody) @@ -195,8 +195,8 @@ namespace ICSharpCode.ILSpy.Disassembler WriteFlags(property.Attributes, propertyAttributes); property.PropertyType.WriteTo(output); output.Write(' '); - output.WriteLine(DisassemblerHelpers.Escape(property.Name)); - OpenBlock(); + output.Write(DisassemblerHelpers.Escape(property.Name)); + OpenBlock(false); WriteAttributes(property.CustomAttributes); WriteNestedMethod(".get", property.GetMethod); WriteNestedMethod(".set", property.SetMethod); @@ -235,8 +235,8 @@ namespace ICSharpCode.ILSpy.Disassembler WriteFlags(ev.Attributes, eventAttributes); ev.EventType.WriteTo(output); output.Write(' '); - output.WriteLine(DisassemblerHelpers.Escape(ev.Name)); - OpenBlock(); + output.Write(DisassemblerHelpers.Escape(ev.Name)); + OpenBlock(false); WriteAttributes(ev.CustomAttributes); WriteNestedMethod(".add", ev.AddMethod); WriteNestedMethod(".remove", ev.RemoveMethod); @@ -293,17 +293,19 @@ namespace ICSharpCode.ILSpy.Disassembler const TypeAttributes masks = TypeAttributes.ClassSemanticMask | TypeAttributes.VisibilityMask | TypeAttributes.LayoutMask | TypeAttributes.StringFormatMask; WriteFlags(type.Attributes & ~masks, typeAttributes); - output.WriteLine(DisassemblerHelpers.Escape(type.Name)); + output.Write(DisassemblerHelpers.Escape(type.Name)); if (type.BaseType != null) { + output.WriteLine(); output.Indent(); output.Write("extends "); type.BaseType.WriteTo(output); output.Unindent(); - output.WriteLine(); } - OpenBlock(); + OpenBlock(isInType); + bool oldIsInType = isInType; + isInType = true; WriteAttributes(type.CustomAttributes); if (type.HasLayoutInfo) { output.WriteLine(".pack {0}", type.PackingSize); @@ -357,6 +359,7 @@ namespace ICSharpCode.ILSpy.Disassembler output.WriteLine(); } CloseBlock(); + isInType = oldIsInType; } #endregion @@ -394,9 +397,10 @@ namespace ICSharpCode.ILSpy.Disassembler output.Write(")"); } - void OpenBlock() + void OpenBlock(bool defaultCollapsed) { - output.MarkFoldStart(); + output.MarkFoldStart(defaultCollapsed: defaultCollapsed); + output.WriteLine(); output.WriteLine("{"); output.Indent(); } diff --git a/ILSpy/ITextOutput.cs b/ILSpy/ITextOutput.cs index e06c6e5af..03f7404b9 100644 --- a/ILSpy/ITextOutput.cs +++ b/ILSpy/ITextOutput.cs @@ -32,7 +32,7 @@ namespace ICSharpCode.ILSpy void WriteDefinition(string text, object definition); void WriteReference(string text, object reference); - void MarkFoldStart(string collapsedText = "...", bool defaultClosed = false); + void MarkFoldStart(string collapsedText = "...", bool defaultCollapsed = false); void MarkFoldEnd(); } @@ -101,7 +101,7 @@ namespace ICSharpCode.ILSpy Write(text); } - void ITextOutput.MarkFoldStart(string collapsedText, bool defaultClosed) + void ITextOutput.MarkFoldStart(string collapsedText, bool defaultCollapsed) { } diff --git a/ILSpy/TextView/DecompilerTextView.cs b/ILSpy/TextView/DecompilerTextView.cs index 3f33d8d41..e78a61f0b 100644 --- a/ILSpy/TextView/DecompilerTextView.cs +++ b/ILSpy/TextView/DecompilerTextView.cs @@ -13,6 +13,7 @@ using System.Windows.Media.Animation; using System.Windows.Threading; using System.Xml; using ICSharpCode.AvalonEdit; +using ICSharpCode.AvalonEdit.Folding; using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting.Xshd; using Mono.Cecil; @@ -25,6 +26,7 @@ namespace ICSharpCode.ILSpy.TextView sealed partial class DecompilerTextView : UserControl { readonly ReferenceElementGenerator referenceElementGenerator; + readonly FoldingManager foldingManager; internal MainWindow mainWindow; DefinitionLookup definitionLookup; @@ -46,6 +48,7 @@ namespace ICSharpCode.ILSpy.TextView this.referenceElementGenerator = new ReferenceElementGenerator(this); textEditor.TextArea.TextView.ElementGenerators.Add(referenceElementGenerator); textEditor.Text = "Welcome to ILSpy!"; + foldingManager = FoldingManager.Install(textEditor.TextArea); } public void Decompile(IEnumerable treeNodes) @@ -66,12 +69,14 @@ namespace ICSharpCode.ILSpy.TextView if (currentCancellationTokenSource == myCancellationTokenSource) { currentCancellationTokenSource = null; waitAdorner.Visibility = Visibility.Collapsed; + foldingManager.Clear(); try { SmartTextOutput textOutput = task.Result; referenceElementGenerator.References = textOutput.References; definitionLookup = textOutput.DefinitionLookup; textEditor.SyntaxHighlighting = ILSpy.Language.Current.SyntaxHighlighting; textEditor.Text = textOutput.ToString(); + foldingManager.UpdateFoldings(textOutput.Foldings.OrderBy(f => f.StartOffset), -1); } catch (AggregateException ex) { textEditor.SyntaxHighlighting = null; referenceElementGenerator.References = null; diff --git a/ILSpy/TextView/ILAsm-Mode.xshd b/ILSpy/TextView/ILAsm-Mode.xshd index 46da2a8b2..93ec0e8fe 100644 --- a/ILSpy/TextView/ILAsm-Mode.xshd +++ b/ILSpy/TextView/ILAsm-Mode.xshd @@ -1,9 +1,7 @@  - - @@ -506,7 +504,6 @@ " " - [\d\w_]+(?=(\s*\()) \b0[xX][0-9a-fA-F]+|\b(\d+(\.[0-9]+)?|\.[0-9]+)([eE][+-]?[0-9]+)?