From 687eb0b39f3bcad9566198b4c60bbd2755032991 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Mon, 31 May 2021 17:35:13 +0600 Subject: feat(startup): Source runtime/plugin/**/*.lua at startup For opt plugins these files are sourced on `:packadd` * `:runtime` Now can exexute lua files --- src/nvim/main.c | 11 ++++++++--- src/nvim/runtime.c | 33 +++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 56cd97f133..e626ad03db 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1367,7 +1367,8 @@ static void load_plugins(void) { if (p_lpl) { char_u *rtp_copy = NULL; - char_u *const plugin_pattern = (char_u *)"plugin/**/*.vim"; // NOLINT + char_u *const plugin_pattern_vim = (char_u *)"plugin/**/*.vim"; // NOLINT + char_u *const plugin_pattern_lua = (char_u *)"plugin/**/*.lua"; // NOLINT // First add all package directories to 'runtimepath', so that their // autoload directories can be found. Only if not done already with a @@ -1380,7 +1381,10 @@ static void load_plugins(void) } source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, - plugin_pattern, + plugin_pattern_vim, + DIP_ALL | DIP_NOAFTER); + source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, + plugin_pattern_lua, DIP_ALL | DIP_NOAFTER); TIME_MSG("loading plugins"); xfree(rtp_copy); @@ -1392,7 +1396,8 @@ static void load_plugins(void) } TIME_MSG("loading packages"); - source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER); + source_runtime(plugin_pattern_vim, DIP_ALL | DIP_AFTER); + source_runtime(plugin_pattern_lua, DIP_ALL | DIP_AFTER); TIME_MSG("loading after plugins"); } } diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index 1fb7e3b434..dadff42456 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -15,6 +15,7 @@ #include "nvim/misc1.h" #include "nvim/os/os.h" #include "nvim/runtime.h" +#include "nvim/lua/executor.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "runtime.c.generated.h" @@ -49,7 +50,11 @@ void ex_runtime(exarg_T *eap) static void source_callback(char_u *fname, void *cookie) { - (void)do_source(fname, false, DOSO_NONE); + if (path_with_extension((const char *)fname, "lua")) { + nlua_exec_file((const char *)fname); + } else { + (void)do_source(fname, false, DOSO_NONE); + } } /// Find the file "name" in all directories in "path" and invoke @@ -245,7 +250,8 @@ int source_in_path(char_u *path, char_u *name, int flags) return do_in_path_and_pp(path, name, flags, source_callback, NULL); } -// Expand wildcards in "pat" and invoke do_source() for each match. +// Expand wildcards in "pat" and invoke do_source()/nlua_exec_file() +// for each match. static void source_all_matches(char_u *pat) { int num_files; @@ -253,7 +259,11 @@ static void source_all_matches(char_u *pat) if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK) { for (int i = 0; i < num_files; i++) { - (void)do_source(files[i], false, DOSO_NONE); + if (path_with_extension((const char *)files[i], "lua")) { + nlua_exec_file((const char *)files[i]); + } else { + (void)do_source(files[i], false, DOSO_NONE); + } } FreeWild(num_files, files); } @@ -405,17 +415,15 @@ theend: /// Load scripts in "plugin" and "ftdetect" directories of the package. static int load_pack_plugin(char_u *fname) { - static const char *plugpat = "%s/plugin/**/*.vim"; // NOLINT static const char *ftpat = "%s/ftdetect/*.vim"; // NOLINT - int retval = FAIL; char *const ffname = fix_fname((char *)fname); size_t len = strlen(ffname) + STRLEN(ftpat); - char_u *pat = try_malloc(len + 1); - if (pat == NULL) { - goto theend; - } - vim_snprintf((char *)pat, len, plugpat, ffname); + char_u *pat = xmallocz(len); + + vim_snprintf((char *)pat, len, "%s/plugin/**/*.vim", ffname); + source_all_matches(pat); + vim_snprintf((char *)pat, len, "%s/plugin/**/*.lua", ffname); source_all_matches(pat); char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes"); @@ -430,12 +438,9 @@ static int load_pack_plugin(char_u *fname) } xfree(cmd); xfree(pat); - retval = OK; - -theend: xfree(ffname); - return retval; + return OK; } // used for "cookie" of add_pack_plugin() -- cgit From 1e6c02510afd79659519f2a69075b36784134322 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Wed, 2 Jun 2021 09:32:37 +0600 Subject: feat(runtime): Allow lua to be used in colorschemes * tests(runtime): move runtime/plugin tests to functional/lua/runtime_spec --- src/nvim/syntax.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index ed886ab7f9..ce81f26d38 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -6438,6 +6438,10 @@ int load_colors(char_u *name) apply_autocmds(EVENT_COLORSCHEMEPRE, name, curbuf->b_fname, false, curbuf); snprintf((char *)buf, buflen, "colors/%s.vim", name); retval = source_runtime(buf, DIP_START + DIP_OPT); + if (retval == FAIL) { + snprintf((char *)buf, buflen, "colors/%s.lua", name); + retval = source_runtime(buf, DIP_START + DIP_OPT); + } xfree(buf); apply_autocmds(EVENT_COLORSCHEME, name, curbuf->b_fname, FALSE, curbuf); -- cgit From 68be8b99cfb1ab6105c48707986ce409ca38dd35 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Wed, 2 Jun 2021 13:48:13 +0600 Subject: feat(runtime): Allow lua to be used in compiler --- src/nvim/ex_cmds2.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 56a14887df..3f35c41e7a 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -2421,6 +2421,7 @@ void ex_compiler(exarg_T *eap) if (*eap->arg == NUL) { // List all compiler scripts. do_cmdline_cmd("echo globpath(&rtp, 'compiler/*.vim')"); // NOLINT + do_cmdline_cmd("echo globpath(&rtp, 'compiler/*.lua')"); // NOLINT } else { size_t bufsize = STRLEN(eap->arg) + 14; buf = xmalloc(bufsize); @@ -2445,7 +2446,11 @@ void ex_compiler(exarg_T *eap) snprintf((char *)buf, bufsize, "compiler/%s.vim", eap->arg); if (source_in_path(p_rtp, buf, DIP_ALL) == FAIL) { - EMSG2(_("E666: compiler not supported: %s"), eap->arg); + // Try lua compiler + snprintf((char *)buf, bufsize, "compiler/%s.lua", eap->arg); + if (source_in_path(p_rtp, buf, DIP_ALL) == FAIL) { + EMSG2(_("E666: compiler not supported: %s"), eap->arg); + } } xfree(buf); -- cgit From f256a18fefeebe76edcca42f530b238e95bc25b6 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Wed, 2 Jun 2021 17:04:42 +0600 Subject: feat(runtime): Allow lua to be used in ftdetect --- src/nvim/runtime.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index dadff42456..22df4d985b 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -421,9 +421,9 @@ static int load_pack_plugin(char_u *fname) size_t len = strlen(ffname) + STRLEN(ftpat); char_u *pat = xmallocz(len); - vim_snprintf((char *)pat, len, "%s/plugin/**/*.vim", ffname); + vim_snprintf((char *)pat, len, "%s/plugin/**/*.vim", ffname); // NOLINT source_all_matches(pat); - vim_snprintf((char *)pat, len, "%s/plugin/**/*.lua", ffname); + vim_snprintf((char *)pat, len, "%s/plugin/**/*.lua", ffname); // NOLINT source_all_matches(pat); char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes"); @@ -434,6 +434,8 @@ static int load_pack_plugin(char_u *fname) do_cmdline_cmd("augroup filetypedetect"); vim_snprintf((char *)pat, len, ftpat, ffname); source_all_matches(pat); + vim_snprintf((char *)pat, len, "%s/ftdetect/*.lua", ffname); // NOLINT + source_all_matches(pat); do_cmdline_cmd("augroup END"); } xfree(cmd); -- cgit From 07c9b20c75d318dcdee9e5196bca12ee66d041ba Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Wed, 2 Jun 2021 19:39:24 +0600 Subject: enhance(runtime): Enable completion for lua files Enabled for `:colorscheme` `:compiler` filetype --- src/nvim/ex_getln.c | 32 ++++++++++++++++++++++++++++---- src/nvim/vim.h | 1 + 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 75ed5dc0e5..f63987136f 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -5115,11 +5115,12 @@ ExpandFromContext ( } if (xp->xp_context == EXPAND_COLORS) { char *directories[] = { "colors", NULL }; - return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, directories); + return ExpandRTDir(pat, DIP_START + DIP_OPT + DIP_LUA, num_file, file, + directories); } if (xp->xp_context == EXPAND_COMPILER) { char *directories[] = { "compiler", NULL }; - return ExpandRTDir(pat, 0, num_file, file, directories); + return ExpandRTDir(pat, DIP_LUA, num_file, file, directories); } if (xp->xp_context == EXPAND_OWNSYNTAX) { char *directories[] = { "syntax", NULL }; @@ -5127,7 +5128,7 @@ ExpandFromContext ( } if (xp->xp_context == EXPAND_FILETYPE) { char *directories[] = { "syntax", "indent", "ftplugin", NULL }; - return ExpandRTDir(pat, 0, num_file, file, directories); + return ExpandRTDir(pat, DIP_LUA, num_file, file, directories); } if (xp->xp_context == EXPAND_CHECKHEALTH) { char *directories[] = { "autoload/health", NULL }; @@ -5567,6 +5568,7 @@ static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file) /// 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim /// When "flags" has DIP_OPT: search also from 'opt' of 'packpath': /// 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim +/// When "flags" has DIP_LUA: search also performed for .lua files /// "dirnames" is an array with one or more directory names. static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirnames[]) @@ -5584,6 +5586,10 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *s = xmalloc(size); snprintf((char *)s, size, "%s/%s*.vim", dirnames[i], pat); globpath(p_rtp, s, &ga, 0); + if (flags & DIP_LUA) { + snprintf((char *)s, size, "%s/%s*.lua", dirnames[i], pat); + globpath(p_rtp, s, &ga, 0); + } xfree(s); } @@ -5593,6 +5599,10 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *s = xmalloc(size); snprintf((char *)s, size, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); // NOLINT globpath(p_pp, s, &ga, 0); + if (flags & DIP_LUA) { + snprintf((char *)s, size, "pack/*/start/*/%s/%s*.lua", dirnames[i], pat); // NOLINT + globpath(p_pp, s, &ga, 0); + } xfree(s); } @@ -5601,6 +5611,10 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *s = xmalloc(size); snprintf((char *)s, size, "start/*/%s/%s*.vim", dirnames[i], pat); // NOLINT globpath(p_pp, s, &ga, 0); + if (flags & DIP_LUA) { + snprintf((char *)s, size, "start/*/%s/%s*.lua", dirnames[i], pat); // NOLINT + globpath(p_pp, s, &ga, 0); + } xfree(s); } } @@ -5611,6 +5625,10 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *s = xmalloc(size); snprintf((char *)s, size, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); // NOLINT globpath(p_pp, s, &ga, 0); + if (flags & DIP_LUA) { + snprintf((char *)s, size, "pack/*/opt/*/%s/%s*.lua", dirnames[i], pat); // NOLINT + globpath(p_pp, s, &ga, 0); + } xfree(s); } @@ -5619,6 +5637,10 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *s = xmalloc(size); snprintf((char *)s, size, "opt/*/%s/%s*.vim", dirnames[i], pat); // NOLINT globpath(p_pp, s, &ga, 0); + if (flags & DIP_LUA) { + snprintf((char *)s, size, "opt/*/%s/%s*.lua", dirnames[i], pat); // NOLINT + globpath(p_pp, s, &ga, 0); + } xfree(s); } } @@ -5627,7 +5649,9 @@ static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char_u *match = ((char_u **)ga.ga_data)[i]; char_u *s = match; char_u *e = s + STRLEN(s); - if (e - s > 4 && STRNICMP(e - 4, ".vim", 4) == 0) { + if (e - s > 4 && (STRNICMP(e - 4, ".vim", 4) == 0 + || ((flags & DIP_LUA) + && STRNICMP(e - 4, ".lua", 4) == 0))) { e -= 4; for (s = e; s > match; MB_PTR_BACK(match, s)) { if (vim_ispathsep(*s)) { diff --git a/src/nvim/vim.h b/src/nvim/vim.h index 0245c472ef..df4ab04eb6 100644 --- a/src/nvim/vim.h +++ b/src/nvim/vim.h @@ -313,6 +313,7 @@ enum { FOLD_TEXT_LEN = 51 }; //!< buffer size for get_foldtext() #define DIP_NORTP 0x20 // do not use 'runtimepath' #define DIP_NOAFTER 0x40 // skip "after" directories #define DIP_AFTER 0x80 // only use "after" directories +#define DIP_LUA 0x100 // also use ".lua" files // Lowest number used for window ID. Cannot have this many windows per tab. #define LOWEST_WIN_ID 1000 -- cgit From e1edc079dd0d0cb4a53e5998086568cf9d10a26a Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Thu, 3 Jun 2021 07:07:51 +0600 Subject: refactor(source): Move lua file detection to do_source So now :source can run lua files too :) * feat: Add support for :[ranged]source for lua files --- src/nvim/ex_cmds2.c | 18 +++++++++++++++--- src/nvim/lua/executor.c | 18 ++++++++++++++++++ src/nvim/main.c | 14 +++----------- src/nvim/runtime.c | 13 ++----------- 4 files changed, 38 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 3f35c41e7a..4798e93b91 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -27,6 +27,7 @@ #include "nvim/ex_getln.h" #include "nvim/fileio.h" #include "nvim/getchar.h" +#include "nvim/globals.h" #include "nvim/mark.h" #include "nvim/mbyte.h" #include "nvim/memline.h" @@ -53,6 +54,7 @@ #include "nvim/os/fs_defs.h" #include "nvim/api/private/helpers.h" #include "nvim/api/private/defs.h" +#include "nvim/lua/executor.h" /// Growarray to store info about already sourced scripts. @@ -2661,8 +2663,13 @@ static void cmd_source_buffer(const exarg_T *eap) .curr_lnum = eap->line1, .final_lnum = eap->line2, }; - source_using_linegetter((void *)&cookie, get_buffer_line, - ":source (no file)"); + if (curbuf != NULL && curbuf->b_fname + && path_with_extension((const char *)curbuf->b_fname, "lua")) { + nlua_source_using_linegetter(get_buffer_line, (void *)&cookie, ":source"); + } else { + source_using_linegetter((void *)&cookie, get_buffer_line, + ":source (no file)"); + } } /// ":source" and associated commands. @@ -2774,7 +2781,8 @@ int do_source_str(const char *cmd, const char *traceback_name) return source_using_linegetter((void *)&cookie, get_str_line, traceback_name); } -/// Reads the file `fname` and executes its lines as Ex commands. +/// When fname is a 'lua' file nlua_exec_file() is invoked to source it. +/// Otherwise reads the file `fname` and executes its lines as Ex commands. /// /// This function may be called recursively! /// @@ -2801,6 +2809,10 @@ int do_source(char_u *fname, int check_other, int is_vimrc) proftime_T wait_start; bool trigger_source_post = false; + if (path_with_extension((const char *)fname, "lua")) { + return (int)nlua_exec_file((const char *)fname); + } + p = expand_env_save(fname); if (p == NULL) { return retval; diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 0a52cc16cb..afc387ef38 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1161,6 +1161,24 @@ static void nlua_typval_exec(const char *lcmd, size_t lcmd_len, } } +int nlua_source_using_linegetter(LineGetter fgetline, + void *cookie, char *name) +{ + garray_T ga; + char_u *line = NULL; + + ga_init(&ga, (int)sizeof(char_u *), 10); + while ((line = fgetline(0, cookie, 0, false)) != NULL) { + GA_APPEND(char_u *, &ga, line); + } + char *code = (char *)ga_concat_strings_sep(&ga, "\n"); + size_t len = strlen(code); + nlua_typval_exec(code, len, name, NULL, 0, false, NULL); + ga_clear_strings(&ga); + xfree(code); + return OK; +} + /// Call a LuaCallable given some typvals /// /// Used to call any lua callable passed from Lua into VimL diff --git a/src/nvim/main.c b/src/nvim/main.c index e626ad03db..53043d293e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1101,11 +1101,7 @@ static void command_line_scan(mparm_T *parmp) size_t s_size = STRLEN(a) + 9; char *s = xmalloc(s_size); - if (path_with_extension(a, "lua")) { - snprintf(s, s_size, "luafile %s", a); - } else { - snprintf(s, s_size, "so %s", a); - } + snprintf(s, s_size, "so %s", a); parmp->cmds_tofree[parmp->n_commands] = true; parmp->commands[parmp->n_commands++] = s; } else { @@ -1888,12 +1884,8 @@ static void source_startup_scripts(const mparm_T *const parmp) || strequal(parmp->use_vimrc, "NORC")) { // Do nothing. } else { - if (path_with_extension(parmp->use_vimrc, "lua")) { - nlua_exec_file(parmp->use_vimrc); - } else { - if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) { - EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); - } + if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) { + EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); } } } else if (!silent_mode) { diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index 22df4d985b..c3cd210538 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -15,7 +15,6 @@ #include "nvim/misc1.h" #include "nvim/os/os.h" #include "nvim/runtime.h" -#include "nvim/lua/executor.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "runtime.c.generated.h" @@ -50,11 +49,7 @@ void ex_runtime(exarg_T *eap) static void source_callback(char_u *fname, void *cookie) { - if (path_with_extension((const char *)fname, "lua")) { - nlua_exec_file((const char *)fname); - } else { - (void)do_source(fname, false, DOSO_NONE); - } + (void)do_source(fname, false, DOSO_NONE); } /// Find the file "name" in all directories in "path" and invoke @@ -259,11 +254,7 @@ static void source_all_matches(char_u *pat) if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK) { for (int i = 0; i < num_files; i++) { - if (path_with_extension((const char *)files[i], "lua")) { - nlua_exec_file((const char *)files[i]); - } else { - (void)do_source(files[i], false, DOSO_NONE); - } + (void)do_source(files[i], false, DOSO_NONE); } FreeWild(num_files, files); } -- cgit