Skip to content

Commit

Permalink
nvim: autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
David Zukowski committed Aug 21, 2021
1 parent ffebe7d commit c6c725a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 54 deletions.
65 changes: 11 additions & 54 deletions config/nvim-lua/lua/lsp.lua
@@ -1,41 +1,10 @@
-- https://github.com/neovim/nvim-lspconfig
-- reference: https://raygervais.dev/articles/2021/3/neovim-lsp
local nvim_lsp=require('lspconfig')

local filetypes={
typescript="eslint",
typescriptreact="eslint",
}

local linters={
eslint={
sourceName="eslint",
command="eslint_d",
rootPatterns={"package.json"},
debounce=100,
args={"--stdin", "--stdin-filename", "%filepath", "--format", "json"},
parseJson={
errorsRoot="[0].messages",
line="line",
column="column",
endLine="endLine",
endColumn="endColumn",
message="${message} [${ruleId}]",
security="severity"
},
securities={[2]="error", [1]="warning"}
}
}

local formatters={
prettier={
command="prettier",
args={"--stdin-filepath", "%filepath"}
}
}

local servers={'tsserver'}

-- https://github.com/neovim/nvim-lspconfig
local on_attach=function(client, bufnr)
require('completion').on_attach()

local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end

Expand All @@ -49,6 +18,9 @@ local on_attach=function(client, bufnr)
-- vim.api.nvim_command [[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]]
-- vim.api.nvim_command [[augroup END]]
-- end
if client.resolved_capabilities.document_formatting then
buf_set_keymap('n', '<leader>af', '<cmd>lua vim.lsp.buf.formatting()<cr>', {noremap=true, silent=true})
end

-- see `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', {noremap=true, silent=true})
Expand All @@ -70,18 +42,6 @@ local on_attach=function(client, bufnr)
-- buf_set_keymap('n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', {noremap=true, silent=true})
end

-- setup diagnostics
-- nvim_lsp.diagnosticls.setup({
-- on_attach=on_attach,
-- filetypes=vim.tbl_keys(filetypes),
-- init_options={
-- filetypes=filetypes,
-- linters=linters,
-- formatters=formatters,
-- formatFiletypes=formatFiletypes
-- }
-- })

-- setup typescript language server
nvim_lsp.tsserver.setup({
on_attach=function(client)
Expand All @@ -91,13 +51,10 @@ nvim_lsp.tsserver.setup({
end
})

-- setup language servers and map buffer-local keybindings when the
-- language server attaches
-- configure servers only when a language server attaches
local servers={'tsserver', 'gopls'}
for _, lsp in ipairs(servers) do
nvim_lsp[lsp].setup {
nvim_lsp[lsp].setup({
on_attach=on_attach,
flags={
debounce_text_changes=150,
}
}
})
end
17 changes: 17 additions & 0 deletions config/nvim-lua/lua/mappings.lua
Expand Up @@ -51,6 +51,23 @@ map('v', '<Leader>ca', '<cmd>Telescope lsp_range_code_actions<cr>', {noremap=tru
-- search command history (same keybinding as in shell)
map('n', '<C-R>', '<cmd>lua require("telescope.builtin").command_history()<cr>', {noremap=true})

-- confirm autocomplete with <tab>
local function feedkeys(s)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(s, true, true, true), 'n', true)
end
function expand_tab()
if vim.fn.pumvisible() == 1 then
if vim.fn.complete_info({"selected"})["selected"] == -1 then
vim.api.nvim_input("<C-n><Plug>(completion_confirm_completion)")
else
vim.api.nvim_input("<Plug>(completion_confirm_completion)")
end
else
feedkeys("<Tab>")
end
end
map('i', '<tab>', '<cmd>lua expand_tab()<cr>', {})

-- easily switch between windows with ctrl + direction
map('n', '<C-h>', '<C-W>h', {noremap=true})
map('n', '<C-j>', '<C-W>j', {noremap=true})
Expand Down
1 change: 1 addition & 0 deletions config/nvim-lua/lua/plugins.lua
Expand Up @@ -12,6 +12,7 @@ return require('packer').startup(function()
-- developer experience
use {'editorconfig/editorconfig-vim'}
use {'neovim/nvim-lspconfig'}
use {'nvim-lua/completion-nvim'}
use {'nvim-telescope/telescope.nvim', requires={'nvim-lua/plenary.nvim'}}
use {'dense-analysis/ale', ft={'javascript', 'typescript', 'typescriptreact'}}
use {'tpope/vim-commentary'}
Expand Down
7 changes: 7 additions & 0 deletions config/nvim-lua/lua/settings.lua
Expand Up @@ -40,6 +40,13 @@ vim.opt.backspace={"indent", "eol", "start"} -- allow backspacing over the
vim.opt.diffopt:append({"vertical"})
vim.opt.wildignore:append({"*.jpg", "*.jpeg", "*.png", "*.svg"})

-- better completion experience
vim.opt.completeopt={"menuone","noinsert","noselect"}

-- avoid showing message extra message when using completion
vim.cmd 'set shortmess+=c'
-- vim.opt.shortmess:append({"c"})

-- fixes crontab (reference?)
vim.opt.backupcopy='yes'

Expand Down

0 comments on commit c6c725a

Please sign in to comment.