init.lua 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. --[[
  2. =====================================================================
  3. ==================== READ THIS BEFORE CONTINUING ====================
  4. =====================================================================
  5. ======== .-----. ========
  6. ======== .----------------------. | === | ========
  7. ======== |.-""""""""""""""""""-.| |-----| ========
  8. ======== || || | === | ========
  9. ======== || KICKSTART.NVIM || |-----| ========
  10. ======== || || | === | ========
  11. ======== || || |-----| ========
  12. ======== ||:Tutor || |:::::| ========
  13. ======== |'-..................-'| |____o| ========
  14. ======== `"")----------------(""` ___________ ========
  15. ======== /::::::::::| |::::::::::\ \ no mouse \ ========
  16. ======== /:::========| |==hjkl==:::\ \ required \ ========
  17. ======== '""""""""""""' '""""""""""""' '""""""""""' ========
  18. ======== ========
  19. =====================================================================
  20. =====================================================================
  21. What is Kickstart?
  22. Kickstart.nvim is *not* a distribution.
  23. Kickstart.nvim is a starting point for your own configuration.
  24. The goal is that you can read every line of code, top-to-bottom, understand
  25. what your configuration is doing, and modify it to suit your needs.
  26. Once you've done that, you can start exploring, configuring and tinkering to
  27. make Neovim your own! That might mean leaving Kickstart just the way it is for a while
  28. or immediately breaking it into modular pieces. It's up to you!
  29. If you don't know anything about Lua, I recommend taking some time to read through
  30. a guide. One possible example which will only take 10-15 minutes:
  31. - https://learnxinyminutes.com/docs/lua/
  32. After understanding a bit more about Lua, you can use `:help lua-guide` as a
  33. reference for how Neovim integrates Lua.
  34. - :help lua-guide
  35. - (or HTML version): https://neovim.io/doc/user/lua-guide.html
  36. Kickstart Guide:
  37. TODO: The very first thing you should do is to run the command `:Tutor` in Neovim.
  38. If you don't know what this means, type the following:
  39. - <escape key>
  40. - :
  41. - Tutor
  42. - <enter key>
  43. (If you already know the Neovim basics, you can skip this step.)
  44. Once you've completed that, you can continue working through **AND READING** the rest
  45. of the kickstart init.lua.
  46. Next, run AND READ `:help`.
  47. This will open up a help window with some basic information
  48. about reading, navigating and searching the builtin help documentation.
  49. This should be the first place you go to look when you're stuck or confused
  50. with something. It's one of my favorite Neovim features.
  51. MOST IMPORTANTLY, we provide a keymap "<space>sh" to [s]earch the [h]elp documentation,
  52. which is very useful when you're not exactly sure of what you're looking for.
  53. I have left several `:help X` comments throughout the init.lua
  54. These are hints about where to find more information about the relevant settings,
  55. plugins or Neovim features used in Kickstart.
  56. NOTE: Look for lines like this
  57. Throughout the file. These are for you, the reader, to help you understand what is happening.
  58. Feel free to delete them once you know what you're doing, but they should serve as a guide
  59. for when you are first encountering a few different constructs in your Neovim config.
  60. If you experience any errors while trying to install kickstart, run `:checkhealth` for more info.
  61. I hope you enjoy your Neovim journey,
  62. - TJ
  63. P.S. You can delete this when you're done too. It's your config now! :)
  64. --]]
  65. -- Set <space> as the leader key
  66. -- See `:help mapleader`
  67. -- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used)
  68. vim.g.mapleader = ' '
  69. vim.g.maplocalleader = ' '
  70. -- Set to true if you have a Nerd Font installed and selected in the terminal
  71. vim.g.have_nerd_font = false
  72. -- [[ Setting options ]]
  73. -- See `:help vim.opt`
  74. -- NOTE: You can change these options as you wish!
  75. -- For more options, you can see `:help option-list`
  76. -- Make line numbers default
  77. vim.opt.number = true
  78. -- You can also add relative line numbers, to help with jumping.
  79. -- Experiment for yourself to see if you like it!
  80. -- vim.opt.relativenumber = true
  81. -- Enable mouse mode, can be useful for resizing splits for example!
  82. vim.opt.mouse = 'a'
  83. -- Don't show the mode, since it's already in the status line
  84. vim.opt.showmode = false
  85. -- Sync clipboard between OS and Neovim.
  86. -- Schedule the setting after `UiEnter` because it can increase startup-time.
  87. -- Remove this option if you want your OS clipboard to remain independent.
  88. -- See `:help 'clipboard'`
  89. vim.schedule(function()
  90. vim.opt.clipboard = 'unnamedplus'
  91. end)
  92. -- Enable break indent
  93. vim.opt.breakindent = true
  94. -- Save undo history
  95. vim.opt.undofile = true
  96. -- Case-insensitive searching UNLESS \C or one or more capital letters in the search term
  97. vim.opt.ignorecase = true
  98. vim.opt.smartcase = true
  99. -- Keep signcolumn on by default
  100. vim.opt.signcolumn = 'yes'
  101. -- Decrease update time
  102. vim.opt.updatetime = 250
  103. -- Decrease mapped sequence wait time
  104. vim.opt.timeoutlen = 300
  105. -- Configure how new splits should be opened
  106. vim.opt.splitright = true
  107. vim.opt.splitbelow = true
  108. -- Sets how neovim will display certain whitespace characters in the editor.
  109. -- See `:help 'list'`
  110. -- and `:help 'listchars'`
  111. vim.opt.list = true
  112. vim.opt.listchars = { tab = '» ', trail = '·', nbsp = '␣' }
  113. -- Preview substitutions live, as you type!
  114. vim.opt.inccommand = 'split'
  115. -- Show which line your cursor is on
  116. vim.opt.cursorline = true
  117. -- Minimal number of screen lines to keep above and below the cursor.
  118. vim.opt.scrolloff = 10
  119. -- if performing an operation that would fail due to unsaved changes in the buffer (like `:q`),
  120. -- instead raise a dialog asking if you wish to save the current file(s)
  121. -- See `:help 'confirm'`
  122. vim.opt.confirm = true
  123. -- [[ Basic Keymaps ]]
  124. -- See `:help vim.keymap.set()`
  125. -- Clear highlights on search when pressing <Esc> in normal mode
  126. -- See `:help hlsearch`
  127. vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>')
  128. -- Diagnostic keymaps
  129. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' })
  130. -- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier
  131. -- for people to discover. Otherwise, you normally need to press <C-\><C-n>, which
  132. -- is not what someone will guess without a bit more experience.
  133. --
  134. -- NOTE: This won't work in all terminal emulators/tmux/etc. Try your own mapping
  135. -- or just use <C-\><C-n> to exit terminal mode
  136. vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
  137. -- TIP: Disable arrow keys in normal mode
  138. -- vim.keymap.set('n', '<left>', '<cmd>echo "Use h to move!!"<CR>')
  139. -- vim.keymap.set('n', '<right>', '<cmd>echo "Use l to move!!"<CR>')
  140. -- vim.keymap.set('n', '<up>', '<cmd>echo "Use k to move!!"<CR>')
  141. -- vim.keymap.set('n', '<down>', '<cmd>echo "Use j to move!!"<CR>')
  142. -- Keybinds to make split navigation easier.
  143. -- Use CTRL+<hjkl> to switch between windows
  144. --
  145. -- See `:help wincmd` for a list of all window commands
  146. vim.keymap.set('n', '<C-h>', '<C-w><C-h>', { desc = 'Move focus to the left window' })
  147. vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right window' })
  148. vim.keymap.set('n', '<C-j>', '<C-w><C-j>', { desc = 'Move focus to the lower window' })
  149. vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper window' })
  150. -- [[ Basic Autocommands ]]
  151. -- See `:help lua-guide-autocommands`
  152. -- Highlight when yanking (copying) text
  153. -- Try it with `yap` in normal mode
  154. -- See `:help vim.highlight.on_yank()`
  155. vim.api.nvim_create_autocmd('TextYankPost', {
  156. desc = 'Highlight when yanking (copying) text',
  157. group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }),
  158. callback = function()
  159. vim.highlight.on_yank()
  160. end,
  161. })
  162. -- [[ Install `lazy.nvim` plugin manager ]]
  163. -- See `:help lazy.nvim.txt` or https://github.com/folke/lazy.nvim for more info
  164. local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
  165. if not (vim.uv or vim.loop).fs_stat(lazypath) then
  166. local lazyrepo = 'https://github.com/folke/lazy.nvim.git'
  167. local out = vim.fn.system { 'git', 'clone', '--filter=blob:none', '--branch=stable', lazyrepo, lazypath }
  168. if vim.v.shell_error ~= 0 then
  169. error('Error cloning lazy.nvim:\n' .. out)
  170. end
  171. end ---@diagnostic disable-next-line: undefined-field
  172. vim.opt.rtp:prepend(lazypath)
  173. -- [[ Configure and install plugins ]]
  174. --
  175. -- To check the current status of your plugins, run
  176. -- :Lazy
  177. --
  178. -- You can press `?` in this menu for help. Use `:q` to close the window
  179. --
  180. -- To update plugins you can run
  181. -- :Lazy update
  182. --
  183. -- NOTE: Here is where you install your plugins.
  184. require('lazy').setup({
  185. -- NOTE: Plugins can be added with a link (or for a github repo: 'owner/repo' link).
  186. 'tpope/vim-sleuth', -- Detect tabstop and shiftwidth automatically
  187. -- NOTE: Plugins can also be added by using a table,
  188. -- with the first argument being the link and the following
  189. -- keys can be used to configure plugin behavior/loading/etc.
  190. --
  191. -- Use `opts = {}` to automatically pass options to a plugin's `setup()` function, forcing the plugin to be loaded.
  192. --
  193. -- Alternatively, use `config = function() ... end` for full control over the configuration.
  194. -- If you prefer to call `setup` explicitly, use:
  195. -- {
  196. -- 'lewis6991/gitsigns.nvim',
  197. -- config = function()
  198. -- require('gitsigns').setup({
  199. -- -- Your gitsigns configuration here
  200. -- })
  201. -- end,
  202. -- }
  203. --
  204. -- Here is a more advanced example where we pass configuration
  205. -- options to `gitsigns.nvim`.
  206. --
  207. -- See `:help gitsigns` to understand what the configuration keys do
  208. { -- Adds git related signs to the gutter, as well as utilities for managing changes
  209. 'lewis6991/gitsigns.nvim',
  210. opts = {
  211. signs = {
  212. add = { text = '+' },
  213. change = { text = '~' },
  214. delete = { text = '_' },
  215. topdelete = { text = '‾' },
  216. changedelete = { text = '~' },
  217. },
  218. },
  219. },
  220. -- NOTE: Plugins can also be configured to run Lua code when they are loaded.
  221. --
  222. -- This is often very useful to both group configuration, as well as handle
  223. -- lazy loading plugins that don't need to be loaded immediately at startup.
  224. --
  225. -- For example, in the following configuration, we use:
  226. -- event = 'VimEnter'
  227. --
  228. -- which loads which-key before all the UI elements are loaded. Events can be
  229. -- normal autocommands events (`:help autocmd-events`).
  230. --
  231. -- Then, because we use the `opts` key (recommended), the configuration runs
  232. -- after the plugin has been loaded as `require(MODULE).setup(opts)`.
  233. { -- Useful plugin to show you pending keybinds.
  234. 'folke/which-key.nvim',
  235. event = 'VimEnter', -- Sets the loading event to 'VimEnter'
  236. opts = {
  237. -- delay between pressing a key and opening which-key (milliseconds)
  238. -- this setting is independent of vim.opt.timeoutlen
  239. delay = 0,
  240. icons = {
  241. -- set icon mappings to true if you have a Nerd Font
  242. mappings = vim.g.have_nerd_font,
  243. -- If you are using a Nerd Font: set icons.keys to an empty table which will use the
  244. -- default which-key.nvim defined Nerd Font icons, otherwise define a string table
  245. keys = vim.g.have_nerd_font and {} or {
  246. Up = '<Up> ',
  247. Down = '<Down> ',
  248. Left = '<Left> ',
  249. Right = '<Right> ',
  250. C = '<C-…> ',
  251. M = '<M-…> ',
  252. D = '<D-…> ',
  253. S = '<S-…> ',
  254. CR = '<CR> ',
  255. Esc = '<Esc> ',
  256. ScrollWheelDown = '<ScrollWheelDown> ',
  257. ScrollWheelUp = '<ScrollWheelUp> ',
  258. NL = '<NL> ',
  259. BS = '<BS> ',
  260. Space = '<Space> ',
  261. Tab = '<Tab> ',
  262. F1 = '<F1>',
  263. F2 = '<F2>',
  264. F3 = '<F3>',
  265. F4 = '<F4>',
  266. F5 = '<F5>',
  267. F6 = '<F6>',
  268. F7 = '<F7>',
  269. F8 = '<F8>',
  270. F9 = '<F9>',
  271. F10 = '<F10>',
  272. F11 = '<F11>',
  273. F12 = '<F12>',
  274. },
  275. },
  276. -- Document existing key chains
  277. spec = {
  278. { '<leader>c', group = '[C]ode', mode = { 'n', 'x' } },
  279. { '<leader>d', group = '[D]ocument' },
  280. { '<leader>r', group = '[R]ename' },
  281. { '<leader>s', group = '[S]earch' },
  282. { '<leader>w', group = '[W]orkspace' },
  283. { '<leader>t', group = '[T]oggle' },
  284. { '<leader>h', group = 'Git [H]unk', mode = { 'n', 'v' } },
  285. },
  286. },
  287. },
  288. -- NOTE: Plugins can specify dependencies.
  289. --
  290. -- The dependencies are proper plugin specifications as well - anything
  291. -- you do for a plugin at the top level, you can do for a dependency.
  292. --
  293. -- Use the `dependencies` key to specify the dependencies of a particular plugin
  294. { -- Fuzzy Finder (files, lsp, etc)
  295. 'nvim-telescope/telescope.nvim',
  296. event = 'VimEnter',
  297. branch = '0.1.x',
  298. dependencies = {
  299. 'nvim-lua/plenary.nvim',
  300. { -- If encountering errors, see telescope-fzf-native README for installation instructions
  301. 'nvim-telescope/telescope-fzf-native.nvim',
  302. -- `build` is used to run some command when the plugin is installed/updated.
  303. -- This is only run then, not every time Neovim starts up.
  304. build = 'make',
  305. -- `cond` is a condition used to determine whether this plugin should be
  306. -- installed and loaded.
  307. cond = function()
  308. return vim.fn.executable 'make' == 1
  309. end,
  310. },
  311. { 'nvim-telescope/telescope-ui-select.nvim' },
  312. -- Useful for getting pretty icons, but requires a Nerd Font.
  313. { 'nvim-tree/nvim-web-devicons', enabled = vim.g.have_nerd_font },
  314. },
  315. config = function()
  316. -- Telescope is a fuzzy finder that comes with a lot of different things that
  317. -- it can fuzzy find! It's more than just a "file finder", it can search
  318. -- many different aspects of Neovim, your workspace, LSP, and more!
  319. --
  320. -- The easiest way to use Telescope, is to start by doing something like:
  321. -- :Telescope help_tags
  322. --
  323. -- After running this command, a window will open up and you're able to
  324. -- type in the prompt window. You'll see a list of `help_tags` options and
  325. -- a corresponding preview of the help.
  326. --
  327. -- Two important keymaps to use while in Telescope are:
  328. -- - Insert mode: <c-/>
  329. -- - Normal mode: ?
  330. --
  331. -- This opens a window that shows you all of the keymaps for the current
  332. -- Telescope picker. This is really useful to discover what Telescope can
  333. -- do as well as how to actually do it!
  334. -- [[ Configure Telescope ]]
  335. -- See `:help telescope` and `:help telescope.setup()`
  336. require('telescope').setup {
  337. -- You can put your default mappings / updates / etc. in here
  338. -- All the info you're looking for is in `:help telescope.setup()`
  339. --
  340. -- defaults = {
  341. -- mappings = {
  342. -- i = { ['<c-enter>'] = 'to_fuzzy_refine' },
  343. -- },
  344. -- },
  345. -- pickers = {}
  346. extensions = {
  347. ['ui-select'] = {
  348. require('telescope.themes').get_dropdown(),
  349. },
  350. },
  351. }
  352. -- Enable Telescope extensions if they are installed
  353. pcall(require('telescope').load_extension, 'fzf')
  354. pcall(require('telescope').load_extension, 'ui-select')
  355. -- See `:help telescope.builtin`
  356. local builtin = require 'telescope.builtin'
  357. vim.keymap.set('n', '<leader>sh', builtin.help_tags, { desc = '[S]earch [H]elp' })
  358. vim.keymap.set('n', '<leader>sk', builtin.keymaps, { desc = '[S]earch [K]eymaps' })
  359. vim.keymap.set('n', '<leader>sf', builtin.find_files, { desc = '[S]earch [F]iles' })
  360. vim.keymap.set('n', '<leader>ss', builtin.builtin, { desc = '[S]earch [S]elect Telescope' })
  361. vim.keymap.set('n', '<leader>sw', builtin.grep_string, { desc = '[S]earch current [W]ord' })
  362. vim.keymap.set('n', '<leader>sg', builtin.live_grep, { desc = '[S]earch by [G]rep' })
  363. vim.keymap.set('n', '<leader>sd', builtin.diagnostics, { desc = '[S]earch [D]iagnostics' })
  364. vim.keymap.set('n', '<leader>sr', builtin.resume, { desc = '[S]earch [R]esume' })
  365. vim.keymap.set('n', '<leader>s.', builtin.oldfiles, { desc = '[S]earch Recent Files ("." for repeat)' })
  366. vim.keymap.set('n', '<leader><leader>', builtin.buffers, { desc = '[ ] Find existing buffers' })
  367. -- Slightly advanced example of overriding default behavior and theme
  368. vim.keymap.set('n', '<leader>/', function()
  369. -- You can pass additional configuration to Telescope to change the theme, layout, etc.
  370. builtin.current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  371. winblend = 10,
  372. previewer = false,
  373. })
  374. end, { desc = '[/] Fuzzily search in current buffer' })
  375. -- It's also possible to pass additional configuration options.
  376. -- See `:help telescope.builtin.live_grep()` for information about particular keys
  377. vim.keymap.set('n', '<leader>s/', function()
  378. builtin.live_grep {
  379. grep_open_files = true,
  380. prompt_title = 'Live Grep in Open Files',
  381. }
  382. end, { desc = '[S]earch [/] in Open Files' })
  383. -- Shortcut for searching your Neovim configuration files
  384. vim.keymap.set('n', '<leader>sn', function()
  385. builtin.find_files { cwd = vim.fn.stdpath 'config' }
  386. end, { desc = '[S]earch [N]eovim files' })
  387. end,
  388. },
  389. -- LSP Plugins
  390. {
  391. -- `lazydev` configures Lua LSP for your Neovim config, runtime and plugins
  392. -- used for completion, annotations and signatures of Neovim apis
  393. 'folke/lazydev.nvim',
  394. ft = 'lua',
  395. opts = {
  396. library = {
  397. -- Load luvit types when the `vim.uv` word is found
  398. { path = '${3rd}/luv/library', words = { 'vim%.uv' } },
  399. },
  400. },
  401. },
  402. {
  403. -- Main LSP Configuration
  404. 'neovim/nvim-lspconfig',
  405. dependencies = {
  406. -- Automatically install LSPs and related tools to stdpath for Neovim
  407. -- Mason must be loaded before its dependents so we need to set it up here.
  408. -- NOTE: `opts = {}` is the same as calling `require('mason').setup({})`
  409. { 'williamboman/mason.nvim', opts = {} },
  410. 'williamboman/mason-lspconfig.nvim',
  411. 'WhoIsSethDaniel/mason-tool-installer.nvim',
  412. -- Useful status updates for LSP.
  413. { 'j-hui/fidget.nvim', opts = {} },
  414. -- Allows extra capabilities provided by nvim-cmp
  415. 'hrsh7th/cmp-nvim-lsp',
  416. },
  417. config = function()
  418. -- Brief aside: **What is LSP?**
  419. --
  420. -- LSP is an initialism you've probably heard, but might not understand what it is.
  421. --
  422. -- LSP stands for Language Server Protocol. It's a protocol that helps editors
  423. -- and language tooling communicate in a standardized fashion.
  424. --
  425. -- In general, you have a "server" which is some tool built to understand a particular
  426. -- language (such as `gopls`, `lua_ls`, `rust_analyzer`, etc.). These Language Servers
  427. -- (sometimes called LSP servers, but that's kind of like ATM Machine) are standalone
  428. -- processes that communicate with some "client" - in this case, Neovim!
  429. --
  430. -- LSP provides Neovim with features like:
  431. -- - Go to definition
  432. -- - Find references
  433. -- - Autocompletion
  434. -- - Symbol Search
  435. -- - and more!
  436. --
  437. -- Thus, Language Servers are external tools that must be installed separately from
  438. -- Neovim. This is where `mason` and related plugins come into play.
  439. --
  440. -- If you're wondering about lsp vs treesitter, you can check out the wonderfully
  441. -- and elegantly composed help section, `:help lsp-vs-treesitter`
  442. -- This function gets run when an LSP attaches to a particular buffer.
  443. -- That is to say, every time a new file is opened that is associated with
  444. -- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
  445. -- function will be executed to configure the current buffer
  446. vim.api.nvim_create_autocmd('LspAttach', {
  447. group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }),
  448. callback = function(event)
  449. -- NOTE: Remember that Lua is a real programming language, and as such it is possible
  450. -- to define small helper and utility functions so you don't have to repeat yourself.
  451. --
  452. -- In this case, we create a function that lets us more easily define mappings specific
  453. -- for LSP related items. It sets the mode, buffer and description for us each time.
  454. local map = function(keys, func, desc, mode)
  455. mode = mode or 'n'
  456. vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc })
  457. end
  458. -- Jump to the definition of the word under your cursor.
  459. -- This is where a variable was first declared, or where a function is defined, etc.
  460. -- To jump back, press <C-t>.
  461. map('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
  462. -- Find references for the word under your cursor.
  463. map('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  464. -- Jump to the implementation of the word under your cursor.
  465. -- Useful when your language has ways of declaring types without an actual implementation.
  466. map('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
  467. -- Jump to the type of the word under your cursor.
  468. -- Useful when you're not sure what type a variable is and you want to see
  469. -- the definition of its *type*, not where it was *defined*.
  470. map('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
  471. -- Fuzzy find all the symbols in your current document.
  472. -- Symbols are things like variables, functions, types, etc.
  473. map('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  474. -- Fuzzy find all the symbols in your current workspace.
  475. -- Similar to document symbols, except searches over your entire project.
  476. map('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  477. -- Rename the variable under your cursor.
  478. -- Most Language Servers support renaming across files, etc.
  479. map('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  480. -- Execute a code action, usually your cursor needs to be on top of an error
  481. -- or a suggestion from your LSP for this to activate.
  482. map('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction', { 'n', 'x' })
  483. -- WARN: This is not Goto Definition, this is Goto Declaration.
  484. -- For example, in C this would take you to the header.
  485. map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  486. -- This function resolves a difference between neovim nightly (version 0.11) and stable (version 0.10)
  487. ---@param client vim.lsp.Client
  488. ---@param method vim.lsp.protocol.Method
  489. ---@param bufnr? integer some lsp support methods only in specific files
  490. ---@return boolean
  491. local function client_supports_method(client, method, bufnr)
  492. if vim.fn.has 'nvim-0.11' == 1 then
  493. return client:supports_method(method, bufnr)
  494. else
  495. return client.supports_method(method, { bufnr = bufnr })
  496. end
  497. end
  498. -- The following two autocommands are used to highlight references of the
  499. -- word under your cursor when your cursor rests there for a little while.
  500. -- See `:help CursorHold` for information about when this is executed
  501. --
  502. -- When you move your cursor, the highlights will be cleared (the second autocommand).
  503. local client = vim.lsp.get_client_by_id(event.data.client_id)
  504. if client and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_documentHighlight, event.buf) then
  505. local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = false })
  506. vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
  507. buffer = event.buf,
  508. group = highlight_augroup,
  509. callback = vim.lsp.buf.document_highlight,
  510. })
  511. vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
  512. buffer = event.buf,
  513. group = highlight_augroup,
  514. callback = vim.lsp.buf.clear_references,
  515. })
  516. vim.api.nvim_create_autocmd('LspDetach', {
  517. group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }),
  518. callback = function(event2)
  519. vim.lsp.buf.clear_references()
  520. vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event2.buf }
  521. end,
  522. })
  523. end
  524. -- The following code creates a keymap to toggle inlay hints in your
  525. -- code, if the language server you are using supports them
  526. --
  527. -- This may be unwanted, since they displace some of your code
  528. if client and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_inlayHint, event.buf) then
  529. map('<leader>th', function()
  530. vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled { bufnr = event.buf })
  531. end, '[T]oggle Inlay [H]ints')
  532. end
  533. end,
  534. })
  535. -- Diagnostic Config
  536. -- See :help vim.diagnostic.Opts
  537. vim.diagnostic.config {
  538. severity_sort = true,
  539. float = { border = 'rounded', source = 'if_many' },
  540. underline = { severity = vim.diagnostic.severity.ERROR },
  541. signs = vim.g.have_nerd_font and {
  542. text = {
  543. [vim.diagnostic.severity.ERROR] = '󰅚 ',
  544. [vim.diagnostic.severity.WARN] = '󰀪 ',
  545. [vim.diagnostic.severity.INFO] = '󰋽 ',
  546. [vim.diagnostic.severity.HINT] = '󰌶 ',
  547. },
  548. } or {},
  549. virtual_text = {
  550. source = 'if_many',
  551. spacing = 2,
  552. format = function(diagnostic)
  553. local diagnostic_message = {
  554. [vim.diagnostic.severity.ERROR] = diagnostic.message,
  555. [vim.diagnostic.severity.WARN] = diagnostic.message,
  556. [vim.diagnostic.severity.INFO] = diagnostic.message,
  557. [vim.diagnostic.severity.HINT] = diagnostic.message,
  558. }
  559. return diagnostic_message[diagnostic.severity]
  560. end,
  561. },
  562. }
  563. -- LSP servers and clients are able to communicate to each other what features they support.
  564. -- By default, Neovim doesn't support everything that is in the LSP specification.
  565. -- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities.
  566. -- So, we create new capabilities with nvim cmp, and then broadcast that to the servers.
  567. local capabilities = vim.lsp.protocol.make_client_capabilities()
  568. capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
  569. -- Enable the following language servers
  570. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  571. --
  572. -- Add any additional override configuration in the following tables. Available keys are:
  573. -- - cmd (table): Override the default command used to start the server
  574. -- - filetypes (table): Override the default list of associated filetypes for the server
  575. -- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
  576. -- - settings (table): Override the default settings passed when initializing the server.
  577. -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
  578. local servers = {
  579. -- clangd = {},
  580. -- gopls = {},
  581. -- pyright = {},
  582. -- rust_analyzer = {},
  583. -- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
  584. --
  585. -- Some languages (like typescript) have entire language plugins that can be useful:
  586. -- https://github.com/pmizio/typescript-tools.nvim
  587. --
  588. -- But for many setups, the LSP (`ts_ls`) will work just fine
  589. -- ts_ls = {},
  590. --
  591. lua_ls = {
  592. -- cmd = { ... },
  593. -- filetypes = { ... },
  594. -- capabilities = {},
  595. settings = {
  596. Lua = {
  597. completion = {
  598. callSnippet = 'Replace',
  599. },
  600. -- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
  601. -- diagnostics = { disable = { 'missing-fields' } },
  602. },
  603. },
  604. },
  605. }
  606. -- Ensure the servers and tools above are installed
  607. --
  608. -- To check the current status of installed tools and/or manually install
  609. -- other tools, you can run
  610. -- :Mason
  611. --
  612. -- You can press `g?` for help in this menu.
  613. --
  614. -- `mason` had to be setup earlier: to configure its options see the
  615. -- `dependencies` table for `nvim-lspconfig` above.
  616. --
  617. -- You can add other tools here that you want Mason to install
  618. -- for you, so that they are available from within Neovim.
  619. local ensure_installed = vim.tbl_keys(servers or {})
  620. vim.list_extend(ensure_installed, {
  621. 'stylua', -- Used to format Lua code
  622. })
  623. require('mason-tool-installer').setup { ensure_installed = ensure_installed }
  624. require('mason-lspconfig').setup {
  625. ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
  626. automatic_installation = false,
  627. handlers = {
  628. function(server_name)
  629. local server = servers[server_name] or {}
  630. -- This handles overriding only values explicitly passed
  631. -- by the server configuration above. Useful when disabling
  632. -- certain features of an LSP (for example, turning off formatting for ts_ls)
  633. server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
  634. require('lspconfig')[server_name].setup(server)
  635. end,
  636. },
  637. }
  638. end,
  639. },
  640. { -- Autoformat
  641. 'stevearc/conform.nvim',
  642. event = { 'BufWritePre' },
  643. cmd = { 'ConformInfo' },
  644. keys = {
  645. {
  646. '<leader>f',
  647. function()
  648. require('conform').format { async = true, lsp_format = 'fallback' }
  649. end,
  650. mode = '',
  651. desc = '[F]ormat buffer',
  652. },
  653. },
  654. opts = {
  655. notify_on_error = false,
  656. format_on_save = function(bufnr)
  657. -- Disable "format_on_save lsp_fallback" for languages that don't
  658. -- have a well standardized coding style. You can add additional
  659. -- languages here or re-enable it for the disabled ones.
  660. local disable_filetypes = { c = true, cpp = true }
  661. if disable_filetypes[vim.bo[bufnr].filetype] then
  662. return nil
  663. else
  664. return {
  665. timeout_ms = 500,
  666. lsp_format = 'fallback',
  667. }
  668. end
  669. end,
  670. formatters_by_ft = {
  671. lua = { 'stylua' },
  672. -- Conform can also run multiple formatters sequentially
  673. -- python = { "isort", "black" },
  674. --
  675. -- You can use 'stop_after_first' to run the first available formatter from the list
  676. -- javascript = { "prettierd", "prettier", stop_after_first = true },
  677. },
  678. },
  679. },
  680. { -- Autocompletion
  681. 'hrsh7th/nvim-cmp',
  682. event = 'InsertEnter',
  683. dependencies = {
  684. -- Snippet Engine & its associated nvim-cmp source
  685. {
  686. 'L3MON4D3/LuaSnip',
  687. build = (function()
  688. -- Build Step is needed for regex support in snippets.
  689. -- This step is not supported in many windows environments.
  690. -- Remove the below condition to re-enable on windows.
  691. if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then
  692. return
  693. end
  694. return 'make install_jsregexp'
  695. end)(),
  696. dependencies = {
  697. -- `friendly-snippets` contains a variety of premade snippets.
  698. -- See the README about individual language/framework/plugin snippets:
  699. -- https://github.com/rafamadriz/friendly-snippets
  700. -- {
  701. -- 'rafamadriz/friendly-snippets',
  702. -- config = function()
  703. -- require('luasnip.loaders.from_vscode').lazy_load()
  704. -- end,
  705. -- },
  706. },
  707. },
  708. 'saadparwaiz1/cmp_luasnip',
  709. -- Adds other completion capabilities.
  710. -- nvim-cmp does not ship with all sources by default. They are split
  711. -- into multiple repos for maintenance purposes.
  712. 'hrsh7th/cmp-nvim-lsp',
  713. 'hrsh7th/cmp-path',
  714. 'hrsh7th/cmp-nvim-lsp-signature-help',
  715. },
  716. config = function()
  717. -- See `:help cmp`
  718. local cmp = require 'cmp'
  719. local luasnip = require 'luasnip'
  720. luasnip.config.setup {}
  721. cmp.setup {
  722. snippet = {
  723. expand = function(args)
  724. luasnip.lsp_expand(args.body)
  725. end,
  726. },
  727. completion = { completeopt = 'menu,menuone,noinsert' },
  728. -- For an understanding of why these mappings were
  729. -- chosen, you will need to read `:help ins-completion`
  730. --
  731. -- No, but seriously. Please read `:help ins-completion`, it is really good!
  732. mapping = cmp.mapping.preset.insert {
  733. -- Select the [n]ext item
  734. ['<C-n>'] = cmp.mapping.select_next_item(),
  735. -- Select the [p]revious item
  736. ['<C-p>'] = cmp.mapping.select_prev_item(),
  737. -- Scroll the documentation window [b]ack / [f]orward
  738. ['<C-b>'] = cmp.mapping.scroll_docs(-4),
  739. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  740. -- Accept ([y]es) the completion.
  741. -- This will auto-import if your LSP supports it.
  742. -- This will expand snippets if the LSP sent a snippet.
  743. ['<C-y>'] = cmp.mapping.confirm { select = true },
  744. -- If you prefer more traditional completion keymaps,
  745. -- you can uncomment the following lines
  746. --['<CR>'] = cmp.mapping.confirm { select = true },
  747. --['<Tab>'] = cmp.mapping.select_next_item(),
  748. --['<S-Tab>'] = cmp.mapping.select_prev_item(),
  749. -- Manually trigger a completion from nvim-cmp.
  750. -- Generally you don't need this, because nvim-cmp will display
  751. -- completions whenever it has completion options available.
  752. ['<C-Space>'] = cmp.mapping.complete {},
  753. -- Think of <c-l> as moving to the right of your snippet expansion.
  754. -- So if you have a snippet that's like:
  755. -- function $name($args)
  756. -- $body
  757. -- end
  758. --
  759. -- <c-l> will move you to the right of each of the expansion locations.
  760. -- <c-h> is similar, except moving you backwards.
  761. ['<C-l>'] = cmp.mapping(function()
  762. if luasnip.expand_or_locally_jumpable() then
  763. luasnip.expand_or_jump()
  764. end
  765. end, { 'i', 's' }),
  766. ['<C-h>'] = cmp.mapping(function()
  767. if luasnip.locally_jumpable(-1) then
  768. luasnip.jump(-1)
  769. end
  770. end, { 'i', 's' }),
  771. -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see:
  772. -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps
  773. },
  774. sources = {
  775. {
  776. name = 'lazydev',
  777. -- set group index to 0 to skip loading LuaLS completions as lazydev recommends it
  778. group_index = 0,
  779. },
  780. { name = 'nvim_lsp' },
  781. { name = 'luasnip' },
  782. { name = 'path' },
  783. { name = 'nvim_lsp_signature_help' },
  784. },
  785. }
  786. end,
  787. },
  788. { -- You can easily change to a different colorscheme.
  789. -- Change the name of the colorscheme plugin below, and then
  790. -- change the command in the config to whatever the name of that colorscheme is.
  791. --
  792. -- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`.
  793. 'folke/tokyonight.nvim',
  794. priority = 1000, -- Make sure to load this before all the other start plugins.
  795. config = function()
  796. ---@diagnostic disable-next-line: missing-fields
  797. require('tokyonight').setup {
  798. styles = {
  799. comments = { italic = false }, -- Disable italics in comments
  800. },
  801. }
  802. -- Load the colorscheme here.
  803. -- Like many other themes, this one has different styles, and you could load
  804. -- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'.
  805. vim.cmd.colorscheme 'tokyonight-night'
  806. end,
  807. },
  808. -- Highlight todo, notes, etc in comments
  809. { 'folke/todo-comments.nvim', event = 'VimEnter', dependencies = { 'nvim-lua/plenary.nvim' }, opts = { signs = false } },
  810. { -- Collection of various small independent plugins/modules
  811. 'echasnovski/mini.nvim',
  812. config = function()
  813. -- Better Around/Inside textobjects
  814. --
  815. -- Examples:
  816. -- - va) - [V]isually select [A]round [)]paren
  817. -- - yinq - [Y]ank [I]nside [N]ext [Q]uote
  818. -- - ci' - [C]hange [I]nside [']quote
  819. require('mini.ai').setup { n_lines = 500 }
  820. -- Add/delete/replace surroundings (brackets, quotes, etc.)
  821. --
  822. -- - saiw) - [S]urround [A]dd [I]nner [W]ord [)]Paren
  823. -- - sd' - [S]urround [D]elete [']quotes
  824. -- - sr)' - [S]urround [R]eplace [)] [']
  825. require('mini.surround').setup()
  826. -- Simple and easy statusline.
  827. -- You could remove this setup call if you don't like it,
  828. -- and try some other statusline plugin
  829. local statusline = require 'mini.statusline'
  830. -- set use_icons to true if you have a Nerd Font
  831. statusline.setup { use_icons = vim.g.have_nerd_font }
  832. -- You can configure sections in the statusline by overriding their
  833. -- default behavior. For example, here we set the section for
  834. -- cursor location to LINE:COLUMN
  835. ---@diagnostic disable-next-line: duplicate-set-field
  836. statusline.section_location = function()
  837. return '%2l:%-2v'
  838. end
  839. -- ... and there is more!
  840. -- Check out: https://github.com/echasnovski/mini.nvim
  841. end,
  842. },
  843. { -- Highlight, edit, and navigate code
  844. 'nvim-treesitter/nvim-treesitter',
  845. build = ':TSUpdate',
  846. main = 'nvim-treesitter.configs', -- Sets main module to use for opts
  847. -- [[ Configure Treesitter ]] See `:help nvim-treesitter`
  848. opts = {
  849. ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
  850. -- Autoinstall languages that are not installed
  851. auto_install = true,
  852. highlight = {
  853. enable = true,
  854. -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
  855. -- If you are experiencing weird indenting issues, add the language to
  856. -- the list of additional_vim_regex_highlighting and disabled languages for indent.
  857. additional_vim_regex_highlighting = { 'ruby' },
  858. },
  859. indent = { enable = true, disable = { 'ruby' } },
  860. },
  861. -- There are additional nvim-treesitter modules that you can use to interact
  862. -- with nvim-treesitter. You should go explore a few and see what interests you:
  863. --
  864. -- - Incremental selection: Included, see `:help nvim-treesitter-incremental-selection-mod`
  865. -- - Show your current context: https://github.com/nvim-treesitter/nvim-treesitter-context
  866. -- - Treesitter + textobjects: https://github.com/nvim-treesitter/nvim-treesitter-textobjects
  867. },
  868. -- The following comments only work if you have downloaded the kickstart repo, not just copy pasted the
  869. -- init.lua. If you want these files, they are in the repository, so you can just download them and
  870. -- place them in the correct locations.
  871. -- NOTE: Next step on your Neovim journey: Add/Configure additional plugins for Kickstart
  872. --
  873. -- Here are some example plugins that I've included in the Kickstart repository.
  874. -- Uncomment any of the lines below to enable them (you will need to restart nvim).
  875. --
  876. -- require 'kickstart.plugins.debug',
  877. -- require 'kickstart.plugins.indent_line',
  878. -- require 'kickstart.plugins.lint',
  879. -- require 'kickstart.plugins.autopairs',
  880. -- require 'kickstart.plugins.neo-tree',
  881. -- require 'kickstart.plugins.gitsigns', -- adds gitsigns recommend keymaps
  882. -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  883. -- This is the easiest way to modularize your config.
  884. --
  885. -- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
  886. -- { import = 'custom.plugins' },
  887. --
  888. -- For additional information with loading, sourcing and examples see `:help lazy.nvim-🔌-plugin-spec`
  889. -- Or use telescope!
  890. -- In normal mode type `<space>sh` then write `lazy.nvim-plugin`
  891. -- you can continue same window with `<space>sr` which resumes last telescope search
  892. }, {
  893. ui = {
  894. -- If you are using a Nerd Font: set icons to an empty table which will use the
  895. -- default lazy.nvim defined Nerd Font icons, otherwise define a unicode icons table
  896. icons = vim.g.have_nerd_font and {} or {
  897. cmd = '⌘',
  898. config = '🛠',
  899. event = '📅',
  900. ft = '📂',
  901. init = '⚙',
  902. keys = '🗝',
  903. plugin = '🔌',
  904. runtime = '💻',
  905. require = '🌙',
  906. source = '📄',
  907. start = '🚀',
  908. task = '📌',
  909. lazy = '💤 ',
  910. },
  911. },
  912. })
  913. -- The line beneath this is called `modeline`. See `:help modeline`
  914. -- vim: ts=2 sts=2 sw=2 et