...
This commit is contained in:
@@ -24,10 +24,7 @@ var server = await LanguageServer.From(options => options
|
||||
|
||||
if (request.RootPath != null)
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(request.RootPath, "*.nub", SearchOption.AllDirectories))
|
||||
{
|
||||
workspaceManager.UpdateFile(file);
|
||||
}
|
||||
workspaceManager.Init(request.RootPath);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -15,25 +15,25 @@ internal class TextDocumentSyncHandler(WorkspaceManager workspaceManager) : Text
|
||||
|
||||
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri.GetFileSystemPath());
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,64 +8,84 @@ namespace NubLang.LSP;
|
||||
|
||||
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILanguageServerFacade server)
|
||||
{
|
||||
private readonly Dictionary<DocumentUri, SyntaxTree> _syntaxTrees = new();
|
||||
private readonly Dictionary<DocumentUri, CompilationUnit> _compilationUnits = new();
|
||||
private readonly Dictionary<string, SyntaxTree> _syntaxTrees = 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 tokenizer = new Tokenizer(path.GetFileSystemPath(), text);
|
||||
var files = Directory.GetFiles(rootPath, "*.nub", SearchOption.AllDirectories);
|
||||
foreach (var path in files)
|
||||
{
|
||||
var text = File.ReadAllText(path);
|
||||
var tokenizer = new Tokenizer(path, text);
|
||||
|
||||
tokenizer.Tokenize();
|
||||
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
||||
|
||||
var parser = new Parser();
|
||||
var parseResult = parser.Parse(tokenizer.Tokens);
|
||||
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
||||
|
||||
_syntaxTrees[path] = parseResult;
|
||||
}
|
||||
|
||||
Generate();
|
||||
}
|
||||
|
||||
public void UpdateFile(DocumentUri path)
|
||||
{
|
||||
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 result = parser.Parse(tokenizer.Tokens);
|
||||
var parseResult = parser.Parse(tokenizer.Tokens);
|
||||
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
||||
|
||||
_syntaxTrees[path] = result;
|
||||
_syntaxTrees[fsPath] = parseResult;
|
||||
|
||||
if (typeCheck)
|
||||
{
|
||||
TypeCheck();
|
||||
}
|
||||
Generate();
|
||||
}
|
||||
|
||||
private void TypeCheck()
|
||||
private void Generate()
|
||||
{
|
||||
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 result = typeChecker.Check();
|
||||
diagnosticsPublisher.Publish(path, typeChecker.Diagnostics);
|
||||
_compilationUnits[path] = result;
|
||||
diagnosticsPublisher.Publish(documentUri, typeChecker.Diagnostics);
|
||||
_compilationUnits[documentUri] = result;
|
||||
|
||||
var generator = new Generator(result);
|
||||
var c = generator.Emit();
|
||||
|
||||
|
||||
server.SendNotification("nub/output", new
|
||||
{
|
||||
content = c,
|
||||
uri = path
|
||||
uri = documentUri
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveFile(Uri path)
|
||||
public void RemoveFile(DocumentUri path)
|
||||
{
|
||||
_syntaxTrees.Remove(path);
|
||||
_compilationUnits.Remove(path);
|
||||
var fsPath = path.GetFileSystemPath();
|
||||
_syntaxTrees.Remove(fsPath);
|
||||
_compilationUnits.Remove(fsPath);
|
||||
}
|
||||
|
||||
public Dictionary<DocumentUri, CompilationUnit> GetCompilationUnits()
|
||||
public Dictionary<string, CompilationUnit> GetCompilationUnits()
|
||||
{
|
||||
return _compilationUnits;
|
||||
}
|
||||
|
||||
public CompilationUnit? GetCompilationUnit(DocumentUri path)
|
||||
{
|
||||
return _compilationUnits.GetValueOrDefault(path);
|
||||
return _compilationUnits.GetValueOrDefault(path.GetFileSystemPath());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
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",
|
||||
"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": {
|
||||
"build": "tsc -p ./",
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
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';
|
||||
|
||||
let client: LanguageClient;
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
const serverExecutable = path.join(context.asAbsolutePath('server'), "nublsp");
|
||||
|
||||
client = new LanguageClient(
|
||||
@@ -27,16 +27,17 @@ export function activate(context: ExtensionContext) {
|
||||
{ scheme: 'file', pattern: '**/*.nub' }
|
||||
],
|
||||
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();
|
||||
@@ -46,5 +47,6 @@ export function deactivate(): Thenable<void> | undefined {
|
||||
if (!client) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return client.stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user