@@ -21,11 +21,17 @@ local function _call_command(keys)
2121end
2222
2323--- Add mappings for unittests.
24- local function _initialize_mappings ()
25- vim .keymap .set (" o" , " [" , " <Plug>(cursor-text-objects-up)" )
26- vim .keymap .set (" o" , " ]" , " <Plug>(cursor-text-objects-down)" )
27- vim .keymap .set (" x" , " [" , " <Plug>(cursor-text-objects-up)" )
28- vim .keymap .set (" x" , " ]" , " <Plug>(cursor-text-objects-down)" )
24+ ---
25+ --- @param up string ? The key (s ) to set as the " go the top to the cursor" .
26+ --- @param down string ? The key (s ) to set as the " go from the cursor down" .
27+ ---
28+ local function _initialize_mappings (up , down )
29+ up = up or " ["
30+ down = down or " ]"
31+ vim .keymap .set (" o" , up , " <Plug>(cursor-text-objects-up)" )
32+ vim .keymap .set (" o" , down , " <Plug>(cursor-text-objects-down)" )
33+ vim .keymap .set (" x" , up , " <Plug>(cursor-text-objects-up)" )
34+ vim .keymap .set (" x" , down , " <Plug>(cursor-text-objects-down)" )
2935end
3036
3137--- Create a new Vim buffer with `text` contents.
@@ -49,11 +55,18 @@ local function _make_buffer(text, file_type)
4955end
5056
5157--- Remove any the default mappings that were added from `_initialize_mappings`.
52- local function _revert_mappings ()
53- vim .keymap .del (" o" , " [" )
54- vim .keymap .del (" o" , " ]" )
55- vim .keymap .del (" x" , " [" )
56- vim .keymap .del (" x" , " ]" )
58+ ---
59+ --- @param up string ? The key (s ) to set as the " go the top to the cursor" .
60+ --- @param down string ? The key (s ) to set as the " go from the cursor down" .
61+ ---
62+ local function _revert_mappings (up , down )
63+ up = up or " ["
64+ down = down or " ]"
65+
66+ vim .keymap .del (" o" , up )
67+ vim .keymap .del (" o" , down )
68+ vim .keymap .del (" x" , up )
69+ vim .keymap .del (" x" , down )
5770end
5871
5972--- Make sure `input` becomes `expected` when `keys` are called.
@@ -130,6 +143,37 @@ describe("basic", function()
130143 end )
131144end )
132145
146+ describe (" custom mappings" , function ()
147+ before_each (function ()
148+ _initialize_mappings (" {" , " }" )
149+ end )
150+ after_each (function ()
151+ _revert_mappings (" {" , " }" )
152+ end )
153+
154+ it (" works with keys, not just [ / ]" , function ()
155+ _run_simple_test (
156+ { 2 , 0 },
157+ " c}ap" ,
158+ [[
159+ some text
160+ more text <-- NOTE: The cursor will be set here
161+ even more lines!
162+ still part of the paragraph
163+
164+ another paragraph
165+ with text in it
166+ ]] ,
167+ [[
168+ some text
169+
170+ another paragraph
171+ with text in it
172+ ]]
173+ )
174+ end )
175+ end )
176+
133177describe (" :help c" , function ()
134178 before_each (_initialize_mappings )
135179 after_each (_revert_mappings )
@@ -1235,6 +1279,111 @@ describe(":help g~", function()
12351279 end )
12361280end )
12371281
1282+ describe (" :help v" , function ()
1283+ before_each (_initialize_mappings )
1284+ after_each (_revert_mappings )
1285+
1286+ describe (" character-wise" , function ()
1287+ describe (" down" , function ()
1288+ it (" works with visual selection" , function ()
1289+ local _ , window = _make_buffer ([[
1290+ A paragraph of text and sentences. Here's another sentence
1291+ that spans multiple lines <-- NOTE: The cursor will be set here
1292+ but our code should handle it. And lastly.
1293+ A sentence that starts on its own line.
1294+ ]] )
1295+ vim .api .nvim_win_set_cursor (window , { 2 , 23 })
1296+
1297+ -- NOTE: We yank the visual selection so we can assert it later
1298+ _call_command (" v]asy" )
1299+
1300+ assert .same (
1301+ [[ t spans multiple lines <-- NOTE: The cursor will be set here
1302+ but our code should handle it. ]] ,
1303+ vim .fn .getreg (" " )
1304+ )
1305+ end )
1306+ end )
1307+
1308+ describe (" up" , function ()
1309+ it (" works with visual selection" , function ()
1310+ local _ , window = _make_buffer ([[
1311+ A paragraph of text and sentences. Here's another sentence
1312+ that spans multiple lines <-- NOTE: The cursor will be set here
1313+ but our code should handle it. And lastly.
1314+ A sentence that starts on its own line.
1315+ ]] )
1316+ vim .api .nvim_win_set_cursor (window , { 2 , 23 })
1317+
1318+ -- NOTE: We yank the visual selection so we can assert it later
1319+ _call_command (" v[asy" )
1320+
1321+ assert .same (
1322+ [[ Here's another sentence
1323+ that]] ,
1324+ vim .fn .getreg (" " )
1325+ )
1326+ end )
1327+ end )
1328+ end )
1329+
1330+ describe (" line-wise" , function ()
1331+ describe (" down" , function ()
1332+ it (" works with visual selection" , function ()
1333+ local _ , window = _make_buffer ([[
1334+ aaaa
1335+ bbbb <-- NOTE: The cursor will be set here
1336+ cccc
1337+
1338+ next
1339+ lines
1340+ blah
1341+
1342+ ]] )
1343+ vim .api .nvim_win_set_cursor (window , { 2 , 0 })
1344+
1345+ -- NOTE: We yank the visual selection so we can assert it later
1346+ _call_command (" v]apy" )
1347+
1348+ assert .same (
1349+ [[
1350+ bbbb <-- NOTE: The cursor will be set here
1351+ cccc
1352+
1353+ ]] ,
1354+ vim .fn .getreg (" " )
1355+ )
1356+ end )
1357+ end )
1358+
1359+ describe (" up" , function ()
1360+ it (" works with visual selection" , function ()
1361+ local _ , window = _make_buffer ([[
1362+ aaaa
1363+ bbbb
1364+ cccc
1365+
1366+ next
1367+ lines <-- NOTE: The cursor will be set here
1368+ blah
1369+
1370+ ]] )
1371+ vim .api .nvim_win_set_cursor (window , { 6 , 4 })
1372+
1373+ -- NOTE: We yank the visual selection so we can assert it later
1374+ _call_command (" v[apy" )
1375+
1376+ assert .same (
1377+ [[
1378+ next
1379+ ]] ,
1380+ vim .fn .getreg (" " )
1381+ )
1382+ end )
1383+ end )
1384+ end )
1385+ end )
1386+
12381387describe (" :help y" , function ()
12391388 before_each (_initialize_mappings )
12401389 after_each (_revert_mappings )
0 commit comments