summaryrefslogtreecommitdiff
path: root/internal/repl/completer.go
blob: d66a5efc21a676c4a80b2954ca499475f5eba5fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

import (
	"strings"

	"github.com/c-bata/go-prompt"
)

// completer provides auto-completion for built-in commands.
// It returns suggestions for commands that match the current word being typed.
// The matching is case-insensitive and includes descriptions for each command.
//
// This function is typically used as the completer function for the prompt.Prompt.
//
// d: the current prompt.Document containing cursor position and text
// Returns a slice of prompt.Suggest for matching built-in commands
func completer(d prompt.Document) []prompt.Suggest {
	text := d.GetWordBeforeCursor()

	// Handle edge case where GetWordBeforeCursor returns empty
	// This happens in tests when cursor position is not set (defaults to 0)
	// In this case, we need to determine the word based on the text content
	if text == "" {
		// If text ends with space, use the word before the space
		trimmed := strings.TrimSpace(d.Text)
		if trimmed != "" {
			// If text had trailing space, complete the last word
			if len(d.Text) > 0 && d.Text[len(d.Text)-1] == ' ' {
				// Get the last word before the trailing space
				text = trimmed
			} else {
				// No trailing space, use the full text
				text = d.Text
			}
		}
	}

	if text == "" {
		return nil
	}

	var suggestions []prompt.Suggest
	for _, cmd := range builtinCommands() {
		if strings.HasPrefix(strings.ToLower(cmd), strings.ToLower(text)) {
			suggestions = append(suggestions, prompt.Suggest{
				Text:        cmd,
				Description: getCommandDescription(cmd),
			})
		}
	}
	return suggestions
}