Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
McJones committed Jan 31, 2025
2 parents 638254e + 7e57129 commit 634b194
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- For example, the following code will emit a warning, because the last `>` character is likely a typo: `<<wait 5>>>`
- Added a new method, `Dialogue.GetHeaders`, which returns the collection of headers present on a node.
- Added a new method, `Dialogue.GetHeaderValue`, which returns the value of the specified header on a node.
- Language Server: Nodes that are part of a node group now show their condition complexity as a code lens.
- Added a new method, `Dialogue.HasSalientContent(nodegroup)`, which returns a bool if there is any salient content for the requested nodegroup.


### Changed

- Commands are now better at checking to see if the first word is a keyword (e.g. `return`) or a word that just _begins_ with a keyword (`returnToMenu`).
Expand Down
24 changes: 24 additions & 0 deletions YarnSpinner.Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,30 @@ internal int ConditionCount
}
}

public partial class NodeContext
{
/// <summary>
/// Gets the computed total complexity of this node's 'when'
/// conditions.
/// </summary>
/// <remarks>
/// If this node has no such conditions, this value is -1.
/// </remarks>
public int ComplexityScore
{
get
{
var headers = this.GetWhenHeaders();
if (headers == null || !headers.Any())
{
return -1;
}

return headers.Sum(h => h.ComplexityScore);
}
}
}

/// <inheritdoc/>
public partial class When_headerContext
{
Expand Down
24 changes: 20 additions & 4 deletions YarnSpinner.LanguageServer/src/Server/Handlers/CodeLensHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -30,8 +31,9 @@ public Task<CodeLensContainer> Handle(CodeLensParams request, CancellationToken
return Task.FromResult(new CodeLensContainer());
}

var results = yarnFile.NodeDefinitions.SelectMany(titleToken =>
var results = yarnFile.NodeInfos.SelectMany(nodeInfo =>
{
var titleToken = nodeInfo.TitleToken;
if (titleToken.StartIndex == -1)
{
// This is an error token - the node doesn't actually
Expand All @@ -52,8 +54,7 @@ public Task<CodeLensContainer> Handle(CodeLensParams request, CancellationToken
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};

return new CodeLens[]
{
List<CodeLens> lenses = new() {
new CodeLens {
Range = PositionHelper.GetRange(yarnFile.LineStarts, titleToken),
Command = new Command
Expand Down Expand Up @@ -81,6 +82,21 @@ public Task<CodeLensContainer> Handle(CodeLensParams request, CancellationToken
},
},
};

if (nodeInfo.NodeGroupComplexity >= 0)
{
lenses.Add(new CodeLens
{
Range = PositionHelper.GetRange(yarnFile.LineStarts, titleToken),
Command = new Command
{
Name = string.Empty,
Title = $"Complexity: {nodeInfo.NodeGroupComplexity}",
},
});
}

return lenses;
});

CodeLensContainer result = new CodeLensContainer(results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public override bool VisitNode([NotNull] YarnSpinnerParser.NodeContext context)

// antlr lines start at 1, but LSP lines start at 0
HeaderStartLine = context.Start.Line - 1,

NodeGroupComplexity = context.ComplexityScore,
};

// Get the first few lines of the node's body as a preview
Expand Down
20 changes: 14 additions & 6 deletions YarnSpinner.LanguageServer/src/Server/Workspace/NodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public record NodeInfo
public int HeaderStartLine { get; set; } = 0;

[JsonProperty("headers")]
public List<NodeHeader> Headers { get; init; } = new ();
public List<NodeHeader> Headers { get; init; } = new();

[JsonProperty("jumps")]
public List<NodeJump> Jumps { get; init; } = new ();
public List<NodeJump> Jumps { get; init; } = new();

/// <summary>
/// Gets or sets the text that can be shown as a short preview of the
Expand All @@ -53,10 +53,18 @@ public record NodeInfo

internal IToken TitleToken { get; set; }

internal List<YarnActionReference> FunctionCalls { get; init; } = new ();
internal List<YarnActionReference> CommandCalls { get; init; } = new ();
internal List<IToken> VariableReferences { get; init; } = new ();
internal List<(string Name, int LineIndex)> CharacterNames { get; init; } = new ();
internal List<YarnActionReference> FunctionCalls { get; init; } = new();
internal List<YarnActionReference> CommandCalls { get; init; } = new();
internal List<IToken> VariableReferences { get; init; } = new();
internal List<(string Name, int LineIndex)> CharacterNames { get; init; } = new();

/// <summary>
/// Gets the computed complexity for this node.
/// </summary>
/// <remarks>
/// If this node is not part of a node group, this value is -1.
/// </remarks>
public int NodeGroupComplexity { get; internal set; } = -1;

/// <summary>
/// Gets a value indicating whether this <see cref="NodeInfo"/> has a valid
Expand Down
4 changes: 2 additions & 2 deletions YarnSpinner.LanguageServer/src/Server/Workspace/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal IEnumerable<Yarn.Compiler.Diagnostic> Diagnostics

private JsonConfigFile? DefinitionsFile { get; set; }

public event System.Action? OnProjectCompiled;
public event System.Action<Yarn.Compiler.CompilationResult>? OnProjectCompiled;

private readonly Yarn.Compiler.Project yarnProject;

Expand Down Expand Up @@ -259,7 +259,7 @@ public Yarn.Compiler.CompilationResult CompileProject(bool notifyOnComplete, Yar

if (notifyOnComplete)
{
OnProjectCompiled?.Invoke();
OnProjectCompiled?.Invoke(compilationResult);
}

return compilationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal void ReloadWorkspace()
project.NotificationSender = this;

// When a project reloads, publish diagnostics.
project.OnProjectCompiled += () =>
project.OnProjectCompiled += (compilationResult) =>
{
PublishDiagnostics();
PublishNodeInfos();
Expand Down
8 changes: 6 additions & 2 deletions YarnSpinner/CRC32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ namespace Yarn.Utility
/// </summary>
public static class CRC32
{
private static readonly uint[] LookupTable;
private static readonly uint[] LookupTable = new uint[256];

static CRC32()
{
uint seedPolynomial = 0xedb88320;
LookupTable = new uint[256];
uint temp;
for (uint i = 0; i < LookupTable.Length; ++i)
{
Expand Down Expand Up @@ -43,6 +42,11 @@ static CRC32()
/// name="bytes"/>.</returns>
public static uint GetChecksum(byte[] bytes)
{
if (bytes == null)
{
throw new System.ArgumentNullException(nameof(bytes));
}

uint crc = 0xffffffff;
for (int i = 0; i < bytes.Length; ++i)
{
Expand Down

0 comments on commit 634b194

Please sign in to comment.