From faef06b66bf47782a3a9beb16a236be15e2d30e9 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Wed, 11 Dec 2024 17:09:16 -0500 Subject: [PATCH] feat: rename command to cmdline --- lua/blink/cmp/completion/trigger/context.lua | 10 ++++---- lua/blink/cmp/completion/trigger/init.lua | 10 ++++---- lua/blink/cmp/completion/windows/menu.lua | 2 +- lua/blink/cmp/config/sources.lua | 14 ++++++----- lua/blink/cmp/keymap/apply.lua | 4 ++-- lua/blink/cmp/keymap/init.lua | 4 ++-- ...{command_events.lua => cmdline_events.lua} | 24 +++++++++---------- lua/blink/cmp/lib/text_edits.lua | 4 ++-- .../cmp/sources/{command => cmdline}/init.lua | 2 +- .../sources/{command => cmdline}/regex.lua | 0 lua/blink/cmp/types.lua | 2 +- 11 files changed, 39 insertions(+), 37 deletions(-) rename lua/blink/cmp/lib/{command_events.lua => cmdline_events.lua} (83%) rename lua/blink/cmp/sources/{command => cmdline}/init.lua (99%) rename lua/blink/cmp/sources/{command => cmdline}/regex.lua (100%) diff --git a/lua/blink/cmp/completion/trigger/context.lua b/lua/blink/cmp/completion/trigger/context.lua index c0267e3d..7f80337d 100644 --- a/lua/blink/cmp/completion/trigger/context.lua +++ b/lua/blink/cmp/completion/trigger/context.lua @@ -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 diff --git a/lua/blink/cmp/completion/trigger/init.lua b/lua/blink/cmp/completion/trigger/init.lua index 9dff0578..ae5bf556 100644 --- a/lua/blink/cmp/completion/trigger/init.lua +++ b/lua/blink/cmp/completion/trigger/init.lua @@ -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 }> @@ -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 @@ -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, @@ -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) diff --git a/lua/blink/cmp/completion/windows/menu.lua b/lua/blink/cmp/completion/windows/menu.lua index 83c92d48..bc5b1141 100644 --- a/lua/blink/cmp/completion/windows/menu.lua +++ b/lua/blink/cmp/completion/windows/menu.lua @@ -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 diff --git a/lua/blink/cmp/config/sources.lua b/lua/blink/cmp/config/sources.lua index d067fa61..662e460d 100644 --- a/lua/blink/cmp/config/sources.lua +++ b/lua/blink/cmp/config/sources.lua @@ -16,7 +16,7 @@ --- ``` --- @field default string[] | fun(): string[] --- @field per_filetype table ---- @field command string[] | fun(): string[] +--- @field cmdline string[] | fun(): string[] --- @field providers table --- @class blink.cmp.SourceProviderConfig @@ -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 = { @@ -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', }, }, }, @@ -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( diff --git a/lua/blink/cmp/keymap/apply.lua b/lua/blink/cmp/keymap/apply.lua index a25371b0..6872acfe 100644 --- a/lua/blink/cmp/keymap/apply.lua +++ b/lua/blink/cmp/keymap/apply.lua @@ -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 diff --git a/lua/blink/cmp/keymap/init.lua b/lua/blink/cmp/keymap/init.lua index cc79fa4b..37279aab 100644 --- a/lua/blink/cmp/keymap/init.lua +++ b/lua/blink/cmp/keymap/init.lua @@ -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 diff --git a/lua/blink/cmp/lib/command_events.lua b/lua/blink/cmp/lib/cmdline_events.lua similarity index 83% rename from lua/blink/cmp/lib/command_events.lua rename to lua/blink/cmp/lib/cmdline_events.lua index 341f8976..e9edfbf6 100644 --- a/lua/blink/cmp/lib/command_events.lua +++ b/lua/blink/cmp/lib/cmdline_events.lua @@ -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', { @@ -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() @@ -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 diff --git a/lua/blink/cmp/lib/text_edits.lua b/lua/blink/cmp/lib/text_edits.lua index a34f07b7..c9cae45c 100644 --- a/lua/blink/cmp/lib/text_edits.lua +++ b/lua/blink/cmp/lib/text_edits.lua @@ -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() diff --git a/lua/blink/cmp/sources/command/init.lua b/lua/blink/cmp/sources/cmdline/init.lua similarity index 99% rename from lua/blink/cmp/sources/command/init.lua rename to lua/blink/cmp/sources/cmdline/init.lua index a932e50b..0d0cbc1b 100644 --- a/lua/blink/cmp/sources/command/init.lua +++ b/lua/blink/cmp/sources/cmdline/init.lua @@ -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 = {} diff --git a/lua/blink/cmp/sources/command/regex.lua b/lua/blink/cmp/sources/cmdline/regex.lua similarity index 100% rename from lua/blink/cmp/sources/command/regex.lua rename to lua/blink/cmp/sources/cmdline/regex.lua diff --git a/lua/blink/cmp/types.lua b/lua/blink/cmp/types.lua index f722a796..6dd38e63 100644 --- a/lua/blink/cmp/types.lua +++ b/lua/blink/cmp/types.lua @@ -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