[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15674] branches/soc-2008-quorn/source/ blender: Removed requirement for suggestions to be pre-sorted.

Ian Thompson quornian at googlemail.com
Mon Jul 21 18:40:39 CEST 2008


Revision: 15674
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15674
Author:   quorn
Date:     2008-07-21 18:40:32 +0200 (Mon, 21 Jul 2008)

Log Message:
-----------
Removed requirement for suggestions to be pre-sorted. Allowed lists of strings to be suggested without having to specify their type. Specifying a prefix when suggesting is now also optional.

Modified Paths:
--------------
    branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h
    branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c
    branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
    branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py
    branches/soc-2008-quorn/source/blender/src/drawtext.c

Modified: branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h	2008-07-21 15:56:42 UTC (rev 15673)
+++ branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h	2008-07-21 16:40:32 UTC (rev 15674)
@@ -34,8 +34,10 @@
 #endif
 
 /* ****************************************************************************
-Suggestions must be added in sorted order (no attempt is made to sort the list)
-The list is then divided up based on the prefix provided by update_suggestions:
+Suggestions should be added in sorted order although a linear sorting method is
+implemented. The list is then divided up based on the prefix provided by
+update_suggestions:
+
 Example:
   Prefix: ab
   aaa <-- first
@@ -70,6 +72,7 @@
 
 void suggest_add(const char *name, char type);
 void suggest_prefix(const char *prefix);
+void suggest_clear_list();
 SuggItem *suggest_first();
 SuggItem *suggest_last();
 

Modified: branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c	2008-07-21 15:56:42 UTC (rev 15673)
+++ branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c	2008-07-21 16:40:32 UTC (rev 15674)
@@ -37,6 +37,10 @@
 #include "BKE_text.h"
 #include "BKE_suggestions.h"
 
+/**********************/
+/* Static definitions */
+/**********************/
+
 static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
 static Text *suggText = NULL;
 static SuggItem *lastInsert = NULL;
@@ -71,13 +75,37 @@
 	}
 }
 
+/**************************/
+/* General tool functions */
+/**************************/
+
 void free_suggestions() {
 	sugg_free();
 	docs_free();
 }
 
+void suggest_set_active(Text *text) {
+	if (suggText == text) return;
+	suggest_clear_active();
+	suggText = text;
+}
+
+void suggest_clear_active() {
+	free_suggestions();
+	suggText = NULL;
+}
+
+short suggest_is_active(Text *text) {
+	return suggText==text ? 1 : 0;
+}
+
+/***************************/
+/* Suggestion list methods */
+/***************************/
+
 void suggest_add(const char *name, char type) {
-	SuggItem *newitem;
+	SuggItem *newitem, *item;
+	int len, cmp;
 
 	newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
 	if (!newitem) {
@@ -86,18 +114,42 @@
 	}
 
 	newitem->name = (char *) (newitem + 1);
-	strcpy(newitem->name, name);
+	len = strlen(name);
+	strncpy(newitem->name, name, len);
+	newitem->name[len] = '\0';
 	newitem->type = type;
 	newitem->prev = newitem->next = NULL;
 
-	if (!suggestions.first) {
+	/* Perform simple linear search for ordered storage */
+	if (!suggestions.first || !suggestions.last) {
 		suggestions.first = suggestions.last = newitem;
 	} else {
-		newitem->prev = suggestions.last;
-		suggestions.last->next = newitem;
-		suggestions.last = newitem;
+		cmp = -1;
+		for (item=suggestions.last; item; item=item->prev) {
+			cmp = suggest_cmp(name, item->name, len);
+
+			/* Newitem comes after this item, insert here */
+			if (cmp >= 0) {
+				newitem->prev = item;
+				if (item->next)
+					item->next->prev = newitem;
+				newitem->next = item->next;
+				item->next = newitem;
+
+				/* At last item, set last pointer here */
+				if (item == suggestions.last)
+					suggestions.last = newitem;
+				break;
+			}
+		}
+		/* Reached beginning of list, insert before first */
+		if (cmp < 0) {
+			newitem->next = suggestions.first;
+			suggestions.first->prev = newitem;
+			suggestions.first = newitem;
+		}
 	}
-	suggestions.selected = NULL;
+	suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL;
 }
 
 void suggest_prefix(const char *prefix) {
@@ -136,6 +188,10 @@
 	}
 }
 
+void suggest_clear_list() {
+	sugg_free();
+}
+
 SuggItem *suggest_first() {
 	return suggestions.firstmatch;
 }
@@ -144,30 +200,17 @@
 	return suggestions.lastmatch;
 }
 
