editing.lua 2.4 KB

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