Skip to content

Commit f08d933

Browse files
committed
Fix index member and global referencs bug
1 parent 681f4de commit f08d933

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

EmmyLua/CodeAnalysis/Compilation/Search/IndexMembers.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ public class IndexMembers(SearchContext context)
354354
{
355355
return new LuaSymbol(string.Empty, arrayType.BaseType, new VirtualInfo());
356356
}
357+
else if (type is LuaGenericType genericType)
358+
{
359+
return FindGenericTypeMember(genericType, indexExpr);
360+
}
357361
else if (type is LuaNamedType namedType && indexExpr.IndexKeyExpr is { } indexKeyExpr)
358362
{
359363
var keyType = context.Infer(indexKeyExpr);
@@ -426,4 +430,26 @@ public class IndexMembers(SearchContext context)
426430

427431
return null;
428432
}
433+
434+
private LuaSymbol? FindGenericTypeMember(LuaGenericType type, LuaIndexExprSyntax indexExpr)
435+
{
436+
if (type.Name == "table")
437+
{
438+
if (type.GenericArgs.Count != 2)
439+
{
440+
return null;
441+
}
442+
443+
if (indexExpr.IndexKeyExpr is { } indexKeyExpr)
444+
{
445+
var keyType = context.Infer(indexKeyExpr);
446+
if (keyType.SubTypeOf(type.GenericArgs[0], context))
447+
{
448+
return new LuaSymbol(string.Empty, type.GenericArgs[1], new VirtualInfo());
449+
}
450+
}
451+
}
452+
453+
return null;
454+
}
429455
}

EmmyLua/CodeAnalysis/Compilation/Search/References.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private IEnumerable<ReferenceResult> GlobalReferences(LuaSymbol symbol)
6868

6969
foreach (var nameExpr in nameExprs)
7070
{
71-
if (context.FindDeclaration(nameExpr) == symbol)
71+
if (IsReferenceTo(nameExpr, symbol))
7272
{
7373
references.Add(new ReferenceResult(nameExpr.Location, nameExpr));
7474
}

EmmyLua/CodeAnalysis/Syntax/Node/SyntaxNodes/Basic.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public LuaDocTypeSyntax? TypeField
166166
{
167167
if (i + 1 < finish)
168168
{
169-
var element = Tree.GetElement(i);
169+
var element = Tree.GetElement(i + 1);
170170
if (element is LuaDocTypeSyntax typeSyntax)
171171
{
172172
return typeSyntax;

EmmyLua/CodeAnalysis/Syntax/Node/SyntaxNodes/Expressions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public LuaExprSyntax? IndexKeyExpr
220220
{
221221
if (i + 1 < finish)
222222
{
223-
var element = Tree.GetElement(i);
223+
var element = Tree.GetElement(i + 1);
224224
if (element is LuaExprSyntax exprSyntax)
225225
{
226226
return exprSyntax;

0 commit comments

Comments
 (0)