Skip to content

Commit

Permalink
Merge branch 'anytext' into LSP-folding-ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
nhett authored Jan 10, 2025
2 parents bc499d6 + 8a68f26 commit 8901f69
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 26 deletions.
21 changes: 10 additions & 11 deletions AnyText/AnyText.Lsp/AnyTextJsonRpcServerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace NMF.AnyText
/// </summary>
public static class AnyTextJsonRpcServerUtil
{
private static readonly TraceSource _traceSource = CreateTraceSource();

/// <summary>
/// Creates a StreamJSON RPC object for the given transport
/// </summary>
Expand All @@ -22,7 +20,6 @@ public static class AnyTextJsonRpcServerUtil
public static JsonRpc CreateServer(WebSocket webSocket, ILspServer server)
{
var rpc = new JsonRpc(new WebSocketMessageHandler(webSocket, CreateFormatter()));
rpc.TraceSource = _traceSource;
rpc.AddLocalRpcTarget(server, CreateTargetOptions());
return rpc;
}
Expand All @@ -36,7 +33,6 @@ public static JsonRpc CreateServer(WebSocket webSocket, ILspServer server)
public static JsonRpc CreateServer(IDuplexPipe pipe, ILspServer server)
{
var rpc = new JsonRpc(new HeaderDelimitedMessageHandler(pipe, CreateFormatter()));
rpc.TraceSource = _traceSource;
rpc.AddLocalRpcTarget(server, CreateTargetOptions());
return rpc;
}
Expand All @@ -50,7 +46,6 @@ public static JsonRpc CreateServer(IDuplexPipe pipe, ILspServer server)
public static JsonRpc CreateServer(Stream stream, ILspServer server)
{
var rpc = new JsonRpc(new HeaderDelimitedMessageHandler(stream, CreateFormatter()));
rpc.TraceSource = _traceSource;
rpc.AddLocalRpcTarget(server, CreateTargetOptions());
return rpc;
}
Expand All @@ -62,7 +57,6 @@ public static JsonRpc CreateServer(Stream stream, ILspServer server)
public static JsonRpc CreateServer(Stream stream)
{
var rpc = new JsonRpc(new HeaderDelimitedMessageHandler(stream, CreateFormatter()));
rpc.TraceSource = _traceSource;
return rpc;
}

Expand All @@ -84,15 +78,20 @@ private static JsonRpcTargetOptions CreateTargetOptions()
EventNameTransform = name => name.ToLowerInvariant()
};
}

private static TraceSource CreateTraceSource()
/// <summary>
/// Creates and configures a <see cref="TraceSource"/> instance for logging trace information.
/// </summary>
/// <param name="sourceLevels">The SourceLevel used to filter messages by type and severity. Defaults to <see cref="SourceLevels.All"/>.</param>
/// <returns>A <see cref="TraceSource"/> instance configured for the specified logging level.</returns>
public static TraceSource CreateTraceSource(SourceLevels sourceLevels = SourceLevels.All)
{
var traceSource = new TraceSource("LSP", SourceLevels.All);
// use error stream such that VS Code can see the stdout
traceSource.Listeners.Add(new ConsoleTraceListener(true));
var traceSource = new TraceSource("LSP", sourceLevels);
// Use error stream (stderr) so that VS Code can capture the output
traceSource.Listeners.Add(new ConsoleTraceListener(useErrorStream: true));
return traceSource;
}


private static IJsonRpcMessageFormatter CreateFormatter()
{
return new JsonMessageFormatter();
Expand Down
8 changes: 8 additions & 0 deletions AnyText/AnyText.Lsp/ILspServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ public InitializeResult Initialize(

[JsonRpcMethod(Methods.ShutdownName)]
void Shutdown();

/// <summary>
/// Handles the <c>*/setTrace</c> request from the client. This is used to set the trace setting of the server.
/// </summary>
/// <param name="arg">The JSON token containing the parameters of the request. (SetTraceParams)</param>
[JsonRpcMethod(MethodConstants.SetTrace)]
public void SetTrace(JToken arg);

}
}
6 changes: 5 additions & 1 deletion AnyText/AnyText.Lsp/LspServer.Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class LspServer

private async void SendDiagnostics(string uri, ParseContext context)
{
SendLogMessage(MessageType.Info, $"Starting diagnostics generation for URI: {uri}");
var diagnostics = new List<Diagnostic>();
var errors = context.Errors;
foreach (var error in errors)
Expand Down Expand Up @@ -38,10 +39,13 @@ private async void SendDiagnostics(string uri, ParseContext context)
try
{
await _rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, diagnosticsParams);
SendLogMessage(MessageType.Info, $"Diagnostics published successfully for URI: {uri} with {diagnostics.Count} issue(s).");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error publishing Diagnostics: {ex.Message}");
var errorMessage = $"Error publishing diagnostics for URI: {uri}. Exception: {ex.Message}";
SendLogMessage(MessageType.Error, errorMessage);
Console.Error.WriteLine(errorMessage);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion AnyText/AnyText.Lsp/LspServer.Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ private async void RegisterCapabilities(Registration[] registrations)

try
{
SendLogMessage(MessageType.Info, "Sending capabilities registration request to client.");
await _rpc.InvokeWithParameterObjectAsync(Methods.ClientRegisterCapabilityName, registrationParams);
SendLogMessage(MessageType.Info, "Capabilities registration request completed successfully.");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error register capabilities: {ex.Message}");
var errorMessage = $"Error registering capabilities: {ex.Message}";
SendLogMessage(MessageType.Error, errorMessage);
Console.Error.WriteLine(errorMessage);
}
}

