Skip to content

Commit 689bcf9

Browse files
committed
Fix global and nodiscard
1 parent 716da7c commit 689bcf9

File tree

10 files changed

+102
-72
lines changed

10 files changed

+102
-72
lines changed

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/AttachDeclarationAnalyzer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace EmmyLua.CodeAnalysis.Compilation.Analyzer.DeclarationAnalyzer;
1010

1111
public class AttachDeclarationAnalyzer(
12-
LuaDocument document,
1312
DeclarationContext declarationContext,
1413
SearchContext searchContext)
1514
{
@@ -78,6 +77,10 @@ private void AnalyzeGeneralDeclaration(LuaSyntaxElement attachedElement, List<Lu
7877
declarations.FirstOrDefault() is { } firstDeclaration)
7978
{
8079
firstDeclaration.Info = firstDeclaration.Info with { DeclarationType = LuaNamedType.Create(name) };
80+
if (firstDeclaration.IsGlobal)
81+
{
82+
searchContext.Compilation.Db.AddGlobal(declarationContext.DocumentId, firstDeclaration.Name, firstDeclaration, true);
83+
}
8184
return;
8285
}
8386

@@ -91,6 +94,10 @@ private void AnalyzeGeneralDeclaration(LuaSyntaxElement attachedElement, List<Lu
9194
if (declarations.Count > i)
9295
{
9396
declarations[i].Info = declarations[i].Info with { DeclarationType = luaTypeList[i] };
97+
if (declarations[i].IsGlobal)
98+
{
99+
searchContext.Compilation.Db.AddGlobal(declarationContext.DocumentId, declarations[i].Name, declarations[i], true);
100+
}
94101
}
95102
}
96103

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override void Analyze(AnalyzeContext analyzeContext)
1919
Compilation.Db.AddDeclarationTree(document.Id, tree);
2020
}
2121

22-
var attachDeclarationAnalyzer = new AttachDeclarationAnalyzer(document, declarationContext, searchContext);
22+
var attachDeclarationAnalyzer = new AttachDeclarationAnalyzer(declarationContext, searchContext);
2323
attachDeclarationAnalyzer.Analyze();
2424
}
2525
}

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationWalker/DeclarationWalker.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace EmmyLua.CodeAnalysis.Compilation.Analyzer.DeclarationAnalyzer.DeclarationWalker;
99

