init.lua 25 KB

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