C++ Autocomplete list

Edit: This problem also happens in Emas, but today I’ve tried Helix and works excellent as VS Code.
Edit2: Some else has the same problem, with same kind of project (sdl2). cmp finds completion , then doesn't , then doesn't again · Discussion #636 · hrsh7th/nvim-cmp · GitHub

HI, and sorry for re-iterate the post, but I cant complete my setup of NeoVim, I’ve been one week researching and nothing.

The last week changed my vim setup to lua, I think works better, not for the lua file, just, its organized. Well, one thing still “not working”:

It shows autocomplete, with the correct methods, but lacks(they are there, but need to force to show) some methods/functions/keywords until I press: Ctrl + Space, it shows correctly.
For example:

In this image, Shows the autocomplete “correct” but lack the method that I am looking for. But Even if still typing, the autocomplete disappear.

In this Image, I needed to press Ctrl + Space to show the method.

And (sorry for comparing) in VS Code I do not have this issue.

######################
This is my config:

 "lspconfig")
if not status_ok then
	return
end

local status_ok, cmp = pcall(require, "cmp")
if not status_ok then
	return
end

local status_ok, lsp_signature = pcall(require, "lsp_signature")
if not status_ok then
	return
end

local status_ok, clangd_extensions = pcall(require, "clangd_extensions")
if not status_ok then
	return
end

local status_ok, crates = pcall(require, "crates")
if not status_ok then
	return
end

local util = lspconfig.util
-- Setup nvim-cmp.
local lspkind = require('lspkind')

cmp.setup({
	formatting = {
		format = lspkind.cmp_format({ with_text = false, maxwidth = 50, menu = ({
			buffer = "[Buffer]",
			nvim_lsp = "[LSP]",
			luasnip = "[LuaSnip]",
			nvim_lua = "[Lua]",
			latex_symbols = "[Latex]",
		}) }),
	},
	snippet = {
		expand = function(args)
			-- For `vsnip` user.
			vim.fn["vsnip#anonymous"](args.body)
		end,
	},
	mapping = cmp.mapping.preset.insert ({
		['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
		['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
		['<Down>'] = cmp.mapping.close(),
		['<Up>'] = cmp.mapping.close(),
		['<Left>'] = cmp.mapping.close(),
		['<Right>'] = cmp.mapping.close(),
		['<C-b>'] = cmp.mapping.scroll_docs(-4),
		['<C-f>'] = cmp.mapping.scroll_docs(4),
		['<C-Space>'] = cmp.mapping.complete(),
		['<C-e>'] = cmp.mapping.abort(),
		['<Tab>'] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 's' }),
		['<S-Tab>'] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 's' }),
		['<CR>'] = cmp.mapping.confirm({
			behavior = cmp.ConfirmBehavior.Replace,
			select = true
		}),
	}),
	sources = {
		{ name = 'nvim_lsp' },

		-- For vsnip user.
		{ name = 'vsnip' },

		{ name = 'buffer', keyword_length = 3, },
		{ name = 'path' },
		{ name = 'calc' },
		{ name = 'nvim_lua' },
		{ name = 'latex_symbols' },
		{ name = 'treesitter' },
	},
	sorting = {
		comparators = {
			cmp.config.compare.offset,
			cmp.config.compare.exact,
			cmp.config.compare.recently_used,
			require("clangd_extensions.cmp_scores"),
			cmp.config.compare.kind,
			cmp.config.compare.sort_text,
			cmp.config.compare.length,
			cmp.config.compare.order,
		},
	},
})

-- Set configuration for specific filetype.
cmp.setup.filetype('gitcommit', {
	sources = cmp.config.sources({
		{ name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it.
	}, {
		{ name = 'buffer' },
	})
})

-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline('/', {
	mapping = cmp.mapping.preset.cmdline(),
	sources = cmp.config.sources({ -- Updated with cmp-nvim-lsp-document-symbol
    { name = 'nvim_lsp_document_symbol' }
  }, {
    { name = 'buffer' }
  })
  	-- Old
	-- sources = {
	-- 	{ name = 'buffer' }
	-- }
})

-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
	mapping = cmp.mapping.preset.cmdline(),
	sources = cmp.config.sources({
		{ name = 'path' }
	}, {
		{ name = 'cmdline',
			  keyword_length = 1,
			-- completeopt = 'menu,menuone,noinsert', -- Auto select(dont use it)
		}
	})
})

