Skip to content

Commit

Permalink
enhance inlayHint
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Apr 14, 2024
1 parent 034d7e9 commit 54a48a7
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 415 deletions.
21 changes: 18 additions & 3 deletions EmmyLua/CodeAnalysis/Compilation/Analyzer/AnalyzeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ public enum ResolveState
UnResolvedIndex = 0x001,
UnResolvedType = 0x002,
UnResolveReturn = 0x004,
UnResolvedParameters = 0x008,
}

public class UnResolved(ResolveState state)
{
public ResolveState ResolvedState { get; set; } = state;
}

public class UnResolvedDeclaration(LuaDeclaration luaDeclaration, LuaExprRef? exprRef, ResolveState state) : UnResolved(state)
public class UnResolvedDeclaration(LuaDeclaration luaDeclaration, LuaExprRef? exprRef, ResolveState state)
: UnResolved(state)
{
public LuaDeclaration LuaDeclaration { get; } = luaDeclaration;

public LuaExprRef? ExprRef { get; } = exprRef;

public bool IsTypeDeclaration { get; set; } = false;

}

public class UnResolvedMethod(LuaMethodType methodType, LuaBlockSyntax block, ResolveState state) : UnResolved(state)
Expand All @@ -49,13 +50,27 @@ public class UnResolvedSource(LuaDocumentId documentId, LuaBlockSyntax block, Re
public LuaBlockSyntax Block { get; } = block;
}

public class UnResolvedForRangeParameter(List<ParameterLuaDeclaration> parameterLuaDeclarations, List<LuaExprSyntax> exprList) : UnResolved(ResolveState.UnResolvedType)
public class UnResolvedForRangeParameter(
List<ParameterLuaDeclaration> parameterLuaDeclarations,
List<LuaExprSyntax> exprList) : UnResolved(ResolveState.UnResolvedType)
{
public List<ParameterLuaDeclaration> ParameterLuaDeclarations { get; } = parameterLuaDeclarations;

public List<LuaExprSyntax> ExprList { get; } = exprList;
}

public class UnResolvedClosureParameters(
List<ParameterLuaDeclaration> parameterLuaDeclarations,
LuaCallExprSyntax callExprSyntax,
int index) : UnResolved(ResolveState.UnResolvedParameters)
{
public List<ParameterLuaDeclaration> ParameterLuaDeclarations { get; } = parameterLuaDeclarations;

public LuaCallExprSyntax CallExprSyntax { get; } = callExprSyntax;

public int Index { get; } = index;
}

