Skip to content

Commit

Permalink
feat(LSP-Snippets): add basic support for scope field
Browse files Browse the repository at this point in the history
  • Loading branch information
hinell committed Mar 27, 2023
1 parent bc8ec05 commit 2909a27
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
24 changes: 23 additions & 1 deletion DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- Works only in github -->
> **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
Expand Down
16 changes: 12 additions & 4 deletions lua/luasnip/loaders/from_vscode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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
Expand All @@ -74,6 +82,8 @@ local function get_file_snippets(file)
end
end
end)

::continue::
end

return lang_snips, auto_lang_snips
Expand All @@ -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),
Expand All @@ -99,7 +109,6 @@ local function load_snippet_files(lang, files, add_opts)
fts = { [lang] = true },
}
end

ls.add_snippets(
lang,
lang_snips,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2909a27

Please sign in to comment.