Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th committed Oct 21, 2021
1 parent 9f51416 commit c30036a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions after/plugin/cmp_cmdline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('cmp').register_source('cmdline', require('cmp_cmdline').new())
120 changes: 120 additions & 0 deletions lua/cmp_cmdline/init.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c30036a

Please sign in to comment.