init.lua 23 KB

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