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

Commit

Permalink
Basic info for action command registration, explicit parameter min/ma…
Browse files Browse the repository at this point in the history
…x counts, and bug fix for params[] handling
  • Loading branch information
pappleby committed Sep 20, 2021
1 parent a762491 commit fe50433
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
34 changes: 19 additions & 15 deletions LanguageServer/src/Server/Diagnostics/SemanticErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,27 @@ private static IEnumerable<Diagnostic> WrongParameterCountFunctions(YarnFileData
if (!defs.Any()) { return; }
var def = defs.First();

// TODO: probably cleaner to have min/max ranges on the number of parameters
if (fi.ParameterCount == def.Parameters.Count()) { return; }
if (fi.ParameterCount > def.Parameters.Count() &&
def.Parameters.Any() &&
def.Parameters.Last().IsParamsArray) { return; }
if (fi.ParameterCount < def.Parameters.Count() &&
fi.ParameterCount >=
def.Parameters.Count() - def.Parameters.Where(p => !string.IsNullOrWhiteSpace(p.DefaultValue)).Count()
) { return; }
if (def.MinParameterCount.HasValue && fi.ParameterCount < def.MinParameterCount)
{
results.Add(new Diagnostic
{
Message = $"Too few parameters. Expected at least {def.MinParameterCount}.",
Severity = DiagnosticSeverity.Error,
Range = fi.ParametersRange,
Code = nameof(YarnDiagnosticCode.YRNCmdParamCnt),
});
}

results.Add(new Diagnostic
if (def.MaxParameterCount.HasValue && fi.ParameterCount > def.MaxParameterCount)
{
Message = $"Incorrect number of parameters",
Severity = DiagnosticSeverity.Error,
Range = fi.ParametersRange,
Code = nameof(YarnDiagnosticCode.YRNCmdParamCnt),
});
results.Add(new Diagnostic
{
Message = $"Too many parameters. Expected at most {def.MaxParameterCount}.",
Severity = DiagnosticSeverity.Error,
Range = fi.ParametersRange,
Code = nameof(YarnDiagnosticCode.YRNCmdParamCnt),
});
}
});
}

Expand Down
5 changes: 3 additions & 2 deletions LanguageServer/src/Server/Handlers/SignatureHelpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Task<SignatureHelp> Handle(SignatureHelpParams request, CancellationToken
IEnumerable<SignatureInformation> results;
if (functionInfos.Any())
{
results = functionInfos.Select(fi => new SignatureInformation
results = functionInfos.Where(fi => fi.Parameters != null).Select(fi => new SignatureInformation
{
Label = fi.IsCommand ?
$"{fi.YarnName} {string.Join(' ', fi.Parameters.Select(p => $"{p.Type}:{p.Name}"))}" :
Expand All @@ -42,7 +42,8 @@ public Task<SignatureHelp> Handle(SignatureHelpParams request, CancellationToken
Label = p.Name,
Documentation = p.Documentation,
})),
ActiveParameter = parameterIndex,
ActiveParameter = parameterIndex < fi.Parameters.Count() || !fi.Parameters.Any() || !fi.Parameters.Last().IsParamsArray ?
parameterIndex : fi.Parameters.Count() - 1, // if last param is a params array, it should be the info for all trailing params input
});
}
else
Expand Down
38 changes: 32 additions & 6 deletions LanguageServer/src/Server/Workspace/CSharpFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static MoreLinq.Extensions.PartitionExtension;

