Skip to content

Commit

Permalink
Fix workspaceSymbol , enum can be infer when as key, add some snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Aug 15, 2024
1 parent e559d65 commit 8b45db6
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EmmyLua.CodeAnalysis.Syntax.Kind;
using System.Text;
using EmmyLua.CodeAnalysis.Syntax.Kind;
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
using EmmyLua.CodeAnalysis.Type;
using EmmyLua.LanguageServer.Framework.Protocol.Message.Completion;
Expand All @@ -24,6 +25,7 @@ public void AddCompletion(CompleteContext context)
case { Kind: LuaTokenKind.TkDocStart or LuaTokenKind.TkTagOther }:
{
AddTagCompletion(context);
AddTagParamReturn(context);
break;
}
case LuaNameToken { Parent: LuaDocTagParamSyntax paramSyntax }:
Expand Down Expand Up @@ -69,6 +71,59 @@ private void AddTagCompletion(CompleteContext context)
context.StopHere();
}

private void AddTagParamReturn(CompleteContext context)
{
var commentSyntax = context.TriggerToken?.Ancestors.OfType<LuaCommentSyntax>().FirstOrDefault();
if (commentSyntax is null)
{
return;
}

var funcStat = commentSyntax.Owner as LuaFuncStatSyntax;
if (funcStat is null)
{
return;
}

var paramList = funcStat.ClosureExpr?.ParamList;
if (paramList is not null)
{
var paramNameList = new List<string>();
foreach (var param in paramList.Params)
{
if (param.IsVarArgs)
{
paramNameList.Add("...");
}
else
{
paramNameList.Add(param.Name?.RepresentText ?? "");
}
}

var sb = new StringBuilder();
for (var i = 0; i < paramNameList.Count; i++)
{
var paramName = paramNameList[i];
if (i == 0)
{
sb.Append($"param {paramName} ${{{i + 1}:any}}\n");
}
else
{
sb.Append($"---@param {paramName} ${{{i + 1}:any}}\n");
}
}

sb.Append("---@return ${0:any}");

context.CreateSnippet("param;@return")
.WithDetail(" ( ... )")
.WithInsertText(sb.ToString())
.AddToContext();
}
}

