aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/edit.c4
-rw-r--r--src/nvim/ops.c1
-rw-r--r--src/nvim/regexp_nfa.c2
-rw-r--r--src/nvim/testdir/test_normal.vim15
-rw-r--r--src/nvim/testdir/test_textformat.vim222
5 files changed, 241 insertions, 3 deletions
diff --git a/src/nvim/edit.c b/src/nvim/edit.c
index 1cf821f786..45bd84261d 100644
--- a/src/nvim/edit.c
+++ b/src/nvim/edit.c
@@ -3716,7 +3716,7 @@ static bool ins_compl_prep(int c)
retval = true;
}
- auto_format(FALSE, TRUE);
+ auto_format(false, true);
// Trigger the CompleteDonePre event to give scripts a chance to
// act upon the completion before clearing the info, and restore
@@ -6503,7 +6503,7 @@ stop_insert (
curwin->w_cursor = tpos;
}
- auto_format(TRUE, FALSE);
+ auto_format(true, false);
if (ascii_iswhite(cc)) {
if (gchar_cursor() != NUL)
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 40dd5f0b5c..37db1bb7e2 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -1683,6 +1683,7 @@ int op_delete(oparg_T *oap)
(int)oap->line_count-1, n, deleted_bytes,
0, 0, 0, kExtmarkUndo);
}
+ auto_format(false, true);
}
msgmore(curbuf->b_ml.ml_line_count - old_lcount);
diff --git a/src/nvim/regexp_nfa.c b/src/nvim/regexp_nfa.c
index 137b2304c0..22cd6baf6e 100644
--- a/src/nvim/regexp_nfa.c
+++ b/src/nvim/regexp_nfa.c
@@ -6601,7 +6601,7 @@ theend:
|| (end->lnum == start->lnum && end->col < start->col)) {
rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
}
- } else {
+ } else if (retval > 0) {
if (rex.reg_match->endp[0] < rex.reg_match->startp[0]) {
rex.reg_match->endp[0] = rex.reg_match->startp[0];
}
diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim
index ad6d325510..7a846e5ea0 100644
--- a/src/nvim/testdir/test_normal.vim
+++ b/src/nvim/testdir/test_normal.vim
@@ -222,6 +222,21 @@ func Test_normal05_formatexpr_setopt()
set formatexpr=
endfunc
+" When 'formatexpr' returns non-zero, internal formatting is used.
+func Test_normal_formatexpr_returns_nonzero()
+ new
+ call setline(1, ['one', 'two'])
+ func! Format()
+ return 1
+ endfunc
+ setlocal formatexpr=Format()
+ normal VGgq
+ call assert_equal(['one two'], getline(1, '$'))
+ setlocal formatexpr=
+ delfunc Format
+ close!
+endfunc
+
func Test_normal06_formatprg()
" basic test for formatprg
" only test on non windows platform
diff --git a/src/nvim/testdir/test_textformat.vim b/src/nvim/testdir/test_textformat.vim
index 2223be952c..4af52b536c 100644
--- a/src/nvim/testdir/test_textformat.vim
+++ b/src/nvim/testdir/test_textformat.vim
@@ -424,6 +424,15 @@ func Test_format_align()
\ ], getline(1, '$'))
enew!
+ " align text with 'wrapmargin'
+ 50vnew
+ call setline(1, ['Vim'])
+ setl textwidth=0
+ setl wrapmargin=30
+ right
+ call assert_equal("\t\t Vim", getline(1))
+ q!
+
set tw&
endfunc
@@ -931,4 +940,217 @@ func Test_substitute()
call assert_equal('a1a2a3a', substitute('123', '\zs', 'a', 'g'))
endfunc
+" Test for 'a' and 'w' flags in 'formatoptions'
+func Test_fo_a_w()
+ new
+ setlocal fo+=aw tw=10
+ call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
+ call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
+
+ " Test for 'a', 'w' and '1' options.
+ setlocal textwidth=0
+ setlocal fo=1aw
+ %d
+ call setline(1, '. foo')
+ normal 72ig
+ call feedkeys('a uu uu uu', 'xt')
+ call assert_equal('g uu uu ', getline(1)[-8:])
+ call assert_equal(['uu. foo'], getline(2, '$'))
+
+ " using backspace or "x" triggers reformat
+ call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
+ set tw=10
+ set fo=taw
+ set bs=indent,eol,start
+ exe "normal 1G4la\<BS>\<BS>\<Esc>"
+ call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
+ exe "normal f4xx"
+ call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
+
+ set tw=0
+ set fo&
+ %bw!
+endfunc
+
+" Test for formatting lines using gq in visual mode
+func Test_visual_gq_format()
+ new
+ call setline(1, ['one two three four', 'five six', 'one two'])
+ setl textwidth=10
+ call feedkeys('ggv$jj', 'xt')
+ redraw!
+ normal gq
+ %d
+ call setline(1, ['one two three four', 'five six', 'one two'])
+ normal G$
+ call feedkeys('v0kk', 'xt')
+ redraw!
+ normal gq
+ setl textwidth&
+ close!
+endfunc
+
+" Test for 'n' flag in 'formatoptions' to format numbered lists
+func Test_fo_n()
+ new
+ setlocal autoindent
+ setlocal textwidth=12
+ setlocal fo=n
+ call setline(1, [' 1) one two three four', ' 2) two'])
+ normal gggqG
+ call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
+ \ getline(1, '$'))
+ close!
+endfunc
+
+" Test for 'formatlistpat' option
+func Test_formatlistpat()
+ new
+ setlocal autoindent
+ setlocal textwidth=10
+ setlocal fo=n
+ setlocal formatlistpat=^\\s*-\\s*
+ call setline(1, [' - one two three', ' - two'])
+ normal gggqG
+ call assert_equal([' - one', ' two', ' three', ' - two'],
+ \ getline(1, '$'))
+ close!
+endfunc
+
+" Test for the 'b' and 'v' flags in 'formatoptions'
+" Text should wrap only if a space character is inserted at or before
+" 'textwidth'
+func Test_fo_b()
+ new
+ setlocal textwidth=20
+
+ setlocal formatoptions=t
+ call setline(1, 'one two three four')
+ call feedkeys('Amore', 'xt')
+ call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
+
+ setlocal formatoptions=bt
+ %d
+ call setline(1, 'one two three four')
+ call feedkeys('Amore five', 'xt')
+ call assert_equal(['one two three fourmore five'], getline(1, '$'))
+
+ setlocal formatoptions=bt
+ %d
+ call setline(1, 'one two three four')
+ call feedkeys('A five', 'xt')
+ call assert_equal(['one two three four', 'five'], getline(1, '$'))
+
+ setlocal formatoptions=vt
+ %d
+ call setline(1, 'one two three four')
+ call feedkeys('Amore five', 'xt')
+ call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
+
+ close!
+endfunc
+
+" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
+" word.
+func Test_fo_1()
+ new
+ setlocal textwidth=20
+
+ setlocal formatoptions=t
+ call setline(1, 'one two three four')
+ call feedkeys('A a bird', 'xt')
+ call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
+
+ %d
+ setlocal formatoptions=t1
+ call setline(1, 'one two three four')
+ call feedkeys('A a bird', 'xt')
+ call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
+
+ close!
+endfunc
+
+" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
+" is longer than 'textwidth', then it is not broken.
+func Test_fo_l()
+ new
+ setlocal textwidth=20
+
+ setlocal formatoptions=t
+ call setline(1, 'one two three four five')
+ call feedkeys('A six', 'xt')
+ call assert_equal(['one two three four', 'five six'], getline(1, '$'))
+
+ %d
+ setlocal formatoptions=tl
+ call setline(1, 'one two three four five')
+ call feedkeys('A six', 'xt')
+ call assert_equal(['one two three four five six'], getline(1, '$'))
+
+ close!
+endfunc
+
+" Test for the '2' flag in 'formatoptions'
+func Test_fo_2()
+ new
+ setlocal autoindent
+ setlocal formatoptions=t2
+ setlocal textwidth=30
+ call setline(1, ["\tfirst line of a paragraph.",
+ \ "second line of the same paragraph.",
+ \ "third line."])
+ normal gggqG
+ call assert_equal(["\tfirst line of a",
+ \ "paragraph. second line of the",
+ \ "same paragraph. third line."], getline(1, '$'))
+ close!
+endfunc
+
+" Test for formatting lines where only the first line has a comment.
+func Test_fo_gq_with_firstline_comment()
+ new
+ setlocal formatoptions=tcq
+ call setline(1, ['- one two', 'three'])
+ normal gggqG
+ call assert_equal(['- one two three'], getline(1, '$'))
+
+ %d
+ call setline(1, ['- one', '- two'])
+ normal gggqG
+ call assert_equal(['- one', '- two'], getline(1, '$'))
+ close!
+endfunc
+
+" Test for trying to join a comment line with a non-comment line
+func Test_join_comments()
+ new
+ call setline(1, ['one', '/* two */', 'three'])
+ normal gggqG
+ call assert_equal(['one', '/* two */', 'three'], getline(1, '$'))
+ close!
+endfunc
+
+" Test for using 'a' in 'formatoptions' with comments
+func Test_autoformat_comments()
+ new
+ setlocal formatoptions+=a
+ call feedkeys("a- one\n- two\n", 'xt')
+ call assert_equal(['- one', '- two', ''], getline(1, '$'))
+
+ %d
+ call feedkeys("a\none\n", 'xt')
+ call assert_equal(['', 'one', ''], getline(1, '$'))
+
+ setlocal formatoptions+=aw
+ %d
+ call feedkeys("aone \ntwo\n", 'xt')
+ call assert_equal(['one two', ''], getline(1, '$'))
+
+ %d
+ call feedkeys("aone\ntwo\n", 'xt')
+ call assert_equal(['one', 'two', ''], getline(1, '$'))
+
+ close!
+endfunc
+
" vim: shiftwidth=2 sts=2 expandtab