Skip to content

Commit

Permalink
Builtin completion
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarAbdi committed Sep 7, 2024
1 parent a9295a5 commit 4dd4067
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 165 deletions.
194 changes: 42 additions & 152 deletions neovim/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,6 @@ vim.api.nvim_create_autocmd("FileType", {
end,
})

vim.api.nvim_create_autocmd("FileType", {
pattern = { "cpp", "c" },
group = general_group,
callback = function()
-- This fixes an issue with nvim-cmp -- see https://github.com/hrsh7th/nvim-cmp/issues/1035#issuecomment-1195456419
vim.opt_local.cindent = false
end,
})
-- A terrible way to handle symlinks
vim.api.nvim_create_autocmd("BufWinEnter", {
callback = function(params)
Expand Down Expand Up @@ -465,13 +457,52 @@ vim.api.nvim_create_autocmd("TermOpen", {
group = general_group,
})

local completion = {
pattern = "\\k",
}

vim.api.nvim_create_autocmd("InsertCharPre", {
callback = function()
if tonumber(vim.fn.pumvisible()) ~= 0 then
return
end
local function feedkeys(keys)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), "n", true)
end
if vim.fn.match(vim.v.char, completion.pattern) >= 0 then
if
vim.iter(vim.lsp.get_clients({ bufnr = 0 })):any(function(client)
return client.supports_method(vim.lsp.protocol.Methods.textDocument_completion)
end)
then
vim.lsp.completion.trigger()
else
feedkeys("<C-n>")
end
end
end,
group = general_group,
})

vim.api.nvim_create_autocmd("FocusGained", { command = "checktime", group = general_group })
vim.api.nvim_create_autocmd("LspDetach", {
callback = function(args)
vim.lsp.completion.enable(false, args.data.client_id, args.buf)
end,
group = lsp_group,
})
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
vim.lsp.completion.enable(true, args.data.client_id, args.buf, { autotrigger = true })
vim.keymap.set("i", "<c-space>", function()
vim.lsp.completion.trigger()
end, { buffer = args.buf })
vim.keymap.set("i", "<CR>", function()
return tonumber(vim.fn.pumvisible()) ~= 0 and "<C-y>" or "<cr>"
end, { expr = true, buffer = args.buf })
vim.keymap.set({ "n", "i" }, "<C-k>", function()
local cmp = require("cmp")
if cmp.visible() then
cmp.close()
if tonumber(vim.fn.pumvisible()) == 1 then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<c-y>", true, false, true), "n", true)
end
vim.lsp.buf.signature_help()
end, { buffer = args.buf, silent = true })
Expand Down Expand Up @@ -1139,133 +1170,6 @@ require("lazy").setup({
}
end,
},
{
"hrsh7th/nvim-cmp",
event = { "InsertEnter" },
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
},
config = function()
local cmp = require("cmp")
local compare = require("cmp.config.compare")
local cache = {}
local cmp_source = {
complete = function(_, params, callback)
local bufnr = vim.api.nvim_get_current_buf()
if not cache[bufnr] then
local completion_items = vim.tbl_map(function(snippet)
---@type lsp.CompletionItem
local item = {
documentation = {
kind = cmp.lsp.MarkupKind.PlainText,
value = snippet.description or "",
},
word = snippet.trigger,
label = snippet.trigger,
kind = vim.lsp.protocol.CompletionItemKind.Snippet,
insertText = type(snippet.body) == "function" and snippet.body() or snippet.body,
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
}
return item
end, get_buffer_snippets(params.context.filetype))
cache[bufnr] = completion_items
end

callback(cache[bufnr])
end,
}

