mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
Merge pull request #19231 from zeertzjq/vim-8.2.3615
vim-patch:8.2.{3615,3754,5122}: indent fixes
This commit is contained in:
@@ -7330,7 +7330,7 @@ static void replace_do_bs(int limit_col)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Check that C-indenting is on.
|
/// Check that C-indenting is on.
|
||||||
static bool cindent_on(void)
|
bool cindent_on(void)
|
||||||
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
|
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
|
||||||
{
|
{
|
||||||
return !p_paste && (curbuf->b_p_cin || *curbuf->b_p_inde != NUL);
|
return !p_paste && (curbuf->b_p_cin || *curbuf->b_p_inde != NUL);
|
||||||
|
@@ -656,6 +656,9 @@ int get_lisp_indent(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (*that == NUL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ((*that == '(') || (*that == '[')) {
|
if ((*that == '(') || (*that == '[')) {
|
||||||
parencount++;
|
parencount++;
|
||||||
|
@@ -4421,6 +4421,7 @@ void format_lines(linenr_T line_count, int avoid_fex)
|
|||||||
int smd_save;
|
int smd_save;
|
||||||
long count;
|
long count;
|
||||||
bool need_set_indent = true; // set indent of next paragraph
|
bool need_set_indent = true; // set indent of next paragraph
|
||||||
|
linenr_T first_line = curwin->w_cursor.lnum;
|
||||||
bool force_format = false;
|
bool force_format = false;
|
||||||
const int old_State = State;
|
const int old_State = State;
|
||||||
|
|
||||||
@@ -4547,9 +4548,24 @@ void format_lines(linenr_T line_count, int avoid_fex)
|
|||||||
*/
|
*/
|
||||||
if (is_end_par || force_format) {
|
if (is_end_par || force_format) {
|
||||||
if (need_set_indent) {
|
if (need_set_indent) {
|
||||||
// replace indent in first line with minimal number of
|
int indent = 0; // amount of indent needed
|
||||||
// tabs and spaces, according to current options
|
|
||||||
(void)set_indent(get_indent(), SIN_CHANGED);
|
// Replace indent in first line of a paragraph with minimal
|
||||||
|
// number of tabs and spaces, according to current options.
|
||||||
|
// For the very first formatted line keep the current
|
||||||
|
// indent.
|
||||||
|
if (curwin->w_cursor.lnum == first_line) {
|
||||||
|
indent = get_indent();
|
||||||
|
} else if (curbuf->b_p_lisp) {
|
||||||
|
indent = get_lisp_indent();
|
||||||
|
} else {
|
||||||
|
if (cindent_on()) {
|
||||||
|
indent = *curbuf->b_p_inde != NUL ? get_expr_indent() : get_c_indent();
|
||||||
|
} else {
|
||||||
|
indent = get_indent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(void)set_indent(indent, SIN_CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// put cursor on last non-space
|
// put cursor on last non-space
|
||||||
|
@@ -144,6 +144,16 @@ func Test_lisp_indent()
|
|||||||
close!
|
close!
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_lisp_indent_quoted()
|
||||||
|
" This was going past the end of the line
|
||||||
|
new
|
||||||
|
setlocal lisp autoindent
|
||||||
|
call setline(1, ['"[', '='])
|
||||||
|
normal Gvk=
|
||||||
|
|
||||||
|
bwipe!
|
||||||
|
endfunc
|
||||||
|
|
||||||
" Test for setting the 'indentexpr' from a modeline
|
" Test for setting the 'indentexpr' from a modeline
|
||||||
func Test_modeline_indent_expr()
|
func Test_modeline_indent_expr()
|
||||||
let modeline = &modeline
|
let modeline = &modeline
|
||||||
@@ -165,4 +175,105 @@ func Test_modeline_indent_expr()
|
|||||||
call delete('Xfile.txt')
|
call delete('Xfile.txt')
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_indent_func_with_gq()
|
||||||
|
|
||||||
|
function GetTeXIndent()
|
||||||
|
" Sample indent expression for TeX files
|
||||||
|
let lnum = prevnonblank(v:lnum - 1)
|
||||||
|
" At the start of the file use zero indent.
|
||||||
|
if lnum == 0
|
||||||
|
return 0
|
||||||
|
endif
|
||||||
|
let line = getline(lnum)
|
||||||
|
let ind = indent(lnum)
|
||||||
|
" Add a 'shiftwidth' after beginning of environments.
|
||||||
|
if line =~ '\\begin{center}'
|
||||||
|
let ind = ind + shiftwidth()
|
||||||
|
endif
|
||||||
|
return ind
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
new
|
||||||
|
setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
|
||||||
|
put =[ '\documentclass{article}', '', '\begin{document}', '',
|
||||||
|
\ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
|
||||||
|
\ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
|
||||||
|
\ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
|
||||||
|
\ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
|
||||||
|
\ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
|
||||||
|
\ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
|
||||||
|
\ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
|
||||||
|
\ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
|
||||||
|
\ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
|
||||||
|
\ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
|
||||||
|
\ 'Donec id semper purus.',
|
||||||
|
\ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
|
||||||
|
\ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
|
||||||
|
\ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
|
||||||
|
\ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
|
||||||
|
\ 'ultrices posuere cubilia curae;', '', '']
|
||||||
|
1d_
|
||||||
|
call cursor(5, 1)
|
||||||
|
ka
|
||||||
|
call cursor(14, 1)
|
||||||
|
kb
|
||||||
|
norm! 'agqap
|
||||||
|
norm! 'bgqG
|
||||||
|
let expected = [ '\documentclass{article}', '', '\begin{document}', '',
|
||||||
|
\ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
|
||||||
|
\ 'elit. Fusce ut enim non libero efficitur aliquet.',
|
||||||
|
\ 'Maecenas metus justo, facilisis convallis blandit',
|
||||||
|
\ 'non, semper eu urna. Suspendisse diam diam,',
|
||||||
|
\ 'iaculis faucibus lorem eu, fringilla condimentum',
|
||||||
|
\ 'lectus. Quisque euismod diam at convallis',
|
||||||
|
\ 'vulputate. Pellentesque laoreet tortor sit amet',
|
||||||
|
\ 'mauris euismod ornare. Sed varius bibendum orci',
|
||||||
|
\ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
|
||||||
|
\ 'accumsan, metus lectus ultrices odio, sed',
|
||||||
|
\ 'elementum mi ante at arcu.', '', '\begin{center}', '',
|
||||||
|
\ ' Proin nec risus consequat nunc dapibus',
|
||||||
|
\ ' consectetur. Mauris lacinia est a augue',
|
||||||
|
\ ' tristique accumsan. Morbi pretium, felis',
|
||||||
|
\ ' molestie eleifend condimentum, arcu ipsum congue',
|
||||||
|
\ ' nisl, quis euismod purus libero in ante.',
|
||||||
|
\ '',
|
||||||
|
\ ' Donec id semper purus. Suspendisse eget aliquam',
|
||||||
|
\ ' nunc. Maecenas fringilla mauris vitae maximus',
|
||||||
|
\ ' condimentum. Cras a quam in mi dictum eleifend',
|
||||||
|
\ ' at a lorem. Sed convallis ante a commodo',
|
||||||
|
\ ' facilisis. Nam suscipit vulputate odio, vel',
|
||||||
|
\ ' dapibus nisl dignissim facilisis. Vestibulum',
|
||||||
|
\ ' ante ipsum primis in faucibus orci luctus et',
|
||||||
|
\ ' ultrices posuere cubilia curae;', '', '']
|
||||||
|
call assert_equal(expected, getline(1, '$'))
|
||||||
|
|
||||||
|
bwipe!
|
||||||
|
delmark ab
|
||||||
|
delfunction GetTeXIndent
|
||||||
|
endfu
|
||||||
|
|
||||||
|
func Test_formatting_keeps_first_line_indent()
|
||||||
|
let lines =<< trim END
|
||||||
|
foo()
|
||||||
|
{
|
||||||
|
int x; // manually positioned
|
||||||
|
// more text that will be formatted
|
||||||
|
// but not reindented
|
||||||
|
END
|
||||||
|
new
|
||||||
|
call setline(1, lines)
|
||||||
|
setlocal sw=4 cindent tw=45 et
|
||||||
|
normal! 4Ggqj
|
||||||
|
let expected =<< trim END
|
||||||
|
foo()
|
||||||
|
{
|
||||||
|
int x; // manually positioned
|
||||||
|
// more text that will be
|
||||||
|
// formatted but not
|
||||||
|
// reindented
|
||||||
|
END
|
||||||
|
call assert_equal(expected, getline(1, '$'))
|
||||||
|
bwipe!
|
||||||
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
Reference in New Issue
Block a user