Skip to content

Commit

Permalink
Add support for local functions #107
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir L. Boulema committed Sep 11, 2021
1 parent b5baf0f commit 3f65c4d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
1 change: 0 additions & 1 deletion CodeNav.Shared/CodeViewUserControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public async Task RegisterDocumentEvents()
private void DocumentEvents_Opened(string obj)
{
RegisterDocumentEvents().FireAndForget();
UpdateDocument();
}

private void WindowEvents_ActiveFrameChanged(ActiveFrameChangeEventArgs obj)
Expand Down
19 changes: 0 additions & 19 deletions CodeNav.Shared/Helpers/VisibilityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Windows;
using System.Windows.Controls;
using CodeNav.Models;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;

namespace CodeNav.Helpers
Expand Down Expand Up @@ -223,24 +222,6 @@ private static bool ShouldBeVisible(CodeItem item, string name = "",
return visible;
}

public static bool ShouldBeVisible(CodeItemKindEnum kind)
{
if (SettingsHelper.FilterRules == null)
{
return true;
}

var filterRule = SettingsHelper.FilterRules
.LastOrDefault(f => f.Kind == kind || f.Kind == CodeItemKindEnum.All);

if (filterRule == null)
{
return true;
}

return filterRule.Visible;
}

public static Visibility GetIgnoreVisibility(CodeItem item)
{
var filterRule = GetFilterRule(item);
Expand Down
51 changes: 37 additions & 14 deletions CodeNav.Shared/Mappers/MethodMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.VisualStudio.Imaging;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using VisualBasicSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;

Expand All @@ -13,37 +14,57 @@ public static class MethodMapper
{
public static CodeItem MapMethod(MethodDeclarationSyntax member, ICodeViewUserControl control, SemanticModel semanticModel)
{
if (member == null) return null;
return MapMethod(member, member.Identifier, member.Modifiers, member.Body, member.ReturnType as ITypeSymbol, member.ParameterList,
CodeItemKindEnum.Method, control, semanticModel);
}

public static CodeItem MapMethod(LocalFunctionStatementSyntax member, ICodeViewUserControl control, SemanticModel semanticModel)
{
return MapMethod(member, member.Identifier, member.Modifiers, member.Body, member.ReturnType as ITypeSymbol, member.ParameterList,
CodeItemKindEnum.LocalFunction, control, semanticModel);
}

private static CodeItem MapMethod(SyntaxNode node, SyntaxToken identifier,
SyntaxTokenList modifiers, BlockSyntax body, ITypeSymbol returnType,
ParameterListSyntax parameterList, CodeItemKindEnum kind,
ICodeViewUserControl control, SemanticModel semanticModel)
{
if (node == null)
{
return null;
}

CodeItem item;

var statementsCodeItems = StatementMapper.MapStatement(member.Body, control, semanticModel);
var statementsCodeItems = StatementMapper.MapStatement(body, control, semanticModel);

VisibilityHelper.SetCodeItemVisibility(statementsCodeItems);

if (VisibilityHelper.ShouldBeVisible(CodeItemKindEnum.Switch) && statementsCodeItems.Any())
if (statementsCodeItems.Any(statement => statement.IsVisible == Visibility.Visible))
{
// Map method as item containing statements
item = BaseMapper.MapBase<CodeClassItem>(member, member.Identifier, member.Modifiers, control, semanticModel);
item = BaseMapper.MapBase<CodeClassItem>(node, identifier,modifiers, control, semanticModel);
((CodeClassItem)item).Members.AddRange(statementsCodeItems);
((CodeClassItem)item).BorderColor = Colors.DarkGray;
}
else
{
// Map method as single item
item = BaseMapper.MapBase<CodeFunctionItem>(member, member.Identifier, member.Modifiers, control, semanticModel);
((CodeFunctionItem)item).Type = TypeMapper.Map(member.ReturnType);
((CodeFunctionItem)item).Parameters = ParameterMapper.MapParameters(member.ParameterList);
item.Tooltip = TooltipMapper.Map(item.Access, ((CodeFunctionItem)item).Type, item.Name, member.ParameterList);
item = BaseMapper.MapBase<CodeFunctionItem>(node, identifier, modifiers, control, semanticModel);
((CodeFunctionItem)item).Type = TypeMapper.Map(returnType);
((CodeFunctionItem)item).Parameters = ParameterMapper.MapParameters(parameterList);
item.Tooltip = TooltipMapper.Map(item.Access, ((CodeFunctionItem)item).Type, item.Name, parameterList);
}

item.Id = IdMapper.MapId(item.FullName, member.ParameterList);
item.Kind = CodeItemKindEnum.Method;
item.Id = IdMapper.MapId(item.FullName, parameterList);
item.Kind = kind;
item.Moniker = IconMapper.MapMoniker(item.Kind, item.Access);

if (TriviaSummaryMapper.HasSummary(member) && SettingsHelper.UseXMLComments)
if (TriviaSummaryMapper.HasSummary(node) && SettingsHelper.UseXMLComments)
{
item.Tooltip = TriviaSummaryMapper.Map(member);
item.Tooltip = TriviaSummaryMapper.Map(node);
}

return item;
}

Expand Down Expand Up @@ -73,7 +94,9 @@ public static CodeItem MapMethod(VisualBasicSyntax.MethodBlockSyntax member, ICo

var statementsCodeItems = StatementMapper.MapStatement(member.Statements, control, semanticModel);

if (VisibilityHelper.ShouldBeVisible(CodeItemKindEnum.Switch) && statementsCodeItems.Any())
VisibilityHelper.SetCodeItemVisibility(statementsCodeItems);

if (statementsCodeItems.Any(statement => statement.IsVisible == Visibility.Visible))
{
// Map method as item containing statements
item = BaseMapper.MapBase<CodeClassItem>(member, member.SubOrFunctionStatement.Identifier,
Expand Down
13 changes: 10 additions & 3 deletions CodeNav.Shared/Mappers/StatementMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CodeNav.Models;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using CodeNav.Helpers;
using Microsoft.CodeAnalysis;
using System.Collections.Generic;
using VisualBasicSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
Expand All @@ -17,7 +16,10 @@ public static class StatementMapper
{
public static List<CodeItem> MapStatement(StatementSyntax statement, ICodeViewUserControl control, SemanticModel semanticModel)
{
if (statement == null) return new List<CodeItem>();
if (statement == null)
{
return new List<CodeItem>();
}

switch (statement.Kind())
{
Expand All @@ -27,6 +29,8 @@ public static List<CodeItem> MapStatement(StatementSyntax statement, ICodeViewUs
return MapStatements((statement as BlockSyntax).Statements, control, semanticModel);
case SyntaxKind.TryStatement:
return MapStatement((statement as TryStatementSyntax).Block, control, semanticModel);
case SyntaxKind.LocalFunctionStatement:
return new List<CodeItem> { MethodMapper.MapMethod(statement as LocalFunctionStatementSyntax, control, semanticModel) };
default:
return new List<CodeItem>();
}
Expand Down Expand Up @@ -54,7 +58,10 @@ public static List<CodeItem> MapStatements(SyntaxList<StatementSyntax> statement
{
var list = new List<CodeItem>();

if (!statements.Any()) return list;
if (!statements.Any())
{
return list;
}

foreach (var statement in statements)
{
Expand Down
7 changes: 4 additions & 3 deletions CodeNav.Shared/Models/CodeItemKindEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum CodeItemKindEnum
{
Unknown,
All,
Class,
Class,
Constant,
Constructor,
Delegate,
Expand All @@ -13,14 +13,15 @@ public enum CodeItemKindEnum
Event,
ImplementedInterface,
Indexer,
Interface,
Interface,
Method,
Namespace,
Property,
Region,
Struct,
Switch,
SwitchSection,
Variable
Variable,
LocalFunction
}
}

0 comments on commit 3f65c4d

Please sign in to comment.