Merge pull request #1014 from Guldoman/PR_commandview_options2

Add `text`, `select_text` and `show_suggestions` options to `CommandView`
This commit is contained in:
Jefferson González 2022-06-07 19:05:32 -04:00 committed by GitHub
commit 61015dd382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 33 deletions

View File

@ -122,15 +122,16 @@ command.add(nil, {
["core:open-file"] = function() ["core:open-file"] = function()
local view = core.active_view local view = core.active_view
local text
if view.doc and view.doc.abs_filename then if view.doc and view.doc.abs_filename then
local dirname, filename = view.doc.abs_filename:match("(.*)[/\\](.+)$") local dirname, filename = view.doc.abs_filename:match("(.*)[/\\](.+)$")
if dirname then if dirname then
dirname = core.normalize_to_project_dir(dirname) dirname = core.normalize_to_project_dir(dirname)
local text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP
core.command_view:set_text(text)
end end
end end
core.command_view:enter("Open File", { core.command_view:enter("Open File", {
text = text,
submit = function(text) submit = function(text)
local filename = system.absolute_path(common.home_expand(text)) local filename = system.absolute_path(common.home_expand(text))
core.root_view:open_doc(core.open_doc(filename)) core.root_view:open_doc(core.open_doc(filename))
@ -182,10 +183,12 @@ command.add(nil, {
["core:change-project-folder"] = function() ["core:change-project-folder"] = function()
local dirname = common.dirname(core.project_dir) local dirname = common.dirname(core.project_dir)
local text
if dirname then if dirname then
core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) text = common.home_encode(dirname) .. PATHSEP
end end
core.command_view:enter("Change Project Folder", { core.command_view:enter("Change Project Folder", {
text = text,
submit = function(text) submit = function(text)
local path = common.home_expand(text) local path = common.home_expand(text)
local abs_path = check_directory_path(path) local abs_path = check_directory_path(path)
@ -204,10 +207,12 @@ command.add(nil, {
["core:open-project-folder"] = function() ["core:open-project-folder"] = function()
local dirname = common.dirname(core.project_dir) local dirname = common.dirname(core.project_dir)
local text
if dirname then if dirname then
core.command_view:set_text(common.home_encode(dirname) .. PATHSEP) text = common.home_encode(dirname) .. PATHSEP
end end
core.command_view:enter("Open Project", { core.command_view:enter("Open Project", {
text = text,
submit = function(text) submit = function(text)
local path = common.home_expand(text) local path = common.home_expand(text)
local abs_path = check_directory_path(path) local abs_path = check_directory_path(path)

View File

@ -448,13 +448,15 @@ local commands = {
["doc:save-as"] = function() ["doc:save-as"] = function()
local last_doc = core.last_active_view and core.last_active_view.doc local last_doc = core.last_active_view and core.last_active_view.doc
local text
if doc().filename then if doc().filename then
core.command_view:set_text(doc().filename) text = doc().filename
elseif last_doc and last_doc.filename then elseif last_doc and last_doc.filename then
local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$") local dirname, filename = core.last_active_view.doc.abs_filename:match("(.*)[/\\](.+)$")
core.command_view:set_text(core.normalize_to_project_dir(dirname) .. PATHSEP) text = core.normalize_to_project_dir(dirname) .. PATHSEP
end end
core.command_view:enter("Save As", { core.command_view:enter("Save As", {
text = text,
submit = function(filename) submit = function(filename)
save(common.home_expand(filename)) save(common.home_expand(filename))
end, end,
@ -482,8 +484,8 @@ local commands = {
core.error("Cannot rename unsaved doc") core.error("Cannot rename unsaved doc")
return return
end end
core.command_view:set_text(old_filename)
core.command_view:enter("Rename", { core.command_view:enter("Rename", {
text = old_filename,
submit = function(filename) submit = function(filename)
save(common.home_expand(filename)) save(common.home_expand(filename))
core.log("Renamed \"%s\" to \"%s\"", old_filename, filename) core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)

View File

@ -58,11 +58,12 @@ local function find(label, search_fn)
local text = last_view.doc:get_text(table.unpack(last_sel)) local text = last_view.doc:get_text(table.unpack(last_sel))
found_expression = false found_expression = false
core.command_view:set_text(text, true)
core.status_view:show_tooltip(get_find_tooltip()) core.status_view:show_tooltip(get_find_tooltip())
core.command_view:set_hidden_suggestions()
core.command_view:enter(label, { core.command_view:enter(label, {
text = text,
select_text = true,
show_suggestions = false,
submit = function(text, item) submit = function(text, item)
insert_unique(core.previous_find, text) insert_unique(core.previous_find, text)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
@ -91,18 +92,19 @@ end
local function replace(kind, default, fn) local function replace(kind, default, fn)
core.command_view:set_text(default, true)
core.status_view:show_tooltip(get_find_tooltip()) core.status_view:show_tooltip(get_find_tooltip())
core.command_view:set_hidden_suggestions()
core.command_view:enter("Find To Replace " .. kind, { core.command_view:enter("Find To Replace " .. kind, {
text = default,
select_text = true,
show_suggestions = false,
submit = function(old) submit = function(old)
insert_unique(core.previous_find, old) insert_unique(core.previous_find, old)
core.command_view:set_text(old, true)
local s = string.format("Replace %s %q With", kind, old) local s = string.format("Replace %s %q With", kind, old)
core.command_view:set_hidden_suggestions()
core.command_view:enter(s, { core.command_view:enter(s, {
text = old,
select_text = true,
show_suggestions = false,
submit = function(new) submit = function(new)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()
insert_unique(core.previous_replace, new) insert_unique(core.previous_replace, new)

View File

@ -26,6 +26,9 @@ local default_state = {
suggest = noop, suggest = noop,
cancel = noop, cancel = noop,
validate = function() return true end, validate = function() return true end,
text = "",
select_text = false,
show_suggestions = true,
typeahead = true, typeahead = true,
wrap = true, wrap = true,
} }
@ -36,7 +39,6 @@ function CommandView:new()
self.suggestion_idx = 1 self.suggestion_idx = 1
self.suggestions = {} self.suggestions = {}
self.suggestions_height = 0 self.suggestions_height = 0
self.show_suggestions = true
self.last_change_id = 0 self.last_change_id = 0
self.last_text = "" self.last_text = ""
self.gutter_width = 0 self.gutter_width = 0
@ -50,7 +52,8 @@ end
function CommandView:set_hidden_suggestions() function CommandView:set_hidden_suggestions()
self.show_suggestions = false core.warn("Using deprecated function CommandView:set_hidden_suggestions")
self.state.show_suggestions = false
end end
@ -101,7 +104,7 @@ function CommandView:move_suggestion_idx(dir)
end end
end end
if self.show_suggestions then if self.state.show_suggestions then
local n = self.suggestion_idx + dir local n = self.suggestion_idx + dir
self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions) self.suggestion_idx = overflow_suggestion_idx(n, #self.suggestions)
self:complete() self:complete()
@ -145,14 +148,14 @@ function CommandView:submit()
end end
function CommandView:enter(text, ...) function CommandView:enter(label, ...)
if self.state ~= default_state then if self.state ~= default_state then
return return
end end
local options = select(1, ...) local options = select(1, ...)
if type(options) ~= "table" then if type(options) ~= "table" then
core.log("Warning: deprecated CommandView:enter usage") core.warn("Using CommandView:enter in a deprecated way")
local submit, suggest, cancel, validate = ... local submit, suggest, cancel, validate = ...
options = { options = {
submit = submit, submit = submit,
@ -162,12 +165,33 @@ function CommandView:enter(text, ...)
} }
end 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) 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) core.set_active_view(self)
self:update_suggestions() self:update_suggestions()
self.gutter_text_brightness = 100 self.gutter_text_brightness = 100
self.label = text .. ": " self.label = label .. ": "
end end
@ -180,7 +204,6 @@ function CommandView:exit(submitted, inexplicit)
self.doc:reset() self.doc:reset()
self.suggestions = {} self.suggestions = {}
if not submitted then cancel(not inexplicit) end if not submitted then cancel(not inexplicit) end
self.show_suggestions = true
self.save_suggestion = nil self.save_suggestion = nil
self.last_text = "" self.last_text = ""
end end
@ -246,7 +269,7 @@ function CommandView:update()
-- update suggestions box height -- update suggestions box height
local lh = self:get_suggestion_line_height() local lh = self:get_suggestion_line_height()
local dest = self.show_suggestions and math.min(#self.suggestions, max_suggestions) * lh or 0 local dest = self.state.show_suggestions and math.min(#self.suggestions, max_suggestions) * lh or 0
self:move_towards("suggestions_height", dest, nil, "commandview") self:move_towards("suggestions_height", dest, nil, "commandview")
-- update suggestion cursor offset -- update suggestion cursor offset
@ -316,7 +339,7 @@ end
function CommandView:draw() function CommandView:draw()
CommandView.super.draw(self) CommandView.super.draw(self)
if self.show_suggestions then if self.state.show_suggestions then
core.root_view:defer_draw(draw_suggestions_box, self) core.root_view:defer_draw(draw_suggestions_box, self)
end end
end end

View File

@ -229,22 +229,20 @@ local function begin_search(text, fn)
end end
local function set_command_view_text() local function get_selected_text()
local view = core.active_view local view = core.active_view
local doc = (view and view.doc) and view.doc or nil local doc = (view and view.doc) and view.doc or nil
if doc then if doc then
core.command_view:set_text( return doc:get_text(table.unpack({ doc:get_selection() }))
doc:get_text(table.unpack({ doc:get_selection() })),
true
)
end end
end end
command.add(nil, { command.add(nil, {
["project-search:find"] = function() ["project-search:find"] = function()
set_command_view_text()
core.command_view:enter("Find Text In Project", { core.command_view:enter("Find Text In Project", {
text = get_selected_text(),
select_text = true,
submit = function(text) submit = function(text)
text = text:lower() text = text:lower()
begin_search(text, function(line_text) begin_search(text, function(line_text)
@ -266,8 +264,9 @@ command.add(nil, {
end, end,
["project-search:fuzzy-find"] = function() ["project-search:fuzzy-find"] = function()
set_command_view_text()
core.command_view:enter("Fuzzy Find Text In Project", { core.command_view:enter("Fuzzy Find Text In Project", {
text = get_selected_text(),
select_text = true,
submit = function(text) submit = function(text)
begin_search(text, function(line_text) begin_search(text, function(line_text)
return common.fuzzy_match(line_text, text) and 1 return common.fuzzy_match(line_text, text) and 1

View File

@ -735,8 +735,8 @@ command.add(function() return treeitem() ~= nil end, {
local item = treeitem() local item = treeitem()
local old_filename = item.filename local old_filename = item.filename
local old_abs_filename = item.abs_filename local old_abs_filename = item.abs_filename
core.command_view:set_text(old_filename)
core.command_view:enter("Rename", { core.command_view:enter("Rename", {
text = old_filename,
submit = function(filename) submit = function(filename)
local abs_filename = filename local abs_filename = filename
if not common.is_absolute_path(filename) then if not common.is_absolute_path(filename) then
@ -763,11 +763,13 @@ command.add(function() return treeitem() ~= nil end, {
end, end,
["treeview:new-file"] = function() ["treeview:new-file"] = function()
local text
local item = treeitem() local item = treeitem()
if not is_project_folder(item.abs_filename) then if not is_project_folder(item.abs_filename) then
core.command_view:set_text(item.filename .. PATHSEP) text = treeitem().filename .. PATHSEP
end end
core.command_view:enter("Filename", { core.command_view:enter("Filename", {
text = text,
submit = function(filename) submit = function(filename)
local doc_filename = item.dir_name .. PATHSEP .. filename local doc_filename = item.dir_name .. PATHSEP .. filename
core.log(doc_filename) core.log(doc_filename)
@ -784,11 +786,13 @@ command.add(function() return treeitem() ~= nil end, {
end, end,
["treeview:new-folder"] = function() ["treeview:new-folder"] = function()
local text
local item = treeitem() local item = treeitem()
if not is_project_folder(item.abs_filename) then if not is_project_folder(item.abs_filename) then
core.command_view:set_text(item.filename .. "/") text = treeitem().filename .. PATHSEP
end end
core.command_view:enter("Folder Name", { core.command_view:enter("Folder Name", {
text = text,
submit = function(filename) submit = function(filename)
local dir_path = item.dir_name .. PATHSEP .. filename local dir_path = item.dir_name .. PATHSEP .. filename
common.mkdirp(dir_path) common.mkdirp(dir_path)