From 3fe39cb283f860c7244589362a903c90b1014e0f Mon Sep 17 00:00:00 2001 From: L3MON4D3 Date: Mon, 29 May 2023 17:56:31 +0000 Subject: [PATCH] Format with stylua --- lua/luasnip/extras/snip_location.lua | 9 ++-- lua/luasnip/loaders/from_vscode.lua | 47 ++++++++++++------- lua/luasnip/loaders/init.lua | 16 +++++-- lua/luasnip/nodes/duplicate.lua | 17 ++++--- lua/luasnip/nodes/multiSnippet.lua | 14 ++++-- lua/luasnip/nodes/snippetProxy.lua | 10 +++- .../session/snippet_collection/init.lua | 3 +- tests/helpers.lua | 3 +- tests/integration/add_snippets_spec.lua | 12 +++-- tests/integration/loaders_spec.lua | 6 ++- 10 files changed, 90 insertions(+), 47 deletions(-) diff --git a/lua/luasnip/extras/snip_location.lua b/lua/luasnip/extras/snip_location.lua index b60f8c443..cb4618ad6 100644 --- a/lua/luasnip/extras/snip_location.lua +++ b/lua/luasnip/extras/snip_location.lua @@ -114,7 +114,11 @@ function M.jump_to_snippet(snip, opts) end local fcall_range - local ft = util.ternary(vim.bo[0].filetype ~= "", vim.bo[0].filetype, vim.api.nvim_buf_get_name(0):match("%.([^%.]+)$")) + local ft = util.ternary( + vim.bo[0].filetype ~= "", + vim.bo[0].filetype, + vim.api.nvim_buf_get_name(0):match("%.([^%.]+)$") + ) if ft == "lua" then if source.line then -- in lua-file, can get region of definition via treesitter. @@ -137,8 +141,7 @@ function M.jump_to_snippet(snip, opts) -- matches *.json or *.jsonc. elseif ft == "json" or ft == "jsonc" then local ok - ok, fcall_range = - pcall(json_find_snippet_definition, 0, ft, snip.name) + ok, fcall_range = pcall(json_find_snippet_definition, 0, ft, snip.name) if not ok then print( "Could not determine range of snippet-definition: " diff --git a/lua/luasnip/loaders/from_vscode.lua b/lua/luasnip/loaders/from_vscode.lua index 4dafc8271..48682d863 100644 --- a/lua/luasnip/loaders/from_vscode.lua +++ b/lua/luasnip/loaders/from_vscode.lua @@ -14,7 +14,7 @@ local duplicate = require("luasnip.nodes.duplicate") local json_decoders = { json = util.json_decode, jsonc = require("luasnip.util.jsonc").decode, - ["code-snippets"] = require("luasnip.util.jsonc").decode + ["code-snippets"] = require("luasnip.util.jsonc").decode, } local function read_json(fname) @@ -69,7 +69,7 @@ local function get_file_snippets(file) dscr = parts.description or name, wordTrig = ls_conf.wordTrig, priority = ls_conf.priority, - snippetType = ls_conf.autotrigger and "autosnippet" or "snippet" + snippetType = ls_conf.autotrigger and "autosnippet" or "snippet", } -- Sometimes it's a list of prefixes instead of a single one @@ -78,12 +78,16 @@ local function get_file_snippets(file) -- vscode documents `,`, but `.` also works. -- an entry `false` in this list will cause a `ft=nil` for the snippet. - local filetypes = parts.scope and vim.split(parts.scope, "[.,]") or {false} + local filetypes = parts.scope and vim.split(parts.scope, "[.,]") + or { false } local contexts = {} for _, prefix in ipairs(prefixes) do for _, filetype in ipairs(filetypes) do - table.insert(contexts, {filetype = filetype or nil, trig = prefix}) + table.insert( + contexts, + { filetype = filetype or nil, trig = prefix } + ) end end @@ -117,12 +121,15 @@ end -- `force_reload`: don't use cache when reloading, default false local function load_snippet_file(file, filetype, add_opts, opts) opts = opts or {} - local refresh_notify = util.ternary(opts.refresh_notify ~= nil, opts.refresh_notify, false) - local force_reload = util.ternary(opts.force_reload ~= nil, opts.force_reload, false) + local refresh_notify = + util.ternary(opts.refresh_notify ~= nil, opts.refresh_notify, false) + local force_reload = + util.ternary(opts.force_reload ~= nil, opts.force_reload, false) if not Path.exists(file) then log.error( - "Trying to read snippets from file %s, but it does not exist.", file + "Trying to read snippets from file %s, but it does not exist.", + file ) return end @@ -151,11 +158,7 @@ local function load_snippet_file(file, filetype, add_opts, opts) refresh_notify = refresh_notify, }, add_opts) ) - log.info( - "Adding %s snippets from %s", - #file_snippets, - file - ) + log.info("Adding %s snippets from %s", #file_snippets, file) end --- Find all files+associated filetypes in a package. @@ -274,7 +277,7 @@ local function update_cache(cache, file, filetype, add_opts) if not filecache then filecache = { filetype_add_opts = {}, - filetypes = {} + filetypes = {}, } cache.path_snippets[file] = filecache end @@ -299,7 +302,7 @@ function M.load(opts) update_cache(package_cache, file, ft, add_opts) -- `false`: don't refresh while adding. - load_snippet_file(file, ft, add_opts, {refresh_notify = false}) + load_snippet_file(file, ft, add_opts, { refresh_notify = false }) end ls.refresh_notify(ft) end @@ -311,7 +314,7 @@ function M._load_lazy_loaded_ft(ft) file, ft, package_cache.path_snippets[file].filetype_add_opts[ft], - {refresh_notify = false} + { refresh_notify = false } ) end ls.refresh_notify(ft) @@ -352,7 +355,12 @@ function M.lazy_load(opts) if package_cache.lazy_loaded_ft[ft] then for _, file in ipairs(files) do -- instantly load snippets if they were already loaded... - load_snippet_file(file, ft, add_opts, {refresh_notify = false}) + load_snippet_file( + file, + ft, + add_opts, + { refresh_notify = false } + ) log.info( "Immediately loading lazy-load-snippets for already-active filetype %s from files:\n%s", ft, @@ -426,7 +434,12 @@ function M._reload_file(filename) -- just use its snippets. local force_reload = true for ft, _ in pairs(package_cached_data.filetypes) do - load_snippet_file(filename, ft, package_cached_data.filetype_add_opts[ft], {force_reload = force_reload}) + load_snippet_file( + filename, + ft, + package_cached_data.filetype_add_opts[ft], + { force_reload = force_reload } + ) -- only force-reload once, then reuse updated snippets. force_reload = false end diff --git a/lua/luasnip/loaders/init.lua b/lua/luasnip/loaders/init.lua index 9cd5cc2b4..14078aa32 100644 --- a/lua/luasnip/loaders/init.lua +++ b/lua/luasnip/loaders/init.lua @@ -10,7 +10,7 @@ local clean_name = { vscode_packages = "vscode", vscode_standalone = "vscode-standalone", snipmate = "snipmate", - lua = "lua" + lua = "lua", } local function default_format(path, _) path = path:gsub( @@ -46,7 +46,7 @@ function M.edit_snippet_files(opts) opts = opts or {} local format = opts.format or default_format local edit = opts.edit or default_edit - local extend = opts.extend or function(_,_) + local extend = opts.extend or function(_, _) return {} end @@ -56,7 +56,12 @@ function M.edit_snippet_files(opts) local items = {} -- concat files from all loaders for the selected filetype ft. - for _, cache_name in ipairs({ "vscode_packages", "vscode_standalone", "snipmate", "lua" }) do + for _, cache_name in ipairs({ + "vscode_packages", + "vscode_standalone", + "snipmate", + "lua", + }) do for _, path in ipairs(Cache[cache_name].ft_paths[ft] or {}) do local fmt_name = format(path, clean_name[cache_name]) if fmt_name then @@ -96,7 +101,10 @@ function M.edit_snippet_files(opts) local all_fts = {} vim.list_extend(all_fts, util.get_snippet_filetypes()) - vim.list_extend(all_fts, loader_util.get_load_fts(vim.api.nvim_get_current_buf())) + vim.list_extend( + all_fts, + loader_util.get_load_fts(vim.api.nvim_get_current_buf()) + ) all_fts = util.deduplicate(all_fts) local filtered_fts = {} diff --git a/lua/luasnip/nodes/duplicate.lua b/lua/luasnip/nodes/duplicate.lua index ee2ca4c23..1f3357903 100644 --- a/lua/luasnip/nodes/duplicate.lua +++ b/lua/luasnip/nodes/duplicate.lua @@ -5,7 +5,9 @@ local M = {} local DupExpandable = {} -- just pass these through to _expandable. -function DupExpandable:get_docstring() return self._expandable:get_docstring() end +function DupExpandable:get_docstring() + return self._expandable:get_docstring() +end function DupExpandable:copy() local copy = self._expandable:copy() copy.id = self.id @@ -23,18 +25,20 @@ function DupExpandable:matches(...) end -- invalidate has to be called on this snippet itself. -function DupExpandable:invalidate() snip_mod.Snippet.invalidate(self) end +function DupExpandable:invalidate() + snip_mod.Snippet.invalidate(self) +end local dup_mt = { -- index DupExpandable for own functions, and then the expandable stored in -- self/t. - __index = function(t,k) + __index = function(t, k) if DupExpandable[k] then return DupExpandable[k] end return t._expandable[k] - end + end, } function M.duplicate_expandable(expandable) @@ -48,19 +52,18 @@ function M.duplicate_expandable(expandable) }, dup_mt) end - local DupAddable = {} function DupAddable:retrieve_all() return vim.tbl_map(M.duplicate_expandable, self.addable:retrieve_all()) end local DupAddable_mt = { - __index = DupAddable + __index = DupAddable, } function M.duplicate_addable(addable) return setmetatable({ - addable = addable + addable = addable, }, DupAddable_mt) end diff --git a/lua/luasnip/nodes/multiSnippet.lua b/lua/luasnip/nodes/multiSnippet.lua index 294f3855e..266a4219b 100644 --- a/lua/luasnip/nodes/multiSnippet.lua +++ b/lua/luasnip/nodes/multiSnippet.lua @@ -77,14 +77,18 @@ local function multisnippet_from_nodes(contexts, nodes, opts) -- create snippet without `context`-fields! -- compare to `S` (aka `s`, the default snippet-constructor) in -- `nodes/snippet.lua`. - return multisnippet_from_snippet_obj(contexts, snip_mod._S( - snip_mod.init_snippet_opts(common_snip_opts), - nodes, + return multisnippet_from_snippet_obj( + contexts, + snip_mod._S( + snip_mod.init_snippet_opts(common_snip_opts), + nodes, + common_snip_opts + ), common_snip_opts - ), common_snip_opts) + ) end return { new_multisnippet = multisnippet_from_nodes, - _raw_ms = multisnippet_from_snippet_obj + _raw_ms = multisnippet_from_snippet_obj, } diff --git a/lua/luasnip/nodes/snippetProxy.lua b/lua/luasnip/nodes/snippetProxy.lua index 69eb2dccd..03e1c8781 100644 --- a/lua/luasnip/nodes/snippetProxy.lua +++ b/lua/luasnip/nodes/snippetProxy.lua @@ -47,7 +47,8 @@ end -- some values of the snippet are nil by default, list them here so snippets -- aren't instantiated because of them. -local license_to_nil = { priority = true, snippetType = true, _source = true, filetype = true } +local license_to_nil = + { priority = true, snippetType = true, _source = true, filetype = true } -- context and opts are (almost) the same objects as in s(contex, nodes, opts), snippet is a string representing the snippet. -- opts can aditionally contain the key `parse_fn`, which will be used to parse @@ -69,7 +70,12 @@ local function new(context, snippet, opts) local sp = vim.tbl_extend( "error", {}, - context and snip_mod.init_snippet_context(node_util.wrap_context(context), opts) or {}, + context + and snip_mod.init_snippet_context( + node_util.wrap_context(context), + opts + ) + or {}, snip_mod.init_snippet_opts(opts), node_util.init_node_opts(opts) ) diff --git a/lua/luasnip/session/snippet_collection/init.lua b/lua/luasnip/session/snippet_collection/init.lua index 025813d19..6e47cc93e 100644 --- a/lua/luasnip/session/snippet_collection/init.lua +++ b/lua/luasnip/session/snippet_collection/init.lua @@ -256,8 +256,7 @@ function M.add_snippets(snippets, opts) local snip_type = snip.snippetType ~= nil and snip.snippetType or opts.type assert( - snip_type == "autosnippets" - or snip_type == "snippets", + snip_type == "autosnippets" or snip_type == "snippets", "snippetType must be either 'autosnippets' or 'snippets'" ) diff --git a/tests/helpers.lua b/tests/helpers.lua index 31d6a9752..9d701ab47 100644 --- a/tests/helpers.lua +++ b/tests/helpers.lua @@ -158,7 +158,8 @@ M.loaders = { exec_lua( string.format( [[require("luasnip.loaders.from_vscode").load_standalone({path="%s"})]], - os.getenv("LUASNIP_SOURCE") .. "/tests/data/vscode-standalone.code-snippets" + os.getenv("LUASNIP_SOURCE") + .. "/tests/data/vscode-standalone.code-snippets" ) ) end, diff --git a/tests/integration/add_snippets_spec.lua b/tests/integration/add_snippets_spec.lua index 0c4742463..c24d141e6 100644 --- a/tests/integration/add_snippets_spec.lua +++ b/tests/integration/add_snippets_spec.lua @@ -319,16 +319,20 @@ describe("add_snippets", function() exec("set ft=c") feed("iin_lua") exec_lua("ls.expand()") - screen:expect{grid=[[ + screen:expect({ + grid = [[ in_lua^ | {0:~ }| - {2:-- INSERT --} |]]} + {2:-- INSERT --} |]], + }) exec("set ft=lua") feed("in_lua") exec_lua("ls.expand()") - screen:expect{grid=[[ + screen:expect({ + grid = [[ in_lua | expanded in lua^ | - {2:-- INSERT --} |]]} + {2:-- INSERT --} |]], + }) end) end) diff --git a/tests/integration/loaders_spec.lua b/tests/integration/loaders_spec.lua index 80f4c5e68..4aa83fd51 100644 --- a/tests/integration/loaders_spec.lua +++ b/tests/integration/loaders_spec.lua @@ -496,11 +496,13 @@ describe("loaders:", function() feed("icodesnippets") exec_lua("ls.expand()") - screen:expect{grid=[[ + screen:expect({ + grid = [[ code-snippets!!!^ | {0:~ }| {0:~ }| {0:~ }| - {2:-- INSERT --} |]]} + {2:-- INSERT --} |]], + }) end) end)