Skip to content
This repository was archived by the owner on Nov 2, 2022. It is now read-only.

Commit 1331ad4

Browse files
committed
When multiple possible C# methods can match a yarnName, prioritize the following order: <yarncommand> in structured comment, [YarnCommand] attribute, AddCommandHandler call.
1 parent 6474535 commit 1331ad4

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

LanguageServer/src/Server/Handlers/SemanticTokensHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protected override SemanticTokensRegistrationOptions CreateRegistrationOptions(S
3737
DocumentSelector = Utils.YarnDocumentSelector,
3838
Legend = new SemanticTokensLegend()
3939
{
40-
TokenModifiers = capability.TokenModifiers,
41-
TokenTypes = capability.TokenTypes,
40+
TokenModifiers = capability?.TokenModifiers,
41+
TokenTypes = capability?.TokenTypes,
4242
},
4343
Full = new SemanticTokensCapabilityRequestFull
4444
{

LanguageServer/src/Server/Workspace/CSharpFileData.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void LookForUnmatchedCommands(bool isLastTime) {
5757
{
5858
try
5959
{
60-
Definitions[matchedUcn.YarnName] = CreateFunctionObject(Uri, matchedUcn.YarnName, command, matchedUcn.IsCommand, false);
60+
Definitions[matchedUcn.YarnName] = CreateFunctionObject(Uri, matchedUcn.YarnName, command, matchedUcn.IsCommand, 3, false);
6161
Workspace.UnmatchedDefinitions.RemoveAll(ucn => ucn.YarnName == matchedUcn.YarnName);
6262
}
6363
catch (Exception e)
@@ -85,7 +85,7 @@ private void RegisterCommandAttributeMatches()
8585
command.AttributeLists.First().Attributes.First(a => a.Name.ToString().Contains("YarnCommand"))
8686
.ArgumentList.Arguments.First().ToString()
8787
.Trim('\"');
88-
Definitions[yarnName] = CreateFunctionObject(Uri, yarnName, command, true, true);
88+
Definitions[yarnName] = CreateFunctionObject(Uri, yarnName, command, true, 2, true);
8989
Workspace.UnmatchedDefinitions.RemoveAll(ucn => ucn.YarnName == yarnName); // Matched some comands, can mark them off the list!
9090
}
9191
}
@@ -105,7 +105,7 @@ private void RegisterCommentTaggedCommandsAndFunctions()
105105
var isCommand = string.IsNullOrWhiteSpace(functionName);
106106
if (!string.IsNullOrWhiteSpace(yarnName))
107107
{
108-
Definitions[yarnName] = CreateFunctionObject(Uri, yarnName, match, isCommand, false);
108+
Definitions[yarnName] = CreateFunctionObject(Uri, yarnName, match, isCommand, 1, false);
109109
Workspace.UnmatchedDefinitions.RemoveAll(ucn => ucn.YarnName == yarnName);
110110
}
111111
}
@@ -140,7 +140,7 @@ private void RegisterCommandAndFunctionBridges()
140140

141141
}
142142

143-
private RegisteredDefinition CreateFunctionObject(Uri uri, string yarnName, MethodDeclarationSyntax methodDeclaration, bool isCommand, bool isAttributeMatch = false)
143+
private RegisteredDefinition CreateFunctionObject(Uri uri, string yarnName, MethodDeclarationSyntax methodDeclaration, bool isCommand, int priority, bool isAttributeMatch = false)
144144
{
145145
string documentation = string.Empty;
146146
Dictionary<string, string> paramsDocumentation = new Dictionary<string, string>();
@@ -245,6 +245,7 @@ private RegisteredDefinition CreateFunctionObject(Uri uri, string yarnName, Meth
245245
IsBuiltIn = false,
246246
IsCommand = isCommand,
247247
Parameters = parameters,
248+
Priority = priority,
248249
MinParameterCount = parameters.Count(p => p.DefaultValue == null && !p.IsParamsArray),
249250
MaxParameterCount = parameters.Any(p => p.IsParamsArray) ? null : parameters.Count(),
250251
DefinitionRange = PositionHelper.GetRange(LineStarts, methodDeclaration.GetLocation().SourceSpan.Start, methodDeclaration.GetLocation().SourceSpan.End),

LanguageServer/src/Server/Workspace/FileDataObjects.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct RegisteredDefinition
1616
public int? MaxParameterCount;
1717
public bool IsCommand;
1818
public bool IsBuiltIn;
19+
public int Priority; // If multiple defined using the same filetype, lower priority wins.
1920
public string Documentation; // Do we care about markup style docstrings?
2021
public string Language; // = "csharp" or "txt";
2122
public string Signature;

LanguageServer/src/Server/Workspace/JsonConfigFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void RegisterDefinition(RegisteredDefinition f, bool isCommand, Uri uri,
2626
f.DefinitionFile = uri;
2727
f.IsCommand = isCommand;
2828
f.IsBuiltIn = IsBuiltIn;
29-
29+
f.Priority = 0;
3030
if (f.Parameters != null && !f.MinParameterCount.HasValue)
3131
{
3232
f.MinParameterCount = f.Parameters.Count(p => p.DefaultValue == null && !p.IsParamsArray);

LanguageServer/src/Server/Workspace/Workspace.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ private void FillFunctionDefinitionCache()
268268
results = results
269269
.GroupBy(f => f.YarnName).Select(g => {
270270
if (g.Count() == 1) { return g.First(); }
271-
271+
if (g.All(f => f.DefinitionFile.ToString().EndsWith(".cs"))) { return g.OrderBy(f => f.Priority).First(); }
272272
var jsonDef = g.FirstOrDefault(f => f.DefinitionFile.ToString().EndsWith(".json"));
273-
var csharpDef = g.FirstOrDefault(f => f.DefinitionFile.ToString().EndsWith(".cs") && !f.Signature.EndsWith("(?)"));
273+
var csharpDef = g.OrderBy(f => f.Priority).FirstOrDefault(f => f.DefinitionFile.ToString().EndsWith(".cs") && !f.Signature.EndsWith("(?)"));
274274
if (string.IsNullOrWhiteSpace(csharpDef.YarnName)) { csharpDef = g.FirstOrDefault(f => f.DefinitionFile.ToString().EndsWith(".cs")); }
275275

276276
jsonDef.DefinitionFile = csharpDef.DefinitionFile;
@@ -311,6 +311,7 @@ private void FillFunctionDefinitionCache()
311311
Language = Utils.CSharpLanguageID,
312312
YarnName = bridge.yarnName,
313313
Documentation = string.Empty,
314+
Priority = 10,
314315
Signature = $"{bridge.yarnName}(?)",
315316
};
316317
}

0 commit comments

Comments
 (0)