Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hrsh7th/nvim-cmp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.2
Choose a base ref
...
head repository: hrsh7th/nvim-cmp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 11 commits
  • 12 files changed
  • 9 contributors

Commits on Jan 23, 2025

  1. fix: Outdated completion item with mini.snippets (#2126)

    * fix: Outdated completion item with mini.snippets
    
    * fix: Outdated completion item with mini.snippets. Prevent completion suggestions directly after snippet expand
    
    * fix: Outdated completion item with mini.snippets. Undo changes
    
    * fix: Outdated completion item with mini.snippets. The cmp.resubscribe solution
    
    ---------
    
    Co-authored-by: abeldekat <abel@nomail.com>
    abeldekat and abeldekat authored Jan 23, 2025
    Copy the full SHA
    1250990 View commit details

Commits on Feb 23, 2025

  1. Copy the full SHA
    1deeb87 View commit details
  2. Format with stylua

    wsdjeg authored and github-actions[bot] committed Feb 23, 2025
    Copy the full SHA
    5a11682 View commit details

Commits on Mar 1, 2025

  1. Fix typos (#2135)

    nizamiza authored Mar 1, 2025
    Copy the full SHA
    c273707 View commit details

Commits on Mar 14, 2025

  1. Copy the full SHA
    1e1900b View commit details

Commits on Apr 1, 2025

  1. 4
    Copy the full SHA
    c4c784a View commit details
  2. Copy the full SHA
    3fce8d9 View commit details
  3. Copy the full SHA
    059e894 View commit details

Commits on Apr 13, 2025

  1. Copy the full SHA
    30d2593 View commit details
  2. Format with stylua

    wsdjeg authored and github-actions[bot] committed Apr 13, 2025
    Copy the full SHA
    684b3cf View commit details
  3. Copy the full SHA
    b5311ab View commit details
Showing with 90 additions and 22 deletions.
  1. +10 −0 README.md
  2. +10 −0 doc/cmp.txt
  3. +2 −2 lua/cmp/config/compare.lua
  4. +4 −4 lua/cmp/core.lua
  5. +6 −0 lua/cmp/init.lua
  6. +3 −3 lua/cmp/types/cmp.lua
  7. +11 −0 lua/cmp/utils/api.lua
  8. +31 −7 lua/cmp/utils/autocmd.lua
  9. +3 −1 lua/cmp/utils/event.lua
  10. +5 −2 lua/cmp/utils/misc.lua
  11. +2 −0 lua/cmp/utils/window.lua
  12. +3 −3 lua/cmp/view/ghost_text_view.lua
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -46,6 +46,10 @@ Plug 'hrsh7th/vim-vsnip'
" Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip'
" For mini.snippets users.
" Plug 'echasnovski/mini.snippets'
" Plug 'abeldekat/cmp-mini-snippets'
" For ultisnips users.
" Plug 'SirVer/ultisnips'
" Plug 'quangnguyen30192/cmp-nvim-ultisnips'
@@ -69,6 +73,12 @@ lua <<EOF
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
-- For `mini.snippets` users:
-- local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
-- insert({ body = args.body }) -- Insert at cursor
-- cmp.resubscribe({ "TextChangedI", "TextChangedP" })
-- require("cmp.config").set_onetime({ sources = {} })
end,
},
window = {
10 changes: 10 additions & 0 deletions doc/cmp.txt
Original file line number Diff line number Diff line change
@@ -62,6 +62,10 @@ A recommended configuration can be found below.
" Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip'

" For mini.snippets users.
" Plug 'echasnovski/mini.snippets'
" Plug 'abeldekat/cmp-mini-snippets'

" For snippy users.
" Plug 'dcampos/nvim-snippy'
" Plug 'dcampos/cmp-snippy'
@@ -86,6 +90,12 @@ A recommended configuration can be found below.
-- require'snippy'.expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)

-- For `mini.snippets` users:
-- local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
-- insert({ body = args.body }) -- Insert at cursor
-- cmp.resubscribe({ "TextChangedI", "TextChangedP" })
-- require("cmp.config").set_onetime({ sources = {} })
end,
},
window = {
4 changes: 2 additions & 2 deletions lua/cmp/config/compare.lua
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ compare.recently_used = setmetatable({
end,
})

---kind: Entires with smaller ordinal value of 'kind' will be ranked higher.
---kind: Entries with smaller ordinal value of 'kind' will be ranked higher.
---(see lsp.CompletionItemKind enum).
---Exceptions are that Text(1) will be ranked the lowest, and snippets be the highest.
---@type cmp.ComparatorFunction
@@ -106,7 +106,7 @@ compare.sort_text = function(entry1, entry2)
return nil
end

---length: Entires with shorter label length will be ranked higher.
---length: Entries with shorter label length will be ranked higher.
---@type cmp.ComparatorFunction
compare.length = function(entry1, entry2)
local diff = #entry1.completion_item.label - #entry2.completion_item.label
8 changes: 4 additions & 4 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
@@ -399,7 +399,7 @@ core.confirm = function(self, e, option, callback)
else
vim.cmd([[silent! undojoin]])
-- This logic must be used nvim_buf_set_text.
-- If not used, the snippet engine's placeholder wil be broken.
-- If not used, the snippet engine's placeholder will be broken.
vim.api.nvim_buf_set_text(0, e.context.cursor.row - 1, e.offset - 1, ctx.cursor.row - 1, ctx.cursor.col - 1, {
e.context.cursor_before_line:sub(e.offset),
})
@@ -433,11 +433,11 @@ core.confirm = function(self, e, option, callback)
return
end
vim.cmd([[silent! undojoin]])
vim.lsp.util.apply_text_edits(text_edits, ctx.bufnr, e.source:get_position_encoding_kind())
api.apply_text_edits(text_edits, ctx.bufnr, e.source:get_position_encoding_kind())
end)
else
vim.cmd([[silent! undojoin]])
vim.lsp.util.apply_text_edits(e.completion_item.additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
api.apply_text_edits(e.completion_item.additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
end
end)
feedkeys.call('', 'n', function()
@@ -486,7 +486,7 @@ core.confirm = function(self, e, option, callback)
if is_snippet then
completion_item.textEdit.newText = ''
end
vim.lsp.util.apply_text_edits({ completion_item.textEdit }, ctx.bufnr, 'utf-8')
api.apply_text_edits({ completion_item.textEdit }, ctx.bufnr, 'utf-8')

local texts = vim.split(completion_item.textEdit.newText, '\n')
vim.api.nvim_win_set_cursor(0, {
6 changes: 6 additions & 0 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
@@ -328,6 +328,12 @@ cmp.status = function()
end
end

---Ensures that cmp is the last receiver of the events specified.
---@param events string[]
cmp.resubscribe = function(events)
autocmd.resubscribe(events)
end

---@type cmp.Setup
cmp.setup = setmetatable({
global = function(c)
6 changes: 3 additions & 3 deletions lua/cmp/types/cmp.lua
Original file line number Diff line number Diff line change
@@ -152,9 +152,9 @@ cmp.ItemField = {
---@field public comparators cmp.Comparator[]

---@class cmp.FormattingConfig
---@field public fields cmp.ItemField[]
---@field public expandable_indicator boolean
---@field public format fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem
---@field public fields? cmp.ItemField[]
---@field public expandable_indicator? boolean
---@field public format? fun(entry: cmp.Entry, vim_item: vim.CompletedItem): vim.CompletedItem

---@class cmp.SnippetConfig
---@field public expand fun(args: cmp.SnippetExpansionParams)
11 changes: 11 additions & 0 deletions lua/cmp/utils/api.lua
Original file line number Diff line number Diff line change
@@ -67,4 +67,15 @@ api.get_cursor_before_line = function()
return string.sub(api.get_current_line(), 1, cursor[2])
end

--- Applies a list of text edits to a buffer. Preserves 'buflisted' state.
---@param text_edits lsp.TextEdit[]
---@param bufnr integer Buffer id
---@param position_encoding 'utf-8'|'utf-16'|'utf-32'
api.apply_text_edits = function(text_edits, bufnr, position_encoding)
-- preserve 'buflisted' state because vim.lsp.util.apply_text_edits forces it to true
local prev_buflisted = vim.bo[bufnr].buflisted
vim.lsp.util.apply_text_edits(text_edits, bufnr, position_encoding)
vim.bo[bufnr].buflisted = prev_buflisted
end

return api
38 changes: 31 additions & 7 deletions lua/cmp/utils/autocmd.lua
Original file line number Diff line number Diff line change
@@ -6,6 +6,16 @@ autocmd.group = vim.api.nvim_create_augroup('___cmp___', { clear = true })

autocmd.events = {}

local function create_autocmd(event)
vim.api.nvim_create_autocmd(event, {
desc = ('nvim-cmp: autocmd: %s'):format(event),
group = autocmd.group,
callback = function()
autocmd.emit(event)
end,
})
end

---Subscribe autocmd
---@param events string|string[]
---@param callback function
@@ -16,13 +26,7 @@ autocmd.subscribe = function(events, callback)
for _, event in ipairs(events) do
if not autocmd.events[event] then
autocmd.events[event] = {}
vim.api.nvim_create_autocmd(event, {
desc = ('nvim-cmp: autocmd: %s'):format(event),
group = autocmd.group,
callback = function()
autocmd.emit(event)
end,
})
create_autocmd(event)
end
table.insert(autocmd.events[event], callback)
end
@@ -50,4 +54,24 @@ autocmd.emit = function(event)
end
end

---Resubscribe to events
---@param events string[]
autocmd.resubscribe = function(events)
-- Delete the autocommands if present
local found = vim.api.nvim_get_autocmds({
group = autocmd.group,
event = events,
})
for _, to_delete in ipairs(found) do
vim.api.nvim_del_autocmd(to_delete.id)
end

-- Recreate if event is known
for _, event in ipairs(events) do
if autocmd.events[event] then
create_autocmd(event)
end
end
end

return autocmd
4 changes: 3 additions & 1 deletion lua/cmp/utils/event.lua
Original file line number Diff line number Diff line change
@@ -44,7 +44,9 @@ end
---@param name string
event.emit = function(self, name, ...)
for _, callback in ipairs(self.events[name] or {}) do
callback(...)
if type(callback) == 'function' then
callback(...)
end
end
end

7 changes: 5 additions & 2 deletions lua/cmp/utils/misc.lua
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ misc.rep = function(str_or_tbl, count)
return rep
end

---Return the valu is empty or not.
---Return whether the value is empty or not.
---@param v any
---@return boolean
misc.empty = function(v)
@@ -197,6 +197,9 @@ end
---@return integer
misc.to_utfindex = function(text, vimindex)
vimindex = vimindex or #text + 1
if vim.fn.has('nvim-0.11') == 1 then
return vim.str_utfindex(text, 'utf-16', math.max(0, math.min(vimindex - 1, #text)))
end
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
end

@@ -208,7 +211,7 @@ misc.to_vimindex = function(text, utfindex)
utfindex = utfindex or #text
for i = utfindex, 1, -1 do
local s, v = pcall(function()
return vim.str_byteindex(text, i) + 1
return vim.str_byteindex(text, 'utf-16', i) + 1
end)
if s then
return v
2 changes: 2 additions & 0 deletions lua/cmp/utils/window.lua
Original file line number Diff line number Diff line change
@@ -145,6 +145,7 @@ window.update = function(self)
row = info.row,
col = info.col + info.width - info.scrollbar_offset, -- info.col was already contained the scrollbar offset.
zindex = (self.style.zindex and (self.style.zindex + 1) or 1),
border = 'none',
}
if self.sbar_win and vim.api.nvim_win_is_valid(self.sbar_win) then
vim.api.nvim_win_set_config(self.sbar_win, style)
@@ -176,6 +177,7 @@ window.update = function(self)
row = info.row + thumb_offset + (info.border_info.visible and info.border_info.top or 0),
col = info.col + info.width - 1, -- info.col was already added scrollbar offset.
zindex = (self.style.zindex and (self.style.zindex + 2) or 2),
border = 'none',
}
if self.thumb_win and vim.api.nvim_win_is_valid(self.thumb_win) then
vim.api.nvim_win_set_config(self.thumb_win, style)
6 changes: 3 additions & 3 deletions lua/cmp/view/ghost_text_view.lua
Original file line number Diff line number Diff line change
@@ -90,15 +90,15 @@ ghost_text_view.text_gen = function(self, line, cursor_col)
if self.entry:get_completion_item().insertTextFormat == types.lsp.InsertTextFormat.Snippet then
word = tostring(snippet.parse(word))
end
local word_clen = vim.str_utfindex(word)
local word_clen = vim.fn.strchars(word, true)
local cword = string.sub(line, self.entry.offset, cursor_col)
local cword_clen = vim.str_utfindex(cword)
local cword_clen = vim.fn.strchars(cword, true)
-- Number of characters from entry text (word) to be displayed as ghost thext
local nchars = word_clen - cword_clen
-- Missing characters to complete the entry text
local text
if nchars > 0 then
text = string.sub(word, vim.str_byteindex(word, word_clen - nchars) + 1)
text = string.sub(word, misc.to_vimindex(word, word_clen - nchars))
else
text = ''
end