[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53467] trunk/blender: add back initial autocomplete support.

Campbell Barton ideasman42 at gmail.com
Mon Dec 31 16:11:39 CET 2012


Revision: 53467
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53467
Author:   campbellbarton
Date:     2012-12-31 15:11:36 +0000 (Mon, 31 Dec 2012)
Log Message:
-----------
add back initial autocomplete support.

- This doesnt use python as 2.4x did, instead it just autocompletes based on the text files unique identifiers so its useful for any language.
- key is same as console (Ctrl+Space)

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_text.py
    trunk/blender/source/blender/editors/space_text/space_text.c
    trunk/blender/source/blender/editors/space_text/text_autocomplete.c
    trunk/blender/source/blender/editors/space_text/text_intern.h

Modified: trunk/blender/release/scripts/startup/bl_ui/space_text.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_text.py	2012-12-31 14:56:07 UTC (rev 53466)
+++ trunk/blender/release/scripts/startup/bl_ui/space_text.py	2012-12-31 15:11:36 UTC (rev 53467)
@@ -301,6 +301,7 @@
 
         layout.operator("text.jump")
         layout.operator("text.properties", text="Find...")
+        layout.operator("text.autocomplete")
 
         layout.separator()
 

Modified: trunk/blender/source/blender/editors/space_text/space_text.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/space_text.c	2012-12-31 14:56:07 UTC (rev 53466)
+++ trunk/blender/source/blender/editors/space_text/space_text.c	2012-12-31 15:11:36 UTC (rev 53467)
@@ -223,6 +223,8 @@
 	WM_operatortype_append(TEXT_OT_to_3d_object);
 
 	WM_operatortype_append(TEXT_OT_resolve_conflict);
+
+	WM_operatortype_append(TEXT_OT_autocomplete);
 }
 
 static void text_keymap(struct wmKeyConfig *keyconf)
@@ -276,7 +278,7 @@
 	kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
 	RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
 	RNA_boolean_set(kmi->ptr, "reverse", TRUE);
-	
+
 	WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_CTRL, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
@@ -375,6 +377,8 @@
 	WM_keymap_add_item(keymap, "TEXT_OT_line_break", PADENTER, KM_PRESS, 0, 0);
 
 	WM_keymap_add_menu(keymap, "TEXT_MT_toolbox", RIGHTMOUSE, KM_PRESS, KM_ANY, 0);
