Skip to content

Commit

Permalink
Improve tooltips and highlighting of local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Jul 25, 2020
1 parent acea95d commit 67b2a45
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.Output;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;

namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
{
Expand Down Expand Up @@ -103,7 +104,7 @@ public void ConvertSymbol(ISymbol symbol, TokenWriter writer, CSharpFormattingOp

if (symbol is ITypeDefinition)
WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy);
else if (symbol is IMember)
else if (symbol is IMember && !(symbol is LocalFunctionMethod))
WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy);
else
writer.WriteIdentifier(Identifier.Create(symbol.Name));
Expand Down
63 changes: 34 additions & 29 deletions ICSharpCode.Decompiler/Output/TextTokenWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
using System;
using System.Collections.Generic;
using System.Linq;

using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;

namespace ICSharpCode.Decompiler
{
Expand All @@ -39,7 +41,7 @@ public class TextTokenWriter : TokenWriter
bool inDocumentationComment = false;
bool firstUsingDeclaration;
bool lastUsingDeclaration;

public TextTokenWriter(ITextOutput output, DecompilerSettings settings, IDecompilerTypeSystem typeSystem)
{
if (output == null)
Expand All @@ -52,13 +54,13 @@ public TextTokenWriter(ITextOutput output, DecompilerSettings settings, IDecompi
this.settings = settings;
this.typeSystem = typeSystem;
}

public override void WriteIdentifier(Identifier identifier)
{
if (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier)) {
output.Write('@');
}

var definition = GetCurrentDefinition();
string name = TextWriterTokenWriter.EscapeIdentifier(identifier.Name);
switch (definition) {
Expand All @@ -69,7 +71,7 @@ public override void WriteIdentifier(Identifier identifier)
output.WriteReference(m, name, true);
return;
}

var member = GetCurrentMemberReference();
switch (member) {
case IType t:
Expand Down Expand Up @@ -110,6 +112,7 @@ ISymbol GetCurrentMemberReference()
if (symbol != null && node.Role == Roles.Type && node.Parent is ObjectCreateExpression) {
symbol = node.Parent.GetSymbol();
}

if (node is IdentifierExpression && node.Role == Roles.TargetExpression && node.Parent is InvocationExpression && symbol is IMember member) {
var declaringType = member.DeclaringType;
if (declaringType != null && declaringType.Kind == TypeKind.Delegate)
Expand All @@ -123,10 +126,8 @@ ISymbol FilterMember(ISymbol symbol)
if (symbol == null)
return null;

//if (settings.AutomaticEvents && member is FieldDefinition) {
// var field = (FieldDefinition)member;
// return field.DeclaringType.Events.FirstOrDefault(ev => ev.Name == field.Name) ?? member;
//}
if (symbol is LocalFunctionMethod)
return null;

return symbol;
}
Expand All @@ -142,14 +143,18 @@ object GetCurrentLocalReference()
if (letClauseVariable != null)
return letClauseVariable;

var gotoStatement = node as GotoStatement;
if (gotoStatement != null)
{
if (node is GotoStatement gotoStatement) {
var method = nodeStack.Select(nd => nd.GetSymbol() as IMethod).FirstOrDefault(mr => mr != null);
if (method != null)
return method + gotoStatement.Label;
}

if (node.Role == Roles.TargetExpression && node.Parent is InvocationExpression) {
var symbol = node.Parent.GetSymbol();
if (symbol is LocalFunctionMethod)
return symbol;
}

return null;
}

Expand Down Expand Up @@ -177,29 +182,29 @@ object GetCurrentLocalDefinition()
return method + label.Label;
}

if (node is LocalFunctionDeclarationStatement) {
var localFunction = node.GetResolveResult() as MemberResolveResult;
if (node is MethodDeclaration && node.Parent is LocalFunctionDeclarationStatement) {
var localFunction = node.Parent.GetResolveResult() as MemberResolveResult;
if (localFunction != null)
return localFunction.Member;
}

return null;
}

ISymbol GetCurrentDefinition()
{
if (nodeStack == null || nodeStack.Count == 0)
return null;

var node = nodeStack.Peek();
if (node is Identifier)
node = node.Parent;
if (IsDefinition(ref node))
return node.GetSymbol();

return null;
}

public override void WriteKeyword(Role role, string keyword)
{
//To make reference for 'this' and 'base' keywords in the ClassName():this() expression
Expand All @@ -211,7 +216,7 @@ public override void WriteKeyword(Role role, string keyword)
}
output.Write(keyword);
}

public override void WriteToken(Role role, string token)
{
switch (token) {
Expand Down Expand Up @@ -253,22 +258,22 @@ public override void WriteToken(Role role, string token)
break;
}
}

public override void Space()
{
output.Write(' ');
}

public override void Indent()
{
output.Indent();
}

public override void Unindent()
{
output.Unindent();
}

public override void NewLine()
{
if (!firstUsingDeclaration && lastUsingDeclaration) {
Expand All @@ -277,7 +282,7 @@ public override void NewLine()
}
output.WriteLine();
}

public override void WriteComment(CommentType commentType, string content)
{
switch (commentType) {
Expand Down Expand Up @@ -309,7 +314,7 @@ public override void WriteComment(CommentType commentType, string content)
break;
}
}

public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
{
// pre-processor directive must start on its own line
Expand All @@ -321,7 +326,7 @@ public override void WritePreProcessorDirective(PreProcessorDirectiveType type,
}
output.WriteLine();
}

public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
{
new TextWriterTokenWriter(new TextOutputWriter(output)).WritePrimitiveValue(value, format);
Expand Down Expand Up @@ -376,7 +381,7 @@ public override void WritePrimitiveType(string type)
break;
}
}

public override void StartNode(AstNode node)
{
if (nodeStack.Count == 0) {
Expand All @@ -390,7 +395,7 @@ public override void StartNode(AstNode node)
}
nodeStack.Push(node);
}

private bool IsUsingDeclaration(AstNode node)
{
return node is UsingDeclaration || node is UsingAliasDeclaration;
Expand All @@ -401,10 +406,10 @@ public override void EndNode(AstNode node)
if (nodeStack.Pop() != node)
throw new InvalidOperationException();
}

public static bool IsDefinition(ref AstNode node)
{
if (node is EntityDeclaration)
if (node is EntityDeclaration && !(node.Parent is LocalFunctionDeclarationStatement))
return true;
if (node is VariableInitializer && node.Parent is FieldDeclaration) {
node = node.Parent;
Expand Down
3 changes: 3 additions & 0 deletions ILSpy/Languages/CSharpLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ public override RichText GetRichTextTooltip(IEntity entity)
if (!settings.LiftNullables) {
flags &= ~ConversionFlags.UseNullableSpecifierForValueTypes;
}
if (entity is IMethod m && m.IsLocalFunction) {
writer.WriteIdentifier(Identifier.Create("(local)"));
}
new CSharpAmbience() {
ConversionFlags = flags,
}.ConvertSymbol(entity, writer, settings.CSharpFormattingOptions);
Expand Down

0 comments on commit 67b2a45

Please sign in to comment.