From 8897f0f316677440966ff8c939f2f5016c8e4446 Mon Sep 17 00:00:00 2001 From: Jon Manning Date: Sun, 31 Mar 2024 14:02:54 +1100 Subject: [PATCH] Add an LSP command for getting all known projects in the workspace --- .../CommandTests.cs | 18 ++++++++++++++++++ .../src/Server/Commands/Commands.cs | 5 +++++ .../src/Server/Workspace/Project.cs | 7 +++++++ .../src/Server/YarnLanguageServer.cs | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/YarnSpinner.LanguageServer.Tests/CommandTests.cs b/YarnSpinner.LanguageServer.Tests/CommandTests.cs index b6ef39908..95565f46c 100644 --- a/YarnSpinner.LanguageServer.Tests/CommandTests.cs +++ b/YarnSpinner.LanguageServer.Tests/CommandTests.cs @@ -308,4 +308,22 @@ public async Task Server_OnCompilingProject_GetsResult() result.Errors.Should().BeEmpty(); result.Data.Should().NotBeEmpty(); } + + [Fact] + public async Task Server_CanListProjects() + { + // Set up the server + var (client, server) = await Initialize(ConfigureClient, ConfigureServer); + + var result = await client.ExecuteCommand(new ExecuteCommandParams> + { + Command = Commands.ListProjects, + Arguments = new JArray { } + }); + result.Should().NotBeNullOrEmpty("because the workspace contains projects"); + + result.Should().ContainSingle(p => p.Uri!.Path.EndsWith("Project1.yarnproject")); + result.Should().ContainSingle(p => p.Uri!.Path.EndsWith("Project2.yarnproject")); + } + } diff --git a/YarnSpinner.LanguageServer/src/Server/Commands/Commands.cs b/YarnSpinner.LanguageServer/src/Server/Commands/Commands.cs index ea9370315..07136f09a 100644 --- a/YarnSpinner.LanguageServer/src/Server/Commands/Commands.cs +++ b/YarnSpinner.LanguageServer/src/Server/Commands/Commands.cs @@ -64,4 +64,9 @@ public static class Commands /// The command to show a specific Yarn node in a graph view. /// public const string ShowNodeInGraphView = "yarn.showNodeInGraphView"; + + /// + /// The command to get all projects in the current workspace. + /// + public const string ListProjects = "yarnspinner.listProjects"; } diff --git a/YarnSpinner.LanguageServer/src/Server/Workspace/Project.cs b/YarnSpinner.LanguageServer/src/Server/Workspace/Project.cs index 03199efab..0a5d6bd97 100644 --- a/YarnSpinner.LanguageServer/src/Server/Workspace/Project.cs +++ b/YarnSpinner.LanguageServer/src/Server/Workspace/Project.cs @@ -6,6 +6,13 @@ namespace YarnLanguageServer { + public record ProjectInfo + { + public DocumentUri? Uri { get; set; } + public IEnumerable Files { get; set; } = Array.Empty(); + public bool IsImplicitProject { get; set; } + } + internal class Project { public IEnumerable Files => yarnFiles.Values; diff --git a/YarnSpinner.LanguageServer/src/Server/YarnLanguageServer.cs b/YarnSpinner.LanguageServer/src/Server/YarnLanguageServer.cs index b230b403f..2e7ec81ee 100644 --- a/YarnSpinner.LanguageServer/src/Server/YarnLanguageServer.cs +++ b/YarnSpinner.LanguageServer/src/Server/YarnLanguageServer.cs @@ -150,6 +150,14 @@ public static LanguageServerOptions ConfigureOptions(LanguageServerOptions optio } ); + // Register List Projects command + options.OnExecuteCommand>( + (commandParams) => ListProjects(workspace, commandParams), (_,_) => new ExecuteCommandRegistrationOptions + { + Commands = new[] { Commands.ListProjects }, + } + ); + return options; } @@ -706,5 +714,16 @@ private static Task ExtractVoiceoverSpreadsheet(Workspace worksp }; return Task.FromResult(output); } + + private static Task> ListProjects(Workspace workspace, ExecuteCommandParams> commandParams) + { + var info = workspace.Projects.Select(p => new ProjectInfo + { + Uri = p.Uri, + Files = p.Files.Select(f => OmniSharp.Extensions.LanguageServer.Protocol.DocumentUri.From(f.Uri)), + IsImplicitProject = p.IsImplicitProject, + }); + return Task.FromResult(Container.From(info)); + } } }