Skip to content

Commit

Permalink
builtins/completion: add nvim-snippets source (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
przepompownia authored Nov 28, 2024
1 parent 2c9d308 commit 6be90f8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/BUILTINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ local sources = { null_ls.builtins.completion.luasnip }

- Registering this source will show available snippets in the completion list, but luasnip is in charge of expanding them. Consult [luasnip's documentation](https://github.com/L3MON4D3/LuaSnip#keymaps) to set up keymaps for expansion and jumping.

### [nvim_snippets](https://github.com/garymjr/nvim-snippets)

Snippets managed by nvim-snippets.

#### Usage

```lua
local sources = { null_ls.builtins.completion.nvim_snippets }
```

#### Defaults

- Filetypes: `{}`
- Method: `completion`

### spell

Spell suggestions completion source.
Expand Down
3 changes: 3 additions & 0 deletions doc/builtins.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
"luasnip": {
"filetypes": []
},
"nvim_snippets": {
"filetypes": []
},
"spell": {
"filetypes": []
},
Expand Down
3 changes: 3 additions & 0 deletions lua/null-ls/builtins/_meta/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ return {
luasnip = {
filetypes = {}
},
nvim_snippets = {
filetypes = {}
},
spell = {
filetypes = {}
},
Expand Down
72 changes: 72 additions & 0 deletions lua/null-ls/builtins/completion/nvim_snippets.lua
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,
},
})

0 comments on commit 6be90f8

Please sign in to comment.