aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2020-11-24 14:18:46 +0100
committerBjörn Linse <bjorn.linse@gmail.com>2020-11-24 14:18:46 +0100
commitd285fa73da7ce2cb9abfbbb423eb8506e7202411 (patch)
tree900f9e0b6ded0bba865106d6c1cf367b06145aed
parent7dfeadb0fcbcedeafc357e60f0b7b2b8916afeaf (diff)
downloadrneovim-d285fa73da7ce2cb9abfbbb423eb8506e7202411.tar.gz
rneovim-d285fa73da7ce2cb9abfbbb423eb8506e7202411.tar.bz2
rneovim-d285fa73da7ce2cb9abfbbb423eb8506e7202411.zip
api: enable nvim_get_runtime_file to find subdirectories
-rw-r--r--src/nvim/api/vim.c18
-rw-r--r--test/functional/api/vim_spec.lua31
2 files changed, 34 insertions, 15 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 710885d295..77002697fe 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -788,10 +788,15 @@ ArrayOf(String) nvim_list_runtime_paths(void)
///
/// 'name' can contain wildcards. For example
/// nvim_get_runtime_file("colors/*.vim", true) will return all color
-/// scheme files.
+/// scheme files. Always use forward slashes (/) in the search pattern for
+/// subdirectories regardless of platform.
///
/// It is not an error to not find any files. An empty array is returned then.
///
+/// To find a directory, `name` must end with a forward slash, like
+/// "rplugin/python/". Without the slash it would instead look for an ordinary
+/// file called "rplugin/python".
+///
/// @param name pattern of files to search for
/// @param all whether to return all matches or only the first
/// @return list of absolute paths to the found files
@@ -801,14 +806,13 @@ ArrayOf(String) nvim_get_runtime_file(String name, Boolean all, Error *err)
{
Array rv = ARRAY_DICT_INIT;
- // TODO(bfredl):
- if (name.size == 0) {
- api_set_error(err, kErrorTypeValidation, "not yet implemented");
- return rv;
+ int flags = DIP_START | (all ? DIP_ALL : 0);
+
+ if (name.size == 0 || name.data[name.size-1] == '/') {
+ flags |= DIP_DIR;
}
- int flags = DIP_START | (all ? DIP_ALL : 0);
- do_in_runtimepath((char_u *)name.data,
+ do_in_runtimepath((char_u *)(name.size ? name.data : ""),
flags, find_runtime_cb, &rv);
return rv;
}
diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua
index 7948f7da09..eb5fd7eca7 100644
--- a/test/functional/api/vim_spec.lua
+++ b/test/functional/api/vim_spec.lua
@@ -1885,25 +1885,40 @@ describe('API', function()
end)
describe('nvim_get_runtime_file', function()
- it('works', function()
+ local p = helpers.alter_slashes
+ it('can find files', function()
eq({}, meths.get_runtime_file("bork.borkbork", false))
eq({}, meths.get_runtime_file("bork.borkbork", true))
eq(1, #meths.get_runtime_file("autoload/msgpack.vim", false))
eq(1, #meths.get_runtime_file("autoload/msgpack.vim", true))
local val = meths.get_runtime_file("autoload/remote/*.vim", true)
eq(2, #val)
- local p = helpers.alter_slashes
if endswith(val[1], "define.vim") then
- ok(endswith(val[1], p("autoload/remote/define.vim")))
- ok(endswith(val[2], p("autoload/remote/host.vim")))
+ ok(endswith(val[1], p"autoload/remote/define.vim"))
+ ok(endswith(val[2], p"autoload/remote/host.vim"))
else
- ok(endswith(val[1], p("autoload/remote/host.vim")))
- ok(endswith(val[2], p("autoload/remote/define.vim")))
+ ok(endswith(val[1], p"autoload/remote/host.vim"))
+ ok(endswith(val[2], p"autoload/remote/define.vim"))
end
val = meths.get_runtime_file("autoload/remote/*.vim", false)
eq(1, #val)
- ok(endswith(val[1], p("autoload/remote/define.vim"))
- or endswith(val[1], p("autoload/remote/host.vim")))
+ ok(endswith(val[1], p"autoload/remote/define.vim")
+ or endswith(val[1], p"autoload/remote/host.vim"))
+
+ eq({}, meths.get_runtime_file("lua", true))
+ eq({}, meths.get_runtime_file("lua/vim", true))
+ end)
+
+ it('can find directories', function()
+ local val = meths.get_runtime_file("lua/", true)
+ eq(1, #val)
+ ok(endswith(val[1], p"lua/"))
+
+ val = meths.get_runtime_file("lua/vim/", true)
+ eq(1, #val)
+ ok(endswith(val[1], p"lua/vim/"))
+
+ eq({}, meths.get_runtime_file("foobarlang/", true))
end)
end)
end)