Skip to content

Commit 681f4de

Browse files
committed
Fix some bug, add namespace type
1 parent 9bc9943 commit 681f4de

File tree

10 files changed

+170
-30
lines changed

10 files changed

+170
-30
lines changed

EmmyLua.LanguageServer/Completion/CompleteProvider/DocProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ private void AddParamNameCompletion(LuaDocTagParamSyntax paramSyntax, CompleteCo
9797

9898
private void AddTypeNameCompletion(string prefix, CompleteContext context)
9999
{
100+
var prefixNamespace = string.Empty;
101+
var dotIndex = prefix.LastIndexOf('.');
102+
if (dotIndex != -1)
103+
{
104+
prefixNamespace = prefix[..dotIndex];
105+
}
100106
var namespaceOrTypes =
101-
context.SemanticModel.Compilation.TypeManager.GetNamespaceOrTypeInfos(prefix,
107+
context.SemanticModel.Compilation.TypeManager.GetNamespaceOrTypeInfos(prefixNamespace,
102108
context.SemanticModel.Document.Id);
103109
foreach (var namespaceOrType in namespaceOrTypes)
104110
{

EmmyLua.LanguageServer/Completion/CompletionItemBuilder.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ public CompletionItemBuilder WithCheckDeclaration(LuaSymbol symbol)
7171
}
7272
}
7373

74+
if (symbol.Info is NamespaceInfo)
75+
{
76+
Kind = CompletionItemKind.Module;
77+
}
78+
else if (symbol.Info is NamedTypeInfo { Kind: { } kind })
79+
{
80+
Kind = kind switch
81+
{
82+
NamedTypeKind.Class => CompletionItemKind.Class,
83+
NamedTypeKind.Enum => CompletionItemKind.Enum,
84+
NamedTypeKind.Interface => CompletionItemKind.Interface,
85+
NamedTypeKind.Alias => CompletionItemKind.Reference,
86+
_ => Kind
87+
};
88+
}
89+
7490
return this;
7591
}
7692

EmmyLua.LanguageServer/Server/Render/Renderer/LuaTypeRenderer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ private static void RenderMultiReturnType(LuaMultiReturnType multiReturnType, Lu
513513

514514
private static void RenderGeneric(LuaGenericType genericType, LuaRenderContext renderContext, int level)
515515
{
516+
if (genericType.Name == "namespace")
517+
{
518+
renderContext.Append("namespace");
519+
if (genericType.GenericArgs.FirstOrDefault() is LuaStringLiteralType namespaceString)
520+
{
521+
renderContext.Append($" {namespaceString.Content}");
522+
}
523+
return;
524+
}
525+
516526
renderContext.Append(genericType.Name);
517527
renderContext.Append('<');
518528
for (var i = 0; i < genericType.GenericArgs.Count; i++)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ private void MergeType(UnResolvedSymbol unResolved, LuaExprSyntax luaExpr, LuaTy
302302
{
303303
declaration.Type = new LuaElementType(declaration.UniqueId);
304304
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
305+
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, type);
306+
}
307+
else if (luaExpr is LuaCallExprSyntax)
308+
{
309+
declaration.Type = new LuaElementType(declaration.UniqueId);
310+
Context.Compilation.TypeManager.AddDocumentElementType(declaration.UniqueId);
311+
Context.Compilation.TypeManager.SetBaseType(declaration.UniqueId, type);
305312
}
306313
else
307314
{

EmmyLua/CodeAnalysis/Compilation/Search/IndexMembers.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using EmmyLua.CodeAnalysis.Compilation.Symbol;
2+
using EmmyLua.CodeAnalysis.Document;
3+
using EmmyLua.CodeAnalysis.Syntax.Node;
24
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
35
using EmmyLua.CodeAnalysis.Type;
46
using EmmyLua.CodeAnalysis.Type.Manager.TypeInfo;
@@ -26,6 +28,11 @@ public class IndexMembers(SearchContext context)
2628

2729
private LuaSymbol? FindNamedTypeMember(LuaNamedType namedType, string name)
2830
{
31+
if (namedType.IsSameType(Builtin.Global, context))
32+
{
33+
return context.Compilation.TypeManager.GetGlobalSymbol(name);
34+
}
35+
2936
var typeInfo = context.Compilation.TypeManager.FindTypeInfo(namedType);
3037
if (typeInfo is null)
3138
{
@@ -224,6 +231,51 @@ public class IndexMembers(SearchContext context)
224231

225232
private LuaSymbol? FindGenericTypeMember(LuaGenericType genericType, string name)
226233
{
234+
if (genericType.Name == "table")
235+
{
236+
if (name.StartsWith('[') && genericType.GenericArgs.FirstOrDefault() is { } numberType
237+
&& (numberType.IsSameType(Builtin.Number, context) ||
238+
numberType.IsSameType(Builtin.Integer, context)))
239+
{
240+
return new LuaSymbol(string.Empty, genericType.GenericArgs.ElementAtOrDefault(1), new VirtualInfo());
241+
}
242+
else if (genericType.GenericArgs.FirstOrDefault() is { } stringType &&
243+
stringType.IsSameType(Builtin.String, context))
244+
{
245+
return new LuaSymbol(string.Empty, genericType.GenericArgs.ElementAtOrDefault(1), new VirtualInfo());
246+
}
247+
}
248+
else if (genericType.Name == "namespace" &&
249+
genericType.GenericArgs.FirstOrDefault() is LuaStringLiteralType namespaceString)
250+
{
251+
var namespaceOrTypeInfo =
252+
context.Compilation.TypeManager.FindNamespaceOrType(namespaceString.Content, name);
253+
if (!namespaceOrTypeInfo.HasValue)
254+
{
255+
return null;
256+
}
257+
258+
if (namespaceOrTypeInfo.Value.IsNamespace)
259+
{
260+
var namespaceType = new LuaGenericType(LuaDocumentId.VirtualDocumentId, "namespace", [
261+
new LuaStringLiteralType($"{namespaceString.Content}.{name}")
262+
]);
263+
return new LuaSymbol(name,
264+
namespaceType,
265+
new NamespaceInfo()
266+
);
267+
}
268+
else
269+
{
270+
var namedType = new LuaNamedType(LuaDocumentId.VirtualDocumentId,
271+
$"{namespaceString.Content}.{name}");
272+
return new LuaSymbol(name, namedType, new NamedTypeInfo(
273+
new LuaElementPtr<LuaDocTagNamedTypeSyntax>(namespaceOrTypeInfo.Value.Id),
274+
namespaceOrTypeInfo.Value.Kind
275+
));
276+
}
277+
}
278+
227279
var typeInfo = context.Compilation.TypeManager.FindTypeInfo(genericType);
228280
if (typeInfo is null)
229281
{

EmmyLua/CodeAnalysis/Compilation/Search/Members.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using EmmyLua.CodeAnalysis.Compilation.Symbol;
2+
using EmmyLua.CodeAnalysis.Document;
3+
using EmmyLua.CodeAnalysis.Syntax.Node;
4+
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
25
using EmmyLua.CodeAnalysis.Type;
36
using EmmyLua.CodeAnalysis.Type.Manager.TypeInfo;
47

@@ -25,6 +28,20 @@ public List<LuaSymbol> GetTypeMembers(LuaType luaType)
2528

2629
private List<LuaSymbol> GetNamedTypeMembers(LuaNamedType namedType)
2730
{
31+
if (namedType.IsSameType(Builtin.Global, context))
32+
{
33+
var list = new List<LuaSymbol>();
34+
foreach (var globalInfo in context.Compilation.TypeManager.GetAllGlobalInfos())
35+
{
36+
if (globalInfo.MainLuaSymbol is { } mainLuaSymbol)
37+
{
38+
list.Add(mainLuaSymbol);
39+
}
40+
}
41+
42+
return list;
43+
}
44+
2845
var typeInfo = context.Compilation.TypeManager.FindTypeInfo(namedType);
2946
if (typeInfo is null)
3047
{
@@ -224,6 +241,37 @@ private List<LuaSymbol> GetGenericTypeMembers(LuaGenericType genericType)
224241
{
225242
return [];
226243
}
244+
else if (genericType.Name == "namespace" &&
245+
genericType.GenericArgs.FirstOrDefault() is LuaStringLiteralType namespaceString)
246+
{
247+
var namespaces = new List<LuaSymbol>();
248+
var namespaceOrTypeIndexs = context.Compilation.TypeManager.GetNamespaceOrTypeInfos(
249+
namespaceString.Content, LuaDocumentId.VirtualDocumentId);
250+
foreach (var namespaceOrTypeInfo in namespaceOrTypeIndexs)
251+
{
252+
if (namespaceOrTypeInfo.IsNamespace)
253+
{
254+
var namespaceType = new LuaGenericType(LuaDocumentId.VirtualDocumentId, "namespace", [
255+
new LuaStringLiteralType($"{namespaceString.Content}.{namespaceOrTypeInfo.Name}")
256+
]);
257+
namespaces.Add(new LuaSymbol(namespaceOrTypeInfo.Name,
258+
namespaceType,
259+
new NamespaceInfo()
260+
));
261+
}
262+
else
263+
{
264+
var namedType = new LuaNamedType(LuaDocumentId.VirtualDocumentId,
265+
$"{namespaceString.Content}.{namespaceOrTypeInfo.Name}");
266+
namespaces.Add(new LuaSymbol(namespaceOrTypeInfo.Name, namedType, new NamedTypeInfo(
267+
new LuaElementPtr<LuaDocTagNamedTypeSyntax>(namespaceOrTypeInfo.Id),
268+
namespaceOrTypeInfo.Kind
269+
)));
270+
}
271+
}
272+
273+
return namespaces;
274+
}
227275

228276
var typeInfo = context.Compilation.TypeManager.FindTypeInfo(genericType);
229277
if (typeInfo is null)
@@ -247,7 +295,7 @@ private List<LuaSymbol> GetGenericTypeMembers(LuaGenericType genericType)
247295
{
248296
for (var i = 0; i < typeInfo.GenericParams.Count && i < genericArgs.Count; i++)
249297
{
250-
substitute.Add(typeInfo.GenericParams[i].Name , genericArgs[i], true);
298+
substitute.Add(typeInfo.GenericParams[i].Name, genericArgs[i], true);
251299
}
252300
}
253301

@@ -327,6 +375,4 @@ public List<LuaSymbol> GetSuperMembers(LuaNamedType namedType)
327375

328376
return members.Values.ToList();
329377
}
330-
331-
332378
}

EmmyLua/CodeAnalysis/Compilation/Symbol/LuaSymbol.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,5 @@ public record VirtualInfo : ISymbolInfo
277277
{
278278
public LuaElementPtr<LuaSyntaxElement> Ptr => LuaElementPtr<LuaSyntaxElement>.Empty;
279279
}
280+
281+
public record NamespaceInfo : VirtualInfo;

EmmyLua/CodeAnalysis/Type/Builtin.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,7 @@ public static class Builtin
1717

1818
public static LuaNamedType Self { get; } = new(LuaDocumentId.VirtualDocumentId, "self");
1919

20-
public static LuaNamedType? FromName(string name)
21-
{
22-
return name switch
23-
{
24-
"unknown" => Unknown,
25-
"any" => Any,
26-
"nil" or "void" => Nil,
27-
"boolean" or "bool" => Boolean,
28-
"number" => Number,
29-
"integer" or "int" => Integer,
30-
"string" => String,
31-
"table" => Table,
32-
"thread" => Thread,
33-
"userdata" => UserData,
34-
"self" => Self,
35-
_ => null,
36-
};
37-
}
20+
public static LuaNamedType Global { get; } = new(LuaDocumentId.VirtualDocumentId, "global");
21+
22+
public static LuaNamedType Namespace { get; } = new(LuaDocumentId.VirtualDocumentId, "namespace");
3823
}

EmmyLua/CodeAnalysis/Type/Manager/LuaTypeManager.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public void BuildSubTypes()
233233
leftTypeInfo.SubTypes.Add(right);
234234
}
235235
}
236+
236237
WaitBuildSubtypes.Clear();
237238
}
238239

@@ -292,6 +293,12 @@ public void AddMemberImplementation(LuaNamedType type, LuaSymbol member)
292293
return;
293294
}
294295

296+
if (IsSameType(type, Builtin.Global))
297+
{
298+
AddGlobal(member.Name, member);
299+
return;
300+
}
301+
295302
if (typeInfo.IsDefinedInDocument(member.DocumentId) || typeInfo.Global)
296303
{
297304
typeInfo.Declarations ??= new();
@@ -499,15 +506,8 @@ public IEnumerable<GlobalTypeInfo> GetAllGlobalInfos()
499506

500507
public record struct NamespaceOrType(string Name, bool IsNamespace, NamedTypeKind Kind, SyntaxElementId Id);
501508

502-
public IEnumerable<NamespaceOrType> GetNamespaceOrTypeInfos(string prefix, LuaDocumentId documentId)
509+
public IEnumerable<NamespaceOrType> GetNamespaceOrTypeInfos(string prefixNamespace, LuaDocumentId documentId)
503510
{
504-
var prefixNamespace = string.Empty;
505-
var dotIndex = prefix.LastIndexOf('.');
506-
if (dotIndex != -1)
507-
{
508-
prefixNamespace = prefix[..dotIndex];
509-
}
510-
511511
if (documentId != LuaDocumentId.VirtualDocumentId)
512512
{
513513
if (NamespaceIndices.TryGetValue(documentId, out var namespaceIndex))
@@ -566,6 +566,20 @@ public IEnumerable<NamespaceOrType> GetNamespaceOrTypeInfos(string prefix, LuaDo
566566
}
567567
}
568568

569+
public NamespaceOrType? FindNamespaceOrType(string fullName, string member)
570+
{
571+
if (RootNamespace.FindNamespaceOrType(fullName) is { } namespaceInfo)
572+
{
573+
if (namespaceInfo.FindNamespaceOrType(member) is { } child)
574+
{
575+
return new NamespaceOrType(member, child.TypeInfo is null, child.TypeInfo?.Kind ?? NamedTypeKind.None,
576+
child.TypeInfo?.MainElementId ?? SyntaxElementId.Empty);
577+
}
578+
}
579+
580+
return null;
581+
}
582+
569583
public bool IsSameType(LuaNamedType left, LuaNamedType right)
570584
{
571585
if (left.DocumentId == right.DocumentId)

EmmyLua/Resources/std/builtin.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@
101101
---@class self
102102

103103
---@class integer : number
104+
105+
---@class namespace<T: string>

0 commit comments

Comments
 (0)