editing.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local MiniAi = require('mini.ai')
  2. local MiniAlign = require('mini.align')
  3. local MiniComment = require('mini.comment')
  4. local MiniPairs = require('mini.pairs')
  5. local MiniSurround = require('mini.surround')
  6. -- comment actions
  7. MiniComment.setup()
  8. -- keymap: overrides default vim gc / gcc
  9. -- surround actions
  10. MiniSurround.setup({
  11. custom_surroundings = {
  12. -- js template string
  13. ['$'] = {
  14. output = {
  15. left = '`${',
  16. right = '}`',
  17. },
  18. },
  19. },
  20. })
  21. -- keymap:
  22. -- sa (surround add)
  23. -- sr (surround replace)
  24. -- sd (surround delete)
  25. local spec_treesitter = require('mini.ai').gen_spec.treesitter
  26. MiniAi.setup({
  27. -- only consider the current location
  28. search_method = 'cover',
  29. custom_textobjects = {
  30. ['.'] = spec_treesitter({
  31. a = '@call.outer',
  32. i = '@call.inner',
  33. }),
  34. [','] = spec_treesitter({
  35. a = '@parameter.outer',
  36. i = '@parameter.inner',
  37. }),
  38. },
  39. });
  40. -- align actions
  41. MiniAlign.setup()
  42. -- keymap: ga / gA
  43. -- Treesitter-aware split/join
  44. require('treesj').setup({
  45. use_default_keymaps = false,
  46. })
  47. vim.keymap.set('n', '<Leader>s', '<cmd>TSJSplit<CR>')
  48. vim.keymap.set('n', '<Leader>j', '<cmd>TSJJoin<CR>')
  49. -- autopair brackets
  50. MiniPairs.setup({
  51. mappings = {
  52. -- default config includes (, [ and {
  53. -- autopair <> if preceded by a character, otherwise it might be a regular
  54. -- comparison operation
  55. ['<'] = { action = 'open', pair = '<>', neigh_pattern = '%w.' },
  56. ['>'] = { action = 'close', pair = '<>' },
  57. },
  58. })
  59. local function toggle_end_char(char)
  60. local cursor = vim.api.nvim_win_get_cursor(0)
  61. local row = cursor[1] - 1
  62. local end_char = vim.api.nvim_buf_get_text(0, row, -2, row, -1, {})[1]
  63. if end_char == char then
  64. vim.api.nvim_buf_set_text(0, row, -2, row, -1, {})
  65. else
  66. vim.api.nvim_buf_set_text(0, row, -1, row, -1, { char })
  67. end
  68. end
  69. -- add ,/; to end of line
  70. vim.keymap.set('i', '<C-,>', function() toggle_end_char(',') end)
  71. vim.keymap.set('i', '<C-;>', function() toggle_end_char(';') end)