public class AnalyzeContext(List<LuaDocument> documents)
{
public List<LuaDocument> LuaDocuments { get; } = documents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@ public void WalkIn(LuaSyntaxElement node)
AnalyzeSource(sourceSyntax);
break;
}
case LuaLabelStatSyntax labelStatSyntax:
{
AnalyzeLuaLabel(labelStatSyntax);
break;
}
case LuaNameExprSyntax nameExpr:
{
IndexNameExpr(nameExpr);
Expand Down Expand Up @@ -312,13 +307,13 @@ private void AnalyzeLocalStatDeclaration(LuaLocalStatSyntax localStatSyntax)
private List<ParameterLuaDeclaration> AnalyzeParamListDeclaration(LuaParamListSyntax paramListSyntax)
{
var parameters = new List<ParameterLuaDeclaration>();
var dic = FindParamTypeDict(paramListSyntax);
var paramTypeDict = FindParamTypeDict(paramListSyntax);
foreach (var param in paramListSyntax.Params)
{
if (param.Name is { } name)
{
var declaration = new ParameterLuaDeclaration(name.RepresentText, new(param), null);
if (dic.TryGetValue(name.RepresentText, out var ty))
if (paramTypeDict.TryGetValue(name.RepresentText, out var ty))
{
declaration.DeclarationType = ty;
}
Expand All @@ -329,7 +324,7 @@ private List<ParameterLuaDeclaration> AnalyzeParamListDeclaration(LuaParamListSy
else if (param.IsVarArgs)
{
var declaration = new ParameterLuaDeclaration("...", new(param), null);
if (dic.TryGetValue("...", out var ty))
if (paramTypeDict.TryGetValue("...", out var ty))
{
declaration.DeclarationType = ty;
}
Expand All @@ -344,8 +339,11 @@ private List<ParameterLuaDeclaration> AnalyzeParamListDeclaration(LuaParamListSy

private LuaType GetRetType(IEnumerable<LuaDocTagSyntax>? docList)
{
var returnTypes = docList?.OfType<LuaDocTagReturnSyntax>()
.SelectMany(tag => tag.TypeList).Select(Context.Infer).ToList();
var returnTypes = docList?
.OfType<LuaDocTagReturnSyntax>()
.SelectMany(tag => tag.TypeList)
.Select(Context.Infer)
.ToList();
LuaType returnType = Builtin.Unknown;
if (returnTypes is null)
{
Expand Down Expand Up @@ -638,7 +636,7 @@ private void AnalyzeClosureExpr(LuaClosureExprSyntax closureExprSyntax)

var overloads = docList?
.OfType<LuaDocTagOverloadSyntax>()
.Where(it=>it.TypeFunc is not null)
.Where(it => it.TypeFunc is not null)
.Select(it => Context.Infer(it.TypeFunc))
.Cast<LuaMethodType>()
.Select(it => it.MainSignature).ToList();
Expand All @@ -651,6 +649,13 @@ private void AnalyzeClosureExpr(LuaClosureExprSyntax closureExprSyntax)
PopScope();
}

if (closureExprSyntax.Parent is LuaCallArgListSyntax { Parent: LuaCallExprSyntax callExprSyntax } callArgList)
{
var index = callArgList.ArgList.ToList().IndexOf(closureExprSyntax);
var unResolved = new UnResolvedClosureParameters(parameters, callExprSyntax, index);
AddUnResolved(unResolved);
}

var mainRetType = GetRetType(docList);
var isColonDefine = closureExprSyntax.Parent is LuaFuncStatSyntax { IsColonFunc: true };
LuaMethodType method =
Expand Down Expand Up @@ -1072,15 +1077,6 @@ private void AnalyzeLuaTableType(LuaDocTableTypeSyntax luaDocTableTypeSyntax)
}
}

private void AnalyzeLuaLabel(LuaLabelStatSyntax labelStatSyntax)
{
// if (labelStatSyntax is { Name: { } name })
// {
// var labelDeclaration =
// new LabelLuaDeclaration(name.RepresentText, new(labelStatSyntax));
// }
}

private void AnalyzeSource(LuaSourceSyntax sourceSyntax)
{
if (sourceSyntax.Block is { } block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public override void Analyze(AnalyzeContext analyzeContext)
ResolveReturn(unResolved);
break;
}
case ResolveState.UnResolvedParameters:
{
ResolveParameters(unResolved);
break;
}
}
}
} while (resolveDependencyGraph.CalcDependency());
Expand Down Expand Up @@ -195,6 +200,35 @@ private void ResolveReturn(UnResolved unResolved)
}
}

private void ResolveParameters(UnResolved unResolved)
{
if (unResolved is UnResolvedClosureParameters unResolvedClosureParameters)
{
var callExpr = unResolvedClosureParameters.CallExprSyntax;
var prefixType = Context.Infer(callExpr.PrefixExpr);
var callArgList = callExpr.ArgList?.ArgList.ToList() ?? [];
TypeHelper.Each<LuaMethodType>(prefixType, type =>
{
var signature = type.FindPerfectMatchSignature(callExpr, callArgList, Context);
var paramIndex = unResolvedClosureParameters.Index;
if (paramIndex == -1) return;
var paramDeclaration = signature.Parameters.ElementAtOrDefault(paramIndex);
if (paramDeclaration is not { DeclarationType: { } paramType }) return;
var closureParams = unResolvedClosureParameters.ParameterLuaDeclarations;
TypeHelper.Each<LuaMethodType>(paramType, methodType =>
{
var mainParams = methodType.MainSignature.Parameters;
for (var i = 0; i < closureParams.Count && i < mainParams.Count; i++)
{
var closureParam = closureParams[i];
var mainParam = mainParams[i];
closureParam.DeclarationType ??= mainParam.DeclarationType;
}
});
});
}
}

private LuaType AnalyzeBlockReturns(LuaBlockSyntax mainBlock)
{
LuaType returnType = Builtin.Unknown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void Build(List<UnResolved> unResolvedList)
{
CalcResolveReturn(unResolved);
}

if ((unResolved.ResolvedState & ResolveState.UnResolvedParameters) != 0)
{
CalcResolveParameters(unResolved);
}
}
}

Expand Down Expand Up @@ -190,6 +195,26 @@ private void CalcResolveReturn(UnResolved unResolved)
}
}

private void CalcResolveParameters(UnResolved unResolved)
{
if (unResolved is UnResolvedClosureParameters unResolvedClosureParameters)
{
var callExprSyntax = unResolvedClosureParameters.CallExprSyntax;
if (callExprSyntax.PrefixExpr is { } prefixExpr)
{
var prefixType = context.Infer(prefixExpr);
if (prefixType.Equals(Builtin.Unknown))
{
AddDependency(unResolved, ResolveState.UnResolvedParameters, prefixExpr);
}
else
{
CanResolvedQueue.Enqueue(new CanResolved(unResolved, ResolveState.UnResolvedParameters));
}
}
}
}

private void AnalyzeBlockReturns(LuaBlockSyntax mainBlock, UnResolved unResolved)
{
var cfg = context.Compilation.GetControlFlowGraph(mainBlock);
Expand Down
Loading

0 comments on commit 54a48a7

Please sign in to comment.