2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
2024-03-06 04:56:01 +01:00
|
|
|
local config = require "core.config"
|
2019-12-28 12:16:32 +01:00
|
|
|
local style = require "core.style"
|
|
|
|
local Doc = require "core.doc"
|
|
|
|
local DocView = require "core.docview"
|
|
|
|
local View = require "core.view"
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@class core.commandview.input : core.doc
|
|
|
|
---@field super core.doc
|
2019-12-28 12:16:32 +01:00
|
|
|
local SingleLineDoc = Doc:extend()
|
|
|
|
|
|
|
|
function SingleLineDoc:insert(line, col, text)
|
|
|
|
SingleLineDoc.super.insert(self, line, col, text:gsub("\n", ""))
|
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@class core.commandview : core.docview
|
|
|
|
---@field super core.docview
|
2019-12-28 12:16:32 +01:00
|
|
|
local CommandView = DocView:extend()
|
|
|
|
|
2021-08-27 23:55:17 +02:00
|
|
|
CommandView.context = "application"
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local noop = function() end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@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
|
2019-12-28 12:16:32 +01:00
|
|
|
local default_state = {
|
|
|
|
submit = noop,
|
|
|
|
suggest = noop,
|
|
|
|
cancel = noop,
|
2022-05-30 21:12:16 +02:00
|
|
|
validate = function() return true end,
|
2022-06-01 19:34:46 +02:00
|
|
|
text = "",
|
|
|
|
select_text = false,
|
2022-06-01 18:34:11 +02:00
|
|
|
show_suggestions = true,
|
2022-05-30 21:12:16 +02:00
|
|
|
typeahead = true,
|
|
|
|
wrap = true,
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function CommandView:new()
|
|
|
|
CommandView.super.new(self, SingleLineDoc())
|
|
|
|
self.suggestion_idx = 1
|
2024-03-06 04:56:01 +01:00
|
|
|
self.suggestions_offset = 1
|
2019-12-28 12:16:32 +01:00
|
|
|
self.suggestions = {}
|
|
|
|
self.suggestions_height = 0
|
|
|
|
self.last_change_id = 0
|
2022-04-30 22:09:40 +02:00
|
|
|
self.last_text = ""
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@deprecated
|
2021-09-07 17:06:19 +02:00
|
|
|
function CommandView:set_hidden_suggestions()
|
2022-06-01 18:34:11 +02:00
|
|
|
core.warn("Using deprecated function CommandView:set_hidden_suggestions")
|
|
|
|
self.state.show_suggestions = false
|
2021-09-07 17:06:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function CommandView:get_name()
|
|
|
|
return View.get_name(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-24 17:01:46 +01:00
|
|
|
function CommandView:get_line_screen_position(line, col)
|
|
|
|
local x = CommandView.super.get_line_screen_position(self, 1, col)
|
2019-12-28 12:16:32 +01:00
|
|
|
local _, y = self:get_content_offset()
|
|
|
|
local lh = self:get_line_height()
|
|
|
|
return x, y + (self.size.y - lh) / 2
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-04-07 18:42:46 +02:00
|
|
|
function CommandView:supports_text_input()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function CommandView:get_scrollable_size()
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2022-10-16 02:12:15 +02:00
|
|
|
function CommandView:get_h_scrollable_size()
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-05-09 17:09:07 +02:00
|
|
|
function CommandView:set_text(text, select)
|
2022-04-30 22:09:40 +02:00
|
|
|
self.last_text = text
|
2019-12-28 12:16:32 +01:00
|
|
|
self.doc:remove(1, 1, math.huge, math.huge)
|
|
|
|
self.doc:text_input(text)
|
2020-05-09 17:09:07 +02:00
|
|
|
if select then
|
|
|
|
self.doc:set_selection(math.huge, math.huge, 1, 1)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-05-30 21:12:16 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function CommandView:move_suggestion_idx(dir)
|
2022-05-30 18:13:21 +02:00
|
|
|
local function overflow_suggestion_idx(n, count)
|
2022-07-11 23:27:26 +02:00
|
|
|
if count == 0 then return 0 end
|
2022-05-30 21:12:16 +02:00
|
|
|
if self.state.wrap then
|
2022-05-30 18:13:21 +02:00
|
|
|
return (n - 1) % count + 1
|
|
|
|
else
|
|
|
|
return common.clamp(n, 1, count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-06 04:56:01 +01:00
|
|
|
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
|
|
|
|
|
2022-06-01 18:34:11 +02:00
|
|
|
if self.state.show_suggestions then
|
2021-09-07 17:06:19 +02:00
|
|
|
local n = self.suggestion_idx + dir
|
2022-05-30 18:13:21 +02:00
|
|
|
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
|
2021-09-07 18:33:58 +02:00
|
|
|
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
|
2022-05-30 18:13:21 +02:00
|
|
|
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
|
2021-09-07 18:33:58 +02:00
|
|
|
self:complete()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.save_suggestion = text
|
|
|
|
self:complete()
|
|
|
|
end
|
|
|
|
self.last_change_id = self.doc:get_change_id()
|
2021-09-07 17:06:19 +02:00
|
|
|
self.state.suggest(self:get_text())
|
|
|
|
end
|
2024-03-06 04:56:01 +01:00
|
|
|
|
|
|
|
self.suggestions_offset = get_suggestions_offset()
|
2019-12-28 12:16:32 +01:00
|
|
|
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()
|
2022-11-15 17:03:13 +01:00
|
|
|
if self.state.validate(text, suggestion) then
|
2021-05-05 08:04:51 +02:00
|
|
|
local submit = self.state.submit
|
|
|
|
self:exit(true)
|
|
|
|
submit(text, suggestion)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@param label string
|
|
|
|
---@varargs any
|
|
|
|
---@overload fun(label:string, options: core.commandview.state)
|
2022-06-01 19:34:46 +02:00
|
|
|
function CommandView:enter(label, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
if self.state ~= default_state then
|
|
|
|
return
|
|
|
|
end
|
2022-05-30 21:12:16 +02:00
|
|
|
local options = select(1, ...)
|
|
|
|
|
|
|
|
if type(options) ~= "table" then
|
2022-06-01 19:34:46 +02:00
|
|
|
core.warn("Using CommandView:enter in a deprecated way")
|
2022-05-30 21:12:16 +02:00
|
|
|
local submit, suggest, cancel, validate = ...
|
|
|
|
options = {
|
|
|
|
submit = submit,
|
|
|
|
suggest = suggest,
|
|
|
|
cancel = cancel,
|
|
|
|
validate = validate,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-06-01 18:34:11 +02:00
|
|
|
-- 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
|
|
|
|
|
2022-05-30 21:12:16 +02:00
|
|
|
self.state = common.merge(default_state, options)
|
|
|
|
|
2022-06-01 19:34:46 +02:00
|
|
|
-- 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
|
|
|
|
|
2020-05-19 13:58:41 +02:00
|
|
|
core.set_active_view(self)
|
2019-12-28 12:16:32 +01:00
|
|
|
self:update_suggestions()
|
|
|
|
self.gutter_text_brightness = 100
|
2022-06-01 19:34:46 +02:00
|
|
|
self.label = label .. ": "
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function CommandView:exit(submitted, inexplicit)
|
|
|
|
if core.active_view == self then
|
2020-05-19 13:58:41 +02:00
|
|
|
core.set_active_view(core.last_active_view)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
local cancel = self.state.cancel
|
|
|
|
self.state = default_state
|
|
|
|
self.doc:reset()
|
|
|
|
self.suggestions = {}
|
|
|
|
if not submitted then cancel(not inexplicit) end
|
2021-09-07 18:33:58 +02:00
|
|
|
self.save_suggestion = nil
|
2022-04-30 22:09:40 +02:00
|
|
|
self.last_text = ""
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-07-15 17:27:27 +02:00
|
|
|
function CommandView:get_line_height()
|
|
|
|
return math.floor(self:get_font():get_height() * 1.2)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
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 t = self.state.suggest(self:get_text()) or {}
|
|
|
|
local res = {}
|
|
|
|
for i, item in ipairs(t) do
|
|
|
|
if type(item) == "string" then
|
|
|
|
item = { text = item }
|
|
|
|
end
|
|
|
|
res[i] = item
|
|
|
|
end
|
|
|
|
self.suggestions = res
|
|
|
|
self.suggestion_idx = 1
|
2024-03-06 04:56:01 +01:00
|
|
|
self.suggestions_offset = 1
|
2019-12-28 12:16:32 +01:00
|
|
|
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:update_suggestions()
|
2022-05-30 21:12:16 +02:00
|
|
|
if self.state.typeahead and self.suggestions[self.suggestion_idx] then
|
2022-04-30 22:09:40 +02:00
|
|
|
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
|
2019-12-28 12:16:32 +01:00
|
|
|
self.last_change_id = self.doc:get_change_id()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- update gutter text color brightness
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards("gutter_text_brightness", 0, 0.1, "commandview")
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- 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
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards("gutter_width", dest, nil, "commandview")
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- update suggestions box height
|
|
|
|
local lh = self:get_suggestion_line_height()
|
2024-03-06 04:56:01 +01:00
|
|
|
local dest = self.state.show_suggestions and math.min(#self.suggestions, config.max_visible_commands) * lh or 0
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards("suggestions_height", dest, nil, "commandview")
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- update suggestion cursor offset
|
2024-03-06 04:56:01 +01:00
|
|
|
local dest = (self.suggestion_idx - self.suggestions_offset + 1) * self:get_suggestion_line_height()
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards("selection_offset", dest, nil, "commandview")
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- 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
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self.size, "y", dest, nil, "commandview")
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function CommandView:draw_line_highlight()
|
|
|
|
-- no-op function to disable this functionality
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-04-18 16:24:45 +02:00
|
|
|
function CommandView:draw_line_gutter(idx, x, y)
|
2019-12-28 12:16:32 +01:00
|
|
|
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)
|
2020-05-14 10:52:10 +02:00
|
|
|
x = x + style.padding.x
|
2019-12-28 12:16:32 +01:00
|
|
|
renderer.draw_text(self:get_font(), self.label, x, y + yoffset, color)
|
|
|
|
core.pop_clip_rect()
|
2022-03-24 17:01:46 +01:00
|
|
|
return self:get_line_height()
|
2019-12-28 12:16:32 +01:00
|
|
|
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()
|
2020-04-22 20:25:47 +02:00
|
|
|
local h = math.ceil(self.suggestions_height)
|
2019-12-28 12:16:32 +01:00
|
|
|
local rx, ry, rw, rh = self.position.x, self.position.y - h - dh, self.size.x, h
|
|
|
|
|
2024-03-06 04:56:01 +01:00
|
|
|
core.push_clip_rect(rx, ry, rw, rh)
|
2019-12-28 12:16:32 +01:00
|
|
|
-- 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
|
2024-03-06 04:56:01 +01:00
|
|
|
local first = math.max(self.suggestions_offset, 1)
|
|
|
|
local last = math.min(self.suggestions_offset + config.max_visible_commands, #self.suggestions)
|
2022-06-20 16:01:28 +02:00
|
|
|
for i=first, last do
|
2021-02-28 23:20:29 +01:00
|
|
|
local item = self.suggestions[i]
|
|
|
|
local color = (i == self.suggestion_idx) and style.accent or style.text
|
2024-03-06 04:56:01 +01:00
|
|
|
local y = self.position.y - (i - first + 1) * lh - dh
|
2021-02-28 23:20:29 +01:00
|
|
|
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)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
core.pop_clip_rect()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function CommandView:draw()
|
|
|
|
CommandView.super.draw(self)
|
2022-06-01 18:34:11 +02:00
|
|
|
if self.state.show_suggestions then
|
2021-09-07 17:06:19 +02:00
|
|
|
core.root_view:defer_draw(draw_suggestions_box, self)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return CommandView
|