Skip to content

Commit

Permalink
support @mapping annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jul 4, 2024
1 parent 2514e79 commit c98e0a4
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DocProvider : ICompleteProviderBase
[
"class", "enum", "interface", "alias", "module", "field", "param", "return", "see", "deprecated",
"type", "overload", "generic", "async", "cast", "private", "protected", "public", "operator",
"meta", "version", "as", "nodiscard", "diagnostic", // "package",
"meta", "version", "as", "nodiscard", "diagnostic", "mapping",// "package",
];

private List<string> Actions { get; } = ["disable-next-line", "disable", "enable"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private static void RenderCommentDescription(IEnumerable<LuaCommentSyntax>? comm
foreach (var comment in comments)
{
// renderContext.AddSeparator();
renderContext.AppendLine();
renderContext.Append("\n\n");
renderContext.Append(comment.CommentText);
}
}
Expand Down Expand Up @@ -126,12 +126,6 @@ public static void RenderFunctionDocComment(MethodInfo methodInfo, LuaRenderCont
{
if (tagParam.Description is { CommentText: {} commentText })
{
// var detailList = details.ToList();
// if (detailList.Count == 0)
// {
// continue;
// }

var renderString = new StringBuilder();
// var nameLength = 0;
if (tagParam.Name is { RepresentText: { } name })
Expand All @@ -147,29 +141,14 @@ public static void RenderFunctionDocComment(MethodInfo methodInfo, LuaRenderCont

// var detailIndent = " - ";
renderString.Append($" - {commentText}");
// for (var index = 0; index < detailList.Count; index++)
// {
// var detail = detailList[index];
// renderString.Append($"{detailIndent}{detail.RepresentText}");
// if (index < detailList.Count - 1)
// {
// renderString.Append("\n\n");
// }
//
// if (index == 0 && detailList.Count > 1)
// {
// detailIndent = new string(' ', 7 + nameLength + 3); // 8 spaces + nameLength + 3 spaces
// }
// }

tagRenderList.Add(renderString.ToString());
}

}

if (tagRenderList.Count > 0)
{
renderContext.AppendLine();
renderContext.Append("\n\n");
foreach (var tagRender in tagRenderList)
{
renderContext.Append(tagRender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ private static void RenderDocFieldDeclaration(LuaDeclaration declaration, DocFie
{
var visibility = declaration.Visibility switch
{
DeclarationVisibility.Public => " public",
DeclarationVisibility.Protected => " protected",
DeclarationVisibility.Private => " private",
DeclarationVisibility.Public => "",
DeclarationVisibility.Protected => "protected ",
DeclarationVisibility.Private => "private ",
_ => ""
};
renderContext.Append($"(field){visibility} {declaration.Name} : ");
renderContext.Append($"{visibility}(field) {declaration.Name} : ");
LuaTypeRenderer.RenderType(docFieldInfo.DeclarationType, renderContext);
});
LuaCommentRenderer.RenderDocFieldComment(docFieldInfo, renderContext);
Expand All @@ -233,13 +233,13 @@ private static void RenderTableFieldDeclaration(LuaDeclaration declaration, Tabl
{
var visibility = declaration.Visibility switch
{
DeclarationVisibility.Public => "public ",
DeclarationVisibility.Public => "",
DeclarationVisibility.Protected => "protected ",
DeclarationVisibility.Private => "private ",
_ => ""
};

renderContext.Append($"{visibility} {declaration.Name} : ");
renderContext.Append($"{visibility}{declaration.Name} : ");
LuaTypeRenderer.RenderType(tableFieldInfo.DeclarationType, renderContext);
if (tableFieldInfo.TableFieldPtr.ToNode(renderContext.SearchContext) is
{ IsValue: false, Value: LuaLiteralExprSyntax expr })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private void AnalyzeGeneralDeclaration(LuaSyntaxElement attachedElement, List<Lu
if (mappingSyntax.Name is { RepresentText: { } name })
{
declaration.Name = name;
declarationContext.Db.AddMapping(declaration.UniqueId, name);
}

break;
Expand Down
12 changes: 12 additions & 0 deletions EmmyLua/CodeAnalysis/Compilation/Index/WorkspaceIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class WorkspaceIndex

private Dictionary<LuaDocumentId, LuaDeclarationTree> DocumentDeclarationTrees { get; } = new();

private UniqueIndex<SyntaxElementId, string> MappingName { get; } = new();

public void Remove(LuaDocumentId documentId)
{
TypeIndex.Remove(documentId);
Expand Down Expand Up @@ -196,6 +198,11 @@ public void AddGenericParam(LuaDocumentId documentId, string name, LuaDeclaratio
TypeIndex.AddGenericParam(documentId, name, luaDeclaration);
}

public void AddMapping(SyntaxElementId id, string name)
{
MappingName.Update(id.DocumentId, id, name);
}

#endregion

#region Query
Expand Down Expand Up @@ -366,5 +373,10 @@ public IEnumerable<LuaDeclaration> QueryAllMembers()
return TypeIndex.QueryAllMembers();
}

public string? QueryMapping(SyntaxElementId id)
{
return MappingName.Query(id);
}

#endregion
}
11 changes: 10 additions & 1 deletion EmmyLua/CodeAnalysis/Compilation/Search/Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,16 @@ public IEnumerable<IDeclaration> FindMember(LuaType luaType, LuaIndexExprSyntax
}

var notFounded = true;
if (indexExpr is { Name: { } name })
var mappingName = context.Compilation.Db.QueryMapping(indexExpr.UniqueId);
if (mappingName is not null)
{
foreach (var declaration in FindMember(luaType, mappingName))
{
notFounded = false;
yield return declaration;
}
}
else if (indexExpr is { Name: { } name })
{
foreach (var declaration in FindMember(luaType, name))
{
Expand Down
15 changes: 11 additions & 4 deletions EmmyLua/CodeAnalysis/Compilation/Search/References.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private IEnumerable<ReferenceResult> LocalReferences(LuaDeclaration declaration,
var luaReferences = context.Compilation.Db.QueryLocalReferences(declaration);
foreach (var luaReference in luaReferences)
{
if (luaReference.Ptr.ToNode(context) is {} element)
if (luaReference.Ptr.ToNode(context) is { } element)
{
references.Add(new ReferenceResult(element.Location, element, luaReference.Kind));
}
Expand Down Expand Up @@ -77,7 +77,7 @@ private IEnumerable<ReferenceResult> FieldReferences(LuaDeclaration declaration,
var indexExprs = context.Compilation.Db.QueryIndexExprReferences(fieldName, context);
foreach (var indexExpr in indexExprs)
{
if (context.FindDeclaration(indexExpr) == declaration && indexExpr.KeyElement is {} keyElement)
if (context.FindDeclaration(indexExpr) == declaration && indexExpr.KeyElement is { } keyElement)
{
references.Add(new ReferenceResult(keyElement.Location, keyElement));
}
Expand All @@ -100,12 +100,19 @@ private IEnumerable<ReferenceResult> MethodReferences(LuaDeclaration declaration
}
default:
{
var results = new List<ReferenceResult>();
var mappingName = context.Compilation.Db.QueryMapping(methodInfo.IndexPtr.UniqueId);
if (mappingName is not null)
{
results.AddRange(FieldReferences(declaration, mappingName));
}

if (methodInfo.IndexPtr.ToNode(context) is { Name: { } name })
{
return FieldReferences(declaration, name);
results.AddRange(FieldReferences(declaration, name));
}

break;
return results;
}
}

Expand Down

0 comments on commit c98e0a4

Please sign in to comment.