-
-
Notifications
You must be signed in to change notification settings - Fork 216
Missing node warning and quickfixes #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
desplesda
merged 8 commits into
YarnSpinnerTool:main
from
pappleby:missing-node-warning-and-quickfixes
Apr 10, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d44fc61
Warn on jump to non-existant node name
pappleby 3b43f32
Quick fixes for jump to missing node name: create new or typo fix
pappleby b51edf5
update changelog
pappleby f116876
Putting together tests
pappleby 487f216
Fix tests
pappleby 16fad6d
Merge remote-tracking branch 'origin/main' into missing-node-warning-…
pappleby 82c2c18
Fix test (added a node to a test yarn file, so the counts were off by…
pappleby c275244
Merge branch 'main' into missing-node-warning-and-quickfixes
desplesda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| using FluentAssertions; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Newtonsoft.Json.Linq; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Workspace; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
| using Yarn.Compiler; | ||
| using YarnLanguageServer.Diagnostics; | ||
| using YarnLanguageServer.Handlers; | ||
|
|
||
| namespace YarnLanguageServer.Tests; | ||
|
|
||
| #pragma warning disable VSTHRD200 // async methods should end in 'Async' | ||
|
|
||
| public class CodeActionTests : LanguageServerTestsBase | ||
| { | ||
| public CodeActionTests(ITestOutputHelper outputHelper) : base(outputHelper) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Server_FixesJumpDestinationTypo() | ||
| { | ||
| var filePath = Path.Combine(TestUtility.PathToTestWorkspace, "Project1", "Test.yarn"); | ||
|
|
||
| Task<NodesChangedParams> getInitialNodesChanged = GetNodesChangedNotificationAsync(n => n.Uri.ToString().Contains(filePath)); | ||
|
|
||
| // Set up the server | ||
| var (client, server) = await Initialize(ConfigureClient, ConfigureServer); | ||
| NodesChangedParams? nodeInfo = await getInitialNodesChanged; | ||
| var workspace = server.Workspace.GetService<Workspace>(); | ||
| var diagnostics = workspace!.GetDiagnostics().SelectMany(d => d.Value); | ||
|
|
||
| var jumpToWarning = diagnostics.FirstOrDefault(d => d.Code.HasValue && d.Code.Value.String == nameof(YarnDiagnosticCode.YRNMsngJumpDest)); | ||
|
|
||
| jumpToWarning.Should().NotBeNull("Expecting a warning for the missing jump destination"); | ||
|
|
||
| var codeActionHandler = new CodeActionHandler(workspace); | ||
| var CodeActionParams = new CodeActionParams | ||
| { | ||
| Context = new CodeActionContext { Diagnostics = Container.From(jumpToWarning!)}, | ||
| TextDocument = new TextDocumentIdentifier(DocumentUri.FromFileSystemPath(filePath)) | ||
| }; | ||
|
|
||
| var commandOrCodeActions = await codeActionHandler.Handle(CodeActionParams, default); | ||
|
|
||
| var typoFix = commandOrCodeActions.FirstOrDefault(c => c.CodeAction?.Title?.Contains("Rename to 'JumpToTest'") ?? false); | ||
| typoFix.Should().NotBeNull("Expecting a code action to fix the jump destination typo"); | ||
|
|
||
| var typoFixEdit = typoFix!.CodeAction!.Edit; | ||
| typoFixEdit.Should().NotBeNull("Expecting the typo fix action to have a workspace edit"); | ||
|
|
||
| // Remember how many nodes we had before making the change | ||
| var nodeCount = nodeInfo.Nodes.Count; | ||
|
|
||
| // Expect to receive a 'nodes changed' notification | ||
| Task<NodesChangedParams> nodesChangedAfterRemovingNode = GetNodesChangedNotificationAsync(n => n.Uri.ToString().Contains(filePath)); | ||
|
|
||
| ChangeTextInDocuments(client, typoFixEdit!); | ||
|
|
||
| nodeInfo = await nodesChangedAfterRemovingNode; | ||
|
|
||
| var jumpToNode = nodeInfo.Nodes.Find(n => n.Title == "JumpToTest"); | ||
| nodeInfo.Nodes.Should().HaveCount(nodeCount, "because didn't change any nodes"); | ||
| jumpToNode!.Jumps.Where(j=>j.DestinationTitle == "JumpToTest").Should().HaveCount(1, "because we fixed the typo and are jumping to the existing JumpToTest node"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Server_CreatesNewNodeBasedOnJumpTarget() | ||
| { | ||
| var filePath = Path.Combine(TestUtility.PathToTestWorkspace, "Project1", "Test.yarn"); | ||
|
|
||
| Task<NodesChangedParams> getInitialNodesChanged = GetNodesChangedNotificationAsync(n => n.Uri.ToString().Contains(filePath)); | ||
|
|
||
| // Set up the server | ||
| var (client, server) = await Initialize(ConfigureClient, ConfigureServer); | ||
| NodesChangedParams? nodeInfo = await getInitialNodesChanged; | ||
| var workspace = server.Workspace.GetService<Workspace>(); | ||
| var diagnostics = workspace!.GetDiagnostics().SelectMany(d => d.Value); | ||
|
|
||
| var jumpToWarning = diagnostics.FirstOrDefault(d => d.Code.HasValue && d.Code.Value.String == nameof(YarnDiagnosticCode.YRNMsngJumpDest)); | ||
|
|
||
| jumpToWarning.Should().NotBeNull("Expecting a warning for the missing jump destination"); | ||
|
|
||
|
|
||
| var codeActionHandler = new CodeActionHandler(workspace); | ||
| var CodeActionParams = new CodeActionParams | ||
| { | ||
| Context = new CodeActionContext { Diagnostics = Container.From(jumpToWarning!) }, | ||
| TextDocument = new TextDocumentIdentifier(DocumentUri.FromFileSystemPath(filePath)) | ||
| }; | ||
| var commandOrCodeActions = await codeActionHandler.Handle(CodeActionParams, default); | ||
|
|
||
| var generateNodeFix = commandOrCodeActions.FirstOrDefault(c => | ||
| c.CodeAction?.Title?.Contains("Generate node 'Jump2Test'") ?? false); | ||
|
|
||
| generateNodeFix.Should().NotBeNull("Expecting a code action to generate a new node"); | ||
|
|
||
| var generateNodeFixEdit = generateNodeFix!.CodeAction!.Edit; | ||
| generateNodeFixEdit.Should().NotBeNull("Expecting the typo fix action to have a workspace edit"); | ||
|
|
||
| // Remember how many nodes we had before making the change | ||
| var nodeCount = nodeInfo.Nodes.Count; | ||
|
|
||
| // Remember how many jumps we had to JumpToTest we had before making the change | ||
| var jumpCount = nodeInfo.Nodes.Find(n => n.Title == "JumpToTest")?.Jumps.Count ?? 0; | ||
|
|
||
| // Expect to receive a 'nodes changed' notification | ||
| Task<NodesChangedParams> nodesChangedAfterRemovingNode = GetNodesChangedNotificationAsync(n => n.Uri.ToString().Contains(filePath)); | ||
|
|
||
| ChangeTextInDocuments(client, generateNodeFixEdit!); | ||
|
|
||
| nodeInfo = await nodesChangedAfterRemovingNode; | ||
|
|
||
| var jumpToNode = nodeInfo.Nodes.Find(n => n.Title == "JumpToTest"); | ||
| var jump2Node = nodeInfo.Nodes.Find(n => n.Title == "Jump2Test"); | ||
| jump2Node.Should().NotBeNull("because we have created a new node"); | ||
| nodeInfo.Nodes.Should().HaveCount(nodeCount + 1, "because we have added a single new node"); | ||
| jumpToNode!.Jumps.Where(j=>j.DestinationTitle == "JumpToTest").Should().HaveCount(0, "because we are jumping to the new generated node, not the exisiting JumpToTest node"); | ||
| jumpToNode!.Jumps.Where(j=>j.DestinationTitle == "Jump2Test").Should().HaveCount(1, "because we are jumping to the new generated node, not the exisiting JumpToTest node"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great if this could move the cursor to the newly created node. Didn't find a way to do this with an edit, but maybe it's possible with a command instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Command is a no go :/ (won't show up as a suggestion if its not a CodeAction QuickFix).