Expand Down
3 changes: 3 additions & 0 deletions AnyText/AnyText.Lsp/LspServer.SemanticToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class LspServer
/// <inheritdoc cref="ILspServer.QuerySemanticTokens"/>
public SemanticTokens QuerySemanticTokens(JToken arg)
{
SendLogMessage(MessageType.Info, "Received request for full semantic tokens.");
var semanticTokensParams = arg.ToObject<SemanticTokensParams>();
var uri = semanticTokensParams.TextDocument.Uri;

Expand All @@ -33,6 +34,7 @@ public SemanticTokens QuerySemanticTokens(JToken arg)
/// <inheritdoc cref="ILspServer.QuerySemanticTokensDelta"/>
public SemanticTokensDelta QuerySemanticTokensDelta(JToken arg)
{
SendLogMessage(MessageType.Info, "Received request for semantic tokens delta.");
var semanticTokensParams = arg.ToObject<SemanticTokensDeltaParams>();
var uri = semanticTokensParams.TextDocument.Uri;

Expand Down Expand Up @@ -74,6 +76,7 @@ public SemanticTokensDelta QuerySemanticTokensDelta(JToken arg)
/// <inheritdoc cref="ILspServer.QuerySemanticTokensDelta"/>
public SemanticTokens QuerySemanticTokensRange(JToken arg)
{
SendLogMessage(MessageType.Info, "Received request for semantic tokens in a range.");
var semanticTokensRangeParams = arg.ToObject<SemanticTokensRangeParams>();
var uri = semanticTokensRangeParams.TextDocument.Uri;
var range = semanticTokensRangeParams.Range;
Expand Down
34 changes: 34 additions & 0 deletions AnyText/AnyText.Lsp/LspServer.Trace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using LspTypes;
using Newtonsoft.Json.Linq;
using System.Diagnostics;

namespace NMF.AnyText
{
public partial class LspServer
{
/// <inheritdoc cref="ILspServer.SetTrace"/>
public void SetTrace(JToken arg)
{
var setTraceParams = arg.ToObject<SetTraceParams>();

UpdateTraceSource(setTraceParams.Value);
SendLogMessage(MessageType.Info, $"Trace level updated to: {setTraceParams.Value}");
}

private void UpdateTraceSource(TraceValue traceValue)
{
_rpc.TraceSource =
AnyTextJsonRpcServerUtil.CreateTraceSource(MapTraceValueToSourceLevels(traceValue));
}
private static SourceLevels MapTraceValueToSourceLevels(TraceValue traceValue)
{
return traceValue switch
{
TraceValue.Off => SourceLevels.Off,
TraceValue.Messages => SourceLevels.Information,
TraceValue.Verbose => SourceLevels.Verbose,
_ => SourceLevels.All
};
}
}
}
37 changes: 33 additions & 4 deletions AnyText/AnyText.Lsp/LspServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LspTypes;
using LspTypes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NMF.AnyText.Grammars;
Expand Down Expand Up @@ -39,7 +39,6 @@ public LspServer(IEnumerable<Grammar> grammars)
_languages = grammars?.ToDictionary(sp => sp.LanguageId);
}

[JsonRpcMethod(Methods.InitializeName)]
public InitializeResult Initialize(
int? processId
, _InitializeParams_ClientInfo clientInfo
Expand Down Expand Up @@ -72,8 +71,12 @@ public InitializeResult Initialize(
WorkDoneProgress = false
}
};
UpdateTraceSource(trace);

SendLogMessage(MessageType.Info, "LSP Server initialization completed.");
return new InitializeResult { Capabilities = serverCapabilities };
}

public void Initialized() { }

public void Shutdown() { }
Expand All @@ -87,11 +90,13 @@ public void DidChange(JToken arg)
{
document.Update(changes.ContentChanges.Select(AsTextEdit));
SendDiagnostics(changes.TextDocument.Uri, document.Context);
SendLogMessage(MessageType.Info, $"Document {changes.TextDocument.Uri} updated.");
}
}

public void DidSave(TextDocumentIdentifier textDocument, string text)
{
SendLogMessage(MessageType.Info, $"Document {textDocument.Uri} saved.");
}

