language server start
This commit is contained in:
@@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NubLang", "NubLang\NubLang.
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NubLang.CLI", "NubLang.CLI\NubLang.CLI.csproj", "{A22F17ED-FA17-45AB-92BA-CD02C28B3524}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NubLang.LSP", "NubLang.LSP\NubLang.LSP.csproj", "{07968F84-0C2E-4D2E-8905-DC8A6140B4C0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -18,5 +20,9 @@ Global
|
||||
{A22F17ED-FA17-45AB-92BA-CD02C28B3524}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A22F17ED-FA17-45AB-92BA-CD02C28B3524}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A22F17ED-FA17-45AB-92BA-CD02C28B3524}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{07968F84-0C2E-4D2E-8905-DC8A6140B4C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{07968F84-0C2E-4D2E-8905-DC8A6140B4C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{07968F84-0C2E-4D2E-8905-DC8A6140B4C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{07968F84-0C2E-4D2E-8905-DC8A6140B4C0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
|
||||
using Range = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range;
|
||||
|
||||
namespace NubLang.LSP;
|
||||
|
||||
@@ -13,12 +15,30 @@ public class DiagnosticsPublisher
|
||||
_server = server;
|
||||
}
|
||||
|
||||
public void Publish(Uri uri, IEnumerable<Diagnostic> diagnostics)
|
||||
public void Publish(DocumentUri uri, IEnumerable<Diagnostics.Diagnostic> diagnostics)
|
||||
{
|
||||
_server.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
|
||||
{
|
||||
Uri = uri,
|
||||
Diagnostics = new Container<Diagnostic>(diagnostics)
|
||||
Diagnostics = new Container<Diagnostic>(diagnostics.Select(MapDiagnostic))
|
||||
});
|
||||
}
|
||||
|
||||
private static Diagnostic MapDiagnostic(Diagnostics.Diagnostic nubDiagnostic)
|
||||
{
|
||||
return new Diagnostic
|
||||
{
|
||||
Severity = nubDiagnostic.Severity switch
|
||||
{
|
||||
Diagnostics.DiagnosticSeverity.Info => DiagnosticSeverity.Information,
|
||||
Diagnostics.DiagnosticSeverity.Warning => DiagnosticSeverity.Warning,
|
||||
Diagnostics.DiagnosticSeverity.Error => DiagnosticSeverity.Error,
|
||||
_ => null
|
||||
},
|
||||
Message = $"{nubDiagnostic.Message}\nhelp: {nubDiagnostic.Help}",
|
||||
Range = nubDiagnostic.Span.HasValue
|
||||
? new Range(nubDiagnostic.Span.Value.Start.Line, nubDiagnostic.Span.Value.Start.Column, nubDiagnostic.Span.Value.End.Line, nubDiagnostic.Span.Value.Start.Column)
|
||||
: new Range(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,21 @@ internal class HoverHandler : HoverHandlerBase
|
||||
{
|
||||
protected override HoverRegistrationOptions CreateRegistrationOptions(HoverCapability capability, ClientCapabilities clientCapabilities)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return new HoverRegistrationOptions
|
||||
{
|
||||
DocumentSelector = TextDocumentSelector.ForLanguage("nub-lang")
|
||||
};
|
||||
}
|
||||
|
||||
public override Task<Hover?> Handle(HoverParams request, CancellationToken cancellationToken)
|
||||
public override async Task<Hover?> Handle(HoverParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return new Hover
|
||||
{
|
||||
Contents = new MarkedStringsOrMarkupContent(new MarkupContent
|
||||
{
|
||||
Value = "# uwu",
|
||||
Kind = MarkupKind.Markdown,
|
||||
})
|
||||
};
|
||||
}
|
||||
}
|
||||
18
compiler/NubLang.LSP/NubLang.LSP.csproj
Normal file
18
compiler/NubLang.LSP/NubLang.LSP.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.19.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NubLang\NubLang.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
36
compiler/NubLang.LSP/Program.cs
Normal file
36
compiler/NubLang.LSP/Program.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NubLang.LSP;
|
||||
using OmniSharp.Extensions.LanguageServer.Server;
|
||||
|
||||
var server = await LanguageServer.From(options => options
|
||||
.WithInput(Console.OpenStandardInput())
|
||||
.WithOutput(Console.OpenStandardOutput())
|
||||
.WithServices(services =>
|
||||
{
|
||||
services.AddSingleton<DiagnosticsPublisher>();
|
||||
services.AddSingleton<WorkspaceManager>();
|
||||
})
|
||||
.ConfigureLogging(x => x
|
||||
.AddLanguageProtocolLogging()
|
||||
.SetMinimumLevel(LogLevel.Debug))
|
||||
.WithHandler<TextDocumentSyncHandler>()
|
||||
.WithHandler<HoverHandler>()
|
||||
.OnInitialize((server, request, ct) =>
|
||||
{
|
||||
server.SendNotification("TEST");
|
||||
var workspaceManager = server.GetRequiredService<WorkspaceManager>();
|
||||
|
||||
if (request.RootPath != null)
|
||||
{
|
||||
foreach (var file in Directory.GetFiles(request.RootPath, "*.nub", SearchOption.AllDirectories))
|
||||
{
|
||||
workspaceManager.UpdateFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
})
|
||||
);
|
||||
|
||||
await server.WaitForExit;
|
||||
@@ -3,19 +3,11 @@ using OmniSharp.Extensions.LanguageServer.Protocol;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
|
||||
|
||||
namespace NubLang.LSP;
|
||||
|
||||
internal class TextDocumentSyncHandler : TextDocumentSyncHandlerBase
|
||||
internal class TextDocumentSyncHandler(WorkspaceManager workspaceManager) : TextDocumentSyncHandlerBase
|
||||
{
|
||||
private readonly DiagnosticsPublisher _diagnostics;
|
||||
|
||||
public TextDocumentSyncHandler(DiagnosticsPublisher diagnostics)
|
||||
{
|
||||
_diagnostics = diagnostics;
|
||||
}
|
||||
|
||||
public override TextDocumentAttributes GetTextDocumentAttributes(DocumentUri uri)
|
||||
{
|
||||
return new TextDocumentAttributes(uri, "nub-lang");
|
||||
@@ -23,25 +15,25 @@ internal class TextDocumentSyncHandler : TextDocumentSyncHandlerBase
|
||||
|
||||
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
_diagnostics.Publish(new Uri("https://example.com"), [new Diagnostic
|
||||
{
|
||||
Severity = DiagnosticSeverity.Error,
|
||||
}]);
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
|
||||
{
|
||||
workspaceManager.UpdateFile(request.TextDocument.Uri);
|
||||
return Unit.Task;
|
||||
}
|
||||
|
||||
@@ -49,23 +41,4 @@ internal class TextDocumentSyncHandler : TextDocumentSyncHandlerBase
|
||||
{
|
||||
return new TextDocumentSyncRegistrationOptions();
|
||||
}
|
||||
}
|
||||
|
||||
public class DiagnosticsPublisher
|
||||
{
|
||||
private readonly ILanguageServerFacade _server;
|
||||
|
||||
public DiagnosticsPublisher(ILanguageServerFacade server)
|
||||
{
|
||||
_server = server;
|
||||
}
|
||||
|
||||
public void Publish(Uri uri, IEnumerable<Diagnostic> diagnostics)
|
||||
{
|
||||
_server.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams
|
||||
{
|
||||
Uri = uri,
|
||||
Diagnostics = new Container<Diagnostic>(diagnostics)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,29 @@
|
||||
using NubLang.Syntax;
|
||||
using OmniSharp.Extensions.LanguageServer.Protocol;
|
||||
|
||||
namespace NubLang.LSP;
|
||||
|
||||
public class WorkspaceManager
|
||||
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
|
||||
{
|
||||
|
||||
private readonly Dictionary<DocumentUri, SyntaxTree> _files = new();
|
||||
|
||||
public void UpdateFile(DocumentUri path)
|
||||
{
|
||||
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);
|
||||
|
||||
_files[path] = result;
|
||||
}
|
||||
|
||||
public void RemoveFile(Uri path)
|
||||
{
|
||||
_files.Remove(path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user