Browse Source

add back variable/parameter reference highlighting for C#

pull/728/head
Siegfried Pammer 9 years ago
parent
commit
f02377632c
  1. 6
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 9
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs
  3. 1
      ILSpy/ILSpy.csproj
  4. 19
      ILSpy/Languages/CSharpLanguage.cs
  5. 360
      ILSpy/Languages/TextTokenWriter.cs
  6. 6
      ILSpy/TextView/AvalonEditTextOutput.cs

6
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -593,6 +593,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -593,6 +593,12 @@ namespace ICSharpCode.Decompiler.CSharp
}
var statementBuilder = new StatementBuilder(decompilationContext, method);
var body = statementBuilder.ConvertAsBlock(function.Body);
int i = 0;
foreach (var parameter in entityDecl.GetChildrenByRole(Roles.Parameter)) {
parameter.AddAnnotation(new ILVariableResolveResult(function.Variables.First(v => v.Kind == VariableKind.Parameter && v.Index == i), method.Parameters[i].Type));
i++;
}
entityDecl.AddChild(body, Roles.Body);
}

9
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -238,13 +238,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -238,13 +238,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
void InsertVariableDeclarations()
{
var replacements = new List<KeyValuePair<AstNode, AstNode>>();
foreach (var v in variableDict.Values) {
foreach (var p in variableDict) {
var v = p.Value;
if (v.RemovedDueToCollision)
continue;
AstType type = context.TypeSystemAstBuilder.ConvertType(v.Type);
var boe = v.InsertionPoint.nextNode as BinaryOperatorExpression;
var boe = (v.InsertionPoint.nextNode as ExpressionStatement)?.Expression as AssignmentExpression;
if (boe != null && boe.Left.IsMatch(new IdentifierExpression(v.Name))) {
var vds = new VariableDeclarationStatement(type, v.Name, boe.Right.Detach());
@ -261,9 +262,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -261,9 +262,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (v.DefaultInitialization) {
initializer = new DefaultValueExpression(type.Clone());
}
var vds = new VariableDeclarationStatement(type, v.Name, initializer);
vds.Variables.Single().AddAnnotation(new ILVariableResolveResult(p.Key, p.Key.Type));
v.InsertionPoint.nextNode.Parent.InsertChildBefore(
v.InsertionPoint.nextNode,
new VariableDeclarationStatement(type, v.Name, initializer),
vds,
BlockStatement.StatementRole);
}
}

1
ILSpy/ILSpy.csproj

@ -164,6 +164,7 @@ @@ -164,6 +164,7 @@
<Compile Include="Languages\IResourceFileHandler.cs" />
<Compile Include="Languages\Language.cs" />
<Compile Include="Languages\Languages.cs" />
<Compile Include="Languages\TextTokenWriter.cs" />
<Compile Include="LoadedAssembly.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="NavigationHistory.cs" />

19
ILSpy/Languages/CSharpLanguage.cs

@ -96,16 +96,19 @@ namespace ICSharpCode.ILSpy @@ -96,16 +96,19 @@ namespace ICSharpCode.ILSpy
return decompiler;
}
void WriteCode(ITextOutput output, SyntaxTree syntaxTree)
void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree)
{
output.WriteLine(syntaxTree.ToString());
syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
var outputFormatter = new TextTokenWriter(output, settings) { FoldBraces = settings.FoldBraces };
var formattingPolicy = settings.CSharpFormattingOptions;
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, formattingPolicy));
}
public override void DecompileMethod(MethodDefinition method, ITextOutput output, DecompilationOptions options)
{
WriteCommentLine(output, TypeToString(method.DeclaringType, includeNamespace: true));
CSharpDecompiler decompiler = CreateDecompiler(method.Module, options);
WriteCode(output, decompiler.Decompile(method));
WriteCode(output, options.DecompilerSettings, decompiler.Decompile(method));
/*
AstBuilder codeDomBuilder = CreateAstBuilder(options, currentType: method.DeclaringType, isSingleMember: true);
if (method.IsConstructor && !method.IsStatic && !method.DeclaringType.IsValueType) {
@ -160,7 +163,7 @@ namespace ICSharpCode.ILSpy @@ -160,7 +163,7 @@ namespace ICSharpCode.ILSpy
{
WriteCommentLine(output, TypeToString(property.DeclaringType, includeNamespace: true));
CSharpDecompiler decompiler = CreateDecompiler(property.Module, options);
WriteCode(output, decompiler.Decompile(property));
WriteCode(output, options.DecompilerSettings, decompiler.Decompile(property));
}
/*
public override void DecompileField(FieldDefinition field, ITextOutput output, DecompilationOptions options)
@ -215,14 +218,14 @@ namespace ICSharpCode.ILSpy @@ -215,14 +218,14 @@ namespace ICSharpCode.ILSpy
{
WriteCommentLine(output, TypeToString(ev.DeclaringType, includeNamespace: true));
CSharpDecompiler decompiler = CreateDecompiler(ev.Module, options);
WriteCode(output, decompiler.Decompile(ev));
WriteCode(output, options.DecompilerSettings, decompiler.Decompile(ev));
}
public override void DecompileType(TypeDefinition type, ITextOutput output, DecompilationOptions options)
{
WriteCommentLine(output, TypeToString(type, includeNamespace: true));
CSharpDecompiler decompiler = CreateDecompiler(type.Module, options);
WriteCode(output, decompiler.Decompile(type));
WriteCode(output, options.DecompilerSettings, decompiler.Decompile(type));
}
/*
@ -339,8 +342,8 @@ namespace ICSharpCode.ILSpy @@ -339,8 +342,8 @@ namespace ICSharpCode.ILSpy
{
if (fileName.EndsWith(".resource", StringComparison.OrdinalIgnoreCase)) {
using (ResourceReader reader = new ResourceReader(entryStream))
using (FileStream fs = new FileStream(Path.Combine(targetDirectory, fileName), FileMode.Create, FileAccess.Write))
using (ResXResourceWriter writer = new ResXResourceWriter(fs)) {
using (FileStream fs = new FileStream(Path.Combine(targetDirectory, fileName), FileMode.Create, FileAccess.Write))
using (ResXResourceWriter writer = new ResXResourceWriter(fs)) {
foreach (DictionaryEntry entry in reader) {
writer.AddResource((string)entry.Key, entry.Value);
}

360
ILSpy/Languages/TextTokenWriter.cs

@ -0,0 +1,360 @@ @@ -0,0 +1,360 @@
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.CSharp;
using Mono.Cecil;
namespace ICSharpCode.ILSpy
{
public class TextTokenWriter : TokenWriter
{
readonly ITextOutput output;
readonly DecompilerSettings settings;
readonly Stack<AstNode> nodeStack = new Stack<AstNode>();
int braceLevelWithinType = -1;
bool inDocumentationComment = false;
bool firstUsingDeclaration;
bool lastUsingDeclaration;
TextLocation? lastEndOfLine;
public bool FoldBraces = false;
public TextTokenWriter(ITextOutput output, DecompilerSettings settings)
{
if (output == null)
throw new ArgumentNullException("output");
if (settings == null)
throw new ArgumentNullException("settings");
this.output = output;
this.settings = settings;
}
public override void WriteIdentifier(Identifier identifier)
{
if (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier)) {
output.Write('@');
}
var definition = GetCurrentDefinition();
if (definition != null) {
output.WriteDefinition(identifier.Name, definition, false);
return;
}
object memberRef = GetCurrentMemberReference();
if (memberRef != null) {
output.WriteReference(identifier.Name, memberRef);
return;
}
definition = GetCurrentLocalDefinition();
if (definition != null) {
output.WriteDefinition(identifier.Name, definition);
return;
}
memberRef = GetCurrentLocalReference();
if (memberRef != null) {
output.WriteReference(identifier.Name, memberRef, true);
return;
}
if (firstUsingDeclaration) {
output.MarkFoldStart(defaultCollapsed: true);
firstUsingDeclaration = false;
}
output.Write(identifier.Name);
}
MemberReference GetCurrentMemberReference()
{
AstNode node = nodeStack.Peek();
MemberReference memberRef = node.Annotation<MemberReference>();
if (memberRef == null && node.Role == Roles.TargetExpression && (node.Parent is InvocationExpression || node.Parent is ObjectCreateExpression)) {
memberRef = node.Parent.Annotation<MemberReference>();
}
if (node is IdentifierExpression && node.Role == Roles.TargetExpression && node.Parent is InvocationExpression && memberRef != null) {
var declaringType = memberRef.DeclaringType.Resolve();
if (declaringType != null && declaringType.IsDelegate())
return null;
}
return FilterMemberReference(memberRef);
}
MemberReference FilterMemberReference(MemberReference memberRef)
{
if (memberRef == null)
return null;
if (settings.AutomaticEvents && memberRef is FieldDefinition) {
var field = (FieldDefinition)memberRef;
return field.DeclaringType.Events.FirstOrDefault(ev => ev.Name == field.Name) ?? memberRef;
}
return memberRef;
}
object GetCurrentLocalReference()
{
AstNode node = nodeStack.Peek();
ILVariable variable = node.Annotation<ILVariableResolveResult>()?.Variable;
if (variable != null)
return variable;
var gotoStatement = node as GotoStatement;
if (gotoStatement != null)
{
var method = nodeStack.Select(nd => nd.Annotation<MethodReference>()).FirstOrDefault(mr => mr != null);
if (method != null)
return method.ToString() + gotoStatement.Label;
}
return null;
}
object GetCurrentLocalDefinition()
{
AstNode node = nodeStack.Peek();
if (node is Identifier && node.Parent != null)
node = node.Parent;
if (node is ParameterDeclaration || node is VariableInitializer || node is CatchClause || node is ForeachStatement) {
var variable = node.Annotation<ILVariableResolveResult>()?.Variable;
if (variable != null)
return variable;
}
var label = node as LabelStatement;
if (label != null) {
var method = nodeStack.Select(nd => nd.Annotation<MethodReference>()).FirstOrDefault(mr => mr != null);
if (method != null)
return method.ToString() + label.Label;
}
return null;
}
object GetCurrentDefinition()
{
if (nodeStack == null || nodeStack.Count == 0)
return null;
var node = nodeStack.Peek();
if (node is Identifier)
node = node.Parent;
if (IsDefinition(node))
return node.Annotation<MemberReference>();
return null;
}
public override void WriteKeyword(Role role, string keyword)
{
output.Write(keyword);
}
public override void WriteToken(Role role, string token)
{
// Attach member reference to token only if there's no identifier in the current node.
MemberReference memberRef = GetCurrentMemberReference();
var node = nodeStack.Peek();
if (memberRef != null && node.GetChildByRole(Roles.Identifier).IsNull)
output.WriteReference(token, memberRef);
else
output.Write(token);
}
public override void Space()
{
output.Write(' ');
}
public void OpenBrace(BraceStyle style)
{
if (braceLevelWithinType >= 0 || nodeStack.Peek() is TypeDeclaration)
braceLevelWithinType++;
if (nodeStack.OfType<BlockStatement>().Count() <= 1 || FoldBraces) {
output.MarkFoldStart(defaultCollapsed: braceLevelWithinType == 1);
}
output.WriteLine();
output.WriteLine("{");
output.Indent();
}
public void CloseBrace(BraceStyle style)
{
output.Unindent();
output.Write('}');
if (nodeStack.OfType<BlockStatement>().Count() <= 1 || FoldBraces)
output.MarkFoldEnd();
if (braceLevelWithinType >= 0)
braceLevelWithinType--;
}
public override void Indent()
{
output.Indent();
}
public override void Unindent()
{
output.Unindent();
}
public override void NewLine()
{
if (lastUsingDeclaration) {
output.MarkFoldEnd();
lastUsingDeclaration = false;
}
// lastEndOfLine = output.Location;
output.WriteLine();
}
public override void WriteComment(CommentType commentType, string content)
{
switch (commentType) {
case CommentType.SingleLine:
output.Write("//");
output.WriteLine(content);
break;
case CommentType.MultiLine:
output.Write("/*");
output.Write(content);
output.Write("*/");
break;
case CommentType.Documentation:
bool isLastLine = !(nodeStack.Peek().NextSibling is Comment);
if (!inDocumentationComment && !isLastLine) {
inDocumentationComment = true;
output.MarkFoldStart("///" + content, true);
}
output.Write("///");
output.Write(content);
if (inDocumentationComment && isLastLine) {
inDocumentationComment = false;
output.MarkFoldEnd();
}
output.WriteLine();
break;
default:
output.Write(content);
break;
}
}
public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
{
// pre-processor directive must start on its own line
output.Write('#');
output.Write(type.ToString().ToLowerInvariant());
if (!string.IsNullOrEmpty(argument)) {
output.Write(' ');
output.Write(argument);
}
output.WriteLine();
}
public override void WritePrimitiveValue(object value, string literalValue = null)
{
new TextWriterTokenWriter(new TextOutputWriter(output)).WritePrimitiveValue(value, literalValue);
}
public override void WritePrimitiveType(string type)
{
output.Write(type);
if (type == "new") {
output.Write("()");
}
}
// Stack<TextLocation> startLocations = new Stack<TextLocation>();
// Stack<MethodDebugSymbols> symbolsStack = new Stack<MethodDebugSymbols>();
public override void StartNode(AstNode node)
{
if (nodeStack.Count == 0) {
if (IsUsingDeclaration(node)) {
firstUsingDeclaration = !IsUsingDeclaration(node.PrevSibling);
lastUsingDeclaration = !IsUsingDeclaration(node.NextSibling);
} else {
firstUsingDeclaration = false;
lastUsingDeclaration = false;
}
}
nodeStack.Push(node);
// startLocations.Push(output.Location);
if (node is EntityDeclaration && node.Annotation<MemberReference>() != null && node.GetChildByRole(Roles.Identifier).IsNull)
output.WriteDefinition("", node.Annotation<MemberReference>(), false);
// if (node.Annotation<MethodDebugSymbols>() != null) {
// symbolsStack.Push(node.Annotation<MethodDebugSymbols>());
// symbolsStack.Peek().StartLocation = startLocations.Peek();
// }
}
private bool IsUsingDeclaration(AstNode node)
{
return node is UsingDeclaration || node is UsingAliasDeclaration;
}
public override void EndNode(AstNode node)
{
if (nodeStack.Pop() != node)
throw new InvalidOperationException();
// var startLocation = startLocations.Pop();
//
// // code mappings
// var ranges = node.Annotation<List<ILRange>>();
// if (symbolsStack.Count > 0 && ranges != null && ranges.Count > 0) {
// // Ignore the newline which was printed at the end of the statement
// TextLocation endLocation = (node is Statement) ? (lastEndOfLine ?? output.Location) : output.Location;
// symbolsStack.Peek().SequencePoints.Add(
// new SequencePoint() {
// ILRanges = ILRange.OrderAndJoin(ranges).ToArray(),
// StartLocation = startLocation,
// EndLocation = endLocation
// });
// }
//
// if (node.Annotation<MethodDebugSymbols>() != null) {
// symbolsStack.Peek().EndLocation = output.Location;
// output.AddDebugSymbols(symbolsStack.Pop());
// }
}
private static bool IsDefinition(AstNode node)
{
return node is EntityDeclaration
|| (node is VariableInitializer && node.Parent is FieldDeclaration)
|| node is FixedVariableInitializer;
}
}
}

