Skip to content

Commit c678490

Browse files
committed
Language server is now aware of variadic functions
1 parent c297ae6 commit c678490

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

YarnSpinner.LanguageServer.Tests/ActionDeclarationTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,40 @@ private static void CheckImplementationMatchesDeclaration(System.Delegate impl,
139139
Types.TypeMappings[implParameter.ParameterType].Should().Be(declParameter.Type);
140140
}
141141
}
142+
143+
[Fact]
144+
public void ActionsFoundInCSharpFile_AreIdentified()
145+
{
146+
// Given
147+
var path = Path.Combine(TestUtility.PathToTestData, "TestWorkspace", "Project1", "ActionDeclarationUsage.yarn");
148+
149+
var workspace = new Workspace();
150+
workspace.Root = Path.Combine(TestUtility.PathToTestData, "TestWorkspace", "Project1");
151+
workspace.Configuration.CSharpLookup = true;
152+
workspace.Initialize();
153+
154+
// When
155+
var diagnostics = workspace
156+
.GetDiagnostics()
157+
.Where(d => d.Key.AbsolutePath.EndsWith("ActionDeclarationUsage.yarn"))
158+
.SelectMany(d => d.Value);
159+
160+
// Then
161+
162+
diagnostics.Should().NotContain(d => d.Severity == DiagnosticSeverity.Error);
163+
164+
// A single command should be detected as unknown
165+
diagnostics.Should().HaveCount(2);
166+
167+
diagnostics.Should().Contain(d => d.Message == "Could not find command definition for unknown_command",
168+
"unknown_command is not declared");
169+
diagnostics.Should().Contain(d => d.Message == "Could not find function definition for unknown_function",
170+
"unknown_function is not declared");
171+
172+
// We should have detected the function with a params array as having the correct type
173+
workspace.Projects.Single().Functions
174+
.Should().Contain(f => f.YarnName == "function_with_params_array")
175+
.Which.VariadicParameterType.Should().Be(Types.Number);
176+
}
142177
}
143178
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
title: ActionDeclarationUsage
2+
---
3+
4+
<<static_command_no_docs>>
5+
<<static_command_no_params>>
6+
<<static_command_with_params "test" 42>>
7+
<<instance_command_no_params>>
8+
<<instance_command_with_params Target "test" 42>>
9+
10+
<<declare $result = 0>>
11+
<<set $result = function_with_params(1,"2")>>
12+
<<set $result = function_with_params_array(1,2,3,4)>>
13+
14+
<<unknown_command>>
15+
<<set $result = unknown_function()>>
16+
17+
===

YarnSpinner.LanguageServer.Tests/TestData/TestWorkspace/Project1/ExampleCommands.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public static int FunctionWithParams(int one, string two)
5555
return -1;
5656
}
5757

58+
[YarnFunction("function_with_params_array")]
59+
public static int FunctionWithParamsArray(int one, params int[] ints)
60+
{
61+
return -1;
62+
}
63+
5864
/// <summary>
5965
/// This command has <c>nested XML</c> nodes.
6066
/// </summary>

YarnSpinner.LanguageServer/src/Server/Workspace/CSharpFileData.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,29 @@ private static Action GetActionFromMethod(MethodDeclarationSyntax method)
233233
Signature = $"{method.Identifier.Text}{method.ParameterList}",
234234
};
235235

236-
foreach (var parameter in method.ParameterList.Parameters)
236+
for (int i = 0; i < method.ParameterList.Parameters.Count; i++)
237237
{
238-
action.Parameters.Add(new Action.ParameterInfo
238+
ParameterSyntax? parameter = method.ParameterList.Parameters[i];
239+
240+
var isLastParameter = i == method.ParameterList.Parameters.Count - 1;
241+
242+
if (isLastParameter && parameter.Type is ArrayTypeSyntax arrayTypeSyntax)
243+
{
244+
// If this is the last parameter and it's an array, then
245+
// this parameter is where all variadic parameters will go.
246+
action.VariadicParameterType = GetYarnType(arrayTypeSyntax.ElementType);
247+
}
248+
else
239249
{
240-
Name = parameter.Identifier.ToString(),
241-
Description = GetParameterDocumentation(method, parameter.Identifier.ToString()),
242-
DisplayDefaultValue = parameter.Default?.Value?.ToString(),
243-
Type = GetYarnType(parameter.Type),
244-
DisplayTypeName = parameter.Type?.ToString() ?? "(unknown)",
245-
});
250+
action.Parameters.Add(new Action.ParameterInfo
251+
{
252+
Name = parameter.Identifier.ToString(),
253+
Description = GetParameterDocumentation(method, parameter.Identifier.ToString()),
254+
DisplayDefaultValue = parameter.Default?.Value?.ToString(),
255+
Type = GetYarnType(parameter.Type),
256+
DisplayTypeName = parameter.Type?.ToString() ?? "(unknown)",
257+
});
258+
}
246259
}
247260

248261
return action;

0 commit comments

Comments
 (0)