Skip to content

Commit

Permalink
optimize hover, fix infer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jun 20, 2024
1 parent cbf54e1 commit 8506089
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 355 deletions.
49 changes: 25 additions & 24 deletions EmmyLua.Cli/DocGenerator/DocGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,33 @@ private LuaWorkspace LoadLuaWorkspace()
return LuaWorkspace.Create(workspacePath, settingManager.GetLuaFeatures());
}

// TODO: Generate APIs
private void GenerateApis(List<TocItem> rootTocItems)
{
var luaWorkspace = LoadLuaWorkspace();
var tocItems = new List<TocItem>();
foreach (var module in luaWorkspace.ModuleManager.GetAllModules())
{
if (module.Workspace == luaWorkspace.MainWorkspace)
{
var renderer = new ModuleDoc(luaWorkspace.Compilation, module);
var text = renderer.Build();
var fileName = $"{module.ModulePath}.md";
tocItems.Add(new TocItem()
{
Name = module.ModulePath,
Href = fileName
});
File.WriteAllText(Path.Combine(ApisPath, fileName), text);
}
}

rootTocItems.Add(new TocItem()
{
Name = "APIs",
Href = "apis/"
});
GenerateToc(ApisPath, tocItems);
// var luaWorkspace = LoadLuaWorkspace();
// var tocItems = new List<TocItem>();
// foreach (var module in luaWorkspace.ModuleManager.GetAllModules())
// {
// if (module.Workspace == luaWorkspace.MainWorkspace)
// {
// var renderer = new ModuleDoc(luaWorkspace.Compilation, module);
// var text = renderer.Build();
// var fileName = $"{module.ModulePath}.md";
// tocItems.Add(new TocItem()
// {
// Name = module.ModulePath,
// Href = fileName
// });
// File.WriteAllText(Path.Combine(ApisPath, fileName), text);
// }
// }
//
// rootTocItems.Add(new TocItem()
// {
// Name = "APIs",
// Href = "apis/"
// });
// GenerateToc(ApisPath, tocItems);
}

private void GenerateToc(string path, List<TocItem> tocItems)
Expand Down
236 changes: 118 additions & 118 deletions EmmyLua.Cli/DocGenerator/Markdown/ModuleDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,127 +2,127 @@
using EmmyLua.CodeAnalysis.Compilation.Declaration;
using EmmyLua.CodeAnalysis.Compilation.Infer;
using EmmyLua.CodeAnalysis.Compilation.Search;
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render.Renderer;
// using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
// using EmmyLua.CodeAnalysis.Compilation.Semantic.Render.Renderer;
using EmmyLua.CodeAnalysis.Document;
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
using EmmyLua.CodeAnalysis.Workspace.Module;

namespace EmmyLua.Cli.DocGenerator.Markdown;

