Tab Completion
JLine provides a powerful tab completion system that allows you to add intelligent suggestions to your command-line applications. This page explains how to configure and use the tab completion feature.
Basic Completers
JLine provides several built-in completers for common use cases:
StringsCompleter
The StringsCompleter completes from a fixed or dynamic set of strings:
Loading snippet: StringsCompleterExample...
FileNameCompleter
The FileNameCompleter completes file and directory names:
Loading snippet: FileNameCompleterExample...
TreeCompleter
The TreeCompleter allows you to define a hierarchical structure of completions:
Loading snippet: TreeCompleterExample...
AggregateCompleter
The AggregateCompleter combines multiple completers:
Loading snippet: AggregateCompleterExample...
Custom Completers
You can create your own completers by implementing the Completer interface:
Loading snippet: CustomCompleter...
Completion Behavior
You can configure how completion behaves:
Loading snippet: CompletionBehaviorExample...
Candidates with Descriptions
You can provide descriptions for completion candidates:
Loading snippet: CandidatesWithDescriptionsExample...
Context-Aware Completion
You can create completers that are aware of the current context:
Loading snippet: ContextAwareCompleter...
Colored Completions
You can add colors to your completions:
Loading snippet: ColoredCompleter...
Additional Completers
JLine provides several additional completers for specific use cases:
NullCompleter
The NullCompleter is used to terminate completion:
Loading snippet: NullCompleterExample...
DirectoriesCompleter
The DirectoriesCompleter completes only directory names:
Loading snippet: DirectoriesCompleterExample...
FilesCompleter
The FilesCompleter completes file names with filtering:
Loading snippet: FilesCompleterExample...
RegexCompleter
The RegexCompleter uses regular expressions to define completion patterns:
Loading snippet: RegexCompleterExample...
EnumCompleter
The EnumCompleter completes enum values:
Loading snippet: EnumCompleterExample...
Builtins Completers
The JLine Builtins module provides additional completers:
SystemCompleter
The SystemCompleter manages completers for different commands:
Loading snippet: SystemCompleterExample...
Builtins TreeCompleter
The Builtins module provides its own TreeCompleter:
Loading snippet: BuiltinsTreeCompleterExample...
Best Practices
When implementing tab completion in JLine, consider these best practices:
-
Combine Completers: Use AggregateCompleter and ArgumentCompleter to create complex completion behavior.
-
Context-Aware Completion: Make your completers aware of the current context for more intelligent suggestions.
-
Descriptions: Provide descriptions for completion candidates to help users understand their options.
-
Colored Completions: Use colors to distinguish between different types of completions.
-
Dynamic Completions: Update completion candidates based on the current state of your application.
-
Filtering: Filter completion candidates based on what the user has typed so far.
-
Performance: Be mindful of performance, especially for large sets of completion candidates.
-
Testing: Test your completers thoroughly with different inputs.
-
Documentation: Document the available completions for your users.
-
Fallbacks: Provide sensible fallbacks when specific completions are not available.