patch 9.1.0494: Wrong matched text highlighted in pum with 'rightleft'

Problem:  Wrong matched text highlighted in pum with 'rightleft'.
Solution: Match using the original text instead of the reversed text.
          (zeertzjq)

closes: #15020

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/popupmenu.c b/src/popupmenu.c
index 79686c6..8a4b68f 100644
--- a/src/popupmenu.c
+++ b/src/popupmenu.c
@@ -421,46 +421,43 @@
 }
 
 /*
- * displays text on the popup menu with specific attributes.
+ * Computes attributes of text on the popup menu.
+ * Returns attributes for every cell, or NULL if all attributes are the same.
  */
-    static void
-pum_screen_put_with_attr(int row, int col, char_u *text, int textlen, hlf_T hlf)
+    static int *
+pum_compute_text_attrs(char_u *text, hlf_T hlf)
 {
     int		i;
     int		leader_len;
-    int		char_len;
-    int		cells;
+    int		char_cells;
     int		new_attr;
-    char_u	*rt_leader = NULL;
-    char_u	*match_leader = NULL;
     char_u	*ptr = text;
+    int		cell_idx = 0;
     garray_T	*ga = NULL;
+    int		*attrs = NULL;
     char_u	*leader = ins_compl_leader();
     int		in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
+    int		matched_start = FALSE;
+    int_u	char_pos = 0;
 
     if (leader == NULL || *leader == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
 	    || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
 		&& highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
-    {
-	screen_puts_len(text, textlen, row, col, highlight_attr[hlf]);
-	return;
-    }
+	return NULL;
 
-#ifdef FEAT_RIGHTLEFT
-    if (pum_rl)
-	rt_leader = reverse_text(leader);
-#endif
-    match_leader = rt_leader != NULL ? rt_leader : leader;
-    leader_len = (int)STRLEN(match_leader);
+    attrs = ALLOC_MULT(int, vim_strsize(text));
+    if (attrs == NULL)
+	return NULL;
+
+    leader_len = (int)STRLEN(leader);
 
     if (in_fuzzy)
-	ga = fuzzy_match_str_with_pos(text, match_leader);
+	ga = fuzzy_match_str_with_pos(text, leader);
+    else
+	matched_start = STRNCMP(text, leader, leader_len) == 0;
 
-    // Render text with proper attributes
-    while (*ptr != NUL && ptr < text + textlen)
+    while (*ptr != NUL)
     {
-	char_len = mb_ptr2len(ptr);
-	cells = mb_ptr2cells(ptr);
 	new_attr = highlight_attr[hlf];
 
 	if (ga != NULL)
@@ -468,15 +465,7 @@
 	    // Handle fuzzy matching
 	    for (i = 0; i < ga->ga_len; i++)
 	    {
-		int_u *match_pos = ((int_u *)ga->ga_data) + i;
-		int_u actual_char_pos = 0;
-		char_u *temp_ptr = text;
-		while (temp_ptr < ptr)
-		{
-		    temp_ptr += mb_ptr2len(temp_ptr);
-		    actual_char_pos++;
-		}
-		if (actual_char_pos == match_pos[0])
+		if (char_pos == ((int_u *)ga->ga_data)[i])
 		{
 		    new_attr = highlight_attr[hlf == HLF_PSI
 							? HLF_PMSI : HLF_PMNI];
@@ -484,13 +473,16 @@
 		}
 	    }
 	}
-	else if (!in_fuzzy && (ptr - text < leader_len)
-			     && (STRNCMP(text, match_leader, leader_len) == 0))
+	else if (matched_start && ptr < text + leader_len)
 	    new_attr = highlight_attr[hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI];
 
-	screen_puts_len(ptr, char_len, row, col, new_attr);
-	col += cells;
-	ptr += char_len;
+	char_cells = mb_ptr2cells(ptr);
+	for (i = 0; i < char_cells; i++)
+	    attrs[cell_idx + i] = new_attr;
+	cell_idx += char_cells;
+
+	MB_PTR_ADV(ptr);
+	char_pos++;
     }
 
     if (ga != NULL)
@@ -498,8 +490,40 @@
 	ga_clear(ga);
 	vim_free(ga);
     }