public class ModuleDoc
{
private SearchContext SearchContext { get; }

private LuaRenderContext RenderContext { get; }

private LuaRenderBuilder RenderBuilder { get; }

private ModuleIndex ModuleIndex { get; }

private LuaRenderFeature Feature { get; } = new LuaRenderFeature()
{
ShowTypeLink = false,
ExpandAlias = false,
};

public ModuleDoc(LuaCompilation compilation, ModuleIndex moduleIndex)
{
SearchContext = new SearchContext(compilation, new SearchContextFeatures());
RenderBuilder = new LuaRenderBuilder(SearchContext);
ModuleIndex = moduleIndex;
RenderContext = new LuaRenderContext(SearchContext, Feature);
}

public string Build()
{
RenderContext.AddH1Title($"module {ModuleIndex.ModulePath}");
var document = SearchContext.Compilation.Workspace.GetDocument(ModuleIndex.DocumentId);
if (document is null)
{
return RenderContext.GetText();
}

RenderModuleDescription(document);
RenderContext.AppendLine();

RenderContext.AddH2Title("Public members:");
RenderContext.AddSeparator();
RenderModuleMembers(document);

return RenderContext.GetText();
}

private void RenderModuleDescription(LuaDocument document)
{
RenderContext.Append(RenderBuilder.RenderModule(document, Feature));
}

private IEnumerable<LuaFuncStatSyntax> GetModuleStats(LuaDocument document)
{
if (document.SyntaxTree.SyntaxRoot.Block is { StatList: { } statList })
{
foreach (var funcStat in statList.OfType<LuaFuncStatSyntax>())
{
yield return funcStat;
}
}
}

private void RenderModuleMembers(LuaDocument document)
{
foreach (var funcStat in GetModuleStats(document))
{
if (funcStat is { NameElement.Parent: { } node })
{
var declaration = SearchContext.FindDeclaration(node);
if (declaration is LuaDeclaration luaDeclaration)
{
RenderFuncDeclaration(luaDeclaration, funcStat);
RenderContext.AddSeparator();
}
}
}
}

private void RenderFuncDeclaration(LuaDeclaration declaration, LuaFuncStatSyntax funcStat)
{
if (declaration.IsLocal || declaration.IsPrivate)
{
return;
}

var asyncText = declaration.IsAsync ? "async " : string.Empty;

if (declaration.Info is MethodInfo methodInfo)
{
if (methodInfo.IndexPtr.ToNode(SearchContext) is { } indexExpr)
{
RenderContext.WrapperLua(() =>
{
RenderContext.Append($"{asyncText}function {indexExpr.Text}");
LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
});
}
else if (methodInfo.NamePtr.ToNode(SearchContext) is { } nameExpr)
{
RenderContext.WrapperLua(() =>
{
RenderContext.Append($"{asyncText}function {nameExpr.Text}");
LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
});
}

var comments = funcStat.Comments;
foreach (var comment in comments)
{
if (comment.CommentText is { Length: > 0 } commentText)
{
RenderContext.Append(commentText);
}

RenderContext.AppendLine();
}
}
}
}
// public class ModuleDoc
// {
// private SearchContext SearchContext { get; }
//
// private LuaRenderContext RenderContext { get; }
//
// private LuaRenderBuilder RenderBuilder { get; }
//
// private ModuleIndex ModuleIndex { get; }
//
// private LuaRenderFeature Feature { get; } = new LuaRenderFeature()
// {
// ShowTypeLink = false,
// ExpandAlias = false,
// };
//
// public ModuleDoc(LuaCompilation compilation, ModuleIndex moduleIndex)
// {
// SearchContext = new SearchContext(compilation, new SearchContextFeatures());
// RenderBuilder = new LuaRenderBuilder(SearchContext);
// ModuleIndex = moduleIndex;
// RenderContext = new LuaRenderContext(SearchContext, Feature);
// }
//
// public string Build()
// {
// RenderContext.AddH1Title($"module {ModuleIndex.ModulePath}");
// var document = SearchContext.Compilation.Workspace.GetDocument(ModuleIndex.DocumentId);
// if (document is null)
// {
// return RenderContext.GetText();
// }
//
// RenderModuleDescription(document);
// RenderContext.AppendLine();
//
// RenderContext.AddH2Title("Public members:");
// RenderContext.AddSeparator();
// RenderModuleMembers(document);
//
// return RenderContext.GetText();
// }
//
// private void RenderModuleDescription(LuaDocument document)
// {
// RenderContext.Append(RenderBuilder.RenderModule(document, Feature));
// }
//
// private IEnumerable<LuaFuncStatSyntax> GetModuleStats(LuaDocument document)
// {
// if (document.SyntaxTree.SyntaxRoot.Block is { StatList: { } statList })
// {
// foreach (var funcStat in statList.OfType<LuaFuncStatSyntax>())
// {
// yield return funcStat;
// }
// }
// }
//
// private void RenderModuleMembers(LuaDocument document)
// {
// foreach (var funcStat in GetModuleStats(document))
// {
// if (funcStat is { NameElement.Parent: { } node })
// {
// var declaration = SearchContext.FindDeclaration(node);
// if (declaration is LuaDeclaration luaDeclaration)
// {
// RenderFuncDeclaration(luaDeclaration, funcStat);
// RenderContext.AddSeparator();
// }
// }
// }
// }
//
// private void RenderFuncDeclaration(LuaDeclaration declaration, LuaFuncStatSyntax funcStat)
// {
// if (declaration.IsLocal || declaration.IsPrivate)
// {
// return;
// }
//
// var asyncText = declaration.IsAsync ? "async " : string.Empty;
//
// if (declaration.Info is MethodInfo methodInfo)
// {
// if (methodInfo.IndexPtr.ToNode(SearchContext) is { } indexExpr)
// {
// RenderContext.WrapperLua(() =>
// {
// RenderContext.Append($"{asyncText}function {indexExpr.Text}");
// LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
// });
// }
// else if (methodInfo.NamePtr.ToNode(SearchContext) is { } nameExpr)
// {
// RenderContext.WrapperLua(() =>
// {
// RenderContext.Append($"{asyncText}function {nameExpr.Text}");
// LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
// });
// }
//
// var comments = funcStat.Comments;
// foreach (var comment in comments)
// {
// if (comment.CommentText is { Length: > 0 } commentText)
// {
// RenderContext.Append(commentText);
// }
//
// RenderContext.AppendLine();
// }
// }
// }
// }
5 changes: 4 additions & 1 deletion EmmyLua.LanguageServer/Completion/CompleteContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using EmmyLua.CodeAnalysis.Compilation.Semantic;
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
using EmmyLua.CodeAnalysis.Compilation.Type;
using EmmyLua.CodeAnalysis.Syntax.Node;
using EmmyLua.LanguageServer.Server;
using EmmyLua.LanguageServer.Server.Render;
using EmmyLua.LanguageServer.Util;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

