Skip to content

Commit 9df53b6

Browse files
committed
patch 8.2.0115: byte2line() does not work correctly with text properties
Problem: Byte2line() does not work correctly with text properties. (Billie Cleek) Solution: Take the bytes of the text properties into account. (closes #5334)
1 parent 7ebcba6 commit 9df53b6

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/memline.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5738,7 +5738,7 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57385738
count = (long)(buf->b_ml.ml_locked_high) -
57395739
(long)(buf->b_ml.ml_locked_low) + 1;
57405740
start_idx = idx = curline - buf->b_ml.ml_locked_low;
5741-
if (idx == 0)// first line in block, text at the end
5741+
if (idx == 0) // first line in block, text at the end
57425742
text_end = dp->db_txt_end;
57435743
else
57445744
text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
@@ -5752,13 +5752,38 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57525752
}
57535753
else
57545754
{
5755+
#ifdef FEAT_PROP_POPUP
5756+
long textprop_total = 0;
5757+
long textprop_size = 0;
5758+
char_u *l1, *l2;
5759+
#endif
5760+
57555761
extra = 0;
5756-
while (offset >= size
5757-
+ text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK)
5758-
+ ffdos)
5762+
for (;;)
57595763
{
5764+
#ifdef FEAT_PROP_POPUP
5765+
if (buf->b_has_textprop)
5766+
{
5767+
// compensate for the extra bytes taken by textprops
5768+
l1 = (char_u *)dp + ((dp->db_index[idx]) & DB_INDEX_MASK);
5769+
l2 = (char_u *)dp + (idx == 0 ? dp->db_txt_end
5770+
: ((dp->db_index[idx - 1]) & DB_INDEX_MASK));
5771+
textprop_size = (l2 - l1) - (STRLEN(l1) + 1);
5772+
}
5773+
#endif
5774+
if (!(offset >= size
5775+
+ text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK)
5776+
#ifdef FEAT_PROP_POPUP
5777+
- textprop_total - textprop_size
5778+
#endif
5779+
+ ffdos))
5780+
break;
5781+
57605782
if (ffdos)
57615783
size++;
5784+
#ifdef FEAT_PROP_POPUP
5785+
textprop_total += textprop_size;
5786+
#endif
57625787
if (idx == count - 1)
57635788
{
57645789
extra = 1;
@@ -5776,7 +5801,8 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57765801
// lengths.
57775802
len = 0;
57785803
for (i = start_idx; i <= idx; ++i)
5779-
len += (int)STRLEN((char_u *)dp + ((dp->db_index[i]) & DB_INDEX_MASK)) + 1;
5804+
len += (int)STRLEN((char_u *)dp
5805+
+ ((dp->db_index[i]) & DB_INDEX_MASK)) + 1;
57805806
}
57815807
else
57825808
#endif

src/testdir/test_textprop.vim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func Test_prop_multiline()
664664
call prop_type_delete('comment')
665665
endfunc
666666

667-
func Test_prop_byteoff()
667+
func Test_prop_line2byte()
668668
call prop_type_add('comment', {'highlight': 'Directory'})
669669
new
670670
call setline(1, ['line1', 'second line', ''])
@@ -677,6 +677,22 @@ func Test_prop_byteoff()
677677
call prop_type_delete('comment')
678678
endfunc
679679

680+
func Test_prop_byte2line()
681+
new
682+
set ff=unix
683+
call setline(1, ['one one', 'two two', 'three three', 'four four', 'five'])
684+
call assert_equal(4, byte2line(line2byte(4)))
685+
call assert_equal(5, byte2line(line2byte(5)))
686+
687+
call prop_type_add('prop', {'highlight': 'Directory'})
688+
call prop_add(3, 1, {'length': 5, 'type': 'prop'})
689+
call assert_equal(4, byte2line(line2byte(4)))
690+
call assert_equal(5, byte2line(line2byte(5)))
691+
692+
bwipe!
693+
call prop_type_delete('prop')
694+
endfunc
695+
680696
func Test_prop_undo()
681697
new
682698
call prop_type_add('comment', {'highlight': 'Directory'})

src/version.c

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

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
115,
745747
/**/
746748
114,
747749
/**/

0 commit comments

Comments
 (0)