Skip to content

Commit

Permalink
feat: rename command to cmdline
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 11, 2024
1 parent 797bedb commit faef06b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 37 deletions.
10 changes: 5 additions & 5 deletions lua/blink/cmp/completion/trigger/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ function context:within_query_bounds(cursor)
return row == bounds.line_number and col >= bounds.start_col and col <= bounds.end_col
end

function context.get_mode() return vim.api.nvim_get_mode().mode == 'c' and 'command' or 'default' end
function context.get_mode() return vim.api.nvim_get_mode().mode == 'c' and 'cmdline' or 'default' end

function context.get_cursor()
return context.get_mode() == 'command' and { 1, vim.fn.getcmdpos() - 1 } or vim.api.nvim_win_get_cursor(0)
return context.get_mode() == 'cmdline' and { 1, vim.fn.getcmdpos() - 1 } or vim.api.nvim_win_get_cursor(0)
end

function context.set_cursor(cursor)
local mode = context.get_mode()
if mode == 'default' then return vim.api.nvim_win_set_cursor(0, cursor) end

assert(mode == 'command', 'Unsupported mode for setting cursor: ' .. mode)
assert(cursor[1] == 1, 'Cursor must be on the first line in command mode')
assert(mode == 'cmdline', 'Unsupported mode for setting cursor: ' .. mode)
assert(cursor[1] == 1, 'Cursor must be on the first line in cmdline mode')
vim.fn.setcmdpos(cursor[2])
end

function context.get_line()
return context.get_mode() == 'command' and vim.fn.getcmdline()
return context.get_mode() == 'cmdline' and vim.fn.getcmdline()
or vim.api.nvim_buf_get_lines(0, context.get_cursor()[1] - 1, context.get_cursor()[1], false)[1]
end

Expand Down
10 changes: 5 additions & 5 deletions lua/blink/cmp/completion/trigger/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

--- @class blink.cmp.CompletionTrigger
--- @field buffer_events blink.cmp.BufferEvents
--- @field command_events blink.cmp.CommandEvents
--- @field cmdline_events blink.cmp.CmdlineEvents
--- @field current_context_id number
--- @field context? blink.cmp.Context
--- @field show_emitter blink.cmp.EventEmitter<{ context: blink.cmp.Context }>
Expand Down Expand Up @@ -38,7 +38,7 @@ function trigger.activate()
has_context = function() return trigger.context ~= nil end,
show_in_snippet = config.show_in_snippet,
})
trigger.command_events = require('blink.cmp.lib.command_events').new()
trigger.cmdline_events = require('blink.cmp.lib.cmdline_events').new()

