Update lsp to have set root path

This commit is contained in:
nub31
2025-11-05 15:53:08 +01:00
parent d3822bc9b4
commit c3d64c4ea9
5 changed files with 74 additions and 13 deletions

View File

@@ -18,17 +18,7 @@ var server = await LanguageServer.From(options => options
.WithHandler<HoverHandler>()
.WithHandler<CompletionHandler>()
.WithHandler<DefinitionHandler>()
.OnInitialize((server, request, ct) =>
{
var workspaceManager = server.GetRequiredService<WorkspaceManager>();
if (request.RootPath != null)
{
workspaceManager.Init(request.RootPath);
}
return Task.CompletedTask;
})
.WithHandler<SetRootPathCommandHandler>()
);
await server.WaitForExit;

View File

@@ -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<string>("nub.setRootPath")
};
}
public override Task<Unit> 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;
}
}

View File

@@ -17,7 +17,7 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
private readonly Dictionary<string, List<TopLevelNode>> _possiblyOutdatedTopLevelNodes = [];
private ModuleRepository _repository = new([]);
public void Init(string rootPath)
public void SetRootPath(string rootPath)
{
_rootPath = rootPath;
Update();

View File

@@ -31,6 +31,12 @@
"configuration": "./language-configuration.json"
}
],
"commands": [
{
"command": "nub.setRootPath",
"title": "Set root path"
}
],
"grammars": [
{
"language": "nub",

View File

@@ -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<void> | undefined {
@@ -41,4 +53,26 @@ export function deactivate(): Thenable<void> | undefined {
}
return client.stop();
}
}
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}`);
}
}