local capabilities = vim.lsp.protocol.make_client_capabilities()
my_capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
	-- Enable completion triggered by <c-x><c-o>
	vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

	if client.resolved_capabilities.document_formatting then
		vim.api.nvim_command [[augroup Format]]
		vim.api.nvim_command [[autocmd! * <buffer>]]
		vim.api.nvim_command [[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]]
		vim.api.nvim_command [[augroup END]]
	end -- Format


	--	This.
	if client.resolved_capabilities.document_highlight then
		vim.api.nvim_exec([[
	"" Just adding collor highlight.
	:hi LspReferenceRead cterm=bold ctermbg=red guibg=#3d3d3d
	:hi LspReferenceText cterm=bold ctermbg=red guibg=#3d3d3d
	:hi LspReferenceWrite cterm=bold ctermbg=red guibg=#3d3d3d
	   augroup lsp_document_highlight
		"autocmd! * <buffer>
		autocmd!
		autocmd CursorHold  <buffer> lua vim.lsp.buf.document_highlight()
		autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
		autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
		" autocmd CursorHold *.* :lua vim.lsp.diagnostic.show_line_diagnostics()
		autocmd BufWritePre * lua vim.lsp.buf.formatting()
		" autocmd BufWritePre * lua vim.lsp.buf.formatting_sync(nil, 300)
	   augroup END
	]], false)
	end

end


-- ######################################################################
-- ######################################################################

-- This was the original

-- lspconfig.clangd.setup {
-- 	flags = { allow_incremental_sync = true, debounce_text_changes = 500 },
-- 	cmd = {
-- 		"clangd",
-- 		-- "clangd",
-- 		"--background-index",
-- 		"--pch-storage=memory",
-- 		"--clang-tidy",
-- 		"--completion-style=detailed",
-- 		-- "--suggest-missing-includes",
-- 		"--all-scopes-completion",
-- 		"--cross-file-rename",
-- 		--"--log=verbose",
-- 		"--pretty",
-- 		"--header-insertion=never",
-- 		"--query-driver=/usr/include/*",
-- 		"-j=2",
-- 		"--enable-config"
-- 	},
-- 	filetypes = { "c", "cpp" },
-- 	-- filetypes = {"c", "cpp", "objc", "objcpp"},
-- 	root_dir = util.root_pattern(".clangd", "compile_commands.json", "compile_flags", ".git"),
-- 	--root_dir = util.root_pattern("compile_commands.json", "compile_flags.txt", ".git")
-- 	-- init_option = { fallbackFlags = {  "-std=c++2a"  } }

-- 	-- on_init = custom_init,
-- 	on_attach = on_attach,
-- 	capabilities = capabilities,
-- }

-- ################################################################
--

-- No matter if I use this plugin:
clangd_extensions.setup {
	server = {
		-- options to pass to nvim-lspconfig
		-- i.e. the arguments to require("lspconfig").clangd.setup({})
		on_attach = on_attach,
		-- ##########
		cmd = {
			"clangd",
			-- "clangd",
			"--background-index",
			"--pch-storage=memory",
			"--clang-tidy",
			"--completion-style=detailed",
			-- "--suggest-missing-includes",
			"--all-scopes-completion",
			"--cross-file-rename",
			--"--log=verbose",
			"--pretty",
			"--header-insertion=never",
			-- "--query-driver=/usr/include/*",
			"-j=8",
			"--enable-config"
		},
		--
		capabilities = my_capabilities,
	},
	extensions = {
		-- defaults:
		-- Automatically set inlay hints (type hints)
		autoSetHints = true,
		-- Whether to show hover actions inside the hover window
		-- This overrides the default hover handler
		hover_with_actions = true,
		-- These apply to the default ClangdSetInlayHints command
		inlay_hints = {
			-- Only show inlay hints for the current line
			only_current_line = false,
			-- Event which triggers a refersh of the inlay hints.
			-- You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but
			-- not that this may cause  higher CPU usage.
			-- This option is only respected when only_current_line and
			-- autoSetHints both are true.
			only_current_line_autocmd = "CursorHold",
			-- whether to show parameter hints with the inlay hints or not
			show_parameter_hints = true,
			-- prefix for parameter hints
			parameter_hints_prefix = "<- ",
			-- prefix for all the other hints (type, chaining)
			other_hints_prefix = "=> ",
			-- whether to align to the length of the longest line in the file
			max_len_align = false,
			-- padding from the left if max_len_align is true
			max_len_align_padding = 1,
			-- whether to align to the extreme right or not
			right_align = false,
			-- padding from the right if right_align is true
			right_align_padding = 7,
			-- The color of the hints
			highlight = "Comment",
			-- The highlight group priority for extmark
			priority = 100,
		},
		ast = {
			role_icons = {
				type = "",
				declaration = "",
				expression = "",
				specifier = "",
				statement = "",
				["template argument"] = "",
			},

			kind_icons = {
				Compound = "",
				Recovery = "",
				TranslationUnit = "",
				PackExpansion = "",
				TemplateTypeParm = "",
				TemplateTemplateParm = "",
				TemplateParamObject = "",
			},

			highlights = {
				detail = "Comment",
			},
			memory_usage = {
				border = "none",
			},
			symbol_info = {
				border = "none",
			},
		},
	}
}


