Skip to content

Commit 8b45db6

Browse files
committed
Fix workspaceSymbol , enum can be infer when as key, add some snippet
1 parent e559d65 commit 8b45db6

File tree

18 files changed

+221
-56
lines changed

18 files changed

+221
-56
lines changed

EmmyLua.LanguageServer/Completion/CompleteProvider/DocProvider.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using EmmyLua.CodeAnalysis.Syntax.Kind;
1+
using System.Text;
2+
using EmmyLua.CodeAnalysis.Syntax.Kind;
23
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
34
using EmmyLua.CodeAnalysis.Type;
45
using EmmyLua.LanguageServer.Framework.Protocol.Message.Completion;
@@ -24,6 +25,7 @@ public void AddCompletion(CompleteContext context)
2425
case { Kind: LuaTokenKind.TkDocStart or LuaTokenKind.TkTagOther }:
2526
{
2627
AddTagCompletion(context);
28+
AddTagParamReturn(context);
2729
break;
2830
}
2931
case LuaNameToken { Parent: LuaDocTagParamSyntax paramSyntax }:
@@ -69,6 +71,59 @@ private void AddTagCompletion(CompleteContext context)
6971
context.StopHere();
7072
}
7173

74+
private void AddTagParamReturn(CompleteContext context)
75+
{
76+
var commentSyntax = context.TriggerToken?.Ancestors.OfType<LuaCommentSyntax>().FirstOrDefault();
77+
if (commentSyntax is null)
78+
{
79+
return;
80+
}
81+
82+
var funcStat = commentSyntax.Owner as LuaFuncStatSyntax;
83+
if (funcStat is null)
84+
{
85+
return;
86+
}
87+
88+
var paramList = funcStat.ClosureExpr?.ParamList;
89+
if (paramList is not null)
90+
{
91+
var paramNameList = new List<string>();
92+
foreach (var param in paramList.Params)
93+
{
94+
if (param.IsVarArgs)
95+
{
96+
paramNameList.Add("...");
97+
}
98+
else
99+
{
100+
paramNameList.Add(param.Name?.RepresentText ?? "");
101+
}
102+
}
103+
104+
var sb = new StringBuilder();
105+
for (var i = 0; i < paramNameList.Count; i++)
106+
{
107+
var paramName = paramNameList[i];
108+
if (i == 0)
109+
{
110+
sb.Append($"param {paramName} ${{{i + 1}:any}}\n");
111+
}
112+
else
113+
{
114+
sb.Append($"---@param {paramName} ${{{i + 1}:any}}\n");
115+
}
116+
}
117+
118+
sb.Append("---@return ${0:any}");
119+
120+
context.CreateSnippet("param;@return")
121+
.WithDetail(" ( ... )")
122+
.WithInsertText(sb.ToString())
123+
.AddToContext();
124+
}
125+
}
126+
72127
private void AddParamNameCompletion(LuaDocTagParamSyntax paramSyntax, CompleteContext context)
73128
{
74129
var comment = paramSyntax.Ancestors.OfType<LuaCommentSyntax>().FirstOrDefault();
@@ -103,6 +158,7 @@ private void AddTypeNameCompletion(string prefix, CompleteContext context)
103158
{
104159
prefixNamespace = prefix[..dotIndex];
105160
}
161+
106162
var namespaceOrTypes =
107163
context.SemanticModel.Compilation.TypeManager.GetNamespaceOrTypeInfos(prefixNamespace,
108164
context.SemanticModel.Document.Id);

EmmyLua.LanguageServer/Completion/SnippetBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void AddToContext()
6363
InsertTextMode = InsertTextMode.AdjustIndentation,
6464
TextEdit = TextEdit,
6565
AdditionalTextEdits = AdditionalTextEdit,
66-
Kind = CompletionItemKind.Event,
66+
Kind = CompletionItemKind.Snippet,
6767
InsertTextFormat = InsertTextFormat.Snippet,
6868
});
6969
}

EmmyLua.LanguageServer/EmmyLua.LanguageServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<PackageReference Include="EmmyLua.LanguageServer.Framework" Version="0.6.0" />
28+
<PackageReference Include="EmmyLua.LanguageServer.Framework" Version="0.6.1" />
2929
</ItemGroup>
3030

3131
<ItemGroup>

EmmyLua.LanguageServer/InlineValues/InlineValuesHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ protected override Task<InlineValueResponse> Handle(InlineValueParams inlineValu
2020
var semanticModel = context.GetSemanticModel(uri);
2121
if (semanticModel is not null)
2222
{
23-
var result = Builder.Build(semanticModel, inlineValueParams.Range);
23+
var range = inlineValueParams.Range;
24+
var stopRangePosition = inlineValueParams.Context.StoppedLocation.End;
25+
var validRange = range with { End = stopRangePosition };
26+
27+
var result = Builder.Build(semanticModel, validRange);
2428
container = new InlineValueResponse(result);
2529
}
2630
});

