...
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user