forked from shiftwinting/defaults.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
400 lines (361 loc) · 12.9 KB
/
Copy pathinit.lua
File metadata and controls
400 lines (361 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
-- Set lightline config BEFORE lazy.setup so it's available when lightline loads
vim.cmd([[
function! LightlineFilename() abort
return empty(expand('%:p')) ? '[No Name]' : expand('%:p')
endfunction
]])
vim.g.lightline = {
colorscheme = "onedark",
active = {
left = {
{ "mode", "paste" },
{ "gitbranch", "readonly", "filename", "modified" },
},
},
component_function = {
gitbranch = "FugitiveHead",
filename = "LightlineFilename",
},
}
require("lazy").setup({
-- Git commands in nvim
"tpope/vim-fugitive",
-- Fugitive-companion to interact with github
"tpope/vim-rhubarb",
-- "gc" to comment visual regions/lines
"tpope/vim-commentary",
-- UI to select things (files, grep results, open buffers...)
{ "nvim-telescope/telescope.nvim", dependencies = { "nvim-lua/plenary.nvim" } },
-- Theme inspired by Atom
"joshdick/onedark.vim",
-- Fancier statusline
"itchyny/lightline.vim",
-- Add indentation guides even on blank lines
"lukas-reineke/indent-blankline.nvim",
-- Collection of configurations for built-in LSP client
"neovim/nvim-lspconfig",
-- Autocompletion plugin
{
"saghen/blink.cmp",
-- Tagged release, so lazy.nvim fetches the prebuilt matcher binary instead
-- of needing a Rust toolchain.
version = "1.*",
opts = {
keymap = {
preset = "none",
["<CR>"] = { "accept", "fallback" },
["<Tab>"] = { "select_next", "snippet_forward", "show", "fallback" },
["<S-Tab>"] = { "select_prev", "snippet_backward", "fallback" },
-- Snippet placeholders get their own keys. <Tab> cannot serve both,
-- because inside a snippet the completion menu reopens as soon as you
-- type.
["<C-l>"] = { "snippet_forward", "fallback" },
["<C-h>"] = { "snippet_backward", "fallback" },
},
completion = {
-- Nothing is selected until you ask, so <CR> still inserts a newline.
list = { selection = { preselect = false } },
},
sources = {
default = { "lsp", "path", "snippets", "buffer" },
providers = {
buffer = { min_keyword_length = 3, score_offset = -3 },
path = { score_offset = -1 },
},
},
fuzzy = {
implementation = "prefer_rust_with_warning",
-- sort_text carries the ranking gopls computed. Keeping it in the chain
-- is the whole point of the switch away from nvim-cmp.
sorts = { "exact", "score", "sort_text" },
},
signature = { enabled = true },
},
},
"hashivim/vim-terraform",
})
vim.opt.completeopt = { "menu", "menuone", "noselect" }
vim.o.shell = "/bin/zsh"
--Incremental live completion
vim.o.inccommand = "nosplit"
--Set highlight on search
vim.o.hlsearch = true
vim.o.incsearch = true
--Make line numbers default
vim.wo.number = true
--Enable mouse mode
vim.o.mouse = "a"
--Auto-reload files changed outside nvim
vim.o.autoread = true
--Enable break indent
vim.o.breakindent = true
--Save undo history
vim.cmd([[set undofile]])
vim.o.sw = 4
vim.o.ts = 4
vim.o.et = 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
vim.cmd([[colorscheme onedark]])
--Remap space as leader key
vim.keymap.set("", "<Space>", "<Nop>", { desc = "Disable space so it can act as leader" })
vim.g.mapleader = " "
vim.g.maplocalleader = " "
--Remap for dealing with word wrap
vim.keymap.set("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, desc = "Up by screen line when no count" })
vim.keymap.set("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, desc = "Down by screen line when no count" })
--Remap escape to leave terminal mode
vim.keymap.set("t", "<Esc>", [[<c-\><c-n>]], { desc = "Leave terminal mode" })
vim.api.nvim_create_user_command("E", "Explore", {})
-- Map blankline (updated for indent-blankline v3)
local highlight = {
"CursorColumn",
"Whitespace",
"LineNr",
}
require("ibl").setup({
indent = {
highlight = highlight,
char = "┊",
},
exclude = {
filetypes = { "help", "lazy" },
buftypes = { "terminal", "nofile" },
},
scope = { enabled = false },
})
local ToggleMouse = function()
if vim.o.mouse == "a" then
vim.cmd([[IBLDisable]])
vim.wo.signcolumn = "no"
vim.o.mouse = "v"
vim.wo.number = false
print("Mouse disabled")
else
vim.cmd([[IBLEnable]])
vim.wo.signcolumn = "yes"
vim.o.mouse = "a"
vim.wo.number = true
print("Mouse enabled")
end
end
vim.keymap.set("n", "<F10>", ToggleMouse, { desc = "Toggle mouse, signs, numbers and indent guides" })
vim.keymap.set("n", "<leader>T", ":tabnew<CR>", { desc = "New tab" })
-- Telescope
require("telescope").setup({
defaults = {
mappings = {
i = {
["<C-j>"] = require("telescope.actions").move_selection_next,
["<C-k>"] = require("telescope.actions").move_selection_previous,
},
},
generic_sorter = require("telescope.sorters").get_fzy_sorter,
file_sorter = require("telescope.sorters").get_fzy_sorter,
},
pickers = {
buffers = {
mappings = {
i = {
["<C-d>"] = require("telescope.actions").delete_buffer + require("telescope.actions").move_to_top,
},
},
},
},
})
--Add leader shortcuts
local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>f", builtin.find_files, { desc = "Find files" })
vim.keymap.set("n", "<leader><space>", builtin.buffers, { desc = "Find open buffers" })
vim.keymap.set("n", "<leader>l", builtin.current_buffer_fuzzy_find, { desc = "Fuzzy find in current buffer" })
vim.keymap.set("n", "<leader>?", builtin.oldfiles, { desc = "Find recently opened files" })
vim.keymap.set("n", "<leader>sd", builtin.grep_string, { desc = "Grep word under cursor" })
vim.keymap.set("n", "<leader>sp", builtin.live_grep, { desc = "Grep in project" })
vim.keymap.set("n", "<leader>sr", builtin.resume, { desc = "Reopen last picker" })
-- Grep inside a directory, prompted. In-editor `cd <dir> && rg <name>`.
vim.keymap.set("n", "<leader>sf", function()
local buf = vim.api.nvim_buf_get_name(0)
local default = buf ~= "" and vim.fn.fnamemodify(buf, ":h:.") .. "/" or "."
vim.ui.input({ prompt = "Dir: ", default = default, completion = "dir" }, function(input)
if not input or input == "" then
return
end
local dir = vim.fn.fnamemodify(vim.fn.expand(input), ":p")
if vim.fn.isdirectory(dir) == 0 then
vim.notify("Not a directory: " .. input, vim.log.levels.ERROR)
return
end
builtin.live_grep({
cwd = dir,
prompt_title = "Grep in " .. vim.fn.fnamemodify(dir, ":~:."),
})
end)
end, { desc = "Grep in a prompted directory" })
vim.keymap.set("n", "<leader>gc", builtin.git_commits, { desc = "Git commits" })
vim.keymap.set("n", "<leader>gb", builtin.git_branches, { desc = "Git branches" })
vim.keymap.set("n", "<leader>gs", builtin.git_status, { desc = "Git status" })
vim.keymap.set("n", "<leader>gp", builtin.git_bcommits, { desc = "Git commits for current buffer" })
-- Change preview window location
vim.o.splitbelow = true
--
-- LSP settings
-- nvim 0.11 ships global grr/grn/gra/gri/grt, which makes plain gr wait
-- timeoutlen before firing. The maps below cover all five.
for _, key in ipairs({ "grr", "grn", "gra", "gri", "grt" }) do
pcall(vim.keymap.del, "n", key)
end
local on_attach = function(_client, bufnr)
vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc"
local map = function(keys, func, desc)
vim.keymap.set("n", keys, func, { buffer = bufnr, desc = desc })
end
map("gD", vim.lsp.buf.declaration, "Go to declaration")
map("gd", vim.lsp.buf.definition, "Go to definition")
map("gi", vim.lsp.buf.implementation, "Go to implementation")
map("<C-k>", vim.lsp.buf.signature_help, "Signature help")
map("<leader>wa", vim.lsp.buf.add_workspace_folder, "Add workspace folder")
map("<leader>wr", vim.lsp.buf.remove_workspace_folder, "Remove workspace folder")
map("<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, "List workspace folders")
map("<leader>D", vim.lsp.buf.type_definition, "Go to type definition")
map("<leader>rn", vim.lsp.buf.rename, "Rename symbol")
map("gr", vim.lsp.buf.references, "List references")
map("<leader>ca", vim.lsp.buf.code_action, "Code action")
map("<leader>e", vim.diagnostic.open_float, "Show diagnostic in a float")
map("[d", function()
vim.diagnostic.jump({ count = -1, float = true })
end, "Previous diagnostic")
map("]d", function()
vim.diagnostic.jump({ count = 1, float = true })
end, "Next diagnostic")
map("<leader>q", vim.diagnostic.setloclist, "Diagnostics to location list")
end
-- setup languages
-- GoLang
-- Merges into the gopls config nvim-lspconfig ships, which already reuses a
-- running gopls for module cache and stdlib files.
vim.lsp.config("gopls", {
on_attach = on_attach,
settings = {
gopls = {
buildFlags = { "-tags=integration" },
experimentalPostfixCompletions = true,
analyses = {
unusedparams = true,
shadow = true,
},
staticcheck = true,
},
},
init_options = {
usePlaceholders = true,
},
})
vim.lsp.enable("gopls")
-- Terraform
-- The default markers are .terraform and .git, so a module directory without
-- .terraform roots the server at the repository top instead. Terraform works a
-- directory at a time, so that directory is the right fallback.
vim.lsp.config("terraformls", {
on_attach = function(client, bufnr)
on_attach(client, bufnr)
-- Passing our own on_attach replaces the one nvim-lspconfig sets, which
-- is the only thing turning code lens on.
vim.lsp.codelens.enable(true, { bufnr = bufnr })
end,
root_dir = function(bufnr, on_dir)
local fname = vim.api.nvim_buf_get_name(bufnr)
on_dir(vim.fs.root(fname, { ".terraform", ".terraform.lock.hcl" }) or vim.fs.dirname(fname))
end,
})
vim.lsp.enable("terraformls")
-- Lua
-- Only VIMRUNTIME goes in the library, so the vim global resolves while editing
-- this file. Indexing all of runtimepath instead would pull in the plugins too,
-- at roughly double the startup and 340MB more memory.
vim.lsp.config("lua_ls", {
on_attach = on_attach,
on_init = function(client)
client.config.settings.Lua = vim.tbl_deep_extend("force", client.config.settings.Lua, {
runtime = { version = "LuaJIT" },
workspace = { checkThirdParty = false, library = { vim.env.VIMRUNTIME } },
})
end,
})
vim.lsp.enable("lua_ls")
vim.keymap.set("n", "<C-n>", ":cn<CR>", { desc = "Next quickfix entry" })
vim.keymap.set("n", "<C-p>", ":cp<CR>", { desc = "Previous quickfix entry" })
vim.keymap.set("n", "<C-c>", ":ccl<CR>", { desc = "Close quickfix window" })
-- Runs synchronously because BufWritePre writes the buffer the moment its
-- callback returns, which rules out the async vim.lsp.buf.code_action.
local function organize_imports(client, timeout_ms)
local params = vim.lsp.util.make_range_params(0, client.offset_encoding)
params.context = { only = { "source.organizeImports" } }
local res = client:request_sync("textDocument/codeAction", params, timeout_ms, 0)
for _, action in pairs(res and res.result or {}) do
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, client.offset_encoding)
end
end
end
local augroup = vim.api.nvim_create_augroup("auto_cmds", { clear = true })
vim.api.nvim_create_autocmd("TextYankPost", {
group = augroup,
callback = function()
vim.hl.on_yank()
end,
})
-- Re-check files changed on disk (e.g. after git checkout, external edits)
-- when switching back to nvim or entering a buffer
vim.api.nvim_create_autocmd({ "FocusGained", "BufEnter" }, {
group = augroup,
command = "checktime",
})
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
pattern = "*.go",
callback = function()
local client = vim.lsp.get_clients({ bufnr = 0, name = "gopls" })[1]
if not client then
return
end
organize_imports(client, 3000)
vim.lsp.buf.format({ async = false })
end,
})
-- Render markdown with glow. Terminal buffer because glow only colors to a tty.
vim.api.nvim_create_autocmd("FileType", {
group = augroup,
pattern = "markdown",
callback = function()
vim.keymap.set("n", "<leader>md", function()
vim.cmd("write")
local file = vim.api.nvim_buf_get_name(0)
vim.cmd("enew")
vim.fn.jobstart({ "glow", file }, { term = true })
vim.cmd("stopinsert")
vim.keymap.set("n", "q", "<cmd>bdelete!<cr>", { buffer = true, desc = "Close glow preview" })
end, { buffer = true, desc = "Render markdown with glow" })
end,
})