-    if (rt_leader)
-	vim_free(rt_leader);
+    return attrs;
+}
+
+/*
+ * Displays text on the popup menu with specific attributes.
+ */
+    static void
+pum_screen_puts_with_attrs(
+    int		row,
+    int		col,
+    int		cells UNUSED,
+    char_u	*text,
+    int		textlen,
+    int		*attrs)
+{
+    int		col_start = col;
+    char_u	*ptr = text;
+    int		char_len;
+    int		attr;
+
+    // Render text with proper attributes
+    while (*ptr != NUL && ptr < text + textlen)
+    {
+	char_len = mb_ptr2len(ptr);
+#ifdef FEAT_RIGHTLEFT
+	if (pum_rl)
+	    attr = attrs[col_start + cells - col - 1];
+	else
+#endif
+	    attr = attrs[col - col_start];
+        screen_puts_len(ptr, char_len, row, col, attr);
+	col += mb_ptr2cells(ptr);
+	ptr += char_len;
+    }
 }
 
 /*
@@ -616,6 +640,7 @@
 			// Display the text that fits or comes before a Tab.
 			// First convert it to printable characters.
 			char_u	*st;
+			int	*attrs;
 			int	saved = *p;
 
 			if (saved != NUL)
@@ -623,6 +648,9 @@
 			st = transstr(s);
 			if (saved != NUL)
 			    *p = saved;
+
+			attrs = pum_compute_text_attrs(st, hlf);
+
 #ifdef FEAT_RIGHTLEFT
 			if (pum_rl)
 			{
@@ -633,19 +661,19 @@
 				if (rt != NULL)
 				{
 				    char_u	*rt_start = rt;
-				    int		size;
+				    int		cells;
 
-				    size = vim_strsize(rt);
-				    if (size > pum_width)
+				    cells = vim_strsize(rt);
+				    if (cells > pum_width)
 				    {
 					do
 					{
-					    size -= has_mbyte
-						    ? (*mb_ptr2cells)(rt) : 1;
+					    cells -= has_mbyte
+						     ? (*mb_ptr2cells)(rt) : 1;
 					    MB_PTR_ADV(rt);
-					} while (size > pum_width);
+					} while (cells > pum_width);
 
-					if (size < pum_width)
+					if (cells < pum_width)
 					{
 					    // Most left character requires
 					    // 2-cells but only 1 cell is
@@ -653,10 +681,18 @@
 					    // '<' on the left of the pum
 					    // item
 					    *(--rt) = '<';
-					    size++;
+					    cells++;
 					}
 				    }
-				    pum_screen_put_with_attr(row, col - size + 1, rt, (int)STRLEN(rt), hlf);
+
+				    if (attrs == NULL)
+					screen_puts_len(rt, (int)STRLEN(rt),
+						   row, col - cells + 1, attr);
+				    else
+					pum_screen_puts_with_attrs(row,
+						    col - cells + 1, cells, rt,
+						       (int)STRLEN(rt), attrs);
+
 				    vim_free(rt_start);
 				}
 				vim_free(st);
@@ -684,12 +720,20 @@
 				    else
 					--cells;
 				}
-				pum_screen_put_with_attr(row, col, st, size, hlf);
+
+				if (attrs == NULL)
+				    screen_puts_len(st, size, row, col, attr);
+				else
+				    pum_screen_puts_with_attrs(row, col, cells,
+							      st, size, attrs);
+
 				vim_free(st);
 			    }
 			    col += width;
 			}
 
+			vim_free(attrs);
+
 			if (*p != TAB)
 			    break;