turbo-editors/turbo-corepublic Fork 0
28d59854361aeda8541d853093e732126f3d7bff
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on 28d59854361aeda8541d853093e732126f3d7bff · k33g · 14h ago
completion.go · 268 lines · 6.9 KBGo Blame HistoryRaw
  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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package app

import (
	"strings"

	"github.com/gdamore/tcell/v2"

	"codeberg.org/turbo-editors/turbo-core/lsp"
	"codeberg.org/turbo-editors/turbo-core/theme"
	"codeberg.org/turbo-editors/turbo-core/ui"
)

// Completion popup geometry. It is deliberately small: a list that covers half
// the file hides the very code the completion is about.
const (
	completionMaxRows  = 8
	completionMinWidth = 24
	completionMaxWidth = 60
)

// CompletionBox is the list of suggestions that pops up under the cursor.
//
// It filters as the user types, so the language server is asked once and the
// narrowing that follows costs nothing.
type CompletionBox struct {
	ui.Box
	items    []lsp.CompletionItem
	matches  []int // indexes into items, in the order they are shown
	selected int
	top      int
	prefix   string
	visible  bool

	// OnAccept is called with the chosen item.
	OnAccept func(lsp.CompletionItem)
}

// Visible reports whether the popup is showing.
func (c *CompletionBox) Visible() bool { return c.visible }

// Show opens the popup with a set of suggestions, filtered by prefix, anchored
// so that its top-left corner is just below the cursor.
//
// It closes again straight away if nothing matches, because a popup showing
// nothing is worse than no popup.
func (c *CompletionBox) Show(items []lsp.CompletionItem, prefix string, cursorX, cursorY int, screen ui.Rect) {
	c.items = items
	c.prefix = prefix
	c.selected = 0
	c.top = 0
	c.refilter()

	if len(c.matches) == 0 {
		c.Hide()
		return
	}
	c.visible = true
	c.SetBounds(c.placeUnder(cursorX, cursorY, screen))
}

// Hide closes the popup.
func (c *CompletionBox) Hide() {
	c.visible = false
	c.items = nil
	c.matches = nil
	c.prefix = ""
}

// SetPrefix narrows the list as the user keeps typing, and closes it once
// nothing matches any more.
func (c *CompletionBox) SetPrefix(prefix string) {
	if !c.visible {
		return
	}
	c.prefix = prefix
	c.selected = 0
	c.top = 0
	c.refilter()

	if len(c.matches) == 0 {
		c.Hide()
	}
}

// refilter recomputes which items match the prefix.
//
// Matching is case-insensitive on the prefix, which is what makes typing
// "prin" find "Println" without having to guess the case first.
func (c *CompletionBox) refilter() {
	c.matches = c.matches[:0]
	wanted := strings.ToLower(c.prefix)

	for i, item := range c.items {
		if strings.HasPrefix(strings.ToLower(item.Label), wanted) {
			c.matches = append(c.matches, i)
		}
	}
}

// placeUnder returns where the popup should sit: under the cursor if it fits,
// above it if it does not, and always inside the screen.
func (c *CompletionBox) placeUnder(cursorX, cursorY int, screen ui.Rect) ui.Rect {
	height := min(len(c.matches), completionMaxRows) + 2
	width := min(max(c.widestEntry()+4, completionMinWidth), completionMaxWidth)

	bounds := ui.Rect{X: cursorX - 1, Y: cursorY + 1, W: width, H: height}
	if bounds.Bottom() > screen.Bottom() {
		bounds.Y = cursorY - height
	}
	return bounds.ClampInto(screen)
}

// widestEntry returns the width of the longest line the popup will show.
func (c *CompletionBox) widestEntry() int {
	widest := 0
	for _, index := range c.matches {
		widest = max(widest, len([]rune(c.entryText(c.items[index]))))
	}
	return widest
}

// entryText is one line of the popup: the label, then its kind.
func (c *CompletionBox) entryText(item lsp.CompletionItem) string {
	if item.Kind == 0 {
		return item.Label
	}
	return item.Label + "  " + item.Kind.String()
}

