aboutsummaryrefslogtreecommitdiff
path: root/test/functional/editor/macro_spec.lua
blob: 680a70b78a88189b00af0972f69f5e1bc51405b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local eq = t.eq
local eval = n.eval
local feed = n.feed
local clear = n.clear
local expect = n.expect
local command = n.command
local fn = n.fn
local api = n.api
local insert = n.insert

describe('macros with default mappings', function()
  before_each(function()
    clear({ args_rm = { '--cmd' } })
  end)

  it('can be recorded and replayed', function()
    feed('qiahello<esc>q')
    expect('hello')
    eq('ahello', eval('@i'))
    feed('@i')
    expect('hellohello')
    eq('ahello', eval('@i'))
  end)

  it('applies maps', function()
    command('imap x l')
    command('nmap l a')
    feed('qilxxx<esc>q')
    expect('lll')
    eq('lxxx', eval('@i'))
    feed('@i')
    expect('llllll')
    eq('lxxx', eval('@i'))
  end)

  it('can be replayed with Q', function()
    insert [[
hello
hello
hello]]
    feed [[gg]]

    feed [[qqAFOO<esc>q]]
    expect [[
helloFOO
hello
hello]]

    feed [[Q]]
    expect [[
helloFOOFOO
hello
hello]]

    feed [[G3Q]]
    expect [[
helloFOOFOO
hello
helloFOOFOOFOO]]

    feed [[ggV3jQ]]
    expect [[
helloFOOFOOFOO
helloFOO
helloFOOFOOFOOFOO]]
  end)

  it('can be replayed with Q and @@', function()
    insert [[
hello
hello
hello]]
    feed [[gg]]

    feed [[qqAFOO<esc>q]]
    expect [[
helloFOO
hello
hello]]

    feed [[Q]]
    expect [[
helloFOOFOO
hello
hello]]

    feed [[G3@@]]
    expect [[
helloFOOFOO
hello
helloFOOFOOFOO]]

    feed [[ggV2j@@]]
    expect [[
helloFOOFOOFOO
helloFOO
helloFOOFOOFOOFOO]]
  end)

  it('can be replayed with @ in linewise Visual mode', function()
    insert [[
hello
hello
hello]]
    feed [[gg]]

    feed [[qqAFOO<esc>qu]]
    expect [[
hello
hello
hello]]

    feed [[qwA123<esc>qu]]
    expect [[
hello
hello
hello]]

    feed [[V3j@q]]
    expect [[
helloFOO
helloFOO
helloFOO]]

    feed [[ggVj@w]]
    expect [[
helloFOO123
helloFOO123
helloFOO]]
  end)

  it('can be recorded and replayed in Visual mode', function()
    insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
    feed('0vqifofRq')
    eq({ 0, 1, 7, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('Q')
    eq({ 0, 1, 19, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('Q')
    eq({ 0, 1, 27, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('@i')
    eq({ 0, 1, 43, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  end)

  it('can be recorded and replayed in Visual mode when ignorecase', function()
    command('set ignorecase')
    insert('foo BAR BAR foo BAR foo BAR BAR BAR foo BAR BAR')
    feed('0vqifofRq')
    eq({ 0, 1, 7, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('Q')
    eq({ 0, 1, 19, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('Q')
    eq({ 0, 1, 27, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
    feed('@i')
    eq({ 0, 1, 43, 0 }, fn.getpos('.'))
    eq({ 0, 1, 1, 0 }, fn.getpos('v'))
  end)

  it('can be replayed with @ in blockwise Visual mode', function()
    insert [[
hello
hello
hello]]
    feed [[gg]]

    feed [[qqAFOO<esc>qu]]
    expect [[
hello
hello
hello]]

    feed [[qwA123<esc>qu]]
    expect [[
hello
hello
hello]]

    feed [[0<C-v>3jl@q]]
    expect [[
heFOOllo
heFOOllo
heFOOllo]]

    feed [[gg0<C-v>j@w]]
    expect [[
h123eFOOllo
h123eFOOllo
heFOOllo]]
  end)
end)

describe('immediately after a macro has finished executing,', function()
  before_each(function()
    clear()
    command([[let @a = 'gg0']])
  end)

  describe('reg_executing() from RPC returns an empty string', function()
    it('if the macro does not end with a <Nop> mapping', function()
      feed('@a')
      eq('', fn.reg_executing())
    end)

    it('if the macro ends with a <Nop> mapping', function()
      command('nnoremap 0 <Nop>')
      feed('@a')
      eq('', fn.reg_executing())
    end)
  end)

  describe('characters from a mapping are not treated as a part of the macro #18015', function()
    before_each(function()
      command('nnoremap s qa')
    end)

    it('if the macro does not end with a <Nop> mapping', function()
      feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
      expect('')
      eq('', eval('@a'))
    end)

    it('if the macro ends with a <Nop> mapping', function()
      command('nnoremap 0 <Nop>')
      feed('@asq') -- "q" from "s" mapping should start recording a macro instead of being no-op
      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
      expect('')
      eq('', eval('@a'))
    end)
  end)
end)

describe('reg_recorded()', function()
  before_each(clear)
  it('returns the correct value', function()
    feed [[qqyyq]]
    eq('q', eval('reg_recorded()'))
  end)
end)