Skip to content

Commit

Permalink
fix: escape magic characters in runtime module path patterns
Browse files Browse the repository at this point in the history
Primary use case is dashes in plugin file names, `ftplugin/my-plugin.fnl`. 

Co-authored-by: rktjmp <[email protected]>
  • Loading branch information
replcat and rktjmp authored Aug 15, 2024
1 parent 0ce9ae4 commit 1c35256
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion fnl/hotpot/loader/record/runtime.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
;; Convert /a/b/c/nvim/ftplugin/x.fnl into
;; /a/b/c/nvim/ <- context, the last path section defines our namespace
;; ftplugin/x.fnl <- inside context
context-pattern (fmt "(%s/%s%%.%s)$" runtime-type modname-suffix ext)
context-pattern (fmt "(%s/%s%%.%s)$"
runtime-type
;; escape regex pattern, e.g. plugin/x-y%.fnl -> plugin/x%-y%.fnl,
;; this is identical to vim.pesc except we preserve any '.' in the pattern.
(string.gsub modname-suffix "[%(%)%%%+%-%*%?%[%]%^%$]" "%%%1")
ext)
path-inside-context-dir (string.match src-path context-pattern)
path-to-context-dir (string.sub src-path 1 (* -1 (+ (length path-inside-context-dir) 1)))
;; ftplugin/y.fnl -> lua/hotpot-runtime-ftplugin/y.lua
Expand Down
4 changes: 2 additions & 2 deletions lua/hotpot/loader/record/runtime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local function new(modname_suffix, src_path, opts)
true_modname = (modname_suffix .. _5_())
local runtime_mod_prefix = fmt("hotpot-runtime-%s", runtime_type)
local modname = fmt("%s.%s", runtime_mod_prefix, modname_suffix)
local context_pattern = fmt("(%s/%s%%.%s)$", runtime_type, modname_suffix, ext)
local context_pattern = fmt("(%s/%s%%.%s)$", runtime_type, string.gsub(modname_suffix, "[%(%)%%%+%-%*%?%[%]%^%$]", "%%%1"), ext)
local path_inside_context_dir = string.match(src_path0, context_pattern)
local path_to_context_dir = string.sub(src_path0, 1, (-1 * (#path_inside_context_dir + 1)))
local lua_code_path = string.gsub(string.gsub(path_inside_context_dir, ("^" .. vim.pesc(runtime_type)), join_path("lua", runtime_mod_prefix)), "fnl$", "lua")
Expand Down Expand Up @@ -55,4 +55,4 @@ local function new(modname_suffix, src_path, opts)
end
return record
end
return {new = new}
return {new = new}
2 changes: 1 addition & 1 deletion test/test-nvim-runtime.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(values fnl-path lua-path)))

(local (plugin-path-1 lua-path-1) (make-plugin :my_plugin_1))
(local (plugin-path-2 lua-path-2) (make-plugin :nested/deeply/my_plugin_2))
(local (plugin-path-2 lua-path-2) (make-plugin :nested/deeply/my-plugin-2))
(local (plugin-path-3 lua-path-3) (make-plugin :init))
(local (plugin-path-4 lua-path-4) (make-plugin :init/init.fnl))
(local lua-paths [lua-path-1 lua-path-2 lua-path-3 lua-path-4])
Expand Down

0 comments on commit 1c35256

Please sign in to comment.