From 2909a27f4856ddcf60af3635b3c6a7ac78c1bb0b Mon Sep 17 00:00:00 2001 From: Hinell Date: Tue, 28 Mar 2023 00:43:24 +0300 Subject: [PATCH] feat(LSP-Snippets): add basic support for `scope` field --- DOC.md | 24 +++++++++++++++++++++++- lua/luasnip/loaders/from_vscode.lua | 16 ++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/DOC.md b/DOC.md index 3b9890c7f..c44003613 100644 --- a/DOC.md +++ b/DOC.md @@ -1932,7 +1932,29 @@ Alternatively, `jsregexp` can be cloned locally, `make`d, and the resulting If `jsregexp` is not available, transformations are replaced by a simple copy. -# Variables +## VSCode snippet extra fields + +> **Note** See also this [Issue 705] for more info +```json + + { + "prefix": ... + "scope": "javascript,typescript", + "body": ... + } +``` +The [`"scope": `](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-scope) +field is honored when loaded into the LuaSnip engine. +If it isn't part of the current buffer (editor) language, then it's not imported. +Let's say you have `shared.json` that has snippets for different languages specified via `scope`. +When specific language mapped to a `.code-snippets` (or `.json`) file by a `package.json` +then only snippets `scope`-ed to the same language are loaded. The rest is ignored. + +`"isFileTemplate" :` field isn't handled in anyway. + +[Issue 705]: https://github.com/L3MON4D3/LuaSnip/issues/705 + +# VARIABLES All `TM_something`-variables are supported with two additions: `LS_SELECT_RAW` and `LS_SELECT_DEDENT`. These were introduced because diff --git a/lua/luasnip/loaders/from_vscode.lua b/lua/luasnip/loaders/from_vscode.lua index 7411150e2..6de4d72e7 100644 --- a/lua/luasnip/loaders/from_vscode.lua +++ b/lua/luasnip/loaders/from_vscode.lua @@ -37,7 +37,7 @@ local function read_json(fname) end end -local function get_file_snippets(file) +local function get_file_snippets(lang, file) local lang_snips = {} local auto_lang_snips = {} @@ -51,6 +51,14 @@ local function get_file_snippets(file) local body = type(parts.body) == "string" and parts.body or table.concat(parts.body, "\n") + -- Skip entire snippet if entry has `scope` field mismatching lang + -- This prevents snippets mapped by package.json to one language + -- to be used for another. This is not part of LSP Snippets! See: + -- https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-scope + if type(parts.scope) == "string" and not (string.match(parts.scope, lang)) then + goto continue + end + -- There are still some snippets that fail while loading pcall(function() -- Sometimes it's a list of prefixes instead of a single one @@ -74,6 +82,8 @@ local function get_file_snippets(file) end end end) + + ::continue:: end return lang_snips, auto_lang_snips @@ -90,7 +100,7 @@ local function load_snippet_files(lang, files, add_opts) auto_lang_snips = vim.deepcopy(cached_path.autosnippets) cached_path.fts[lang] = true else - lang_snips, auto_lang_snips = get_file_snippets(file) + lang_snips, auto_lang_snips = get_file_snippets(lang, file) -- store snippets to prevent parsing the same file more than once. cache.path_snippets[file] = { snippets = vim.deepcopy(lang_snips), @@ -99,7 +109,6 @@ local function load_snippet_files(lang, files, add_opts) fts = { [lang] = true }, } end - ls.add_snippets( lang, lang_snips, @@ -286,7 +295,6 @@ function M.lazy_load(opts) local ft_files = get_snippet_files(opts) local add_opts = loader_util.add_opts(opts) - loader_util.extend_ft_paths(cache.ft_paths, ft_files) -- immediately load filetypes that have already been loaded.