From f01bcc4c794ef234c8638d07b44e0b264861c730 Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sat, 14 Oct 2023 18:21:58 +0200 Subject: [PATCH] Fixes --- .../Internal/Symbols/Metadata/TypeProvider.cs | 17 ++++----------- .../Server/LanguageServerConnection.cs | 21 +++++++------------ 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs index f8b4093b01..317b1acbec 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -21,7 +22,7 @@ internal sealed class TypeProvider : ISignatureTypeProvider, private IntrinsicSymbols IntrinsicSymbols => this.compilation.IntrinsicSymbols; private readonly Compilation compilation; - private readonly Dictionary cache = new(); + private readonly ConcurrentDictionary cache = new(); public TypeProvider(Compilation compilation) { @@ -94,12 +95,7 @@ public TypeSymbol GetGenericTypeParameter(Symbol genericContext, int index) public TypeSymbol GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) { var key = new CacheKey(reader, handle); - if (!this.cache.TryGetValue(key, out var type)) - { - type = this.BuildTypeFromDefinition(reader, handle, rawTypeKind); - this.cache.Add(key, type); - } - return type; + return this.cache.GetOrAdd(key, _ => this.BuildTypeFromDefinition(reader, handle, rawTypeKind)); } private TypeSymbol BuildTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) @@ -137,12 +133,7 @@ private TypeSymbol BuildTypeFromDefinition(MetadataReader reader, TypeDefinition public TypeSymbol GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) { var key = new CacheKey(reader, handle); - if (!this.cache.TryGetValue(key, out var type)) - { - type = this.BuildTypeFromReference(reader, handle, rawTypeKind); - this.cache.Add(key, type); - } - return type; + return this.cache.GetOrAdd(key, _ => this.BuildTypeFromReference(reader, handle, rawTypeKind)); } private TypeSymbol BuildTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) diff --git a/src/Draco.Lsp/Server/LanguageServerConnection.cs b/src/Draco.Lsp/Server/LanguageServerConnection.cs index 2cf2cc5999..3535d44852 100644 --- a/src/Draco.Lsp/Server/LanguageServerConnection.cs +++ b/src/Draco.Lsp/Server/LanguageServerConnection.cs @@ -63,27 +63,22 @@ public LanguageServerConnection(IDuplexPipe transport) protected override LspMessage CreateOkResponseMessage(object id, JsonElement okResult) => new(new ResponseMessage { Jsonrpc = "2.0", - Id = id switch - { - int i => i, - string s => s, - _ => -1, - }, + Id = (OneOf)id, Result = okResult, }); protected override LspMessage CreateErrorResponseMessage(object id, ResponseError errorResult) => new(new ResponseMessage { Jsonrpc = "2.0", - Id = id switch - { - int i => i, - string s => s, - _ => -1, - }, + Id = (OneOf)id, Error = errorResult, }); protected override LspMessage CreateCancelRequestMessage(int id) => throw new NotImplementedException(); - protected override LspMessage CreateNotificationMessage(string method, JsonElement @params) => throw new NotImplementedException(); + protected override LspMessage CreateNotificationMessage(string method, JsonElement @params) => new(new NotificationMessage + { + Jsonrpc = "2.0", + Method = method, + Params = @params, + }); protected override ResponseError CreateExceptionError(Exception exception) {