From be9db264dd51fda3243bdd7c8fbe02fbe346a156 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Fri, 6 Sep 2024 12:48:50 +1200 Subject: [PATCH] Minor toolshed fixes / tweaks (#5315) * Don't use markup for type names * Cache TypeTypeParser completions * Cache all type parsers * Release notes * More IConError fixes * a --- RELEASE-NOTES.md | 1 + .../Errors/NotForServerConsoleError.cs | 2 +- .../Errors/SessionHasNoEntityError.cs | 2 +- .../Toolshed/Syntax/ParserContext.cs | 2 +- Robust.Shared/Toolshed/Syntax/ValueRef.cs | 3 +-- .../Toolshed/ToolshedManager.Parsing.cs | 12 +++++++--- .../Toolshed/TypeParsers/StringTypeParser.cs | 2 +- .../Toolshed/TypeParsers/TypeTypeParser.cs | 23 +++++++------------ 8 files changed, 23 insertions(+), 24 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 7e477bed387..49713b58c42 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -46,6 +46,7 @@ END TEMPLATE--> ### Bugfixes * Fixed equality checks for `MarkupNode` not properly handling attributes. +* Fixed toolshed commands failing to generate error messages when working with array types * Fixed `MarkupNode` not having a `GetHashCode()` implementation. ### Other diff --git a/Robust.Shared/Toolshed/Errors/NotForServerConsoleError.cs b/Robust.Shared/Toolshed/Errors/NotForServerConsoleError.cs index cdd8ddff140..77ef43fb06b 100644 --- a/Robust.Shared/Toolshed/Errors/NotForServerConsoleError.cs +++ b/Robust.Shared/Toolshed/Errors/NotForServerConsoleError.cs @@ -8,7 +8,7 @@ public sealed class NotForServerConsoleError : IConError { public FormattedMessage DescribeInner() { - return FormattedMessage.FromMarkupOrThrow( + return FormattedMessage.FromUnformatted( "You must be logged in with a client to use this, the server console isn't workable."); } diff --git a/Robust.Shared/Toolshed/Errors/SessionHasNoEntityError.cs b/Robust.Shared/Toolshed/Errors/SessionHasNoEntityError.cs index 3fddf8c78ed..d835a5bc3cd 100644 --- a/Robust.Shared/Toolshed/Errors/SessionHasNoEntityError.cs +++ b/Robust.Shared/Toolshed/Errors/SessionHasNoEntityError.cs @@ -9,7 +9,7 @@ public record SessionHasNoEntityError(ICommonSession Session) : IConError { public FormattedMessage DescribeInner() { - return FormattedMessage.FromMarkupOrThrow($"The user {Session.Name} has no attached entity."); + return FormattedMessage.FromUnformatted($"The user {Session.Name} has no attached entity."); } public string? Expression { get; set; } diff --git a/Robust.Shared/Toolshed/Syntax/ParserContext.cs b/Robust.Shared/Toolshed/Syntax/ParserContext.cs index 1b684898db2..bf2a4308e0d 100644 --- a/Robust.Shared/Toolshed/Syntax/ParserContext.cs +++ b/Robust.Shared/Toolshed/Syntax/ParserContext.cs @@ -352,7 +352,7 @@ public record OutOfInputError : IConError { public FormattedMessage DescribeInner() { - return FormattedMessage.FromMarkupOrThrow("Ran out of input data when data was expected."); + return FormattedMessage.FromUnformatted("Ran out of input data when data was expected."); } public string? Expression { get; set; } diff --git a/Robust.Shared/Toolshed/Syntax/ValueRef.cs b/Robust.Shared/Toolshed/Syntax/ValueRef.cs index cb0d5042d81..2716cb60da1 100644 --- a/Robust.Shared/Toolshed/Syntax/ValueRef.cs +++ b/Robust.Shared/Toolshed/Syntax/ValueRef.cs @@ -103,8 +103,7 @@ public record BadVarTypeError(Type Got, Type Expected, string VarName) : IConErr { public FormattedMessage DescribeInner() { - return FormattedMessage.FromMarkupOrThrow( - $"Got unexpected type {Got.PrettyName()} in {VarName}, expected {Expected.PrettyName()}"); + return FormattedMessage.FromUnformatted($"Got unexpected type {Got.PrettyName()} in {VarName}, expected {Expected.PrettyName()}"); } public string? Expression { get; set; } diff --git a/Robust.Shared/Toolshed/ToolshedManager.Parsing.cs b/Robust.Shared/Toolshed/ToolshedManager.Parsing.cs index b346c99a8f9..8171749b5c7 100644 --- a/Robust.Shared/Toolshed/ToolshedManager.Parsing.cs +++ b/Robust.Shared/Toolshed/ToolshedManager.Parsing.cs @@ -16,7 +16,7 @@ namespace Robust.Shared.Toolshed; public sealed partial class ToolshedManager { - private readonly Dictionary _consoleTypeParsers = new(); + private readonly Dictionary _consoleTypeParsers = new(); private readonly Dictionary _genericTypeParsers = new(); private readonly List<(Type, Type)> _constrainedParsers = new(); @@ -55,6 +55,14 @@ private void InitializeParser() if (_consoleTypeParsers.TryGetValue(t, out var parser)) return parser; + parser = FindParserForType(t); + _consoleTypeParsers.TryAdd(t, parser); + return parser; + + } + + private ITypeParser? FindParserForType(Type t) + { if (t.IsConstructedGenericType) { if (_genericTypeParsers.TryGetValue(t.GetGenericTypeDefinition(), out var genParser)) @@ -65,7 +73,6 @@ private void InitializeParser() var builtParser = (ITypeParser) _typeFactory.CreateInstanceUnchecked(concreteParser, true); builtParser.PostInject(); - _consoleTypeParsers.Add(builtParser.Parses, builtParser); return builtParser; } catch (SecurityException) @@ -86,7 +93,6 @@ private void InitializeParser() var builtParser = (ITypeParser) _typeFactory.CreateInstanceUnchecked(concreteParser, true); builtParser.PostInject(); - _consoleTypeParsers.Add(builtParser.Parses, builtParser); return builtParser; } catch (SecurityException) diff --git a/Robust.Shared/Toolshed/TypeParsers/StringTypeParser.cs b/Robust.Shared/Toolshed/TypeParsers/StringTypeParser.cs index f4d4c3b7a3e..7c901803e7c 100644 --- a/Robust.Shared/Toolshed/TypeParsers/StringTypeParser.cs +++ b/Robust.Shared/Toolshed/TypeParsers/StringTypeParser.cs @@ -93,7 +93,7 @@ public record struct StringMustStartWithQuote : IConError { public FormattedMessage DescribeInner() { - return FormattedMessage.FromMarkupOrThrow("A string must start with a quote."); + return FormattedMessage.FromUnformatted("A string must start with a quote."); } public string? Expression { get; set; } diff --git a/Robust.Shared/Toolshed/TypeParsers/TypeTypeParser.cs b/Robust.Shared/Toolshed/TypeParsers/TypeTypeParser.cs index e8803fe7c54..243301abf44 100644 --- a/Robust.Shared/Toolshed/TypeParsers/TypeTypeParser.cs +++ b/Robust.Shared/Toolshed/TypeParsers/TypeTypeParser.cs @@ -10,7 +10,6 @@ using Robust.Shared.Console; using Robust.Shared.ContentPack; using Robust.Shared.IoC; -using Robust.Shared.Log; using Robust.Shared.Maths; using Robust.Shared.Toolshed.Errors; using Robust.Shared.Toolshed.Syntax; @@ -56,6 +55,7 @@ internal sealed class TypeTypeParser : TypeParser }; private readonly HashSet _ambiguousTypes = new(); + private CompletionResult? _optionsCache; public override void PostInject() { @@ -75,6 +75,8 @@ public override void PostInject() } } } + + _optionsCache = CompletionResult.FromHintOptions(Types.Select(x => new CompletionOption(x.Key)), "C# level type"); } public override bool TryParse(ParserContext parserContext, [NotNullWhen(true)] out object? result, out IConError? error) @@ -168,8 +170,7 @@ public override bool TryParse(ParserContext parserContext, [NotNullWhen(true)] o string? argName) { // TODO: Suggest generics. - var options = Types.Select(x => new CompletionOption(x.Key)); - return ValueTask.FromResult<(CompletionResult? result, IConError? error)>((CompletionResult.FromHintOptions(options, "C# level type"), null)); + return ValueTask.FromResult<(CompletionResult? result, IConError? error)>((_optionsCache, null)); } } @@ -177,9 +178,7 @@ public record struct ExpectedNextType() : IConError { public FormattedMessage DescribeInner() { - var msg = new FormattedMessage(); - msg.AddText($"Expected another type in the generic arguments."); - return msg; + return FormattedMessage.FromUnformatted("Expected another type in the generic arguments."); } public string? Expression { get; set; } @@ -191,9 +190,7 @@ public record struct ExpectedGeneric() : IConError { public FormattedMessage DescribeInner() { - var msg = new FormattedMessage(); - msg.AddText($"Expected a generic type, did you forget the angle brackets?"); - return msg; + return FormattedMessage.FromUnformatted("Expected a generic type, did you forget the angle brackets?"); } public string? Expression { get; set; } @@ -205,9 +202,7 @@ public record struct UnknownType(string T) : IConError { public FormattedMessage DescribeInner() { - var msg = new FormattedMessage(); - msg.AddText($"The type {T} is not known and cannot be used."); - return msg; + return FormattedMessage.FromUnformatted($"The type {T} is not known and cannot be used."); } public string? Expression { get; set; } @@ -220,9 +215,7 @@ internal record struct TypeIsSandboxViolation(Type T) : IConError { public FormattedMessage DescribeInner() { - var msg = new FormattedMessage(); - msg.AddText($"The type {T.PrettyName()} is not permitted under sandbox rules."); - return msg; + return FormattedMessage.FromUnformatted($"The type {T.PrettyName()} is not permitted under sandbox rules."); } public string? Expression { get; set; }