|
local utils = {} |
|
|
|
local scopes = {o = vim.o, b = vim.bo, w = vim.wo} |
|
|
|
function utils.opt(scope, key, value) |
|
scopes[scope][key] = value |
|
if scope ~= "o" then |
|
scopes["o"][key] = value |
|
end |
|
end |
|
|
|
function utils.map(mode, lhs, rhs, opts) |
|
local options = {noremap = true} |
|
if opts then |
|
options = vim.tbl_extend("force", options, opts) |
|
end |
|
vim.api.nvim_set_keymap(mode, lhs, rhs, options) |
|
end |
|
|
|
-- Install packer |
|
local install_path = vim.fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim" |
|
|
|
if vim.fn.empty(vim.fn.glob(install_path)) > 0 then |
|
vim.fn.execute("!git clone https://github.com/wbthomason/packer.nvim " .. install_path) |
|
end |
|
|
|
vim.api.nvim_exec( |
|
[[ |
|
augroup Packer |
|
autocmd! |
|
autocmd BufWritePost init.lua PackerCompile |
|
augroup end |
|
]], |
|
false |
|
) |
|
|
|
local use = require("packer").use |
|
require("packer").startup( |
|
function() |
|
-- For Packer |
|
use "wbthomason/packer.nvim" |
|
|
|
-- Explorer |
|
|
|
use { |
|
"kyazdani42/nvim-tree.lua", |
|
requires = "kyazdani42/nvim-web-devicons", |
|
config = function() |
|
require "nvim-tree".setup {} |
|
end |
|
} |
|
|
|
use "mhartington/formatter.nvim" |
|
use "neovim/nvim-lspconfig" |
|
use "glepnir/lspsaga.nvim" |
|
use "onsails/lspkind-nvim" |
|
|
|
use "hrsh7th/cmp-nvim-lsp" |
|
use "hrsh7th/cmp-buffer" |
|
use "EdenEast/nightfox.nvim" |
|
use "hrsh7th/nvim-cmp" |
|
use "hrsh7th/cmp-path" |
|
use "saadparwaiz1/cmp_luasnip" |
|
use "L3MON4D3/LuaSnip" -- Snippets plugin |
|
use "windwp/nvim-autopairs" |
|
use "maxmellon/vim-jsx-pretty" |
|
-- Highlight, edit, and navigate code using a fast incremental parsing library |
|
use "nvim-treesitter/nvim-treesitter" |
|
use "nvim-lualine/lualine.nvim" |
|
-- Additional textobjects for treesitter |
|
use "nvim-treesitter/nvim-treesitter-textobjects" |
|
use "tpope/vim-commentary" -- "gc" to comment visual regions/lines |
|
-- Add indentation guides even on blank lines |
|
use "lukas-reineke/indent-blankline.nvim" |
|
use { |
|
"nvim-telescope/telescope.nvim", |
|
requires = {{"nvim-lua/plenary.nvim"}} |
|
} |
|
use "mfussenegger/nvim-jdtls" |
|
end |
|
) |
|
|
|
require("nvim-autopairs").setup() |
|
require("lspkind").init() |
|
|
|
require("nightfox").load("nordfox") |
|
|
|
require("lualine").setup( |
|
{ |
|
theme = "nightfox" |
|
} |
|
) |
|
|
|
--Incremental live completion (note: this is now a default on master) |
|
vim.o.inccommand = "nosplit" |
|
|
|
--Set highlight on search |
|
vim.o.hlsearch = false |
|
|
|
--Make line numbers default |
|
vim.wo.number = true |
|
|
|
--Do not save when switching buffers (note: this is now a default on master) |
|
vim.o.hidden = true |
|
|
|
--Enable mouse mode |
|
vim.o.mouse = "a" |
|
|
|
--Enable break indent |
|
vim.o.breakindent = true |
|
|
|
--Save undo history |
|
vim.opt.undofile = true |
|
|
|
--Case insensitive searching UNLESS /C or capital in search |
|
vim.o.ignorecase = true |
|
vim.o.smartcase = true |
|
|
|
--Decrease update time |
|
vim.o.updatetime = 250 |
|
vim.wo.signcolumn = "yes" |
|
|
|
--Set colorscheme (order is important here) |
|
vim.o.termguicolors = true |
|
-- vim.g.onedark_terminal_italics = 2 |
|
|
|
--Set statusbar |
|
-- vim.g.lightline = { |
|
-- colorscheme = 'onedark', |
|
-- active = { left = { { 'mode', 'paste' }, { 'gitbranch', 'readonly', 'filename', 'modified' } } }, |
|
-- component_function = { gitbranch = 'fugitive#head' }, |
|
-- } |
|
|
|
--Remap space as leader key |
|
vim.api.nvim_set_keymap("", "<Space>", "<Nop>", {noremap = true, silent = true}) |
|
vim.g.mapleader = " " |
|
vim.g.maplocalleader = " " |
|
|
|
--Remap for dealing with word wrap |
|
local opts = {noremap = true, expr = true, silent = true} |
|
vim.api.nvim_set_keymap("n", "k", "v:count == 0 ? 'gk' : 'k'", opts) |
|
vim.api.nvim_set_keymap("n", "j", "v:count == 0 ? 'gj' : 'j'", opts) |
|
|
|
-- vim.api.nvim_set_keymap('i', 'jk', "<ESC>", opts) |
|
-- vim.api.nvim_set_keymap('i', 'kj', "<ESC>", opts) |
|
|
|
-- Highlight on yank |
|
vim.api.nvim_exec( |
|
[[ |
|
augroup YankHighlight |
|
autocmd! |
|
autocmd TextYankPost * silent! lua vim.highlight.on_yank() |
|
augroup end |
|
]], |
|
false |
|
) |
|
|
|
-- Y yank until the end of line (note: this is now a default on master) |
|
vim.api.nvim_set_keymap("n", "Y", "y$", {noremap = true}) |
|
|
|
--Map blankline |
|
vim.g.indent_blankline_char = "┊" |
|
vim.g.indent_blankline_filetype_exclude = {"help", "packer"} |
|
vim.g.indent_blankline_buftype_exclude = {"terminal", "nofile"} |
|
vim.g.indent_blankline_char_highlight = "LineNr" |
|
vim.g.indent_blankline_show_trailing_blankline_indent = false |
|
|
|
-- Telescope |
|
require("telescope").setup { |
|
defaults = { |
|
mappings = { |
|
i = { |
|
["<C-u>"] = false, |
|
["<C-d>"] = false |
|
} |
|
} |
|
} |
|
} |
|
|
|
--Add leader shortcuts |
|
utils.map('n', '<leader>ff', [[<cmd>lua require('telescope.builtin').find_files()<cr>]], opts ) |
|
utils.map('n', '<C-p>', [[<cmd>lua require('telescope.builtin').find_files()<cr>]], opts) |
|
utils.map('n', '<leader>fg', [[<cmd>lua require('telescope.builtin').live_grep()<cr>]], opts) |
|
utils.map('n', '<leader>fb', [[<cmd>lua require('telescope.builtin').buffers()<cr>]], opts) |
|
utils.map('n', '<leader>fh', [[<cmd>lua require('telescope.builtin').help_tags()<cr>]], opts) |
|
|
|
-- Treesitter configuration |
|
-- Parsers must be installed manually via :TSInstall |
|
require("nvim-treesitter.configs").setup { |
|
highlight = { |
|
enable = true -- false will disable the whole extension |
|
}, |
|
incremental_selection = { |
|
enable = true, |
|
keymaps = { |
|
init_selection = "gnn", |
|
node_incremental = "grn", |
|
scope_incremental = "grc", |
|
node_decremental = "grm" |
|
} |
|
}, |
|
indent = { |
|
enable = true |
|
}, |
|
textobjects = { |
|
select = { |
|
enable = true, |
|
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim |
|
keymaps = { |
|
-- You can use the capture groups defined in textobjects.scm |
|
["af"] = "@function.outer", |
|
["if"] = "@function.inner", |
|
["ac"] = "@class.outer", |
|
["ic"] = "@class.inner" |
|
} |
|
}, |
|
move = { |
|
enable = true, |
|
set_jumps = true, -- whether to set jumps in the jumplist |
|
goto_next_start = { |
|
["]m"] = "@function.outer", |
|
["]]"] = "@class.outer" |
|
}, |
|
goto_next_end = { |
|
["]M"] = "@function.outer", |
|
["]["] = "@class.outer" |
|
}, |
|
goto_previous_start = { |
|
["[m"] = "@function.outer", |
|
["[["] = "@class.outer" |
|
}, |
|
goto_previous_end = { |
|
["[M"] = "@function.outer", |
|
["[]"] = "@class.outer" |
|
} |
|
} |
|
} |
|
} |
|
|
|
-- LSP settings |
|
local nvim_lsp = require "lspconfig" |
|
local on_attach = function(_, bufnr) |
|
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") |
|
|
|
local opts = {noremap = true, silent = true} |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap( |
|
bufnr, |
|
"n", |
|
"<leader>wl", |
|
"<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", |
|
opts |
|
) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts) |
|
-- vim.api.nvim_buf_set_keymap(bufnr, 'v', '<leader>ca', '<cmd>lua vim.lsp.buf.range_code_action()<CR>', opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts) |
|
vim.api.nvim_buf_set_keymap( |
|
bufnr, |
|
"n", |
|
"<leader>so", |
|
[[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]], |
|
opts |
|
) |
|
vim.cmd [[ command! Format execute 'lua vim.lsp.buf.formatting()' ]] |
|
end |
|
|
|
-- nvim-cmp supports additional completion capabilities |
|
local capabilities = vim.lsp.protocol.make_client_capabilities() |
|
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) |
|
|
|
-- Enable the following language servers |
|
local servers = { |
|
"bashls", |
|
"cssls", |
|
"dockerls", |
|
"jsonls", |
|
"sqls", |
|
"tsserver", |
|
"vimls", |
|
"yamlls" |
|
} |
|
for _, lsp in ipairs(servers) do |
|
nvim_lsp[lsp].setup { |
|
on_attach = on_attach, |
|
capabilities = capabilities |
|
} |
|
end |
|
|
|
-- Set completeopt to have a better completion experience |
|
vim.o.completeopt = "menuone,noselect" |
|
|
|
-- luasnip setup |
|
local luasnip = require "luasnip" |
|
|
|
-- nvim-cmp setup |
|
local lspkind = require("lspkind") |
|
-- formatting = { |
|
-- format = lspkind.cmp_format({with_text = false, maxwidth = 50}) |
|
-- } |
|
local cmp = require "cmp" |
|
cmp.setup { |
|
formatting = { |
|
format = lspkind.cmp_format({with_text = false, maxwidth = 50}) |
|
}, |
|
snippet = { |
|
expand = function(args) |
|
luasnip.lsp_expand(args.body) |
|
end |
|
}, |
|
mapping = { |
|
["<C-p>"] = cmp.mapping.select_prev_item(), |
|
["<C-n>"] = cmp.mapping.select_next_item(), |
|
["<C-d>"] = cmp.mapping.scroll_docs(-4), |
|
["<C-f>"] = cmp.mapping.scroll_docs(4), |
|
["<C-Space>"] = cmp.mapping.complete(), |
|
["<C-e>"] = cmp.mapping.close(), |
|
["<CR>"] = cmp.mapping.confirm { |
|
behavior = cmp.ConfirmBehavior.Replace, |
|
select = true |
|
}, |
|
["<Tab>"] = function(fallback) |
|
if cmp.visible() then |
|
cmp.select_next_item() |
|
elseif luasnip.expand_or_jumpable() then |
|
luasnip.expand_or_jump() |
|
else |
|
fallback() |
|
end |
|
end, |
|
["<S-Tab>"] = function(fallback) |
|
if cmp.visible() then |
|
cmp.select_prev_item() |
|
elseif luasnip.jumpable(-1) then |
|
luasnip.jump(-1) |
|
else |
|
fallback() |
|
end |
|
end |
|
}, |
|
sources = { |
|
{name = "nvim_lsp"}, |
|
{name = "luasnip"}, |
|
{name = "buffer"}, |
|
{name = "path"} |
|
} |
|
} |
|
|
|
require("nvim-autopairs.completion.cmp").setup { |
|
map_cr = true, |
|
map_complete = true, |
|
auto_select = true |
|
} |
|
|
|
-- Setup lspconfig. |
|
require("lspconfig").tsserver.setup { |
|
capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities()) |
|
} |
|
|
|
-- explorer |
|
utils.map('n', '<Leader>e', ':NvimTreeToggle<CR>', {noremap = true, silent = true}) |
|
utils.map('n', '-', ':RnvimrToggle<CR>', {noremap = true, silent = true}) |
|
|
|
-- better window movement |
|
utils.map('n', '<C-h>', '<C-w>h', {silent = true}) |
|
utils.map('n', '<C-j>', '<C-w>j', {silent = true}) |
|
utils.map('n', '<C-k>', '<C-w>k', {silent = true}) |
|
utils.map('n', '<C-l>', '<C-w>l', {silent = true}) |
|
|
|
|
|
utils.map("i", "jk", "<ESC>") |
|
utils.map("i", "kj", "<ESC>") |
|
utils.map("i", "jj", "<ESC>") |
|
|
|
utils.map("n", "<leader>p", '"_dp') |
|
|
|
-- Y yank until the end of line |
|
utils.map("n", "Y", "y$") |
|
-- map('n', '<leader>/', '<cmd>noh<CR>') -- Clear highlights |
|
utils.map("n", "<A-/>", "<cmd>noh<CR>") -- Clear highlights |
|
utils.map("i", "jk", "<Esc>") |
|
utils.map("", "<leader>c", '"+y') --Copy to clipboard in normal, visual, select and operator modes |
|
utils.map("i", "<C-u>", "<C-g>u<C-u>") -- Make <C-u> undo-friendly |
|
utils.map("i", "<C-w>", "<C-g>u<C-w>") -- Make <C-w> undo-friendly |
|
|
|
-- Formatters |
|
vim.cmd([[nnoremap <silent> <leader>f :Format<CR>]]) |
|
local home = os.getenv("HOME") |
|
require("formatter").setup( |
|
{ |
|
filetype = { |
|
typescript = { |
|
function() |
|
return { |
|
exe = "prettier", |
|
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"}, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
typescriptreact = { |
|
function() |
|
return { |
|
exe = "prettier", |
|
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"}, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
java = { |
|
function() |
|
return { |
|
exe = "java", |
|
-- Formatter uses '-' as stdin |
|
args = { |
|
"--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", |
|
"-jar", |
|
home .. "/.config/formatters/google-java-format-1.11.0-all-deps.jar", |
|
"--aosp", -- 4 spaces |
|
vim.api.nvim_buf_get_name(0) |
|
}, |
|
-- "-" }, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
javascript = { |
|
function() |
|
return { |
|
exe = "prettier", |
|
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"}, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
javascriptreact = { |
|
function() |
|
return { |
|
exe = "prettier", |
|
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"}, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
json = { |
|
function() |
|
return { |
|
exe = "prettier", |
|
args = {"--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)), "--single-quote"}, |
|
stdin = true |
|
} |
|
end |
|
}, |
|
lua = { |
|
-- luafmt |
|
function() |
|
return { |
|
exe = "luafmt", |
|
args = {"--indent-count", 2, "--stdin"}, |
|
stdin = true |
|
} |
|
end |
|
} |
|
} |
|
} |
|
) |
|
|
|
-- following options are the default |
|
require'nvim-tree'.setup { |
|
-- disables netrw completely |
|
disable_netrw = true, |
|
-- hijack netrw window on startup |
|
hijack_netrw = true, |
|
-- open the tree when running this setup function |
|
open_on_setup = false, |
|
-- will not open on setup if the filetype is in this list |
|
ignore_ft_on_setup = {}, |
|
-- closes neovim automatically when the tree is the last **WINDOW** in the view |
|
auto_close = true, |
|
-- opens the tree when changing/opening a new tab if the tree wasn't previously opened |
|
open_on_tab = false, |
|
-- hijacks new directory buffers when they are opened. |
|
update_to_buf_dir = { |
|
-- enable the feature |
|
enable = true, |
|
-- allow to open the tree if it was previously closed |
|
auto_open = true, |
|
}, |
|
-- hijack the cursor in the tree to put it at the start of the filename |
|
hijack_cursor = false, |
|
-- updates the root directory of the tree on `DirChanged` (when you run `:cd` usually) |
|
update_cwd = false, |
|
-- show lsp diagnostics in the signcolumn |
|
diagnostics = { |
|
enable = false, |
|
icons = { |
|
hint = "", |
|
info = "", |
|
warning = "", |
|
error = "", |
|
} |
|
}, |
|
-- update the focused file on `BufEnter`, un-collapses the folders recursively until it finds the file |
|
update_focused_file = { |
|
-- enables the feature |
|
enable = false, |
|
-- update the root directory of the tree to the one of the folder containing the file if the file is not under the current root directory |
|
-- only relevant when `update_focused_file.enable` is true |
|
update_cwd = false, |
|
-- list of buffer names / filetypes that will not update the cwd if the file isn't found under the current root directory |
|
-- only relevant when `update_focused_file.update_cwd` is true and `update_focused_file.enable` is true |
|
ignore_list = {} |
|
}, |
|
-- configuration options for the system open command (`s` in the tree by default) |
|
system_open = { |
|
-- the command to run this, leaving nil should work in most cases |
|
cmd = nil, |
|
-- the command arguments as a list |
|
args = {} |
|
}, |
|
|
|
view = { |
|
-- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement |
|
width = 20, |
|
-- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement |
|
height = 30, |
|
-- Hide the root path of the current folder on top of the tree |
|
hide_root_folder = false, |
|
-- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom' |
|
side = 'left', |
|
-- if true the tree will resize itself after opening a file |
|
auto_resize = true, |
|
mappings = { |
|
-- custom only false will merge the list with the default mappings |
|
-- if true, it will only use your list to set the mappings |
|
custom_only = false, |
|
-- list of mappings to set on the tree manually |
|
list = {} |
|
} |
|
} |
|
} |
|
|