+
+	WM_keymap_add_item(keymap, "TEXT_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0);
 	
 	WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
 	WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!

Modified: trunk/blender/source/blender/editors/space_text/text_autocomplete.c
===================================================================
--- trunk/blender/source/blender/editors/space_text/text_autocomplete.c	2012-12-31 14:56:07 UTC (rev 53466)
+++ trunk/blender/source/blender/editors/space_text/text_autocomplete.c	2012-12-31 15:11:36 UTC (rev 53467)
@@ -4,7 +4,7 @@
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. 
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,35 +15,47 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
- * The Original Code is Copyright (C) 2008 Blender Foundation.
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
  *
- * 
- * Contributor(s): Blender Foundation
+ * The Original Code is: all of this file.
  *
+ * Contributor(s): none yet.
+ *
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/** \file blender/editors/space_text/text_python.c
+/** \file blender/editors/space_text/text_autocomplete.c
  *  \ingroup sptext
  */
 
 #include <ctype.h>
+#include <string.h>
 
-#include "DNA_screen_types.h"
-#include "DNA_space_types.h"
+#include "MEM_guardedalloc.h"
+
 #include "DNA_text_types.h"
-#include "DNA_userdef_types.h"
 
+#include "BLI_blenlib.h"
+#include "BLI_ghash.h"
+
+#include "BKE_context.h"
+#include "BKE_text.h"
+#include "BKE_screen.h"
 #include "BKE_suggestions.h"
-#include "BKE_text.h"
 
-#include "BLI_blenlib.h"
-
+#include "WM_api.h"
 #include "WM_types.h"
 
-#include "text_intern.h"
+#include "ED_screen.h"
+#include "UI_interface.h"
 
+#include "text_intern.h"  /* own include */
+
+
+/* -------------------------------------------------------------------- */
+/* Public API */
+
 int text_do_suggest_select(SpaceText *st, ARegion *ar)
 {
 	SuggItem *item, *first, *last /* , *sel */ /* UNUSED */;
@@ -51,7 +63,7 @@
 	int l, x, y, w, h, i;
 	int tgti, *top;
 	int mval[2] = {0, 0};
-	
+
 	if (!st || !st->text) return 0;
 	if (!texttool_text_is_active(st->text)) return 0;
 
@@ -68,7 +80,7 @@
 	if (l < 0) return 0;
 
 	text_update_character_width(st);
-	
+
 	if (st->showlinenrs) {
 		x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4;
 	}
@@ -119,10 +131,100 @@
 		*top = i;
 }
 
+/* -------------------------------------------------------------------- */
+/* Private API */
+
+static void text_autocomplete_free(bContext *C, wmOperator *op);
+
+static GHash *text_autocomplete_build(Text *text)
+{
+	GHash *gh;
+	int seek_len = 0;
+	const char *seek;
+	texttool_text_clear();
+
+	texttool_text_set_active(text);
+
+	/* first get the word we're at */
+	{
+		int i = text->curc;
+		while (i--) {
+			if (!text_check_identifier(text->curl->line[i])) {
+				break;
+			}
+		}
+		i++;
+		seek_len = text->curc - i;
+		seek = text->curl->line + i;
+
+		// BLI_strncpy(seek, seek_ptr, seek_len);
+	}
+
+	/* now walk over entire doc and suggest words */
+	{
+		TextLine *linep;
+
+		gh = BLI_ghash_str_new(__func__);
+
+		for (linep = text->lines.first; linep; linep = linep->next) {
+			int i_start = 0;
+			int i_end = 0;
+
+			while (i_start < linep->len) {
+				/* seek identifier beginning */
+				while (i_start < linep->len && !text_check_identifier(linep->line[i_start])) {
+					i_start++;
+				}
+				i_end = i_start;
+				while (i_end < linep->len && text_check_identifier(linep->line[i_end])) {
+					i_end++;
+				}
+
+				if (i_start != i_end) {
+					char *str_sub = &linep->line[i_start];
+					const int choice_len = i_end - i_start;
+
+					if ((choice_len > seek_len) &&
+					    (seek_len == 0 || strncmp(seek, str_sub, seek_len) == 0) &&
+					    (seek != str_sub))
+					{
+						// printf("Adding: %s\n", s);
+						char str_sub_last = str_sub[choice_len];
+						str_sub[choice_len] = '\0';
+						if (!BLI_ghash_lookup(gh, str_sub)) {
+							char *str_dup = BLI_strdupn(str_sub, choice_len);
+							BLI_ghash_insert(gh, str_dup, str_dup);  /* A 'set' would make more sense here */
+						}
+						str_sub[choice_len] = str_sub_last;
+					}
+				}
+				i_start = i_end;
+			}
+		}
+
+		{
+			GHashIterator *iter = BLI_ghashIterator_new(gh);
+
+			for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
+				const char *s = BLI_ghashIterator_getValue(iter);
+				texttool_suggest_add(s, 'k');
+			}
+			BLI_ghashIterator_free(iter);
+
+		}
+	}
+
+	texttool_suggest_prefix(seek, seek_len);
+
+	return gh;
+}
+
+/* -- */
+
 static void get_suggest_prefix(Text *text, int offset)
 {
 	int i, len;
-	char *line;
+	char *line, tmp[256];
 
 	if (!text) return;
 	if (!texttool_text_is_active(text)) return;
@@ -133,7 +235,14 @@
 			break;
 	i++;
 	len = text->curc - i + offset;
-	texttool_suggest_prefix(line + i, len);
+	if (len > 255) {
+		printf("Suggestion prefix too long\n");
+		len = 255;
+	}
+	if (len != 0)
+		BLI_strncpy(tmp, line + i, len);
+	tmp[len] = '\0';
+	texttool_suggest_prefix(tmp, len);
 }
 
 static void confirm_suggestion(Text *text, int skipleft)