private void AddParamNameCompletion(LuaDocTagParamSyntax paramSyntax, CompleteContext context)
{
var comment = paramSyntax.Ancestors.OfType<LuaCommentSyntax>().FirstOrDefault();
Expand Down Expand Up @@ -103,6 +158,7 @@ private void AddTypeNameCompletion(string prefix, CompleteContext context)
{
prefixNamespace = prefix[..dotIndex];
}

var namespaceOrTypes =
context.SemanticModel.Compilation.TypeManager.GetNamespaceOrTypeInfos(prefixNamespace,
context.SemanticModel.Document.Id);
Expand Down
2 changes: 1 addition & 1 deletion EmmyLua.LanguageServer/Completion/SnippetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void AddToContext()
InsertTextMode = InsertTextMode.AdjustIndentation,
TextEdit = TextEdit,
AdditionalTextEdits = AdditionalTextEdit,
Kind = CompletionItemKind.Event,
Kind = CompletionItemKind.Snippet,
InsertTextFormat = InsertTextFormat.Snippet,
});
}
Expand Down
2 changes: 1 addition & 1 deletion EmmyLua.LanguageServer/EmmyLua.LanguageServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EmmyLua.LanguageServer.Framework" Version="0.6.0" />
<PackageReference Include="EmmyLua.LanguageServer.Framework" Version="0.6.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion EmmyLua.LanguageServer/InlineValues/InlineValuesHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ protected override Task<InlineValueResponse> Handle(InlineValueParams inlineValu
var semanticModel = context.GetSemanticModel(uri);
if (semanticModel is not null)
{
var result = Builder.Build(semanticModel, inlineValueParams.Range);
var range = inlineValueParams.Range;
var stopRangePosition = inlineValueParams.Context.StoppedLocation.End;
var validRange = range with { End = stopRangePosition };

var result = Builder.Build(semanticModel, validRange);
container = new InlineValueResponse(result);
}
});
Expand Down
8 changes: 7 additions & 1 deletion EmmyLua.LanguageServer/SemanticToken/SemanticTokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public class SemanticTokenHandler(ServerContext context) : SemanticTokensHandler
{
var uri = semanticTokensParams.TextDocument.Uri.UnescapeUri;
SemanticTokens? semanticTokens = null;
if (!context.SettingManager.Setting.SemanticTokens.Enable)
{
return Task.FromResult(semanticTokens);
}

context.ReadyRead(() =>
{
var semanticModel = context.LuaProject.Compilation.GetSemanticModel(uri);
Expand Down Expand Up @@ -49,7 +54,8 @@ public class SemanticTokenHandler(ServerContext context) : SemanticTokensHandler
{
semanticTokens = new()
{
Data = Analyzer.TokenizeByRange(semanticModel, context.IsVscode, semanticTokensRangeParams.Range, cancellationToken)
Data = Analyzer.TokenizeByRange(semanticModel, context.IsVscode, semanticTokensRangeParams.Range,
cancellationToken)
};
}
});
Expand Down
63 changes: 28 additions & 35 deletions EmmyLua.LanguageServer/WorkspaceSymbol/WorkspaceSymbolBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EmmyLua.CodeAnalysis.Compilation.Search;
using EmmyLua.CodeAnalysis.Type;
using EmmyLua.CodeAnalysis.Compilation.Symbol;
using EmmyLua.CodeAnalysis.Syntax.Node;
using EmmyLua.LanguageServer.Framework.Protocol.Message.DocumentSymbol;
using EmmyLua.LanguageServer.Server;
using EmmyLua.LanguageServer.Util;
Expand All @@ -16,44 +17,35 @@ public class WorkspaceSymbolBuilder
{
var luaProject = context.LuaProject;
var searchContext = new SearchContext(luaProject.Compilation, new SearchContextFeatures());
var globals = context.LuaProject.Compilation.TypeManager.GetAllGlobalInfos();
foreach (var global in globals)
var namedElements = context.LuaProject.Compilation.Db.QueryNamedElements(searchContext);
foreach (var pair in namedElements)
{
if (global.Name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
var name = pair.Item1;
if (name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
{
cancellationToken.ThrowIfCancellationRequested();
var globalSymbol = global.MainLuaSymbol;
if (globalSymbol is not null)
var elementIds = pair.Item2;
foreach (var elementId in elementIds)
{
var location = globalSymbol.GetLocation(searchContext)?.ToLspLocation();
result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
var document = luaProject.GetDocument(elementId.DocumentId);
if (document is not null)
{
Name = global.Name,
Kind = ToSymbolKind(globalSymbol.Type),
Location = location
});
var ptr = new LuaElementPtr<LuaSyntaxElement>(elementId);
if (ptr.ToNode(document) is { } node)
{
var location = node.Location.ToLspLocation();
var declaration = searchContext.FindDeclaration(node);
result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
{
Name = name,
Kind = ToSymbolKind(declaration),
Location = location
});
}
}
}
}
}

// var members = context.LuaProject.Compilation.TypeManager.GetAllTypeMembers();
// foreach (var member in members)
// {
// if (member.Name.StartsWith(query, StringComparison.OrdinalIgnoreCase))
// {
// cancellationToken.ThrowIfCancellationRequested();
// var document = luaProject.GetDocument(member.DocumentId);
// if (document is not null && member.Info.Ptr.ToNode(document) is { } node)
// {
// result.Add(new Framework.Protocol.Message.WorkspaceSymbol.WorkspaceSymbol()
// {
// Name = member.Name,
// Kind = ToSymbolKind(member.Type),
// Location = node.Range.ToLspLocation(document)
// });
// }
// }
// }

return result;
}
Expand All @@ -63,12 +55,13 @@ public class WorkspaceSymbolBuilder
}
}

