Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating the autocomplete options always auto-confirms #2123

Open
foersben opened this issue Jan 6, 2025 · 0 comments
Open

Iterating the autocomplete options always auto-confirms #2123

foersben opened this issue Jan 6, 2025 · 0 comments

Comments

@foersben
Copy link

foersben commented Jan 6, 2025

Greetings,
according to the docs my config should be right but nonetheless it seems to always autoaccept either the first or last item in the dropdown menu:

mapping = cmp.mapping.preset.insert({
	['<C-k>'] = cmp.mapping.scroll_docs(-4),
	['<C-j>'] = cmp.mapping.scroll_docs(4),
	-- ['<C-n>'] = cmp.mapping.select_next_item(),
	-- ['<C-p>'] = cmp.mapping.select_prev_item(),
	["<Tab>"] = cmp.mapping(function(fallback)
		if cmp.visible() then
			cmp.select_next_item()
		-- elseif vim.fn["vsnip#available"](1) == 1 then
		-- 	feedkey("<Plug>(vsnip-expand-or-jump)", "")
		-- elseif has_words_before() then
		-- 	cmp.complete()
		else
			fallback()
		end
	end, { "i", "s" }),
	["<S-Tab>"] = cmp.mapping(function()
		if cmp.visible() then
			cmp.select_prev_item()
		-- elseif vim.fn["vsnip#jumpable"](-1) == 1 then
		-- 	feedkey("<Plug>(vsnip-jump-prev)", "")
		else
			fallback()
		end
	end, { "i", "s" }),
	['<CR>'] = cmp.mapping.confirm({ select = true }),
}),

It either accepts the first option (tab) or accepts the last (shift + tab) option in the dropdown menu. It does not simply switch to the next item. The same happens when I enable the C-n and C-p options, currently commented.

Complete setup of nvim-cmp:

{
		'hrsh7th/nvim-cmp',
		event = { "BufReadPre", "BufNewFile" },
		dependencies = {
			{ 
				"folke/lsp-colors.nvim", 
			},
			{ 
				"folke/neodev.nvim", 
				opts = { 
					experimental = { 
						pathStrict = true 
					} 
				} 
			},
			"williamboman/mason.nvim",
			"williamboman/mason-lspconfig.nvim",
			'neovim/nvim-lspconfig',
			'hrsh7th/cmp-nvim-lsp',
			'hrsh7th/cmp-buffer',
			'hrsh7th/cmp-path',
			'hrsh7th/cmp-cmdline',
			'hrsh7th/cmp-vsnip',
			'hrsh7th/vim-vsnip',
		},
		config = function()
			-- LSP Diagnostics Options Setup 
			local sign = function(opts)
				vim.fn.sign_define(opts.name, {
					texthl = opts.name,
					text = opts.text,
					numhl = ''
				})
			end

			sign({name = 'DiagnosticSignError', text = ''})
			sign({name = 'DiagnosticSignWarn', text = ''})
			sign({name = 'DiagnosticSignHint', text = ''})
			sign({name = 'DiagnosticSignInfo', text = ''})

			vim.diagnostic.config({
				virtual_text = false,
				signs = true,
				update_in_insert = true,
				underline = true,
				severity_sort = false,
				float = {
					border = 'rounded',
					source = 'always',
					header = '',
					prefix = '',
				},
			})

			vim.o.updatetime = 250
			vim.cmd [[
			autocmd CursorHold,CursorHoldI * lua vim.diagnostic.open_float(nil, {focus=false})
			]]

			local has_words_before = function()
				local line, col = unpack(vim.api.nvim_win_get_cursor(0))
				return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
			end

			local feedkey = function(key, mode)
				vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true)
			end

			local on_attach = function(client, bufnr)
				local bufopts = { noremap=true, silent=true, buffer=bufnr }
				vim.keymap.set('n', '<leader>d', vim.lsp.buf.definition, bufopts)
			end

			-- Pyright
			local configs = require('lspconfig/configs')
			local util = require('lspconfig/util')
			local path = util.path
			local function get_python_path(workspace)
				-- Use activated virtualenv.
				if vim.env.VIRTUAL_ENV then
					return path.join(vim.env.VIRTUAL_ENV, 'bin', 'python')
				end

				-- Find and use virtualenv in workspace directory.
				for _, pattern in ipairs({'*', '.*'}) do
					local match = vim.fn.glob(path.join(workspace, pattern, 'pyvenv.cfg'))
					if match ~= '' then
						return path.join(path.dirname(match), 'bin', 'python')
					end
				end

				-- Fallback to system Python.
				-- return exepath('python3') or exepath('python') or 'python'
			end
			--
			local cmp = require('cmp')
			cmp.setup({
				snippet = {
					expand = function(args)
						vim.fn["vsnip#anonymous"](args.body)
					end,
				},
				window = {
					completion = cmp.config.window.bordered(),
					documentation = cmp.config.window.bordered(),
				},
				mapping = cmp.mapping.preset.insert({
					['<C-k>'] = cmp.mapping.scroll_docs(-4),
					['<C-j>'] = cmp.mapping.scroll_docs(4),
					-- ['<C-n>'] = cmp.mapping.select_next_item(),
					-- ['<C-p>'] = cmp.mapping.select_prev_item(),
					["<Tab>"] = cmp.mapping(function(fallback)
						if cmp.visible() then
							cmp.select_next_item()
						-- elseif vim.fn["vsnip#available"](1) == 1 then
						-- 	feedkey("<Plug>(vsnip-expand-or-jump)", "")
						-- elseif has_words_before() then
						-- 	cmp.complete()
						else
							fallback()
						end
					end, { "i", "s" }),
					["<S-Tab>"] = cmp.mapping(function()
						if cmp.visible() then
							cmp.select_prev_item()
						-- elseif vim.fn["vsnip#jumpable"](-1) == 1 then
						-- 	feedkey("<Plug>(vsnip-jump-prev)", "")
						else
							fallback()
						end
					end, { "i", "s" }),
					['<CR>'] = cmp.mapping.confirm({ select = true }),
				}),
				sources = cmp.config.sources({
					{ name = 'path' },
					{ name = 'nvim_lsp' },
					{ name = 'vsnip' },
					{ name = 'nvim_lsp_signature_help' },
					{ name = 'render-markdown' },
					{
						name = 'spell',
						option = {
							keep_all_entries = false,
							enable_in_context = function()
								return true
							end,
						},
					},
					{ name = 'buffer' },
				})
			})
			local capabilities = require('cmp_nvim_lsp').default_capabilities()
			require('lspconfig')['rust_analyzer'].setup {
				settings = {
					['rust-analyzer'] = {
						cargo = {
							buildScripts = {
								enable = true,
							},
						},
						checkOnSave = {
							allFeatures = true,
							overrideCommand = {
								'cargo', 'clippy', '--workspace', '--message-format=json', '--all-targets', '--all-features'
							}
						}
					}
				},
				capabilities = capabilities,
				on_attach = on_attach
			}
			require("lspconfig").pyright.setup{
				before_init = function(_, config)
					config.settings.python.pythonPath = get_python_path(config.root_dir)
				end,
				on_attach = on_attach,
				capabilities = capabilities,
			}
			vim.cmd [[
				set signcolumn=yes
				autocmd CursorHold * lua vim.diagnostic.open_float(nil, { focusable = false })
				]] 
		end,
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant