Skip to content

Commit d15224e

Browse files
committed
Add NodeInfo.ContainsExternalJumps
1 parent b3e7ec3 commit d15224e

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
- When the `tags:` header on a node is blank, `Node.Tags` now returns an empty collection, instead of a collection containing a single empty string.
1414
- Fixed an issue that would cause testplans with syntax errors to incorrectly pass.
1515
- Fixed an issue where using `visited()` or `visited_count()` would not correctly track visiting node groups.
16+
- Language Server: NodeInfo objects now indicate whether a node contains any jumps to nodes in a different file.
1617

1718
### Removed
1819

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: NodeInAnotherFile
2+
---
3+
This node is in a different file.
4+
===

YarnSpinner.LanguageServer.Tests/TestData/TestWorkspace/JumpsAndDetours/JumpsAndDetours.yarn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
title: Node1
22
---
33
<<jump Node2>>
4+
<<jump NodeInAnotherFile>>
45
===
56
title: Node2
67
---

YarnSpinner.LanguageServer.Tests/WorkspaceTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class WorkspaceTests
1414
private static string Project2Path = Path.Combine(TestUtility.PathToTestWorkspace, "Project2", "Project2.yarnproject");
1515
private static string NoProjectPath = Path.Combine(TestUtility.PathToTestWorkspace, "FilesWithNoProject");
1616
private static string MultipleDefsPath = Path.Combine(TestUtility.PathToTestWorkspace, "ProjectWithMultipleDefinitionsFiles");
17+
private static string JumpsAndDetoursPath = Path.Combine(TestUtility.PathToTestWorkspace, "JumpsAndDetours");
1718

1819
[Fact]
1920
public void Projects_CanOpen()
@@ -163,5 +164,21 @@ public void Workspace_WithMultipleDefinitionsFiles_UsesMultipleFiles()
163164
relativeToWorkspaceProject.Functions.Should().Contain(c => c.YarnName == "custom_function_2");
164165

165166
}
167+
168+
[Fact]
169+
public void Workspace_WithJumpsBetweenFiles_IdentifiesJumpsToOtherFiles()
170+
{
171+
var workspace = new Workspace();
172+
workspace.Root = JumpsAndDetoursPath;
173+
workspace.Initialize();
174+
175+
var project = workspace.Projects.Single();
176+
var file = project.Files.Single(f => f.Uri.AbsolutePath.EndsWith("JumpsAndDetours.yarn"));
177+
var node1 = file.NodeInfos.Single(n => n.UniqueTitle == "Node1");
178+
var node2 = file.NodeInfos.Single(n => n.UniqueTitle == "Node2");
179+
180+
node1.ContainsExternalJumps.Should().BeTrue();
181+
node2.ContainsExternalJumps.Should().BeFalse();
182+
}
166183
}
167184
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Newtonsoft.Json;
33
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
44
using System.Collections.Generic;
5+
using System.Linq;
56

67
namespace YarnLanguageServer;
78

@@ -81,6 +82,9 @@ public record NodeInfo
8182
internal List<IToken> VariableReferences { get; init; } = new();
8283
internal List<(string name, int lineIndex)> CharacterNames { get; init; } = new();
8384

85+
[JsonProperty("containsExternalJumps")]
86+
public bool ContainsExternalJumps => this.Jumps.Any(j => this.File != null && j.DestinationFile != null && j.DestinationFile.Uri != this.File.Uri);
87+
8488
/// <summary>
8589
/// Gets the computed complexity for this node.
8690
/// </summary>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public NodeJump(string destinationTitle, IToken destinationToken, JumpType jumpT
2121
[JsonProperty("type")]
2222
public JumpType Type { get; init; }
2323

24+
internal YarnFileData? DestinationFile { get; set; }
25+
26+
[JsonProperty("destinationFileUri")]
27+
internal System.Uri? DestinationFileUri => DestinationFile?.Uri;
28+
2429
/// <summary>
2530
/// A type of jump from one node to another.
2631
/// </summary>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ public Yarn.Compiler.CompilationResult CompileProject(bool notifyOnComplete, Yar
272272

273273
this.LastCompilationResult = compilationResult;
274274

275+
276+
// For all jumps in all files, attempt to identify the file that the
277+
// target of the jump is in, and store it in the jump info
278+
var nodesToFiles = this.Files
279+
.SelectMany(f => f.NodeInfos
280+
.Where(n => n.SourceTitle != null))
281+
.ToLookup(n => n.SourceTitle!);
282+
283+
284+
foreach (var file in this.Files)
285+
{
286+
foreach (var jump in file.NodeJumps)
287+
{
288+
var nodesWithTitle = nodesToFiles.FirstOrDefault(n => n.Key == jump.DestinationTitle);
289+
jump.DestinationFile = nodesWithTitle?.FirstOrDefault()?.File;
290+
}
291+
}
292+
275293
if (notifyOnComplete)
276294
{
277295
OnProjectCompiled?.Invoke(compilationResult);

0 commit comments

Comments
 (0)