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

View File

@ -448,13 +448,15 @@ local commands = {
["doc:save-as"] = function()
local last_doc = core.last_active_view and core.last_active_view.doc
local text
if doc().filename then
core.command_view:set_text(doc().filename)
text = doc().filename
elseif last_doc and last_doc.filename then
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
core.command_view:enter("Save As", {
text = text,
submit = function(filename)
save(common.home_expand(filename))
end,
@ -482,8 +484,8 @@ local commands = {
core.error("Cannot rename unsaved doc")
return
end
core.command_view:set_text(old_filename)
core.command_view:enter("Rename", {
text = old_filename,
submit = function(filename)
save(common.home_expand(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))
found_expression = false
core.command_view:set_text(text, true)
core.status_view:show_tooltip(get_find_tooltip())
core.command_view:enter(label, {
text = text,
select_text = true,
show_suggestions = false,
submit = function(text, item)
insert_unique(core.previous_find, text)
@ -91,17 +92,18 @@ end
local function replace(kind, default, fn)
core.command_view:set_text(default, true)
core.status_view:show_tooltip(get_find_tooltip())
core.command_view:enter("Find To Replace " .. kind, {
text = default,
select_text = true,
show_suggestions = false,
submit = function(old)
insert_unique(core.previous_find, old)
core.command_view:set_text(old, true)
local s = string.format("Replace %s %q With", kind, old)
core.command_view:enter(s, {
text = old,
select_text = true,
show_suggestions = false,
submit = function(new)
core.status_view:remove_tooltip()

View File

@ -26,6 +26,8 @@ local default_state = {
suggest = noop,
cancel = noop,
validate = function() return true end,
text = "",
select_text = false,
show_suggestions = true,
typeahead = true,
wrap = true,
@ -146,14 +148,14 @@ function CommandView:submit()
end
function CommandView:enter(text, ...)
function CommandView:enter(label, ...)
if self.state ~= default_state then
return
end
local options = select(1, ...)
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 = ...
options = {
submit = submit,
@ -171,10 +173,25 @@ function CommandView:enter(text, ...)
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 = text .. ": "
self.label = label .. ": "
end

View File

@ -229,22 +229,20 @@ local function begin_search(text, fn)
end
local function set_command_view_text()
local function get_selected_text()
local view = core.active_view
local doc = (view and view.doc) and view.doc or nil
if doc then
core.command_view:set_text(
doc:get_text(table.unpack({ doc:get_selection() })),
true
)
return doc:get_text(table.unpack({ doc:get_selection() }))
end
end
command.add(nil, {
["project-search:find"] = function()
set_command_view_text()
core.command_view:enter("Find Text In Project", {
text = get_selected_text(),
select_text = true,
submit = function(text)
text = text:lower()
begin_search(text, function(line_text)
@ -266,8 +264,9 @@ command.add(nil, {
end,
["project-search:fuzzy-find"] = function()
set_command_view_text()
core.command_view:enter("Fuzzy Find Text In Project", {
text = get_selected_text(),
select_text = true,
submit = function(text)
begin_search(text, function(line_text)
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()
local old_filename = treeitem().filename
local old_abs_filename = treeitem().abs_filename
core.command_view:set_text(old_filename)
core.command_view:enter("Rename", {
text = old_filename,
submit = function(filename)
filename = core.normalize_to_project_dir(filename)
local abs_filename = core.project_absolute_path(filename)
@ -713,10 +713,12 @@ command.add(function() return treeitem() ~= nil end, {
end,
["treeview:new-file"] = function()
local text
if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/")
text = treeitem().filename .. "/"
end
core.command_view:enter("Filename", {
text = text,
submit = function(filename)
local doc_filename = core.project_dir .. PATHSEP .. filename
local file = io.open(doc_filename, "a+")
@ -730,10 +732,12 @@ command.add(function() return treeitem() ~= nil end, {
end,
["treeview:new-folder"] = function()
local text
if not is_project_folder(treeitem().abs_filename) then
core.command_view:set_text(treeitem().filename .. "/")
text = treeitem().filename .. "/"
end
core.command_view:enter("Folder Name", {
text = text,
submit = function(filename)
local dir_path = core.project_dir .. PATHSEP .. filename
common.mkdirp(dir_path)