forked from nvim-lua/kickstart.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
469 lines (413 loc) · 16.9 KB
/
init.lua
File metadata and controls
469 lines (413 loc) · 16.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
--[[
=====================================================================
==================== READ THIS BEFORE CONTINUING ====================
=====================================================================
This was taken from [Kickstart.nvim](https://github.com/nvim-lua/kickstart.nvim)
It has been heavily modified since then by me.
--]]
require("custom.vim").apply_options()
-- add binds without plugin dep in case I break config =P
local keybinds = require("custom.keybinds")
keybinds.setup_vim_binds()
-- pull in vim options
-- [[ Install `lazy.nvim` plugin manager ]]
-- https://github.com/folke/lazy.nvim
-- `:help lazy.nvim.txt` for more info
local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system {
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
}
end
vim.opt.rtp:prepend(lazypath)
-- [[ Configure plugins ]]
-- NOTE: Here is where you install your plugins.
-- You can configure plugins using the `config` key.
--
-- You can also configure plugins after the setup call,
-- as they will be available in your neovim runtime.
require("lazy").setup({
-- NOTE: First, some plugins that don't require any configuration
-- Git related plugins
"tpope/vim-fugitive",
"tpope/vim-rhubarb",
-- Detect tabstop and shiftwidth automatically
"tpope/vim-sleuth",
-- NOTE: This is where your plugins related to LSP can be installed.
-- The configuration is done below. Search for lspconfig to find it below.
{
-- LSP Configuration & Plugins
"neovim/nvim-lspconfig",
dependencies = {
-- Automatically install LSPs to stdpath for neovim
{ "williamboman/mason.nvim", config = true },
"williamboman/mason-lspconfig.nvim",
-- Useful status updates for LSP
-- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
{ "j-hui/fidget.nvim", opts = {} },
-- Additional lua configuration, makes nvim stuff amazing!
{ "folke/lazydev.nvim", ft = "lua", opts = {} },
},
},
-- Useful plugin to show you pending keybinds.
{ "folke/which-key.nvim", opts = {} },
{
-- Adds git related signs to the gutter, as well as utilities for managing changes
"lewis6991/gitsigns.nvim",
opts = {
-- See `:help gitsigns.txt`
signs = {
add = { text = "+" },
change = { text = "~" },
delete = { text = "_" },
topdelete = { text = "‾" },
changedelete = { text = "~" },
},
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local function map(mode, l, r, opts)
opts = opts or {}
opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts)
end
-- Navigation
map({ "n", "v" }, "]c", function()
if vim.wo.diff then
return "]c"
end
vim.schedule(function()
gs.next_hunk()
end)
return "<Ignore>"
end, { expr = true, desc = "Jump to next hunk" })
map({ "n", "v" }, "[c", function()
if vim.wo.diff then
return "[c"
end
vim.schedule(function()
gs.prev_hunk()
end)
return "<Ignore>"
end, { expr = true, desc = "Jump to previous hunk" })
-- Actions
-- visual mode
map("v", "<leader>hs", function()
gs.stage_hunk { vim.fn.line ".", vim.fn.line "v" }
end, { desc = "stage git hunk" })
map("v", "<leader>hr", function()
gs.reset_hunk { vim.fn.line ".", vim.fn.line "v" }
end, { desc = "reset git hunk" })
-- normal mode
map("n", "<leader>hs", gs.stage_hunk, { desc = "git stage hunk" })
map("n", "<leader>hr", gs.reset_hunk, { desc = "git reset hunk" })
map("n", "<leader>hS", gs.stage_buffer, { desc = "git Stage buffer" })
map("n", "<leader>hu", gs.undo_stage_hunk, { desc = "undo stage hunk" })
map("n", "<leader>hR", gs.reset_buffer, { desc = "git Reset buffer" })
map("n", "<leader>hp", gs.preview_hunk, { desc = "preview git hunk" })
map("n", "<leader>hb", function()
gs.blame_line { full = false }
end, { desc = "git blame line" })
map("n", "<leader>hd", gs.diffthis, { desc = "git diff against index" })
map("n", "<leader>hD", function()
gs.diffthis "~"
end, { desc = "git diff against last commit" })
-- Toggles
map("n", "<leader>tb", gs.toggle_current_line_blame, { desc = "toggle git blame line" })
map("n", "<leader>td", gs.toggle_deleted, { desc = "toggle git show deleted" })
-- Text object
map({ "o", "x" }, "ih", ":<C-U>Gitsigns select_hunk<CR>", { desc = "select git hunk" })
end,
},
},
{
-- Add indentation guides even on blank lines
"lukas-reineke/indent-blankline.nvim",
-- Enable `lukas-reineke/indent-blankline.nvim`
-- See `:help ibl`
main = "ibl",
opts = {},
},
-- Fuzzy Finder (files, lsp, etc)
{
"nvim-telescope/telescope.nvim",
version = "v2.2.0",
dependencies = {
"nvim-lua/plenary.nvim",
-- Fuzzy Finder Algorithm which requires local dependencies to be built.
-- Only load if `make` is available. Make sure you have the system
-- requirements installed.
{
"nvim-telescope/telescope-fzf-native.nvim",
-- NOTE: If you are having trouble with this installation,
-- refer to the README for telescope-fzf-native for more instructions.
build = "make",
cond = function()
return vim.fn.executable "make" == 1
end,
},
},
},
{
"nvim-treesitter/nvim-treesitter-textobjects",
branch = "main",
},
{
-- Highlight, edit, and navigate code
"nvim-treesitter/nvim-treesitter",
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
},
build = ":TSUpdate",
branch = "main",
},
-- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
-- These are some example plugins that I've included in the kickstart repository.
-- Uncomment any of the lines below to enable them.
-- require 'kickstart.plugins.autoformat',
-- require 'kickstart.plugins.debug',
-- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
-- You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
-- up-to-date with whatever is in the kickstart repo.
-- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
--
-- For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
{ import = "custom.plugins" },
}, {})
-- add plugin binds now that they are installed
keybinds.setup_plugin_binds()
-- [[ Highlight on yank ]]
-- See `:help vim.highlight.on_yank()`
local highlight_group = vim.api.nvim_create_augroup("YankHighlight", { clear = true })
vim.api.nvim_create_autocmd("TextYankPost", {
callback = function()
vim.highlight.on_yank()
end,
group = highlight_group,
pattern = "*",
})
-- [[ Configure Telescope ]]
-- See `:help telescope` and `:help telescope.setup()`
require("telescope").setup {
defaults = {
mappings = {
i = {
["<C-u>"] = false,
["<C-d>"] = false,
},
},
},
}
-- Enable telescope fzf native, if installed
pcall(require("telescope").load_extension, "fzf")
-- Telescope live_grep in git root
-- Function to find the git root directory based on the current buffer's path
local function find_git_root()
-- Use the current buffer's path as the starting point for the git search
local current_file = vim.api.nvim_buf_get_name(0)
local current_dir
local cwd = vim.fn.getcwd()
-- If the buffer is not associated with a file, return nil
if current_file == "" then
current_dir = cwd
else
-- Extract the directory from the current file's path
current_dir = vim.fn.fnamemodify(current_file, ":h")
end
-- Find the Git root directory from the current file's path
local git_root = vim.fn.systemlist("git -C " .. vim.fn.escape(current_dir, " ") .. " rev-parse --show-toplevel")[1]
if vim.v.shell_error ~= 0 then
print "Not a git repository. Searching on current working directory"
return cwd
end
return git_root
end
-- Custom live_grep function to search in git root
local function live_grep_git_root()
local git_root = find_git_root()
if git_root then
require("telescope.builtin").live_grep {
search_dirs = { git_root },
}
end
end
vim.api.nvim_create_user_command("LiveGrepGitRoot", live_grep_git_root, {})
-- See `:help telescope.builtin`
vim.keymap.set("n", "<leader>f?", require("telescope.builtin").oldfiles, { desc = "[?] Find recently opened files" })
vim.keymap.set("n", "<leader>f<space>", require("telescope.builtin").buffers, { desc = "[ ] Find existing buffers" })
vim.keymap.set("n", "<leader>fb", function()
-- You can pass additional configuration to telescope to change theme, layout, etc.
require("telescope.builtin").current_buffer_fuzzy_find(require("telescope.themes").get_dropdown {
winblend = 10,
previewer = false,
})
end, { desc = "[b] Fuzzily search in current buffer" })
local function telescope_live_grep_open_files()
require("telescope.builtin").live_grep {
grep_open_files = true,
prompt_title = "Live Grep in Open Files",
}
end
vim.keymap.set("n", "<leader>s/", telescope_live_grep_open_files, { desc = "[S]earch [/] in Open Files" })
vim.keymap.set("n", "<leader>ss", require("telescope.builtin").builtin, { desc = "[S]earch [S]elect Telescope" })
vim.keymap.set("n", "<leader>gf", require("telescope.builtin").git_files, { desc = "Search [G]it [F]iles" })
vim.keymap.set("n", "<leader>sf", require("telescope.builtin").find_files, { desc = "[S]earch [F]iles" })
vim.keymap.set("n", "<leader>ff", require("telescope.builtin").find_files, { desc = "[F]ind [F]iles" })
vim.keymap.set("n", "<leader>fw", require("telescope.builtin").live_grep, { desc = "[F]ind [G]rep" })
vim.keymap.set("n", "<leader>sh", require("telescope.builtin").help_tags, { desc = "[S]earch [H]elp" })
vim.keymap.set("n", "<leader>sw", require("telescope.builtin").grep_string, { desc = "[S]earch current [W]ord" })
vim.keymap.set("n", "<leader>sg", require("telescope.builtin").live_grep, { desc = "[S]earch by [G]rep" })
vim.keymap.set("n", "<leader>sG", ":LiveGrepGitRoot<cr>", { desc = "[S]earch by [G]rep on Git Root" })
vim.keymap.set("n", "<leader>sd", require("telescope.builtin").diagnostics, { desc = "[S]earch [D]iagnostics" })
vim.keymap.set("n", "<leader>sr", require("telescope.builtin").resume, { desc = "[S]earch [R]esume" })
-- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter`
-- Defer Treesitter setup after first render to improve startup time of 'nvim {filename}'
--
-- See `:help nvim-treesitter-intro`
-- NOTE: You can also specify a branch or a specific commit
vim.defer_fn(
function()
local parsers = { "c", "cpp", "go", "lua", "python", "rust", "tsx", "javascript", "typescript", "vimdoc", "vim",
"bash" }
-- require('nvim-treesitter').install(parsers)
---@param buf integer
---@param language string
local function treesitter_try_attach(buf, language)
-- Check if a parser exists and load it
if not vim.treesitter.language.add(language) then return end
-- Enable syntax highlighting and other treesitter features
vim.treesitter.start(buf, language)
-- Enable treesitter based folds
-- For more info on folds see `:help folds`
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
-- vim.wo.foldmethod = 'expr'
-- Check if treesitter indentation is available for this language, and if so enable it
-- in case there is no indent query, the indentexpr will fallback to the vim's built in one
local has_indent_query = vim.treesitter.query.get(language, "indents") ~= nil
-- Enable treesitter based indentation
if has_indent_query then vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end
end
local available_parsers = require("nvim-treesitter").get_available()
vim.api.nvim_create_autocmd("FileType", {
callback = function(args)
local buf, filetype = args.buf, args.match
local language = vim.treesitter.language.get_lang(filetype)
if not language then return end
local installed_parsers = require("nvim-treesitter").get_installed "parsers"
if vim.tbl_contains(installed_parsers, language) then
-- Enable the parser if it is already installed
treesitter_try_attach(buf, language)
elseif vim.tbl_contains(available_parsers, language) then
-- If a parser is available in `nvim-treesitter`, auto-install it and enable it after the installation is done
require("nvim-treesitter").install(language):await(function() treesitter_try_attach(buf, language) end)
else
-- Try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
treesitter_try_attach(buf, language)
end
end,
})
end, 0)
-- Diagnostic keymaps
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, { desc = "Go to previous diagnostic message" })
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, { desc = "Go to next diagnostic message" })
vim.keymap.set("n", "<leader>e", vim.diagnostic.open_float, { desc = "Open floating diagnostic message" })
vim.keymap.set("n", "<leader>dd", vim.diagnostic.setloclist, { desc = "Open diagnostics list" })
-- Git Keymaps
-- auto cmd to make sure the opened buffer is tracked if in git repo
vim.api.nvim_create_autocmd({ "BufEnter" }, { pattern = { "*" }, callback = require("lazygit.utils").project_root_dir })
vim.keymap.set("n", "<leader>gg", "<CMD>LazyGit<CR>", { desc = "Open LazyGit panel" })
-- File Tree
vim.keymap.set("n", "<leader>o", "<CMD>Neotree<CR>", { desc = "Open file tree" })
-- [[ Configure LSP ]]
-- This function gets run when an LSP connects to a particular buffer.
local on_attach = keybinds.lsp_on_attach_binds
-- mason-lspconfig requires that these setup functions are called in this order
-- before setting up the servers.
require("mason").setup()
-- require("mason-lspconfig").setup()
-- Enable the following language servers
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
--
-- Add any additional override configuration in the following tables. They will be passed to
-- the `settings` field of the server config. You must look up that documentation yourself.
--
-- If you want to override the default filetypes that your language server will attach to you can
-- define the property 'filetypes' to the map in question.
local servers = {
-- clangd = {},
-- go
gopls = {},
-- python
-- ruff doesn't give docs on hover or code completion so use zuban for that
zuban = {
--@type lspconfig.settings.zuban
settings = {}
},
ruff = {
-- on_attach = function(client, _)
-- client.server_capabilities.hoverProvider = false
-- end,
},
-- rust
rust_analyzer = {},
-- js/ts
-- tsserver = {},
-- other
dockerls = {},
html = { filetypes = { "html", "twig", "hbs" } },
lua_ls = {
---@type lspconfig.settings.lua_ls
settings = {
Lua = {
format = { enable = false }, -- Disable formatting (formatting is done by stylua)
runtime = { version = "LuaJIT" },
workspace = { checkThirdParty = false },
},
},
},
sqlls = {},
yamlls = { redhat = { telemetry = { enable = false } } },
jsonls = {},
cspell_ls = {},
}
-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
-- Ensure the servers above are installed
local mason_lspconfig = require "mason-lspconfig"
mason_lspconfig.setup {
ensure_installed = vim.tbl_keys(servers),
}
-- Setup servers using new vim.lsp.config API (Neovim 0.12.2+)
for server_name, server_config in pairs(servers) do
local config = {
capabilities = capabilities,
on_attach = on_attach,
settings = server_config,
}
if server_config.filetypes then
config.filetypes = server_config.filetypes
end
if server_config.init_options then
config.init_options = server_config.init_options
end
vim.lsp.config(server_name, config)
end
-- load in helper files, decided not to use right now, but may be good in future
-- for _, source in ipairs {
-- "custom.bootstrap",
-- "custom.options",
-- "custom.lazy",
-- "custom.autocmds",
-- "custom.mappings",
-- } do
-- local status_ok, fault = pcall(require, source)
-- if not status_ok then vim.api.nvim_err_writeln("Failed to load " .. source .. "\n\n" .. fault) end
-- end
-- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et