Skip to content

Commit 59ff640

Browse files
committed
patch 8.2.2429: :goto does not work correctly with text properties
Problem: :goto does not work correctly with text properties. (Sam McCall) Solution: Add a test. (Andrew Radev) Also use the text property size when computing the remaining offset. (closes #5930)
1 parent 92e5df8 commit 59ff640

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

src/memline.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5701,6 +5701,10 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57015701

57025702
while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset))
57035703
{
5704+
#ifdef FEAT_PROP_POPUP
5705+
size_t textprop_total = 0;
5706+
#endif
5707+
57045708
if (curline > buf->b_ml.ml_line_count
57055709
|| (hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
57065710
return -1;
@@ -5722,18 +5726,16 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57225726
}
57235727
else
57245728
{
5725-
#ifdef FEAT_PROP_POPUP
5726-
size_t textprop_total = 0;
5727-
size_t textprop_size = 0;
5728-
char_u *l1, *l2;
5729-
#endif
5730-
57315729
extra = 0;
57325730
for (;;)
57335731
{
57345732
#ifdef FEAT_PROP_POPUP
5733+
size_t textprop_size = 0;
5734+
57355735
if (buf->b_has_textprop)
57365736
{
5737+
char_u *l1, *l2;
5738+
57375739
// compensate for the extra bytes taken by textprops
57385740
l1 = (char_u *)dp + ((dp->db_index[idx]) & DB_INDEX_MASK);
57395741
l2 = (char_u *)dp + (idx == 0 ? dp->db_txt_end
@@ -5763,20 +5765,26 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57635765
}
57645766
}
57655767
#ifdef FEAT_PROP_POPUP
5766-
if (buf->b_has_textprop)
5768+
if (buf->b_has_textprop && lnum != 0)
57675769
{
57685770
int i;
57695771

57705772
// cannot use the db_index pointer, need to get the actual text
57715773
// lengths.
57725774
len = 0;
57735775
for (i = start_idx; i <= idx; ++i)
5774-
len += (int)STRLEN((char_u *)dp
5775-
+ ((dp->db_index[i]) & DB_INDEX_MASK)) + 1;
5776+
{
5777+
char_u *p = (char_u *)dp + ((dp->db_index[i]) & DB_INDEX_MASK);
5778+
len += (int)STRLEN(p) + 1;
5779+
}
57765780
}
57775781
else
57785782
#endif
5779-
len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK);
5783+
len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK)
5784+
#ifdef FEAT_PROP_POPUP
5785+
- (long)textprop_total
5786+
#endif
5787+
;
57805788
size += len;
57815789
if (offset != 0 && size >= offset)
57825790
{
@@ -5786,7 +5794,11 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57865794
*offp = offset - size + len;
57875795
else
57885796
*offp = offset - size + len
5789-
- (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK));
5797+
- (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK))
5798+
#ifdef FEAT_PROP_POPUP
5799+
+ (long)textprop_total
5800+
#endif
5801+
;
57905802
curline += idx - start_idx + extra;
57915803
if (curline > buf->b_ml.ml_line_count)
57925804
return -1; // exactly one byte beyond the end

src/testdir/test_textprop.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,36 @@ func Test_prop_byte2line()
771771
call prop_type_delete('prop')
772772
endfunc
773773

774+
func Test_prop_goto_byte()
775+
new
776+
call setline(1, '')
777+
call setline(2, 'two three')
778+
call setline(3, '')
779+
call setline(4, 'four five')
780+
781+
call prop_type_add('testprop', {'highlight': 'Directory'})
782+
call search('^two')
783+
call prop_add(line('.'), col('.'), {
784+
\ 'length': len('two'),
785+
\ 'type': 'testprop'
786+
\ })
787+
788+
call search('two \zsthree')
789+
let expected_pos = line2byte(line('.')) + col('.') - 1
790+
exe expected_pos .. 'goto'
791+
let actual_pos = line2byte(line('.')) + col('.') - 1
792+
eval actual_pos->assert_equal(expected_pos)
793+
794+
call search('four \zsfive')
795+
let expected_pos = line2byte(line('.')) + col('.') - 1
796+
exe expected_pos .. 'goto'
797+
let actual_pos = line2byte(line('.')) + col('.') - 1
798+
eval actual_pos->assert_equal(expected_pos)
799+
800+
call prop_type_delete('testprop')
801+
bwipe!
802+
endfunc
803+
774804
func Test_prop_undo()
775805
new
776806
call prop_type_add('comment', {'highlight': 'Directory'})

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2429,
753755
/**/
754756
2428,
755757
/**/

0 commit comments

Comments
 (0)