Skip to content

Commit

Permalink
Minor toolshed fixes / tweaks (#5315)
Browse files Browse the repository at this point in the history
* Don't use markup for type names

* Cache TypeTypeParser completions

* Cache all type parsers

* Release notes

* More IConError fixes

* a
  • Loading branch information
ElectroJr authored Sep 6, 2024
1 parent 2f73f61 commit be9db26
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Toolshed/Errors/NotForServerConsoleError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Toolshed/Errors/SessionHasNoEntityError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Toolshed/Syntax/ParserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 1 addition & 2 deletions Robust.Shared/Toolshed/Syntax/ValueRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
12 changes: 9 additions & 3 deletions Robust.Shared/Toolshed/ToolshedManager.Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Robust.Shared.Toolshed;

public sealed partial class ToolshedManager
{
private readonly Dictionary<Type, ITypeParser> _consoleTypeParsers = new();
private readonly Dictionary<Type, ITypeParser?> _consoleTypeParsers = new();
private readonly Dictionary<Type, Type> _genericTypeParsers = new();
private readonly List<(Type, Type)> _constrainedParsers = new();

Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Toolshed/TypeParsers/StringTypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
23 changes: 8 additions & 15 deletions Robust.Shared/Toolshed/TypeParsers/TypeTypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +55,7 @@ internal sealed class TypeTypeParser : TypeParser<Type>
};

private readonly HashSet<string> _ambiguousTypes = new();
private CompletionResult? _optionsCache;

public override void PostInject()
{
Expand All @@ -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)
Expand Down Expand Up @@ -168,18 +170,15 @@ 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));
}
}

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; }
Expand All @@ -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; }
Expand All @@ -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; }
Expand All @@ -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; }
Expand Down

0 comments on commit be9db26

Please sign in to comment.