// Selected returns the highlighted item and whether there is one.
func (c *CompletionBox) Selected() (lsp.CompletionItem, bool) {
	if !c.visible || c.selected >= len(c.matches) {
		return lsp.CompletionItem{}, false
	}
	return c.items[c.matches[c.selected]], true
}

// Count returns how many suggestions are currently shown.
func (c *CompletionBox) Count() int { return len(c.matches) }

// Matches returns the suggestions currently shown, in the order they appear.
func (c *CompletionBox) Matches() []lsp.CompletionItem {
	items := make([]lsp.CompletionItem, 0, len(c.matches))
	for _, index := range c.matches {
		items = append(items, c.items[index])
	}
	return items
}

// Accept chooses the highlighted item and closes the popup.
func (c *CompletionBox) Accept() {
	item, ok := c.Selected()
	c.Hide()
	if ok && c.OnAccept != nil {
		c.OnAccept(item)
	}
}

// move walks the list, clamping at either end rather than wrapping: a list
// that wraps makes it easy to sail past the entry you wanted.
func (c *CompletionBox) move(by int) {
	if len(c.matches) == 0 {
		return
	}
	c.selected = min(max(c.selected+by, 0), len(c.matches)-1)

	rows := min(len(c.matches), completionMaxRows)
	c.top = min(c.top, c.selected)
	if c.selected >= c.top+rows {
		c.top = c.selected - rows + 1
	}
}

// Draw paints the popup, framed and shadowed like every other floating thing.
func (c *CompletionBox) Draw(p *ui.Painter, th *theme.Theme) {
	if !c.visible {
		return
	}

	bounds := c.Bounds()
	ui.DrawShadow(p, bounds, th.Style(theme.KeyShadow))

	panel := p.Sub(bounds)
	panel.Clear(th.Style(theme.KeyCompletionItem))
	ui.DrawFrame(panel, panel.Size(), ui.FrameSingle, th.Style(theme.KeyCompletionFrame))

	rows := min(panel.Size().H-2, len(c.matches)-c.top)
	for row := range rows {
		c.drawEntry(panel, th, row)
	}
}

// drawEntry paints one line of the popup.
func (c *CompletionBox) drawEntry(p *ui.Painter, th *theme.Theme, row int) {
	index := c.matches[c.top+row]
	item := c.items[index]
	width := p.Size().W - 2

	style := th.Style(theme.KeyCompletionItem)
	if c.top+row == c.selected {
		style = th.Style(theme.KeyCompletionSelected)
	}

	p.HLine(1, row+1, width, ' ', style)
	end := p.TextLimited(1, row+1, width, item.Label, style)

	if item.Kind != 0 {
		tag := " " + item.Kind.String()
		x := p.Size().W - 1 - len([]rune(tag))
		if x > end {
			p.Text(x, row+1, tag, th.Style(theme.KeyCompletionDetail))
		}
	}
}

// HandleKey works the popup and reports whether it consumed the key.
//
// Anything it does not use — a printable character, most notably — falls
// through to the editor, which is what lets the user keep typing while the
// list narrows itself.
func (c *CompletionBox) HandleKey(ev *tcell.EventKey) bool {
	if !c.visible {
		return false
	}

	switch ev.Key() {
	case tcell.KeyUp:
		c.move(-1)
	case tcell.KeyDown:
		c.move(+1)
	case tcell.KeyPgUp:
		c.move(-completionMaxRows)
	case tcell.KeyPgDn:
		c.move(+completionMaxRows)
	case tcell.KeyEnter, tcell.KeyTab:
		c.Accept()
	case tcell.KeyEscape:
		c.Hide()
	default:
		return false
	}
	return true
}

// HandleMouse selects and accepts entries with the pointer.
func (c *CompletionBox) HandleMouse(ev *tcell.EventMouse) bool {
	if !c.visible {
		return false
	}

	x, y := ev.Position()
	if !c.Bounds().Contains(x, y) {
		if ev.Buttons() == tcell.Button1 {
			c.Hide() // a click elsewhere puts the list away
		}
		return false
	}

	index := c.top + y - c.Bounds().Y - 1
	if index < 0 || index >= len(c.matches) {
		return true
	}
	c.selected = index
	if ev.Buttons() == tcell.Button1 {
		c.Accept()
	}
	return true
}