Add `text` and `select_text` to `CommandView` options

This commit is contained in:
Guldoman 2022-06-01 19:34:46 +02:00
parent 6c89a3e575
commit ec58b1f0bd
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
6 changed files with 53 additions and 24 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,10 +58,11 @@ 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:enter(label, { core.command_view:enter(label, {
text = text,
select_text = true,
show_suggestions = false, show_suggestions = false,
submit = function(text, item) submit = function(text, item)
insert_unique(core.previous_find, text) insert_unique(core.previous_find, text)
@ -91,17 +92,18 @@ 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:enter("Find To Replace " .. kind, { core.command_view:enter("Find To Replace " .. kind, {
text = default,
select_text = true,
show_suggestions = false, 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:enter(s, { core.command_view:enter(s, {
text = old,
select_text = true,
show_suggestions = false, show_suggestions = false,
submit = function(new) submit = function(new)
core.status_view:remove_tooltip() core.status_view:remove_tooltip()

View File

@ -26,6 +26,8 @@ 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, show_suggestions = true,
typeahead = true, typeahead = true,
wrap = true, wrap = true,
@ -146,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,
@ -171,10 +173,25 @@ function CommandView:enter(text, ...)
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

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

@ -689,8 +689,8 @@ command.add(function() return treeitem() ~= nil end, {
["treeview:rename"] = function() ["treeview:rename"] = function()
local old_filename = treeitem().filename local old_filename = treeitem().filename
local old_abs_filename = treeitem().abs_filename local old_abs_filename = treeitem().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)
filename = core.normalize_to_project_dir(filename) filename = core.normalize_to_project_dir(filename)
local abs_filename = core.project_absolute_path(filename) local abs_filename = core.project_absolute_path(filename)
@ -713,10 +713,12 @@ command.add(function() return treeitem() ~= nil end, {
end, end,
["treeview:new-file"] = function() ["treeview:new-file"] = function()
local text
if not is_project_folder(treeitem().abs_filename) then if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/") text = treeitem().filename .. "/"
end end
core.command_view:enter("Filename", { core.command_view:enter("Filename", {
text = text,
submit = function(filename) submit = function(filename)
local doc_filename = core.project_dir .. PATHSEP .. filename local doc_filename = core.project_dir .. PATHSEP .. filename
local file = io.open(doc_filename, "a+") local file = io.open(doc_filename, "a+")
@ -730,10 +732,12 @@ command.add(function() return treeitem() ~= nil end, {
end, end,
["treeview:new-folder"] = function() ["treeview:new-folder"] = function()
local text
if not is_project_folder(treeitem().abs_filename) then if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/") text = treeitem().filename .. "/"
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 = core.project_dir .. PATHSEP .. filename local dir_path = core.project_dir .. PATHSEP .. filename
common.mkdirp(dir_path) common.mkdirp(dir_path)