Skip to content

Commit 8506089

Browse files
committed
optimize hover, fix infer bug
1 parent cbf54e1 commit 8506089

File tree

22 files changed

+380
-355
lines changed

22 files changed

+380
-355
lines changed

EmmyLua.Cli/DocGenerator/DocGenerator.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,33 @@ private LuaWorkspace LoadLuaWorkspace()
8787
return LuaWorkspace.Create(workspacePath, settingManager.GetLuaFeatures());
8888
}
8989

90+
// TODO: Generate APIs
9091
private void GenerateApis(List<TocItem> rootTocItems)
9192
{
92-
var luaWorkspace = LoadLuaWorkspace();
93-
var tocItems = new List<TocItem>();
94-
foreach (var module in luaWorkspace.ModuleManager.GetAllModules())
95-
{
96-
if (module.Workspace == luaWorkspace.MainWorkspace)
97-
{
98-
var renderer = new ModuleDoc(luaWorkspace.Compilation, module);
99-
var text = renderer.Build();
100-
var fileName = $"{module.ModulePath}.md";
101-
tocItems.Add(new TocItem()
102-
{
103-
Name = module.ModulePath,
104-
Href = fileName
105-
});
106-
File.WriteAllText(Path.Combine(ApisPath, fileName), text);
107-
}
108-
}
109-
110-
rootTocItems.Add(new TocItem()
111-
{
112-
Name = "APIs",
113-
Href = "apis/"
114-
});
115-
GenerateToc(ApisPath, tocItems);
93+
// var luaWorkspace = LoadLuaWorkspace();
94+
// var tocItems = new List<TocItem>();
95+
// foreach (var module in luaWorkspace.ModuleManager.GetAllModules())
96+
// {
97+
// if (module.Workspace == luaWorkspace.MainWorkspace)
98+
// {
99+
// var renderer = new ModuleDoc(luaWorkspace.Compilation, module);
100+
// var text = renderer.Build();
101+
// var fileName = $"{module.ModulePath}.md";
102+
// tocItems.Add(new TocItem()
103+
// {
104+
// Name = module.ModulePath,
105+
// Href = fileName
106+
// });
107+
// File.WriteAllText(Path.Combine(ApisPath, fileName), text);
108+
// }
109+
// }
110+
//
111+
// rootTocItems.Add(new TocItem()
112+
// {
113+
// Name = "APIs",
114+
// Href = "apis/"
115+
// });
116+
// GenerateToc(ApisPath, tocItems);
116117
}
117118

118119
private void GenerateToc(string path, List<TocItem> tocItems)

EmmyLua.Cli/DocGenerator/Markdown/ModuleDoc.cs

Lines changed: 118 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,127 +2,127 @@
22
using EmmyLua.CodeAnalysis.Compilation.Declaration;
33
using EmmyLua.CodeAnalysis.Compilation.Infer;
44
using EmmyLua.CodeAnalysis.Compilation.Search;
5-
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
6-
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render.Renderer;
5+
// using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
6+
// using EmmyLua.CodeAnalysis.Compilation.Semantic.Render.Renderer;
77
using EmmyLua.CodeAnalysis.Document;
88
using EmmyLua.CodeAnalysis.Syntax.Node.SyntaxNodes;
99
using EmmyLua.CodeAnalysis.Workspace.Module;
1010

1111
namespace EmmyLua.Cli.DocGenerator.Markdown;
1212

