|
Hi, The difficulty I have is understanding what kind items cmp accepts. One item would be 'label' which I understand to be The format of this JSON file I copied from the rafamadriz /friendly-snippets json files inside snippet folder. So far the result I get is nothing. 👎 Expected results would be 'MediaItem reaper.AddMediaItemToTrack' contents of 'prefix:' for label, what's in the 'body' for completion (snippetable) and the content of 'description:' for documentation. How should I go about doing this please? P.S I have already added the source to the cmp config and confirmed that the default template you provided for 'month' works. |
Replies: 4 comments 2 replies
|
For anyone looking for answers: '- #input' <-- is for replacing what you already typed with a completion rather than completion concatenating on to what you typed. the definition of things like 'label' and 'InsertTextMode' go see LSP Spec at: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/ |
|
I created a plugin to parse JSON and extract fields to be used for completions, check it out joe-dil/json-cmp |
|
Writing a custom nvim-cmp source is well-documented once you find the right examples. The key is the source object interface: local source = {}
-- Required: return source name
source.get_trigger_characters = function()
return { '.', ':' }
end
-- Required: provide completion items
source.complete = function(self, params, callback)
local items = {}
-- params.context has cursor position, buffer info, etc.
local bufnr = params.context.bufnr
-- Build your items
table.insert(items, {
label = 'my_completion',
kind = require('cmp').lsp.CompletionItemKind.Text,
detail = 'My custom completion',
documentation = {
kind = 'markdown',
value = '**Description**\n\nThis is my item'
},
})
callback({ items = items, isIncomplete = false })
end
-- Register the source
require('cmp').register_source('my_source', source)For JSON parsing to cmp table: source.complete = function(self, params, callback)
local json_str = vim.fn.system('your-command-that-returns-json')
local ok, data = pcall(vim.json.decode, json_str)
if not ok then
callback({ items = {}, isIncomplete = false })
return
end
local items = vim.tbl_map(function(entry)
return {
label = entry.name,
detail = entry.type,
documentation = entry.description,
}
end, data)
callback({ items = items })
endAsync sources: for slow data (API calls, filesystem), use Sorting/filtering: cmp handles fuzzy matching on labels automatically. You just need to return the candidate set. What's the data source for your JSON — an external API, a local file, or a tool output? |
For anyone looking for answers:
'- #input' <-- is for replacing what you already typed with a completion rather than completion concatenating on to what you typed.
the definition of things like 'label' and 'InsertTextMode' go see LSP Spec at: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/