init.lua 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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 colliding 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. 'NMAC427/guess-indent.nvim', -- 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>s', group = '[S]earch' },
  284. { '<leader>t', group = '[T]oggle' },
  285. { '<leader>h', group = 'Git [H]unk', mode = { 'n', 'v' } },
  286. },
  287. },
  288. },
  289. -- NOTE: Plugins can specify dependencies.
  290. --
  291. -- The dependencies are proper plugin specifications as well - anything
  292. -- you do for a plugin at the top level, you can do for a dependency.
  293. --
  294. -- Use the `dependencies` key to specify the dependencies of a particular plugin
  295. { -- Fuzzy Finder (files, lsp, etc)
  296. 'nvim-telescope/telescope.nvim',
  297. event = 'VimEnter',
  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. { 'mason-org/mason.nvim', opts = {} },
  410. 'mason-org/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 blink.cmp
  415. 'saghen/blink.cmp',
  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. -- Rename the variable under your cursor.
  459. -- Most Language Servers support renaming across files, etc.
  460. map('grn', vim.lsp.buf.rename, '[R]e[n]ame')
  461. -- Execute a code action, usually your cursor needs to be on top of an error
  462. -- or a suggestion from your LSP for this to activate.
  463. map('gra', vim.lsp.buf.code_action, '[G]oto Code [A]ction', { 'n', 'x' })
  464. -- Find references for the word under your cursor.
  465. map('grr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  466. -- Jump to the implementation of the word under your cursor.
  467. -- Useful when your language has ways of declaring types without an actual implementation.
  468. map('gri', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
  469. -- Jump to the definition of the word under your cursor.
  470. -- This is where a variable was first declared, or where a function is defined, etc.
  471. -- To jump back, press <C-t>.
  472. map('grd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
  473. -- WARN: This is not Goto Definition, this is Goto Declaration.
  474. -- For example, in C this would take you to the header.
  475. map('grD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  476. -- Fuzzy find all the symbols in your current document.
  477. -- Symbols are things like variables, functions, types, etc.
  478. map('gO', require('telescope.builtin').lsp_document_symbols, 'Open Document Symbols')
  479. -- Fuzzy find all the symbols in your current workspace.
  480. -- Similar to document symbols, except searches over your entire project.
  481. map('gW', require('telescope.builtin').lsp_dynamic_workspace_symbols, 'Open Workspace Symbols')
  482. -- Jump to the type of the word under your cursor.
  483. -- Useful when you're not sure what type a variable is and you want to see
  484. -- the definition of its *type*, not where it was *defined*.
  485. map('grt', require('telescope.builtin').lsp_type_definitions, '[G]oto [T]ype Definition')
  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 blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
  566. -- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
  567. local capabilities = require('blink.cmp').get_lsp_capabilities()
  568. -- Enable the following language servers
  569. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  570. --
  571. -- Add any additional override configuration in the following tables. Available keys are:
  572. -- - cmd (table): Override the default command used to start the server
  573. -- - filetypes (table): Override the default list of associated filetypes for the server
  574. -- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
  575. -- - settings (table): Override the default settings passed when initializing the server.
  576. -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
  577. local servers = {
  578. -- clangd = {},
  579. -- gopls = {},
  580. -- pyright = {},
  581. -- rust_analyzer = {},
  582. -- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
  583. --
  584. -- Some languages (like typescript) have entire language plugins that can be useful:
  585. -- https://github.com/pmizio/typescript-tools.nvim
  586. --
  587. -- But for many setups, the LSP (`ts_ls`) will work just fine
  588. -- ts_ls = {},
  589. --
  590. lua_ls = {
  591. -- cmd = { ... },
  592. -- filetypes = { ... },
  593. -- capabilities = {},
  594. settings = {
  595. Lua = {
  596. completion = {
  597. callSnippet = 'Replace',
  598. },
  599. -- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
  600. -- diagnostics = { disable = { 'missing-fields' } },
  601. },
  602. },
  603. },
  604. }
  605. -- Ensure the servers and tools above are installed
  606. --
  607. -- To check the current status of installed tools and/or manually install
  608. -- other tools, you can run
  609. -- :Mason
  610. --
  611. -- You can press `g?` for help in this menu.
  612. --
  613. -- `mason` had to be setup earlier: to configure its options see the
  614. -- `dependencies` table for `nvim-lspconfig` above.
  615. --
  616. -- You can add other tools here that you want Mason to install
  617. -- for you, so that they are available from within Neovim.
  618. local ensure_installed = vim.tbl_keys(servers or {})
  619. vim.list_extend(ensure_installed, {
  620. 'stylua', -- Used to format Lua code
  621. })
  622. require('mason-tool-installer').setup { ensure_installed = ensure_installed }
  623. require('mason-lspconfig').setup {
  624. ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
  625. automatic_installation = false,
  626. handlers = {
  627. function(server_name)
  628. local server = servers[server_name] or {}
  629. -- This handles overriding only values explicitly passed
  630. -- by the server configuration above. Useful when disabling
  631. -- certain features of an LSP (for example, turning off formatting for ts_ls)
  632. server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
  633. require('lspconfig')[server_name].setup(server)
  634. end,
  635. },
  636. }
  637. end,
  638. },
  639. { -- Autoformat
  640. 'stevearc/conform.nvim',
  641. event = { 'BufWritePre' },
  642. cmd = { 'ConformInfo' },
  643. keys = {
  644. {
  645. '<leader>f',
  646. function()
  647. require('conform').format { async = true, lsp_format = 'fallback' }
  648. end,
  649. mode = '',
  650. desc = '[F]ormat buffer',
  651. },
  652. },
  653. opts = {
  654. notify_on_error = false,
  655. format_on_save = function(bufnr)
  656. -- Disable "format_on_save lsp_fallback" for languages that don't
  657. -- have a well standardized coding style. You can add additional
  658. -- languages here or re-enable it for the disabled ones.
  659. local disable_filetypes = { c = true, cpp = true }
  660. if disable_filetypes[vim.bo[bufnr].filetype] then
  661. return nil
  662. else
  663. return {
  664. timeout_ms = 500,
  665. lsp_format = 'fallback',
  666. }
  667. end
  668. end,
  669. formatters_by_ft = {
  670. lua = { 'stylua' },
  671. -- Conform can also run multiple formatters sequentially
  672. -- python = { "isort", "black" },
  673. --
  674. -- You can use 'stop_after_first' to run the first available formatter from the list
  675. -- javascript = { "prettierd", "prettier", stop_after_first = true },
  676. },
  677. },
  678. },
  679. { -- Autocompletion
  680. 'saghen/blink.cmp',
  681. event = 'VimEnter',
  682. version = '1.*',
  683. dependencies = {
  684. -- Snippet Engine
  685. {
  686. 'L3MON4D3/LuaSnip',
  687. version = '2.*',
  688. build = (function()
  689. -- Build Step is needed for regex support in snippets.
  690. -- This step is not supported in many windows environments.
  691. -- Remove the below condition to re-enable on windows.
  692. if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then
  693. return
  694. end
  695. return 'make install_jsregexp'
  696. end)(),
  697. dependencies = {
  698. -- `friendly-snippets` contains a variety of premade snippets.
  699. -- See the README about individual language/framework/plugin snippets:
  700. -- https://github.com/rafamadriz/friendly-snippets
  701. -- {
  702. -- 'rafamadriz/friendly-snippets',
  703. -- config = function()
  704. -- require('luasnip.loaders.from_vscode').lazy_load()
  705. -- end,
  706. -- },
  707. },
  708. opts = {},
  709. },
  710. 'folke/lazydev.nvim',
  711. },
  712. --- @module 'blink.cmp'
  713. --- @type blink.cmp.Config
  714. opts = {
  715. keymap = {
  716. -- 'default' (recommended) for mappings similar to built-in completions
  717. -- <c-y> to accept ([y]es) the completion.
  718. -- This will auto-import if your LSP supports it.
  719. -- This will expand snippets if the LSP sent a snippet.
  720. -- 'super-tab' for tab to accept
  721. -- 'enter' for enter to accept
  722. -- 'none' for no mappings
  723. --
  724. -- For an understanding of why the 'default' preset is recommended,
  725. -- you will need to read `:help ins-completion`
  726. --
  727. -- No, but seriously. Please read `:help ins-completion`, it is really good!
  728. --
  729. -- All presets have the following mappings:
  730. -- <tab>/<s-tab>: move to right/left of your snippet expansion
  731. -- <c-space>: Open menu or open docs if already open
  732. -- <c-n>/<c-p> or <up>/<down>: Select next/previous item
  733. -- <c-e>: Hide menu
  734. -- <c-k>: Toggle signature help
  735. --
  736. -- See :h blink-cmp-config-keymap for defining your own keymap
  737. preset = 'default',
  738. -- For more advanced Luasnip keymaps (e.g. selecting choice nodes, expansion) see:
  739. -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#keymaps
  740. },
  741. appearance = {
  742. -- 'mono' (default) for 'Nerd Font Mono' or 'normal' for 'Nerd Font'
  743. -- Adjusts spacing to ensure icons are aligned
  744. nerd_font_variant = 'mono',
  745. },
  746. completion = {
  747. -- By default, you may press `<c-space>` to show the documentation.
  748. -- Optionally, set `auto_show = true` to show the documentation after a delay.
  749. documentation = { auto_show = false, auto_show_delay_ms = 500 },
  750. },
  751. sources = {
  752. default = { 'lsp', 'path', 'snippets', 'lazydev' },
  753. providers = {
  754. lazydev = { module = 'lazydev.integrations.blink', score_offset = 100 },
  755. },
  756. },
  757. snippets = { preset = 'luasnip' },
  758. -- Blink.cmp includes an optional, recommended rust fuzzy matcher,
  759. -- which automatically downloads a prebuilt binary when enabled.
  760. --
  761. -- By default, we use the Lua implementation instead, but you may enable
  762. -- the rust implementation via `'prefer_rust_with_warning'`
  763. --
  764. -- See :h blink-cmp-config-fuzzy for more information
  765. fuzzy = { implementation = 'lua' },
  766. -- Shows a signature help window while you type arguments for a function
  767. signature = { enabled = true },
  768. },
  769. },
  770. { -- You can easily change to a different colorscheme.
  771. -- Change the name of the colorscheme plugin below, and then
  772. -- change the command in the config to whatever the name of that colorscheme is.
  773. --
  774. -- If you want to see what colorschemes are already installed, you can use `:Telescope colorscheme`.
  775. 'folke/tokyonight.nvim',
  776. priority = 1000, -- Make sure to load this before all the other start plugins.
  777. config = function()
  778. ---@diagnostic disable-next-line: missing-fields
  779. require('tokyonight').setup {
  780. styles = {
  781. comments = { italic = false }, -- Disable italics in comments
  782. },
  783. }
  784. -- Load the colorscheme here.
  785. -- Like many other themes, this one has different styles, and you could load
  786. -- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'.
  787. vim.cmd.colorscheme 'tokyonight-night'
  788. end,
  789. },
  790. -- Highlight todo, notes, etc in comments
  791. { 'folke/todo-comments.nvim', event = 'VimEnter', dependencies = { 'nvim-lua/plenary.nvim' }, opts = { signs = false } },
  792. { -- Collection of various small independent plugins/modules
  793. 'echasnovski/mini.nvim',
  794. config = function()
  795. -- Better Around/Inside textobjects
  796. --
  797. -- Examples:
  798. -- - va) - [V]isually select [A]round [)]paren
  799. -- - yinq - [Y]ank [I]nside [N]ext [Q]uote
  800. -- - ci' - [C]hange [I]nside [']quote
  801. require('mini.ai').setup { n_lines = 500 }
  802. -- Add/delete/replace surroundings (brackets, quotes, etc.)
  803. --
  804. -- - saiw) - [S]urround [A]dd [I]nner [W]ord [)]Paren
  805. -- - sd' - [S]urround [D]elete [']quotes
  806. -- - sr)' - [S]urround [R]eplace [)] [']
  807. require('mini.surround').setup()
  808. -- Simple and easy statusline.
  809. -- You could remove this setup call if you don't like it,
  810. -- and try some other statusline plugin
  811. local statusline = require 'mini.statusline'
  812. -- set use_icons to true if you have a Nerd Font
  813. statusline.setup { use_icons = vim.g.have_nerd_font }
  814. -- You can configure sections in the statusline by overriding their
  815. -- default behavior. For example, here we set the section for
  816. -- cursor location to LINE:COLUMN
  817. ---@diagnostic disable-next-line: duplicate-set-field
  818. statusline.section_location = function()
  819. return '%2l:%-2v'
  820. end
  821. -- ... and there is more!
  822. -- Check out: https://github.com/echasnovski/mini.nvim
  823. end,
  824. },
  825. { -- Highlight, edit, and navigate code
  826. 'nvim-treesitter/nvim-treesitter',
  827. build = ':TSUpdate',
  828. main = 'nvim-treesitter.configs', -- Sets main module to use for opts
  829. -- [[ Configure Treesitter ]] See `:help nvim-treesitter`
  830. opts = {
  831. ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
  832. -- Autoinstall languages that are not installed
  833. auto_install = true,
  834. highlight = {
  835. enable = true,
  836. -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
  837. -- If you are experiencing weird indenting issues, add the language to
  838. -- the list of additional_vim_regex_highlighting and disabled languages for indent.
  839. additional_vim_regex_highlighting = { 'ruby' },
  840. },
  841. indent = { enable = true, disable = { 'ruby' } },
  842. },
  843. -- There are additional nvim-treesitter modules that you can use to interact
  844. -- with nvim-treesitter. You should go explore a few and see what interests you:
  845. --
  846. -- - Incremental selection: Included, see `:help nvim-treesitter-incremental-selection-mod`
  847. -- - Show your current context: https://github.com/nvim-treesitter/nvim-treesitter-context
  848. -- - Treesitter + textobjects: https://github.com/nvim-treesitter/nvim-treesitter-textobjects
  849. },
  850. -- The following comments only work if you have downloaded the kickstart repo, not just copy pasted the
  851. -- init.lua. If you want these files, they are in the repository, so you can just download them and
  852. -- place them in the correct locations.
  853. -- NOTE: Next step on your Neovim journey: Add/Configure additional plugins for Kickstart
  854. --
  855. -- Here are some example plugins that I've included in the Kickstart repository.
  856. -- Uncomment any of the lines below to enable them (you will need to restart nvim).
  857. --
  858. -- require 'kickstart.plugins.debug',
  859. -- require 'kickstart.plugins.indent_line',
  860. -- require 'kickstart.plugins.lint',
  861. -- require 'kickstart.plugins.autopairs',
  862. -- require 'kickstart.plugins.neo-tree',
  863. -- require 'kickstart.plugins.gitsigns', -- adds gitsigns recommend keymaps
  864. -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  865. -- This is the easiest way to modularize your config.
  866. --
  867. -- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going.
  868. -- { import = 'custom.plugins' },
  869. --
  870. -- For additional information with loading, sourcing and examples see `:help lazy.nvim-🔌-plugin-spec`
  871. -- Or use telescope!
  872. -- In normal mode type `<space>sh` then write `lazy.nvim-plugin`
  873. -- you can continue same window with `<space>sr` which resumes last telescope search
  874. }, {
  875. ui = {
  876. -- If you are using a Nerd Font: set icons to an empty table which will use the
  877. -- default lazy.nvim defined Nerd Font icons, otherwise define a unicode icons table
  878. icons = vim.g.have_nerd_font and {} or {
  879. cmd = '⌘',
  880. config = '🛠',
  881. event = '📅',
  882. ft = '📂',
  883. init = '⚙',
  884. keys = '🗝',
  885. plugin = '🔌',
  886. runtime = '💻',
  887. require = '🌙',
  888. source = '📄',
  889. start = '🚀',
  890. task = '📌',
  891. lazy = '💤 ',
  892. },
  893. },
  894. })
  895. -- The line beneath this is called `modeline`. See `:help modeline`
  896. -- vim: ts=2 sts=2 sw=2 et