init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. -- disable netrw for nvim-tree plugin
  2. vim.g.loaded_netrw = 1
  3. vim.g.loaded_netrwPlugin = 1
  4. -- configure neon theme
  5. vim.g.neon_style = "doom"
  6. vim.g.mapleader = ' '
  7. vim.g.maplocalleader = ' '
  8. -- prepend ghcup to path
  9. vim.env.PATH = '/home/dora/.cabal/bin:/home/dora/.ghcup/bin:' .. vim.env.PATH
  10. -- package manager
  11. local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
  12. if not vim.loop.fs_stat(lazypath) then
  13. vim.fn.system({
  14. "git",
  15. "clone",
  16. "--filter=blob:none",
  17. "https://github.com/folke/lazy.nvim.git",
  18. "--branch=stable", -- latest stable release
  19. lazypath,
  20. })
  21. end
  22. vim.opt.rtp:prepend(lazypath)
  23. require("lazy").setup({
  24. "nvim-tree/nvim-tree.lua",
  25. "nvim-tree/nvim-web-devicons",
  26. {
  27. "rafamadriz/neon",
  28. config = function ()
  29. vim.cmd.colorscheme("neon")
  30. end,
  31. },
  32. {
  33. "nvim-lualine/lualine.nvim",
  34. opts = {
  35. options = {
  36. theme = "neon",
  37. },
  38. },
  39. },
  40. -- LSP
  41. {
  42. "neovim/nvim-lspconfig",
  43. dependencies = {
  44. {'williamboman/mason.nvim', config = true},
  45. 'williamboman/mason-lspconfig.nvim',
  46. { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} },
  47. 'folke/neodev.nvim',
  48. }
  49. },
  50. -- Show definitions and referenes in a floating window
  51. 'rmagatti/goto-preview',
  52. -- Autocompletion
  53. {
  54. 'hrsh7th/nvim-cmp',
  55. dependencies = {
  56. -- Snippet Engine & its associated nvim-cmp source
  57. 'L3MON4D3/LuaSnip',
  58. 'saadparwaiz1/cmp_luasnip',
  59. -- Adds LSP completion capabilities
  60. 'hrsh7th/cmp-nvim-lsp',
  61. -- Adds a number of user-friendly snippets
  62. 'rafamadriz/friendly-snippets',
  63. },
  64. },
  65. -- Useful plugin to show you pending keybinds.
  66. { 'folke/which-key.nvim', opts = {} },
  67. -- "gc" to comment visual regions/lines
  68. { 'numToStr/Comment.nvim', opts = {} },
  69. -- Fuzzy Finder (files, lsp, etc)
  70. {
  71. 'nvim-telescope/telescope.nvim',
  72. branch = '0.1.x',
  73. dependencies = {
  74. 'nvim-lua/plenary.nvim',
  75. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  76. -- Only load if `make` is available. Make sure you have the system
  77. -- requirements installed.
  78. {
  79. 'nvim-telescope/telescope-fzf-native.nvim',
  80. -- NOTE: If you are having trouble with this installation,
  81. -- refer to the README for telescope-fzf-native for more instructions.
  82. build = 'make',
  83. cond = function()
  84. return vim.fn.executable 'make' == 1
  85. end,
  86. },
  87. },
  88. },
  89. -- Highlight, edit, and navigate code
  90. {
  91. 'nvim-treesitter/nvim-treesitter',
  92. dependencies = {
  93. 'nvim-treesitter/nvim-treesitter-textobjects',
  94. },
  95. build = ':TSUpdate',
  96. },
  97. {
  98. "gbprod/cutlass.nvim",
  99. opts = {
  100. cut_key = "<Leader>x",
  101. }
  102. }
  103. })
  104. -- copy to system clipboard by default
  105. vim.o.clipboard = "unnamedplus"
  106. vim.o.termguicolors = true
  107. vim.o.completeopt = "menuone,noselect"
  108. vim.opt.guifont = "Hack Nerd Font Mono:h12"
  109. vim.opt.number = true
  110. vim.opt.mouse = ""
  111. vim.opt.hlsearch = true
  112. vim.opt.tabstop = 2
  113. vim.opt.shiftwidth = 2
  114. require("nvim-tree").setup({
  115. view = {
  116. width = 30,
  117. },
  118. on_attach = function (bufnr)
  119. local api = require "nvim-tree.api"
  120. api.config.mappings.default_on_attach(bufnr)
  121. vim.keymap.set("n", ".", api.tree.change_root_to_node,
  122. {noremap=true, silent=true, buffer=bufnr, nowait=true})
  123. end,
  124. })
  125. -- unmap space
  126. vim.api.nvim_set_keymap('n', '<Space>', '', {noremap=true, silent=true})
  127. -- window operations with leader key
  128. vim.api.nvim_set_keymap("n", "<Leader>w", "<C-w>", {noremap=true, silent=true})
  129. -- custom nvim tree mappings
  130. vim.api.nvim_set_keymap("n", "<Leader>ft", ":NvimTreeToggle<CR>", {noremap=true, silent=true})
  131. vim.api.nvim_set_keymap("n", "<BS>", ":NvimTreeFocus<CR>", {noremap=true, silent=true})
  132. -- configure telescope
  133. -- See `:help telescope` and `:help telescope.setup()`
  134. require('telescope').setup {
  135. defaults = {
  136. mappings = {
  137. i = {
  138. ['<C-u>'] = false,
  139. ['<C-d>'] = false,
  140. },
  141. },
  142. },
  143. }
  144. -- Enable telescope fzf native, if installed
  145. pcall(require('telescope').load_extension, 'fzf')
  146. -- See `:help telescope.builtin`
  147. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  148. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  149. vim.keymap.set('n', '<leader>/', function()
  150. -- You can pass additional configuration to telescope to change theme, layout, etc.
  151. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  152. winblend = 10,
  153. previewer = false,
  154. })
  155. end, { desc = '[/] Fuzzily search in current buffer' })
  156. -- vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  157. vim.keymap.set('n', '<leader>ff', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  158. -- vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  159. -- vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  160. -- vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  161. -- vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  162. -- Configure Treesitter
  163. -- See `:help nvim-treesitter`
  164. require('nvim-treesitter.configs').setup {
  165. modules = {},
  166. -- Add languages to be installed here that you want installed for treesitter
  167. ensure_installed = {'haskell', 'c', 'cpp', 'go', 'glsl', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' },
  168. -- Install parsers synchronously (only applied to `ensure_installed`)
  169. sync_install = false,
  170. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  171. auto_install = true,
  172. -- List of parsers to ignore installing (or "all")
  173. ignore_install = {},
  174. highlight = { enable = true },
  175. indent = { enable = true },
  176. incremental_selection = {
  177. enable = true,
  178. keymaps = {
  179. init_selection = '<c-space>',
  180. node_incremental = '<c-space>',
  181. scope_incremental = '<c-s>',
  182. node_decremental = '<M-space>',
  183. },
  184. },
  185. textobjects = {
  186. select = {
  187. enable = true,
  188. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  189. keymaps = {
  190. -- You can use the capture groups defined in textobjects.scm
  191. ['aa'] = '@parameter.outer',
  192. ['ia'] = '@parameter.inner',
  193. ['af'] = '@function.outer',
  194. ['if'] = '@function.inner',
  195. ['ac'] = '@class.outer',
  196. ['ic'] = '@class.inner',
  197. },
  198. },
  199. move = {
  200. enable = true,
  201. set_jumps = true, -- whether to set jumps in the jumplist
  202. goto_next_start = {
  203. [']m'] = '@function.outer',
  204. [']]'] = '@class.outer',
  205. },
  206. goto_next_end = {
  207. [']M'] = '@function.outer',
  208. [']['] = '@class.outer',
  209. },
  210. goto_previous_start = {
  211. ['[m'] = '@function.outer',
  212. ['[['] = '@class.outer',
  213. },
  214. goto_previous_end = {
  215. ['[M'] = '@function.outer',
  216. ['[]'] = '@class.outer',
  217. },
  218. },
  219. swap = {
  220. enable = true,
  221. swap_next = {
  222. ['<leader>a'] = '@parameter.inner',
  223. },
  224. swap_previous = {
  225. ['<leader>A'] = '@parameter.inner',
  226. },
  227. },
  228. },
  229. }
  230. -- Diagnostic keymaps
  231. vim.keymap.set('n', '<Leader>dp', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
  232. vim.keymap.set('n', '<Leader>dn', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
  233. vim.keymap.set('n', '<leader>de', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
  234. vim.keymap.set('n', '<leader>dl', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
  235. require('goto-preview').setup {
  236. default_mappings = false,
  237. references = { -- Configure the telescope UI for slowing the references cycling window.
  238. telescope = require("telescope.themes").get_dropdown({ hide_preview = false })
  239. },
  240. }
  241. -- Configure LSP
  242. -- This function gets run when an LSP connects to a particular buffer.
  243. local on_attach = function(_, bufnr)
  244. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  245. -- to define small helper and utility functions so you don't have to repeat yourself
  246. -- many times.
  247. --
  248. -- In this case, we create a function that lets us more easily define mappings specific
  249. -- for LSP related items. It sets the mode, buffer and description for us each time.
  250. local nmap = function(keys, func, desc)
  251. if desc then
  252. desc = 'LSP: ' .. desc
  253. end
  254. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  255. end
  256. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  257. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  258. nmap('gd', require('goto-preview').goto_preview_definition, '[G]oto [D]efinition')
  259. nmap('gr', require('goto-preview').goto_preview_references, '[G]oto [R]eferences')
  260. nmap('gc', require('goto-preview').close_all_win, 'Close all preview windows')
  261. nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  262. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  263. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  264. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  265. -- See `:help K` for why this keymap
  266. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  267. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  268. -- Lesser used LSP functionality
  269. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  270. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  271. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  272. nmap('<leader>wl', function()
  273. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  274. end, '[W]orkspace [L]ist Folders')
  275. -- Create a command `:Format` local to the LSP buffer
  276. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  277. vim.lsp.buf.format()
  278. end, { desc = 'Format current buffer with LSP' })
  279. vim.keymap.set("v", "<leader>k", vim.lsp.buf.format, { remap = false })
  280. end
  281. -- Enable the following language servers
  282. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  283. --
  284. -- Add any additional override configuration in the following tables. They will be passed to
  285. -- the `settings` field of the server config. You must look up that documentation yourself.
  286. --
  287. -- If you want to override the default filetypes that your language server will attach to you can
  288. -- define the property 'filetypes' to the map in question.
  289. local servers = {
  290. clangd = {},
  291. gopls = {},
  292. pyright = {},
  293. rust_analyzer = {},
  294. hls = {},
  295. -- tsserver = {},
  296. -- html = { filetypes = { 'html', 'twig', 'hbs'} },
  297. lua_ls = {
  298. Lua = {
  299. workspace = { checkThirdParty = false },
  300. telemetry = { enable = false },
  301. },
  302. },
  303. }
  304. -- Setup neovim lua configuration
  305. require('neodev').setup()
  306. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  307. local capabilities = vim.lsp.protocol.make_client_capabilities()
  308. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  309. -- Ensure the servers above are installed
  310. local mason_lspconfig = require 'mason-lspconfig'
  311. mason_lspconfig.setup {
  312. ensure_installed = vim.tbl_keys(servers),
  313. }
  314. mason_lspconfig.setup_handlers {
  315. function(server_name)
  316. require('lspconfig')[server_name].setup {
  317. capabilities = capabilities,
  318. on_attach = on_attach,
  319. settings = servers[server_name],
  320. filetypes = (servers[server_name] or {}).filetypes,
  321. }
  322. end
  323. }
  324. -- Configure autocompletion nvim-cmp
  325. -- See `:help cmp`
  326. local cmp = require 'cmp'
  327. local luasnip = require 'luasnip'
  328. require('luasnip.loaders.from_vscode').lazy_load()
  329. luasnip.config.setup {}
  330. cmp.setup {
  331. snippet = {
  332. expand = function(args)
  333. luasnip.lsp_expand(args.body)
  334. end,
  335. },
  336. mapping = cmp.mapping.preset.insert {
  337. ['<C-n>'] = cmp.mapping.select_next_item(),
  338. ['<C-p>'] = cmp.mapping.select_prev_item(),
  339. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  340. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  341. ['<C-Space>'] = cmp.mapping.complete {},
  342. ['<CR>'] = cmp.mapping.confirm {
  343. behavior = cmp.ConfirmBehavior.Replace,
  344. select = true,
  345. },
  346. ['<Tab>'] = cmp.mapping(function(fallback)
  347. if cmp.visible() then
  348. cmp.select_next_item()
  349. elseif luasnip.expand_or_locally_jumpable() then
  350. luasnip.expand_or_jump()
  351. else
  352. fallback()
  353. end
  354. end, { 'i', 's' }),
  355. ['<S-Tab>'] = cmp.mapping(function(fallback)
  356. if cmp.visible() then
  357. cmp.select_prev_item()
  358. elseif luasnip.locally_jumpable(-1) then
  359. luasnip.jump(-1)
  360. else
  361. fallback()
  362. end
  363. end, { 'i', 's' }),
  364. },
  365. sources = {
  366. { name = 'nvim_lsp' },
  367. { name = 'luasnip' },
  368. },
  369. }
  370. -- Do not yank when pasting over selected text
  371. -- "x" - visual mode
  372. vim.keymap.set("x", "p", "P", { silent = true })