mirror of
https://github.com/hrsh7th/cmp-cmdline
synced 2024-09-16 20:24:04 +02:00
Initial commit
This commit is contained in:
parent
9f5141614e
commit
c30036a7ec
3 changed files with 138 additions and 1 deletions
18
README.md
18
README.md
|
@ -1,2 +1,18 @@
|
|||
# cmp-cmdline
|
||||
nvim-cmp source for vim's cmdline
|
||||
|
||||
nvim-cmp source for vim's cmdline.
|
||||
|
||||
# Setup
|
||||
|
||||
```lua
|
||||
require'cmp'.setup.cmdline(':', {
|
||||
sources = {
|
||||
{ name = 'cmdline' }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
# Warning
|
||||
|
||||
This source will work after merged https://github.com/hrsh7th/nvim-cmp/pull/362
|
||||
|
||||
|
|
1
after/plugin/cmp_cmdline.lua
Normal file
1
after/plugin/cmp_cmdline.lua
Normal file
|
@ -0,0 +1 @@
|
|||
require('cmp').register_source('cmdline', require('cmp_cmdline').new())
|
120
lua/cmp_cmdline/init.lua
Normal file
120
lua/cmp_cmdline/init.lua
Normal file
|
@ -0,0 +1,120 @@
|
|||
local cmp = require('cmp')
|
||||
|
||||
local definitions = {
|
||||
{
|
||||
type = 'option',
|
||||
regex = [=[&[^[:blank:]]*$]=],
|
||||
kind = cmp.lsp.CompletionItemKind.Variable,
|
||||
isIncomplete = false,
|
||||
exec = function(_, _, _)
|
||||
return vim.fn.getcompletion('', 'option')
|
||||
end
|
||||
},
|
||||
{
|
||||
type = 'environment',
|
||||
regex = [=[\$[^[:blank:]]*$]=],
|
||||
kind = cmp.lsp.CompletionItemKind.Variable,
|
||||
isIncomplete = false,
|
||||
exec = function(_, _, _)
|
||||
return vim.fn.getcompletion('', 'environment')
|
||||
end
|
||||
},
|
||||
{
|
||||
type = 'customlist',
|
||||
regex = [=[\%(\k\)*$]=],
|
||||
kind = cmp.lsp.CompletionItemKind.Variable,
|
||||
fallback = true,
|
||||
isIncomplete = false,
|
||||
exec = function(arglead, cmdline, curpos)
|
||||
local name = cmdline:match([=[^[ <'>]*(%a*)]=])
|
||||
if not name then
|
||||
return {}
|
||||
end
|
||||
for name_, option in pairs(vim.api.nvim_get_commands({ builtin = false })) do
|
||||
if name_ == name then
|
||||
if vim.tbl_contains({ 'customlist', 'custom' }, option.complete) then
|
||||
local ok, items = pcall(function()
|
||||
local func = string.gsub(option.complete_arg, 's:', ('<SNR>%d_'):format(option.script_id))
|
||||
return vim.api.nvim_call_function(func, { arglead, cmdline, curpos })
|
||||
end)
|
||||
if not ok then
|
||||
return {}
|
||||
end
|
||||
if type(items) == 'string' then
|
||||
return vim.split(items, '\n')
|
||||
elseif type(items) == 'table' then
|
||||
return items
|
||||
end
|
||||
return {}
|
||||
end
|
||||
end
|
||||
end
|
||||
return {}
|
||||
end
|
||||
},
|
||||
{
|
||||
type = 'cmdline',
|
||||
regex = [=[.*]=],
|
||||
kind = cmp.lsp.CompletionItemKind.Variable,
|
||||
isIncomplete = true,
|
||||
exec = function(arglead, _, _)
|
||||
return vim.fn.getcompletion(arglead, 'cmdline')
|
||||
end
|
||||
},
|
||||
}
|
||||
|
||||
local source = {}
|
||||
|
||||
source.new = function()
|
||||
return setmetatable({}, { __index = source })
|
||||
end
|
||||
|
||||
source.get_keyword_pattern = function()
|
||||
return [=[\h\%(\w\|-\|\/\|#\|:\|\.\)*]=]
|
||||
end
|
||||
|
||||
source.get_trigger_characters = function()
|
||||
return { '$', ':', '!', '&', ' ' }
|
||||
end
|
||||
|
||||
source.is_available = function()
|
||||
return vim.api.nvim_get_mode().mode == 'c'
|
||||
end
|
||||
|
||||
source.complete = function(_, params, callback)
|
||||
local items, kind, isIncomplete = {}, cmp.lsp.CompletionItemKind.Text, false
|
||||
for _, type in ipairs(definitions) do
|
||||
local s, e = vim.regex(type.regex):match_str(params.context.cursor_before_line)
|
||||
if s and e then
|
||||
items = type.exec(
|
||||
string.sub(params.context.cursor_before_line, s + 1, e + 1),
|
||||
params.context.cursor_before_line,
|
||||
params.context.cursor.col
|
||||
)
|
||||
kind = type.kind
|
||||
isIncomplete = type.isIncomplete
|
||||
if not (#items == 0 and type.fallback) then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
callback({
|
||||
isIncomplete = isIncomplete,
|
||||
items = vim.tbl_map(function(item)
|
||||
if type(item) == 'string' then
|
||||
return {
|
||||
label = item,
|
||||
kind = kind,
|
||||
}
|
||||
end
|
||||
return {
|
||||
label = item.word,
|
||||
kind = kind,
|
||||
}
|
||||
end, items)
|
||||
})
|
||||
end
|
||||
|
||||
return source
|
||||
|
Loading…
Reference in a new issue