EmmyLua.LanguageServer/SemanticToken/SemanticTokenHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public class SemanticTokenHandler(ServerContext context) : SemanticTokensHandler
1616
{
1717
var uri = semanticTokensParams.TextDocument.Uri.UnescapeUri;
1818
SemanticTokens? semanticTokens = null;
19+
if (!context.SettingManager.Setting.SemanticTokens.Enable)
20+
{
21+
return Task.FromResult(semanticTokens);
22+
}
23+
1924
context.ReadyRead(() =>
2025
{
2126
var semanticModel = context.LuaProject.Compilation.GetSemanticModel(uri);
@@ -49,7 +54,8 @@ public class SemanticTokenHandler(ServerContext context) : SemanticTokensHandler
4954
{
5055
semanticTokens = new()
5156
{
52-
Data = Analyzer.TokenizeByRange(semanticModel, context.IsVscode, semanticTokensRangeParams.Range, cancellationToken)
57+
Data = Analyzer.TokenizeByRange(semanticModel, context.IsVscode, semanticTokensRangeParams.Range,
58+
cancellationToken)
5359
};
5460
}
5561
});

EmmyLua.LanguageServer/WorkspaceSymbol/WorkspaceSymbolBuilder.cs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using EmmyLua.CodeAnalysis.Compilation.Search;
2-
using EmmyLua.CodeAnalysis.Type;
2+
using EmmyLua.CodeAnalysis.Compilation.Symbol;
3+
using EmmyLua.CodeAnalysis.Syntax.Node;
34
using EmmyLua.LanguageServer.Framework.Protocol.Message.DocumentSymbol;
45
using EmmyLua.LanguageServer.Server;
56
using EmmyLua.LanguageServer.Util;
@@ -16,44 +17,35 @@ public class WorkspaceSymbolBuilder
1617
{
1718
var luaProject = context.LuaProject;
1819
var searchContext = new SearchContext(luaProject.Compilation, new SearchContextFeatures());
19-
var globals = context.LuaProject.Compilation.TypeManager.GetAllGlobalInfos();
20-
foreach (var global in globals)
20+
var namedElements = context.LuaProject.Compilation.Db.QueryNamedElements(searchContext);
21+
foreach (var pair in namedElements)
2122
{
22-
if (global.Name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
23+
var name = pair.Item1;
24+
if (name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
2325
{
2426
cancellationToken.ThrowIfCancellationRequested();
25-
var globalSymbol = global.MainLuaSymbol;
26-
if (globalSymbol is not null)
27+
var elementIds = pair.Item2;
28+
foreach (var elementId in elementIds)
2729
{
28-
var location = globalSymbol.GetLocation(searchContext)?.ToLspLocation();
29-
result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
30+
var document = luaProject.GetDocument(elementId.DocumentId);
31+
if (document is not null)
3032
{
31-
Name = global.Name,
32-
Kind = ToSymbolKind(globalSymbol.Type),
33-
Location = location
34-
});
33+
var ptr = new LuaElementPtr<LuaSyntaxElement>(elementId);
34+
if (ptr.ToNode(document) is { } node)
35+
{
36+
var location = node.Location.ToLspLocation();
37+
var declaration = searchContext.FindDeclaration(node);
38+
result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
39+
{
40+
Name = name,
41+
Kind = ToSymbolKind(declaration),
42+
Location = location
43+
});
44+
}
45+
}
3546
}
3647
}
3748
}
38-
39-
// var members = context.LuaProject.Compilation.TypeManager.GetAllTypeMembers();
40-
// foreach (var member in members)
41-
// {
42-
// if (member.Name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
43-
// {
44-
// cancellationToken.ThrowIfCancellationRequested();
45-
// var document = luaProject.GetDocument(member.DocumentId);
46-
// if (document is not null && member.Info.Ptr.ToNode(document) is { } node)
47-
// {
48-
// result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
49-
// {
50-
// Name = member.Name,
51-
// Kind = ToSymbolKind(member.Type),
52-
// Location = node.Range.ToLspLocation(document)
53-
// });
54-
// }
55-
// }
56-
// }
5749

5850
return result;
5951
}
@@ -63,12 +55,13 @@ public class WorkspaceSymbolBuilder
6355
}
6456
}
6557

