Skip to content

Commit

Permalink
feat(completion): add cmp-under-comparator (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamilcuk committed Jun 16, 2024
1 parent 2289358 commit 2387d05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lua/astrocommunity/completion/cmp-under-comparator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# cmp-under-comparator

A tiny function for nvim-cmp to better sort completion items that start with one or more underlines.

In most languages, especially Python, items that start with one or more underlines should be at the end of the completion suggestion.

**Repository:** <https://github.com/lukas-reineke/cmp-under-comparator>
26 changes: 26 additions & 0 deletions lua/astrocommunity/completion/cmp-under-comparator/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local function list_index(t, value)
for i, v in ipairs(t) do
if v == value then return i end
end
return nil
end

---@type LazySpec
return {
"hrsh7th/nvim-cmp",
optional = true,
dependencies = { "lukas-reineke/cmp-under-comparator", lazy = true },
opts = function(_, opts)
local cmp = require "cmp"
opts.sorting = opts.sorting or {}
-- If comparators is not set, use the default. get_config() has defaults now.
opts.sorting.comparators = opts.sorting.comparators or cmp.get_config().sorting.comparators
-- Find element in comparators we will position ourselves after.
-- Position after recently_used, fallback to after score, fallback to 4th position.
-- recently_used was not in nvim-cmp some time ago.
local pos = list_index(opts.sorting.comparators, cmp.config.compare.recently_used)
if pos == nil then pos = list_index(opts.sorting.comparators, cmp.config.compare.score) end
if pos == nil then pos = 3 end
table.insert(opts.sorting.comparators, pos + 1, require("cmp-under-comparator").under)
end,
}

0 comments on commit 2387d05

Please sign in to comment.