mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
Merge pull request #18180 from zeertzjq/vim-8.2.4792
vim-patch:8.2.{0358,4792,4794}: indent operator creates an undo entry for every line
This commit is contained in:
commit
a4b75010ba
@ -636,7 +636,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def
|
||||
*/
|
||||
void op_reindent(oparg_T *oap, Indenter how)
|
||||
{
|
||||
long i;
|
||||
long i = 0;
|
||||
char_u *l;
|
||||
int amount;
|
||||
linenr_T first_changed = 0;
|
||||
@ -649,38 +649,41 @@ void op_reindent(oparg_T *oap, Indenter how)
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = oap->line_count - 1; i >= 0 && !got_int; i--) {
|
||||
/* it's a slow thing to do, so give feedback so there's no worry that
|
||||
* the computer's just hung. */
|
||||
// Save for undo. Do this once for all lines, much faster than doing this
|
||||
// for each line separately, especially when undoing.
|
||||
if (u_savecommon(curbuf, start_lnum - 1, start_lnum + oap->line_count,
|
||||
start_lnum + oap->line_count, false) == OK) {
|
||||
for (i = oap->line_count - 1; i >= 0 && !got_int; i--) {
|
||||
// it's a slow thing to do, so give feedback so there's no worry
|
||||
// that the computer's just hung.
|
||||
|
||||
if (i > 1
|
||||
&& (i % 50 == 0 || i == oap->line_count - 1)
|
||||
&& oap->line_count > p_report) {
|
||||
smsg(_("%" PRId64 " lines to indent... "), (int64_t)i);
|
||||
}
|
||||
|
||||
/*
|
||||
* Be vi-compatible: For lisp indenting the first line is not
|
||||
* indented, unless there is only one line.
|
||||
*/
|
||||
if (i != oap->line_count - 1 || oap->line_count == 1
|
||||
|| how != get_lisp_indent) {
|
||||
l = skipwhite(get_cursor_line_ptr());
|
||||
if (*l == NUL) { // empty or blank line
|
||||
amount = 0;
|
||||
} else {
|
||||
amount = how(); // get the indent for this line
|
||||
if (i > 1
|
||||
&& (i % 50 == 0 || i == oap->line_count - 1)
|
||||
&& oap->line_count > p_report) {
|
||||
smsg(_("%" PRId64 " lines to indent... "), (int64_t)i);
|
||||
}
|
||||
if (amount >= 0 && set_indent(amount, SIN_UNDO)) {
|
||||
// did change the indent, call changed_lines() later
|
||||
if (first_changed == 0) {
|
||||
first_changed = curwin->w_cursor.lnum;
|
||||
|
||||
// Be vi-compatible: For lisp indenting the first line is not
|
||||
// indented, unless there is only one line.
|
||||
if (i != oap->line_count - 1 || oap->line_count == 1
|
||||
|| how != get_lisp_indent) {
|
||||
l = skipwhite(get_cursor_line_ptr());
|
||||
if (*l == NUL) { // empty or blank line
|
||||
amount = 0;
|
||||
} else {
|
||||
amount = how(); // get the indent for this line
|
||||
}
|
||||
if (amount >= 0 && set_indent(amount, 0)) {
|
||||
// did change the indent, call changed_lines() later
|
||||
if (first_changed == 0) {
|
||||
first_changed = curwin->w_cursor.lnum;
|
||||
}
|
||||
last_changed = curwin->w_cursor.lnum;
|
||||
}
|
||||
last_changed = curwin->w_cursor.lnum;
|
||||
}
|
||||
curwin->w_cursor.lnum++;
|
||||
curwin->w_cursor.col = 0; // make sure it's valid
|
||||
}
|
||||
++curwin->w_cursor.lnum;
|
||||
curwin->w_cursor.col = 0; // make sure it's valid
|
||||
}
|
||||
|
||||
// put cursor on first non-blank of indented line
|
||||
|
@ -51,6 +51,13 @@ func Test_ex_mode()
|
||||
call assert_equal([' foo', ' foo'], Ex(" foo\<C-d>"), e)
|
||||
call assert_equal(['foo', ' foo0'], Ex(" foo0\<C-d>"), e)
|
||||
call assert_equal(['foo', ' foo^'], Ex(" foo^\<C-d>"), e)
|
||||
call assert_equal(['foo', 'foo'],
|
||||
\ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e)
|
||||
" default wildchar <Tab> interferes with this test
|
||||
set wildchar=<c-e>
|
||||
call assert_equal(["a\tb", "a\tb"], Ex("a\t\t\<C-H>b"), e)
|
||||
call assert_equal(["\t mn", "\tm\<C-T>n"], Ex("\tm\<C-T>n"), e)
|
||||
set wildchar&
|
||||
endfor
|
||||
|
||||
set sw&
|
||||
|
@ -37,15 +37,6 @@ func Test_expand_sflnum()
|
||||
delcommand Flnum
|
||||
endfunc
|
||||
|
||||
func Test_expand()
|
||||
new
|
||||
call assert_equal("", expand('%:S'))
|
||||
call assert_equal('3', '<slnum>'->expand())
|
||||
call assert_equal(['4'], expand('<slnum>', v:false, v:true))
|
||||
" Don't add any line above this, otherwise <slnum> will change.
|
||||
quit
|
||||
endfunc
|
||||
|
||||
func Test_expand_sfile()
|
||||
call assert_match('test_expand_func\.vim$', s:sfile)
|
||||
call assert_match('^function .*\.\.Test_expand_sfile$', expand('<sfile>'))
|
||||
@ -77,6 +68,15 @@ func Test_expand_slnum()
|
||||
delcommand Slnum
|
||||
endfunc
|
||||
|
||||
func Test_expand()
|
||||
new
|
||||
call assert_equal("", expand('%:S'))
|
||||
call assert_equal('3', '<slnum>'->expand())
|
||||
call assert_equal(['4'], expand('<slnum>', v:false, v:true))
|
||||
" Don't add any line above this, otherwise <slnum> will change.
|
||||
quit
|
||||
endfunc
|
||||
|
||||
func s:sid_test()
|
||||
return 'works'
|
||||
endfunc
|
||||
@ -87,4 +87,17 @@ func Test_expand_SID()
|
||||
call assert_equal('works', g:sid_result)
|
||||
endfunc
|
||||
|
||||
|
||||
" Test for 'wildignore' with expand()
|
||||
func Test_expand_wildignore()
|
||||
set wildignore=*.vim
|
||||
call assert_equal('', expand('test_expand_func.vim'))
|
||||
call assert_equal('', expand('test_expand_func.vim', 0))
|
||||
call assert_equal([], expand('test_expand_func.vim', 0, 1))
|
||||
call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1))
|
||||
call assert_equal(['test_expand_func.vim'],
|
||||
\ expand('test_expand_func.vim', 1, 1))
|
||||
set wildignore&
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
124
src/nvim/testdir/test_indent.vim
Normal file
124
src/nvim/testdir/test_indent.vim
Normal file
@ -0,0 +1,124 @@
|
||||
" Test for various indent options
|
||||
|
||||
func Test_preserveindent()
|
||||
new
|
||||
" Test for autoindent copying indent from the previous line
|
||||
setlocal autoindent
|
||||
call setline(1, [repeat(' ', 16) .. 'line1'])
|
||||
call feedkeys("A\nline2", 'xt')
|
||||
call assert_equal("\t\tline2", getline(2))
|
||||
setlocal autoindent&
|
||||
|
||||
" Test for using CTRL-T with and without 'preserveindent'
|
||||
set shiftwidth=4
|
||||
call cursor(1, 1)
|
||||
call setline(1, " \t ")
|
||||
call feedkeys("Al\<C-T>", 'xt')
|
||||
call assert_equal("\t\tl", getline(1))
|
||||
set preserveindent
|
||||
call setline(1, " \t ")
|
||||
call feedkeys("Al\<C-T>", 'xt')
|
||||
call assert_equal(" \t \tl", getline(1))
|
||||
set pi& sw&
|
||||
|
||||
" Test for using CTRL-T with 'expandtab' and 'preserveindent'
|
||||
call cursor(1, 1)
|
||||
call setline(1, "\t \t")
|
||||
set shiftwidth=4 expandtab preserveindent
|
||||
call feedkeys("Al\<C-T>", 'xt')
|
||||
call assert_equal("\t \t l", getline(1))
|
||||
set sw& et& pi&
|
||||
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test for indent()
|
||||
func Test_indent_func()
|
||||
call assert_equal(-1, indent(-1))
|
||||
new
|
||||
call setline(1, "\tabc")
|
||||
call assert_equal(8, indent(1))
|
||||
call setline(1, " abc")
|
||||
call assert_equal(4, indent(1))
|
||||
call setline(1, " \t abc")
|
||||
call assert_equal(12, indent(1))
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test for reindenting a line using the '=' operator
|
||||
func Test_reindent()
|
||||
new
|
||||
call setline(1, 'abc')
|
||||
set nomodifiable
|
||||
call assert_fails('normal ==', 'E21:')
|
||||
set modifiable
|
||||
|
||||
call setline(1, ['foo', 'bar'])
|
||||
call feedkeys('ggVG=', 'xt')
|
||||
call assert_equal(['foo', 'bar'], getline(1, 2))
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test indent operator creating one undo entry
|
||||
func Test_indent_operator_undo()
|
||||
enew
|
||||
call setline(1, range(12)->map('"\t" .. v:val'))
|
||||
func FoldExpr()
|
||||
let g:foldcount += 1
|
||||
return '='
|
||||
endfunc
|
||||
set foldmethod=expr foldexpr=FoldExpr()
|
||||
let g:foldcount = 0
|
||||
redraw
|
||||
call assert_equal(12, g:foldcount)
|
||||
normal gg=G
|
||||
call assert_equal(24, g:foldcount)
|
||||
undo
|
||||
call assert_equal(38, g:foldcount)
|
||||
|
||||
bwipe!
|
||||
set foldmethod& foldexpr=
|
||||
delfunc FoldExpr
|
||||
unlet g:foldcount
|
||||
endfunc
|
||||
|
||||
" Test for shifting a line with a preprocessor directive ('#')
|
||||
func Test_preproc_indent()
|
||||
new
|
||||
set sw=4
|
||||
call setline(1, '#define FOO 1')
|
||||
normal >>
|
||||
call assert_equal(' #define FOO 1', getline(1))
|
||||
|
||||
" with 'smartindent'
|
||||
call setline(1, '#define FOO 1')
|
||||
set smartindent
|
||||
normal >>
|
||||
call assert_equal('#define FOO 1', getline(1))
|
||||
set smartindent&
|
||||
|
||||
" with 'cindent'
|
||||
set cindent
|
||||
normal >>
|
||||
call assert_equal('#define FOO 1', getline(1))
|
||||
set cindent&
|
||||
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" Test for 'copyindent'
|
||||
func Test_copyindent()
|
||||
new
|
||||
set shiftwidth=4 autoindent expandtab copyindent
|
||||
call setline(1, " \t abc")
|
||||
call feedkeys("ol", 'xt')
|
||||
call assert_equal(" \t l", getline(2))
|
||||
set noexpandtab
|
||||
call setline(1, " \t abc")
|
||||
call feedkeys("ol", 'xt')
|
||||
call assert_equal(" \t l", getline(2))
|
||||
set sw& ai& et& ci&
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
@ -45,6 +45,7 @@ func Test_lisp_indent()
|
||||
\ ])
|
||||
call assert_equal(7, lispindent(2))
|
||||
call assert_equal(5, 6->lispindent())
|
||||
call assert_equal(-1, lispindent(-1))
|
||||
|
||||
set lisp
|
||||
set lispwords&
|
||||
@ -83,3 +84,5 @@ func Test_lisp_indent()
|
||||
let &cpoptions=save_copt
|
||||
set nolisp
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
@ -38,4 +38,27 @@ func Test_smartindent_has_no_effect()
|
||||
bwipe!
|
||||
endfunc
|
||||
|
||||
" Test for inserting '{' and '} with smartindent
|
||||
func Test_smartindent_braces()
|
||||
new
|
||||
set smartindent shiftwidth=4
|
||||
call setline(1, [' if (a)', "\tif (b)", "\t return 1"])
|
||||
normal 2ggO{
|
||||
normal 3ggA {
|
||||
normal 4ggo}
|
||||
normal o}
|
||||
normal 4ggO#define FOO 1
|
||||
call assert_equal([
|
||||
\ ' if (a)',
|
||||
\ ' {',
|
||||
\ "\tif (b) {",
|
||||
\ '#define FOO 1',
|
||||
\ "\t return 1",
|
||||
\ "\t}",
|
||||
\ ' }'
|
||||
\ ], getline(1, '$'))
|
||||
set si& sw& ai&
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
@ -92,6 +92,18 @@ func Test_vartabs()
|
||||
let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l"
|
||||
call assert_equal(expect, getline(1))
|
||||
|
||||
" Test for 'retab' with vts
|
||||
set ts=8 sts=0 vts=5,3,6,2 vsts=
|
||||
exe "norm! S l"
|
||||
.retab!
|
||||
call assert_equal("\t\t\t\tl", getline(1))
|
||||
|
||||
" Test for 'retab' with same vlaues as vts
|
||||
set ts=8 sts=0 vts=5,3,6,2 vsts=
|
||||
exe "norm! S l"
|
||||
.retab! 5,3,6,2
|
||||
call assert_equal("\t\t\t\tl", getline(1))
|
||||
|
||||
" Check that global and local values are set.
|
||||
set ts=4 vts=6 sts=8 vsts=10
|
||||
call assert_equal(&ts, 4)
|
||||
@ -389,3 +401,33 @@ func Test_vartabs_reset()
|
||||
set all&
|
||||
call assert_equal('', &vts)
|
||||
endfunc
|
||||
|
||||
func s:SaveCol(l)
|
||||
call add(a:l, [col('.'), virtcol('.')])
|
||||
return ''
|
||||
endfunc
|
||||
|
||||
" Test for 'varsofttabstop'
|
||||
func Test_varsofttabstop()
|
||||
new
|
||||
inoremap <expr> <F2> s:SaveCol(g:cols)
|
||||
|
||||
set backspace=indent,eol,start
|
||||
set varsofttabstop=6,2,5,3
|
||||
let g:cols = []
|
||||
call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt')
|
||||
call assert_equal("\t\t ", getline(1))
|
||||
call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols)
|
||||
|
||||
let g:cols = []
|
||||
call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt')
|
||||
call assert_equal('', getline(1))
|
||||
call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols)
|
||||
|
||||
set varsofttabstop&
|
||||
set backspace&
|
||||
iunmap <F2>
|
||||
close!
|
||||
endfunc
|
||||
|
||||
" vim: shiftwidth=2 sts=2 expandtab
|
||||
|
@ -32,5 +32,11 @@ describe('Ex mode', function()
|
||||
test_ex_edit(' foo', ' foo<C-d>')
|
||||
test_ex_edit(' foo0', ' foo0<C-d>')
|
||||
test_ex_edit(' foo^', ' foo^<C-d>')
|
||||
test_ex_edit('foo', '<BS><C-H><Del><kDel>foo')
|
||||
-- default wildchar <Tab> interferes with this test
|
||||
command('set wildchar=<c-e>')
|
||||
test_ex_edit('a\tb', 'a\t\t<C-H>b')
|
||||
test_ex_edit('\tm<C-T>n', '\tm<C-T>n')
|
||||
command('set wildchar&')
|
||||
end)
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user