comparison src/textprop.c @ 15255:19e79a1ed6b6 v8.1.0636

patch 8.1.0636: line2byte() gives wrong values with text properties commit https://github.com/vim/vim/commit/b413d2e6a8cc7b1611a41bfa9462b986393ca5fe Author: Bram Moolenaar <Bram@vim.org> Date: Tue Dec 25 23:15:46 2018 +0100 patch 8.1.0636: line2byte() gives wrong values with text properties Problem: line2byte() gives wrong values with text properties. (Bjorn Linse) Solution: Compute byte offsets differently when text properties were added. (closes #3718)
author Bram Moolenaar <Bram@vim.org>
date Tue, 25 Dec 2018 23:30:07 +0100
parents 17525ca95e1e
children 2d8225cc1315
comparison
equal deleted inserted replaced
15254:7790044a7348 15255:19e79a1ed6b6
15 * 15 *
16 * Text properties have a user specified ID number, which can be unique. 16 * Text properties have a user specified ID number, which can be unique.
17 * Text properties have a type, which can be used to specify highlighting. 17 * Text properties have a type, which can be used to specify highlighting.
18 * 18 *
19 * TODO: 19 * TODO:
20 * - mismatch in column 1 being the first column
21 * - Let props overrule syntax HL.
20 * - When deleting a line where a prop ended, adjust flag of previous line. 22 * - When deleting a line where a prop ended, adjust flag of previous line.
21 * - When deleting a line where a prop started, adjust flag of next line. 23 * - When deleting a line where a prop started, adjust flag of next line.
22 * - When inserting a line add props that continue from previous line. 24 * - When inserting a line add props that continue from previous line.
23 * - Adjust property column and length when text is inserted/deleted 25 * - Adjust property column and length when text is inserted/deleted
24 * - Add an arrray for global_proptypes, to quickly lookup a proptype by ID 26 * - Add an arrray for global_proptypes, to quickly lookup a proptype by ID
25 * - Add an arrray for b_proptypes, to quickly lookup a proptype by ID 27 * - Add an arrray for b_proptypes, to quickly lookup a proptype by ID
28 * - Also test line2byte() with many lines, so that ml_updatechunk() is taken
29 * into account.
26 * - add mechanism to keep track of changed lines. 30 * - add mechanism to keep track of changed lines.
27 */ 31 */
28 32
29 #include "vim.h" 33 #include "vim.h"
30 34
259 263
260 if (lnum == end_lnum) 264 if (lnum == end_lnum)
261 length = end_col - col + 1; 265 length = end_col - col + 1;
262 else 266 else
263 length = textlen - col + 1; 267 length = textlen - col + 1;
264 if (length > textlen) 268 if (length > (long)textlen)
265 length = textlen; // can include the end-of-line 269 length = textlen; // can include the end-of-line
266 if (length < 1) 270 if (length < 1)
267 length = 1; 271 length = 1;
268 272
269 // Allocate the new line with space for the new proprety. 273 // Allocate the new line with space for the new proprety.
306 buf->b_ml.ml_line_ptr = newtext; 310 buf->b_ml.ml_line_ptr = newtext;
307 buf->b_ml.ml_line_len += sizeof(textprop_T); 311 buf->b_ml.ml_line_len += sizeof(textprop_T);
308 buf->b_ml.ml_flags |= ML_LINE_DIRTY; 312 buf->b_ml.ml_flags |= ML_LINE_DIRTY;
309 } 313 }
310 314
315 buf->b_has_textprop = TRUE; // this is never reset
311 redraw_buf_later(buf, NOT_VALID); 316 redraw_buf_later(buf, NOT_VALID);
312 }
313
314 /*
315 * Return TRUE if any text properties are defined globally or for buffer
316 * "buf".
317 */
318 int
319 has_any_text_properties(buf_T *buf)
320 {
321 return buf->b_proptypes != NULL || global_proptypes != NULL;
322 } 317 }
323 318
324 /* 319 /*
325 * Fetch the text properties for line "lnum" in buffer "buf". 320 * Fetch the text properties for line "lnum" in buffer "buf".
326 * Returns the number of text properties and, when non-zero, a pointer to the 321 * Returns the number of text properties and, when non-zero, a pointer to the
332 { 327 {
333 char_u *text; 328 char_u *text;
334 size_t textlen; 329 size_t textlen;
335 size_t proplen; 330 size_t proplen;
336 331
337 // Be quick when no text property types are defined. 332 // Be quick when no text property types have been defined or the buffer,
338 if (!has_any_text_properties(buf)) 333 // unless we are adding one.
334 if (!buf->b_has_textprop && !will_change)
339 return 0; 335 return 0;
340 336
341 // Fetch the line to get the ml_line_len field updated. 337 // Fetch the line to get the ml_line_len field updated.
342 text = ml_get_buf(buf, lnum, will_change); 338 text = ml_get_buf(buf, lnum, will_change);
343 textlen = STRLEN(text) + 1; 339 textlen = STRLEN(text) + 1;