66-
private static SymbolKind ToSymbolKind(LuaType? type)
58+
private static SymbolKind ToSymbolKind(LuaSymbol? luaSymbol)
6759
{
68-
return type switch
60+
return luaSymbol?.Info switch
6961
{
70-
LuaNamedType => SymbolKind.Variable,
71-
LuaMethodType => SymbolKind.Method,
62+
MethodInfo => SymbolKind.Method,
63+
ParamInfo => SymbolKind.TypeParameter,
64+
NamedTypeInfo => SymbolKind.Class,
7265
_ => SymbolKind.Variable
7366
};
7467
}

EmmyLua/CodeAnalysis/Compilation/Analyzer/ResolveAnalyzer/ResolveAnalyzer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ private void MergeType(UnResolvedSymbol unResolved, LuaExprSyntax luaExpr, LuaTy
310310
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
311311
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, type);
312312
}
313+
else if (type is LuaElementType elementType)
314+
{
315+
// same file use ref
316+
if (elementType.Id.DocumentId == declaration.DocumentId)
317+
{
318+
declaration.Type = elementType;
319+
}
320+
else
321+
{
322+
declaration.Type = new LuaElementType(declaration.UniqueId);
323+
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
324+
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, elementType);
325+
}
326+
}
313327
else
314328
{
315329
declaration.Type = type;

EmmyLua/CodeAnalysis/Compilation/Index/ProjectIndex.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,36 @@ public IEnumerable<LuaElementPtr<LuaExprSyntax>> QueryModuleReturns(LuaDocumentI
208208
return Source.Query(id);
209209
}
210210

211+
public IEnumerable<(string, List<SyntaxElementId>)> QueryNamedElements(SearchContext context)
212+
{
213+
foreach (var pair in NameExpr.QueryAllWithKey())
214+
{
215+
var name = pair.Key;
216+
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
217+
yield return (name, elements);
218+
}
219+
220+
foreach (var pair in TableField.QueryAllWithKey())
221+
{
222+
var name = pair.Key;
223+
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
224+
yield return (name, elements);
225+
}
226+
227+
foreach (var pair in NameType.QueryAllWithKey())
228+
{
229+
var name = pair.Key;
230+
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
231+
yield return (name, elements);
232+
}
233+
234+
foreach (var pair in MultiIndexExpr.QueryAllWithKey())
235+
{
236+
var name = pair.Key;
237+
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
238+
yield return (name, elements);
239+
}
240+
}
241+
211242
#endregion
212243
}

EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ private static LuaType InferGenericType(LuaDocGenericTypeSyntax genericType, Sea
161161
var typeArgs = genericType.GenericArgs.Select(context.Infer).ToList();
162162
if (genericType is { Name: { } name })
163163
{
164+
var nameText = name.RepresentText;
165+
if (nameText == "instance" && typeArgs.FirstOrDefault() is { } firstType)
166+
{
167+
return new InstanceType(firstType);
168+
}
169+
164170
return new LuaGenericType(genericType.DocumentId, name.RepresentText, typeArgs);
165171
}
166172

EmmyLua/CodeAnalysis/Compilation/Search/IndexMembers.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public class IndexMembers(SearchContext context)
7878
{
7979
if (declarations.TryGetValue(name, out var symbol))
8080
{
81-
return new LuaSymbol(symbol.Name, typeInfo.BaseType ?? symbol.Type, symbol.Info);
81+
var enumMemberType = new EnumInstanceType(namedType, typeInfo.BaseType ?? symbol.Type);
82+
return symbol.WithType(enumMemberType);
8283
}
8384
}
8485

@@ -374,6 +375,14 @@ public class IndexMembers(SearchContext context)
374375
return new LuaSymbol(string.Empty, op.Ret, new VirtualInfo());
375376
}
376377
}
378+
else if (keyType is EnumInstanceType { EnumType: { } enumType })
379+
{
380+
var op = context.GetBestMatchedIndexOperator(namedType, enumType);
381+
if (op is not null)
382+
{
383+
return new LuaSymbol(string.Empty, op.Ret, new VirtualInfo());
384+
}
385+
}
377386

378387
return null;
379388
}
@@ -443,7 +452,14 @@ public class IndexMembers(SearchContext context)
443452
if (indexExpr.IndexKeyExpr is { } indexKeyExpr)
444453
{
445454
var keyType = context.Infer(indexKeyExpr);
446-
if (keyType.SubTypeOf(type.GenericArgs[0], context))
455+
if (keyType is EnumInstanceType { EnumType: { } enumType })
456+
{
457+
if (enumType.SubTypeOf(type.GenericArgs[0], context))
458+
{
459+
return new LuaSymbol(string.Empty, type.GenericArgs[1], new VirtualInfo());
460+
}
461+
}
462+
else if (keyType.SubTypeOf(type.GenericArgs[0], context))
447463
{
448464
return new LuaSymbol(string.Empty, type.GenericArgs[1], new VirtualInfo());
449465
}

0 commit comments

Comments
 (0)