...
This commit is contained in:
@@ -24,10 +24,7 @@ var server = await LanguageServer.From(options => options
|
|||||||
|
|
||||||
if (request.RootPath != null)
|
if (request.RootPath != null)
|
||||||
{
|
{
|
||||||
foreach (var file in Directory.GetFiles(request.RootPath, "*.nub", SearchOption.AllDirectories))
|
workspaceManager.Init(request.RootPath);
|
||||||
{
|
|
||||||
workspaceManager.UpdateFile(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|||||||
@@ -15,25 +15,25 @@ internal class TextDocumentSyncHandler(WorkspaceManager workspaceManager) : Text
|
|||||||
|
|
||||||
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||||
return Unit.Task;
|
return Unit.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,39 +8,58 @@ namespace NubLang.LSP;
|
|||||||
|
|
||||||
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILanguageServerFacade server)
|
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILanguageServerFacade server)
|
||||||
{
|
{
|
||||||
private readonly Dictionary<DocumentUri, SyntaxTree> _syntaxTrees = new();
|
private readonly Dictionary<string, SyntaxTree> _syntaxTrees = new();
|
||||||
private readonly Dictionary<DocumentUri, CompilationUnit> _compilationUnits = new();
|
private readonly Dictionary<string, CompilationUnit> _compilationUnits = new();
|
||||||
|
|
||||||
public void UpdateFile(DocumentUri path, bool typeCheck = true)
|
public void Init(string rootPath)
|
||||||
{
|
{
|
||||||
var text = File.ReadAllText(path.GetFileSystemPath());
|
var files = Directory.GetFiles(rootPath, "*.nub", SearchOption.AllDirectories);
|
||||||
var tokenizer = new Tokenizer(path.GetFileSystemPath(), text);
|
foreach (var path in files)
|
||||||
|
{
|
||||||
|
var text = File.ReadAllText(path);
|
||||||
|
var tokenizer = new Tokenizer(path, text);
|
||||||
|
|
||||||
tokenizer.Tokenize();
|
tokenizer.Tokenize();
|
||||||
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
||||||
|
|
||||||
var parser = new Parser();
|
var parser = new Parser();
|
||||||
var result = parser.Parse(tokenizer.Tokens);
|
var parseResult = parser.Parse(tokenizer.Tokens);
|
||||||
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
||||||
|
|
||||||
_syntaxTrees[path] = result;
|
_syntaxTrees[path] = parseResult;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeCheck)
|
Generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateFile(DocumentUri path)
|
||||||
{
|
{
|
||||||
TypeCheck();
|
var fsPath = path.GetFileSystemPath();
|
||||||
}
|
var text = File.ReadAllText(fsPath);
|
||||||
|
var tokenizer = new Tokenizer(fsPath, text);
|
||||||
|
|
||||||
|
tokenizer.Tokenize();
|
||||||
|
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
||||||
|
|
||||||
|
var parser = new Parser();
|
||||||
|
var parseResult = parser.Parse(tokenizer.Tokens);
|
||||||
|
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
||||||
|
|
||||||
|
_syntaxTrees[fsPath] = parseResult;
|
||||||
|
|
||||||
|
Generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TypeCheck()
|
private void Generate()
|
||||||
{
|
{
|
||||||
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
|
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
|
||||||
|
|
||||||
foreach (var (path, syntaxTree) in _syntaxTrees)
|
foreach (var (documentUri, syntaxTree) in _syntaxTrees)
|
||||||
{
|
{
|
||||||
var typeChecker = new TypeChecker(syntaxTree, modules);
|
var typeChecker = new TypeChecker(syntaxTree, modules);
|
||||||
var result = typeChecker.Check();
|
var result = typeChecker.Check();
|
||||||
diagnosticsPublisher.Publish(path, typeChecker.Diagnostics);
|
diagnosticsPublisher.Publish(documentUri, typeChecker.Diagnostics);
|
||||||
_compilationUnits[path] = result;
|
_compilationUnits[documentUri] = result;
|
||||||
|
|
||||||
var generator = new Generator(result);
|
var generator = new Generator(result);
|
||||||
var c = generator.Emit();
|
var c = generator.Emit();
|
||||||
@@ -48,24 +67,25 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILangua
|
|||||||
server.SendNotification("nub/output", new
|
server.SendNotification("nub/output", new
|
||||||
{
|
{
|
||||||
content = c,
|
content = c,
|
||||||
uri = path
|
uri = documentUri
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveFile(Uri path)
|
public void RemoveFile(DocumentUri path)
|
||||||
{
|
{
|
||||||
_syntaxTrees.Remove(path);
|
var fsPath = path.GetFileSystemPath();
|
||||||
_compilationUnits.Remove(path);
|
_syntaxTrees.Remove(fsPath);
|
||||||
|
_compilationUnits.Remove(fsPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<DocumentUri, CompilationUnit> GetCompilationUnits()
|
public Dictionary<string, CompilationUnit> GetCompilationUnits()
|
||||||
{
|
{
|
||||||
return _compilationUnits;
|
return _compilationUnits;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompilationUnit? GetCompilationUnit(DocumentUri path)
|
public CompilationUnit? GetCompilationUnit(DocumentUri path)
|
||||||
{
|
{
|
||||||
return _compilationUnits.GetValueOrDefault(path);
|
return _compilationUnits.GetValueOrDefault(path.GetFileSystemPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
dotnet publish -c Release ../compiler/NubLang.LSP/NubLang.LSP.csproj
|
dotnet publish -c Release ../compiler/NubLang.LSP/NubLang.LSP.csproj
|
||||||
cp ../compiler/NubLang.LSP/bin/Release/net9.0/linux-x64/publish/nublsp src/server/
|
cp ../compiler/NubLang.LSP/bin/Release/net9.0/linux-x64/publish/nublsp server/
|
||||||
@@ -31,7 +31,23 @@
|
|||||||
"scopeName": "source.nub",
|
"scopeName": "source.nub",
|
||||||
"path": "./syntaxes/nub.tmLanguage.json"
|
"path": "./syntaxes/nub.tmLanguage.json"
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"commands": [
|
||||||
|
{
|
||||||
|
"command": "nub.openOutput",
|
||||||
|
"title": "Nub: Show Output",
|
||||||
|
"icon": "$(open-preview)"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"menus": {
|
||||||
|
"editor/title": [
|
||||||
|
{
|
||||||
|
"command": "nub.openOutput",
|
||||||
|
"when": "editorLangId == nub || resourceExtname == .nub",
|
||||||
|
"group": "navigation"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc -p ./",
|
"build": "tsc -p ./",
|
||||||
|
|||||||
Binary file not shown.
@@ -1,10 +1,10 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { workspace, ExtensionContext, window, Uri, commands, StatusBarAlignment, ViewColumn } from 'vscode';
|
import vscode from 'vscode';
|
||||||
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';
|
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';
|
||||||
|
|
||||||
let client: LanguageClient;
|
let client: LanguageClient;
|
||||||
|
|
||||||
export function activate(context: ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
const serverExecutable = path.join(context.asAbsolutePath('server'), "nublsp");
|
const serverExecutable = path.join(context.asAbsolutePath('server'), "nublsp");
|
||||||
|
|
||||||
client = new LanguageClient(
|
client = new LanguageClient(
|
||||||
@@ -27,16 +27,17 @@ export function activate(context: ExtensionContext) {
|
|||||||
{ scheme: 'file', pattern: '**/*.nub' }
|
{ scheme: 'file', pattern: '**/*.nub' }
|
||||||
],
|
],
|
||||||
synchronize: {
|
synchronize: {
|
||||||
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
|
fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let outputMap: Map<string, string> = new Map();
|
client.onNotification("nub/output", (params: { path: string, content: string }) => {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
vscode.commands.registerCommand('nub.openOutput', async () => {
|
||||||
|
|
||||||
client.onNotification("nub/output", (params) => {
|
|
||||||
const virtualUri = Uri.parse(`nub://${params.uri}`);
|
|
||||||
outputMap.set(virtualUri.toString(), params.content);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.start();
|
client.start();
|
||||||
@@ -46,5 +47,6 @@ export function deactivate(): Thenable<void> | undefined {
|
|||||||
if (!client) {
|
if (!client) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.stop();
|
return client.stop();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user