From e781f35956032a301e49274322c4bbfb769d23d7 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Tue, 18 Jun 2024 20:36:17 +0800 Subject: [PATCH] keep global unique --- .../CompleteProvider/AliasAndEnumProvider.cs | 3 +- .../CodeAnalysis/Common/IQueryableIndex.cs | 4 +- .../DeclarationAnalyzer/DeclarationBuilder.cs | 6 +- .../ResolveAnalyzer/ResolveAnalyzer.cs | 17 ++---- .../Compilation/Index/UniqueIndexStorage.cs | 47 +++++++++++++++ .../Compilation/Index/WorkspaceIndex.cs | 57 +++++++++++++------ .../Compilation/Infer/ExpressionInfer.cs | 38 +------------ .../Compilation/Infer/GenericInfer.cs | 19 ++++--- .../Compilation/Infer/MethodInfer.cs | 3 +- .../Compilation/Infer/TypeInfer.cs | 3 +- .../Compilation/LuaCompilation.cs | 2 - .../Compilation/Search/Declarations.cs | 2 +- .../Compilation/Search/Members.cs | 2 +- .../Compilation/Search/SearchContext.cs | 5 ++ .../Semantic/Render/LuaRenderContext.cs | 2 +- .../Render/Renderer/LuaTypeRenderer.cs | 6 +- .../Compilation/Semantic/SemanticModel.cs | 1 - .../CodeAnalysis/Compilation/Type/LuaTypes.cs | 15 +++++ .../CodeAnalysis/IndexSystem/IndexFacade.cs | 16 +++--- 19 files changed, 148 insertions(+), 100 deletions(-) create mode 100644 EmmyLua/CodeAnalysis/Compilation/Index/UniqueIndexStorage.cs diff --git a/EmmyLua.LanguageServer/Completion/CompleteProvider/AliasAndEnumProvider.cs b/EmmyLua.LanguageServer/Completion/CompleteProvider/AliasAndEnumProvider.cs index 034cad5e..db4990ca 100644 --- a/EmmyLua.LanguageServer/Completion/CompleteProvider/AliasAndEnumProvider.cs +++ b/EmmyLua.LanguageServer/Completion/CompleteProvider/AliasAndEnumProvider.cs @@ -93,8 +93,7 @@ private void AddFuncParamCompletion(LuaCallArgListSyntax callArgList, CompleteCo private void AddAliasParamCompletion(LuaNamedType namedType, CompleteContext context) { var originType = context.SemanticModel.Compilation.Db - .QueryAliasOriginTypes(namedType.Name) - .FirstOrDefault(); + .QueryAliasOriginTypes(namedType.Name); if (originType is LuaAggregateType aggregateType) { AddAggregateTypeCompletion(aggregateType, context); diff --git a/EmmyLua/CodeAnalysis/Common/IQueryableIndex.cs b/EmmyLua/CodeAnalysis/Common/IQueryableIndex.cs index 5b9b60ca..d3a70bab 100644 --- a/EmmyLua/CodeAnalysis/Common/IQueryableIndex.cs +++ b/EmmyLua/CodeAnalysis/Common/IQueryableIndex.cs @@ -8,7 +8,7 @@ public interface IQueryableIndex IEnumerable QueryMembers(LuaType type); - IEnumerable QueryGlobals(string name); + IDeclaration? QueryGlobals(string name); IEnumerable QuerySupers(string name); @@ -16,8 +16,6 @@ public interface IQueryableIndex IEnumerable QueryNamedTypeDefinitions(string name); - IEnumerable QueryAliasOriginTypes(string name); - IEnumerable QueryGenericParams(string name); NamedTypeKind QueryNamedTypeKind(string name); diff --git a/EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationBuilder.cs b/EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationBuilder.cs index 53cd6bd9..7d2685c9 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationBuilder.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationBuilder.cs @@ -605,7 +605,7 @@ private void AnalyzeAssignStatDeclaration(LuaAssignStatSyntax luaAssignStat) ); AddReference(ReferenceKind.Definition, declaration, nameExpr); AnalyzeDeclarationDoc(declaration, luaAssignStat); - WorkspaceIndex.AddGlobal(DocumentId, name.RepresentText, declaration); + var unResolveDeclaration = new UnResolvedDeclaration(declaration, relatedExpr, ResolveState.UnResolvedType); AddUnResolved(unResolveDeclaration); @@ -624,6 +624,8 @@ private void AnalyzeAssignStatDeclaration(LuaAssignStatSyntax luaAssignStat) } } + WorkspaceIndex.AddGlobal(DocumentId, unResolveDeclaration.IsTypeDeclaration, + name.RepresentText, declaration); AddDeclaration(nameExpr.Position, declaration); } else @@ -713,7 +715,7 @@ private void AnalyzeMethodDeclaration(LuaFuncStatSyntax luaFuncStat) DeclarationFeature.Global ); AnalyzeDeclarationDoc(declaration, luaFuncStat); - WorkspaceIndex.AddGlobal(DocumentId, name2.RepresentText, declaration); + WorkspaceIndex.AddGlobal(DocumentId, true, name2.RepresentText, declaration); AddDeclaration(luaFuncStat.NameExpr.Position, declaration); var unResolved = new UnResolvedDeclaration(declaration, new LuaExprRef(closureExpr), ResolveState.UnResolvedType); diff --git a/EmmyLua/CodeAnalysis/Compilation/Analyzer/ResolveAnalyzer/ResolveAnalyzer.cs b/EmmyLua/CodeAnalysis/Compilation/Analyzer/ResolveAnalyzer/ResolveAnalyzer.cs index c3e0b682..d4ef25e1 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Analyzer/ResolveAnalyzer/ResolveAnalyzer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Analyzer/ResolveAnalyzer/ResolveAnalyzer.cs @@ -1,5 +1,4 @@ using EmmyLua.CodeAnalysis.Compilation.Declaration; -using EmmyLua.CodeAnalysis.Compilation.Infer; using EmmyLua.CodeAnalysis.Compilation.Search; using EmmyLua.CodeAnalysis.Compilation.Type; using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes; @@ -98,24 +97,20 @@ private void ResolveType(UnResolved unResolved) for (var i = 0; i < unResolvedForRangeParameter.ParameterLuaDeclarations.Count; i++) { var parameter = unResolvedForRangeParameter.ParameterLuaDeclarations[i]; - if (parameter.Info.DeclarationType is null) + if (parameter.Info.DeclarationType is LuaVariableRefType luaVariableRefType) { - parameter.Info = parameter.Info with - { - DeclarationType = multiReturnType.GetElementType(i) - }; + Context.Compilation.Db.WorkspaceIndex.AddIdRelatedType(luaVariableRefType.Id, + multiReturnType.GetElementType(i)); } } } else if (unResolvedForRangeParameter.ParameterLuaDeclarations.FirstOrDefault() is { } firstDeclaration) { - if (firstDeclaration.Info.DeclarationType is null) + if (firstDeclaration.Info.DeclarationType is LuaVariableRefType luaVariableRefType) { - firstDeclaration.Info = firstDeclaration.Info with - { - DeclarationType = returnType - }; + Context.Compilation.Db.WorkspaceIndex.AddIdRelatedType(luaVariableRefType.Id, + returnType); } } } diff --git a/EmmyLua/CodeAnalysis/Compilation/Index/UniqueIndexStorage.cs b/EmmyLua/CodeAnalysis/Compilation/Index/UniqueIndexStorage.cs new file mode 100644 index 00000000..c4a341c2 --- /dev/null +++ b/EmmyLua/CodeAnalysis/Compilation/Index/UniqueIndexStorage.cs @@ -0,0 +1,47 @@ +using EmmyLua.CodeAnalysis.Document; + +namespace EmmyLua.CodeAnalysis.Compilation.Index; + +public class UniqueIndexStorage where TKey : notnull +{ + record struct ElementIndex(LuaDocumentId DocumentId, TStubElement Element); + + private readonly Dictionary _indexMap = new(); + + public void Update(LuaDocumentId documentId, TKey key, TStubElement element) + { + _indexMap[key] = new ElementIndex(documentId, element); + } + + public void Remove(LuaDocumentId documentId) + { + var waitRemove = new List(); + foreach (var (key, element) in _indexMap) + { + if (element.DocumentId == documentId) + { + waitRemove.Add(key); + } + } + + foreach (var key in waitRemove) + { + _indexMap.Remove(key); + } + } + + public TStubElement? Query(TKey key) + { + if (_indexMap.TryGetValue(key, out var element)) + { + return element.Element; + } + + return default; + } + + public IEnumerable QueryAll() => + _indexMap.Values.Select(it => it.Element); + + public bool ContainsKey(TKey key) => _indexMap.ContainsKey(key); +} diff --git a/EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs b/EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs index 4e324c8a..48c8c8b4 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs @@ -12,13 +12,13 @@ public class WorkspaceIndex : IQueryableIndex { public IndexStorage TypeMembers { get; } = new(); - public IndexStorage DocumentVariableMembers { get; } = new(); + private IndexStorage DocumentVariableMembers { get; } = new(); - public IndexStorage GlobalVariableMembers { get; } = new(); + private IndexStorage GlobalVariableMembers { get; } = new(); - public InFiledDictionary ParentType { get; } = new(); + public UniqueIndexStorage ParentType { get; } = new(); - private IndexStorage Globals { get; } = new(); + private UniqueIndexStorage Globals { get; } = new(); private IndexStorage Supers { get; } = new(); @@ -26,11 +26,11 @@ public class WorkspaceIndex : IQueryableIndex private IndexStorage NamedTypeDefinition { get; } = new(); - public InFiledDictionary IdRelatedType { get; } = new(); + public UniqueIndexStorage IdRelatedType { get; } = new(); - public IndexStorage GlobalRelationTypes { get; } = new(); + public UniqueIndexStorage GlobalRelationTypes { get; } = new(); - private IndexStorage AliasTypes { get; } = new(); + private UniqueIndexStorage AliasTypes { get; } = new(); private IndexStorage GenericParams { get; } = new(); @@ -93,7 +93,7 @@ public void AddMember(LuaDocumentId documentId, LuaType type, LuaDeclaration lua var name = namedType.Name; if (name == "global") { - AddGlobal(documentId, luaDeclaration.Name, luaDeclaration); + AddGlobal(documentId, false, luaDeclaration.Name, luaDeclaration); return; } @@ -127,7 +127,7 @@ public void AddMember(LuaDocumentId documentId, LuaType type, LuaDeclaration lua private void AddNamedTypeMember(LuaDocumentId documentId, string name, LuaDeclaration luaDeclaration) { TypeMembers.Add(documentId, name, luaDeclaration); - ParentType.Add(documentId, luaDeclaration.UniqueId, name); + ParentType.Update(documentId, luaDeclaration.UniqueId, name); } private void AddLocalVariableMember(SyntaxElementId id, LuaDeclaration luaDeclaration) @@ -140,9 +140,20 @@ private void AddGlobalVariableMember(LuaDocumentId documentId, string name, LuaD GlobalVariableMembers.Add(documentId, name, luaDeclaration); } - public void AddGlobal(LuaDocumentId documentId, string name, LuaDeclaration luaDeclaration) + public void AddGlobal(LuaDocumentId documentId, bool forceDefine, string name, LuaDeclaration luaDeclaration) { - Globals.Add(documentId, name, luaDeclaration); + if (forceDefine) // forceUpdate + { + Globals.Update(documentId, name, luaDeclaration); + return; + } + + if (Globals.Query(name) is not null) + { + return; + } + + Globals.Update(documentId, name, luaDeclaration); } public void AddSuper(LuaDocumentId documentId, string name, LuaType type) @@ -168,17 +179,29 @@ public void AddAlias(LuaDocumentId documentId, string name, LuaType baseType, LuaDeclaration declaration) { AddTypeDefinition(documentId, name, declaration); - AliasTypes.Add(documentId, name, baseType); + AliasTypes.Update(documentId, name, baseType); } public void AddIdRelatedType(SyntaxElementId id, LuaType relatedType) { - IdRelatedType.Add(id.DocumentId, id, relatedType); + IdRelatedType.Update(id.DocumentId, id, relatedType); } public void AddGlobalRelationType(LuaDocumentId documentId, string name, LuaType type) { - GlobalRelationTypes.Add(documentId, name, type); + if (type.Equals(Builtin.Unknown)) + { + return; + } + + if (GlobalRelationTypes.Query(name) is { } oldType) + { + GlobalRelationTypes.Update(documentId, name, oldType.Union(type)); + } + else + { + GlobalRelationTypes.Update(documentId, name, type); + } } public void AddEnum(LuaDocumentId documentId, string name, LuaType? baseType, @@ -303,7 +326,7 @@ public IEnumerable QueryMembers(LuaType type) return []; } - public IEnumerable QueryGlobals(string name) + public IDeclaration? QueryGlobals(string name) { return Globals.Query(name); } @@ -323,7 +346,7 @@ public IEnumerable QueryNamedTypeDefinitions(string name) return NamedTypeDefinition.Query(name); } - public IEnumerable QueryAliasOriginTypes(string name) + public LuaType? QueryAliasOriginTypes(string name) { return AliasTypes.Query(name); } @@ -388,6 +411,6 @@ public IEnumerable QueryDocumentLocalDeclarations(LuaDocumentId public LuaType? QueryRelatedGlobalType(string name) { - return GlobalRelationTypes.Query(name).FirstOrDefault(); + return GlobalRelationTypes.Query(name); } } diff --git a/EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs b/EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs index cfd65d56..ae028064 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Infer/ExpressionInfer.cs @@ -25,41 +25,9 @@ public static LuaType InferExpr(LuaExprSyntax expr, SearchContext context) }; } - private static LuaType UnwrapType(LuaType luaType, SearchContext context) - { - switch (luaType) - { - case LuaVariableRefType variableRefType: - { - var ty = context.Compilation.Db.QueryTypeFromId(variableRefType.Id); - if (ty is not null) - { - return ty; - } - - return variableRefType; - } - case GlobalNameType globalNameType: - { - var ty = context.Compilation.Db.QueryRelatedGlobalType(globalNameType.Name); - if (ty is not null) - { - return ty; - } - - return globalNameType; - } - - default: - { - return luaType; - } - } - } - private static LuaType InferUnaryExpr(LuaUnaryExprSyntax unaryExpr, SearchContext context) { - var exprTy = UnwrapType(context.Infer(unaryExpr.Expression), context); + var exprTy = context.Infer(unaryExpr.Expression).UnwrapType(context); var opKind = TypeOperatorKindHelper.ToTypeOperatorKind(unaryExpr.Operator); var op = context.GetBestMatchedUnaryOperator(opKind, exprTy); @@ -126,8 +94,8 @@ private static LuaType GuessAndOrType(LuaBinaryExprSyntax binaryExpr, OperatorKi private static LuaType GuessBinaryMathType(LuaBinaryExprSyntax binaryExpr, OperatorKind.BinaryOperator op, SearchContext context) { - var leftTy = UnwrapType(context.Infer(binaryExpr.LeftExpr), context); - var rightTy = UnwrapType(context.Infer(binaryExpr.RightExpr), context); + var leftTy = context.Infer(binaryExpr.LeftExpr).UnwrapType(context); + var rightTy = context.Infer(binaryExpr.RightExpr).UnwrapType(context); var opKind = TypeOperatorKindHelper.ToTypeOperatorKind(op); var bop = context.GetBestMatchedBinaryOperator(opKind, leftTy, rightTy); if (bop is not null) diff --git a/EmmyLua/CodeAnalysis/Compilation/Infer/GenericInfer.cs b/EmmyLua/CodeAnalysis/Compilation/Infer/GenericInfer.cs index a8724da2..5ad9a068 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Infer/GenericInfer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Infer/GenericInfer.cs @@ -1,5 +1,4 @@ -using EmmyLua.CodeAnalysis.Common; -using EmmyLua.CodeAnalysis.Compilation.Search; +using EmmyLua.CodeAnalysis.Compilation.Search; using EmmyLua.CodeAnalysis.Compilation.Type; using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes; @@ -14,13 +13,16 @@ public static void InferInstantiateByExpr( Dictionary genericReplace, SearchContext context) { - if (type is LuaTemplateType templateType && expr is LuaLiteralExprSyntax {Literal: LuaStringToken {} stringToken}) + if (type is LuaTemplateType templateType && expr is LuaLiteralExprSyntax + { + Literal: LuaStringToken { } stringToken + }) { TemplateTypeInstantiateByString(templateType, stringToken, genericParameter, genericReplace, context); return; } - var exprType = context.Infer(expr); + var exprType = context.InferAndUnwrap(expr); InferInstantiateByType(type, exprType, genericParameter, genericReplace, context); } @@ -32,7 +34,7 @@ public static void InferInstantiateByExpandTypeAndExprs( SearchContext context ) { - InferInstantiateByExpandTypeAndTypes(expandType, expr.Select(context.Infer), genericParameter, genericReplace, + InferInstantiateByExpandTypeAndTypes(expandType, expr.Select(context.InferAndUnwrap), genericParameter, genericReplace, context); } @@ -173,7 +175,7 @@ private static void GenericTableExprInstantiate( keyType = keyType.Union(Builtin.String); } - var fieldValueType = context.Infer(fieldSyntax.Value); + var fieldValueType = context.InferAndUnwrap(fieldSyntax.Value); valueType = valueType.Union(fieldValueType); } @@ -216,7 +218,7 @@ private static void ArrayTypeInstantiateByType( { if (field.IsValue) { - var fieldValueType = context.Infer(field.Value); + var fieldValueType = context.InferAndUnwrap(field.Value); valueType = valueType.Union(fieldValueType); } } @@ -296,7 +298,8 @@ private static void TupleTypeInstantiateByType( else { var rightElementType = tupleType2.TupleDeclaration[i].Type; - InferInstantiateByType(leftElementType, rightElementType, genericParameter, genericReplace, context); + InferInstantiateByType(leftElementType, rightElementType, genericParameter, genericReplace, + context); } } } diff --git a/EmmyLua/CodeAnalysis/Compilation/Infer/MethodInfer.cs b/EmmyLua/CodeAnalysis/Compilation/Infer/MethodInfer.cs index 5a5058f4..2917aef7 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Infer/MethodInfer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Infer/MethodInfer.cs @@ -18,7 +18,7 @@ enum SignatureMatchType struct SignatureMatchResult { - public static readonly SignatureMatchResult NotMatch = new SignatureMatchResult + public static readonly SignatureMatchResult NotMatch = new() { MatchType = SignatureMatchType.DoNotMatch, MatchCount = 0 @@ -68,7 +68,6 @@ struct SignatureMatchResult } } - public static LuaSignature FindPerfectMatchSignature( LuaMethodType methodType, LuaCallExprSyntax callExpr, diff --git a/EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs b/EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs index e5f105bc..601f0c33 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Infer/TypeInfer.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using System.Diagnostics; +using System.Diagnostics; using EmmyLua.CodeAnalysis.Common; using EmmyLua.CodeAnalysis.Compilation.Declaration; using EmmyLua.CodeAnalysis.Compilation.Search; diff --git a/EmmyLua/CodeAnalysis/Compilation/LuaCompilation.cs b/EmmyLua/CodeAnalysis/Compilation/LuaCompilation.cs index ff59697a..78ff7659 100644 --- a/EmmyLua/CodeAnalysis/Compilation/LuaCompilation.cs +++ b/EmmyLua/CodeAnalysis/Compilation/LuaCompilation.cs @@ -2,9 +2,7 @@ using EmmyLua.CodeAnalysis.Compilation.Analyzer.DeclarationAnalyzer; using EmmyLua.CodeAnalysis.Compilation.Analyzer.FlowAnalyzer; using EmmyLua.CodeAnalysis.Compilation.Analyzer.ResolveAnalyzer; -using EmmyLua.CodeAnalysis.Compilation.Declaration; using EmmyLua.CodeAnalysis.Compilation.Index; -using EmmyLua.CodeAnalysis.Compilation.Infer; using EmmyLua.CodeAnalysis.Compilation.Search; using EmmyLua.CodeAnalysis.Compilation.Semantic; using EmmyLua.CodeAnalysis.Diagnostics; diff --git a/EmmyLua/CodeAnalysis/Compilation/Search/Declarations.cs b/EmmyLua/CodeAnalysis/Compilation/Search/Declarations.cs index 2e8d68f9..335b6717 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Search/Declarations.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Search/Declarations.cs @@ -116,7 +116,7 @@ public class Declarations(SearchContext context) if (nameExpr.Name is { } name) { - return context.Compilation.Db.QueryGlobals(name.RepresentText).FirstOrDefault(); + return context.Compilation.Db.QueryGlobals(name.RepresentText); } return null; diff --git a/EmmyLua/CodeAnalysis/Compilation/Search/Members.cs b/EmmyLua/CodeAnalysis/Compilation/Search/Members.cs index 1d6714a9..acc13353 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Search/Members.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Search/Members.cs @@ -133,7 +133,7 @@ private IEnumerable InnerGetMembers(LuaType luaType) var namedTypeKind = namedType.GetTypeKind(context); if (namedTypeKind == NamedTypeKind.Alias) { - var originType = context.Compilation.Db.QueryAliasOriginTypes(namedType.Name).FirstOrDefault(); + var originType = context.Compilation.Db.QueryAliasOriginTypes(namedType.Name); if (originType is not null) { return GetMembers(originType); diff --git a/EmmyLua/CodeAnalysis/Compilation/Search/SearchContext.cs b/EmmyLua/CodeAnalysis/Compilation/Search/SearchContext.cs index 615cef8f..e094c433 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Search/SearchContext.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Search/SearchContext.cs @@ -43,6 +43,11 @@ public LuaType Infer(LuaSyntaxElement? element) return ElementInfer.Infer(element); } + public LuaType InferAndUnwrap(LuaSyntaxElement? element) + { + return Infer(element).UnwrapType(this); + } + public void ClearMemberCache(LuaType luaType) { Members.ClearMember(luaType); diff --git a/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/LuaRenderContext.cs b/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/LuaRenderContext.cs index 444b4f34..3b790980 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/LuaRenderContext.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/LuaRenderContext.cs @@ -137,7 +137,7 @@ private void RenderAliasExpand() foreach (var type in _aliasExpand) { var name = type.Name; - var originType = SearchContext.Compilation.Db.QueryAliasOriginTypes(name).FirstOrDefault(); + var originType = SearchContext.Compilation.Db.QueryAliasOriginTypes(name); if (originType is LuaAggregateType aggregateType) { LuaTypeRenderer.RenderAliasMember(name, aggregateType, this); diff --git a/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/Renderer/LuaTypeRenderer.cs b/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/Renderer/LuaTypeRenderer.cs index 71979605..9709ff0e 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/Renderer/LuaTypeRenderer.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Semantic/Render/Renderer/LuaTypeRenderer.cs @@ -85,8 +85,7 @@ private static void InnerRenderDetailType(LuaNamedType namedType, LuaRenderConte var namedTypeKind = namedType.GetTypeKind(renderContext.SearchContext); if (namedTypeKind == NamedTypeKind.Alias) { - var originType = renderContext.SearchContext.Compilation.Db.QueryAliasOriginTypes(namedType.Name) - .FirstOrDefault(); + var originType = renderContext.SearchContext.Compilation.Db.QueryAliasOriginTypes(namedType.Name); if (originType is LuaAggregateType) { renderContext.AddAliasExpand(namedType); @@ -331,8 +330,7 @@ private static void RenderNamedType(LuaNamedType namedType, LuaRenderContext ren { if (namedTypeKind == NamedTypeKind.Alias) { - var originType = renderContext.SearchContext.Compilation.Db.QueryAliasOriginTypes(namedType.Name) - .FirstOrDefault(); + var originType = renderContext.SearchContext.Compilation.Db.QueryAliasOriginTypes(namedType.Name); if (originType is not null) { InnerRenderType(originType, renderContext, 1); diff --git a/EmmyLua/CodeAnalysis/Compilation/Semantic/SemanticModel.cs b/EmmyLua/CodeAnalysis/Compilation/Semantic/SemanticModel.cs index 8dc2ab86..f260d100 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Semantic/SemanticModel.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Semantic/SemanticModel.cs @@ -1,6 +1,5 @@ using EmmyLua.CodeAnalysis.Common; using EmmyLua.CodeAnalysis.Compilation.Declaration; -using EmmyLua.CodeAnalysis.Compilation.Infer; using EmmyLua.CodeAnalysis.Compilation.Scope; using EmmyLua.CodeAnalysis.Compilation.Search; using EmmyLua.CodeAnalysis.Compilation.Semantic.Render; diff --git a/EmmyLua/CodeAnalysis/Compilation/Type/LuaTypes.cs b/EmmyLua/CodeAnalysis/Compilation/Type/LuaTypes.cs index b89226f1..b7cee771 100644 --- a/EmmyLua/CodeAnalysis/Compilation/Type/LuaTypes.cs +++ b/EmmyLua/CodeAnalysis/Compilation/Type/LuaTypes.cs @@ -41,6 +41,11 @@ public virtual LuaType Instantiate(Dictionary genericReplace) { return this; } + + public virtual LuaType UnwrapType(SearchContext context) + { + return this; + } } public class LuaNamedType( @@ -708,6 +713,11 @@ public override int GetHashCode() { return Id.GetHashCode(); } + + public override LuaType UnwrapType(SearchContext context) + { + return context.Compilation.Db.QueryTypeFromId(Id) ?? this; + } } public class GlobalNameType(string name) @@ -729,5 +739,10 @@ public override int GetHashCode() { return Name.GetHashCode(); } + + public override LuaType UnwrapType(SearchContext context) + { + return context.Compilation.Db.QueryRelatedGlobalType(Name) ?? this; + } } diff --git a/EmmyLua/CodeAnalysis/IndexSystem/IndexFacade.cs b/EmmyLua/CodeAnalysis/IndexSystem/IndexFacade.cs index dc5ac4ae..06f36042 100644 --- a/EmmyLua/CodeAnalysis/IndexSystem/IndexFacade.cs +++ b/EmmyLua/CodeAnalysis/IndexSystem/IndexFacade.cs @@ -48,9 +48,9 @@ public IEnumerable QueryAllMembers() return WorkspaceIndex.TypeMembers.QueryAll(); } - public IEnumerable QueryGlobals(string name) + public IDeclaration? QueryGlobals(string name) { - return QueryableIndexes.SelectMany(it => it.QueryGlobals(name)); + return QueryableIndexes.Select(index => index.QueryGlobals(name)).OfType().FirstOrDefault(); } public IEnumerable QueryAllGlobal() @@ -75,12 +75,12 @@ public IEnumerable QueryNamedTypeDefinitions(string name) public LuaType? QueryTypeFromId(SyntaxElementId id) { - return WorkspaceIndex.IdRelatedType.Query(id.DocumentId, id); + return WorkspaceIndex.IdRelatedType.Query(id); } - public IEnumerable QueryAliasOriginTypes(string name) + public LuaType? QueryAliasOriginTypes(string name) { - return QueryableIndexes.SelectMany(it => it.QueryAliasOriginTypes(name)); + return WorkspaceIndex.QueryAliasOriginTypes(name); } public LuaType? QueryModuleType(LuaDocumentId documentId) @@ -149,7 +149,7 @@ public IEnumerable QueryNamedTypeReferences(string name) public LuaNamedType? QueryParentType(LuaSyntaxNode node) { - var parentType = WorkspaceIndex.ParentType.Query(node.UniqueId.DocumentId, node.UniqueId); + var parentType = WorkspaceIndex.ParentType.Query(node.UniqueId); if (parentType is not null) { return new LuaNamedType(parentType); @@ -162,7 +162,7 @@ public IEnumerable QueryNamedTypeReferences(string name) { if (declaration is LuaDeclaration luaDeclaration) { - var parentType = WorkspaceIndex.ParentType.Query(luaDeclaration.UniqueId.DocumentId, luaDeclaration.UniqueId); + var parentType = WorkspaceIndex.ParentType.Query(luaDeclaration.UniqueId); if (parentType is not null) { return new LuaNamedType(parentType); @@ -209,7 +209,7 @@ public IEnumerable QueryDocumentLocalDeclarations(LuaDocumentId public LuaType? QueryRelatedType(SyntaxElementId id) { - return WorkspaceIndex.IdRelatedType.Query(id.DocumentId, id); + return WorkspaceIndex.IdRelatedType.Query(id); } public LuaType? QueryRelatedGlobalType(string name)