-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builtins/completion: add nvim-snippets source (#201)
- Loading branch information
1 parent
2c9d308
commit 6be90f8
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,9 @@ | |
"luasnip": { | ||
"filetypes": [] | ||
}, | ||
"nvim_snippets": { | ||
"filetypes": [] | ||
}, | ||
"spell": { | ||
"filetypes": [] | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
local h = require("null-ls.helpers") | ||
local methods = require("null-ls.methods") | ||
|
||
local COMPLETION = methods.internal.COMPLETION | ||
|
||
-- based on pattern from cmp-luasnip | ||
local pattern = "\\%([^[:alnum:][:blank:]]\\+\\|\\w\\+\\)" | ||
local regex = vim.regex([[\%(]] .. pattern .. [[\)\m$]]) | ||
|
||
local function nvim_snippet_exists() | ||
local status, _ = pcall(require, "snippets") | ||
|
||
return status | ||
end | ||
|
||
local function get_loaded_snippets() | ||
return require("snippets").get_loaded_snippets() | ||
end | ||
|
||
return h.make_builtin({ | ||
name = "nvim_snippets", | ||
can_run = nvim_snippet_exists, | ||
condition = nvim_snippet_exists, | ||
runtime_condition = h.cache.by_bufnr(function() | ||
return not vim.tbl_isempty(get_loaded_snippets()) | ||
end), | ||
meta = { | ||
url = "https://github.com/garymjr/nvim-snippets", | ||
description = "Snippets managed by nvim-snippets.", | ||
}, | ||
method = COMPLETION, | ||
filetypes = {}, | ||
generator = { | ||
--- @param params NullLsParams | ||
--- @param done fun() | ||
fn = function(params, done) | ||
local line_to_cursor = params.content[params.row]:sub(1, params.col) | ||
local start_col = regex:match_str(line_to_cursor) | ||
|
||
if nil == start_col then | ||
done({ { items = {}, isIncomplete = true } }) | ||
return | ||
end | ||
|
||
local items = {} | ||
local snippets = get_loaded_snippets() | ||
for _, item in pairs(snippets) do | ||
if vim.startswith(item.prefix, line_to_cursor:sub(start_col)) then | ||
local insertText = (type(item.body) == "table") and table.concat(item.body, "\n") or item.body | ||
local textEdit = { | ||
range = { | ||
start = { line = params.row - 1, character = start_col }, | ||
["end"] = { line = params.row - 1, character = params.col - start_col }, | ||
}, | ||
newText = insertText, | ||
} | ||
|
||
items[#items + 1] = { | ||
label = item.prefix, | ||
kind = vim.lsp.protocol.CompletionItemKind.Snippet, | ||
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet, | ||
detail = item.description, | ||
insertText = insertText, | ||
textEdit = textEdit, | ||
} | ||
end | ||
end | ||
done({ { items = items, isIncomplete = #items == 0 } }) | ||
end, | ||
async = true, | ||
}, | ||
}) |