init.lua 25 KB

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