init.lua 42 KB

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