private static SymbolKind ToSymbolKind(LuaType? type)
private static SymbolKind ToSymbolKind(LuaSymbol? luaSymbol)
{
return type switch
return luaSymbol?.Info switch
{
LuaNamedType => SymbolKind.Variable,
LuaMethodType => SymbolKind.Method,
MethodInfo => SymbolKind.Method,
ParamInfo => SymbolKind.TypeParameter,
NamedTypeInfo => SymbolKind.Class,
_ => SymbolKind.Variable
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ private void MergeType(UnResolvedSymbol unResolved, LuaExprSyntax luaExpr, LuaTy
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, type);
}
else if (type is LuaElementType elementType)
{
// same file use ref
if (elementType.Id.DocumentId == declaration.DocumentId)
{
declaration.Type = elementType;
}
else
{
declaration.Type = new LuaElementType(declaration.UniqueId);
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, elementType);
}
}
else
{
declaration.Type = type;
Expand Down
31 changes: 31 additions & 0 deletions EmmyLua/CodeAnalysis/Compilation/Index/ProjectIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,36 @@ public IEnumerable<LuaElementPtr<LuaExprSyntax>> QueryModuleReturns(LuaDocumentI
return Source.Query(id);
}

public IEnumerable<(string, List<SyntaxElementId>)> QueryNamedElements(SearchContext context)
{
foreach (var pair in NameExpr.QueryAllWithKey())
{
var name = pair.Key;
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
yield return (name, elements);
}

foreach (var pair in TableField.QueryAllWithKey())
{
var name = pair.Key;
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
yield return (name, elements);
}

foreach (var pair in NameType.QueryAllWithKey())
{
var name = pair.Key;
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
yield return (name, elements);
}

foreach (var pair in MultiIndexExpr.QueryAllWithKey())
{
var name = pair.Key;
var elements = pair.Value.Select(it => it.Element.UniqueId).ToList();
yield return (name, elements);
}
}

#endregion
}
6 changes: 6 additions & 0 deletions EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ private static LuaType InferGenericType(LuaDocGenericTypeSyntax genericType, Sea
var typeArgs = genericType.GenericArgs.Select(context.Infer).ToList();
if (genericType is { Name: { } name })
{
var nameText = name.RepresentText;
if (nameText == "instance" && typeArgs.FirstOrDefault() is { } firstType)
{
return new InstanceType(firstType);
}

return new LuaGenericType(genericType.DocumentId, name.RepresentText, typeArgs);
}

Expand Down
20 changes: 18 additions & 2 deletions EmmyLua/CodeAnalysis/Compilation/Search/IndexMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public class IndexMembers(SearchContext context)
{
if (declarations.TryGetValue(name, out var symbol))
{
return new LuaSymbol(symbol.Name, typeInfo.BaseType ?? symbol.Type, symbol.Info);
var enumMemberType = new EnumInstanceType(namedType, typeInfo.BaseType ?? symbol.Type);
return symbol.WithType(enumMemberType);
}
}

Expand Down Expand Up @@ -374,6 +375,14 @@ public class IndexMembers(SearchContext context)
return new LuaSymbol(string.Empty, op.Ret, new VirtualInfo());
}
}
else if (keyType is EnumInstanceType { EnumType: { } enumType })
{
var op = context.GetBestMatchedIndexOperator(namedType, enumType);
if (op is not null)
{
return new LuaSymbol(string.Empty, op.Ret, new VirtualInfo());
}
}

return null;
}
Expand Down Expand Up @@ -443,7 +452,14 @@ public class IndexMembers(SearchContext context)
if (indexExpr.IndexKeyExpr is { } indexKeyExpr)
{
var keyType = context.Infer(indexKeyExpr);
if (keyType.SubTypeOf(type.GenericArgs[0], context))
if (keyType is EnumInstanceType { EnumType: { } enumType })
{
if (enumType.SubTypeOf(type.GenericArgs[0], context))
{
return new LuaSymbol(string.Empty, type.GenericArgs[1], new VirtualInfo());
}
}
else if (keyType.SubTypeOf(type.GenericArgs[0], context))
{
return new LuaSymbol(string.Empty, type.GenericArgs[1], new VirtualInfo());
}
Expand Down
10 changes: 9 additions & 1 deletion EmmyLua/CodeAnalysis/Container/MultiIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace EmmyLua.CodeAnalysis.Container;
public class MultiIndex<TKey, TStubElement>
where TKey : notnull
{
record struct ElementIndex(LuaDocumentId DocumentId, TStubElement Element);
public record struct ElementIndex(LuaDocumentId DocumentId, TStubElement Element);

private readonly Dictionary<TKey, List<ElementIndex>> _indexMap = new();

Expand Down Expand Up @@ -51,5 +51,13 @@ public IEnumerable<TStubElement> Query(TKey key)
public IEnumerable<TStubElement> QueryAll() =>
_indexMap.Values.SelectMany(it => it.Select(element => element.Element));

public IEnumerable<KeyValuePair<TKey, List<ElementIndex>>> QueryAllWithKey()
{
foreach (var pair in _indexMap)
{
yield return pair;
}
}

public bool ContainsKey(TKey key) => _indexMap.ContainsKey(key);
}
6 changes: 0 additions & 6 deletions EmmyLua/CodeAnalysis/Extension/ExtensionDocument.cs

This file was deleted.

6 changes: 0 additions & 6 deletions EmmyLua/CodeAnalysis/Extension/ExtensionIndex.cs

This file was deleted.

Loading

0 comments on commit 8b45db6

Please sign in to comment.