aboutsummaryrefslogtreecommitdiff
path: root/data/core/commands
diff options
context:
space:
mode:
authorjgmdev <jgmdev@gmail.com>2023-08-08 02:27:02 -0400
committerjgmdev <jgmdev@gmail.com>2023-08-08 02:27:02 -0400
commit47bfcb07bb1db489f97d2fa4a6c0432905eb87c0 (patch)
tree3b067adf4cc5b02c90d24ef20dc57f4a68406a0e /data/core/commands
parenta9ee42b2267241fd3273052822904aba8a593cb8 (diff)
downloadpragtical-47bfcb07bb1db489f97d2fa4a6c0432905eb87c0.tar.gz
pragtical-47bfcb07bb1db489f97d2fa4a6c0432905eb87c0.zip
Fix core:open-file to not be dependant on chdir
Diffstat (limited to 'data/core/commands')
-rw-r--r--data/core/commands/core.lua58
1 files changed, 33 insertions, 25 deletions
diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua
index 3bfab450..e86e967e 100644
--- a/data/core/commands/core.lua
+++ b/data/core/commands/core.lua
@@ -100,42 +100,50 @@ command.add(nil, {
["core:open-file"] = function()
local view = core.active_view
- local text
+ local text, root_dir, filename = "", core.root_project().path, ""
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)
- text = dirname == core.root_project().path and "" or common.home_encode(dirname) .. PATHSEP
+ local dirname = common.dirname(view.doc.abs_filename)
+ if dirname and common.path_belongs_to(dirname, root_dir) then
+ local dirname = core.normalize_to_project_dir(dirname)
+ text = dirname == root_dir and "" or common.home_encode(dirname) .. PATHSEP
+ elseif dirname then
+ root_dir = dirname
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))
end,
- suggest = function (text)
- return common.home_encode_list(common.path_suggest(common.home_expand(text)))
- end,
+ suggest = function(text)
+ return common.home_encode_list(
+ common.path_suggest(common.home_expand(text), root_dir)
+ )
+ end,
validate = function(text)
- local filename = common.home_expand(text)
- local path_stat, err = system.get_file_info(filename)
- if err then
- if err:find("No such file", 1, true) then
- -- check if the containing directory exists
- local dirname = common.dirname(filename)
- local dir_stat = dirname and system.get_file_info(dirname)
- if not dirname or (dir_stat and dir_stat.type == 'dir') then
- return true
- end
+ filename = root_dir == core.root_project().path and
+ core.root_project():absolute_path(
+ common.home_expand(text)
+ ) or system.absolute_path(
+ common.home_expand(root_dir .. PATHSEP .. text)
+ )
+ local path_stat, err = system.get_file_info(filename)
+ if err then
+ if err:find("No such file", 1, true) then
+ -- check if the containing directory exists
+ local dirname = common.dirname(filename)
+ local dir_stat = dirname and system.get_file_info(dirname)
+ if not dirname or (dir_stat and dir_stat.type == 'dir') then
+ return true
end
- core.error("Cannot open file %s: %s", text, err)
- elseif path_stat.type == 'dir' then
- core.error("Cannot open %s, is a folder", text)
- else
- return true
end
- end,
+ core.error("Cannot open file %s: %s", text, err)
+ elseif path_stat.type == 'dir' then
+ core.error("Cannot open %s, is a folder", text)
+ else
+ return true
+ end
+ end,
})
end,