Skip to content

Commit 14c7530

Browse files
committed
patch 8.2.3348: line2byte() returns wrong value after adding textprop
Problem: line2byte() returns wrong value after adding textprop. (Yuto Kimura) Solution: Reduce the length by the size of the text property. (closes #8759)
1 parent dd9de50 commit 14c7530

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/memline.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,6 +3977,9 @@ ml_flush_line(buf_T *buf)
39773977
*/
39783978
if ((int)dp->db_free >= extra)
39793979
{
3980+
#ifdef FEAT_BYTEOFF
3981+
int old_prop_len = 0;
3982+
#endif
39803983
// if the length changes and there are following lines
39813984
count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1;
39823985
if (extra != 0 && idx < count - 1)
@@ -3995,13 +3998,24 @@ ml_flush_line(buf_T *buf)
39953998
// adjust free space
39963999
dp->db_free -= extra;
39974000
dp->db_txt_start -= extra;
4001+
#ifdef FEAT_BYTEOFF
4002+
if (buf->b_has_textprop)
4003+
old_prop_len = old_len - STRLEN(new_line) - 1;
4004+
#endif
39984005

39994006
// copy new line into the data block
40004007
mch_memmove(old_line - extra, new_line, (size_t)new_len);
40014008
buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
40024009
#ifdef FEAT_BYTEOFF
40034010
// The else case is already covered by the insert and delete
4004-
ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE);
4011+
if (buf->b_has_textprop)
4012+
{
4013+
// Do not count the size of any text properties.
4014+
extra += old_prop_len;
4015+
extra -= new_len - STRLEN(new_line) - 1;
4016+
}
4017+
if (extra != 0)
4018+
ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE);
40054019
#endif
40064020
}
40074021
else
@@ -5595,7 +5609,7 @@ ml_updatechunk(
55955609
else
55965610
#endif
55975611
{
5598-
if (idx == 0)// first line in block, text at the end
5612+
if (idx == 0) // first line in block, text at the end
55995613
text_end = dp->db_txt_end;
56005614
else
56015615
text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
@@ -5734,7 +5748,7 @@ ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
57345748
return 1; // Not a "find offset" and offset 0 _must_ be in line 1
57355749
/*
57365750
* Find the last chunk before the one containing our line. Last chunk is
5737-
* special because it will never qualify
5751+
* special because it will never qualify.
57385752
*/
57395753
curline = 1;
57405754
curix = size = 0;

src/testdir/test_textprop.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,18 @@ func Test_prop_line2byte()
809809
call assert_equal(19, line2byte(3))
810810
call prop_add(1, 1, {'end_col': 3, 'type': 'comment'})
811811
call assert_equal(19, line2byte(3))
812+
bwipe!
812813

814+
new
815+
call setline(1, range(500))
816+
call assert_equal(1491, line2byte(401))
817+
call prop_add(2, 1, {'type': 'comment'})
818+
call prop_add(222, 1, {'type': 'comment'})
819+
call assert_equal(1491, line2byte(401))
820+
call prop_remove({'type': 'comment'})
821+
call assert_equal(1491, line2byte(401))
813822
bwipe!
823+
814824
call prop_type_delete('comment')
815825
endfunc
816826

src/version.c

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

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3348,
758760
/**/
759761
3347,
760762
/**/

0 commit comments

Comments
 (0)