Skip to content

Commit 6b11bb1

Browse files
committed
feat(cmdline): enable in command-line-window and vim filetype
Enables cmdline source provider in the command-line window and vim filetype. You can also add it to other filetypes, just like any other source. Note that if you already have `opts.sources.default` configured and want this feature, simply add the `cmdline` source alongside your other sources, or enable it specifically for the vim filetype with: ```lua sources = { per_filetype = { vim = { 'cmdline' } }, } ``` Closes #1835 Related #1447
1 parent 102db2f commit 6b11bb1

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

doc/configuration/reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ See the [mode specific configurations](#mode-specific) for setting sources for `
461461
```lua
462462
sources = {
463463
-- Static list of providers to enable, or a function to dynamically enable/disable providers based on the context
464-
default = { 'lsp', 'path', 'snippets', 'buffer' },
465-
464+
default = { 'cmdline', 'lsp', 'path', 'snippets', 'buffer' },
465+
466466
-- You may also define providers per filetype
467467
per_filetype = {
468468
-- optionally inherit from the `default` sources

lua/blink/cmp/completion/windows/ghost_text/init.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ function ghost_text.show_preview(items, selection_idx)
6969
ghost_text.selected_item = selected_item
7070

7171
vim.api.nvim_set_decoration_provider(ghost_text.ns, {
72-
on_win = function(_, _, buf) return utils.is_noice() and buf == ghost_text_buf end,
72+
on_win = function(_, _, buf)
73+
return (vim.fn.win_gettype() == 'command' or utils.is_noice()) and buf == ghost_text_buf
74+
end,
7375
on_line = function() ghost_text.draw_preview() end,
7476
})
7577

lua/blink/cmp/config/sources.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ local validate = require('blink.cmp.config.utils').validate
4545
local sources = {
4646
--- @type blink.cmp.SourceConfig
4747
default = {
48-
default = { 'lsp', 'path', 'snippets', 'buffer' },
48+
default = { 'cmdline', 'lsp', 'path', 'snippets', 'buffer' },
4949
per_filetype = {},
50-
5150
transform_items = function(_, items) return items end,
5251
min_keyword_length = 0,
5352

lua/blink/cmp/lib/text_edits.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ function text_edits.apply(text_edit, additional_text_edits)
1414
assert(mode == 'default' or mode == 'cmdline' or mode == 'term', 'Unsupported mode for text edits: ' .. mode)
1515

1616
if mode == 'default' then
17-
-- writing to dot repeat may fail in cases like `q:`, which aren't easy to detect
18-
-- so we ignore the error
19-
if config.completion.accept.dot_repeat then pcall(text_edits.write_to_dot_repeat, text_edit) end
17+
-- writing to dot repeat may fail in cases like `q:`
18+
if config.completion.accept.dot_repeat and vim.fn.win_gettype() ~= 'command' then
19+
text_edits.write_to_dot_repeat(text_edit)
20+
end
2021

2122
local all_edits = utils.shallow_copy(additional_text_edits)
2223
table.insert(all_edits, text_edit)

lua/blink/cmp/sources/cmdline/init.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ function cmdline.new()
1717
return self
1818
end
1919

20-
function cmdline:enabled()
21-
return vim.api.nvim_get_mode().mode == 'c' and vim.tbl_contains({ ':', '@' }, vim.fn.getcmdtype())
22-
end
23-
2420
---@param name string
2521
---@return boolean?
2622
function cmdline:is_boolean_option(name)
2723
local ok, opt = pcall(function() return vim.opt[name]:get() end)
2824
if ok then return type(opt) == 'boolean' end
2925
end
3026

27+
function cmdline:enabled()
28+
return vim.bo.ft == 'vim'
29+
or vim.api.nvim_get_mode().mode == 'c' and vim.tbl_contains({ ':', '@' }, vim.fn.getcmdtype())
30+
end
31+
3132
function cmdline:get_trigger_characters() return { ' ', '.', '#', '-', '=', '/', ':' } end
3233

3334
function cmdline:get_completions(context, callback)
@@ -48,6 +49,7 @@ function cmdline:get_completions(context, callback)
4849
local cmd = (valid_cmd and parsed.cmd) or arguments[1] or ''
4950

5051
local is_help_command = constants.help_commands[cmd] and arg_number > 1
52+
local is_cmdline = vim.api.nvim_get_mode().mode == 'c'
5153

5254
local task = async.task
5355
.empty()
@@ -173,6 +175,19 @@ function cmdline:get_completions(context, callback)
173175
start_pos = start_pos + #prefix
174176
end
175177

178+
-- Adjust start position for Lua expressions entered without space
179+
-- `=vim.api` which are valid expressions
180+
if is_first_arg and not is_cmdline then
181+
local prefix = current_arg:match('^=([^%s].*)')
182+
if prefix then start_pos = start_pos + #prefix end
183+
end
184+
185+
-- Calculate line and column values based on context
186+
local line = is_cmdline and 0 or (context.cursor[1] - 1)
187+
local insert_end_char = is_cmdline and (vim.fn.getcmdpos() - 1) or context.cursor[2]
188+
local replace_end_char =
189+
math.min(start_pos + #current_arg, context.bounds.start_col + context.bounds.length - 1)
190+
176191
local item = {
177192
label = filter_text,
178193
filterText = filter_text,
@@ -181,15 +196,12 @@ function cmdline:get_completions(context, callback)
181196
textEdit = {
182197
newText = new_text,
183198
insert = {
184-
start = { line = 0, character = start_pos },
185-
['end'] = { line = 0, character = vim.fn.getcmdpos() - 1 },
199+
start = { line = line, character = start_pos },
200+
['end'] = { line = line, character = insert_end_char },
186201
},
187202
replace = {
188-
start = { line = 0, character = start_pos },
189-
['end'] = {
190-
line = 0,
191-
character = math.min(start_pos + #current_arg, context.bounds.start_col + context.bounds.length - 1),
192-
},
203+
start = { line = line, character = start_pos },
204+
['end'] = { line = line, character = replace_end_char },
193205
},
194206
},
195207
kind = require('blink.cmp.types').CompletionItemKind.Property,

0 commit comments

Comments
 (0)