[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15635] branches/soc-2008-quorn: Added a documentation panel with primitive word-wrap functionality.
Ian Thompson
quornian at googlemail.com
Sat Jul 19 01:12:21 CEST 2008
Revision: 15635
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15635
Author: quorn
Date: 2008-07-19 01:12:19 +0200 (Sat, 19 Jul 2008)
Log Message:
-----------
Added a documentation panel with primitive word-wrap functionality. It can be displayed by Text.showDoc(string) in python and has a text-plugin script for function docs which may be invoked with Ctrl+I inside its params list. Eg. type "dir(" <Ctrl+I>
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/src/drawtext.c
Added Paths:
-----------
branches/soc-2008-quorn/release/scripts/textplugin_functiondocs.py
Added: branches/soc-2008-quorn/release/scripts/textplugin_functiondocs.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_functiondocs.py (rev 0)
+++ branches/soc-2008-quorn/release/scripts/textplugin_functiondocs.py 2008-07-18 23:12:19 UTC (rev 15635)
@@ -0,0 +1,68 @@
+#!BPY
+"""
+Name: 'Function Documentation'
+Blender: 246
+Group: 'TextPlugin'
+Shortcut: 'Ctrl+I'
+Tooltip: 'Attempts to display documentation about the function preceding the cursor.'
+"""
+
+# Only run if we have the required modules
+try:
+ import bpy
+ from BPyTextPlugin import *
+ OK = True
+except ImportError:
+ OK = False
+
+def main():
+ txt = bpy.data.texts.active
+ if not txt:
+ return
+
+ (line, c) = current_line(txt)
+
+ # Check we are in a normal context
+ if get_context(txt) != NORMAL:
+ return
+
+ # Look backwards for first '(' without ')'
+ b = 0
+ for i in range(c-1, -1, -1):
+ if line[i] == ')': b += 1
+ elif line[i] == '(':
+ b -= 1
+ if b < 0:
+ c = i
+ break
+
+ pre = get_targets(line, c)
+
+ if len(pre) == 0:
+ return
+
+ imports = get_imports(txt)
+ builtins = get_builtins()
+
+ # Identify the root (root.sub.sub.)
+ if imports.has_key(pre[0]):
+ obj = imports[pre[0]]
+ elif builtins.has_key(pre[0]):
+ obj = builtins[pre[0]]
+ else:
+ return
+
+ # Step through sub-attributes
+ try:
+ for name in pre[1:]:
+ obj = getattr(obj, name)
+ except AttributeError:
+ print "Attribute not found '%s' in '%s'" % (name, '.'.join(pre))
+ return
+
+ if hasattr(obj, '__doc__') and obj.__doc__:
+ txt.showDocs(obj.__doc__)
+
+# Check we are running as a script and not imported as a module
+if __name__ == "__main__" and OK:
+ main()
Modified: branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h 2008-07-18 22:24:20 UTC (rev 15634)
+++ branches/soc-2008-quorn/source/blender/blenkernel/BKE_suggestions.h 2008-07-18 23:12:19 UTC (rev 15635)
@@ -60,20 +60,26 @@
SuggItem *selected;
} SuggList;
+/* Free all suggestion related memory */
void free_suggestions();
+/* Used to identify which Text object the current tools should appear against */
+void suggest_set_active(Text *text);
+void suggest_clear_active();
+short suggest_is_active(Text *text);
+
void suggest_add(const char *name, char type);
void suggest_prefix(const char *prefix);
SuggItem *suggest_first();
SuggItem *suggest_last();
-void suggest_set_text(Text *text);
-void suggest_clear_text();
-short suggest_is_active(Text *text);
-
void suggest_set_selected(SuggItem *sel);
SuggItem *suggest_get_selected();
+void suggest_documentation(const char *docs);
+char *suggest_get_docs();
+void suggest_clear_docs();
+
#ifdef __cplusplus
}
#endif
Modified: branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c
===================================================================
--- branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c 2008-07-18 22:24:20 UTC (rev 15634)
+++ branches/soc-2008-quorn/source/blender/blenkernel/intern/suggestions.c 2008-07-18 23:12:19 UTC (rev 15635)
@@ -37,20 +37,23 @@
#include "BKE_text.h"
#include "BKE_suggestions.h"
-static SuggList suggestions= {NULL, NULL, NULL, NULL, NULL};
+static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
static Text *suggText = NULL;
-static SuggItem *lastInsert= NULL;
+static SuggItem *lastInsert = NULL;
+static char *documentation = NULL;
+static int doc_lines = 0;
-static suggest_cmp(const char *first, const char *second, int len) {
+static int suggest_cmp(const char *first, const char *second, int len) {
int cmp, i;
for (cmp=0, i=0; i<len; i++) {
- if (cmp= toupper(first[i]) - toupper(second[i])) {
+ if (cmp= toupper(first[i])-toupper(second[i])) {
break;
}
}
return cmp;
}
-void free_suggestions() {
+
+static void sugg_free() {
SuggItem *item, *prev;
for (item = suggestions.last; item; item=prev) {
prev = item->prev;
@@ -61,6 +64,18 @@
suggestions.selected = NULL;
}
+static void docs_free() {
+ if (documentation) {
+ MEM_freeN(documentation);
+ documentation = NULL;
+ }
+}
+
+void free_suggestions() {
+ sugg_free();
+ docs_free();
+}
+
void suggest_add(const char *name, char type) {
SuggItem *newitem;
@@ -87,7 +102,7 @@
void suggest_prefix(const char *prefix) {
SuggItem *match, *first, *last;
- int cmp, len = strlen(prefix), i;
+ int cmp, len = strlen(prefix);
if (!suggestions.first) return;
if (len==0) {
@@ -111,10 +126,13 @@
}
if (first) {
if (!last) last = suggestions.last;
- suggestions.selected = suggestions.firstmatch = first;
+ suggestions.firstmatch = first;
suggestions.lastmatch = last;
+ suggestions.selected = first;
} else {
- suggestions.firstmatch = suggestions.lastmatch = NULL;
+ suggestions.firstmatch = NULL;
+ suggestions.lastmatch = NULL;
+ suggestions.selected = NULL;
}
}
@@ -126,11 +144,13 @@
return suggestions.lastmatch;
}
-void suggest_set_text(Text *text) {
+void suggest_set_active(Text *text) {
+ if (suggText == text) return;
+ suggest_clear_active();
suggText = text;
}
-void suggest_clear_text() {
+void suggest_clear_active() {
free_suggestions();
suggText = NULL;
}
@@ -146,3 +166,37 @@
SuggItem *suggest_get_selected() {
return suggestions.selected;
}
+
+/* Documentation methods */
+
+void suggest_documentation(const char *docs) {
+ int len;
+
+ if (!docs) return;
+
+ len = strlen(docs);
+
+ if (documentation) {
+ MEM_freeN(documentation);
+ documentation = NULL;
+ }
+
+ /* Ensure documentation ends with a '\n' */
+ if (docs[len-1] != '\n') {
+ documentation = MEM_mallocN(len+2, "Documentation");
+ strncpy(documentation, docs, len);
+ documentation[len++] = '\n';
+ } else {
+ documentation = MEM_mallocN(len+1, "Documentation");
+ strncpy(documentation, docs, len);
+ }
+ documentation[len] = '\0';
+}
+
+char *suggest_get_docs() {
+ return documentation;
+}
+
+void suggest_clear_docs() {
+ docs_free();
+}
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-18 22:24:20 UTC (rev 15634)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c 2008-07-18 23:12:19 UTC (rev 15635)
@@ -103,6 +103,7 @@
static PyObject *Text_getCursorPos( BPy_Text * self );
static PyObject *Text_setCursorPos( BPy_Text * self, PyObject * args );
static PyObject *Text_suggest( BPy_Text * self, PyObject * args );
+static PyObject *Text_showDocs( BPy_Text * self, PyObject * args );
/*****************************************************************************/
/* Python BPy_Text methods table: */
@@ -136,7 +137,9 @@
{"setCursorPos", ( PyCFunction ) Text_setCursorPos, METH_VARARGS,
"(row, col) - Set the cursor position to (row, col)"},
{"suggest", ( PyCFunction ) Text_suggest, METH_VARARGS,
- "(list) - 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) - List of tuples of the form (name, type) where type is one of 'm', 'v', 'f', 'k' for module, variable, function and keyword respectively"},
+ {"showDocs", ( PyCFunction ) Text_showDocs, METH_VARARGS,
+ "(docs) - Documentation string"},
{NULL, NULL, 0, NULL}
};
@@ -548,7 +551,7 @@
int row, col;
int oldstate;
- if(!self->text)
+ if (!self->text)
return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"This object isn't linked to a Blender Text Object");
@@ -573,12 +576,12 @@
char *prefix, *name, type;
SpaceText *st;
- if(!self->text)
+ 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))
+ if (!PyArg_ParseTuple(args, "O!s", &PyList_Type, &list, &prefix))
return EXPP_ReturnPyObjError(PyExc_TypeError,
"expected list of tuples followed by a string");
@@ -591,8 +594,8 @@
return EXPP_ReturnPyObjError(PyExc_RuntimeError,
"Active text area has no Text object");
+ suggest_set_active(st->text);
list_len = PyList_Size(list);
- suggest_clear_text();
for (i = 0; i < list_len; i++) {
item = PyList_GetItem(list, i);
@@ -610,12 +613,40 @@
suggest_add(name, type);
}
suggest_prefix(prefix);
- suggest_set_text(st->text);
scrarea_queue_redraw(curarea);
Py_RETURN_NONE;
}
+static PyObject *Text_showDocs( BPy_Text * self, PyObject * args )
+{
+ char *docs;
+ SpaceText *st;
+
+ if (!self->text)
+ return EXPP_ReturnPyObjError(PyExc_RuntimeError,
+ "This object isn't linked to a Blender Text Object");
+
+ if (!PyArg_ParseTuple(args, "s", &docs))
+ return EXPP_ReturnPyObjError( PyExc_TypeError,
+ "expected a string as argument" );
+
+ if (curarea->spacetype != SPACE_TEXT)
+ return EXPP_ReturnPyObjError(PyExc_RuntimeError,
+ "Active space type is not text");
+
+ st = curarea->spacedata.first;
+ if (!st || !st->text)
+ return EXPP_ReturnPyObjError(PyExc_RuntimeError,
+ "Active text area has no Text object");
+
+ suggest_set_active(st->text);
+ suggest_documentation(docs);
+ scrarea_queue_redraw(curarea);
+
+ Py_RETURN_NONE;
+}
+
/*****************************************************************************/
/* Function: Text_compare */
/* Description: This is a callback function for the BPy_Text type. It */
Modified: branches/soc-2008-quorn/source/blender/src/drawtext.c
===================================================================
--- branches/soc-2008-quorn/source/blender/src/drawtext.c 2008-07-18 22:24:20 UTC (rev 15634)
+++ branches/soc-2008-quorn/source/blender/src/drawtext.c 2008-07-18 23:12:19 UTC (rev 15635)
@@ -87,11 +87,15 @@
#include "blendef.h"
#include "winlay.h"
-#define TEXTXLOC 38
+#define TEXTXLOC 38
-#define SUGG_LIST_SIZE 7
-#define SUGG_LIST_WIDTH 20
+#define SUGG_LIST_SIZE 7
+#define SUGG_LIST_WIDTH 20
+#define DOC_WIDTH 40
+#define TOOL_SUGG_LIST 0x01
+#define TOOL_DOCUMENT 0x02
+
/* forward declarations */
void drawtextspace(ScrArea *sa, void *spacedata);
@@ -106,6 +110,7 @@
static int check_builtinfuncs(char *string);
static int check_specialvars(char *string);
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list