Skip to content

Commit aeeb056

Browse files
authored
Add support for local function indexing in C# (#37)
1 parent 34600eb commit aeeb056

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

ScipDotnet/ScipCSharpSyntaxWalker.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
112112
base.VisitMethodDeclaration(node);
113113
}
114114

115+
public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
116+
{
117+
_scipDocumentIndexer.VisitOccurrence(_semanticModel.GetDeclaredSymbol(node), node.Identifier.GetLocation(), true);
118+
base.VisitLocalFunctionStatement(node);
119+
}
120+
115121
public override void VisitParameter(ParameterSyntax node)
116122
{
117123
_scipDocumentIndexer.VisitOccurrence(_semanticModel.GetDeclaredSymbol(node), node.Identifier.GetLocation(), true);

ScipDotnet/ScipDocumentIndexer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ private static bool IsLocalSymbol(ISymbol sym)
371371
// Anonymous classes/methods have empty names and can not be accessed outside their file.
372372
// The "global namespace" (parent of all namespaces) also has an empty name and should not
373373
// be treated as a local variable.
374+
sym is IMethodSymbol { MethodKind: MethodKind.LocalFunction } ||
374375
(sym.Name.Equals("") && sym.Kind != SymbolKind.Namespace);
375376
}
376377
}

snapshots/input/syntax/Main/Methods.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,19 @@ public static void InheritedOverloads()
9595
new InheritedOverloads3().Method("42");
9696
}
9797

98+
public class LocalFunction
99+
{
100+
public static void Method()
101+
{
102+
var myWorld = GetWorld();
103+
SayHi(myWorld);
104+
105+
string GetWorld() => "world";
106+
107+
void SayHi(string world)
108+
{
109+
Console.WriteLine($"Hello {world}!");
110+
}
111+
}
112+
}
98113
}

snapshots/output-net6.0/syntax/Main/Methods.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,35 @@ public static void InheritedOverloads()
210210
// ^^^^^^ reference scip-dotnet nuget . . Main/Methods#InheritedOverloads3#Method().
211211
}
212212

213+
public class LocalFunction
214+
// ^^^^^^^^^^^^^ definition scip-dotnet nuget . . Main/Methods#LocalFunction#
215+
// documentation ```cs\nclass LocalFunction\n```
216+
{
217+
public static void Method()
218+
// ^^^^^^ definition scip-dotnet nuget . . Main/Methods#LocalFunction#Method().
219+
// documentation ```cs\npublic static void LocalFunction.Method()\n```
220+
{
221+
var myWorld = GetWorld();
222+
// ^^^^^^^ definition local 0
223+
// documentation ```cs\nstring? myWorld\n```
224+
// ^^^^^^^^ reference local 1
225+
SayHi(myWorld);
226+
// ^^^^^ reference local 2
227+
// ^^^^^^^ reference local 0
228+
229+
string GetWorld() => "world";
230+
// ^^^^^^^^ definition local 1
231+
// documentation ```cs\nstring GetWorld()\n```
232+
233+
void SayHi(string world)
234+
// ^^^^^ definition local 2
235+
// documentation ```cs\nvoid SayHi(string world)\n```
236+
// ^^^^^ definition local 3
237+
// documentation ```cs\nstring world\n```
238+
{
239+
Console.WriteLine($"Hello {world}!");
240+
// ^^^^^ reference local 3
241+
}
242+
}
243+
}
213244
}

snapshots/output-net7.0/syntax/Main/Methods.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,37 @@ public static void InheritedOverloads()
214214
// ^^^^^^ reference scip-dotnet nuget . . Main/Methods#InheritedOverloads3#Method().
215215
}
216216

217+
public class LocalFunction
218+
// ^^^^^^^^^^^^^ definition scip-dotnet nuget . . Main/Methods#LocalFunction#
219+
// documentation ```cs\nclass LocalFunction\n```
220+
{
221+
public static void Method()
222+
// ^^^^^^ definition scip-dotnet nuget . . Main/Methods#LocalFunction#Method().
223+
// documentation ```cs\npublic static void LocalFunction.Method()\n```
224+
{
225+
var myWorld = GetWorld();
226+
// ^^^^^^^ definition local 0
227+
// documentation ```cs\nstring? myWorld\n```
228+
// ^^^^^^^^ reference local 1
229+
SayHi(myWorld);
230+
// ^^^^^ reference local 2
231+
// ^^^^^^^ reference local 0
232+
233+
string GetWorld() => "world";
234+
// ^^^^^^^^ definition local 1
235+
// documentation ```cs\nstring GetWorld()\n```
236+
237+
void SayHi(string world)
238+
// ^^^^^ definition local 2
239+
// documentation ```cs\nvoid SayHi(string world)\n```
240+
// ^^^^^ definition local 3
241+
// documentation ```cs\nstring world\n```
242+
{
243+
Console.WriteLine($"Hello {world}!");
244+
// ^^^^^^^ reference scip-dotnet nuget System.Console 7.0.0.0 System/Console#
245+
// ^^^^^^^^^ reference scip-dotnet nuget System.Console 7.0.0.0 System/Console#WriteLine(+11).
246+
// ^^^^^ reference local 3
247+
}
248+
}
249+
}
217250
}

0 commit comments

Comments
 (0)