Skip to content

Commit

Permalink
Various fixes (#450)
Browse files Browse the repository at this point in the history
* Update Draco.LanguageServer.csproj

* Crashbug fixes

* More crashbugs

* Update TypeProvider.cs
  • Loading branch information
LPeter1997 authored Aug 28, 2024
1 parent e42ac24 commit fb878c9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/Draco.Compiler/Api/CodeCompletion/SignatureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ public sealed class SignatureService
var activeParam = separatorCount == paramCount - 1 ? paramCount - 1 : paramCount;

// Select the best overload to show as default in the signature
// TODO: Improve this, this is really primitive
var currentOverload = symbols.FirstOrDefault(x => x.Parameters.Length == paramCount && (separatorCount == paramCount - 1 || paramCount == 0));
currentOverload ??= symbols.FirstOrDefault(x => x.Parameters.Length > paramCount);
currentOverload ??= symbols.First();
IParameterSymbol? currentParameter = null;
if (currentOverload.Parameters.Length != 0) currentParameter = currentOverload.Parameters[activeParam];
if (currentOverload.Parameters.Length != 0)
{
currentParameter = currentOverload.Parameters.Length > activeParam
? currentOverload.Parameters[activeParam]
: currentOverload.Parameters[^1];
}
// Return all the overloads
return new SignatureItem(symbols, currentOverload, currentParameter);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Draco.Compiler/Internal/Binding/SymbolResolutionErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,13 @@ internal static class SymbolResolutionErrors
severity: DiagnosticSeverity.Error,
format: "the member {0} must be a gettable property",
code: Code(17));

/// <summary>
/// The referenced assembly can not be found.
/// </summary>
public static readonly DiagnosticTemplate CanNotResolveReferencedAssembly = DiagnosticTemplate.Create(
title: "can not resolve referenced assembly",
severity: DiagnosticSeverity.Error,
format: "the referenced assembly {0} can not be resolved",
code: Code(18));
}
6 changes: 6 additions & 0 deletions src/Draco.Compiler/Internal/Symbols/Error/ErrorTypeSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Draco.Compiler.Internal.Symbols.Generic;
using System.Collections.Immutable;

namespace Draco.Compiler.Internal.Symbols.Error;

/// <summary>
Expand All @@ -13,4 +16,7 @@ internal sealed class ErrorTypeSymbol(string name) : TypeSymbol
public string DisplayName { get; } = name;

public override string ToString() => this.DisplayName;

public override TypeSymbol GenericInstantiate(Symbol? containingSymbol, ImmutableArray<TypeSymbol> arguments) => this;
public override TypeSymbol GenericInstantiate(Symbol? containingSymbol, GenericContext context) => this;
}
22 changes: 17 additions & 5 deletions src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Reflection;
using System.Reflection.Metadata;
using Draco.Compiler.Api;
using Draco.Compiler.Api.Diagnostics;
using Draco.Compiler.Internal.Binding;
using Draco.Compiler.Internal.Symbols.Synthetized;

namespace Draco.Compiler.Internal.Symbols.Metadata;
Expand Down Expand Up @@ -192,9 +194,19 @@ private TypeSymbol BuildTypeFromReference(MetadataReader reader, TypeReferenceHa
if (!string.IsNullOrEmpty(@namespace)) parts.AddRange(@namespace.Split('.').Reverse());
parts.Reverse();

// TODO: If we don't have the assembly report error
var assemblyName = reader.GetAssemblyReference((AssemblyReferenceHandle)resolutionScope).GetAssemblyName();
var assembly = compilation.MetadataAssemblies.Single(x => AssemblyNamesEqual(x.AssemblyName, assemblyName));
var assembly = compilation.MetadataAssemblies.FirstOrDefault(x =>
AssemblyIsSufficient(assemblyName, x.AssemblyName));
if (assembly is null)
{
// The assembly for some reason isn't included, report it
compilation.GlobalDiagnosticBag.Add(Diagnostic.Create(
template: SymbolResolutionErrors.CanNotResolveReferencedAssembly,
location: Location.None,
formatArgs: assemblyName));
return WellKnownTypes.ErrorType;
}

return assembly.RootNamespace.Lookup([.. parts]).OfType<TypeSymbol>().Single();
}

Expand Down Expand Up @@ -226,7 +238,7 @@ private static string ConcatenateNamespaceAndName(string? @namespace, string nam
// NOTE: For some reason we had to disregard public key token, otherwise some weird type referencing
// case in the REPL with lists threw an exception
// TODO: Could it be that we don't write the public key token in the type refs we use?
private static bool AssemblyNamesEqual(AssemblyName a, AssemblyName b) =>
a.Name == b.Name
&& a.Version == b.Version;
private static bool AssemblyIsSufficient(AssemblyName wanted, AssemblyName got) =>
got.Name == wanted.Name
&& got.Version >= wanted.Version;
}
1 change: 1 addition & 0 deletions src/Draco.LanguageServer/Draco.LanguageServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="NuGet.Packaging" Version="6.11.0" ExcludeAssets="runtime" />
<PackageReference Include="NuGet.Common" Version="6.11.0" ExcludeAssets="runtime" />
<PackageReference Include="NuGet.Versioning" Version="6.11.0" ExcludeAssets="runtime" />
<PackageReference Include="NuGet.Configuration" Version="6.11.0" ExcludeAssets="runtime" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion src/Draco.LanguageServer/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ or CompilerApi.CodeCompletion.CompletionKind.DeclarationKeyword
CompilerApi.CodeCompletion.CompletionKind.ReferenceTypeName => LspModels.CompletionItemKind.Class,
CompilerApi.CodeCompletion.CompletionKind.ValueTypeName => LspModels.CompletionItemKind.Struct,

_ => throw new System.ArgumentOutOfRangeException(nameof(kind)),
CompilerApi.CodeCompletion.CompletionKind.Operator => LspModels.CompletionItemKind.Operator,

_ => LspModels.CompletionItemKind.Text,
};

public static LspModels.SignatureHelp? ToLsp(CompilerApi.CodeCompletion.SignatureItem? item) => item is null ? null : new()
Expand Down

0 comments on commit fb878c9

Please sign in to comment.