cmp.register_source("snippets", cmp_source)
cmp.setup({
snippet = {
expand = function(args)
vim.snippet.expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["Tab"] = cmp.config.disable,
["S-Tab"] = cmp.config.disable,
["<C-f>"] = cmp.config.disable,
["<C-d>"] = cmp.mapping.scroll_docs(4),
["<C-u>"] = cmp.mapping.scroll_docs(-4),
["<CR>"] = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Insert,
select = false,
}),
}),
sources = {
{ name = "nvim_lsp" },
{ name = "snippets" },
{
name = "buffer",
option = {
get_bufnrs = function()
return vim.api.nvim_list_bufs()
end,
},
},
},
formatting = {
format = function(entry, vim_item)
vim_item.menu = ({
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
snippets = "[Snippet]",
})[entry.source.name]
local label = vim_item.abbr
-- https://github.com/hrsh7th/nvim-cmp/discussions/609
local ELLIPSIS_CHAR = ""
local MAX_LABEL_WIDTH = math.floor(vim.o.columns * 0.4)
local truncated_label = vim.fn.strcharpart(label, 0, MAX_LABEL_WIDTH)
if truncated_label ~= label then
vim_item.abbr = truncated_label .. ELLIPSIS_CHAR
end
return vim_item
end,
},
sorting = {
comparators = {
compare.offset,
compare.exact,
-- compare.score,
-- https://github.com/p00f/clangd_extensions.nvim/blob/main/lua/clangd_extensions/cmp_scores.lua
function(entry1, entry2)
local diff
if entry1.completion_item.score and entry2.completion_item.score then
diff = (entry2.completion_item.score * entry2.score)
- (entry1.completion_item.score * entry1.score)
else
diff = entry2.score - entry1.score
end
if diff < 0 then
return true
elseif diff > 0 then
return false
end
end,
-- https://github.com/lukas-reineke/cmp-under-comparator
function(entry1, entry2)
local _, entry1_under = entry1.completion_item.label:find("^_+")
local _, entry2_under = entry2.completion_item.label:find("^_+")
entry1_under = entry1_under or 0
entry2_under = entry2_under or 0
if entry1_under > entry2_under then
return false
elseif entry1_under < entry2_under then
return true
end
end,
compare.recently_used,
compare.kind,
compare.sort_text,
compare.length,
compare.order,
},
},
})
end,
},
}, {
defaults = {
lazy = true, -- every plugin is lazy-loaded by default
Expand Down Expand Up @@ -1383,20 +1287,6 @@ end

local q = require("qwahl")

local function try_jump(direction, key)
if vim.snippet.active({ direction = direction }) then
return string.format("<cmd>lua vim.snippet.jump(%d)<cr>", direction)
end
return key
end

vim.keymap.set({ "i", "s" }, "<Tab>", function()
return try_jump(1, "<Tab>")
end, { expr = true })
vim.keymap.set({ "i", "s" }, "<S-Tab>", function()
return try_jump(-1, "<S-Tab>")
end, { expr = true })

vim.keymap.set("t", "<ESC>", [[<C-\><C-n>]], { silent = true })
vim.keymap.set({ "i", "s" }, "<ESC>", function()
if vim.snippet then
Expand Down
14 changes: 1 addition & 13 deletions neovim/.config/nvim/lazy-lock.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
{
"cmp-buffer": {
"branch": "main",
"commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa"
},
"cmp-nvim-lsp": {
"branch": "main",
"commit": "39e2eda76828d88b773cc27a3f61d2ad782c922d"
},
"copilot.vim": {
"branch": "release",
"commit": "a9fa7946d7307ceb138bfe2706ba75febf6450da"
"commit": "782461159655b259cff10ecff05efa761e3d4764"
},
"lazy.nvim": {
"branch": "main",
"commit": "077102c5bfc578693f12377846d427f49bc50076"
},
"nvim-cmp": {
"branch": "main",
"commit": "ae644feb7b67bf1ce4260c231d1d4300b19c6f30"
},
"nvim-dap": {
"branch": "master",
"commit": "281a2e4cd1e7a17cea7ecb1745d84a8ab1249925"
Expand Down

0 comments on commit 4dd4067

Please sign in to comment.