Class CommandParser
It supports various argument types including strings, vectors, colours, UUIDs, numbers,
and booleans.
It also integrates with CommandsManager for command lookup and execution,
and RunHistory for command history management.
- Author:
- Lehlogonolo Poole
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionCollected argument objects for the current command invocation.final CommandPaletteThe UI command palette that supplies input and receives feedback from this parser.final KeyBindingsKey bindings for the command palette's input field.private booleanWhen true, document events coming from the input field are ignored.private final SafeJLabelHelper label wrapper used to show parsing/execution messages to the user.final RunHistoryManages the history of executed commands.private final ArrayList<TaggedArgValue<?>> Collected tagged argument values for the current command invocation.A list ofCmdTokenobjects representing the parsed input line.private final TypingHintsProvides typing hints and tab completion suggestions. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddArg(CmdToken cmdToken, Object arg, CmdToken.Type t, boolean injected) Adds an argument to the internal list of arguments and updates the command palette's input field if the argument was "injected" (i.e., programmatically added rather than typed by the user).private voidargAddUUID(CmdToken tok, UUID uuid, boolean injected) Attempts to finder a GObject or Thing with the given UUID and adds it to the arguments list.booleanChecks if the caret in the command palette's input field is at the end of the text.static StringConvert aColorinto the textual representation used by the command palette (format: #R:G:B:A#).voiddisable()Disable the command input field and restore the default background styling.voidenable()Enable the command input field and apply the 'active' background styling.Returns theKeyBindingsinstance used for the command palette.Returns theTypingHintsinstance associated with this parser.private voidAppends the given string to the command palette's input field, followed by a space.voidinjectArgument(Object obj) Injects an object as an argument into the command palette's input field.booleanisEmpty()Checks if the command parser currently has no tokens.private voidParses a single command token, attempting to convert its string representation into a specific object type (String, Vector3, Colour, UUID-referenced object, number, boolean, or tagged argument).private voidparseAsNumberOrBool(CmdToken token, String accumulator, Consumer<String> otherwise) Attempt to parse the given token as an integer or double and add the resulting number toarguments.private ColorparseColor(String input) Parse a colour specification string into aColor.private voidRegisters the following keybinds:KeyEvent.VK_ENTERto run the commandKeyEvent.VK_RIGHTto tab complete the commandKeyEvent.VK_ESCAPEto defocus the command paletteKeyEvent.VK_UPto navigate up the command historyKeyEvent.VK_DOWNto navigate down the command historyprivate voidReparses the current line in the command palette's input field.private voidrun()Execute the parsed command using the collectedarguments.booleanCalls a specific command.booleanrun(Command cmd, ArrayList<Object> arguments, ArrayList<TaggedArgValue<?>> taggedArguments, Command caller) Calls a specific command wit an extra note labelling that it was called by some other command's invocation.private booleanrunCommand(Command cmd, String cmdName, ArrayList<Object> arguments, ArrayList<TaggedArgValue<?>> taggedArguments, Invoker invoker) booleanReturns theSafeJLabelinstance used by this parser to display messages.voidsetInputField(String input) Sets the text of the command palette's input field and triggers a re-parsing of the line.
-
Field Details
-
arguments
-
taggedArguments
Collected tagged argument values for the current command invocation. -
ignoreDocumentEvent
private boolean ignoreDocumentEventWhen true, document events coming from the input field are ignored. This is used to prevent re-entrant parsing when the parser programmatically updates the field. -
commandPalette
The UI command palette that supplies input and receives feedback from this parser. -
label
Helper label wrapper used to show parsing/execution messages to the user. -
commandPaletteBinds
Key bindings for the command palette's input field. -
runHistory
Manages the history of executed commands. -
typingHints
Provides typing hints and tab completion suggestions. -
tokens
A list ofCmdTokenobjects representing the parsed input line.
-
-
Constructor Details
-
CommandParser
Create a newCommandParserbound to the givenCommandPalette.It registers key bindings for command execution, tab completion, and history navigation. It also sets up a
DocumentListenerto reparse the input line as the user types.- Parameters:
p- theCommandPaletteinstance this parser should use for input and output
-
-
Method Details
-
enable
public void enable()Enable the command input field and apply the 'active' background styling.This makes the input field editable and darkens its background to indicate focus/availability.
-
disable
public void disable()Disable the command input field and restore the default background styling.This prevents user input and brightens the field background to indicate it is inactive.
-
setInputField
Sets the text of the command palette's input field and triggers a re-parsing of the line. This method temporarily ignores document events to prevent re-entrant parsing.- Parameters:
input- The string to set as the input field's text.
-
reParseLine
private void reParseLine()Reparses the current line in the command palette's input field. This method clears existing tokens and arguments, then re-tokenises and reparses the input. -
injectArgument
Injects an object as an argument into the command palette's input field. This method converts the object into its command palette string representation and appends it to the input. -
inject
Appends the given string to the command palette's input field, followed by a space. This method is used internally to update the input field when arguments are programmatically injected.- Parameters:
t- The string to inject into the input field.
-
colourToCommandPaletteString
Convert aColorinto the textual representation used by the command palette (format: #R:G:B:A#).- Parameters:
col- the colour to convert- Returns:
- a string in the palette colour format, including surrounding '#'
-
argAddUUID
Attempts to finder a GObject or Thing with the given UUID and adds it to the arguments list.This will be used by the command parser to convert UUID strings into actual objects when parsing command arguments.
- Parameters:
uuid- The UUID of the GObject or Thing to finder.
-
addArg
Adds an argument to the internal list of arguments and updates the command palette's input field if the argument was "injected" (i.e., programmatically added rather than typed by the user).- Parameters:
cmdToken- TheCmdTokenrepresenting the argument.arg- The parsed argument object.t- TheCmdToken.Typeof the argument.injected- A boolean indicating whether the argument was injected programmatically. If true, the argument's string representation will be appended to the input field.
-
parse
Parses a single command token, attempting to convert its string representation into a specific object type (String, Vector3, Colour, UUID-referenced object, number, boolean, or tagged argument). The parsed object is then added to theargumentsortaggedArgumentslist.- Parameters:
token- TheCmdTokento parse.
-
parseAsNumberOrBool
Attempt to parse the given token as an integer or double and add the resulting number toarguments. If parsing as a number fails, the providedotherwiseconsumer is invoked with the original token.- Parameters:
token- the token to parseaccumulator- the token to parseotherwise- a fallback consumer called when the token is not numeric
-
isEmpty
public boolean isEmpty()Checks if the command parser currently has no tokens.- Returns:
trueif the tokens list is empty,falseotherwise.
-
run
private void run()Execute the parsed command using the collectedarguments.The first argument is expected to be the command name (a
String). The parser will resolve the command viaCommandsManager.getCommand(String), check for any currently running stateful command, and if applicable mark the resolved command as the current stateful command before invoking itsrunmethod. -
run
public boolean run(Command cmd, ArrayList<Object> arguments, ArrayList<TaggedArgValue<?>> taggedArguments) Calls a specific command. This is the default run method for anything which is labelled as invoked by the engine- Parameters:
cmd- The command to runarguments- The arguments to pass to the commandtaggedArguments- The tagged arguments to pass to the command- Returns:
trueif the command was run successfully,falseotherwise
-
run
public boolean run(Command cmd, ArrayList<Object> arguments, ArrayList<TaggedArgValue<?>> taggedArguments, Command caller) Calls a specific command wit an extra note labelling that it was called by some other command's invocation. This is different from a command calling it's own subcommand as that usesInvoker.byParentCommand(Command)whereas this usesInvoker.byCommandCall(Command)- Parameters:
cmd- The command to runarguments- The arguments to pass to the commandtaggedArguments- The tagged arguments to pass to the commandcaller- The command that is invoking this command.- Returns:
trueif the command was run successfully,falseotherwise
-
runMacro
public boolean runMacro(Command cmd, ArrayList<Object> arguments, ArrayList<TaggedArgValue<?>> taggedArguments) -
runCommand
-
parseColor
Parse a colour specification string into aColor.Supported formats:
- R:G:B:A (four integers separated by colons)
- R:G:B (three integers separated by colons)
- Hex string parseable by
Color.decode(String)
- Parameters:
input- the colour string (without surrounding hashes)- Returns:
- a
Colorinstance if parsing succeeds, ornullon failure
-
safeJLabel
Returns theSafeJLabelinstance used by this parser to display messages.- Returns:
- the
SafeJLabelinstance
-
getTypingHintSession
Returns theTypingHintsinstance associated with this parser.- Returns:
- the
TypingHintsinstance
-
caretAtEnd
public boolean caretAtEnd()Checks if the caret in the command palette's input field is at the end of the text.- Returns:
trueif the caret is at the end,falseotherwise
-
getCommandPaletteKeyBinds
Returns theKeyBindingsinstance used for the command palette.- Returns:
- the
KeyBindingsinstance
-
getTokens
-
getTaggedArguments
-
registerKeys
private void registerKeys()Registers the following keybinds:KeyEvent.VK_ENTERto run the commandKeyEvent.VK_RIGHTto tab complete the commandKeyEvent.VK_ESCAPEto defocus the command paletteKeyEvent.VK_UPto navigate up the command historyKeyEvent.VK_DOWNto navigate down the command history
-