-- ######################################################################
-- ######################################################################
-- require 'lspconfig'.cmake.setup {
lspconfig.cmake.setup {
	filetypes = { 'cmake' },
	on_attach = on_attach,
	init_options = {
		buildDirectory = {
			"build/Debug",
			"build/Release"
		}
	},
	capabilities = my_capabilities,
}


-- ######################################################################
-- ######################################################################
-- Rust
lspconfig.rust_analyzer.setup({
	on_attach = on_attach,
	settings = {
		["rust-analyzer"] = {
			assist = {
				importGranularity = "module",
				importPrefix = "by_self",
			},
			cargo = {
				loadOutDirsFromCheck = true
			},
			procMacro = {
				enable = true
			},
		}
	},
	capabilities = my_capabilities,
});

-- ######################################################################
-- ######################################################################
-- LUA


local system_name
if vim.fn.has("mac") == 1 then
	system_name = "macOS"
elseif vim.fn.has("unix") == 1 then
	system_name = "Linux"
elseif vim.fn.has('win32') == 1 then
	system_name = "Windows"
else
	print("Unsupported system for sumneko")
end

-- set the path to the sumneko installation; if you previously installed via the now deprecated :LspInstall, use
-- local sumneko_root_path = vim.fn.stdpath('cache')..'/lspconfig/sumneko_lua/lua-language-server'
local sumneko_root_path = '/home/cris/.My_Libs/LuaLSP/lua-language-server'
local sumneko_binary = sumneko_root_path .. "/bin/" .. system_name .. "/lua-language-server"

local runtime_path = vim.split(package.path, ';')
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")

lspconfig.sumneko_lua.setup {
	cmd = { sumneko_binary, "-E", sumneko_root_path .. "/main.lua" };
	settings = {
		Lua = {
			runtime = {
				-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
				version = 'LuaJIT',
				-- Setup your lua path
				path = runtime_path,
			},
			diagnostics = {
				-- Get the language server to recognize the `vim` global
				globals = { 'vim' },
			},
			workspace = {
				-- Make the server aware of Neovim runtime files
				library = vim.api.nvim_get_runtime_file("", true),
			},
			-- Do not send telemetry data containing a randomized but unique identifier
			telemetry = {
				enable = false,
			},
		},
	},
}

lsp_signature.setup({
	bind = true,
	doc_lines = 10,
	fix_pos = false,
	floating_window = true,
	floating_window_above_cur_line = true, -- Need it. If turn off, other settings does not work
	hint_enable = false,
	--	auto_close_after = ,
	use_lspsaga = false,
	always_trigger = false,
	transpancy = 0,
	max_height = 10,
	max_width = 120,
	handler_opts = {
		border = "rounded" -- double, rounded, single, shadow, none
	},
	-- toggle_key = '<M-k>'
})

-- #########################################################################
-- #########################################################################

vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
	virtual_text = {
		prefix = '■', -- Could be '●', '▎', 'x'
		source = "always", -- Or "if_many"
	},
	signs = true,
	underline = true,
	update_in_insert = true,
})

vim.diagnostic.config({
	virtual_text = true,
	signs = true,
	underline = true,
	update_in_insert = false,
	severity_sort = true,
})

local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
for type, icon in pairs(signs) do
	local hl = "DiagnosticSign" .. type
	vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end


I’m not sure what the issue is, but I also sometimes get a slightly different set of completion options after hitting c-space, vs. just letting autocomplete happen without a trigger key. But I rarely/never need to hit c-space it because I’ve setup the ordering of results such that the one I want is almost always towards the top automatically (and if it’s not, I just type another character or two).

Just a guess, but what might be happening is that you have your cmp.sorting.comparators setup so that something else is being given priority weight over the exact prefix match that you want.
Here’s how I have it:

	sources = {
		{name = "nvim_lsp", priority = 9},
		{name = "nvim_lua", priority = 8},
		{name = "buffer", priority = 7, keyword_length = 3, max_item_count=10},
		{name = "dictionary", priority = 5, keyword_length = 5, keyword_pattern = [[\w\+]], max_item_count=4},
		{name = "path", priority = 4},
		{name = "calc", priority = 3},
	},

	sorting = {
		priority_weight = 1.0,
		comparators = {
		  cmp.config.compare.locality,
		  cmp.config.compare.recently_used,
		  cmp.config.compare.score,
		  cmp.config.compare.offset,
		  cmp.config.compare.order,
		  cmp.config.compare.scope,
		},
	},

If you think this is the issue, I’d recommend getting help with nvim-cmp since it’s an issue with configuring that plugin, not an issue with nvim or nvim-lspconfig.

Thank you, but nothing, same result.
I will continue check, and as said at first, someone has the same problem, almost with the same project (SDL2).

I think is lsp, because I’ve switched to coc, and works excellent.

Edit: After some time, happens the same. Returned to cmp and lsp.