init.lua 41 KB

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