private static ParsePosition AsParsePosition(Position position) => new ParsePosition((int)position.Line, (int)position.Character);
Expand All @@ -108,6 +113,7 @@ public void DidClose(JToken arg)
if (_documents.TryGetValue(closeParams.TextDocument.Uri, out var document))
{
_documents.Remove(closeParams.TextDocument.Uri);
SendLogMessage(MessageType.Info, $"Document {closeParams.TextDocument.Uri} closed.");
}
}

Expand All @@ -126,20 +132,43 @@ public void DidOpen(JToken arg)

RegisterCapabilitiesOnOpen(openParams.TextDocument.LanguageId, parser);
SendDiagnostics(openParams.TextDocument.Uri, parser.Context);
SendLogMessage(MessageType.Info, $"Document {openParams.TextDocument.Uri} opened with language {openParams.TextDocument.LanguageId}.");
}
else
{
throw new NotSupportedException($"No grammar found for extension {openParams.TextDocument.LanguageId}");
var errorMessage = $"No grammar found for extension {openParams.TextDocument.LanguageId}";
SendLogMessage(MessageType.Error, errorMessage);
throw new NotSupportedException(errorMessage);
}
}
else
{
throw new NotSupportedException($"Cannot open URI {openParams.TextDocument.Uri}");
var errorMessage = $"Cannot open URI {openParams.TextDocument.Uri}";
SendLogMessage(MessageType.Error, errorMessage);
throw new NotSupportedException(errorMessage);
}
}
public void Exit()
{
SendLogMessage(MessageType.Info, "LSP Server exiting.");
throw new NotImplementedException();
}

// <summary>
/// Sends a log message to the client.
/// </summary>
/// <param name="type">The type of the message (Info, Warning, Error).</param>
/// <param name="message">The message content.</param>
private void SendLogMessage(MessageType type, string message)
{
var logMessageParams = new LogMessageParams
{
MessageType = type,
Message = message
};

_rpc.NotifyWithParameterObjectAsync(Methods.WindowLogMessageName, logMessageParams);

}
}
}
1 change: 1 addition & 0 deletions AnyText/AnyText.Lsp/MethodConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace NMF.AnyText
public static class MethodConstants
{
public const string RegisterSemanticTokens = "textDocument/semanticTokens";
public const string SetTrace = "$/setTrace";
}
}
4 changes: 2 additions & 2 deletions AnyText/Tests/AnyTextExtension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"type": "object",
"title": "Example configuration",
"properties": {
"languageServerExample.maxNumberOfProblems": {
"anytext.maxNumberOfProblems": {
"scope": "resource",
"type": "number",
"default": 100,
"description": "Controls the maximum number of problems produced by the server."
},
"languageServerExample.trace.server": {
"anytext.trace.server": {
"scope": "window",
"type": "string",
"enum": [
Expand Down
32 changes: 26 additions & 6 deletions Glsp/Glsp/Processing/Layouting/LayeredLayoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,35 @@ public class LayeredLayoutService : AglLayoutService
/// </summary>
public static readonly LayeredLayoutService Instance = new LayeredLayoutService();

private static readonly SugiyamaLayoutSettings _defaultSettings = new SugiyamaLayoutSettings
{
Transformation = PlaneTransformation.Rotation(Math.PI / 2),
EdgeRoutingSettings = { EdgeRoutingMode = EdgeRoutingMode.Rectilinear }
};

/// <summary>
/// Creates a new instance
/// </summary>
public LayeredLayoutService() : this(null) { }

/// <summary>
/// Creates a new instance
/// </summary>
/// <param name="settings">layout settings</param>
public LayeredLayoutService(SugiyamaLayoutSettings settings)
{
Settings = settings ?? _defaultSettings;
}

/// <summary>
/// Gets the layout settings for this layout service
/// </summary>
public SugiyamaLayoutSettings Settings { get; }

/// <inheritdoc />
protected override void ProcessLayout(GeometryGraph g)
{
var settings = new SugiyamaLayoutSettings
{
Transformation = PlaneTransformation.Rotation(Math.PI / 2),
EdgeRoutingSettings = { EdgeRoutingMode = EdgeRoutingMode.Rectilinear }
};
var layout = new LayeredLayout(g, settings);
var layout = new LayeredLayout(g, Settings);
layout.Run();
}
}
Expand Down
12 changes: 11 additions & 1 deletion Models/Models/Repository/MetaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ namespace NMF.Models.Repository
/// </summary>
public sealed class MetaRepository : IModelRepository
{
private static readonly MetaRepository instance = new MetaRepository();
private static readonly MetaRepository instance;
private readonly ModelCollection entries;
private readonly ModelSerializer serializer = new ModelSerializer();
private readonly HashSet<Assembly> traversedAssemblies = new HashSet<Assembly>();

/// <summary>
/// Initializes the type
/// </summary>
static MetaRepository()
{
// we need an explicit static type constructor because the runtime is sometimes too lazy
// to initialize static variables in time, but they are needed for static lookup operations
instance = new MetaRepository();
}

event EventHandler<BubbledChangeEventArgs> IModelRepository.BubbledChange
{
add { }
Expand Down

0 comments on commit 8901f69

Please sign in to comment.