6
ILSpy/TextView/AvalonEditTextOutput.cs

@ -202,7 +202,7 @@ namespace ICSharpCode.ILSpy.TextView @@ -202,7 +202,7 @@ namespace ICSharpCode.ILSpy.TextView
}
}
public void WriteDefinition(string text, object definition, bool isLocal)
public void WriteDefinition(string text, object definition, bool isLocal = true)
{
WriteIndent();
int start = this.TextLength;
@ -212,7 +212,7 @@ namespace ICSharpCode.ILSpy.TextView @@ -212,7 +212,7 @@ namespace ICSharpCode.ILSpy.TextView
references.Add(new ReferenceSegment { StartOffset = start, EndOffset = end, Reference = definition, IsLocal = isLocal, IsLocalTarget = true });
}
public void WriteReference(string text, object reference, bool isLocal)
public void WriteReference(string text, object reference, bool isLocal = false)
{
WriteIndent();
int start = this.TextLength;
@ -221,7 +221,7 @@ namespace ICSharpCode.ILSpy.TextView @@ -221,7 +221,7 @@ namespace ICSharpCode.ILSpy.TextView
references.Add(new ReferenceSegment { StartOffset = start, EndOffset = end, Reference = reference, IsLocal = isLocal });
}
public void MarkFoldStart(string collapsedText, bool defaultCollapsed)
public void MarkFoldStart(string collapsedText = "...", bool defaultCollapsed = false)
{
WriteIndent();
openFoldings.Push(

Loading…
Cancel
Save