Expand Down Expand Up @@ -34,6 +34,8 @@ public class CompleteContext
false,
100
);

public LuaRenderBuilder RenderBuilder { get; }

// ReSharper disable once ConvertToPrimaryConstructor
public CompleteContext(SemanticModel semanticModel, Position position, CancellationToken cancellationToken,
Expand All @@ -47,6 +49,7 @@ public CompleteContext(SemanticModel semanticModel, Position position, Cancellat
semanticModel.Document.SyntaxTree.SyntaxRoot.TokenLeftBiasedAt(position.Line, position.Character);
CompletionConfig = context.SettingManager.GetCompletionConfig();
ServerContext = context;
RenderBuilder = new LuaRenderBuilder(semanticModel.Context);
}

public void Add(CompletionItem item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void AddCompletion(CompleteContext context)
LabelDetails = new CompletionItemLabelDetails()
{
Detail = $" (in {module.ModulePath})",
Description = context.SemanticModel.RenderBuilder.RenderType(retTy, context.RenderFeature)
Description = context.RenderBuilder.RenderType(retTy, context.RenderFeature)
},
Data = module.DocumentId.Id.ToString(),
Command = AutoRequire.MakeCommand(
Expand Down
13 changes: 8 additions & 5 deletions EmmyLua.LanguageServer/Completion/CompletionDocumentResolver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
using EmmyLua.CodeAnalysis.Document;
using EmmyLua.CodeAnalysis.Document;
using EmmyLua.CodeAnalysis.Syntax.Node;
using EmmyLua.LanguageServer.Server;
using EmmyLua.LanguageServer.Server.Render;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;

Expand All @@ -14,7 +14,8 @@ public class CompletionDocumentResolver
ExpandAlias = true,
ShowTypeLink = false,
InHint = false,
MaxStringPreviewLength = 100
MaxStringPreviewLength = 100,
InHover = true
};

public CompletionItem Resolve(CompletionItem completionItem, ServerContext context)
Expand All @@ -41,12 +42,13 @@ private CompletionItem ModuleResolve(CompletionItem completionItem, ServerContex
var id = new LuaDocumentId((int) completionItem.Data);
if (context.GetSemanticModel(id) is {} semanticModel)
{
var renderBuilder = new LuaRenderBuilder(semanticModel.Context);
completionItem = completionItem with
{
Documentation = new MarkupContent()
{
Kind = MarkupKind.Markdown,
Value = semanticModel.RenderBuilder.RenderModule(semanticModel.Document, RenderFeature)
Value = renderBuilder.RenderModule(semanticModel.Document, RenderFeature)
}
};
}
Expand All @@ -70,12 +72,13 @@ private CompletionItem GeneralResolve(CompletionItem completionItem, ServerConte

if (context.GetSemanticModel(ptr.DocumentId) is {} semanticModel)
{
var renderBuilder = new LuaRenderBuilder(semanticModel.Context);
return completionItem with
{
Documentation = new MarkupContent()
{
Kind = MarkupKind.Markdown,
Value = semanticModel.RenderBuilder.Render(node, RenderFeature)
Value = renderBuilder.Render(node, RenderFeature)
}
};
}
Expand Down
Loading

0 comments on commit 8506089

Please sign in to comment.