@@ -163,62 +272,79 @@
 		txt_move_left(text, 1);
 
 	txt_insert_buf(text, sel->name);
-	
+
 	for (i = 0; i < skipleft; i++)
 		txt_move_right(text, 0);
 
 	texttool_text_clear();
 }
 
-// XXX
-#define LR_SHIFTKEY 0
-#define LR_ALTKEY 0
-#define LR_CTRLKEY 0
+/* -- */
 
-// XXX
+
+static int text_autocomplete_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
+{
+	SpaceText *st = CTX_wm_space_text(C);
+	Text *text = CTX_data_edit_text(C);
+
+	st->doplugins = TRUE;
+	op->customdata = text_autocomplete_build(text);
+
+	if (texttool_suggest_first()) {
+
+		ED_area_tag_redraw(CTX_wm_area(C));
+
+		if (texttool_suggest_first() == texttool_suggest_last()) {
+			confirm_suggestion(st->text, 0);
+			text_update_line_edited(st->text->curl);
+			text_autocomplete_free(C, op);
+			return OPERATOR_FINISHED;
+		}
+		else {
+			WM_event_add_modal_handler(C, op);
+			return OPERATOR_RUNNING_MODAL;
+		}
+	}
+	else {
+		text_autocomplete_free(C, op);
+		return OPERATOR_CANCELLED;
+	}
+}
+
 static int doc_scroll = 0;
 
-static short UNUSED_FUNCTION(do_texttools) (SpaceText * st, char ascii, unsigned short evnt, short val)
+static int text_autocomplete_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-	ARegion *ar = NULL; // XXX
-	int qual = 0; // XXX
+	SpaceText *st = CTX_wm_space_text(C);
+	ScrArea *sa = CTX_wm_area(C);
+	ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+
 	int draw = 0, tools = 0, swallow = 0, scroll = 1;
-	if (!texttool_text_is_active(st->text)) return 0;
-	if (!st->text || st->text->id.lib) return 0;
+	Text *text = CTX_data_edit_text(C);
+	int retval = OPERATOR_RUNNING_MODAL;
 
+	(void)text;
+
 	if (st->doplugins && texttool_text_is_active(st->text)) {
 		if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST;
 		if (texttool_docs_get()) tools |= TOOL_DOCUMENT;
 	}
 
-	if (ascii) {
-		if (tools & TOOL_SUGG_LIST) {
-			if ((ascii != '_' && ascii != '*' && ispunct(ascii)) || text_check_whitespace(ascii)) {
-				confirm_suggestion(st->text, 0);
-				text_update_line_edited(st->text->curl);
-			}
-			else if ((st->overwrite && txt_replace_char(st->text, ascii)) || txt_add_char(st->text, ascii)) {
-				get_suggest_prefix(st->text, 0);
-				text_pop_suggest_list();
-				swallow = 1;
-				draw = 1;
-			}
-		}
-		if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
-
-	}
-	else if (val == 1 && evnt) {
-		switch (evnt) {
-			case LEFTMOUSE:
+	switch (event->type) {
+		case LEFTMOUSE:
+			if (event->val == KM_PRESS) {
 				if (text_do_suggest_select(st, ar))
 					swallow = 1;
 				else {
 					if (tools & TOOL_SUGG_LIST) texttool_suggest_clear();
 					if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
+					retval = OPERATOR_FINISHED;
 				}
 				draw = 1;
-				break;
-			case MIDDLEMOUSE:
+			}
+			break;
+		case MIDDLEMOUSE:
+			if (event->val == KM_PRESS) {
 				if (text_do_suggest_select(st, ar)) {
 					confirm_suggestion(st->text, 0);
 					text_update_line_edited(st->text->curl);
@@ -227,16 +353,22 @@
 				else {
 					if (tools & TOOL_SUGG_LIST) texttool_suggest_clear();
 					if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
+					retval = OPERATOR_FINISHED;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list