From 4ae0d477c062f9dbc043a308e05fd3553b42c783 Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 17 May 2020 16:59:19 +0100 Subject: [PATCH] Made lite set project dir to CWD; removed core.project_dir Fixes #100 --- data/core/commands/core.lua | 6 +++--- data/core/init.lua | 15 ++++++--------- data/plugins/treeview.lua | 5 ++--- src/api/system.c | 10 ++++++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 5c9d6227..e30fe90a 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -56,13 +56,13 @@ command.add(nil, { ["core:file-finder"] = function() core.command_view:enter("Open File From Project", function(text, item) - text = core.project_dir .. PATHSEP .. (item and item.text or text) + text = item and item.text or text core.root_view:open_doc(core.open_doc(text)) end, function(text) local files = {} for _, item in pairs(core.project_files) do if item.type == "file" then - table.insert(files, item.filename:sub(#core.project_dir + 2)) + table.insert(files, item.filename) end end return common.fuzzy_match(files, text) @@ -89,7 +89,7 @@ command.add(nil, { end, ["core:open-project-module"] = function() - local filename = core.project_dir .. "/.lite_project.lua" + local filename = ".lite_project.lua" if system.get_file_info(filename) then core.root_view:open_doc(core.open_doc(filename)) else diff --git a/data/core/init.lua b/data/core/init.lua index 777dd4e4..af88126b 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -36,7 +36,7 @@ local function project_scan_thread() for _, file in ipairs(all) do if not common.match_pattern(file, config.ignore_files) then - local file = path .. PATHSEP .. file + local file = (path ~= "." and path .. PATHSEP or "") .. file local info = system.get_file_info(file) if info and info.size < size_limit then info.filename = file @@ -62,7 +62,7 @@ local function project_scan_thread() while true do -- get project files and replace previous table if the new table is -- different - local t = get_files(core.project_dir) + local t = get_files(".") if diff_files(core.project_files, t) then core.project_files = t core.redraw = true @@ -88,12 +88,6 @@ function core.init() core.docs = {} core.threads = setmetatable({}, { __mode = "k" }) core.project_files = {} - core.project_dir = "." - - local info = ARGS[2] and system.get_file_info(ARGS[2]) - if info and info.type == "dir" then - core.project_dir = ARGS[2]:gsub("[\\/]$", "") - end core.root_view = RootView() core.command_view = CommandView() @@ -120,6 +114,9 @@ function core.init() if got_plugin_error or got_user_error or got_project_error then command.perform("core:open-log") end + + local info = ARGS[2] and system.get_file_info(ARGS[2]) + system.chdir(info and info.type == "dir" and ARGS[2] or ".") end @@ -166,7 +163,7 @@ end function core.load_project_module() - local filename = core.project_dir .. "/.lite_project.lua" + local filename = ".lite_project.lua" if system.get_file_info(filename) then return core.try(function() local fn, err = loadfile(filename) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 2007d707..2c2460d4 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -35,7 +35,7 @@ function TreeView:get_cached(item) t = {} t.filename = item.filename t.abs_filename = system.absolute_path(item.filename) - t.path, t.name = t.filename:match("^(.*)[\\/](.+)$") + t.name = t.filename:match("[^\\/]+$") t.depth = get_depth(t.filename) t.type = item.type self.cache[t.filename] = t @@ -143,7 +143,6 @@ function TreeView:draw() local icon_width = style.icon_font:get_width("D") local spacing = style.font:get_width(" ") * 2 - local root_depth = get_depth(core.project_dir) + 1 local doc = core.active_view.doc local active_filename = doc and system.absolute_path(doc.filename or "") @@ -163,7 +162,7 @@ function TreeView:draw() end -- icons - x = x + (item.depth - root_depth) * style.padding.x + style.padding.x + x = x + item.depth * style.padding.x + style.padding.x if item.type == "dir" then local icon1 = item.expanded and "-" or "+" local icon2 = item.expanded and "D" or "d" diff --git a/src/api/system.c b/src/api/system.c index cbe8810a..bacafcb5 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include "api.h" @@ -216,6 +217,14 @@ static int f_show_confirm_dialog(lua_State *L) { } +static int f_chdir(lua_State *L) { + const char *path = luaL_checkstring(L, 1); + int err = chdir(path); + if (err) { luaL_error(L, "chdir() failed"); } + return 0; +} + + static int f_list_dir(lua_State *L) { const char *path = luaL_checkstring(L, 1); @@ -370,6 +379,7 @@ static const luaL_Reg lib[] = { { "set_window_mode", f_set_window_mode }, { "window_has_focus", f_window_has_focus }, { "show_confirm_dialog", f_show_confirm_dialog }, + { "chdir", f_chdir }, { "list_dir", f_list_dir }, { "absolute_path", f_absolute_path }, { "get_file_info", f_get_file_info },