1+ --[[
2+
3+ =====================================================================
4+ ==================== READ THIS BEFORE CONTINUING ====================
5+ =====================================================================
6+
7+ Kickstart.nvim is *not* a distribution.
8+
9+ Kickstart.nvim is a template for your own configuration.
10+ The goal is that you can read every line of code, top-to-bottom, understand
11+ what your configuration is doing, and modify it to suit your needs.
12+
13+ Once you've done that, you should start exploring, configuring and tinkering to
14+ explore Neovim!
15+
16+ If you don't know anything about Lua, I recommend taking some time to read through
17+ a guide. One possible example:
18+ - https://learnxinyminutes.com/docs/lua/
19+
20+
21+ And then you can explore or search through `:help lua-guide`
22+ - https://neovim.io/doc/user/lua-guide.html
23+
24+
25+ Kickstart Guide:
26+
27+ I have left several `:help X` comments throughout the init.lua
28+ You should run that command and read that help section for more information.
29+
30+ In addition, I have some `NOTE:` items throughout the file.
31+ These are for you, the reader to help understand what is happening. Feel free to delete
32+ them once you know what you're doing, but they should serve as a guide for when you
33+ are first encountering a few different constructs in your nvim config.
34+
35+ I hope you enjoy your Neovim journey,
36+ - TJ
37+
38+ P.S. You can delete this when you're done too. It's your config now :)
39+ --]]
40+
41+ -- Set <space> as the leader key
142-- See `:help mapleader`
243-- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
344vim .g .mapleader = ' '
445vim .g .maplocalleader = ' '
546
6- -- Install package manager
47+ -- [[ Install `lazy.nvim` plugin manager ]]
748-- https://github.com/folke/lazy.nvim
849-- `:help lazy.nvim.txt` for more info
950local lazypath = vim .fn .stdpath ' data' .. ' /lazy/lazy.nvim'
@@ -19,6 +60,7 @@ if not vim.loop.fs_stat(lazypath) then
1960end
2061vim .opt .rtp :prepend (lazypath )
2162
63+ -- [[ Configure plugins ]]
2264-- NOTE: Here is where you install your plugins.
2365-- You can configure plugins using the `config` key.
2466--
@@ -46,7 +88,7 @@ require('lazy').setup({
4688
4789 -- Useful status updates for LSP
4890 -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
49- { ' j-hui/fidget.nvim' , tag = ' legacy ' , opts = {} },
91+ { ' j-hui/fidget.nvim' , opts = {} },
5092
5193 -- Additional lua configuration, makes nvim stuff amazing!
5294 ' folke/neodev.nvim' ,
@@ -63,6 +105,7 @@ require('lazy').setup({
63105
64106 -- Adds LSP completion capabilities
65107 ' hrsh7th/cmp-nvim-lsp' ,
108+ ' hrsh7th/cmp-path' ,
66109
67110 -- Adds a number of user-friendly snippets
68111 ' rafamadriz/friendly-snippets' ,
@@ -84,28 +127,64 @@ require('lazy').setup({
84127 changedelete = { text = ' ~' },
85128 },
86129 on_attach = function (bufnr )
87- vim .keymap .set (' n' , ' <leader>hp' , require (' gitsigns' ).preview_hunk , { buffer = bufnr , desc = ' Preview git hunk' })
88-
89- -- don't override the built-in and fugitive keymaps
90130 local gs = package.loaded .gitsigns
91- vim .keymap .set ({ ' n' , ' v' }, ' ]c' , function ()
131+
132+ local function map (mode , l , r , opts )
133+ opts = opts or {}
134+ opts .buffer = bufnr
135+ vim .keymap .set (mode , l , r , opts )
136+ end
137+
138+ -- Navigation
139+ map ({ ' n' , ' v' }, ' ]c' , function ()
92140 if vim .wo .diff then
93141 return ' ]c'
94142 end
95143 vim .schedule (function ()
96144 gs .next_hunk ()
97145 end )
98146 return ' <Ignore>'
99- end , { expr = true , buffer = bufnr , desc = ' Jump to next hunk' })
100- vim .keymap .set ({ ' n' , ' v' }, ' [c' , function ()
147+ end , { expr = true , desc = ' Jump to next hunk' })
148+
149+ map ({ ' n' , ' v' }, ' [c' , function ()
101150 if vim .wo .diff then
102151 return ' [c'
103152 end
104153 vim .schedule (function ()
105154 gs .prev_hunk ()
106155 end )
107156 return ' <Ignore>'
108- end , { expr = true , buffer = bufnr , desc = ' Jump to previous hunk' })
157+ end , { expr = true , desc = ' Jump to previous hunk' })
158+
159+ -- Actions
160+ -- visual mode
161+ map (' v' , ' <leader>hs' , function ()
162+ gs .stage_hunk { vim .fn .line ' .' , vim .fn .line ' v' }
163+ end , { desc = ' stage git hunk' })
164+ map (' v' , ' <leader>hr' , function ()
165+ gs .reset_hunk { vim .fn .line ' .' , vim .fn .line ' v' }
166+ end , { desc = ' reset git hunk' })
167+ -- normal mode
168+ map (' n' , ' <leader>hs' , gs .stage_hunk , { desc = ' git stage hunk' })
169+ map (' n' , ' <leader>hr' , gs .reset_hunk , { desc = ' git reset hunk' })
170+ map (' n' , ' <leader>hS' , gs .stage_buffer , { desc = ' git Stage buffer' })
171+ map (' n' , ' <leader>hu' , gs .undo_stage_hunk , { desc = ' undo stage hunk' })
172+ map (' n' , ' <leader>hR' , gs .reset_buffer , { desc = ' git Reset buffer' })
173+ map (' n' , ' <leader>hp' , gs .preview_hunk , { desc = ' preview git hunk' })
174+ map (' n' , ' <leader>hb' , function ()
175+ gs .blame_line { full = false }
176+ end , { desc = ' git blame line' })
177+ map (' n' , ' <leader>hd' , gs .diffthis , { desc = ' git diff against index' })
178+ map (' n' , ' <leader>hD' , function ()
179+ gs .diffthis ' ~'
180+ end , { desc = ' git diff against last commit' })
181+
182+ -- Toggles
183+ map (' n' , ' <leader>tb' , gs .toggle_current_line_blame , { desc = ' toggle git blame line' })
184+ map (' n' , ' <leader>td' , gs .toggle_deleted , { desc = ' toggle git show deleted' })
185+
186+ -- Text object
187+ map ({ ' o' , ' x' }, ' ih' , ' :<C-U>Gitsigns select_hunk<CR>' , { desc = ' select git hunk' })
109188 end ,
110189 },
111190 },
@@ -241,6 +320,12 @@ vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
241320vim .keymap .set (' n' , ' k' , " v:count == 0 ? 'gk' : 'k'" , { expr = true , silent = true })
242321vim .keymap .set (' n' , ' j' , " v:count == 0 ? 'gj' : 'j'" , { expr = true , silent = true })
243322
323+ -- Diagnostic keymaps
324+ vim .keymap .set (' n' , ' [d' , vim .diagnostic .goto_prev , { desc = ' Go to previous diagnostic message' })
325+ vim .keymap .set (' n' , ' ]d' , vim .diagnostic .goto_next , { desc = ' Go to next diagnostic message' })
326+ vim .keymap .set (' n' , ' <leader>e' , vim .diagnostic .open_float , { desc = ' Open floating diagnostic message' })
327+ vim .keymap .set (' n' , ' <leader>q' , vim .diagnostic .setloclist , { desc = ' Open diagnostics list' })
328+
244329-- [[ Highlight on yank ]]
245330-- See `:help vim.highlight.on_yank()`
246331local highlight_group = vim .api .nvim_create_augroup (' YankHighlight' , { clear = true })
@@ -268,6 +353,42 @@ require('telescope').setup {
268353-- Enable telescope fzf native, if installed
269354pcall (require (' telescope' ).load_extension , ' fzf' )
270355
356+ -- Telescope live_grep in git root
357+ -- Function to find the git root directory based on the current buffer's path
358+ local function find_git_root ()
359+ -- Use the current buffer's path as the starting point for the git search
360+ local current_file = vim .api .nvim_buf_get_name (0 )
361+ local current_dir
362+ local cwd = vim .fn .getcwd ()
363+ -- If the buffer is not associated with a file, return nil
364+ if current_file == ' ' then
365+ current_dir = cwd
366+ else
367+ -- Extract the directory from the current file's path
368+ current_dir = vim .fn .fnamemodify (current_file , ' :h' )
369+ end
370+
371+ -- Find the Git root directory from the current file's path
372+ local git_root = vim .fn .systemlist (' git -C ' .. vim .fn .escape (current_dir , ' ' ) .. ' rev-parse --show-toplevel' )[1 ]
373+ if vim .v .shell_error ~= 0 then
374+ print ' Not a git repository. Searching on current working directory'
375+ return cwd
376+ end
377+ return git_root
378+ end
379+
380+ -- Custom live_grep function to search in git root
381+ local function live_grep_git_root ()
382+ local git_root = find_git_root ()
383+ if git_root then
384+ require (' telescope.builtin' ).live_grep {
385+ search_dirs = { git_root },
386+ }
387+ end
388+ end
389+
390+ vim .api .nvim_create_user_command (' LiveGrepGitRoot' , live_grep_git_root , {})
391+
271392-- See `:help telescope.builtin`
272393vim .keymap .set (' n' , ' <leader>?' , require (' telescope.builtin' ).oldfiles , { desc = ' [?] Find recently opened files' })
273394vim .keymap .set (' n' , ' <leader><space>' , require (' telescope.builtin' ).buffers , { desc = ' [ ] Find existing buffers' })
@@ -279,11 +400,20 @@ vim.keymap.set('n', '<leader>/', function()
279400 })
280401end , { desc = ' [/] Fuzzily search in current buffer' })
281402
403+ local function telescope_live_grep_open_files ()
404+ require (' telescope.builtin' ).live_grep {
405+ grep_open_files = true ,
406+ prompt_title = ' Live Grep in Open Files' ,
407+ }
408+ end
409+ vim .keymap .set (' n' , ' <leader>s/' , telescope_live_grep_open_files , { desc = ' [S]earch [/] in Open Files' })
410+ vim .keymap .set (' n' , ' <leader>ss' , require (' telescope.builtin' ).builtin , { desc = ' [S]earch [S]elect Telescope' })
282411vim .keymap .set (' n' , ' <leader>gf' , require (' telescope.builtin' ).git_files , { desc = ' Search [G]it [F]iles' })
283412vim .keymap .set (' n' , ' <leader>sf' , require (' telescope.builtin' ).find_files , { desc = ' [S]earch [F]iles' })
284413vim .keymap .set (' n' , ' <leader>sh' , require (' telescope.builtin' ).help_tags , { desc = ' [S]earch [H]elp' })
285414vim .keymap .set (' n' , ' <leader>sw' , require (' telescope.builtin' ).grep_string , { desc = ' [S]earch current [W]ord' })
286415vim .keymap .set (' n' , ' <leader>sg' , require (' telescope.builtin' ).live_grep , { desc = ' [S]earch by [G]rep' })
416+ vim .keymap .set (' n' , ' <leader>sG' , ' :LiveGrepGitRoot<cr>' , { desc = ' [S]earch by [G]rep on Git Root' })
287417vim .keymap .set (' n' , ' <leader>sd' , require (' telescope.builtin' ).diagnostics , { desc = ' [S]earch [D]iagnostics' })
288418vim .keymap .set (' n' , ' <leader>sr' , require (' telescope.builtin' ).resume , { desc = ' [S]earch [R]esume' })
289419
@@ -356,12 +486,6 @@ vim.defer_fn(function()
356486 }
357487end , 0 )
358488
359- -- Diagnostic keymaps
360- vim .keymap .set (' n' , ' [d' , vim .diagnostic .goto_prev , { desc = ' Go to previous diagnostic message' })
361- vim .keymap .set (' n' , ' ]d' , vim .diagnostic .goto_next , { desc = ' Go to next diagnostic message' })
362- vim .keymap .set (' n' , ' <leader>e' , vim .diagnostic .open_float , { desc = ' Open floating diagnostic message' })
363- vim .keymap .set (' n' , ' <leader>q' , vim .diagnostic .setloclist , { desc = ' Open diagnostics list' })
364-
365489-- [[ Configure LSP ]]
366490-- This function gets run when an LSP connects to a particular buffer.
367491local on_attach = function (_ , bufnr )
@@ -412,11 +536,18 @@ require('which-key').register {
412536 [' <leader>c' ] = { name = ' [C]ode' , _ = ' which_key_ignore' },
413537 [' <leader>d' ] = { name = ' [D]ocument' , _ = ' which_key_ignore' },
414538 [' <leader>g' ] = { name = ' [G]it' , _ = ' which_key_ignore' },
415- [' <leader>h' ] = { name = ' More git ' , _ = ' which_key_ignore' },
539+ [' <leader>h' ] = { name = ' Git [H]unk ' , _ = ' which_key_ignore' },
416540 [' <leader>r' ] = { name = ' [R]ename' , _ = ' which_key_ignore' },
417541 [' <leader>s' ] = { name = ' [S]earch' , _ = ' which_key_ignore' },
542+ [' <leader>t' ] = { name = ' [T]oggle' , _ = ' which_key_ignore' },
418543 [' <leader>w' ] = { name = ' [W]orkspace' , _ = ' which_key_ignore' },
419544}
545+ -- register which-key VISUAL mode
546+ -- required for visual <leader>hs (hunk stage) to work
547+ require (' which-key' ).register ({
548+ [' <leader>' ] = { name = ' VISUAL <leader>' },
549+ [' <leader>h' ] = { ' Git [H]unk' },
550+ }, { mode = ' v' })
420551
421552-- mason-lspconfig requires that these setup functions are called in this order
422553-- before setting up the servers.
@@ -443,6 +574,8 @@ local servers = {
443574 Lua = {
444575 workspace = { checkThirdParty = false },
445576 telemetry = { enable = false },
577+ -- NOTE: toggle below to ignore Lua_LS's noisy `missing-fields` warnings
578+ -- diagnostics = { disable = { 'missing-fields' } },
446579 },
447580 },
448581}
@@ -486,6 +619,9 @@ cmp.setup {
486619 luasnip .lsp_expand (args .body )
487620 end ,
488621 },
622+ completion = {
623+ completeopt = ' menu,menuone,noinsert' ,
624+ },
489625 mapping = cmp .mapping .preset .insert {
490626 [' <C-n>' ] = cmp .mapping .select_next_item (),
491627 [' <C-p>' ] = cmp .mapping .select_prev_item (),
@@ -518,6 +654,7 @@ cmp.setup {
518654 sources = {
519655 { name = ' nvim_lsp' },
520656 { name = ' luasnip' },
657+ { name = ' path' },
521658 },
522659}
523660vim .wo .relativenumber = true
0 commit comments