10-
public partial class DeclarationWalker(DeclarationContext declarationContext, SearchContext searchContext) : ILuaElementWalker
10+
public partial class DeclarationWalker(DeclarationContext declarationContext, SearchContext searchContext)
11+
: ILuaElementWalker
1112
{
1213
private LuaCompilation Compilation => searchContext.Compilation;
1314

@@ -127,6 +128,11 @@ public void WalkIn(LuaSyntaxElement node)
127128
AnalyzeSimpleTag(deprecatedSyntax);
128129
break;
129130
}
131+
case LuaDocTagNodiscardSyntax nodiscardSyntax:
132+
{
133+
AnalyzeSimpleTag(nodiscardSyntax);
134+
break;
135+
}
130136
case LuaDocTagAsyncSyntax asyncSyntax:
131137
{
132138
AnalyzeSimpleTag(asyncSyntax);

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationWalker/DocWalker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace EmmyLua.CodeAnalysis.Compilation.Analyzer.DeclarationAnalyzer.Declarat
77

88
public partial class DeclarationWalker
99
{
10-
private void AnalyzeDocDetailField(LuaType parentType, LuaDocFieldSyntax field)
10+
private void AnalyzeDocDetailField(LuaType parentType, LuaDocFieldSyntax field)
1111
{
1212
var visibility = field.Visibility;
1313
switch (field)

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationWalker/MethodWalker.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace EmmyLua.CodeAnalysis.Compilation.Analyzer.DeclarationAnalyzer.Declarat
99

1010
public partial class DeclarationWalker
1111
{
12-
13-
1412
private void AnalyzeMethod(LuaFuncStatSyntax luaFuncStat)
1513
{
1614
switch (luaFuncStat)
@@ -49,7 +47,7 @@ private void AnalyzeMethod(LuaFuncStatSyntax luaFuncStat)
4947
);
5048
declarationContext.AddLocalDeclaration(nameExpr, declaration);
5149
declarationContext.AddReference(ReferenceKind.Definition, declaration, nameExpr);
52-
declarationContext.Db.AddGlobal(DocumentId, true, name2.RepresentText, declaration);
50+
declarationContext.Db.AddGlobal(DocumentId, name2.RepresentText, declaration, true);
5351
var unResolved = new UnResolvedDeclaration(declaration, new LuaExprRef(closureExpr),
5452
ResolveState.UnResolvedType);
5553
declarationContext.AddUnResolved(unResolved);

EmmyLua/CodeAnalysis/Compilation/Analyzer/DeclarationAnalyzer/DeclarationWalker/VariableWalker.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ private void AnalyzeAssignStat(LuaAssignStatSyntax luaAssignStat)
154154
ResolveState.UnResolvedType
155155
);
156156
declarationContext.AddUnResolved(unResolveDeclaration);
157-
declarationContext.Db.AddGlobal(DocumentId, false,
158-
name.RepresentText, declaration);
157+
declarationContext.Db.AddGlobal(DocumentId, name.RepresentText, declaration, false);
159158
}
160159
else
161160
{

EmmyLua/CodeAnalysis/Compilation/Index/IndexContainer/GlobalIndex.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using EmmyLua.CodeAnalysis.Document;
2+
3+
namespace EmmyLua.CodeAnalysis.Compilation.Index.IndexContainer;
4+
5+
public class PriorityIndex<TKey, TStubElement> where TKey : notnull
6+
{
7+
record struct ElementIndex(LuaDocumentId DocumentId, TStubElement Element);
8+
9+
private Dictionary<TKey, List<ElementIndex>> _indexMap = new();
10+
11+
public void AddGlobal(LuaDocumentId documentId, TKey key, TStubElement element, bool highestPriority = false)
12+
{
13+
if (!_indexMap.TryGetValue(key, out var elementIndices))
14+
{
15+
elementIndices = new();
16+
_indexMap.Add(key, elementIndices);
17+
}
18+
19+
if (highestPriority && elementIndices.Count != 0)
20+
{
21+
elementIndices.Insert(0, new ElementIndex(documentId, element));
22+
}
23+
else
24+
{
25+
elementIndices.Add(new ElementIndex(documentId, element));
26+
}
27+
}
28+
29+
public TStubElement? Query(TKey key)
30+
{
31+
if (_indexMap.TryGetValue(key, out var elements))
32+
{
33+
return elements.First().Element;
34+
}
35+
36+
return default;
37+
}
38+
39+
public IEnumerable<TStubElement> QueryAll()
40+
{
41+
return _indexMap.Values.Select(it => it.First().Element);
42+
}
43+
44+
public void Remove(LuaDocumentId documentId)
45+
{
46+
var waitRemove = new List<TKey>();
47+
foreach (var (key, declarations) in _indexMap)
48+
{
49+
declarations.RemoveAll(it => it.DocumentId == documentId);
50+
if (declarations.Count == 0)
51+
{
52+
waitRemove.Add(key);
53+
}
54+
}
55+
56+
foreach (var key in waitRemove)
57+
{
58+
_indexMap.Remove(key);
59+
}
60+
}
61+
}

EmmyLua/CodeAnalysis/Compilation/Index/TypeIndex.cs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public class TypeIndex
3737

3838
#endregion
3939

40-
private UniqueIndex<string, LuaDeclaration> Globals { get; } = new();
41-
4240
#region TypeInfo
4341
private MultiIndex<string, LuaType> Supers { get; } = new();
4442

@@ -72,9 +70,6 @@ public void Remove(LuaDocumentId documentId)
7270
AssociatedType.Remove(documentId);
7371
GlobalAssociatedType.Remove(documentId);
7472

75-
// globals
76-
Globals.Remove(documentId);
77-
7873
// typeInfo
7974
Supers.Remove(documentId);
8075
SubTypes.Remove(documentId);
@@ -103,22 +98,9 @@ public void AddGlobalVariableMember(LuaDocumentId documentId, string name, LuaDe
10398
ParentGlobalType.Update(documentId, luaDeclaration.UniqueId, name);
10499
}
105100

106-
public void AddGlobal(LuaDocumentId documentId, bool forceDefine, string name, LuaDeclaration luaDeclaration)
101+
public void AddParentNamedType(LuaDocumentId documentId, string name, LuaDeclaration luaDeclaration)
107102
{
108-
if (forceDefine) // forceUpdate
109-
{
110-
Globals.Update(documentId, name, luaDeclaration);
111-
ParentNamedType.Update(documentId, luaDeclaration.UniqueId, "global");
112-
return;
113-
}
114-
115-
if (Globals.Query(name) is not null)
116-
{
117-
return;
118-
}
119-
120-
Globals.Update(documentId, name, luaDeclaration);
121-
ParentNamedType.Update(documentId, luaDeclaration.UniqueId, "global");
103+
ParentNamedType.Update(documentId, luaDeclaration.UniqueId, name);
122104
}
123105

124106
public void AddSuper(LuaDocumentId documentId, string name, LuaType type)
@@ -194,11 +176,6 @@ public void AddTypeOverload(LuaDocumentId documentId, string name, LuaMethodType
194176
TypeOverloads.Add(documentId, name, methodType);
195177
}
196178

197-
public IEnumerable<IDeclaration> QueryAllGlobal()
198-
{
199-
return Globals.QueryAll();
200-
}
201-
202179
public IEnumerable<IDeclaration> QueryMembers(LuaType type)
203180
{
204181
if (!type.HasMember)
@@ -211,11 +188,6 @@ public IEnumerable<IDeclaration> QueryMembers(LuaType type)
211188
case LuaNamedType namedType:
212189
{
213190
var name = namedType.Name;
214-
if (name == "global")
215-
{
216-
return QueryAllGlobal();
217-
}
218-
219191
return TypeMembers.Query(name);
220192
}
221193
case GlobalNameType globalNameType:
@@ -240,11 +212,6 @@ public IEnumerable<IDeclaration> QueryMembers(LuaType type)
240212
return [];
241213
}
242214

243-
public IDeclaration? QueryGlobals(string name)
244-
{
245-
return Globals.Query(name);
246-
}
247-
248215
public IEnumerable<LuaType> QuerySupers(string name)
249216
{
250217
return Supers.Query(name);
@@ -311,6 +278,7 @@ public IEnumerable<LuaMethodType> QueryTypeOverloads(string name)
311278
{
312279
return new LuaVariableRefType(parentId);
313280
}
281+
314282
return null;
315283
}
316284

EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class WorkspaceIndex
1414
{
1515
private TypeIndex TypeIndex { get; } = new();
1616

17+
private PriorityIndex<string, LuaDeclaration> Globals { get; } = new();
18+
1719
private Dictionary<LuaDocumentId, LuaType> ModuleTypes { get; } = new();
1820

1921
private Dictionary<LuaDocumentId, List<LuaElementPtr<LuaExprSyntax>>> ModuleReturns { get; } = new();
@@ -33,6 +35,7 @@ public class WorkspaceIndex
3335
public void Remove(LuaDocumentId documentId)
3436
{
3537
TypeIndex.Remove(documentId);
38+
Globals.Remove(documentId);
3639
ModuleTypes.Remove(documentId);
3740
ModuleReturns.Remove(documentId);
3841
NameExpr.Remove(documentId);
@@ -59,7 +62,7 @@ public void AddMember(LuaDocumentId documentId, LuaType type, LuaDeclaration lua
5962
var name = namedType.Name;
6063
if (name == "global")
6164
{
62-
TypeIndex.AddGlobal(documentId, false, luaDeclaration.Name, luaDeclaration);
65+
AddGlobal(documentId, luaDeclaration.Name, luaDeclaration, false);
6366
return;
6467
}
6568

@@ -166,9 +169,11 @@ public void AddSuper(LuaDocumentId documentId, string name, LuaType super)
166169
TypeIndex.AddSuper(documentId, name, super);
167170
}
168171

169-
public void AddGlobal(LuaDocumentId documentId, bool forceDefine, string name, LuaDeclaration declaration)
172+
public void AddGlobal(LuaDocumentId documentId, string name, LuaDeclaration declaration,
173+
bool hightestPriority = false)
170174
{
171-
TypeIndex.AddGlobal(documentId, forceDefine, name, declaration);
175+
Globals.AddGlobal(documentId, name, declaration, hightestPriority);
176+
TypeIndex.AddParentNamedType(documentId, "global", declaration);
172177
}
173178

174179
public void AddEnum(LuaDocumentId documentId, string name, LuaType? baseType, LuaDeclaration declaration)
@@ -197,17 +202,22 @@ public void AddGenericParam(LuaDocumentId documentId, string name, LuaDeclaratio
197202

198203
public IEnumerable<IDeclaration> QueryAllGlobal()
199204
{
200-
return TypeIndex.QueryAllGlobal();
205+
return Globals.QueryAll();
201206
}
202207

203208
public IEnumerable<IDeclaration> QueryMembers(LuaType type)
204209
{
210+
if (type is LuaNamedType { Name: "global" })
211+
{
212+
return QueryAllGlobal();
213+
}
214+
205215
return TypeIndex.QueryMembers(type);
206216
}
207217

208218
public IDeclaration? QueryGlobals(string name)
209219
{
210-
return TypeIndex.QueryGlobals(name);
220+
return Globals.Query(name);
211221
}
212222

213223
public IEnumerable<LuaType> QuerySupers(string name)
@@ -317,7 +327,7 @@ public IEnumerable<LuaIndexExprSyntax> QueryIndexExprReferences(string fieldName
317327
{
318328
foreach (var ptr in MultiIndexExpr.Query(fieldName))
319329
{
320-
if (ptr.ToNode(context) is {} node)
330+
if (ptr.ToNode(context) is { } node)
321331
{
322332
yield return node;
323333
}
@@ -328,7 +338,7 @@ public IEnumerable<LuaNameExprSyntax> QueryNameExprReferences(string name, Searc
328338
{
329339
foreach (var ptr in NameExpr.Query(name))
330340
{
331-
if (ptr.ToNode(context) is {} node)
341+
if (ptr.ToNode(context) is { } node)
332342
{
333343
yield return node;
334344
}
@@ -339,7 +349,7 @@ public IEnumerable<LuaDocNameTypeSyntax> QueryNamedTypeReferences(string name, S
339349
{
340350
foreach (var ptr in NameType.Query(name))
341351
{
342-
if (ptr.ToNode(context) is {} node)
352+
if (ptr.ToNode(context) is { } node)
343353
{
344354
yield return node;
345355
}
@@ -355,5 +365,6 @@ public IEnumerable<LuaDeclaration> QueryAllMembers()
355365
{
356366
return TypeIndex.QueryAllMembers();
357367
}
368+
358369
#endregion
359370
}

0 commit comments

Comments
 (0)