-void suggest_set_active(Text *text) {
-	if (suggText == text) return;
-	suggest_clear_active();
-	suggText = text;
+SuggItem *suggest_get_selected() {
+	return suggestions.selected;
 }
 
-void suggest_clear_active() {
-	free_suggestions();
-	suggText = NULL;
-}
-
-short suggest_is_active(Text *text) {
-	return suggText==text ? 1 : 0;
-}
-
 void suggest_set_selected(SuggItem *sel) {
 	suggestions.selected = sel;
 }
 
-SuggItem *suggest_get_selected() {
-	return suggestions.selected;
-}
-
+/*************************/
 /* Documentation methods */
+/*************************/
 
 void suggest_documentation(const char *docs) {
 	int len;

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-07-21 15:56:42 UTC (rev 15673)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-07-21 16:40:32 UTC (rev 15674)
@@ -137,7 +137,7 @@
 	{"setCursorPos", ( PyCFunction ) Text_setCursorPos, METH_VARARGS,
 	 "(row, col) - Set the cursor position to (row, col)"},
 	{"suggest", ( PyCFunction ) Text_suggest, METH_VARARGS,
-	 "(list, prefix) - List of tuples of the form (name, type) where type is one of 'm', 'v', 'f', 'k' for module, variable, function and keyword respectively"},
+	 "(list, prefix='') - Presents a list of suggestions. List is of strings, or tuples. Tuples must be of the form (name, type) where type is one of 'm', 'v', 'f', 'k' for module, variable, function and keyword respectively or '?' for other types"},
 	{"showDocs", ( PyCFunction ) Text_showDocs, METH_VARARGS,
 	 "(docs) - Documentation string"},
 	{NULL, NULL, 0, NULL}
@@ -574,20 +574,20 @@
 
 static PyObject *Text_suggest( BPy_Text * self, PyObject * args )
 {
-	PyObject *item = NULL;
+	PyObject *item = NULL, *tup1 = NULL, *tup2 = NULL;
 	PyObject *list = NULL, *resl = NULL;
 	int list_len, i;
-	char *prefix, *name, type;
+	char *prefix = NULL, *name, type;
 	SpaceText *st;
 
 	if (!self->text)
 		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
 				"This object isn't linked to a Blender Text Object");
 
-	/* Parse args for a list of tuples */
-	if (!PyArg_ParseTuple(args, "O!s", &PyList_Type, &list, &prefix))
+	/* Parse args for a list of strings/tuples */
+	if (!PyArg_ParseTuple(args, "O!|s", &PyList_Type, &list, &prefix))
 		return EXPP_ReturnPyObjError(PyExc_TypeError,
-				"expected list of tuples followed by a string");
+				"expected list of strings or tuples followed by an optional string");
 
 	if (curarea->spacetype != SPACE_TEXT)
 		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
@@ -598,24 +598,37 @@
 		return EXPP_ReturnPyObjError(PyExc_RuntimeError,
 				"Active text area has no Text object");
 	
+	suggest_clear_list();
 	suggest_set_active(st->text);
 	list_len = PyList_Size(list);
 	
 	for (i = 0; i < list_len; i++) {
 		item = PyList_GetItem(list, i);
-		if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2)
+
+		if (PyString_Check(item)) {
+			name = PyString_AsString(item);
+			type = '?';
+		} else if (PyTuple_Check(item) && PyTuple_GET_SIZE(item) == 2) {
+			tup1 = PyTuple_GetItem(item, 0);
+			tup2 = PyTuple_GetItem(item, 1);
+			if (PyString_Check(tup1) && PyString_Check(tup2)) {
+				name = PyString_AsString(tup1);
+				type = PyString_AsString(tup2)[0];
+			} else
+				return EXPP_ReturnPyObjError(PyExc_AttributeError,
+						"list must contain tuples of two strings only: (name, type)" );
+		} else
 			return EXPP_ReturnPyObjError(PyExc_AttributeError,
-					"list must contain only tuples of size 2" );
+					"list must contain only individual strings or tuples of size 2" );
 
-		name = PyString_AsString(PyTuple_GetItem(item, 0));
-		type = PyString_AsString(PyTuple_GetItem(item, 1))[0];
-
-		if (!strlen(name) || (type!='m' && type!='v' && type!='f' && type!='k'))
+		if (!strlen(name) || (type!='m' && type!='v' && type!='f' && type!='k' && type!='?'))
 			return EXPP_ReturnPyObjError(PyExc_AttributeError,
-					"names must be non-empty and types in ['m', 'v', 'f', 'k']" );
+					"names must be non-empty and types in ['m', 'v', 'f', 'k', '?']" );
 
 		suggest_add(name, type);
 	}
+	if (!prefix)
+		prefix = "";
 	suggest_prefix(prefix);
 	scrarea_queue_redraw(curarea);
 

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-07-21 15:56:42 UTC (rev 15673)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-07-21 16:40:32 UTC (rev 15674)
@@ -162,15 +162,32 @@
 			cursor.
 		"""
 
-	def suggest(list):
+	def suggest(list, prefix=''):
 		"""
-		Set the suggestion list to the given list of tuples. This list *must* be
-		sorted by its first element, name.
-		@type list: list of tuples
+		Suggest a list of names. If list is a list of tuples (name, type) the
+		list will be formatted to syntax-highlight each entry type. Types must
+		be strings in the list ['m', 'f', 'v', 'k', '?']. It is recommended that
+		the list be sorted, case-insensitively by name.
+		
+		@type list: list of tuples or strings
 		@param list:  List of pair-tuples of the form (name, type) where name is
 			the suggested name and type is one of 'm' (module or class), 'f'
-			(function or method), 'v' (variable).
+			(function or method), 'v' (variable), 'k' (keyword), '?' (other).
+			Lists of plain strings are also accepted where the type is always
+			'?'.
+		@type prefix: string
+		@param prefix: The optional prefix used to limit what is suggested from
+			the list. This is usually whatever precedes the cursor so that
+			backspace will update it.
 		"""
+	
+	def showDocs(docs):
+		"""
+		Displays a word-wrapped message box containing the specified
+		documentation when this Text object is visible.
+		@type docs: string
+		@param docs: The documentation string to display.
+		"""
 
 import id_generics
 Text.__doc__ += id_generics.attributes

Modified: branches/soc-2008-quorn/source/blender/src/drawtext.c
===================================================================
--- branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-07-21 15:56:42 UTC (rev 15673)
+++ branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-07-21 16:40:32 UTC (rev 15674)
@@ -1203,6 +1203,7 @@
 			case 'm': BIF_ThemeColor(TH_TEXT); break;
 			case 'f': BIF_ThemeColor(TH_SYNTAX_L); break;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list