diff src/popupwin.c @ 16811:0457d49eb2d9 v8.1.1407

patch 8.1.1407: popup_create() does not support text properties commit https://github.com/vim/vim/commit/7a8d0278bd6bd57e04f61183cb8e2969cf148e3f Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 26 23:32:06 2019 +0200 patch 8.1.1407: popup_create() does not support text properties Problem: Popup_create() does not support text properties. Solution: Support the third form of the text argument.
author Bram Moolenaar <Bram@vim.org>
date Sun, 26 May 2019 23:45:05 +0200
parents 5ff14f96f1c9
children 4cfad94161f4
line wrap: on
line diff
--- a/src/popupwin.c
+++ b/src/popupwin.c
@@ -60,6 +60,91 @@ apply_options(win_T *wp, buf_T *buf UNUS
 }
 
 /*
+ * Add lines to the popup from a list of strings.
+ */
+    static void
+add_popup_strings(buf_T *buf, list_T *l)
+{
+    listitem_T  *li;
+    linenr_T    lnum = 0;
+    char_u	*p;
+
+    for (li = l->lv_first; li != NULL; li = li->li_next)
+	if (li->li_tv.v_type == VAR_STRING)
+	{
+	    p = li->li_tv.vval.v_string;
+	    ml_append_buf(buf, lnum++,
+			       p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
+	}
+}
+
+/*
+ * Add lines to the popup from a list of dictionaries.
+ */
+    static void
+add_popup_dicts(buf_T *buf, list_T *l)
+{
+    listitem_T  *li;
+    listitem_T  *pli;
+    linenr_T    lnum = 0;
+    char_u	*p;
+    dict_T	*dict;
+
+    // first add the text lines
+    for (li = l->lv_first; li != NULL; li = li->li_next)
+    {
+	if (li->li_tv.v_type != VAR_DICT)
+	{
+	    emsg(_(e_dictreq));
+	    return;
+	}
+	dict = li->li_tv.vval.v_dict;
+	p = dict == NULL ? NULL
+			      : dict_get_string(dict, (char_u *)"text", FALSE);
+	ml_append_buf(buf, lnum++,
+			       p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
+    }
+
+    // add the text properties
+    lnum = 1;
+    for (li = l->lv_first; li != NULL; li = li->li_next, ++lnum)
+    {
+	dictitem_T	*di;
+	list_T		*plist;
+
+	dict = li->li_tv.vval.v_dict;
+	di = dict_find(dict, (char_u *)"props", -1);
+	if (di != NULL)
+	{
+	    if (di->di_tv.v_type != VAR_LIST)
+	    {
+		emsg(_(e_listreq));
+		return;
+	    }
+	    plist = di->di_tv.vval.v_list;
+	    if (plist != NULL)
+	    {
+		for (pli = plist->lv_first; pli != NULL; pli = pli->li_next)
+		{
+		    if (pli->li_tv.v_type != VAR_DICT)
+		    {
+			emsg(_(e_dictreq));
+			return;
+		    }
+		    dict = pli->li_tv.vval.v_dict;
+		    if (dict != NULL)
+		    {
+			int col = dict_get_number(dict, (char_u *)"col");
+
+			prop_add_common( lnum, col, dict, buf, NULL);
+		    }
+		}
+	    }
+	}
+    }
+}
+
+/*
  * popup_create({text}, {options})
  */
     void
@@ -128,27 +213,21 @@ f_popup_create(typval_T *argvars, typval
 
     // Add text to the buffer.
     if (argvars[0].v_type == VAR_STRING)
+    {
 	// just a string
 	ml_append_buf(buf, 0, argvars[0].vval.v_string, (colnr_T)0, TRUE);
-    else if (argvars[0].vval.v_list->lv_first->li_tv.v_type == VAR_STRING)
-    {
-	listitem_T  *li;
-	linenr_T    lnum = 0;
-	char_u	    *p;
-
-	// list of strings
-	for (li = argvars[0].vval.v_list->lv_first; li != NULL;
-							      li = li->li_next)
-	    if (li->li_tv.v_type == VAR_STRING)
-	    {
-		p = li->li_tv.vval.v_string;
-		ml_append_buf(buf, lnum++,
-			       p == NULL ? (char_u *)"" : p, (colnr_T)0, TRUE);
-	    }
     }
     else
-	// TODO: handle a list of dictionaries
-	emsg("Not implemented yet");
+    {
+	list_T *l = argvars[0].vval.v_list;
+
+	if (l->lv_first->li_tv.v_type == VAR_STRING)
+	    // list of strings
+	    add_popup_strings(buf, l);
+	else
+	    // list of dictionaries
+	    add_popup_dicts(buf, l);
+    }
 
     // Delete the line of the empty buffer.
     curbuf = buf;