local function on_char_added(char, is_ignored)
-- we were told to ignore the text changed event, so we update the context
Expand Down Expand Up @@ -107,7 +107,7 @@ function trigger.activate()
on_cursor_moved = on_cursor_moved,
on_insert_leave = function() trigger.hide() end,
})
trigger.command_events:listen({
trigger.cmdline_events:listen({
on_char_added = on_char_added,
on_cursor_moved = on_cursor_moved,
on_leave = function() trigger.hide() end,
Expand Down Expand Up @@ -135,9 +135,9 @@ end

--- Suppresses on_hide and on_show events for the duration of the callback
function trigger.suppress_events_for_callback(cb)
local mode = vim.api.nvim_get_mode().mode == 'c' and 'command' or 'default'
local mode = vim.api.nvim_get_mode().mode == 'c' and 'cmdline' or 'default'

local events = mode == 'default' and trigger.buffer_events or trigger.command_events
local events = mode == 'default' and trigger.buffer_events or trigger.cmdline_events
if not events then return cb() end

events:suppress_events_for_callback(cb)
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/windows/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function menu.update_position()
end

local redraw_queued = false
--- In command mode, the window won't be redrawn automatically so we redraw ourselves on schedule
--- In cmdline mode, the window won't be redrawn automatically so we redraw ourselves on schedule
function menu.redraw_if_needed()
if vim.api.nvim_get_mode().mode ~= 'c' or menu.win:get_win() == nil then return end
if redraw_queued then return end
Expand Down
14 changes: 8 additions & 6 deletions lua/blink/cmp/config/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
--- ```
--- @field default string[] | fun(): string[]
--- @field per_filetype table<string, string[] | fun(): string[]>
--- @field command string[] | fun(): string[]
--- @field cmdline string[] | fun(): string[]
--- @field providers table<string, blink.cmp.SourceProviderConfig>

--- @class blink.cmp.SourceProviderConfig
Expand All @@ -40,10 +40,11 @@ local sources = {
default = {
default = { 'lsp', 'path', 'snippets', 'buffer' },
per_filetype = {},
command = function()
cmdline = function()
local type = vim.fn.getcmdtype()
if type == '/' or type == '?' then return { 'buffer' } end
return { 'command' }
if type == ':' then return { 'cmdline' } end
return {}
end,
providers = {
lsp = {
Expand All @@ -70,9 +71,9 @@ local sources = {
name = 'Buffer',
module = 'blink.cmp.sources.buffer',
},
command = {
name = 'Command',
module = 'blink.cmp.sources.command',
cmdline = {
name = 'cmdline',
module = 'blink.cmp.sources.cmdline',
},
},
},
Expand All @@ -82,6 +83,7 @@ function sources.validate(config)
validate('sources', {
default = { config.default, { 'function', 'table' } },
per_filetype = { config.per_filetype, 'table' },
cmdline = { config.cmdline, { 'function', 'table' } },
providers = { config.providers, 'table' },
})
assert(
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/keymap/apply.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function apply.keymap_to_current_buffer(keys_to_commands)
end
end

function apply.command_mode_keymaps(keys_to_commands)
-- command mode: uses only insert commands
function apply.cmdline_keymaps(keys_to_commands)
-- cmdline mode: uses only insert commands
for key, commands in pairs(keys_to_commands) do
local has_insert_command = false
for _, command in ipairs(commands) do
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/keymap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ function keymap.setup()
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
end

-- Always apply command mode keymaps since they're global
require('blink.cmp.keymap.apply').command_mode_keymaps(mappings)
-- Always apply cmdline keymaps since they're global
require('blink.cmp.keymap.apply').cmdline_keymaps(mappings)
end

return keymap
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
--- @class blink.cmp.CommandEvents
--- @class blink.cmp.CmdlineEvents
--- @field has_context fun(): boolean
--- @field ignore_next_text_changed boolean
--- @field ignore_next_cursor_moved boolean
---
--- @field new fun(): blink.cmp.CommandEvents
--- @field listen fun(self: blink.cmp.CommandEvents, opts: blink.cmp.CommandEventsListener)
--- @field suppress_events_for_callback fun(self: blink.cmp.CommandEvents, cb: fun())
--- @field new fun(): blink.cmp.CmdlineEvents
--- @field listen fun(self: blink.cmp.CmdlineEvents, opts: blink.cmp.CmdlineEventsListener)
--- @field suppress_events_for_callback fun(self: blink.cmp.CmdlineEvents, cb: fun())

--- @class blink.cmp.CommandEventsListener
--- @class blink.cmp.CmdlineEventsListener
--- @field on_char_added fun(char: string, is_ignored: boolean)
--- @field on_cursor_moved fun(event: 'CursorMovedI' | 'InsertEnter', is_ignored: boolean)
--- @field on_leave fun()

--- @type blink.cmp.CommandEvents
--- @type blink.cmp.CmdlineEvents
--- @diagnostic disable-next-line: missing-fields
local command_events = {}
local cmdline_events = {}

function command_events.new()
function cmdline_events.new()
return setmetatable({
ignore_next_text_changed = false,
ignore_next_cursor_moved = false,
}, { __index = command_events })
}, { __index = cmdline_events })
end

function command_events:listen(opts)
function cmdline_events:listen(opts)
local previous_cmdline = ''

vim.api.nvim_create_autocmd('CmdlineEnter', {
Expand Down Expand Up @@ -85,7 +85,7 @@ end

--- Suppresses autocmd events for the duration of the callback
--- HACK: there's likely edge cases with this
function command_events:suppress_events_for_callback(cb)
function cmdline_events:suppress_events_for_callback(cb)
local cursor_before = vim.fn.getcmdpos()
local text_before = vim.fn.getcmdline()

Expand All @@ -101,4 +101,4 @@ function command_events:suppress_events_for_callback(cb)
self.ignore_next_cursor_moved = cursor_after ~= cursor_before
end

return command_events
return cmdline_events
4 changes: 2 additions & 2 deletions lua/blink/cmp/lib/text_edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function text_edits.apply(edits)
local mode = context.get_mode()
if mode == 'default' then return vim.lsp.util.apply_text_edits(edits, vim.api.nvim_get_current_buf(), 'utf-8') end

assert(mode == 'command', 'Unsupported mode for text edits: ' .. mode)
assert(#edits == 1, 'Command mode only supports one text edit. Contributions welcome!')
assert(mode == 'cmdline', 'Unsupported mode for text edits: ' .. mode)
assert(#edits == 1, 'Cmdline mode only supports one text edit. Contributions welcome!')

local edit = edits[1]
local line = context.get_line()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local regex = require('blink.cmp.sources.command.regex')
local regex = require('blink.cmp.sources.cmdline.regex')

--- @class blink.cmp.Source
local cmdline = {}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lua/blink/cmp/types.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- @alias blink.cmp.Mode 'command' | 'default'
--- @alias blink.cmp.Mode 'cmdline' | 'default'

--- @class blink.cmp.CompletionItem : lsp.CompletionItem
--- @field score_offset? number
Expand Down

0 comments on commit faef06b

Please sign in to comment.