diff --git a/compiler/NubLang.LSP/Program.cs b/compiler/NubLang.LSP/Program.cs index 095630f..6152364 100644 --- a/compiler/NubLang.LSP/Program.cs +++ b/compiler/NubLang.LSP/Program.cs @@ -18,17 +18,7 @@ var server = await LanguageServer.From(options => options .WithHandler() .WithHandler() .WithHandler() - .OnInitialize((server, request, ct) => - { - var workspaceManager = server.GetRequiredService(); - - if (request.RootPath != null) - { - workspaceManager.Init(request.RootPath); - } - - return Task.CompletedTask; - }) + .WithHandler() ); await server.WaitForExit; \ No newline at end of file diff --git a/compiler/NubLang.LSP/SetRootPathCommandHandler.cs b/compiler/NubLang.LSP/SetRootPathCommandHandler.cs new file mode 100644 index 0000000..899e5dc --- /dev/null +++ b/compiler/NubLang.LSP/SetRootPathCommandHandler.cs @@ -0,0 +1,31 @@ +using MediatR; +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; +using OmniSharp.Extensions.LanguageServer.Protocol.Models; +using OmniSharp.Extensions.LanguageServer.Protocol.Workspace; + +namespace NubLang.LSP; + +public class SetRootPathCommandHandler(WorkspaceManager workspaceManager) : ExecuteCommandHandlerBase +{ + protected override ExecuteCommandRegistrationOptions CreateRegistrationOptions(ExecuteCommandCapability capability, ClientCapabilities clientCapabilities) + { + return new ExecuteCommandRegistrationOptions + { + Commands = new Container("nub.setRootPath") + }; + } + + public override Task Handle(ExecuteCommandParams request, CancellationToken cancellationToken) + { + if (request is { Command: "nub.setRootPath", Arguments.Count: > 0 }) + { + var newRoot = request.Arguments[0].ToString(); + if (!string.IsNullOrEmpty(newRoot)) + { + workspaceManager.SetRootPath(newRoot); + } + } + + return Unit.Task; + } +} \ No newline at end of file diff --git a/compiler/NubLang.LSP/WorkspaceManager.cs b/compiler/NubLang.LSP/WorkspaceManager.cs index 3fbe6eb..ee0686f 100644 --- a/compiler/NubLang.LSP/WorkspaceManager.cs +++ b/compiler/NubLang.LSP/WorkspaceManager.cs @@ -17,7 +17,7 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher) private readonly Dictionary> _possiblyOutdatedTopLevelNodes = []; private ModuleRepository _repository = new([]); - public void Init(string rootPath) + public void SetRootPath(string rootPath) { _rootPath = rootPath; Update(); diff --git a/vscode-lsp/package.json b/vscode-lsp/package.json index fff14ae..b80cfc8 100644 --- a/vscode-lsp/package.json +++ b/vscode-lsp/package.json @@ -31,6 +31,12 @@ "configuration": "./language-configuration.json" } ], + "commands": [ + { + "command": "nub.setRootPath", + "title": "Set root path" + } + ], "grammars": [ { "language": "nub", diff --git a/vscode-lsp/src/extension.ts b/vscode-lsp/src/extension.ts index 065b5cd..6e42d7f 100644 --- a/vscode-lsp/src/extension.ts +++ b/vscode-lsp/src/extension.ts @@ -32,7 +32,19 @@ export async function activate(context: vscode.ExtensionContext) { } ); + vscode.commands.registerCommand('nub.setRootPath', setRootPath); + client.start(); + + const choice = await vscode.window.showInformationMessage( + 'Do you want to set the root directory for the project', + 'Yes', + 'No' + ); + + if (choice === 'Yes') { + await setRootPath(); + } } export function deactivate(): Thenable | undefined { @@ -41,4 +53,26 @@ export function deactivate(): Thenable | undefined { } return client.stop(); -} \ No newline at end of file +} + +async function setRootPath() { + if (!client) return; + + const folder = await vscode.window.showOpenDialog({ + canSelectFolders: true, + canSelectFiles: false, + canSelectMany: false, + openLabel: 'Select root location' + }); + + if (folder && folder.length > 0) { + const newRoot = folder[0].fsPath; + + await client.sendRequest('workspace/executeCommand', { + command: 'nub.setRootPath', + arguments: [newRoot] + }); + + vscode.window.showInformationMessage(`Root path set to: ${newRoot}`); + } +}