namespace YarnLanguageServer
{
Expand Down Expand Up @@ -51,18 +52,39 @@ public void Update(string text, Uri uri)
var regMatches = commandRegMatches.Concat(functionRegMatches);

// TODO: This doesn't match anonymous functions, fix that at somepoint
var commandbridges = regMatches
(var commandBridges, var unmatchableCommandBridges) = regMatches
.Select(e =>
(e.m.ArgumentList.Arguments[0].ToString().Trim('\"'), e.m.ArgumentList.Arguments[1].Expression, e.Item2))
.Where(b =>
.Partition(b =>
b.Item2.Kind() == SyntaxKind.IdentifierName);




var registeredmatched = root.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Where(m => commandbridges.Select(b => b.Item2.ToString()).Contains(m.Identifier.ToString()));
.Where(m => commandBridges.Select(b => b.Item2.ToString()).Contains(m.Identifier.ToString()));
try
{
var matchedCommandbridges = commandbridges.Select(cb => (cb.Item1, registeredmatched.FirstOrDefault(rm => rm.Identifier.ToString() == cb.Item2.ToString()), cb.Item3))
foreach (var unmatchable in unmatchableCommandBridges)
{
FunctionDefinitions[unmatchable.Item1] = new RegisteredFunction
{
DefinitionFile = uri,
DefinitionName = unmatchable.Item1,
IsBuiltIn = false,
IsCommand = unmatchable.Item3,
Documentation = string.Empty,
Language = "csharp",
Parameters = null,
Signature = $"{unmatchable.Item1}(?)",
YarnName = unmatchable.Item1,
DefinitionRange =
PositionHelper.GetRange(LineStarts, unmatchable.Item2.GetLocation().SourceSpan.Start, unmatchable.Item2.GetLocation().SourceSpan.End),
};
}

var matchedCommandbridges = commandBridges.Select(cb => (cb.Item1, registeredmatched.FirstOrDefault(rm => rm.Identifier.ToString() == cb.Item2.ToString()), cb.Item3))
.Where(cbm => cbm.Item2 != null);

foreach ((var yarnName, var command, var isCommand) in matchedCommandbridges)
Expand All @@ -71,10 +93,12 @@ public void Update(string text, Uri uri)
{
FunctionDefinitions[yarnName] = CreateFunctionObject(uri, yarnName, command, isCommand, false);
}
catch (Exception) { }
catch (Exception e) {
}
}
}
catch (Exception) { }
catch (Exception e) {
}
}

private RegisteredFunction CreateFunctionObject(Uri uri, string yarnName, MethodDeclarationSyntax methodDeclaration, bool isCommand, bool isAttributeMatch = false)
Expand Down Expand Up @@ -191,6 +215,8 @@ private RegisteredFunction CreateFunctionObject(Uri uri, string yarnName, Method
IsBuiltIn = false,
IsCommand = isCommand,
Parameters = parameters,
MinParameterCount = parameters.Count(p => p.DefaultValue == null && !p.IsParamsArray),
MaxParameterCount = parameters.Any(p => p.IsParamsArray) ? null : parameters.Count(),
DefinitionRange = PositionHelper.GetRange(LineStarts, methodDeclaration.GetLocation().SourceSpan.Start, methodDeclaration.GetLocation().SourceSpan.End),
Documentation = documentation,
Signature = $"{methodDeclaration.Identifier.Text}{methodDeclaration.ParameterList}",
Expand Down
2 changes: 2 additions & 0 deletions LanguageServer/src/Server/Workspace/FileDataObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public struct RegisteredFunction
public Range DefinitionRange;
public string DefinitionName; // Does this need to be qualified? Do we even need this?
public IEnumerable<ParameterInfo> Parameters;
public int? MinParameterCount;
public int? MaxParameterCount;
public bool IsCommand;
public bool IsBuiltIn;
public string Documentation; // Do we care about markup style docstrings?
Expand Down
11 changes: 11 additions & 0 deletions LanguageServer/src/Server/Workspace/JsonConfigFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace YarnLanguageServer
Expand All @@ -17,6 +18,16 @@ public JsonConfigFile(string text, Uri uri)
parsedConfig.Functions.ForEach(f =>
{
f.DefinitionFile = uri;

if (f.Parameters != null && !f.MinParameterCount.HasValue) {
f.MinParameterCount = f.Parameters.Count(p => p.DefaultValue == null && !p.IsParamsArray);
}

if (f.Parameters != null && !f.MaxParameterCount.HasValue)
{
f.MaxParameterCount = f.Parameters.Any(p => p.IsParamsArray) ? null : f.Parameters.Count();
}

FunctionDefinitions[f.DefinitionName] = f;
var worked = FunctionDefinitions[f.DefinitionName].DefinitionFile == uri;
});
Expand Down
2 changes: 1 addition & 1 deletion VSCodeExtension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "yarn-spinner-language-server",
"displayName": "Yarn Spinner Language Server",
"description": "Adds support for the Yarn Spinner dialogue language powered by the language service protocol",
"version": "2.5.2",
"version": "2.5.3",
"homepage": "https://github.com/pappleby/YarnSpinnerLanguageServer",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit fe50433

Please sign in to comment.