On Fr, 25 Okt 2019, Christian Brabandt wrote:

> Bram,
> while looking into #4427 I noticed that there is some dead code, that 
> cannot be reached. So here is a patch:
> 
> diff --git a/src/textprop.c b/src/textprop.c
> index cc95546e2..7029daaae 100644
> --- a/src/textprop.c
> +++ b/src/textprop.c
> @@ -1067,13 +1067,7 @@ adjust_prop_columns(
>         if (bytes_added > 0
>                 && (tmp_prop.tp_col >= col + (start_incl ? 2 : 1)))
>         {
> -           if (tmp_prop.tp_col < col + (start_incl ? 2 : 1))
> -           {
> -               tmp_prop.tp_len += (tmp_prop.tp_col - 1 - col) + bytes_added;
> -               tmp_prop.tp_col = col + 1;
> -           }
> -           else
> -               tmp_prop.tp_col += bytes_added;
> +           tmp_prop.tp_col += bytes_added;
>             // Save for undo if requested and not done yet.
>             if ((flags & APC_SAVE_FOR_UNDO) && !dirty)
>                 u_savesub(lnum);
> 
> 
> BTW: I think the problem for #4427 is, that one uses the old byte column
> numbers for adjust_prop_columns(), while one should use the column
> numbers for the newly added text.

I think the attached patch fixes the problem.

I don't know text properties very well, so please verify.

Best,
Christian
-- 
Gescheit - gescheiter - gescheitert.

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20191025142648.GM26491%40256bit.org.
From e2ad33d64475bdddc7fb2b8491f8e769eabdcd6d Mon Sep 17 00:00:00 2001
From: Christian Brabandt <[email protected]>
Date: Fri, 25 Oct 2019 16:23:59 +0200
Subject: [PATCH] Adjust text property correctly for :s

---
 src/ex_cmds.c                 |  7 ++++++-
 src/testdir/test_textprop.vim | 28 ++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/ex_cmds.c b/src/ex_cmds.c
index e472c8a6f..807e06dc6 100644
--- a/src/ex_cmds.c
+++ b/src/ex_cmds.c
@@ -3856,6 +3856,7 @@ do_sub(exarg_T *eap)
 	    colnr_T	matchcol;
 	    colnr_T	prev_matchcol = MAXCOL;
 	    char_u	*new_end, *new_start = NULL;
+	    colnr_T	new_matchcol =  0;  // column offset newly replaced string
 	    unsigned	new_start_len = 0;
 	    char_u	*p1;
 	    int		did_sub = FALSE;
@@ -4281,11 +4282,15 @@ do_sub(exarg_T *eap)
 		    {
 			// When text properties are changed, need to save for
 			// undo first, unless done already.
-			if (adjust_prop_columns(lnum, regmatch.startpos[0].col,
+			if (adjust_prop_columns(lnum, new_matchcol + regmatch.startpos[0].col,
 			      sublen - 1 - (regmatch.endpos[0].col
 						   - regmatch.startpos[0].col),
 								    apc_flags))
 			    apc_flags &= ~APC_SAVE_FOR_UNDO;
+			// offset for column byte number of the text property 
+			// in the resulting buffer.
+			// If sublen = 4, this means we are adding 2 columns
+			new_matchcol += sublen - 2;
 		    }
 #endif
 		}
diff --git a/src/testdir/test_textprop.vim b/src/testdir/test_textprop.vim
index 54cf5f3c7..c369b54f9 100644
--- a/src/testdir/test_textprop.vim
+++ b/src/testdir/test_textprop.vim
@@ -866,3 +866,31 @@ func Test_textprop_in_unloaded_buf()
   cal delete('Xaaa')
   cal delete('Xbbb')
 endfunc
+
+func Test_proptype_substitute2()
+  new
+  " text_prop.vim
+  call setline(1, [
+        \ 'The number 123 is smaller than 4567.',
+        \ '123 The number 123 is smaller than 4567.',
+        \ '123 The number 123 is smaller than 4567.'])
+
+  call prop_type_add('number', {'highlight': 'ErrorMsg'})
+
+  call prop_add(1, 12, {'length': 3, 'type': 'number'})
+  call prop_add(2, 1, {'length': 3, 'type': 'number'})
+  call prop_add(3, 36, {'length': 4, 'type': 'number'})
+  set ul&
+  let expected = [{'id': 0, 'col': 16, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}, 
+        \ {'id': 0, 'col': 1, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}, 
+        \ {'id': 0, 'col': 50, 'end': 1, 'type': 'number', 'length': 4, 'start': 1}]
+  " Add some text in between
+  %s/\s/   /g
+  call assert_equal(expected, prop_list(1) + prop_list(2) + prop_list(3)) 
+
+  " remove some text
+  :1s/[a-z]\{3\}//g
+  let expected = [{'id': 0, 'col': 10, 'end': 1, 'type': 'number', 'length': 3, 'start': 1}]
+  call assert_equal(expected, prop_list(1))
+  bwipe!
+endfunc
-- 
2.20.1

Raspunde prin e-mail lui