robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
9.2 KiB311 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, pending = t.describe, t.it, t.before_each, t.pending
5local eq, neq, call = t.eq, t.neq, n.call
6local eval, feed, clear = n.eval, n.feed, n.clear
7local command, insert, expect = n.command, n.insert, n.expect
8local feed_command = n.feed_command
9local curwin = n.api.nvim_get_current_win
10
11describe("'langmap'", function()
12  before_each(function()
13    clear()
14    insert('iii www')
15    command('set langmap=iw,wi')
16    feed('gg0')
17  end)
18
19  it('converts keys in normal mode', function()
20    feed('ix')
21    expect('iii ww')
22    feed('whello<esc>')
23    expect('iii helloww')
24  end)
25  it('gives characters that are mapped by :nmap.', function()
26    command('map i 0x')
27    feed('w')
28    expect('ii www')
29  end)
30  describe("'langnoremap' option.", function()
31    before_each(function()
32      command('nmapclear')
33    end)
34    it("'langnoremap' is by default ON", function()
35      eq(1, eval('&langnoremap'))
36    end)
37    it("Results of maps are not converted when 'langnoremap' ON.", function()
38      command('nmap x i')
39      feed('xdl<esc>')
40      expect('dliii www')
41    end)
42    it('applies when deciding whether to map recursively', function()
43      command('nmap l i')
44      command('nmap w j')
45      feed('ll')
46      expect('liii www')
47    end)
48    it("does not stop applying 'langmap' on first character of a mapping", function()
49      command('1t1')
50      command('1t1')
51      command('goto 1')
52      command('nmap w j')
53      feed('iiahello')
54      expect([[
55      iii www
56      iii www
57      ihelloii www]])
58    end)
59    it("Results of maps are converted when 'langnoremap' OFF.", function()
60      command('set nolangnoremap')
61      command('nmap x i')
62      feed('xdl<esc>')
63      expect('iii ww')
64    end)
65  end)
66  -- e.g. CTRL-W_j  ,  mj , 'j and "jp
67  it('conversions are applied to keys in middle of command', function()
68    -- Works in middle of window command
69    feed('<C-w>s')
70    local origwin = curwin()
71    feed('<C-w>i')
72    neq(origwin, curwin())
73    -- Works when setting a mark
74    feed('yy3p3gg0mwgg0mi')
75    eq({ 0, 3, 1, 0 }, call('getpos', "'i"))
76    eq({ 0, 1, 1, 0 }, call('getpos', "'w"))
77    feed('3dd')
78    -- Works when moving to a mark
79    feed("'i")
80    eq({ 0, 1, 1, 0 }, call('getpos', '.'))
81    -- Works when selecting a register
82    feed('qillqqwhhq')
83    eq('hh', eval('@i'))
84    eq('ll', eval('@w'))
85    feed('a<C-r>i<esc>')
86    expect('illii www')
87    feed('"ip')
88    expect('illllii www')
89    -- Works with i_CTRL-O
90    feed('0a<C-O>ihi<esc>')
91    expect('illllii hiwww')
92  end)
93
94  describe('exceptions', function()
95    -- All "command characters" that 'langmap' does not apply to.
96    -- These tests consist of those places where some subset of ASCII
97    -- characters define certain commands, yet 'langmap' is not applied to
98    -- them.
99    -- n.b. I think these shouldn't be exceptions.
100    it(':s///c confirmation', function()
101      command('set langmap=yn,ny')
102      feed('qa')
103      feed_command('s/i/w/gc')
104      feed('yynq')
105      expect('wwi www')
106      feed('u@a')
107      expect('wwi www')
108      eq(':s/i/w/gc\ryyn', eval('@a'))
109    end)
110    it('insert-mode CTRL-G', function()
111      command('set langmap=jk,kj')
112      command('d')
113      insert([[
114      hello
115      hello
116      hello]])
117      expect([[
118      hello
119      hello
120      hello]])
121      feed('qa')
122      feed('gg3|ahello<C-G>jx<esc>')
123      feed('q')
124      expect([[
125      helhellolo
126      helxlo
127      hello]])
128      eq('gg3|ahellojx', eval('@a'))
129    end)
130    it('command-line CTRL-\\', function()
131      command('set langmap=en,ne')
132      feed(':<C-\\>e\'hello\'\r<C-B>put ="<C-E>"<CR>')
133      expect([[
134      iii www
135      hello]])
136    end)
137    it('command-line CTRL-R', function()
138      n.source([[
139        let i_value = 0
140        let j_value = 0
141        call setreg('i', 'i_value')
142        call setreg('j', 'j_value')
143        set langmap=ij,ji
144      ]])
145      feed(':let <C-R>i=1<CR>')
146      eq(1, eval('i_value'))
147      eq(0, eval('j_value'))
148    end)
149    -- it('-- More -- prompt', function()
150    --   -- The 'b' 'j' 'd' 'f' commands at the -- More -- prompt
151    -- end)
152    it('ask yes/no after backwards range', function()
153      command('set langmap=yn,ny')
154      feed('dd')
155      insert([[
156      hello
157      there
158      these
159      are
160      some
161      lines
162      ]])
163      feed_command('4,2d')
164      feed('n')
165      expect([[
166      hello
167      there
168      these
169      are
170      some
171      lines
172      ]])
173    end)
174    it('prompt for number', function()
175      command('set langmap=12,21')
176      n.source([[
177        let gotten_one = 0
178        function Map()
179          let answer = inputlist(['a', '1.', '2.', '3.'])
180          if answer == 1
181            let g:gotten_one = 1
182          endif
183        endfunction
184        nnoremap x :call Map()<CR>
185      ]])
186      feed('x1<CR>')
187      eq(1, eval('gotten_one'))
188      command('let g:gotten_one = 0')
189      feed_command('call Map()')
190      feed('1<CR>')
191      eq(1, eval('gotten_one'))
192    end)
193  end)
194  it('conversions are not applied during setreg()', function()
195    call('setreg', 'i', 'ww')
196    eq('ww', eval('@i'))
197  end)
198  it('conversions not applied in insert mode', function()
199    feed('aiiiwww')
200    expect('iiiiwwwii www')
201  end)
202  it('conversions not applied in search mode', function()
203    feed('/iii<cr>x')
204    expect('ii www')
205  end)
206  it('conversions not applied in cmdline mode', function()
207    feed(':call append(1, "iii")<cr>')
208    expect([[
209    iii www
210    iii]])
211  end)
212
213  local function testrecording(command_string, expect_string, setup_function, expect_macro)
214    if setup_function then
215      setup_function()
216    end
217    feed('qa' .. command_string .. 'q')
218    expect(expect_string)
219    eq(expect_macro or n.fn.nvim_replace_termcodes(command_string, true, true, true), eval('@a'))
220    if setup_function then
221      setup_function()
222    end
223    -- n.b. may need nvim_replace_termcodes() here.
224    feed('@a')
225    expect(expect_string)
226  end
227
228  local function local_setup()
229    -- Can't use `insert` as it uses `i` and we've swapped the meaning of that
230    -- with the `langmap` setting.
231    command('%d')
232    command("put ='hello'")
233    command('1d')
234  end
235
236  it('does not affect recording special keys', function()
237    testrecording('A<BS><esc>', 'hell', local_setup)
238    testrecording('>><lt><lt>', 'hello', local_setup)
239    command('nnoremap \\ x')
240    testrecording('\\', 'ello', local_setup)
241    testrecording('A<C-V><BS><esc>', 'hello<BS>', local_setup)
242  end)
243  pending('Translates modified keys correctly', function()
244    command('nnoremap <M-i> x')
245    command('nnoremap <M-w> l')
246    testrecording('<M-w>', 'ello', local_setup)
247    testrecording('<M-i>x', 'hllo', local_setup)
248  end)
249  it('handles multibyte characters', function()
250    command('set langmap=ïx,δd,ςw,λl')
251    feed('ïλδς')
252    expect('iwww')
253  end)
254  it('handles multibyte mappings', function()
255    command('set langmap=ςw,λl,…ö,ν],μä')
256    command('nnoremap äö x')
257    command('nnoremap ää i…μpäike<esc>')
258    command('nnoremap ]] r')
259    command('nnoremap ï ie<esc>')
260    feed('λμ…')
261    expect('ii www')
262    feed('μμ')
263    expect('i…μpäikei www')
264    feed('lννn')
265    expect('i…μpäiken www')
266    feed('lï')
267    expect('i…μpäikene www')
268  end)
269  pending('handles multibyte characters in a macro', function()
270    command('set langmap=ïx')
271    testrecording('ï', 'ello', local_setup)
272    -- The test below checks that what's recorded is correct.
273    -- It doesn't check the behaviour, as in order to cause some behaviour we
274    -- need to map the multi-byte character, and there is a known bug
275    -- preventing this from working (see the test below).
276    command('set langmap=xï')
277    testrecording('x', 'hello', local_setup)
278  end)
279  pending('handles multibyte mappings in a macro', function()
280    -- See this vim issue for the problem, may as well add a test.
281    -- https://github.com/vim/vim/issues/297
282    command('set langmap=ïx')
283    command('nnoremap x diw')
284    testrecording('ï', '', local_setup)
285    command('set nolangnoremap')
286    command('set langmap=xï')
287    command('nnoremap ï ix<esc>')
288    testrecording('x', 'xhello', local_setup)
289  end)
290  it('does not crash when mapping char < 256 to char >= 256', function()
291    -- Note: this warning should be removed when the pending tests above are fixed.
292    local wmsg = table.concat({
293      "'langmap': Mapping from e to ε will not work properly",
294      "'langmap': Mapping from ü to μ will not work properly",
295    }, '\n')
296    -- ä ë ï ö ü ÿ < 256, β γ ε μ >= 256
297    command('set langmap=iw,wi,äë,ëä,ïx,xï,βγ,γβ,eε,εe,üμ,μü,ÿy,yÿ')
298    eq(wmsg, n.exec_capture('messages'))
299    command('messages clear | set langmap=iwäëïxβγeεüμÿy;wiëäxïγβεeμüyÿ')
300    eq(wmsg, n.exec_capture('messages'))
301  end)
302  -- This test is to ensure the behaviour doesn't change from what's already
303  -- around. I (hardenedapple) personally think this behaviour should be
304  -- changed.
305  it('treats control modified keys as characters', function()
306    command('nnoremap <C-w> iw<esc>')
307    command('nnoremap <C-i> ii<esc>')
308    testrecording('<C-w>', 'whello', local_setup, eval([["\<*C-w>"]]))
309    testrecording('<C-i>', 'ihello', local_setup, eval([["\<*C-i>"]]))
310  end)
311end)