Fix problem when project directory has a trailing slash

This commit is contained in:
Francesco Abbate 2021-01-10 12:56:15 +01:00
parent 7f9e4e4984
commit fcf763fe9c
4 changed files with 28 additions and 14 deletions

View File

@ -196,6 +196,13 @@ function common.serialize(val)
end 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) function common.home_encode(text)
if HOME then if HOME then
local n = #HOME local n = #HOME

View File

@ -31,8 +31,14 @@ local function save_session()
end 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) 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 if not dirname then return end
local recents = core.recent_projects local recents = core.recent_projects
local n = #recents local n = #recents
@ -53,11 +59,6 @@ function core.reschedule_project_scan()
end 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) function core.set_project_dir(new_dir, change_project_fn)
local chdir_ok = pcall(system.chdir, new_dir) local chdir_ok = pcall(system.chdir, new_dir)
if chdir_ok then if chdir_ok then
@ -86,6 +87,12 @@ local function strip_leading_path(filename)
return filename:sub(2) return filename:sub(2)
end 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 project_scan_thread()
local function diff_files(a, b) local function diff_files(a, b)
@ -297,7 +304,7 @@ function core.add_project_directory(path)
path = normalize_path(path) path = normalize_path(path)
table.insert(core.project_directories, { table.insert(core.project_directories, {
name = path, name = path,
item = {filename = path:match("[^\\/]+$"), type = "dir", topdir = true}, item = {filename = common.basename(path), type = "dir", topdir = true},
files = {} files = {}
}) })
end end
@ -335,15 +342,16 @@ function core.init()
local project_dir = core.recent_projects[1] or "." local project_dir = core.recent_projects[1] or "."
local files = {} local files = {}
for i = 2, #ARGS do 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 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 if file_abs then
table.insert(files, file_abs) table.insert(files, file_abs)
project_dir = file_abs:match("^(.+)[/\\].+$") project_dir = file_abs:match("^(.+)[/\\].+$")
end end
elseif info.type == "dir" then elseif info.type == "dir" then
project_dir = ARGS[i] project_dir = arg_filename
end end
end end

View File

@ -38,7 +38,7 @@ function TreeView:get_cached(item, dirname)
local t = dir_cache[item.filename] local t = dir_cache[item.filename]
if not t then if not t then
t = {} t = {}
local basename = item.filename:match("[^\\/]+$") local basename = common.basename(item.filename)
if item.topdir then if item.topdir then
t.filename = basename t.filename = basename
t.expanded = true t.expanded = true
@ -145,7 +145,6 @@ end
local function create_directory_in(item) local function create_directory_in(item)
local path = item.abs_filename local path = item.abs_filename
local basename = path:match("[^\\/]+$")
core.command_view:enter("Create directory in " .. path, function(text) core.command_view:enter("Create directory in " .. path, function(text)
local dirname = path .. PATHSEP .. text local dirname = path .. PATHSEP .. text
local success, err = system.mkdir(dirname) local success, err = system.mkdir(dirname)

View File

@ -4,7 +4,7 @@ local DocView = require "core.docview"
local function workspace_files_for(project_dir) 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 workspace_dir = USERDIR .. PATHSEP .. "ws"
local info_wsdir = system.get_file_info(workspace_dir) local info_wsdir = system.get_file_info(workspace_dir)
if not info_wsdir then if not info_wsdir then
@ -49,7 +49,7 @@ local function get_workspace_filename(project_dir)
while id_list[id] do while id_list[id] do
id = id + 1 id = id + 1
end end
local basename = project_dir:match("[^\\/]+$") local basename = common.basename(project_dir)
return USERDIR .. PATHSEP .. "ws" .. PATHSEP .. basename .. "-" .. tostring(id) return USERDIR .. PATHSEP .. "ws" .. PATHSEP .. basename .. "-" .. tostring(id)
end end