Class TypingHints

java.lang.Object
com.j3d.engine.interact.cmd.complete.TypingHints

public class TypingHints extends Object
Provides real-time typing hints, suggestions, and visual feedback for the command palette input. It analyses user input (tokens) to suggest command names, validate argument types, and highlight potential errors or matches. This includes tab completion for command names, argument type checking (e.g., Vector3, Colour, UUID, numbers, booleans, arg sets), and styling of usage strings to indicate correctness or partial matches.
Author:
Lehlogonolo Poole
See Also:
  • Field Details

    • CMDNAME_LIEKLY_MATCH

      public static Color CMDNAME_LIEKLY_MATCH
      Colour used for command names that are considered "likely matches" (e.g., start with the input).
    • CMDNAME_PARTIAL_MATCH

      public static Color CMDNAME_PARTIAL_MATCH
      Colour used for command names that are considered "partial matches" (e.g., contain the input but don't start with it).
    • EXACT_MATCH

      public static Color EXACT_MATCH
      Colour used for arguments that are an exact match in type and value.
    • PARTIAL_MATCH

      public static Color PARTIAL_MATCH
      Colour used for arguments that are a partial match or unfinished but potentially correct.
    • INCORRECT_TYPE

      public static Color INCORRECT_TYPE
      Colour used for arguments that are of an incorrect type or value.
    • MAX_CMDNAME_SUGGESTIONS

      int MAX_CMDNAME_SUGGESTIONS
      The maximum number of command name suggestions to display for both likely and partial matches.
    • taggedArgErr

      private boolean taggedArgErr
      A flag indicating whether an error related to tagged arguments has occurred. This is used to prevent other hints from overriding the tagged argument error message.
    • options

      ArrayList<String> options
  • Constructor Details

    • TypingHints

      public TypingHints()
  • Method Details

    • parse

      public void parse(ArrayList<CmdToken> init, boolean endsWithSpace)
      Parses the given command tokens and provides typing hints or suggestions based on the current input. This method updates the CommandParser.safeJLabel() with relevant information, such as command matches, usage hints, or error messages.
      Parameters:
      init - The initial list of CmdToken objects, including any tagged arguments, representing the user's input.
      endsWithSpace - A boolean indicating whether the user's input currently ends with a space.
    • findUsages

      public ArrayList<String> findUsages(String alias, ArrayList<String> usages, ArrayList<CmdToken> tokens)
      Filters a list of command usages based on the provided command tokens. It iteratively narrows down the possible usages by checking if each token's type is compatible with the corresponding argument in the usage string.
      Parameters:
      alias - The alias of the command being used.
      usages - An array of all possible usage strings for the command.
      tokens - An ArrayList of CmdToken objects representing the user's input arguments.
      Returns:
      An ArrayList of String containing the usage strings that are compatible with the given tokens.
    • similarTypes

      public boolean similarTypes(CmdToken token, String usage)
      Determines if a given command token's type is compatible with a specified usage string. This method checks for various type matches, including generic ("any"), string, typed arguments, numbers, and specific object references (UUIDs).
      Parameters:
      token - The CmdToken representing the user's input argument.
      usage - The expected usage string for an argument, e.g., <string>, <int>, [option1|option2], or <vector3>.
      Returns:
      true if the token's type is similar or compatible with the usage string, false otherwise.
    • possibleCommandAliasMatches

      private SamePair<ArrayList<InlineHTML>> possibleCommandAliasMatches(CmdToken token)
      Finds and styles possible command alias matches based on the given command token. It categorises matches into "likely" (aliases starting with the input) and "partial" (aliases containing the input but not starting with it), and styles them accordingly.
      Parameters:
      token - The CmdToken representing the user's current input for a command name.
      Returns:
      A SamePair containing two ArrayLists of InlineHTML. The first list contains likely matches, and the second contains partial matches.
    • onTabComplete

      Provides a QuadConsumer that handles tab completion logic for the command palette. This consumer attempts to complete the current command name or argument based on available aliases and suggestions. If no specific completion is found, it delegates to the default Action.
      Returns:
      A QuadConsumer that takes:
    • colourTaggedArgs

      private InlineHTML colourTaggedArgs(InlineHTML rich, Command command, ArrayList<CmdToken> init)
      Appends a hint for tagged arguments to the given InlineHTML based on the command's support for tagged arguments and whether any tagged arguments are present in the input.
      Parameters:
      rich - The current InlineHTML to append to.
      command - The Command for which the hints are being generated.
      init - The initial list of CmdTokens, including any tagged arguments.
      Returns:
      The modified InlineHTML with the tagged argument hint appended and styled.
    • colourGivenUsage

      public InlineHTML colourGivenUsage(String usage, ArrayList<CmdToken> tokens)
      Styles a given command usage string based on the provided user input tokens. This method provides visual feedback (colors) to indicate whether each argument in the usage string is correctly typed, partially matched, or incorrectly typed by the user.
      Parameters:
      usage - The expected command usage string, e.g., "mycommand [option1|option2] ".
      tokens - An ArrayList of CmdToken representing the user's parsed input.
      Returns:
      A InlineHTML object containing the styled usage string.
    • argSetMatch

      private InlineHTML argSetMatch(String arg, CmdToken token)
      Type checks a given String value as to be at least partially or fully matched within the given argument set.
      Parameters:
      arg - The expected arg string, which is [value1|value2|value3] and defines the accepted values.
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument. This is either, the single fully matched value, a list of partially matched values or otherwise incorrect.
    • idReferenceMatch

      private InlineHTML idReferenceMatch(String arg, CmdToken token)
      Type checks a given value as to expect a UUID which has to reference a GObject or Thing
      Parameters:
      arg - The expected arg string, which is <point>, <line>, <tri>, <thing>, <point?>, <line?>, <tri?> or <thing?>
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument.
    • numberMatch

      private InlineHTML numberMatch(String arg, CmdToken token)
      Type checks a given value as to expect an Integer or Double
      Parameters:
      arg - The expected arg string, which is <int>, <int?>, <number> or <number?>
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument
    • colourMatch

      private InlineHTML colourMatch(String arg, CmdToken token)
      Type checks a given value as to expect a Color
      Parameters:
      arg - The expected arg string, which is <color> or <color?>
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument. This goes the extra mile by colouring the background of the rich text to be the given input colour as to tell the user the input they gave.
    • boolMatch

      private InlineHTML boolMatch(String arg, CmdToken token)
      Type checks a given value as to expect a Boolean
      Parameters:
      arg - The expected arg string, which is <boolean> or <boolean?>
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument. This is either a fully matched boolean, or a partially matched boolean. Otherwise it is incorrect.
    • vector3

      private InlineHTML vector3(String arg, CmdToken token)
      Type checks a given value as to expect a Vector3
      Parameters:
      arg - The expected arg string, which is <vector3> or <vector3?>
      token - The token to check against
      Returns:
      A styled InlineHTML which colours the given expected argument
    • correctType

      private InlineHTML correctType(String arg)
      Colours the given type as fully correct.
      Parameters:
      arg - The expected arg string, like <vector3> or [p|r|e]
      Returns:
      A styled InlineHTML which colours the given expected argument
    • partialType

      private InlineHTML partialType(String arg)
      Colours the given type as partially correct. Partial matches are usually those who aren't a fully enclosed typed and get parsed as a string. These include:
      • Color #...
      • Vector3 (...
      • UUID <uuid like string>
      • invalid input: '{@link /*missing*/}'
      Parameters:
      arg - The expected arg string, like <vector3> or [p|r|e]
      Returns:
      A styled InlineHTML which colours the given expected argument PARTIAL_MATCH
    • incorrectType

      private InlineHTML incorrectType(String arg)
      Colours the given type as incorrect
      Parameters:
      arg - The expected arg string, like <vector3> or [p|r|e]
      Returns:
      A styled InlineHTML which colours the given expected argument INCORRECT_TYPE
    • notGivenYet

      private InlineHTML notGivenYet(String arg)
      Styles a given input as generic (The user has not given this input yet.)
      Parameters:
      arg - The input to style
      Returns:
      A InlineHTML object containing the styled input
    • partialStringMatch

      private InlineHTML partialStringMatch(ArrayList<String> options, String input)
      Parameters:
      options - The list of options
      input - The input the user gave
      Returns:
      A styled InlineHTML consisting of a partially coloured match.
    • taggedArgErr

      public void taggedArgErr(boolean value)
      Sets the taggedArgErr flag.
      Parameters:
      value - The value to set it to.
    • getOptions

      public ArrayList<String> getOptions()
    • setOptions

      private void setOptions(ArrayList<String> options)