71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using NubLang.Ast;
|
|
using NubLang.Generation;
|
|
using NubLang.Syntax;
|
|
using OmniSharp.Extensions.LanguageServer.Protocol;
|
|
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
|
|
|
|
namespace NubLang.LSP;
|
|
|
|
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILanguageServerFacade server)
|
|
{
|
|
private readonly Dictionary<DocumentUri, SyntaxTree> _syntaxTrees = new();
|
|
private readonly Dictionary<DocumentUri, CompilationUnit> _compilationUnits = new();
|
|
|
|
public void UpdateFile(DocumentUri path, bool typeCheck = true)
|
|
{
|
|
var text = File.ReadAllText(path.GetFileSystemPath());
|
|
var tokenizer = new Tokenizer(path.GetFileSystemPath(), text);
|
|
|
|
tokenizer.Tokenize();
|
|
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
|
|
|
|
var parser = new Parser();
|
|
var result = parser.Parse(tokenizer.Tokens);
|
|
diagnosticsPublisher.Publish(path, parser.Diagnostics);
|
|
|
|
_syntaxTrees[path] = result;
|
|
|
|
if (typeCheck)
|
|
{
|
|
TypeCheck();
|
|
}
|
|
}
|
|
|
|
private void TypeCheck()
|
|
{
|
|
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
|
|
|
|
foreach (var (path, syntaxTree) in _syntaxTrees)
|
|
{
|
|
var typeChecker = new TypeChecker(syntaxTree, modules);
|
|
var result = typeChecker.Check();
|
|
diagnosticsPublisher.Publish(path, typeChecker.Diagnostics);
|
|
_compilationUnits[path] = result;
|
|
|
|
var generator = new Generator(result);
|
|
var c = generator.Emit();
|
|
|
|
server.SendNotification("nub/output", new
|
|
{
|
|
content = c,
|
|
uri = path
|
|
});
|
|
}
|
|
}
|
|
|
|
public void RemoveFile(Uri path)
|
|
{
|
|
_syntaxTrees.Remove(path);
|
|
_compilationUnits.Remove(path);
|
|
}
|
|
|
|
public Dictionary<DocumentUri, CompilationUnit> GetCompilationUnits()
|
|
{
|
|
return _compilationUnits;
|
|
}
|
|
|
|
public CompilationUnit? GetCompilationUnit(DocumentUri path)
|
|
{
|
|
return _compilationUnits.GetValueOrDefault(path);
|
|
}
|
|
} |