Skip to content

Commit 55aa265

Browse files
committed
support stupid require path
1 parent 23c12af commit 55aa265

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

EmmyLua.LanguageServer/Completion/CompleteProvider/RequireProvider.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace EmmyLua.LanguageServer.Completion.CompleteProvider;
66

77
public class RequireProvider : ICompleteProviderBase
88
{
9+
// support stupid code
10+
private char[] Separators { get; } = ['.', '/', '\\'];
11+
912
public void AddCompletion(CompleteContext context)
1013
{
1114
var trigger = context.TriggerToken;
@@ -15,23 +18,23 @@ public void AddCompletion(CompleteContext context)
1518
{
1619
var moduleInfos =
1720
context.SemanticModel.Compilation.Workspace.ModuleManager.GetCurrentModuleNames(modulePathToken.Value);
18-
21+
1922
var modulePath = modulePathToken.Value;
20-
var parts = modulePath.Split('.');
23+
var index = modulePath.LastIndexOfAny(Separators);
2124
var moduleBase = string.Empty;
22-
if (parts.Length > 1)
25+
if (index != -1)
2326
{
24-
moduleBase = string.Join('.', parts[..^1]);
27+
moduleBase = modulePath[..(index + 1)];
2528
}
2629

2730
foreach (var moduleInfo in moduleInfos)
2831
{
2932
var filterText = moduleInfo.Name;
3033
if (moduleBase.Length != 0)
3134
{
32-
filterText = $"{moduleBase}.{filterText}";
35+
filterText = $"{moduleBase}{filterText}";
3336
}
34-
37+
3538
context.Add(new CompletionItem
3639
{
3740
Label = moduleInfo.Name,
@@ -42,6 +45,7 @@ public void AddCompletion(CompleteContext context)
4245
Data = new LSPAny(moduleInfo.DocumentId?.Id.ToString())
4346
});
4447
}
48+
4549
context.StopHere();
4650
}
4751
}

EmmyLua.LanguageServer/Definition/DefinitionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class DefinitionHandler(ServerContext context) : DefinitionHandlerBase
2323
{
2424
var document = semanticModel.Document;
2525
var pos = request.Position;
26-
var token = document.SyntaxTree.SyntaxRoot.TokenAt((int)pos.Line, (int)pos.Character);
26+
var token = document.SyntaxTree.SyntaxRoot.TokenAt(pos.Line, pos.Character);
2727
if (token is LuaStringToken module
2828
&& token.Parent?.Parent?.Parent is LuaCallExprSyntax { Name: { } funcName }
2929
&& workspace.Features.RequireLikeFunction.Contains(funcName))
@@ -38,7 +38,7 @@ public class DefinitionHandler(ServerContext context) : DefinitionHandlerBase
3838
}
3939
}
4040

41-
var node = document.SyntaxTree.SyntaxRoot.NameNodeAt((int)pos.Line, (int)pos.Character);
41+
var node = document.SyntaxTree.SyntaxRoot.NameNodeAt(pos.Line, pos.Character);
4242
if (node is not null)
4343
{
4444
var declaration = semanticModel.Context.FindDeclaration(node);

EmmyLua.LanguageServer/DocumentLink/DocumentLinkBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ public class DocumentLinkBuilder(ServerContext context)
1818
foreach (var stringToken in stringTokens)
1919
{
2020
var path = stringToken.Value;
21-
if (resourceManager.MayFilePath(path))
21+
if (IsModule(stringToken))
2222
{
23-
var targetPath = resourceManager.ResolvePath(path);
24-
if (targetPath is not null)
23+
var moduleDocument = context.LuaWorkspace.ModuleManager.FindModule(path);
24+
if (moduleDocument is not null)
2525
{
2626
var link = new Framework.Protocol.Message.DocumentLink.DocumentLink
2727
{
2828
Range = stringToken.Range.ToLspRange(document),
29-
Target = targetPath
29+
Target = moduleDocument.Uri
3030
};
3131
links.Add(link);
3232
}
3333
}
34-
else if (IsModule(stringToken))
34+
else if (resourceManager.MayFilePath(path))
3535
{
36-
var moduleDocument = context.LuaWorkspace.ModuleManager.FindModule(path);
37-
if (moduleDocument is not null)
36+
var targetPath = resourceManager.ResolvePath(path);
37+
if (targetPath is not null)
3838
{
3939
var link = new Framework.Protocol.Message.DocumentLink.DocumentLink
4040
{
4141
Range = stringToken.Range.ToLspRange(document),
42-
Target = moduleDocument.Uri
42+
Target = targetPath
4343
};
4444
links.Add(link);
4545
}

EmmyLua/CodeAnalysis/Workspace/Module/ModuleManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,16 @@ public void AddVirtualModule(LuaDocumentId documentId, string modulePath)
211211
root.AddModule(modulePath.Split('.'), documentId);
212212
}
213213

214+
// workaround for stupid code
215+
private char[] Separators { get; } = [ '/', '\\', '.' ];
216+
214217
public LuaDocument? FindModule(string modulePath)
215218
{
219+
if (modulePath.IndexOfAny(Separators) != -1)
220+
{
221+
modulePath = modulePath.Replace(Separators[0], '.').Replace(Separators[1], '.');
222+
}
223+
216224
foreach (var moduleNode in WorkspaceModule)
217225
{
218226
var documentId = moduleNode.Value.FindModule(modulePath);
@@ -262,7 +270,7 @@ public void AddVirtualModule(LuaDocumentId documentId, string modulePath)
262270
public List<ModuleInfo> GetCurrentModuleNames(string modulePath)
263271
{
264272
var moduleInfos = new List<ModuleInfo>();
265-
var parts = modulePath.Split('.');
273+
var parts = modulePath.Split(Separators);
266274
if (parts.Length <= 1)
267275
{
268276
foreach (var moduleNode in WorkspaceModule)

EmmyLua/CodeAnalysis/Workspace/Module/ModuleNode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void AddModule(IEnumerable<string> modulePaths, LuaDocumentId documentId)
5757
node.DocumentId = documentId;
5858
}
5959

60+
6061
public LuaDocumentId? FindModule(string modulePath)
6162
{
6263
var modulePaths = modulePath.Split('.');

0 commit comments

Comments
 (0)