1
0
forked from aniani/vim

patch 8.1.0671: cursor in the wrong column after auto-formatting

Problem:    Cursor in the wrong column after auto-formatting.
Solution:   Check for deleting more spaces than adding. (closes #3748)
This commit is contained in:
Bram Moolenaar
2018-12-31 23:58:24 +01:00
parent 3d631cb0b3
commit e1e714ef0d
6 changed files with 31 additions and 6 deletions

View File

@@ -1211,6 +1211,8 @@ mark_adjust_internal(
posp->lnum += lnum_amount; \ posp->lnum += lnum_amount; \
if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \ if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
posp->col = 0; \ posp->col = 0; \
else if (posp->col < spaces_removed) \
posp->col = col_amount + spaces_removed; \
else \ else \
posp->col += col_amount; \ posp->col += col_amount; \
} \ } \
@@ -1220,13 +1222,16 @@ mark_adjust_internal(
* Adjust marks in line "lnum" at column "mincol" and further: add * Adjust marks in line "lnum" at column "mincol" and further: add
* "lnum_amount" to the line number and add "col_amount" to the column * "lnum_amount" to the line number and add "col_amount" to the column
* position. * position.
* "spaces_removed" is the number of spaces that were removed, matters when the
* cursor is inside them.
*/ */
void void
mark_col_adjust( mark_col_adjust(
linenr_T lnum, linenr_T lnum,
colnr_T mincol, colnr_T mincol,
long lnum_amount, long lnum_amount,
long col_amount) long col_amount,
int spaces_removed)
{ {
int i; int i;
int fnum = curbuf->b_fnum; int fnum = curbuf->b_fnum;

View File

@@ -1705,7 +1705,7 @@ open_line(
if (flags & OPENLINE_MARKFIX) if (flags & OPENLINE_MARKFIX)
mark_col_adjust(curwin->w_cursor.lnum, mark_col_adjust(curwin->w_cursor.lnum,
curwin->w_cursor.col + less_cols_off, curwin->w_cursor.col + less_cols_off,
1L, (long)-less_cols); 1L, (long)-less_cols, 0);
} }
else else
changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);

View File

@@ -4707,6 +4707,8 @@ do_join(
*/ */
for (t = count - 1; ; --t) for (t = count - 1; ; --t)
{ {
int spaces_removed;
cend -= currsize; cend -= currsize;
mch_memmove(cend, curr, (size_t)currsize); mch_memmove(cend, curr, (size_t)currsize);
if (spaces[t] > 0) if (spaces[t] > 0)
@@ -4714,8 +4716,13 @@ do_join(
cend -= spaces[t]; cend -= spaces[t];
vim_memset(cend, ' ', (size_t)(spaces[t])); vim_memset(cend, ' ', (size_t)(spaces[t]));
} }
// If deleting more spaces than adding, the cursor moves no more than
// what is added if it is inside these spaces.
spaces_removed = (curr - curr_start) - spaces[t];
mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t, mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t,
(long)(cend - newp + spaces[t] - (curr - curr_start))); (long)(cend - newp - spaces_removed), spaces_removed);
if (t == 0) if (t == 0)
break; break;
curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1)); curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1));
@@ -5225,7 +5232,7 @@ format_lines(
{ {
(void)del_bytes((long)next_leader_len, FALSE, FALSE); (void)del_bytes((long)next_leader_len, FALSE, FALSE);
mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L,
(long)-next_leader_len); (long)-next_leader_len, 0);
} else } else
#endif #endif
if (second_indent > 0) /* the "leader" for FO_Q_SECOND */ if (second_indent > 0) /* the "leader" for FO_Q_SECOND */
@@ -5236,7 +5243,7 @@ format_lines(
{ {
(void)del_bytes(indent, FALSE, FALSE); (void)del_bytes(indent, FALSE, FALSE);
mark_col_adjust(curwin->w_cursor.lnum, mark_col_adjust(curwin->w_cursor.lnum,
(colnr_T)0, 0L, (long)-indent); (colnr_T)0, 0L, (long)-indent, 0);
} }
} }
curwin->w_cursor.lnum--; curwin->w_cursor.lnum--;

View File

@@ -21,7 +21,7 @@ void ex_clearjumps(exarg_T *eap);
void ex_changes(exarg_T *eap); void ex_changes(exarg_T *eap);
void mark_adjust(linenr_T line1, linenr_T line2, long amount, long amount_after); void mark_adjust(linenr_T line1, linenr_T line2, long amount, long amount_after);
void mark_adjust_nofold(linenr_T line1, linenr_T line2, long amount, long amount_after); void mark_adjust_nofold(linenr_T line1, linenr_T line2, long amount, long amount_after);
void mark_col_adjust(linenr_T lnum, colnr_T mincol, long lnum_amount, long col_amount); void mark_col_adjust(linenr_T lnum, colnr_T mincol, long lnum_amount, long col_amount, int spaces_removed);
void cleanup_jumplist(win_T *wp, int loadfiles); void cleanup_jumplist(win_T *wp, int loadfiles);
void copy_jumplist(win_T *from, win_T *to); void copy_jumplist(win_T *from, win_T *to);
void free_jumplist(win_T *wp); void free_jumplist(win_T *wp);

View File

@@ -450,5 +450,16 @@ func Test_format_undo()
\ ], getline(1, '$')) \ ], getline(1, '$'))
unmap gg unmap gg
set tw&
enew! enew!
endfunc endfunc
func Test_format_list_auto()
new
call setline(1, ['1. abc', '2. def', '3. ghi'])
set fo=tan ai bs=2
call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
call assert_equal('2. defx ghi', getline(2))
bwipe!
set fo& ai& bs&
endfunc

View File

@@ -799,6 +799,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
671,
/**/ /**/
670, 670,
/**/ /**/