init.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. --[[
  2. =====================================================================
  3. ==================== READ THIS BEFORE CONTINUING ====================
  4. =====================================================================
  5. Kickstart.nvim is *not* a distribution.
  6. Kickstart.nvim is a template for your own configuration.
  7. The goal is that you can read every line of code, top-to-bottom, understand
  8. what your configuration is doing, and modify it to suit your needs.
  9. Once you've done that, you should start exploring, configuring and tinkering to
  10. explore Neovim!
  11. If you don't know anything about Lua, I recommend taking some time to read through
  12. a guide. One possible example:
  13. - https://learnxinyminutes.com/docs/lua/
  14. And then you can explore or search through `:help lua-guide`
  15. - https://neovim.io/doc/user/lua-guide.html
  16. Kickstart Guide:
  17. I have left several `:help X` comments throughout the init.lua
  18. You should run that command and read that help section for more information.
  19. In addition, I have some `NOTE:` items throughout the file.
  20. These are for you, the reader to help understand what is happening. Feel free to delete
  21. them once you know what you're doing, but they should serve as a guide for when you
  22. are first encountering a few different constructs in your nvim config.
  23. I hope you enjoy your Neovim journey,
  24. - TJ
  25. P.S. You can delete this when you're done too. It's your config now :)
  26. --]]
  27. -- Set <space> as the leader key
  28. -- See `:help mapleader`
  29. -- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
  30. vim.g.mapleader = ' '
  31. vim.g.maplocalleader = ' '
  32. -- [[ Install `lazy.nvim` plugin manager ]]
  33. -- https://github.com/folke/lazy.nvim
  34. -- `:help lazy.nvim.txt` for more info
  35. local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
  36. if not vim.loop.fs_stat(lazypath) then
  37. vim.fn.system {
  38. 'git',
  39. 'clone',
  40. '--filter=blob:none',
  41. 'https://github.com/folke/lazy.nvim.git',
  42. '--branch=stable', -- latest stable release
  43. lazypath,
  44. }
  45. end
  46. vim.opt.rtp:prepend(lazypath)
  47. -- [[ Configure plugins ]]
  48. -- NOTE: Here is where you install your plugins.
  49. -- You can configure plugins using the `config` key.
  50. --
  51. -- You can also configure plugins after the setup call,
  52. -- as they will be available in your neovim runtime.
  53. require('lazy').setup({
  54. -- NOTE: First, some plugins that don't require any configuration
  55. -- Git related plugins
  56. 'tpope/vim-fugitive',
  57. 'tpope/vim-rhubarb',
  58. -- Detect tabstop and shiftwidth automatically
  59. 'tpope/vim-sleuth',
  60. -- NOTE: This is where your plugins related to LSP can be installed.
  61. -- The configuration is done below. Search for lspconfig to find it below.
  62. {
  63. -- LSP Configuration & Plugins
  64. 'neovim/nvim-lspconfig',
  65. dependencies = {
  66. -- Automatically install LSPs to stdpath for neovim
  67. { 'williamboman/mason.nvim', config = true },
  68. 'williamboman/mason-lspconfig.nvim',
  69. -- Useful status updates for LSP
  70. -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
  71. { 'j-hui/fidget.nvim', opts = {} },
  72. -- Additional lua configuration, makes nvim stuff amazing!
  73. 'folke/neodev.nvim',
  74. },
  75. },
  76. {
  77. -- Autocompletion
  78. 'hrsh7th/nvim-cmp',
  79. dependencies = {
  80. -- Snippet Engine & its associated nvim-cmp source
  81. {
  82. 'L3MON4D3/LuaSnip',
  83. build = (function()
  84. -- Build Step is needed for regex support in snippets
  85. -- This step is not supported in many windows environments
  86. -- Remove the below condition to re-enable on windows
  87. if vim.fn.has 'win32' == 1 then
  88. return
  89. end
  90. return 'make install_jsregexp'
  91. end)(),
  92. },
  93. 'saadparwaiz1/cmp_luasnip',
  94. -- Adds LSP completion capabilities
  95. 'hrsh7th/cmp-nvim-lsp',
  96. 'hrsh7th/cmp-path',
  97. -- Adds a number of user-friendly snippets
  98. 'rafamadriz/friendly-snippets',
  99. },
  100. },
  101. -- Useful plugin to show you pending keybinds.
  102. { 'folke/which-key.nvim', opts = {} },
  103. {
  104. -- Adds git related signs to the gutter, as well as utilities for managing changes
  105. 'lewis6991/gitsigns.nvim',
  106. opts = {
  107. -- See `:help gitsigns.txt`
  108. signs = {
  109. add = { text = '+' },
  110. change = { text = '~' },
  111. delete = { text = '_' },
  112. topdelete = { text = '‾' },
  113. changedelete = { text = '~' },
  114. },
  115. on_attach = function(bufnr)
  116. local gs = package.loaded.gitsigns
  117. local function map(mode, l, r, opts)
  118. opts = opts or {}
  119. opts.buffer = bufnr
  120. vim.keymap.set(mode, l, r, opts)
  121. end
  122. -- Navigation
  123. map({ 'n', 'v' }, ']c', function()
  124. if vim.wo.diff then
  125. return ']c'
  126. end
  127. vim.schedule(function()
  128. gs.next_hunk()
  129. end)
  130. return '<Ignore>'
  131. end, { expr = true, desc = 'Jump to next hunk' })
  132. map({ 'n', 'v' }, '[c', function()
  133. if vim.wo.diff then
  134. return '[c'
  135. end
  136. vim.schedule(function()
  137. gs.prev_hunk()
  138. end)
  139. return '<Ignore>'
  140. end, { expr = true, desc = 'Jump to previous hunk' })
  141. -- Actions
  142. -- visual mode
  143. map('v', '<leader>hs', function()
  144. gs.stage_hunk { vim.fn.line '.', vim.fn.line 'v' }
  145. end, { desc = 'stage git hunk' })
  146. map('v', '<leader>hr', function()
  147. gs.reset_hunk { vim.fn.line '.', vim.fn.line 'v' }
  148. end, { desc = 'reset git hunk' })
  149. -- normal mode
  150. map('n', '<leader>hs', gs.stage_hunk, { desc = 'git stage hunk' })
  151. map('n', '<leader>hr', gs.reset_hunk, { desc = 'git reset hunk' })
  152. map('n', '<leader>hS', gs.stage_buffer, { desc = 'git Stage buffer' })
  153. map('n', '<leader>hu', gs.undo_stage_hunk, { desc = 'undo stage hunk' })
  154. map('n', '<leader>hR', gs.reset_buffer, { desc = 'git Reset buffer' })
  155. map('n', '<leader>hp', gs.preview_hunk, { desc = 'preview git hunk' })
  156. map('n', '<leader>hb', function()
  157. gs.blame_line { full = false }
  158. end, { desc = 'git blame line' })
  159. map('n', '<leader>hd', gs.diffthis, { desc = 'git diff against index' })
  160. map('n', '<leader>hD', function()
  161. gs.diffthis '~'
  162. end, { desc = 'git diff against last commit' })
  163. -- Toggles
  164. map('n', '<leader>tb', gs.toggle_current_line_blame, { desc = 'toggle git blame line' })
  165. map('n', '<leader>td', gs.toggle_deleted, { desc = 'toggle git show deleted' })
  166. -- Text object
  167. map({ 'o', 'x' }, 'ih', ':<C-U>Gitsigns select_hunk<CR>', { desc = 'select git hunk' })
  168. end,
  169. },
  170. },
  171. {
  172. -- Theme inspired by Atom
  173. 'navarasu/onedark.nvim',
  174. priority = 1000,
  175. lazy = false,
  176. config = function()
  177. require('onedark').setup {
  178. -- Set a style preset. 'dark' is default.
  179. style = 'dark', -- dark, darker, cool, deep, warm, warmer, light
  180. }
  181. require('onedark').load()
  182. end,
  183. },
  184. {
  185. -- Set lualine as statusline
  186. 'nvim-lualine/lualine.nvim',
  187. -- See `:help lualine.txt`
  188. opts = {
  189. options = {
  190. icons_enabled = false,
  191. theme = 'auto',
  192. component_separators = '|',
  193. section_separators = '',
  194. },
  195. },
  196. },
  197. {
  198. -- Add indentation guides even on blank lines
  199. 'lukas-reineke/indent-blankline.nvim',
  200. -- Enable `lukas-reineke/indent-blankline.nvim`
  201. -- See `:help ibl`
  202. main = 'ibl',
  203. opts = {},
  204. },
  205. -- "gc" to comment visual regions/lines
  206. { 'numToStr/Comment.nvim', opts = {} },
  207. -- Fuzzy Finder (files, lsp, etc)
  208. {
  209. 'nvim-telescope/telescope.nvim',
  210. branch = '0.1.x',
  211. dependencies = {
  212. 'nvim-lua/plenary.nvim',
  213. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  214. -- Only load if `make` is available. Make sure you have the system
  215. -- requirements installed.
  216. {
  217. 'nvim-telescope/telescope-fzf-native.nvim',
  218. -- NOTE: If you are having trouble with this installation,
  219. -- refer to the README for telescope-fzf-native for more instructions.
  220. build = 'make',
  221. cond = function()
  222. return vim.fn.executable 'make' == 1
  223. end,
  224. },
  225. },
  226. },
  227. {
  228. -- Highlight, edit, and navigate code
  229. 'nvim-treesitter/nvim-treesitter',
  230. dependencies = {
  231. 'nvim-treesitter/nvim-treesitter-textobjects',
  232. },
  233. build = ':TSUpdate',
  234. },
  235. -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
  236. -- These are some example plugins that I've included in the kickstart repository.
  237. -- Uncomment any of the lines below to enable them.
  238. -- require 'kickstart.plugins.autoformat',
  239. -- require 'kickstart.plugins.debug',
  240. -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  241. -- You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
  242. -- up-to-date with whatever is in the kickstart repo.
  243. -- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
  244. --
  245. -- For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
  246. -- { import = 'custom.plugins' },
  247. }, {})
  248. -- [[ Setting options ]]
  249. -- See `:help vim.o`
  250. -- NOTE: You can change these options as you wish!
  251. -- Set highlight on search
  252. vim.o.hlsearch = false
  253. -- Make line numbers default
  254. vim.wo.number = true
  255. -- Enable mouse mode
  256. vim.o.mouse = 'a'
  257. -- Sync clipboard between OS and Neovim.
  258. -- Remove this option if you want your OS clipboard to remain independent.
  259. -- See `:help 'clipboard'`
  260. vim.o.clipboard = 'unnamedplus'
  261. -- Enable break indent
  262. vim.o.breakindent = true
  263. -- Save undo history
  264. vim.o.undofile = true
  265. -- Case-insensitive searching UNLESS \C or capital in search
  266. vim.o.ignorecase = true
  267. vim.o.smartcase = true
  268. -- Keep signcolumn on by default
  269. vim.wo.signcolumn = 'yes'
  270. -- Decrease update time
  271. vim.o.updatetime = 250
  272. vim.o.timeoutlen = 300
  273. -- Set completeopt to have a better completion experience
  274. vim.o.completeopt = 'menuone,noselect'
  275. -- NOTE: You should make sure your terminal supports this
  276. vim.o.termguicolors = true
  277. -- [[ Basic Keymaps ]]
  278. -- Keymaps for better default experience
  279. -- See `:help vim.keymap.set()`
  280. vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
  281. -- Remap for dealing with word wrap
  282. vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
  283. vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
  284. -- Diagnostic keymaps
  285. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
  286. vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
  287. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
  288. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
  289. -- [[ Highlight on yank ]]
  290. -- See `:help vim.highlight.on_yank()`
  291. local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
  292. vim.api.nvim_create_autocmd('TextYankPost', {
  293. callback = function()
  294. vim.highlight.on_yank()
  295. end,
  296. group = highlight_group,
  297. pattern = '*',
  298. })
  299. -- [[ Configure Telescope ]]
  300. -- See `:help telescope` and `:help telescope.setup()`
  301. require('telescope').setup {
  302. defaults = {
  303. mappings = {
  304. i = {
  305. ['<C-u>'] = false,
  306. ['<C-d>'] = false,
  307. },
  308. },
  309. },
  310. }
  311. -- Enable telescope fzf native, if installed
  312. pcall(require('telescope').load_extension, 'fzf')
  313. -- Telescope live_grep in git root
  314. -- Function to find the git root directory based on the current buffer's path
  315. local function find_git_root()
  316. -- Use the current buffer's path as the starting point for the git search
  317. local current_file = vim.api.nvim_buf_get_name(0)
  318. local current_dir
  319. local cwd = vim.fn.getcwd()
  320. -- If the buffer is not associated with a file, return nil
  321. if current_file == '' then
  322. current_dir = cwd
  323. else
  324. -- Extract the directory from the current file's path
  325. current_dir = vim.fn.fnamemodify(current_file, ':h')
  326. end
  327. -- Find the Git root directory from the current file's path
  328. local git_root = vim.fn.systemlist('git -C ' .. vim.fn.escape(current_dir, ' ') .. ' rev-parse --show-toplevel')[1]
  329. if vim.v.shell_error ~= 0 then
  330. print 'Not a git repository. Searching on current working directory'
  331. return cwd
  332. end
  333. return git_root
  334. end
  335. -- Custom live_grep function to search in git root
  336. local function live_grep_git_root()
  337. local git_root = find_git_root()
  338. if git_root then
  339. require('telescope.builtin').live_grep {
  340. search_dirs = { git_root },
  341. }
  342. end
  343. end
  344. vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, {})
  345. -- See `:help telescope.builtin`
  346. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  347. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  348. vim.keymap.set('n', '<leader>/', function()
  349. -- You can pass additional configuration to telescope to change theme, layout, etc.
  350. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  351. winblend = 10,
  352. previewer = false,
  353. })
  354. end, { desc = '[/] Fuzzily search in current buffer' })
  355. local function telescope_live_grep_open_files()
  356. require('telescope.builtin').live_grep {
  357. grep_open_files = true,
  358. prompt_title = 'Live Grep in Open Files',
  359. }
  360. end
  361. vim.keymap.set('n', '<leader>s/', telescope_live_grep_open_files, { desc = '[S]earch [/] in Open Files' })
  362. vim.keymap.set('n', '<leader>ss', require('telescope.builtin').builtin, { desc = '[S]earch [S]elect Telescope' })
  363. vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  364. vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  365. vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  366. vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  367. vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  368. vim.keymap.set('n', '<leader>sG', ':LiveGrepGitRoot<cr>', { desc = '[S]earch by [G]rep on Git Root' })
  369. vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  370. vim.keymap.set('n', '<leader>sr', require('telescope.builtin').resume, { desc = '[S]earch [R]esume' })
  371. -- [[ Configure Treesitter ]]
  372. -- See `:help nvim-treesitter`
  373. -- Defer Treesitter setup after first render to improve startup time of 'nvim {filename}'
  374. vim.defer_fn(function()
  375. require('nvim-treesitter.configs').setup {
  376. -- Add languages to be installed here that you want installed for treesitter
  377. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'javascript', 'typescript', 'vimdoc', 'vim', 'bash' },
  378. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  379. auto_install = false,
  380. -- Install languages synchronously (only applied to `ensure_installed`)
  381. sync_install = false,
  382. -- List of parsers to ignore installing
  383. ignore_install = {},
  384. -- You can specify additional Treesitter modules here: -- For example: -- playground = {--enable = true,-- },
  385. modules = {},
  386. highlight = { enable = true },
  387. indent = { enable = true },
  388. incremental_selection = {
  389. enable = true,
  390. keymaps = {
  391. init_selection = '<c-space>',
  392. node_incremental = '<c-space>',
  393. scope_incremental = '<c-s>',
  394. node_decremental = '<M-space>',
  395. },
  396. },
  397. textobjects = {
  398. select = {
  399. enable = true,
  400. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  401. keymaps = {
  402. -- You can use the capture groups defined in textobjects.scm
  403. ['aa'] = '@parameter.outer',
  404. ['ia'] = '@parameter.inner',
  405. ['af'] = '@function.outer',
  406. ['if'] = '@function.inner',
  407. ['ac'] = '@class.outer',
  408. ['ic'] = '@class.inner',
  409. },
  410. },
  411. move = {
  412. enable = true,
  413. set_jumps = true, -- whether to set jumps in the jumplist
  414. goto_next_start = {
  415. [']m'] = '@function.outer',
  416. [']]'] = '@class.outer',
  417. },
  418. goto_next_end = {
  419. [']M'] = '@function.outer',
  420. [']['] = '@class.outer',
  421. },
  422. goto_previous_start = {
  423. ['[m'] = '@function.outer',
  424. ['[['] = '@class.outer',
  425. },
  426. goto_previous_end = {
  427. ['[M'] = '@function.outer',
  428. ['[]'] = '@class.outer',
  429. },
  430. },
  431. swap = {
  432. enable = true,
  433. swap_next = {
  434. ['<leader>a'] = '@parameter.inner',
  435. },
  436. swap_previous = {
  437. ['<leader>A'] = '@parameter.inner',
  438. },
  439. },
  440. },
  441. }
  442. end, 0)
  443. -- [[ Configure LSP ]]
  444. -- This function gets run when an LSP connects to a particular buffer.
  445. local on_attach = function(_, bufnr)
  446. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  447. -- to define small helper and utility functions so you don't have to repeat yourself
  448. -- many times.
  449. --
  450. -- In this case, we create a function that lets us more easily define mappings specific
  451. -- for LSP related items. It sets the mode, buffer and description for us each time.
  452. local nmap = function(keys, func, desc)
  453. if desc then
  454. desc = 'LSP: ' .. desc
  455. end
  456. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  457. end
  458. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  459. nmap('<leader>ca', function()
  460. vim.lsp.buf.code_action { context = { only = { 'quickfix', 'refactor', 'source' } } }
  461. end, '[C]ode [A]ction')
  462. nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
  463. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  464. nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
  465. nmap('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
  466. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  467. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  468. -- See `:help K` for why this keymap
  469. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  470. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  471. -- Lesser used LSP functionality
  472. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  473. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  474. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  475. nmap('<leader>wl', function()
  476. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  477. end, '[W]orkspace [L]ist Folders')
  478. -- Create a command `:Format` local to the LSP buffer
  479. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  480. vim.lsp.buf.format()
  481. end, { desc = 'Format current buffer with LSP' })
  482. end
  483. -- document existing key chains
  484. require('which-key').register {
  485. ['<leader>c'] = { name = '[C]ode', _ = 'which_key_ignore' },
  486. ['<leader>d'] = { name = '[D]ocument', _ = 'which_key_ignore' },
  487. ['<leader>g'] = { name = '[G]it', _ = 'which_key_ignore' },
  488. ['<leader>h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' },
  489. ['<leader>r'] = { name = '[R]ename', _ = 'which_key_ignore' },
  490. ['<leader>s'] = { name = '[S]earch', _ = 'which_key_ignore' },
  491. ['<leader>t'] = { name = '[T]oggle', _ = 'which_key_ignore' },
  492. ['<leader>w'] = { name = '[W]orkspace', _ = 'which_key_ignore' },
  493. }
  494. -- register which-key VISUAL mode
  495. -- required for visual <leader>hs (hunk stage) to work
  496. require('which-key').register({
  497. ['<leader>'] = { name = 'VISUAL <leader>' },
  498. ['<leader>h'] = { 'Git [H]unk' },
  499. }, { mode = 'v' })
  500. -- mason-lspconfig requires that these setup functions are called in this order
  501. -- before setting up the servers.
  502. require('mason').setup()
  503. require('mason-lspconfig').setup()
  504. -- Enable the following language servers
  505. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  506. --
  507. -- Add any additional override configuration in the following tables. They will be passed to
  508. -- the `settings` field of the server config. You must look up that documentation yourself.
  509. --
  510. -- If you want to override the default filetypes that your language server will attach to you can
  511. -- define the property 'filetypes' to the map in question.
  512. local servers = {
  513. -- clangd = {},
  514. -- gopls = {},
  515. -- pyright = {},
  516. -- rust_analyzer = {},
  517. -- tsserver = {},
  518. -- html = { filetypes = { 'html', 'twig', 'hbs'} },
  519. lua_ls = {
  520. Lua = {
  521. workspace = { checkThirdParty = false },
  522. telemetry = { enable = false },
  523. -- NOTE: toggle below to ignore Lua_LS's noisy `missing-fields` warnings
  524. -- diagnostics = { disable = { 'missing-fields' } },
  525. },
  526. },
  527. }
  528. -- Setup neovim lua configuration
  529. require('neodev').setup()
  530. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  531. local capabilities = vim.lsp.protocol.make_client_capabilities()
  532. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  533. -- Ensure the servers above are installed
  534. local mason_lspconfig = require 'mason-lspconfig'
  535. mason_lspconfig.setup {
  536. ensure_installed = vim.tbl_keys(servers),
  537. }
  538. mason_lspconfig.setup_handlers {
  539. function(server_name)
  540. require('lspconfig')[server_name].setup {
  541. capabilities = capabilities,
  542. on_attach = on_attach,
  543. settings = servers[server_name],
  544. filetypes = (servers[server_name] or {}).filetypes,
  545. }
  546. end,
  547. }
  548. -- [[ Configure nvim-cmp ]]
  549. -- See `:help cmp`
  550. local cmp = require 'cmp'
  551. local luasnip = require 'luasnip'
  552. require('luasnip.loaders.from_vscode').lazy_load()
  553. luasnip.config.setup {}
  554. cmp.setup {
  555. snippet = {
  556. expand = function(args)
  557. luasnip.lsp_expand(args.body)
  558. end,
  559. },
  560. completion = {
  561. completeopt = 'menu,menuone,noinsert',
  562. },
  563. mapping = cmp.mapping.preset.insert {
  564. ['<C-n>'] = cmp.mapping.select_next_item(),
  565. ['<C-p>'] = cmp.mapping.select_prev_item(),
  566. ['<C-b>'] = cmp.mapping.scroll_docs(-4),
  567. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  568. ['<C-Space>'] = cmp.mapping.complete {},
  569. ['<CR>'] = cmp.mapping.confirm {
  570. behavior = cmp.ConfirmBehavior.Replace,
  571. select = true,
  572. },
  573. ['<Tab>'] = cmp.mapping(function(fallback)
  574. if cmp.visible() then
  575. cmp.select_next_item()
  576. elseif luasnip.expand_or_locally_jumpable() then
  577. luasnip.expand_or_jump()
  578. else
  579. fallback()
  580. end
  581. end, { 'i', 's' }),
  582. ['<S-Tab>'] = cmp.mapping(function(fallback)
  583. if cmp.visible() then
  584. cmp.select_prev_item()
  585. elseif luasnip.locally_jumpable(-1) then
  586. luasnip.jump(-1)
  587. else
  588. fallback()
  589. end
  590. end, { 'i', 's' }),
  591. },
  592. sources = {
  593. { name = 'nvim_lsp' },
  594. { name = 'luasnip' },
  595. { name = 'path' },
  596. },
  597. }
  598. -- The line beneath this is called `modeline`. See `:help modeline`
  599. -- vim: ts=2 sts=2 sw=2 et