Skip to content

Commit

Permalink
support stupid require path
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jul 19, 2024
1 parent 23c12af commit 55aa265
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace EmmyLua.LanguageServer.Completion.CompleteProvider;

public class RequireProvider : ICompleteProviderBase
{
// support stupid code
private char[] Separators { get; } = ['.', '/', '\\'];

public void AddCompletion(CompleteContext context)
{
var trigger = context.TriggerToken;
Expand All @@ -15,23 +18,23 @@ public void AddCompletion(CompleteContext context)
{
var moduleInfos =
context.SemanticModel.Compilation.Workspace.ModuleManager.GetCurrentModuleNames(modulePathToken.Value);

var modulePath = modulePathToken.Value;
var parts = modulePath.Split('.');
var index = modulePath.LastIndexOfAny(Separators);
var moduleBase = string.Empty;
if (parts.Length > 1)
if (index != -1)
{
moduleBase = string.Join('.', parts[..^1]);
moduleBase = modulePath[..(index + 1)];
}

foreach (var moduleInfo in moduleInfos)
{
var filterText = moduleInfo.Name;
if (moduleBase.Length != 0)
{
filterText = $"{moduleBase}.{filterText}";
filterText = $"{moduleBase}{filterText}";
}

context.Add(new CompletionItem
{
Label = moduleInfo.Name,
Expand All @@ -42,6 +45,7 @@ public void AddCompletion(CompleteContext context)
Data = new LSPAny(moduleInfo.DocumentId?.Id.ToString())
});
}

context.StopHere();
}
}
Expand Down
4 changes: 2 additions & 2 deletions EmmyLua.LanguageServer/Definition/DefinitionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DefinitionHandler(ServerContext context) : DefinitionHandlerBase
{
var document = semanticModel.Document;
var pos = request.Position;
var token = document.SyntaxTree.SyntaxRoot.TokenAt((int)pos.Line, (int)pos.Character);
var token = document.SyntaxTree.SyntaxRoot.TokenAt(pos.Line, pos.Character);
if (token is LuaStringToken module
&& token.Parent?.Parent?.Parent is LuaCallExprSyntax { Name: { } funcName }
&& workspace.Features.RequireLikeFunction.Contains(funcName))
Expand All @@ -38,7 +38,7 @@ public class DefinitionHandler(ServerContext context) : DefinitionHandlerBase
}
}

var node = document.SyntaxTree.SyntaxRoot.NameNodeAt((int)pos.Line, (int)pos.Character);
var node = document.SyntaxTree.SyntaxRoot.NameNodeAt(pos.Line, pos.Character);
if (node is not null)
{
var declaration = semanticModel.Context.FindDeclaration(node);
Expand Down
16 changes: 8 additions & 8 deletions EmmyLua.LanguageServer/DocumentLink/DocumentLinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ public class DocumentLinkBuilder(ServerContext context)
foreach (var stringToken in stringTokens)
{
var path = stringToken.Value;
if (resourceManager.MayFilePath(path))
if (IsModule(stringToken))
{
var targetPath = resourceManager.ResolvePath(path);
if (targetPath is not null)
var moduleDocument = context.LuaWorkspace.ModuleManager.FindModule(path);
if (moduleDocument is not null)
{
var link = new Framework.Protocol.Message.DocumentLink.DocumentLink
{
Range = stringToken.Range.ToLspRange(document),
Target = targetPath
Target = moduleDocument.Uri
};
links.Add(link);
}
}
else if (IsModule(stringToken))
else if (resourceManager.MayFilePath(path))
{
var moduleDocument = context.LuaWorkspace.ModuleManager.FindModule(path);
if (moduleDocument is not null)
var targetPath = resourceManager.ResolvePath(path);
if (targetPath is not null)
{
var link = new Framework.Protocol.Message.DocumentLink.DocumentLink
{
Range = stringToken.Range.ToLspRange(document),
Target = moduleDocument.Uri
Target = targetPath
};
links.Add(link);
}
Expand Down
10 changes: 9 additions & 1 deletion EmmyLua/CodeAnalysis/Workspace/Module/ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,16 @@ public void AddVirtualModule(LuaDocumentId documentId, string modulePath)
root.AddModule(modulePath.Split('.'), documentId);
}

// workaround for stupid code
private char[] Separators { get; } = [ '/', '\\', '.' ];

public LuaDocument? FindModule(string modulePath)
{
if (modulePath.IndexOfAny(Separators) != -1)
{
modulePath = modulePath.Replace(Separators[0], '.').Replace(Separators[1], '.');
}

foreach (var moduleNode in WorkspaceModule)
{
var documentId = moduleNode.Value.FindModule(modulePath);
Expand Down Expand Up @@ -262,7 +270,7 @@ public void AddVirtualModule(LuaDocumentId documentId, string modulePath)
public List<ModuleInfo> GetCurrentModuleNames(string modulePath)
{
var moduleInfos = new List<ModuleInfo>();
var parts = modulePath.Split('.');
var parts = modulePath.Split(Separators);
if (parts.Length <= 1)
{
foreach (var moduleNode in WorkspaceModule)
Expand Down
1 change: 1 addition & 0 deletions EmmyLua/CodeAnalysis/Workspace/Module/ModuleNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void AddModule(IEnumerable<string> modulePaths, LuaDocumentId documentId)
node.DocumentId = documentId;
}


public LuaDocumentId? FindModule(string modulePath)
{
var modulePaths = modulePath.Split('.');
Expand Down

0 comments on commit 55aa265

Please sign in to comment.