Skip to content

Commit

Permalink
Merge pull request #39 from sbulav/feat-nvim-07
Browse files Browse the repository at this point in the history
Feat nvim 07
  • Loading branch information
sbulav authored Apr 18, 2022
2 parents 1bd708f + 200989e commit f646702
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 233 deletions.
24 changes: 12 additions & 12 deletions nvim/after/plugin/functions/Grep.vim
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
" https://gist.github.com/romainl/56f0c28ef953ffc157f36cc495947ab3
" Execute grep in a subshell to avoid annoying confirmation screens
function! Grep(args)
let args = split(a:args, ' ')
return system(join([&grepprg, shellescape(args[0]), len(args) > 1 ? join(args[1:-1], ' ') : ''], ' '))
" Execset grepprg=ag\ --vimgrep
function! Grep(...)
return system(join([&grepprg] + [expandcmd(join(a:000, ' '))], ' '))
endfunction

" Command to populate quickfix with Grep function results
command! -nargs=+ -complete=file_in_path -bar Grep cgetexpr Grep(<q-args>)
command! -nargs=+ -complete=file_in_path -bar LGrep lgetexpr Grep(<q-args>)
command! -nargs=+ -complete=file_in_path -bar Grep cgetexpr Grep(<f-args>)
command! -nargs=+ -complete=file_in_path -bar LGrep lgetexpr Grep(<f-args>)

