summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-11-27 22:47:43 +0000
committerBram Moolenaar <Bram@vim.org>2021-11-27 22:47:43 +0000
commit59f4f9505ae7ca2499904b94100db103e5ada5a6 (patch)
tree6bbe5b8e14920ef063fb1784b796e351e74221cb
parent3d14c0f2b964195d08b34bb43f89ec5f99255194 (diff)
downloadvim-git-59f4f9505ae7ca2499904b94100db103e5ada5a6.tar.gz
patch 8.2.3687: blockwise insert does not handle autoindent properlyv8.2.3687
Problem: Blockwise insert does not handle autoindent properly when tab is inserted. Solution: Adjust text column for indent before computing column. (closes #9229)
-rw-r--r--src/ops.c45
-rw-r--r--src/testdir/test_blockedit.vim25
-rw-r--r--src/version.c2
3 files changed, 57 insertions, 15 deletions
diff --git a/src/ops.c b/src/ops.c
index 354b2e251..a1afc258e 100644
--- a/src/ops.c
+++ b/src/ops.c
@@ -1456,7 +1456,8 @@ op_insert(oparg_T *oap, long count1)
{
long ins_len, pre_textlen = 0;
char_u *firstline, *ins_text;
- colnr_T ind_pre = 0, ind_post;
+ colnr_T ind_pre_col = 0, ind_post_col;
+ int ind_pre_vcol = 0, ind_post_vcol = 0;
struct block_def bd;
int i;
pos_T t1;
@@ -1497,7 +1498,8 @@ op_insert(oparg_T *oap, long count1)
// Get the info about the block before entering the text
block_prep(oap, &bd, oap->start.lnum, TRUE);
// Get indent information
- ind_pre = (colnr_T)getwhitecols_curline();
+ ind_pre_col = (colnr_T)getwhitecols_curline();
+ ind_pre_vcol = get_indent();
firstline = ml_get(oap->start.lnum) + bd.textcol;
if (oap->op_type == OP_APPEND)
@@ -1563,11 +1565,12 @@ op_insert(oparg_T *oap, long count1)
// If indent kicked in, the firstline might have changed
// but only do that, if the indent actually increased.
- ind_post = (colnr_T)getwhitecols_curline();
- if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre)
+ ind_post_col = (colnr_T)getwhitecols_curline();
+ if (curbuf->b_op_start.col > ind_pre_col && ind_post_col > ind_pre_col)
{
- bd.textcol += ind_post - ind_pre;
- bd.start_vcol += ind_post - ind_pre;
+ bd.textcol += ind_post_col - ind_pre_col;
+ ind_post_vcol = get_indent();
+ bd.start_vcol += ind_post_vcol - ind_pre_vcol;
did_indent = TRUE;
}
@@ -1612,12 +1615,28 @@ op_insert(oparg_T *oap, long count1)
}
}
- /*
- * Spaces and tabs in the indent may have changed to other spaces and
- * tabs. Get the starting column again and correct the length.
- * Don't do this when "$" used, end-of-line will have changed.
- */
+ // Spaces and tabs in the indent may have changed to other spaces and
+ // tabs. Get the starting column again and correct the length.
+ // Don't do this when "$" used, end-of-line will have changed.
+ //
+ // if indent was added and the inserted text was after the indent,
+ // correct the selection for the new indent.
+ if (did_indent && bd.textcol - ind_post_col > 0)
+ {
+ oap->start.col += ind_post_col - ind_pre_col;
+ oap->start_vcol += ind_post_vcol - ind_pre_vcol;
+ oap->end.col += ind_post_col - ind_pre_col;
+ oap->end_vcol += ind_post_vcol - ind_pre_vcol;
+ }
block_prep(oap, &bd2, oap->start.lnum, TRUE);
+ if (did_indent && bd.textcol - ind_post_col > 0)
+ {
+ // undo for where "oap" is used below
+ oap->start.col -= ind_post_col - ind_pre_col;
+ oap->start_vcol -= ind_post_vcol - ind_pre_vcol;
+ oap->end.col -= ind_post_col - ind_pre_col;
+ oap->end_vcol -= ind_post_vcol - ind_pre_vcol;
+ }
if (!bd.is_MAX || bd2.textlen < bd.textlen)
{
if (oap->op_type == OP_APPEND)
@@ -1627,10 +1646,6 @@ op_insert(oparg_T *oap, long count1)
--bd2.textlen;
}
bd.textcol = bd2.textcol;
- if (did_indent && bd.textcol > ind_pre)
- // If the insert was in the indent then include the indent
- // change in the new text, otherwise don't.
- bd.textcol += ind_post - ind_pre;
bd.textlen = bd2.textlen;
}
diff --git a/src/testdir/test_blockedit.vim b/src/testdir/test_blockedit.vim
index 216238a52..e95520ff0 100644
--- a/src/testdir/test_blockedit.vim
+++ b/src/testdir/test_blockedit.vim
@@ -37,6 +37,31 @@ func Test_blockinsert_autoindent()
END
call assert_equal(expected, getline(1, 5))
+ " insert on the next column should do exactly the same
+ :%dele
+ call setline(1, lines)
+ exe "norm! 2Gf)l\<c-v>2jI: asdf\<esc>"
+ call assert_equal(expected, getline(1, 5))
+
+ :%dele
+ call setline(1, lines)
+ setlocal sw=8 noet
+ exe "norm! 2Gf)\<c-v>2jA: asdf\<esc>"
+ let expected =<< trim END
+ var d = {
+ a: (): asdf => 0,
+ b: (): asdf => 0,
+ c: (): asdf => 0,
+ }
+ END
+ call assert_equal(expected, getline(1, 5))
+
+ " insert on the next column should do exactly the same
+ :%dele
+ call setline(1, lines)
+ exe "norm! 2Gf)l\<c-v>2jI: asdf\<esc>"
+ call assert_equal(expected, getline(1, 5))
+
filetype off
bwipe!
endfunc
diff --git a/src/version.c b/src/version.c
index 945673784..8972dbd36 100644
--- a/src/version.c
+++ b/src/version.c
@@ -758,6 +758,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3687,
+/**/
3686,
/**/
3685,