13-
public class ModuleDoc
14-
{
15-
private SearchContext SearchContext { get; }
16-
17-
private LuaRenderContext RenderContext { get; }
18-
19-
private LuaRenderBuilder RenderBuilder { get; }
20-
21-
private ModuleIndex ModuleIndex { get; }
22-
23-
private LuaRenderFeature Feature { get; } = new LuaRenderFeature()
24-
{
25-
ShowTypeLink = false,
26-
ExpandAlias = false,
27-
};
28-
29-
public ModuleDoc(LuaCompilation compilation, ModuleIndex moduleIndex)
30-
{
31-
SearchContext = new SearchContext(compilation, new SearchContextFeatures());
32-
RenderBuilder = new LuaRenderBuilder(SearchContext);
33-
ModuleIndex = moduleIndex;
34-
RenderContext = new LuaRenderContext(SearchContext, Feature);
35-
}
36-
37-
public string Build()
38-
{
39-
RenderContext.AddH1Title($"module {ModuleIndex.ModulePath}");
40-
var document = SearchContext.Compilation.Workspace.GetDocument(ModuleIndex.DocumentId);
41-
if (document is null)
42-
{
43-
return RenderContext.GetText();
44-
}
45-
46-
RenderModuleDescription(document);
47-
RenderContext.AppendLine();
48-
49-
RenderContext.AddH2Title("Public members:");
50-
RenderContext.AddSeparator();
51-
RenderModuleMembers(document);
52-
53-
return RenderContext.GetText();
54-
}
55-
56-
private void RenderModuleDescription(LuaDocument document)
57-
{
58-
RenderContext.Append(RenderBuilder.RenderModule(document, Feature));
59-
}
60-
61-
private IEnumerable<LuaFuncStatSyntax> GetModuleStats(LuaDocument document)
62-
{
63-
if (document.SyntaxTree.SyntaxRoot.Block is { StatList: { } statList })
64-
{
65-
foreach (var funcStat in statList.OfType<LuaFuncStatSyntax>())
66-
{
67-
yield return funcStat;
68-
}
69-
}
70-
}
71-
72-
private void RenderModuleMembers(LuaDocument document)
73-
{
74-
foreach (var funcStat in GetModuleStats(document))
75-
{
76-
if (funcStat is { NameElement.Parent: { } node })
77-
{
78-
var declaration = SearchContext.FindDeclaration(node);
79-
if (declaration is LuaDeclaration luaDeclaration)
80-
{
81-
RenderFuncDeclaration(luaDeclaration, funcStat);
82-
RenderContext.AddSeparator();
83-
}
84-
}
85-
}
86-
}
87-
88-
private void RenderFuncDeclaration(LuaDeclaration declaration, LuaFuncStatSyntax funcStat)
89-
{
90-
if (declaration.IsLocal || declaration.IsPrivate)
91-
{
92-
return;
93-
}
94-
95-
var asyncText = declaration.IsAsync ? "async " : string.Empty;
96-
97-
if (declaration.Info is MethodInfo methodInfo)
98-
{
99-
if (methodInfo.IndexPtr.ToNode(SearchContext) is { } indexExpr)
100-
{
101-
RenderContext.WrapperLua(() =>
102-
{
103-
RenderContext.Append($"{asyncText}function {indexExpr.Text}");
104-
LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
105-
});
106-
}
107-
else if (methodInfo.NamePtr.ToNode(SearchContext) is { } nameExpr)
108-
{
109-
RenderContext.WrapperLua(() =>
110-
{
111-
RenderContext.Append($"{asyncText}function {nameExpr.Text}");
112-
LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
113-
});
114-
}
115-
116-
var comments = funcStat.Comments;
117-
foreach (var comment in comments)
118-
{
119-
if (comment.CommentText is { Length: > 0 } commentText)
120-
{
121-
RenderContext.Append(commentText);
122-
}
123-
124-
RenderContext.AppendLine();
125-
}
126-
}
127-
}
128-
}
13+
// public class ModuleDoc
14+
// {
15+
// private SearchContext SearchContext { get; }
16+
//
17+
// private LuaRenderContext RenderContext { get; }
18+
//
19+
// private LuaRenderBuilder RenderBuilder { get; }
20+
//
21+
// private ModuleIndex ModuleIndex { get; }
22+
//
23+
// private LuaRenderFeature Feature { get; } = new LuaRenderFeature()
24+
// {
25+
// ShowTypeLink = false,
26+
// ExpandAlias = false,
27+
// };
28+
//
29+
// public ModuleDoc(LuaCompilation compilation, ModuleIndex moduleIndex)
30+
// {
31+
// SearchContext = new SearchContext(compilation, new SearchContextFeatures());
32+
// RenderBuilder = new LuaRenderBuilder(SearchContext);
33+
// ModuleIndex = moduleIndex;
34+
// RenderContext = new LuaRenderContext(SearchContext, Feature);
35+
// }
36+
//
37+
// public string Build()
38+
// {
39+
// RenderContext.AddH1Title($"module {ModuleIndex.ModulePath}");
40+
// var document = SearchContext.Compilation.Workspace.GetDocument(ModuleIndex.DocumentId);
41+
// if (document is null)
42+
// {
43+
// return RenderContext.GetText();
44+
// }
45+
//
46+
// RenderModuleDescription(document);
47+
// RenderContext.AppendLine();
48+
//
49+
// RenderContext.AddH2Title("Public members:");
50+
// RenderContext.AddSeparator();
51+
// RenderModuleMembers(document);
52+
//
53+
// return RenderContext.GetText();
54+
// }
55+
//
56+
// private void RenderModuleDescription(LuaDocument document)
57+
// {
58+
// RenderContext.Append(RenderBuilder.RenderModule(document, Feature));
59+
// }
60+
//
61+
// private IEnumerable<LuaFuncStatSyntax> GetModuleStats(LuaDocument document)
62+
// {
63+
// if (document.SyntaxTree.SyntaxRoot.Block is { StatList: { } statList })
64+
// {
65+
// foreach (var funcStat in statList.OfType<LuaFuncStatSyntax>())
66+
// {
67+
// yield return funcStat;
68+
// }
69+
// }
70+
// }
71+
//
72+
// private void RenderModuleMembers(LuaDocument document)
73+
// {
74+
// foreach (var funcStat in GetModuleStats(document))
75+
// {
76+
// if (funcStat is { NameElement.Parent: { } node })
77+
// {
78+
// var declaration = SearchContext.FindDeclaration(node);
79+
// if (declaration is LuaDeclaration luaDeclaration)
80+
// {
81+
// RenderFuncDeclaration(luaDeclaration, funcStat);
82+
// RenderContext.AddSeparator();
83+
// }
84+
// }
85+
// }
86+
// }
87+
//
88+
// private void RenderFuncDeclaration(LuaDeclaration declaration, LuaFuncStatSyntax funcStat)
89+
// {
90+
// if (declaration.IsLocal || declaration.IsPrivate)
91+
// {
92+
// return;
93+
// }
94+
//
95+
// var asyncText = declaration.IsAsync ? "async " : string.Empty;
96+
//
97+
// if (declaration.Info is MethodInfo methodInfo)
98+
// {
99+
// if (methodInfo.IndexPtr.ToNode(SearchContext) is { } indexExpr)
100+
// {
101+
// RenderContext.WrapperLua(() =>
102+
// {
103+
// RenderContext.Append($"{asyncText}function {indexExpr.Text}");
104+
// LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
105+
// });
106+
// }
107+
// else if (methodInfo.NamePtr.ToNode(SearchContext) is { } nameExpr)
108+
// {
109+
// RenderContext.WrapperLua(() =>
110+
// {
111+
// RenderContext.Append($"{asyncText}function {nameExpr.Text}");
112+
// LuaTypeRenderer.RenderFunc(methodInfo.Method, RenderContext);
113+
// });
114+
// }
115+
//
116+
// var comments = funcStat.Comments;
117+
// foreach (var comment in comments)
118+
// {
119+
// if (comment.CommentText is { Length: > 0 } commentText)
120+
// {
121+
// RenderContext.Append(commentText);
122+
// }
123+
//
124+
// RenderContext.AppendLine();
125+
// }
126+
// }
127+
// }
128+
// }

