File: completion.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (195 lines) | stat: -rw-r--r-- 4,982 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package cli

import (
	"fmt"
	"path/filepath"
	"strings"

	"github.com/kovidgoyal/kitty/tools/utils"
	"github.com/kovidgoyal/kitty/tools/wcswidth"
)

var _ = fmt.Print

type Match struct {
	Word        string `json:"word,omitempty"`
	Description string `json:"description,omitempty"`
}

type MatchGroup struct {
	Title           string   `json:"title,omitempty"`
	NoTrailingSpace bool     `json:"no_trailing_space,omitempty"`
	IsFiles         bool     `json:"is_files,omitempty"`
	Matches         []*Match `json:"matches,omitempty"`
}

func (self *MatchGroup) remove_common_prefix() string {
	if self.IsFiles {
		if len(self.Matches) > 1 {
			lcp := self.longest_common_prefix()
			if strings.Contains(lcp, utils.Sep) {
				lcp = strings.TrimRight(filepath.Dir(lcp), utils.Sep) + utils.Sep
				self.remove_prefix_from_all_matches(lcp)
				return lcp
			}
		}
	} else if len(self.Matches) > 1 && strings.HasPrefix(self.Matches[0].Word, "--") && strings.Contains(self.Matches[0].Word, "=") {
		lcp, _, _ := strings.Cut(self.longest_common_prefix(), "=")
		lcp += "="
		if len(lcp) > 3 {
			self.remove_prefix_from_all_matches(lcp)
			return lcp
		}
	}
	return ""
}

func (self *MatchGroup) AddMatch(word string, description ...string) *Match {
	ans := Match{Word: word, Description: strings.Join(description, " ")}
	self.Matches = append(self.Matches, &ans)
	return &ans
}

func (self *MatchGroup) AddPrefixToAllMatches(prefix string) {
	for _, m := range self.Matches {
		m.Word = prefix + m.Word
	}
}

func (self *MatchGroup) remove_prefix_from_all_matches(prefix string) {
	for _, m := range self.Matches {
		m.Word = m.Word[len(prefix):]
	}
}

func (self *MatchGroup) has_descriptions() bool {
	for _, m := range self.Matches {
		if m.Description != "" {
			return true
		}
	}
	return false
}

func (self *MatchGroup) max_visual_word_length(limit int) int {
	ans := 0
	for _, m := range self.Matches {
		if q := wcswidth.Stringwidth(m.Word); q > ans {
			ans = q
			if ans > limit {
				return limit
			}
		}
	}
	return ans
}

func (self *MatchGroup) longest_common_prefix() string {
	limit := len(self.Matches)
	i := 0
	return utils.LongestCommon(func() (string, bool) {
		if i < limit {
			i++
			return self.Matches[i-1].Word, false
		}
		return "", true
	}, true)
}

type Delegate struct {
	NumToRemove int    `json:"num_to_remove,omitempty"`
	Command     string `json:"command,omitempty"`
}

type Completions struct {
	Groups   []*MatchGroup `json:"groups,omitempty"`
	Delegate Delegate      `json:"delegate,omitempty"`

	CurrentCmd             *Command `json:"-"`
	AllWords               []string `json:"-"` // all words passed to parse_args()
	CurrentWordIdx         int      `json:"-"` // index of current word in all_words
	CurrentWordIdxInParent int      `json:"-"` // index of current word in parents command line 1 for first word after parent

	split_on_equals bool // true if the cmdline is split on = (BASH does this because readline does this)
}

func NewCompletions() *Completions {
	return &Completions{Groups: make([]*MatchGroup, 0, 4)}
}

func (self *Completions) AddPrefixToAllMatches(prefix string) {
	for _, mg := range self.Groups {
		mg.AddPrefixToAllMatches(prefix)
	}
}

func (self *Completions) MergeMatchGroup(mg *MatchGroup) {
	if len(mg.Matches) == 0 {
		return
	}
	var dest *MatchGroup
	for _, q := range self.Groups {
		if q.Title == mg.Title {
			dest = q
			break
		}
	}
	if dest == nil {
		dest = self.AddMatchGroup(mg.Title)
		dest.NoTrailingSpace = mg.NoTrailingSpace
		dest.IsFiles = mg.IsFiles
	}
	seen := utils.NewSet[string](64)
	for _, q := range self.Groups {
		for _, m := range q.Matches {
			seen.Add(m.Word)
		}
	}
	for _, m := range mg.Matches {
		if !seen.Has(m.Word) {
			seen.Add(m.Word)
			dest.Matches = append(dest.Matches, m)
		}
	}
}

func (self *Completions) AddMatchGroup(title string) *MatchGroup {
	for _, q := range self.Groups {
		if q.Title == title {
			return q
		}
	}
	ans := MatchGroup{Title: title, Matches: make([]*Match, 0, 8)}
	self.Groups = append(self.Groups, &ans)
	return &ans
}

type CompletionFunc = func(completions *Completions, word string, arg_num int)

func NamesCompleter(title string, names ...string) CompletionFunc {
	return func(completions *Completions, word string, arg_num int) {
		mg := completions.AddMatchGroup(title)
		for _, q := range names {
			if strings.HasPrefix(q, word) {
				mg.AddMatch(q)
			}
		}
	}
}

func ChainCompleters(completers ...CompletionFunc) CompletionFunc {
	return func(completions *Completions, word string, arg_num int) {
		for _, f := range completers {
			f(completions, word, arg_num)
		}
	}
}

func CompletionForWrapper(wrapped_cmd string) func(completions *Completions, word string, arg_num int) {
	return func(completions *Completions, word string, arg_num int) {
		completions.Delegate.NumToRemove = completions.CurrentWordIdx
		completions.Delegate.Command = wrapped_cmd
	}
}