mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-15 14:52:35 +02:00
* Add parsing for arrays of values via ValueArray<T>. * oops forgot command description * add valarr to engine toolshed perms, fix stupidness, remove useless code in parser * make optional not suck * support for skipping optional parameters * Switch to List<T> because I'm dumb and that just works™ * remove optional stuff to separate into its own PR * Remove ValArr command `valarr int [ 1, 2 ]` can just be replaced with ` val List<int> [ 1, 2 ]` or ` val int[] [ 1, 2 ]` * Fix namespace * Add support for array parsing * Make ListTypeParser output nullable * Pass ListLengthAttribute in the CommandArgument struct The current method of trying to fetch the attribute for argument doesn't actually work. i.e., the argIndex calculation isn't always correct. This is still kinda janky and I hate it, but it should work better. * Fix empty list parsing, add tests * rename flag, and clarify description * More tests --------- Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Robust.Shared.Console;
|
|
|
|
/// <summary>
|
|
/// Contains the result of a command completion.
|
|
/// </summary>
|
|
public sealed record CompletionResult(CompletionOption[] Options, string? Hint)
|
|
{
|
|
/// <summary>
|
|
/// The possible full arguments to complete with. These are from the start of the entire argument.
|
|
/// </summary>
|
|
public CompletionOption[] Options { get; init; } = Options;
|
|
|
|
/// <summary>
|
|
/// Type hint string for the current argument being typed.
|
|
/// </summary>
|
|
public string? Hint { get; set; } = Hint;
|
|
|
|
public static readonly CompletionResult Empty = new(Array.Empty<CompletionOption>(), null);
|
|
|
|
public static CompletionResult FromHintOptions(IEnumerable<string> options, string? hint) => new(ConvertOptions(options), hint);
|
|
public static CompletionResult FromHintOptions(IEnumerable<CompletionOption> options, string? hint) => new(options.ToArray(), hint);
|
|
|
|
public static CompletionResult FromOptions(IEnumerable<string> options) => new(ConvertOptions(options), null);
|
|
public static CompletionResult FromOptions(IEnumerable<CompletionOption> options) => new(options.ToArray(), null);
|
|
|
|
public static CompletionResult FromHint(string hint) => new(Array.Empty<CompletionOption>(), hint);
|
|
|
|
private static CompletionOption[] ConvertOptions(IEnumerable<string> stringOpts)
|
|
{
|
|
return stringOpts.Select(c => new CompletionOption(c)).ToArray();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Possible option to tab-complete in a <see cref="CompletionResult"/>.
|
|
/// </summary>
|
|
public record struct CompletionOption(string Value, string? Hint = null, CompletionOptionFlags Flags = default) : IComparable<CompletionOption>
|
|
{
|
|
/// <summary>
|
|
/// The value that will be filled in if completed.
|
|
/// </summary>
|
|
public string Value { get; set; } = Value;
|
|
|
|
/// <summary>
|
|
/// Additional hint value that is shown to users, but not included in the completed value.
|
|
/// </summary>
|
|
public string? Hint { get; set; } = Hint;
|
|
|
|
/// <summary>
|
|
/// Flags that control how this completion is used.
|
|
/// </summary>
|
|
public CompletionOptionFlags Flags { get; set; } = Flags;
|
|
|
|
public int CompareTo(CompletionOption other)
|
|
{
|
|
var valueComparison = string.Compare(Value, other.Value, StringComparison.CurrentCultureIgnoreCase);
|
|
return valueComparison;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Flag options for <see cref="CompletionOption"/>.
|
|
/// </summary>
|
|
[Flags]
|
|
public enum CompletionOptionFlags
|
|
{
|
|
/// <summary>
|
|
/// The completion is "partial", it does complete the whole argument.
|
|
/// Therefore, tab completing it should keep the cursor on the current argument
|
|
/// (instead of adding a space to go to the next one).
|
|
/// </summary>
|
|
PartialCompletion = 1 << 0,
|
|
|
|
/// <summary>
|
|
/// Prevents suggestions containing spaces from being automatically wrapped in quotes.
|
|
/// </summary>
|
|
NoQuote = 1 << 1,
|
|
|
|
/// <summary>
|
|
/// Prevents suggestions from being escaped using <see cref="CommandParsing.Escape"/>.
|
|
/// </summary>
|
|
NoEscape = 1 << 2,
|
|
|
|
/// <summary>
|
|
/// Prevents suggestions from being filtered based on what the client has "currently typed"
|
|
/// so that your completion shows up anyway based on the defined rules in your parser.
|
|
/// </summary>
|
|
NoFilter = 1 << 3,
|
|
|
|
/// <summary>
|
|
/// Instead of replacing the entire argument, the suggestion will
|
|
/// be appended to what already exists.
|
|
/// </summary>
|
|
AppendOnly = 1 << 4,
|
|
}
|