From fcf763fe9cffd23a1e4f42f0e3075ee7fe611f0f Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 10 Jan 2021 12:56:15 +0100 Subject: [PATCH] Fix problem when project directory has a trailing slash --- data/core/common.lua | 7 +++++++ data/core/init.lua | 28 ++++++++++++++++++---------- data/plugins/treeview.lua | 3 +-- data/plugins/workspace.lua | 4 ++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/data/core/common.lua b/data/core/common.lua index 3f2d309a..6918a2b5 100644 --- a/data/core/common.lua +++ b/data/core/common.lua @@ -196,6 +196,13 @@ function common.serialize(val) end +function common.basename(path) + -- a path should never end by / or \ except if it is '/' (unix root) or + -- 'X:\' (windows drive) + return path:match("[^\\/]+$") or path +end + + function common.home_encode(text) if HOME then local n = #HOME diff --git a/data/core/init.lua b/data/core/init.lua index 2b4dab7f..f022f5e2 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -31,8 +31,14 @@ local function save_session() end +local function normalize_path(s) + local drive, path = s:match("^([a-z]):([/\\].*)") + return drive and drive:upper() .. ":" .. path or s +end + + local function add_project_to_recents(dirname) - dirname = system.absolute_path(dirname) + dirname = normalize_path(system.absolute_path(dirname)) if not dirname then return end local recents = core.recent_projects local n = #recents @@ -53,11 +59,6 @@ function core.reschedule_project_scan() end -local function normalize_path(s) - local drive, path = s:match("^([a-z]):([/\\].*)") - return drive and drive:upper() .. ":" .. path or s -end - function core.set_project_dir(new_dir, change_project_fn) local chdir_ok = pcall(system.chdir, new_dir) if chdir_ok then @@ -86,6 +87,12 @@ local function strip_leading_path(filename) return filename:sub(2) end +local function strip_trailing_slash(filename) + if filename:match("[^:][/\\]$") then + return filename:sub(1, -2) + end + return filename +end local function project_scan_thread() local function diff_files(a, b) @@ -297,7 +304,7 @@ function core.add_project_directory(path) path = normalize_path(path) table.insert(core.project_directories, { name = path, - item = {filename = path:match("[^\\/]+$"), type = "dir", topdir = true}, + item = {filename = common.basename(path), type = "dir", topdir = true}, files = {} }) end @@ -335,15 +342,16 @@ function core.init() local project_dir = core.recent_projects[1] or "." local files = {} for i = 2, #ARGS do - local info = system.get_file_info(ARGS[i]) or {} + local arg_filename = strip_trailing_slash(ARGS[i]) + local info = system.get_file_info(arg_filename) or {} if info.type == "file" then - local file_abs = system.absolute_path(ARGS[i]) + local file_abs = system.absolute_path(arg_filename) if file_abs then table.insert(files, file_abs) project_dir = file_abs:match("^(.+)[/\\].+$") end elseif info.type == "dir" then - project_dir = ARGS[i] + project_dir = arg_filename end end diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 1d3ad345..71ea9869 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -38,7 +38,7 @@ function TreeView:get_cached(item, dirname) local t = dir_cache[item.filename] if not t then t = {} - local basename = item.filename:match("[^\\/]+$") + local basename = common.basename(item.filename) if item.topdir then t.filename = basename t.expanded = true @@ -145,7 +145,6 @@ end local function create_directory_in(item) local path = item.abs_filename - local basename = path:match("[^\\/]+$") core.command_view:enter("Create directory in " .. path, function(text) local dirname = path .. PATHSEP .. text local success, err = system.mkdir(dirname) diff --git a/data/plugins/workspace.lua b/data/plugins/workspace.lua index 92b8e78c..8712e52d 100644 --- a/data/plugins/workspace.lua +++ b/data/plugins/workspace.lua @@ -4,7 +4,7 @@ local DocView = require "core.docview" local function workspace_files_for(project_dir) - local basename = project_dir:match("[^\\/]+$") + local basename = common.basename(project_dir) local workspace_dir = USERDIR .. PATHSEP .. "ws" local info_wsdir = system.get_file_info(workspace_dir) if not info_wsdir then @@ -49,7 +49,7 @@ local function get_workspace_filename(project_dir) while id_list[id] do id = id + 1 end - local basename = project_dir:match("[^\\/]+$") + local basename = common.basename(project_dir) return USERDIR .. PATHSEP .. "ws" .. PATHSEP .. basename .. "-" .. tostring(id) end