Skip to content

Commit

Permalink
Add nvim-cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
phelipetls committed Jul 8, 2023
1 parent 90ee12b commit a8ec9b9
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ require("plugins.config.fzf_lua")
-- lsp
vim.cmd("packadd! nvim-lspconfig")
vim.cmd("packadd! null-ls.nvim")
vim.cmd("packadd! cmp-nvim-lsp")
vim.cmd("packadd! cmp-path")
vim.cmd("packadd! cmp-buffer")
vim.cmd("packadd! nvim-cmp")
vim.cmd("packadd! cmp-vsnip")
vim.cmd("packadd! vim-vsnip")
require("plugins.config.lsp")

-- incremental search/substitute highlighting
Expand Down
121 changes: 69 additions & 52 deletions .config/nvim/lua/plugins/config/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,82 @@
-- {{{ cmp

local cmp = require("cmp")

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

cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-u>"] = cmp.mapping.scroll_docs(-4),
["<C-d>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
return
end

if has_words_before() then
cmp.complete()
return
end

fallback()
end),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
return
end

fallback()
end),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "vsnip" },
}, {
{ name = "buffer" },
{ name = "path" },
}),
})

-- }}}
-- {{{ lspconfig

local lspconfig = require("lspconfig")

lspconfig.tsserver.setup({})
lspconfig.astro.setup({})
lspconfig.lua_ls.setup({})
lspconfig.html.setup({})
lspconfig.cssls.setup({})
lspconfig.jsonls.setup({})
local cmp_capabilities = require("cmp_nvim_lsp").default_capabilities()
local opts = {
capabilities = cmp_capabilities,
}

lspconfig.tsserver.setup(opts)
lspconfig.astro.setup(opts)
lspconfig.lua_ls.setup(opts)
lspconfig.html.setup(opts)
lspconfig.cssls.setup(opts)
lspconfig.jsonls.setup(opts)
lspconfig.tailwindcss.setup({
capabilities = cmp_capabilities,
settings = {
tailwindCSS = {
classAttributes = { "class", "class:list", "className", "classList", "ngClass" },
validate = true
}
}
validate = true,
},
},
})
lspconfig.eslint.setup({
capabilities = cmp_capabilities,
on_attach = function(_, bufnr)
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
Expand Down Expand Up @@ -80,8 +140,6 @@ vim.api.nvim_create_autocmd("LspAttach", {
-- vim.keymap.set("n", "<C-w><C-d>", vim.lsp.buf.definition, opts)
vim.keymap.set("n", "[t", vim.lsp.buf.type_definition, opts)

vim.keymap.set("i", "<C-Space>", vim.lsp.buf.signature_help, opts)

vim.keymap.set("n", "<F2>", vim.lsp.buf.rename, opts)

vim.keymap.set({ "n", "v" }, "<M-CR>", vim.lsp.buf.code_action, opts)
Expand All @@ -107,46 +165,5 @@ vim.keymap.set("n", "[g", vim.diagnostic.goto_prev)
vim.keymap.set("n", "]g", vim.diagnostic.goto_next)
vim.keymap.set("n", "<C-Space>", vim.diagnostic.open_float)

local function is_preceded_by_whitespace()
local col = vim.api.nvim_win_get_cursor(0)[2]
local current_line = vim.api.nvim_get_current_line()
local prev_char = current_line:sub(col, col)
return col == 0 or prev_char:find("%s") ~= nil
end

vim.keymap.set("i", "<Tab>", function()
if vim.fn.pumvisible() == 1 then
return vim.api.nvim_replace_termcodes("<C-n>", true, true, true)
end

if is_preceded_by_whitespace() then
return vim.api.nvim_replace_termcodes("<Tab>", true, true, true)
end

if vim.bo.omnifunc ~= "" then
return vim.api.nvim_replace_termcodes("<C-x><C-o>", true, true, true)
end

return vim.api.nvim_replace_termcodes("<C-n>", true, true, true)
end, {
silent = true,
expr = true,
replace_keycodes = false,
desc = "Navigate to next completion item, unless there is only whitespace before cursor",
})

vim.keymap.set("i", "<S-Tab>", function()
if vim.fn.pumvisible() == 1 then
return vim.api.nvim_replace_termcodes("<C-p>", true, true, true)
end

return vim.api.nvim_replace_termcodes("<S-Tab>", true, true, true)
end, {
silent = true,
expr = true,
replace_keycodes = false,
desc = "Navigate to previous completion item",
})

-- }}}
-- vim: foldmethod=marker foldlevel=999
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/cmp-buffer
Submodule cmp-buffer added at 3022db
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/cmp-nvim-lsp
Submodule cmp-nvim-lsp added at 0e6b2e
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/cmp-path
Submodule cmp-path added at 91ff86
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/cmp-vsnip
Submodule cmp-vsnip added at 989a8a
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/nvim-cmp
Submodule nvim-cmp added at fc0f69
1 change: 1 addition & 0 deletions .config/nvim/pack/plugins/opt/vim-vsnip
Submodule vim-vsnip added at 7753ba
18 changes: 18 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,21 @@
[submodule "null-ls"]
path = .config/nvim/pack/plugins/opt/null-ls.nvim
url = https://github.com/jose-elias-alvarez/null-ls.nvim
[submodule "nvim-cmp"]
path = .config/nvim/pack/plugins/opt/nvim-cmp
url = https://github.com/hrsh7th/nvim-cmp
[submodule "cmp-buffer"]
path = .config/nvim/pack/plugins/opt/cmp-buffer
url = https://github.com/hrsh7th/cmp-buffer
[submodule "cmp-path"]
path = .config/nvim/pack/plugins/opt/cmp-path
url = https://github.com/hrsh7th/cmp-path
[submodule "cmp-nvim-lsp"]
path = .config/nvim/pack/plugins/opt/cmp-nvim-lsp
url = https://github.com/hrsh7th/cmp-nvim-lsp
[submodule "cmp-vsnip"]
path = .config/nvim/pack/plugins/opt/cmp-vsnip
url = https://github.com/hrsh7th/cmp-vsnip
[submodule "vim-vsnip"]
path = .config/nvim/pack/plugins/opt/vim-vsnip
url = https://github.com/hrsh7th/vim-vsnip

0 comments on commit a8ec9b9

Please sign in to comment.