Skip to content

Commit

Permalink
fix: resolve "no such file or directory" error in scratchpad (mistwea…
Browse files Browse the repository at this point in the history
…verco#254)

* fix(parser): resolve "no such file or directory" error in scratchpad

* fix(scratchpad): use vim.loop.cwd() for script.cwd

* fix(scratchpad): also handle external files

---------

Co-authored-by: Marco Kellershoff <[email protected]>
  • Loading branch information
2 people authored and iamxiaojianzheng committed Oct 24, 2024
1 parent 7916068 commit b7b4639
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local M = {}

local plugin_tmp_dir = FS.get_plugin_tmp_dir()

M.VERSION = "4.0.0"
M.VERSION = "4.0.1"
M.UI_ID = "kulala://ui"
M.SCRATCHPAD_ID = "kulala://scratchpad"
M.HEADERS_FILE = plugin_tmp_dir .. "/headers.txt"
Expand Down
10 changes: 7 additions & 3 deletions lua/kulala/parser/scripts/engines/javascript/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ local generate_one = function(script_type, is_external_file, script_data)
return nil, nil
end
local script_cwd
-- buf_dir is "kulala:" when the buffer is scratch buffer
-- in this case, use current working directory for script_cwd and base_dir
local buf_dir = FS.get_current_buffer_dir()

if is_external_file then
-- if script_data starts with ./ or ../, it is a relative path
if string.match(script_data, "^%./") or string.match(script_data, "^%../") then
local local_script_path = script_data:gsub("^%./", "")
script_data = FS.join_paths(FS.get_current_buffer_dir(), local_script_path)
local base_dir = buf_dir == "kulala:" and vim.loop.cwd() or buf_dir
script_data = FS.join_paths(base_dir, local_script_path)
end
script_cwd = FS.get_dir_by_filepath(script_data)
script_cwd = buf_dir == "kulala:" and vim.loop.cwd() or FS.get_dir_by_filepath(script_data)
userscript = FS.read_file(script_data)
else
script_cwd = FS.get_current_buffer_dir()
script_cwd = buf_dir == "kulala:" and vim.loop.cwd() or buf_dir
userscript = vim.fn.join(script_data, "\n")
end
base_file = base_file .. "\n" .. userscript
Expand Down

0 comments on commit b7b4639

Please sign in to comment.