This repository has been archived on 2025-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nub-lang-archive-2/compiler/NubLang.LSP/WorkspaceManager.cs
nub31 3da68a1e34 ...
2025-10-23 12:14:10 +02:00

60 lines
1.7 KiB
C#

using NubLang.Ast;
using NubLang.Syntax;
using OmniSharp.Extensions.LanguageServer.Protocol;
namespace NubLang.LSP;
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
{
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;
}
}
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);
}
}