EmmyLua.LanguageServer/Completion/CompleteContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using EmmyLua.CodeAnalysis.Compilation.Semantic;
2-
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
32
using EmmyLua.CodeAnalysis.Compilation.Type;
43
using EmmyLua.CodeAnalysis.Syntax.Node;
54
using EmmyLua.LanguageServer.Server;
5+
using EmmyLua.LanguageServer.Server.Render;
66
using EmmyLua.LanguageServer.Util;
77
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
88

@@ -34,6 +34,8 @@ public class CompleteContext
3434
false,
3535
100
3636
);
37+
38+
public LuaRenderBuilder RenderBuilder { get; }
3739

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

5255
public void Add(CompletionItem item)

EmmyLua.LanguageServer/Completion/CompleteProvider/ModuleProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void AddCompletion(CompleteContext context)
4444
LabelDetails = new CompletionItemLabelDetails()
4545
{
4646
Detail = $" (in {module.ModulePath})",
47-
Description = context.SemanticModel.RenderBuilder.RenderType(retTy, context.RenderFeature)
47+
Description = context.RenderBuilder.RenderType(retTy, context.RenderFeature)
4848
},
4949
Data = module.DocumentId.Id.ToString(),
5050
Command = AutoRequire.MakeCommand(

EmmyLua.LanguageServer/Completion/CompletionDocumentResolver.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using EmmyLua.CodeAnalysis.Compilation.Semantic.Render;
2-
using EmmyLua.CodeAnalysis.Document;
1+
using EmmyLua.CodeAnalysis.Document;
32
using EmmyLua.CodeAnalysis.Syntax.Node;
43
using EmmyLua.LanguageServer.Server;
4+
using EmmyLua.LanguageServer.Server.Render;
55
using Newtonsoft.Json.Linq;
66
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
77

@@ -14,7 +14,8 @@ public class CompletionDocumentResolver
1414
ExpandAlias = true,
1515
ShowTypeLink = false,
1616
InHint = false,
17-
MaxStringPreviewLength = 100
17+
MaxStringPreviewLength = 100,
18+
InHover = true
1819
};
1920

2021
public CompletionItem Resolve(CompletionItem completionItem, ServerContext context)
@@ -41,12 +42,13 @@ private CompletionItem ModuleResolve(CompletionItem completionItem, ServerContex
4142
var id = new LuaDocumentId((int) completionItem.Data);
4243
if (context.GetSemanticModel(id) is {} semanticModel)
4344
{
45+
var renderBuilder = new LuaRenderBuilder(semanticModel.Context);
4446
completionItem = completionItem with
4547
{
4648
Documentation = new MarkupContent()
4749
{
4850
Kind = MarkupKind.Markdown,
49-
Value = semanticModel.RenderBuilder.RenderModule(semanticModel.Document, RenderFeature)
51+
Value = renderBuilder.RenderModule(semanticModel.Document, RenderFeature)
5052
}
5153
};
5254
}
@@ -70,12 +72,13 @@ private CompletionItem GeneralResolve(CompletionItem completionItem, ServerConte
7072

7173
if (context.GetSemanticModel(ptr.DocumentId) is {} semanticModel)
7274
{
75+
var renderBuilder = new LuaRenderBuilder(semanticModel.Context);
7376
return completionItem with
7477
{
7578
Documentation = new MarkupContent()
7679
{
7780
Kind = MarkupKind.Markdown,
78-
Value = semanticModel.RenderBuilder.Render(node, RenderFeature)
81+
Value = renderBuilder.Render(node, RenderFeature)
7982
}
8083
};
8184
}

0 commit comments

Comments
 (0)