cnoreabbrev <expr> grep (getcmdtype() ==# ':' && getcmdline() ==# 'grep') ? 'Grep' : 'grep'
cnoreabbrev <expr> lgrep (getcmdtype() ==# ':' && getcmdline() ==# 'lgrep') ? 'LGrep' : 'lgrep'

" Open quickfix automatically if there are valid entries
augroup quickfix
autocmd!
autocmd QuickFixCmdPost cgetexpr cwindow
autocmd QuickFixCmdPost lgetexpr lwindow
augroup END
autocmd!
autocmd QuickFixCmdPost cgetexpr cwindow
autocmd QuickFixCmdPost lgetexpr lwindow
augroup ENDute grep in a subshell to avoid annoying confirmation screens
4 changes: 0 additions & 4 deletions nvim/after/plugin/wildmenu.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ set wildignore+=*/target/* " sbt target dires `,`. You can use space
" in your local
" set completeopt=menu,longest
set completeopt=menu,menuone,noselect
" automatically open and close the popup menu / preview window
" augroup vimmic_popup_menu
" autocmd CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
" augroup END
68 changes: 46 additions & 22 deletions nvim/lua/autocommands.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
local autocmds = {
no_cursorline_in_insert_mode = {
{ "InsertLeave,WinEnter", "*", "set cursorline" },
{ "InsertEnter,WinLeave", "*", "set nocursorline" },
},
hl_yank = {
{ "TextYankPost", "*", 'lua vim.highlight.on_yank{higroup="IncSearch", timeout=1500, on_visual=true}' },
},
restore_curpos = {
{
"BufReadPost",
"*",
[[
if line("'\"") >= 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
]],
},
},
add_filetypes = {
{ "BufWritePre", "*enkinsfile*", "lua require'jenkinsfile_linter'.validate()" },
},
}
local utils = require "utils"

require("utils").nvim_create_augroups(autocmds)
vim.api.nvim_create_augroup("Highlight", { clear = true })
vim.api.nvim_create_autocmd("TextYankPost", {
command = "silent! lua vim.highlight.on_yank({higroup='IncSearch', timeout=1500, on_visual = true})",
group = "Highlight",
desc = "Highlight yanked text",
})

vim.api.nvim_create_augroup("CursorInsertMode", { clear = true })
vim.api.nvim_create_autocmd({ "InsertLeave", "WinEnter" }, {
callback = function()
vim.opt.cursorline = true
end,
group = "CursorInsertMode",
desc = "Enable cursorline in normal mode",
})
vim.api.nvim_create_autocmd({ "InsertEnter", "WinLeave" }, {
callback = function()
vim.opt.cursorline = false
end,
group = "CursorInsertMode",
desc = "Disable cursorline on insert",
})

vim.api.nvim_create_augroup("RestoreCursorPosition", { clear = true })
vim.api.nvim_create_autocmd("BufReadPost", {
callback = function()
-- TODO?: filetype/buftype exclude
local row, col = unpack(vim.api.nvim_buf_get_mark(0, '"'))
if row > 0 and row <= vim.api.nvim_buf_line_count(0) then
vim.api.nvim_win_set_cursor(0, { row, col })
end
end,
desc = "Return to last known cursor position",
group = "RestoreCursorPosition",
})
vim.api.nvim_create_augroup("ValidateJenkinsfiles", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function()
utils.info("Validating Jenkinsfile", "LSP")
require("jenkinsfile_linter").validate()
end,
group = "ValidateJenkinsfiles",
desc = "Validate Jenkins files on save",
pattern = "*enkinsfile*",
})
11 changes: 9 additions & 2 deletions nvim/lua/config/toggleterm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ function _G.set_terminal_keymaps()
vim.api.nvim_buf_set_keymap(0, "t", "<C-l>", [[<C-\><C-n><C-W>l]], opts)
end

vim.cmd "autocmd! TermOpen term://* lua set_terminal_keymaps()"

vim.api.nvim_create_augroup("Terminal", { clear = true })
vim.api.nvim_create_autocmd("TermOpen", {
callback = function()
set_terminal_keymaps()
end,
group = "Terminal",
desc = "Attach mappings to Terminal",
pattern = "term://*",
})
local Terminal = require("toggleterm.terminal").Terminal

local k9s = Terminal:new { cmd = "k9s", hidden = true, direction = "float" }
Expand Down
163 changes: 91 additions & 72 deletions nvim/lua/lsp/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,109 @@ local utils = require "utils"
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
function M.on_attach(client, bufnr)
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")

local opts = { noremap = true, silent = true }
local keymap = vim.api.nvim_set_keymap
local attach_opts = { silent = true, buffer = bufnr }

-- lsp provider to find the cursor word definition and reference
keymap("n", "gh", '<cmd>lua require"lspsaga.provider".lsp_finder()<CR>', opts)
-- code action
keymap("n", "ga", '<cmd>lua require"lspsaga.codeaction".code_action()<CR>', opts)
keymap("n", "gA", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
-- lsp provider to find the cursor word definition and reference
vim.keymap.set("n", "gh", function()
require("lspsaga.provider").lsp_finder()
end, attach_opts)
-- show function signature help
vim.keymap.set("n", "gs", vim.lsp.buf.signature_help, attach_opts)
-- rename
vim.keymap.set("n", "gr", function()
require("lspsaga.rename").rename()
end, attach_opts)
-- preview definition
vim.keymap.set("n", "gD", function()
require("lspsaga.provider").preview_definition()
end, attach_opts)
-- go to implementation
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, attach_opts)
-- go to definition
vim.keymap.set("n", "gd", vim.lsp.buf.definition, attach_opts)
-- show line diagnostic
vim.keymap.set("n", "ge", function()
require("lspsaga.diagnostic").show_line_diagnostics()
end, attach_opts)
-- jump diagnostic
vim.keymap.set("n", "]e", function()
require("lspsaga.diagnostic").navigate "next"
end, attach_opts)
vim.keymap.set("n", "[e", function()
require("lspsaga.diagnostic").navigate "prev"
end, attach_opts)
vim.keymap.set("n", "<leader>so", function()
require("telescope.builtin").lsp_document_symbols()
end, attach_opts)
vim.keymap.set("n", "<leader>sr", function()
require("telescope.builtin").lsp_buf_references()
end, attach_opts)

-- show function signature help
keymap("n", "gs", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
-- rename
keymap("n", "gr", '<cmd>lua require"lspsaga.rename".rename()<CR>', opts)
-- preview definition
keymap("n", "gD", '<cmd>lua require"lspsaga.provider".preview_definition()<CR>', opts)
-- go to implementation
keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
-- go to definition
keymap("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
-- show line diagnostic
keymap("n", "ge", '<cmd>lua require"lspsaga.diagnostic".show_line_diagnostics()<CR>', opts)
-- jump diagnostic
keymap("n", "[e", "<cmd>Lspsaga diagnostic_jump_next<cr>", opts)
keymap("n", "]e", "<cmd>Lspsaga diagnostic_jump_prev<cr>", opts)
-- manage git worktree
keymap("n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
keymap("n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
keymap("n", "<leader>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
keymap("n", "<leader>so", [[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]], opts)
keymap("n", "<leader>sr", [[<cmd>lua require('telescope.builtin').lsp_buf_references()<CR>]], opts)
vim.api.nvim_create_user_command("Format", vim.lsp.buf.formatting, {})

vim.cmd 'command! Format :lua require("lsp.utils").formatDocument()'
-- client.resolved_capabilities.document_formatting = enable
-- -- format on save
-- if client.resolved_capabilities.document_formatting then
vim.cmd [[
augroup LspFormat
autocmd BufWritePre <buffer> lua require("lsp.utils").formatDocument()
augroup END
autocmd CursorHold,CursorHoldI * lua require'nvim-lightbulb'.update_lightbulb()
]]
-- end
-- Disable formatting with other LSPs because we're handling formatting via null-ls
-- Otherwise you'll be prompted to Select a language server
if client.name ~= "null-ls" then
client.resolved_capabilities.document_formatting = false
end
if client.resolved_capabilities.document_highlight then
vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
vim.api.nvim_clear_autocmds { buffer = bufnr, group = "lsp_document_highlight" }
vim.api.nvim_create_autocmd("CursorHold", {
callback = vim.lsp.buf.document_highlight,
buffer = bufnr,
group = "lsp_document_highlight",
desc = "Document Highlight",
})
vim.api.nvim_create_autocmd("CursorMoved", {
callback = vim.lsp.buf.clear_references,
buffer = bufnr,
group = "lsp_document_highlight",
desc = "Clear All the References",
})
end

-- Disable formatting with other LSPs because we're handling formatting via null-ls
-- Otherwise you'll be prompted to Select a language server
if client.name ~= "null-ls" then
client.resolved_capabilities.document_formatting = false
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec(
[[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]],
false
)
end
end

function M.formatDocument()
-- check if LSP is attached
if (#vim.lsp.buf_get_clients()) < 1 then
return
end
if client.resolved_capabilities.code_action then
vim.keymap.set("n", "<leader>ga", function()
require("lspsaga.codeaction").code_action()
end, { buffer = bufnr, desc = "Code Actions" })
vim.keymap.set("v", "<leader>ga", function()
require("lspsaga.codeaction").range_code_action()
end, { buffer = bufnr, desc = "Range Code Actions" })
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
callback = function()
require("nvim-lightbulb").update_lightbulb()
end,
buffer = bufnr,
desc = "Update the LightBulb",
})
end

vim.lsp.buf.formatting_sync(nil, 1500)
if client.resolved_capabilities.document_formatting then
vim.api.nvim_create_augroup("LspFormat", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function()
utils.info("Formatting file via lsp", "LSP")
vim.lsp.buf.formatting()
end,
group = "LspFormat",
desc = "Format document on save with LSP",
})
end
end

function M.custom_on_init()
-- print "Language Server Protocol started!"
utils.info("Language Server Protocol started!", "LSP")
-- print "Language Server Protocol started!"
utils.info("Language Server Protocol started!", "LSP")
end

function M.has_formatter(ft)
local sources = require "null-ls.sources"
local available = sources.get_available(ft, "NULL_LS_FORMATTING")
return #available > 0
local sources = require "null-ls.sources"
local available = sources.get_available(ft, "NULL_LS_FORMATTING")
return #available > 0
end

return M
65 changes: 37 additions & 28 deletions nvim/lua/mappings.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
local map = require("utils").map
local keymap = vim.api.nvim_set_keymap
local cmd = vim.cmd
local fn = vim.fn

local attach_opts = { silent = true }
-- lsp provider to find the cursor word definition and reference
-- URL handling
if vim.fn.has "mac" == 1 then
map[""].gx = { '<Cmd>call jobstart(["open", expand("<cfile>")], {"detach": v:true})<CR>' }
vim.keymap.set("n", "gx", '<Cmd>call jobstart(["open", expand("<cfile>")], {"detach": v:true})<CR>', attach_opts)
elseif vim.fn.has "unix" == 1 then
map[""].gx = { '<Cmd>call jobstart(["xdg-open", expand("<cfile>")], {"detach": v:true})<CR>' }
vim.keymap.set(
"n",
"gx",
'<Cmd>call jobstart(["xdg-open", expand("<cfile>")], {"detach": v:true})<CR>',
attach_opts
)
else
map[""].gx = { '<Cmd>lua print("Error: gx is not supported on this OS!")<CR>' }
vim.keymap.set("n", "gx", function()
print "Error: gx is not supported on this OS!"
end, attach_opts)
end

--- Commands to troubleshoot LSP{{{
Expand All @@ -20,29 +24,34 @@ end
--- vim.lsp.set_log_level("debug")}}}

function Show_documentation()
if fn.index({ "vim", "help" }, vim.bo.filetype) >= 0 then
cmd("h " .. vim.fn.expand "<cword>")
if vim.fn.index({ "vim", "help" }, vim.bo.filetype) >= 0 then
vim.cmd("h " .. vim.fn.expand "<cword>")
else
cmd 'lua require"lspsaga.hover".render_hover_doc()'
require("lspsaga.hover").render_hover_doc()
end
end

local opts = { noremap = true, silent = true }
keymap("n", "K", "<CMD>lua Show_documentation()<CR>", opts)
keymap("n", "<space>t9", "<CMD>lua _K9S_TOGGLE()<CR>", opts)
keymap("t", "<space>t9", "<CMD>lua _K9S_TOGGLE()<CR>", opts)
keymap("n", "<M-\\>", "<cmd>ToggleTerm direction=float<CR>", opts)
keymap("t", "<M-\\>", "<cmd>ToggleTerm<CR>", opts)
vim.keymap.set("n", "K", function()
Show_documentation()
end, attach_opts)
vim.keymap.set({ "n", "t" }, "<leader>t9", function()
_K9S_TOGGLE()
end, attach_opts)
vim.keymap.set({ "n", "t" }, "<M-\\>", "<cmd>ToggleTerm direction=float<CR>", attach_opts)

--- luasnip keymappings
keymap("i", "<c-x>", "<cmd>lua require'luasnip'.expand_or_jump()<CR>", opts)
keymap("s", "<c-x>", "<cmd>lua require'luasnip'.expand_or_jump()<CR>", opts)
keymap("i", "<c-n>", "<cmd>lua require'luasnip'.change_choice(1)<CR>", opts)
keymap("s", "<c-n>", "<cmd>lua require'luasnip'.change_choice(1)<CR>", opts)
keymap("i", "<c-p>", "<cmd>lua require'luasnip'.change_choice(-1)<CR>", opts)
keymap("s", "<c-p>", "<cmd>lua require'luasnip'.change_choice(-1)<CR>", opts)

keymap("i", "<c-j>", "<cmd>lua require'luasnip'.jump(1)<CR>", opts)
keymap("s", "<c-j>", "<cmd>lua require'luasnip'.jump(1)<CR>", opts)
keymap("i", "<c-k>", "<cmd>lua require'luasnip'.jump(-1)<CR>", opts)
keymap("s", "<c-k>", "<cmd>lua require'luasnip'.jump(-1)<CR>", opts)
vim.keymap.set({ "i", "s" }, "<c-x>", function()
require("luasnip").expand_or_jump()
end, attach_opts)
vim.keymap.set({ "i", "s" }, "<c-n>", function()
require("luasnip").change_choice(1)
end, attach_opts)
vim.keymap.set({ "i", "s" }, "<c-p>", function()
require("luasnip").change_choice(-1)
end, attach_opts)
vim.keymap.set({ "i", "s" }, "<c-j>", function()
require("luasnip").jump(1)
end, attach_opts)
vim.keymap.set({ "i", "s" }, "<c-k>", function()
require("luasnip").jump(-1)
end, attach_opts)
Loading

0 comments on commit f646702

Please sign in to comment.