CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-groovysh

Interactive command-line shell (REPL) for evaluating Groovy expressions with advanced editing, completion, and command features

Pending
Overview
Eval results
Files

completion-system.mddocs/

Completion System

The Groovy Shell provides an advanced tab completion system with 20+ specialized completers supporting Groovy syntax, shell commands, file paths, and contextual completions. The system integrates with JLine for terminal-based completion.

Capabilities

Core Completion Interface

All completion classes implement the JLine Completer interface for consistent tab completion behavior.

/**
 * JLine completer interface for tab completion
 */
interface Completer {
    /**
     * Perform completion for buffer at cursor position
     * @param buffer - Input buffer string
     * @param cursor - Cursor position in buffer
     * @param candidates - List to populate with completion candidates
     * @return Position where completion should start
     */
    int complete(String buffer, int cursor, List<CharSequence> candidates)
}

Primary Groovy Syntax Completer

Main completion engine for Groovy syntax, handling identifiers, method calls, and property access.

/**
 * Main completion engine for Groovy syntax, handling identifiers, method calls, and property access
 */
class GroovySyntaxCompleter implements Completer {
    /**
     * Different completion scenarios based on context
     */
    enum CompletionCase {
        /** Completion after dot (method/property access) */
        DOT_LAST,
        
        /** Completion with no dot and no prefix */
        NO_DOT_PREFIX,
        
        /** Completion with no dot but some prefix text */
        NO_DOT_SOME_PREFIX,
        
        /** Other completion scenarios */
        OTHER
    }
    
    /**
     * Perform completion for Groovy syntax
     * @param bufferLine - Current input line
     * @param cursor - Cursor position
     * @param candidates - List to populate with candidates
     * @return Start position for completion
     */
    int complete(String bufferLine, int cursor, List<CharSequence> candidates)
    
    /**
     * Determine completion case from tokens
     * @param tokens - Parsed source tokens
     * @return Completion case type
     */
    static CompletionCase getCompletionCase(List<GroovySourceToken> tokens)
    
    /**
     * Check if buffer line is a shell command
     * @param bufferLine - Input line to check
     * @param registry - Command registry
     * @return true if line starts with shell command
     */
    static boolean isCommand(String bufferLine, CommandRegistry registry)
}

Reflection-based Completion

Completes methods and properties using runtime reflection.

/**
 * Complete methods and properties via runtime reflection
 */
class ReflectionCompleter implements Completer {
    /**
     * Complete using reflection on target object
     * @param buffer - Input buffer
     * @param cursor - Cursor position
     * @param candidates - Completion candidates
     * @return Start position
     */
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    /**
     * Create reflection completer
     * @param shell - Shell instance for context
     */
    ReflectionCompleter(Groovysh shell)
}

/**
 * Completion candidate with metadata for reflection-based completion
 */
class ReflectionCompletionCandidate {
    /** Candidate text */
    final String value
    
    /** Display text (may include type information) */
    final String displayText
    
    /** Whether candidate represents a method */
    final boolean isMethod
    
    /** Method parameter information */
    final String params
    
    ReflectionCompletionCandidate(String value, String displayText, boolean isMethod, String params)
}

Syntax-Specific Completers

Specialized completers for different aspects of Groovy syntax.

/**
 * Complete Groovy keywords
 */
class KeywordSyntaxCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
}

/**
 * Complete variable names from shell binding
 */
class VariableSyntaxCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    VariableSyntaxCompleter(Groovysh shell)
}

/**
 * Complete identifier names (classes, methods, variables)
 */
class IdentifierCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    IdentifierCompleter(Groovysh shell)
}

/**
 * Complete import statements
 */
class ImportsSyntaxCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    ImportsSyntaxCompleter(Groovysh shell)
}

/**
 * Complete custom class names from classpath
 */
class CustomClassSyntaxCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    CustomClassSyntaxCompleter(Groovysh shell)
}

/**
 * Complete infix operators and keywords
 */
class InfixKeywordSyntaxCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
}

Utility Completers

General-purpose completers for common completion scenarios.

/**
 * Complete shell command names
 */
class CommandNameCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    CommandNameCompleter(CommandRegistry registry)
}

/**
 * Complete file and directory names
 */
class FileNameCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
}

/**
 * Complete navigable properties with dot notation
 */
class NavigablePropertiesCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    NavigablePropertiesCompleter(Groovysh shell)
}

/**
 * Complete backslash escape sequences
 */
class BackslashEscapeCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
}

/**
 * Enhanced argument completer with stricter matching
 */
class StricterArgumentCompleter extends ArgumentCompleter {
    StricterArgumentCompleter(Completer... completers)
    StricterArgumentCompleter(List<Completer> completers)
}

/**
 * Simple completer for fixed candidate lists
 */
class SimpleCompleter implements Completer {
    int complete(String buffer, int cursor, List<CharSequence> candidates)
    
