plugins.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. -- add extra filetypes for plenary
  2. require('plenary.filetype').add_table({
  3. extension = {
  4. ['elv'] = [[elvish]]
  5. }
  6. })
  7. -- add extra builtin filetypes
  8. vim.filetype.add({
  9. pattern = {
  10. ['.*%.ts$'] = 'typescript'
  11. },
  12. })
  13. -- helpers
  14. local function resolve_git_path(buf_id)
  15. local bufname = vim.api.nvim_buf_get_name(buf_id)
  16. if not vim.startswith(bufname, "fugitive://") then
  17. return false
  18. end
  19. local parsed = vim.fn.FugitiveParse(bufname)
  20. local resolved_path = parsed[1]
  21. local repo = parsed[2]
  22. if resolved_path == "" then
  23. return false
  24. end
  25. local parts = vim.split(resolved_path, ':')
  26. local commit = parts[1]
  27. local path = parts[2]
  28. return {
  29. repo = repo,
  30. commit = commit,
  31. path = path,
  32. }
  33. end
  34. -- file/buffer/etc picker
  35. local telescope_actions = require('telescope.actions')
  36. local action_state = require('telescope.actions.state')
  37. local actions = {}
  38. actions.git_show_commit = function(prompt_bufnr)
  39. local selection = action_state.get_selected_entry()
  40. local path = vim.fn.FugitiveFind(selection.value)
  41. telescope_actions.close(prompt_bufnr)
  42. vim.cmd.edit(path)
  43. end
  44. local commit_mappings = {
  45. i = {
  46. ['<CR>'] = actions.git_show_commit,
  47. ['<C-r>c'] = telescope_actions.git_checkout,
  48. }
  49. }
  50. require('telescope').setup({
  51. defaults = {
  52. mappings = {
  53. i = {
  54. ['jj'] = 'close',
  55. },
  56. },
  57. layout_config = {
  58. prompt_position = 'top',
  59. },
  60. sorting_strategy = 'ascending',
  61. -- use filename as preview window title
  62. dynamic_preview_title = true,
  63. path_display = {
  64. -- shorten directory names of everything but the last 3 parts
  65. -- foo/bar/baz/file.txt -> f/boo/bar/file.txt
  66. shorten = { len = 1, exclude = { -3, -2, -1 } },
  67. -- truncate the beginning of the file name if wider than the window
  68. truncate = true,
  69. },
  70. preview = {
  71. -- don't preview files larger than 1MB
  72. filesize_limit = 1,
  73. timeout = 500,
  74. },
  75. vimgrep_arguments = {
  76. -- defaults
  77. "rg",
  78. "--color=never",
  79. "--no-heading",
  80. "--with-filename",
  81. "--line-number",
  82. "--column",
  83. "--smart-case",
  84. -- search "hidden" files except git folder
  85. "--hidden",
  86. "--iglob=!.git"
  87. },
  88. -- ignore things we're likely not to edit
  89. file_ignore_patterns = {
  90. "%.zip$",
  91. "%.yarn/releases/",
  92. "%.yarn/plugins/"
  93. },
  94. -- picker history
  95. cache_picker = {
  96. num_pickers = 10,
  97. },
  98. },
  99. pickers = {
  100. buffers = {
  101. sort_lastused = true,
  102. sort_mru = true,
  103. mappings = {
  104. i = {
  105. ['<C-k>'] = 'delete_buffer'
  106. },
  107. },
  108. },
  109. find_files = {
  110. find_command = { "fd", "--type", "f", "--strip-cwd-prefix" }
  111. },
  112. git_commits = {
  113. mappings = commit_mappings,
  114. },
  115. git_bcommits = {
  116. mappings = commit_mappings,
  117. },
  118. },
  119. })
  120. -- use native sorter for better performance
  121. require('telescope').load_extension('fzf')
  122. local telescope_builtin = require('telescope.builtin')
  123. local telescope_pickers = require('telescope.pickers')
  124. local telescope_finders = require('telescope.finders')
  125. local telescope_previewers = require('telescope.previewers')
  126. local telescope_putils = require('telescope.previewers.utils')
  127. local telescope_conf = require('telescope.config').values
  128. -- arbitrary git log picker
  129. vim.api.nvim_create_user_command('GLg', function(opts)
  130. local git_command = { "git", "log", "--pretty=oneline", "--abbrev-commit" }
  131. vim.list_extend(git_command, opts.fargs)
  132. telescope_builtin.git_commits({ git_command = git_command })
  133. end, { nargs = '*', complete = vim.fn['fugitive#LogComplete'] })
  134. -- custom picker to fallback to files if no git
  135. _G.project_files = function()
  136. local ok = pcall(telescope_builtin.git_files, { show_untracked = true })
  137. if not ok then telescope_builtin.find_files({}) end
  138. end
  139. -- custom picker for files within a commit
  140. _G.commit_files = function(opts)
  141. local resolved = resolve_git_path(0)
  142. if not resolved then
  143. vim.print("current file is not a fugitive path")
  144. return
  145. end
  146. opts = opts or {}
  147. telescope_pickers.new(opts, {
  148. prompt_title = resolved.commit,
  149. finder = telescope_finders.new_oneshot_job({ "git", "ls-tree", "--name-only", "-r", resolved.commit }, {
  150. entry_maker = function(entry)
  151. local path = string.format("fugitive://%s//%s/%s", resolved.repo, resolved.commit, entry)
  152. return {
  153. path = path,
  154. value = entry,
  155. display = entry,
  156. ordinal = entry,
  157. }
  158. end,
  159. }),
  160. sorter = telescope_conf.file_sorter(opts),
  161. -- the builtin previewer has fancy async loading which doesn't work for
  162. -- fugitive paths so we have to define our own
  163. previewer = telescope_previewers.new_buffer_previewer({
  164. title = function(self)
  165. return 'Commit Files'
  166. end,
  167. dyn_title = function(self, entry)
  168. return entry.value
  169. end,
  170. define_preview = function(self, entry, status)
  171. -- the builtin previewer does more things like using mime type
  172. -- fallbacks as well as binary file detection which ours doesn't do
  173. local ft = telescope_putils.filetype_detect(entry.value)
  174. vim.api.nvim_buf_call(self.state.bufnr, function()
  175. vim.cmd('Gread ' .. entry.path)
  176. telescope_putils.highlighter(self.state.bufnr, ft, opts)
  177. end)
  178. end,
  179. }),
  180. }):find()
  181. end
  182. -- shows added/removed/changed lines
  183. local MiniDiff = require('mini.diff')
  184. MiniDiff.setup({
  185. view = {
  186. style = 'sign',
  187. signs = {
  188. delete = '_',
  189. },
  190. },
  191. source = {
  192. MiniDiff.gen_source.git(),
  193. -- handle fugitive paths
  194. {
  195. name = "fugitive",
  196. attach = function(buf_id)
  197. local resolved = resolve_git_path(buf_id)
  198. if not resolved or resolved.path == "" then
  199. return false
  200. end
  201. local source = vim.fn.FugitiveFind(string.format("%s~1:%s", resolved.commit, resolved.path))
  202. local text = vim.fn['fugitive#readfile'](source)
  203. MiniDiff.set_ref_text(buf_id, text)
  204. end
  205. }
  206. }
  207. })
  208. require('mini.statusline').setup({
  209. content = {
  210. -- copy-pasted from default, we just want to remove the icon
  211. active = function()
  212. local mode, mode_hl = MiniStatusline.section_mode({ trunc_width = 120 })
  213. local git = MiniStatusline.section_git({ trunc_width = 75, icon = '' })
  214. local diagnostics = MiniStatusline.section_diagnostics({ trunc_width = 75, icon = '' })
  215. local filename = MiniStatusline.section_filename({ trunc_width = 140 })
  216. local fileinfo = MiniStatusline.section_fileinfo({ trunc_width = 120 })
  217. local location = MiniStatusline.section_location({ trunc_width = 75 })
  218. return MiniStatusline.combine_groups({
  219. { hl = mode_hl, strings = { mode } },
  220. { hl = 'MiniStatuslineDevinfo', strings = { git, diagnostics } },
  221. '%<', -- Mark general truncate point
  222. { hl = 'MiniStatuslineFilename', strings = { filename } },
  223. '%=', -- End left alignment
  224. { hl = 'MiniStatuslineFileinfo', strings = { fileinfo } },
  225. { hl = mode_hl, strings = { location } },
  226. })
  227. end
  228. },
  229. })
  230. -- delete buffer while preserving layout
  231. require('mini.bufremove').setup()
  232. -- shows a line indicating the current indentation scope
  233. require('mini.indentscope').setup()
  234. -- comment actions
  235. require('mini.comment').setup()
  236. -- surround actions
  237. require('mini.surround').setup()
  238. local spec_treesitter = require('mini.ai').gen_spec.treesitter
  239. require('mini.ai').setup({
  240. custom_textobjects = {
  241. [','] = spec_treesitter({
  242. a = '@parameter.outer',
  243. i = '@parameter.inner',
  244. }),
  245. },
  246. });
  247. -- align actions
  248. require('mini.align').setup()
  249. -- repeatable f/t
  250. require('mini.jump').setup({
  251. mappings = {
  252. repeat_jump = '',
  253. },
  254. delay = {
  255. highlight = 10000000,
  256. },
  257. })
  258. -- autopair brackets
  259. require('mini.pairs').setup({
  260. mappings = {
  261. -- default config includes (, [ and {
  262. -- autopair <> if preceded by a character, otherwise it might be a regular
  263. -- comparison operation
  264. ['<'] = { action = 'open', pair = '<>', neigh_pattern = '%w.' },
  265. ['>'] = { action = 'close', pair = '<>' },
  266. },
  267. })
  268. -- notifications
  269. require('mini.notify').setup()
  270. vim.notify = require('mini.notify').make_notify()
  271. -- file explorer
  272. require('mini.files').setup({
  273. content = {
  274. -- remove icons
  275. prefix = function() end,
  276. }
  277. })
  278. -- rest client
  279. require('kulala').setup({
  280. global_keymaps = true,
  281. global_keymaps_prefix = '<leader>r'
  282. })
  283. -- Use Treesitter for syntax highlighting
  284. require('nvim-treesitter.configs').setup({
  285. highlight = {
  286. enable = true,
  287. },
  288. indent = {
  289. enable = true,
  290. },
  291. incremental_selection = {
  292. enable = true,
  293. keymaps = {
  294. init_selection = "]t",
  295. node_incremental = "]t",
  296. node_decremental = "[t",
  297. },
  298. },
  299. textobjects = {
  300. swap = {
  301. enable = true,
  302. swap_next = {
  303. ['>,'] = '@parameter.inner',
  304. },
  305. swap_previous = {
  306. ['<,'] = '@parameter.inner',
  307. },
  308. },
  309. },
  310. })
  311. local tsj_utils = require('treesj.langs.utils')
  312. -- Treesitter-aware split/join
  313. require('treesj').setup({
  314. use_default_keymaps = false,
  315. })
  316. -- Treesitter context
  317. require('treesitter-context').setup({
  318. enable = true,
  319. multiline_threshold = 5,
  320. })
  321. -- completion
  322. require('blink.cmp').setup({
  323. cmdline = {
  324. enabled = false,
  325. },
  326. completion = {
  327. documentation = {
  328. auto_show = true,
  329. auto_show_delay_ms = 500,
  330. },
  331. trigger = {
  332. prefetch_on_insert = false,
  333. show_on_keyword = false,
  334. show_on_trigger_character = false,
  335. },
  336. menu = {
  337. draw = {
  338. columns = {
  339. { "label", "label_description", gap = 1 },
  340. { "kind" },
  341. },
  342. },
  343. },
  344. },
  345. keymap = {
  346. ['<Enter>'] = { 'select_and_accept', 'fallback' },
  347. ['<C-u>'] = { 'scroll_documentation_up', 'fallback_to_mappings' },
  348. ['<C-d>'] = { 'scroll_documentation_down', 'fallback_to_mappings' },
  349. },
  350. })
  351. -- typescript-vim compiler options
  352. vim.g.typescript_compiler_options = '--incremental --noEmit'