Skip to content

Commit cd8b2c7

Browse files
committed
feat(LSP-Snippets): add basic support for scope field
1 parent bc8ec05 commit cd8b2c7

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

DOC.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,29 @@ Alternatively, `jsregexp` can be cloned locally, `make`d, and the resulting
19321932

19331933
If `jsregexp` is not available, transformations are replaced by a simple copy.
19341934

1935-
# Variables
1935+
## VSCode snippet extra fields
1936+
<!-- Works only in github -->
1937+
> **Note** See also this [Issue 705] for more info
1938+
```json
1939+
1940+
{
1941+
"prefix": ...
1942+
"scope": "javascript,typescript",
1943+
"body": ...
1944+
}
1945+
```
1946+
The [`"scope": `](https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-scope)
1947+
field is honored when loaded into the LuaSnip engine.
1948+
If it isn't part of the current buffer (editor) language, then it's not imported.
1949+
Let's say you have `shared.json` that has snippets for different languages specified via `scope`.
1950+
When specific language mapped to a `.code-snippets` (or `.json`) file by a `package.json`
1951+
then only snippets `scope`-ed to the same language are loaded. The rest is ignored.
1952+
1953+
`"isFileTemplate" :` field isn't handled in anyway.
1954+
1955+
[Issue 705]: https://github.com/L3MON4D3/LuaSnip/issues/705
1956+
1957+
# VARIABLES
19361958

19371959
All `TM_something`-variables are supported with two additions:
19381960
`LS_SELECT_RAW` and `LS_SELECT_DEDENT`. These were introduced because

lua/luasnip/loaders/from_vscode.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ local function read_json(fname)
3737
end
3838
end
3939

40-
local function get_file_snippets(file)
40+
local function get_file_snippets(lang, file)
4141
local lang_snips = {}
4242
local auto_lang_snips = {}
4343

@@ -51,6 +51,14 @@ local function get_file_snippets(file)
5151
local body = type(parts.body) == "string" and parts.body
5252
or table.concat(parts.body, "\n")
5353

54+
-- Skip entire snippet if entry has `scope` field mismatching lang
55+
-- This prevents snippets mapped by package.json to one language
56+
-- to be used for another. This is not part of LSP Snippets! See:
57+
-- https://code.visualstudio.com/docs/editor/userdefinedsnippets#_snippet-scope
58+
if type(parts.scope) == "string" and not (string.match(parts.scope, lang)) then
59+
goto continue
60+
end
61+
5462
-- There are still some snippets that fail while loading
5563
pcall(function()
5664
-- Sometimes it's a list of prefixes instead of a single one
@@ -74,6 +82,8 @@ local function get_file_snippets(file)
7482
end
7583
end
7684
end)
85+
86+
::continue::
7787
end
7888

7989
return lang_snips, auto_lang_snips
@@ -90,7 +100,7 @@ local function load_snippet_files(lang, files, add_opts)
90100
auto_lang_snips = vim.deepcopy(cached_path.autosnippets)
91101
cached_path.fts[lang] = true
92102
else
93-
lang_snips, auto_lang_snips = get_file_snippets(file)
103+
lang_snips, auto_lang_snips = get_file_snippets(lang, file)
94104
-- store snippets to prevent parsing the same file more than once.
95105
cache.path_snippets[file] = {
96106
snippets = vim.deepcopy(lang_snips),
@@ -99,7 +109,6 @@ local function load_snippet_files(lang, files, add_opts)
99109
fts = { [lang] = true },
100110
}
101111
end
102-
103112
ls.add_snippets(
104113
lang,
105114
lang_snips,
@@ -285,8 +294,9 @@ function M.lazy_load(opts)
285294
opts = opts or {}
286295

287296
local ft_files = get_snippet_files(opts)
288-
local add_opts = loader_util.add_opts(opts)
297+
print(vim.inspect(ft_files))
289298

299+
local add_opts = loader_util.add_opts(opts)
290300
loader_util.extend_ft_paths(cache.ft_paths, ft_files)
291301

292302
-- immediately load filetypes that have already been loaded.

0 commit comments

Comments
 (0)