    /**
     * Create completer with fixed candidates
     * @param candidates - Fixed list of completion candidates
     */
    SimpleCompleter(String... candidates)
}

ANTLR4 Completion Support

The org.apache.groovy.groovysh.completion.antlr4 package provides ANTLR4-optimized implementations of all completion classes with the same API but enhanced for the ANTLR4 parser.

// ANTLR4 versions of all completers are available in the antlr4 package:
// org.apache.groovy.groovysh.completion.antlr4.GroovySyntaxCompleter
// org.apache.groovy.groovysh.completion.antlr4.ReflectionCompleter  
// org.apache.groovy.groovysh.completion.antlr4.KeywordSyntaxCompleter
// ... etc for all completer classes

Completion Usage Examples

Basic Completion Setup:

import org.apache.groovy.groovysh.completion.*
import jline.console.completer.AggregateCompleter

// Create individual completers
def keywordCompleter = new KeywordSyntaxCompleter()
def variableCompleter = new VariableSyntaxCompleter(shell)
def reflectionCompleter = new ReflectionCompleter(shell)
def commandCompleter = new CommandNameCompleter(shell.registry)

// Combine completers
def aggregateCompleter = new AggregateCompleter([
    keywordCompleter,
    variableCompleter,  
    reflectionCompleter,
    commandCompleter
])

// Use with console reader
def reader = shell.runner.reader
reader.addCompleter(aggregateCompleter)

Custom Completer Implementation:

import org.apache.groovy.groovysh.completion.SimpleCompleter

// Create custom completer with fixed options
class DatabaseCompleter extends SimpleCompleter {
    DatabaseCompleter() {
        super('SELECT', 'INSERT', 'UPDATE', 'DELETE', 
              'CREATE', 'DROP', 'ALTER', 'FROM', 'WHERE')
    }
}

// Register with shell
shell.runner.completer.add(new DatabaseCompleter())

Context-Aware Completion:

import org.apache.groovy.groovysh.completion.GroovySyntaxCompleter

// Example showing completion context detection
def completer = new GroovySyntaxCompleter(shell)
def buffer = "myObject."
def cursor = buffer.length()
def candidates = []

// Get completions after dot - will show methods/properties
int startPos = completer.complete(buffer, cursor, candidates)
println "Completion candidates: ${candidates}"
println "Start position: ${startPos}"

// Example with keywords
buffer = "def "
cursor = buffer.length()
candidates.clear()
startPos = completer.complete(buffer, cursor, candidates)
// Will suggest variable names, method names, etc.

Completion for Shell Commands:

// Command completion automatically works for registered commands
shell.execute(':he<TAB>')     // Completes to ':help'
shell.execute(':l<TAB>')      // Shows ':load', ':list', etc.
shell.execute(':show <TAB>')  // Shows 'variables', 'classes', etc.

File Path Completion:

import org.apache.groovy.groovysh.completion.FileNameCompleter

def fileCompleter = new FileNameCompleter()
def buffer = '/usr/loc'
def cursor = buffer.length()
def candidates = []

fileCompleter.complete(buffer, cursor, candidates)
// Candidates might include: '/usr/local/', '/usr/local/bin/', etc.

Reflection-based Method Completion:

// Type an object variable followed by dot to get method completions
shell.execute('def list = [1,2,3]')
// Now type: list.<TAB>
// Shows: add, remove, size, get, iterator, etc.

shell.execute('def str = "hello"')  
// Now type: str.<TAB>
// Shows: length, substring, toLowerCase, toUpperCase, etc.

Import Statement Completion:

// Complete import statements
shell.execute('import java.u<TAB>')
// Shows: java.util, java.util.concurrent, etc.

shell.execute('import java.util.concurrent.<TAB>')
// Shows: Future, ExecutorService, ThreadPoolExecutor, etc.

Completion Configuration

Shell Integration:

// Completion system is automatically integrated with shell
// Access through runner
def runner = shell.runner
def multiCompleter = runner.completer

// Add custom completer
multiCompleter.add(myCustomCommand)
multiCompleter.refresh()

// Configure completion behavior
shell.execute(':set meta-completion-prefix-length 3')  // Minimum prefix length

Performance Tuning:

// Completion can be tuned through preferences
def prefs = shell.preferences

// Set minimum prefix length for metaclass completion
prefs.put(Groovysh.METACLASS_COMPLETION_PREFIX_LENGTH_PREFERENCE_KEY, '2')

// Disable expensive completions if needed
// (This would require custom completer implementation)

The completion system provides comprehensive tab completion for all aspects of Groovy development within the shell, from basic syntax and keywords to complex reflection-based method and property completion. The modular design allows for easy extension with custom completers for domain-specific completions.

Install with Tessl CLI

npx tessl i tessl/maven-org-codehaus-groovy--groovy-groovysh

docs

command-system.md

completion-system.md

index.md

shell-features.md

utilities.md

tile.json