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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
local core = require "core"
local common = require "core.common"
local config = require "core.config"
local style = require "core.style"
local Doc = require "core.doc"
local DocView = require "core.docview"
local View = require "core.view"
---@class core.commandview.input : core.doc
---@field super core.doc
local SingleLineDoc = Doc:extend()
function SingleLineDoc:__tostring() return "SingleLineDoc" end
function SingleLineDoc:insert(line, col, text)
SingleLineDoc.super.insert(self, line, col, text:gsub("\n", ""))
end
---@class core.commandview : core.docview
---@field super core.docview
local CommandView = DocView:extend()
function CommandView:__tostring() return "CommandView" end
CommandView.context = "application"
local noop = function() end
---@class core.commandview.state
---@field submit function
---@field suggest function
---@field cancel function
---@field validate function
---@field text string
---@field select_text boolean
---@field show_suggestions boolean
---@field typeahead boolean
---@field wrap boolean
local default_state = {
submit = noop,
suggest = noop,
cancel = noop,
validate = function() return true end,
text = "",
select_text = false,
show_suggestions = true,
typeahead = true,
wrap = true,
}
function CommandView:new()
CommandView.super.new(self, SingleLineDoc())
self.suggestion_idx = 1
self.suggestions_offset = 1
self.suggestions = {}
self.suggestions_height = 0
self.last_change_id = 0
self.last_text = ""
self.user_supplied_text = ""
self.last_change = "text"
self.gutter_width = 0
self.gutter_text_brightness = 0
self.selection_offset = 0
self.state = default_state
self.font = "font"
self.size.y = 0
self.label = ""
end
---@deprecated
function CommandView:set_hidden_suggestions()
core.warn("Using deprecated function CommandView:set_hidden_suggestions")
self.state.show_suggestions = false
end
function CommandView:get_name()
return View.get_name(self)
end
function CommandView:get_line_screen_position(line, col)
local x = CommandView.super.get_line_screen_position(self, 1, col)
local _, y = self:get_content_offset()
local lh = self:get_line_height()
return x, y + (self.size.y - lh) / 2
end
function CommandView:supports_text_input()
return true
end
function CommandView:get_scrollable_size()
return 0
end
function CommandView:get_h_scrollable_size()
return 0
end
function CommandView:scroll_to_make_visible()
-- no-op function to disable this functionality
end
function CommandView:get_text()
return self.doc:get_text(1, 1, 1, math.huge)
end
function CommandView:set_text(text, select)
self.last_text = text
self.doc:remove(1, 1, math.huge, math.huge)
self.doc:text_input(text)
if select then
self.doc:set_selection(math.huge, math.huge, 1, 1)
end
end
function CommandView:move_suggestion_idx(dir)
local function overflow_suggestion_idx(n, count)
if count == 0 then return 0 end
if self.state.wrap then
return (n - 1) % count + 1
else
return common.clamp(n, 1, count)
end
end
local function get_suggestions_offset()
local max_visible = math.min(config.max_visible_commands, #self.suggestions)
if dir > 0 then
if self.suggestions_offset + max_visible < self.suggestion_idx + 1 then
return self.suggestion_idx - max_visible + 1
elseif self.suggestions_offset > self.suggestion_idx then
return self.suggestion_idx
end
else
if self.suggestions_offset > self.suggestion_idx then
return self.suggestion_idx
elseif self.suggestions_offset + max_visible < self.suggestion_idx + 1 then
return self.suggestion_idx - max_visible + 1
end
end
return self.suggestions_offset
end
self.last_change = "suggestion"
if self.state.show_suggestions then
local n = self.suggestion_idx + dir
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete()
self.last_change_id = self.doc:get_change_id()
else
local current_suggestion = #self.suggestions > 0 and self.suggestions[self.suggestion_idx].text
local text = self:get_text()
if text == current_suggestion then
local n = self.suggestion_idx + dir
if n == 0 and self.save_suggestion then
self:set_text(self.save_suggestion)
else
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete()
end
else
self.save_suggestion = text
self:complete()
end
self.last_change_id = self.doc:get_change_id()
self.state.suggest(self:get_text())
end
self.suggestions_offset = get_suggestions_offset()
end
function CommandView:complete()
if #self.suggestions > 0 then
self:set_text(self.suggestions[self.suggestion_idx].text)
end
end
function CommandView:submit()
local suggestion = self.suggestions[self.suggestion_idx]
local text = self:get_text()
if self.state.validate(text, suggestion) then
local submit = self.state.submit
self:exit(true)
submit(text, suggestion)
end
end
---@param label string
---@varargs any
---@overload fun(label:string, options: core.commandview.state)
function CommandView:enter(label, ...)
if self.state ~= default_state then
return
end
local options = select(1, ...)
if type(options) ~= "table" then
core.warn("Using CommandView:enter in a deprecated way")
local submit, suggest, cancel, validate = ...
options = {
submit = submit,
suggest = suggest,
cancel = cancel,
validate = validate,
}
end
-- Support deprecated CommandView:set_hidden_suggestions
-- Remove this when set_hidden_suggestions is not supported anymore
if options.show_suggestions == nil then
options.show_suggestions = self.state.show_suggestions
end
self.state = common.merge(default_state, options)
-- We need to keep the text entered with CommandView:set_text to
-- maintain compatibility with deprecated usage, but still allow
-- overwriting with options.text
local old_text = self:get_text()
if old_text ~= "" then
core.warn("Using deprecated function CommandView:set_text")
end
if options.text or options.select_text then
local text = options.text or old_text
self:set_text(text, self.state.select_text)
end
-- Replace with a simple
-- self:set_text(self.state.text, self.state.select_text)
-- once old usage is removed
core.set_active_view(self)
self:update_suggestions()
self.gutter_text_brightness = 100
self.label = label .. ": "
end
function CommandView:exit(submitted, inexplicit)
if core.active_view == self then
core.set_active_view(core.last_active_view)
end
local cancel = self.state.cancel
self.state = default_state
self.doc:reset()
self.suggestions = {}
if not submitted then cancel(not inexplicit) end
self.save_suggestion = nil
self.last_text = ""
end
function CommandView:get_line_height()
return math.floor(self:get_font():get_height() * 1.2)
end
function CommandView:get_gutter_width()
return self.gutter_width
end
function CommandView:get_suggestion_line_height()
return self:get_font():get_height() + style.padding.y
end
function CommandView:update_suggestions()
local text = self:get_text()
local t = self.state.suggest(self.last_change == "suggestion" and self.user_supplied_text or text) or {}
local res = {}
for i, item in ipairs(t) do
if type(item) == "string" then
item = { text = item }
end
res[i] = item
end
if self.suggestions and self.last_change == "suggestion" then
local new_suggestion_idx
for i, v in ipairs(res) do
if v.text == self.suggestions[self.suggestion_idx].text then
new_suggestion_idx = i
break
end
end
self.suggestion_idx = new_suggestion_idx
-- This preserves the suggestion_offset and realigns it with the new table.
self:move_suggestion_idx(0)
else
self.suggestion_idx = 1
self.suggestions_offset = 1
end
self.suggestions = res
end
function CommandView:update()
CommandView.super.update(self)
if core.active_view ~= self and self.state ~= default_state then
self:exit(false, true)
end
-- update suggestions if text has changed
if self.last_change_id ~= self.doc:get_change_id() then
self.last_change = "text"
self.user_supplied_text = self:get_text()
self:update_suggestions()
if self.state.typeahead and self.suggestions[self.suggestion_idx] then
local current_text = self:get_text()
local suggested_text = self.suggestions[self.suggestion_idx].text or ""
if #self.last_text < #current_text and
string.find(suggested_text, current_text, 1, true) == 1 then
self:set_text(suggested_text)
self.doc:set_selection(1, #current_text + 1, 1, math.huge)
end
self.last_text = current_text
end
self.last_change_id = self.doc:get_change_id()
end
-- update gutter text color brightness
self:move_towards("gutter_text_brightness", 0, 0.1, "commandview")
-- update gutter width
local dest = self:get_font():get_width(self.label) + style.padding.x
if self.size.y <= 0 then
self.gutter_width = dest
else
self:move_towards("gutter_width", dest, nil, "commandview")
end
-- update suggestions box height
local lh = self:get_suggestion_line_height()
local dest = self.state.show_suggestions and math.min(#self.suggestions, config.max_visible_commands) * lh or 0
self:move_towards("suggestions_height", dest, nil, "commandview")
-- update suggestion cursor offset
local dest = (self.suggestion_idx - self.suggestions_offset + 1) * self:get_suggestion_line_height()
self:move_towards("selection_offset", dest, nil, "commandview")
-- update size based on whether this is the active_view
local dest = 0
if self == core.active_view then
dest = style.font:get_height() + style.padding.y * 2
end
self:move_towards(self.size, "y", dest, nil, "commandview")
end
function CommandView:draw_line_highlight()
-- no-op function to disable this functionality
end
function CommandView:draw_line_gutter(idx, x, y)
local yoffset = self:get_line_text_y_offset()
local pos = self.position
local color = common.lerp(style.text, style.accent, self.gutter_text_brightness / 100)
core.push_clip_rect(pos.x, pos.y, self:get_gutter_width(), self.size.y)
x = x + style.padding.x
renderer.draw_text(self:get_font(), self.label, x, y + yoffset, color)
core.pop_clip_rect()
return self:get_line_height()
end
local function draw_suggestions_box(self)
local lh = self:get_suggestion_line_height()
local dh = style.divider_size
local x, _ = self:get_line_screen_position()
local h = math.ceil(self.suggestions_height)
local rx, ry, rw, rh = self.position.x, self.position.y - h - dh, self.size.x, h
core.push_clip_rect(rx, ry, rw, rh)
-- draw suggestions background
if #self.suggestions > 0 then
renderer.draw_rect(rx, ry, rw, rh, style.background3)
renderer.draw_rect(rx, ry - dh, rw, dh, style.divider)
local y = self.position.y - self.selection_offset - dh
renderer.draw_rect(rx, y, rw, lh, style.line_highlight)
end
-- draw suggestion text
local first = math.max(self.suggestions_offset, 1)
local last = math.min(self.suggestions_offset + config.max_visible_commands, #self.suggestions)
for i=first, last do
local item = self.suggestions[i]
local color = (i == self.suggestion_idx) and style.accent or style.text
local y = self.position.y - (i - first + 1) * lh - dh
common.draw_text(self:get_font(), color, item.text, nil, x, y, 0, lh)
if item.info then
local w = self.size.x - x - style.padding.x
common.draw_text(self:get_font(), style.dim, item.info, "right", x, y, w, lh)
end
end
core.pop_clip_rect()
end
function CommandView:draw()
CommandView.super.draw(self)
if self.state.show_suggestions then
core.root_view:defer_draw(draw_suggestions_box, self)
end
end
return CommandView
|