Class CompletionMatcherImpl

java.lang.Object
org.jline.reader.impl.CompletionMatcherImpl
All Implemented Interfaces:
CompletionMatcher

public class CompletionMatcherImpl extends Object implements CompletionMatcher
Default implementation of the CompletionMatcher interface.

This matcher provides sophisticated algorithms for matching completion candidates against user input, with support for:

  • Prefix matching - candidates that start with the input text
  • Substring matching - candidates that contain the input text
  • Camel case matching - matching based on camel case patterns
  • Fuzzy matching - candidates that approximately match with allowed typos

The matcher uses a chain of matching strategies, trying each one in sequence until matches are found. This allows for a graceful fallback from exact matches to more approximate matches.

The behavior of the matcher can be controlled through LineReader options such as LineReader.Option.COMPLETE_MATCHER_TYPO and LineReader.Option.COMPLETE_MATCHER_CAMELCASE.

See Also:
  • Field Details

  • Constructor Details

    • CompletionMatcherImpl

      public CompletionMatcherImpl()
  • Method Details

    • reset

      protected void reset(boolean caseInsensitive)
    • compile

      public void compile(Map<LineReader.Option,Boolean> options, boolean prefix, CompletingParsedLine line, boolean caseInsensitive, int errors, String originalGroupName)
      Description copied from interface: CompletionMatcher
      Initializes the matcher with the current completion context.

      This method is called before any matching operations to set up the matcher with the current completion context, including the line being completed, reader options, and other parameters that affect how matching should be performed.

      The matcher uses this information to compile its internal matching functions that will be used to filter candidates.

      Specified by:
      compile in interface CompletionMatcher
      Parameters:
      options - LineReader options that may affect matching behavior
      prefix - true if invoked by complete-prefix or expand-or-complete-prefix widget
      line - the parsed line within which completion has been requested
      caseInsensitive - true if completion should be case insensitive
      errors - number of typo errors accepted in matching (for fuzzy matching)
      originalGroupName - value of the LineReader variable original-group-name
    • matches

      public List<Candidate> matches(List<Candidate> candidates)
      Description copied from interface: CompletionMatcher
      Filters the provided candidates based on the current matching criteria.

      This method applies the matching algorithm to the list of candidates and returns only those that match the current input according to the configured matching rules. The returned list may be sorted based on match quality.

      Specified by:
      matches in interface CompletionMatcher
      Parameters:
      candidates - the list of candidates to filter
      Returns:
      a list of candidates that match the current input
    • exactMatch

      public Candidate exactMatch()
      Description copied from interface: CompletionMatcher
      Returns a candidate that exactly matches the current input, if any.

      An exact match typically means the candidate's value is identical to what the user has typed, possibly ignoring case depending on the matcher configuration. This is used to determine if the completion should be accepted immediately without showing a list of options.

      Specified by:
      exactMatch in interface CompletionMatcher
      Returns:
      a candidate that exactly matches the current input, or null if no exact match is found
    • getCommonPrefix

      public String getCommonPrefix()
      Description copied from interface: CompletionMatcher
      Returns the longest common prefix shared by all matched candidates.

      This is used to implement tab completion behavior where pressing tab will automatically complete as much of the input as can be unambiguously determined from the available candidates.

      Specified by:
      getCommonPrefix in interface CompletionMatcher
      Returns:
      the longest common prefix of all matched candidates, or an empty string if none
    • defaultMatchers

      protected void defaultMatchers(Map<LineReader.Option,Boolean> options, boolean prefix, CompletingParsedLine line, boolean caseInsensitive, int errors, String originalGroupName)
      Default JLine matchers
    • simpleMatcher

      protected Function<Map<String,List<Candidate>>,Map<String,List<Candidate>>> simpleMatcher(Predicate<String> predicate)
    • typoMatcher

      protected Function<Map<String,List<Candidate>>,Map<String,List<Candidate>>> typoMatcher(String word, int errors, boolean caseInsensitive, String originalGroupName)
    • camelMatch

      protected boolean camelMatch(String word, int i, String candidate, int j)