Skip to content

Commit eeb0c4a

Browse files
committed
chore(lsp.completion): first attempt for native lsp completion
1 parent e947649 commit eeb0c4a

File tree

1 file changed

+205
-119
lines changed

1 file changed

+205
-119
lines changed

init.lua

Lines changed: 205 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ require('lazy').setup({
487487
{ 'j-hui/fidget.nvim', opts = {} },
488488

489489
-- Allows extra capabilities provided by nvim-cmp
490-
'hrsh7th/cmp-nvim-lsp',
490+
-- 'hrsh7th/cmp-nvim-lsp',
491491
},
492492
config = function()
493493
-- Brief aside: **What is LSP?**
@@ -623,6 +623,92 @@ require('lazy').setup({
623623
end,
624624
})
625625

626+
vim.api.nvim_create_autocmd('LspAttach', {
627+
group = vim.api.nvim_create_augroup('kickstart-lsp-completion', { clear = true }),
628+
callback = function(event)
629+
local function keymap(lhs, rhs, opts, mode)
630+
opts = type(opts) == 'string' and { desc = opts } or vim.tbl_extend('error', opts --[[@as table]], { buffer = event.buf })
631+
mode = mode or 'n'
632+
vim.keymap.set(mode, lhs, rhs, opts)
633+
end
634+
635+
local function feedkeys(keys)
636+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, false, true), 'n', true)
637+
end
638+
639+
---Is the completion menu open?
640+
local function pumvisible()
641+
return tonumber(vim.fn.pumvisible()) ~= 0
642+
end
643+
644+
local client = vim.lsp.get_client_by_id(event.data.client_id)
645+
if client and client:supports_method 'textDocument/completion' then
646+
vim.lsp.completion.enable(true, client.id, event.buf, { autotrigger = true })
647+
648+
-- Use enter to accept completions.
649+
keymap('<cr>', function()
650+
return pumvisible() and '<C-y>' or '<cr>'
651+
end, { expr = true }, 'i')
652+
653+
-- Use slash to dismiss the completion menu.
654+
keymap('/', function()
655+
return pumvisible() and '<C-e>' or '/'
656+
end, { expr = true }, 'i')
657+
658+
-- Use <C-n> to navigate to the next completion or:
659+
-- - Trigger LSP completion.
660+
-- - If there's no one, fallback to vanilla omnifunc.
661+
keymap('<C-n>', function()
662+
if pumvisible() then
663+
feedkeys '<C-n>'
664+
else
665+
if next(vim.lsp.get_clients { bufnr = 0 }) then
666+
vim.lsp.completion.trigger()
667+
else
668+
if vim.bo.omnifunc == '' then
669+
feedkeys '<C-x><C-n>'
670+
else
671+
feedkeys '<C-x><C-o>'
672+
end
673+
end
674+
end
675+
end, 'Trigger/select next completion', 'i')
676+
677+
-- Buffer completions.
678+
keymap('<C-u>', '<C-x><C-n>', { desc = 'Buffer completions' }, 'i')
679+
680+
-- Use <Tab> to accept a Copilot suggestion, navigate between snippet tabstops,
681+
-- or select the next completion.
682+
-- Do something similar with <S-Tab>.
683+
keymap('<Tab>', function()
684+
local copilot = require 'copilot.suggestion'
685+
686+
if copilot.is_visible() then
687+
copilot.accept()
688+
elseif pumvisible() then
689+
feedkeys '<C-n>'
690+
elseif vim.snippet.active { direction = 1 } then
691+
vim.snippet.jump(1)
692+
else
693+
feedkeys '<Tab>'
694+
end
695+
end, {}, { 'i', 's' })
696+
keymap('<S-Tab>', function()
697+
if pumvisible() then
698+
feedkeys '<C-p>'
699+
elseif vim.snippet.active { direction = -1 } then
700+
vim.snippet.jump(-1)
701+
else
702+
feedkeys '<S-Tab>'
703+
end
704+
end, {}, { 'i', 's' })
705+
706+
-- Inside a snippet, use backspace to remove the placeholder.
707+
keymap('<BS>', '<C-o>s', {}, 's')
708+
end
709+
end,
710+
})
711+
626712
-- Diagnostic Config
627713
-- See :help vim.diagnostic.Opts
628714
vim.diagnostic.config {
@@ -657,7 +743,7 @@ require('lazy').setup({
657743
-- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.
658744
-- So, we create new capabilities with nvim cmp, and then broadcast that to the servers.
659745
local capabilities = vim.lsp.protocol.make_client_capabilities()
660-
capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
746+
-- capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
661747

662748
-- Enable the following language servers
663749
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
@@ -775,123 +861,123 @@ require('lazy').setup({
775861
},
776862
},
777863

778-
{ -- Autocompletion
779-
'hrsh7th/nvim-cmp',
780-
event = 'InsertEnter',
781-
dependencies = {
782-
-- Snippet Engine & its associated nvim-cmp source
783-
{
784-
'L3MON4D3/LuaSnip',
785-
build = (function()
786-
-- Build Step is needed for regex support in snippets.
787-
-- This step is not supported in many windows environments.
788-
-- Remove the below condition to re-enable on windows.
789-
if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then
790-
return
791-
end
792-
return 'make install_jsregexp'
793-
end)(),
794-
dependencies = {
795-
-- `friendly-snippets` contains a variety of premade snippets.
796-
-- See the README about individual language/framework/plugin snippets:
797-
-- https://github.com/rafamadriz/friendly-snippets
798-
-- {
799-
-- 'rafamadriz/friendly-snippets',
800-
-- config = function()
801-
-- require('luasnip.loaders.from_vscode').lazy_load()
802-
-- end,
803-
-- },
804-
},
805-
},
806-
'saadparwaiz1/cmp_luasnip',
807-
808-
-- Adds other completion capabilities.
809-
-- nvim-cmp does not ship with all sources by default. They are split
810-
-- into multiple repos for maintenance purposes.
811-
'hrsh7th/cmp-nvim-lsp',
812-
'hrsh7th/cmp-path',
813-
'hrsh7th/cmp-nvim-lsp-signature-help',
814-
},
815-
config = function()
816-
-- See `:help cmp`
817-
local cmp = require 'cmp'
818-
local luasnip = require 'luasnip'
819-
luasnip.config.setup {}
820-
821-
cmp.setup {
822-
snippet = {
823-
expand = function(args)
824-
luasnip.lsp_expand(args.body)
825-
end,
826-
},
827-
completion = { completeopt = 'menu,menuone,noinsert' },
828-
829-
-- For an understanding of why these mappings were
830-
-- chosen, you will need to read `:help ins-completion`
831-
--
832-
-- No, but seriously. Please read `:help ins-completion`, it is really good!
833-
mapping = cmp.mapping.preset.insert {
834-
-- Select the [n]ext item
835-
['<C-n>'] = cmp.mapping.select_next_item(),
836-
-- Select the [p]revious item
837-
['<C-p>'] = cmp.mapping.select_prev_item(),
838-
839-
-- Scroll the documentation window [b]ack / [f]orward
840-
['<C-b>'] = cmp.mapping.scroll_docs(-4),
841-
['<C-f>'] = cmp.mapping.scroll_docs(4),
842-
843-
-- Accept ([y]es) the completion.
844-
-- This will auto-import if your LSP supports it.
845-
-- This will expand snippets if the LSP sent a snippet.
846-
['<C-y>'] = cmp.mapping.confirm { select = true },
847-
848-
-- If you prefer more traditional completion keymaps,
849-
-- you can uncomment the following lines
850-
--['<CR>'] = cmp.mapping.confirm { select = true },
851-
--['<Tab>'] = cmp.mapping.select_next_item(),
852-
--['<S-Tab>'] = cmp.mapping.select_prev_item(),
853-
854-
-- Manually trigger a completion from nvim-cmp.
855-
-- Generally you don't need this, because nvim-cmp will display
856-
-- completions whenever it has completion options available.
857-
['<C-Space>'] = cmp.mapping.complete {},
858-
859-
-- Think of <c-l> as moving to the right of your snippet expansion.
860-
-- So if you have a snippet that's like:
861-
-- function $name($args)
862-
-- $body
863-
-- end
864-
--
865-
-- <c-l> will move you to the right of each of the expansion locations.
866-
-- <c-h> is similar, except moving you backwards.
867-
['<C-l>'] = cmp.mapping(function()
868-
if luasnip.expand_or_locally_jumpable() then
869-
luasnip.expand_or_jump()
870-
end
871-
end, { 'i', 's' }),
872-
['<C-h>'] = cmp.mapping(function()
873-
if luasnip.locally_jumpable(-1) then
874-
luasnip.jump(-1)
875-
end
876-
end, { 'i', 's' }),
877-
878-
-- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see:
879-
-- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps
880-
},
881-
sources = {
882-
{
883-
name = 'lazydev',
884-
-- set group index to 0 to skip loading LuaLS completions as lazydev recommends it
885-
group_index = 0,
886-
},
887-
{ name = 'nvim_lsp' },
888-
{ name = 'luasnip' },
889-
{ name = 'path' },
890-
{ name = 'nvim_lsp_signature_help' },
891-
},
892-
}
893-
end,
894-
},
864+
-- { -- Autocompletion
865+
-- 'hrsh7th/nvim-cmp',
866+
-- event = 'InsertEnter',
867+
-- dependencies = {
868+
-- -- Snippet Engine & its associated nvim-cmp source
869+
-- {
870+
-- 'L3MON4D3/LuaSnip',
871+
-- build = (function()
872+
-- -- Build Step is needed for regex support in snippets.
873+
-- -- This step is not supported in many windows environments.
874+
-- -- Remove the below condition to re-enable on windows.
875+
-- if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then
876+
-- return
877+
-- end
878+
-- return 'make install_jsregexp'
879+
-- end)(),
880+
-- dependencies = {
881+
-- -- `friendly-snippets` contains a variety of premade snippets.
882+
-- -- See the README about individual language/framework/plugin snippets:
883+
-- -- https://github.com/rafamadriz/friendly-snippets
884+
-- -- {
885+
-- -- 'rafamadriz/friendly-snippets',
886+
-- -- config = function()
887+
-- -- require('luasnip.loaders.from_vscode').lazy_load()
888+
-- -- end,
889+
-- -- },
890+
-- },
891+
-- },
892+
-- 'saadparwaiz1/cmp_luasnip',
893+
--
894+
-- -- Adds other completion capabilities.
895+
-- -- nvim-cmp does not ship with all sources by default. They are split
896+
-- -- into multiple repos for maintenance purposes.
897+
-- 'hrsh7th/cmp-nvim-lsp',
898+
-- 'hrsh7th/cmp-path',
899+
-- 'hrsh7th/cmp-nvim-lsp-signature-help',
900+
-- },
901+
-- config = function()
902+
-- -- See `:help cmp`
903+
-- local cmp = require 'cmp'
904+
-- local luasnip = require 'luasnip'
905+
-- luasnip.config.setup {}
906+
--
907+
-- cmp.setup {
908+
-- snippet = {
909+
-- expand = function(args)
910+
-- luasnip.lsp_expand(args.body)
911+
-- end,
912+
-- },
913+
-- completion = { completeopt = 'menu,menuone,noinsert' },
914+
--
915+
-- -- For an understanding of why these mappings were
916+
-- -- chosen, you will need to read `:help ins-completion`
917+
-- --
918+
-- -- No, but seriously. Please read `:help ins-completion`, it is really good!
919+
-- mapping = cmp.mapping.preset.insert {
920+
-- -- Select the [n]ext item
921+
-- ['<C-n>'] = cmp.mapping.select_next_item(),
922+
-- -- Select the [p]revious item
923+
-- ['<C-p>'] = cmp.mapping.select_prev_item(),
924+
--
925+
-- -- Scroll the documentation window [b]ack / [f]orward
926+
-- ['<C-b>'] = cmp.mapping.scroll_docs(-4),
927+
-- ['<C-f>'] = cmp.mapping.scroll_docs(4),
928+
--
929+
-- -- Accept ([y]es) the completion.
930+
-- -- This will auto-import if your LSP supports it.
931+
-- -- This will expand snippets if the LSP sent a snippet.
932+
-- ['<C-y>'] = cmp.mapping.confirm { select = true },
933+
--
934+
-- -- If you prefer more traditional completion keymaps,
935+
-- -- you can uncomment the following lines
936+
-- --['<CR>'] = cmp.mapping.confirm { select = true },
937+
-- --['<Tab>'] = cmp.mapping.select_next_item(),
938+
-- --['<S-Tab>'] = cmp.mapping.select_prev_item(),
939+
--
940+
-- -- Manually trigger a completion from nvim-cmp.
941+
-- -- Generally you don't need this, because nvim-cmp will display
942+
-- -- completions whenever it has completion options available.
943+
-- ['<C-Space>'] = cmp.mapping.complete {},
944+
--
945+
-- -- Think of <c-l> as moving to the right of your snippet expansion.
946+
-- -- So if you have a snippet that's like:
947+
-- -- function $name($args)
948+
-- -- $body
949+
-- -- end
950+
-- --
951+
-- -- <c-l> will move you to the right of each of the expansion locations.
952+
-- -- <c-h> is similar, except moving you backwards.
953+
-- ['<C-l>'] = cmp.mapping(function()
954+
-- if luasnip.expand_or_locally_jumpable() then
955+
-- luasnip.expand_or_jump()
956+
-- end
957+
-- end, { 'i', 's' }),
958+
-- ['<C-h>'] = cmp.mapping(function()
959+
-- if luasnip.locally_jumpable(-1) then
960+
-- luasnip.jump(-1)
961+
-- end
962+
-- end, { 'i', 's' }),
963+
--
964+
-- -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see:
965+
-- -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps
966+
-- },
967+
-- sources = {
968+
-- {
969+
-- name = 'lazydev',
970+
-- -- set group index to 0 to skip loading LuaLS completions as lazydev recommends it
971+
-- group_index = 0,
972+
-- },
973+
-- { name = 'nvim_lsp' },
974+
-- { name = 'luasnip' },
975+
-- { name = 'path' },
976+
-- { name = 'nvim_lsp_signature_help' },
977+
-- },
978+
-- }
979+
-- end,
980+
-- },
895981

896982
{ -- You can easily change to a different colorscheme.
897983
-- Change the name of the colorscheme plugin below, and then

0 commit comments

Comments
 (0)