Change !BLI_ghashIterator_isDone to BLI_ghashIterator_notDone. It is
[blender.git] / source / blender / editors / space_text / text_autocomplete.c
index 31f6d92988ce69d0b5027bace008b9b92467cda5..106093d2531999f0aa38d2b96f134f6470152c0a 100644 (file)
@@ -162,20 +162,30 @@ static GHash *text_autocomplete_build(Text *text)
                gh = BLI_ghash_str_new(__func__);
 
                for (linep = text->lines.first; linep; linep = linep->next) {
-                       int i_start = 0;
-                       int i_end = 0;
+                       size_t i_start = 0;
+                       size_t i_end = 0;
+                       size_t i_pos = 0;
 
                        while (i_start < linep->len) {
                                /* seek identifier beginning */
-                               while (i_start < linep->len && !text_check_identifier(linep->line[i_start])) {
-                                       i_start++;
+                               i_pos = i_start;
+                               while ((i_start < linep->len) &&
+                                      (!text_check_identifier_nodigit_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos))))
+                               {
+                                       i_start = i_pos;
                                }
-                               i_end = i_start;
-                               while (i_end < linep->len && text_check_identifier(linep->line[i_end])) {
-                                       i_end++;
+                               i_pos = i_end = i_start;
+                               while ((i_end < linep->len) &&
+                                      (text_check_identifier_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &i_pos))))
+                               {
+                                       i_end = i_pos;
                                }
 
-                               if (i_start != i_end) {
+                               if ((i_start != i_end) &&
+                                   /* check we're at the beginning of a line or that the previous char is not an identifier
+                                    * this prevents digits from being added */
+                                   ((i_start < 1) || !text_check_identifier_unicode(BLI_str_utf8_as_unicode(&linep->line[i_start - 1]))))
+                               {
                                        char *str_sub = &linep->line[i_start];
                                        const int choice_len = i_end - i_start;
 
@@ -193,7 +203,13 @@ static GHash *text_autocomplete_build(Text *text)
                                                str_sub[choice_len] = str_sub_last;
                                        }
                                }
-                               i_start = i_end;
+                               if (i_end != i_start) {
+                                       i_start = i_end;
+                               }
+                               else {
+                                       /* highly unlikely, but prevent eternal loop */
+                                       i_start++;
+                               }
                        }
                }
 
@@ -204,7 +220,7 @@ static GHash *text_autocomplete_build(Text *text)
                        TextFormatType *tft;
                        tft = ED_text_format_get(text);
 
-                       for (; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
+                       for (; BLI_ghashIterator_notDone(iter); BLI_ghashIterator_step(iter)) {
                                const char *s = BLI_ghashIterator_getValue(iter);
                                texttool_suggest_add(s, tft->format_identifier(s));
                        }
@@ -223,32 +239,22 @@ static GHash *text_autocomplete_build(Text *text)
 static void get_suggest_prefix(Text *text, int offset)
 {
        int i, len;
-       char *line, tmp[256];
+       char *line;
 
        if (!text) return;
        if (!texttool_text_is_active(text)) return;
 
        line = text->curl->line;
-       for (i = text->curc - 1 + offset; i >= 0; i--)
-               if (!text_check_identifier(line[i]))
-                       break;
-       i++;
+       i = text_find_identifier_start(line, text->curc + offset);
        len = text->curc - i + offset;
-       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);
+       texttool_suggest_prefix(line + i, len);
 }
 
-static void confirm_suggestion(Text *text, int skipleft)
+static void confirm_suggestion(Text *text)
 {
        SuggItem *sel;
        int i, over = 0;
-       char *line;
+       const char *line;
 
        if (!text) return;
        if (!texttool_text_is_active(text)) return;
@@ -257,23 +263,18 @@ static void confirm_suggestion(Text *text, int skipleft)
        if (!sel) return;
 
        line = text->curl->line;
-       i = text->curc - skipleft - 1;
-       while (i >= 0) {
-               if (!text_check_identifier(line[i]))
-                       break;
-               over++;
-               i--;
-       }
+       i = text_find_identifier_start(line, text->curc /* - skipleft */);
+       over = text->curc - i;
 
-       for (i = 0; i < skipleft; i++)
-               txt_move_left(text, 0);
+//     for (i = 0; i < skipleft; i++)
+//             txt_move_left(text, 0);
        for (i = 0; i < over; i++)
                txt_move_left(text, 1);
 
        txt_insert_buf(text, sel->name);
 
-       for (i = 0; i < skipleft; i++)
-               txt_move_right(text, 0);
+//     for (i = 0; i < skipleft; i++)
+//             txt_move_right(text, 0);
 
        texttool_text_clear();
 }
@@ -294,7 +295,7 @@ static int text_autocomplete_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED
                ED_area_tag_redraw(CTX_wm_area(C));
 
                if (texttool_suggest_first() == texttool_suggest_last()) {
-                       confirm_suggestion(st->text, 0);
+                       confirm_suggestion(st->text);
                        text_update_line_edited(st->text->curl);
                        text_autocomplete_free(C, op);
                        return OPERATOR_FINISHED;
@@ -345,7 +346,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, wmEvent *event)
                case MIDDLEMOUSE:
                        if (event->val == KM_PRESS) {
                                if (text_do_suggest_select(st, ar)) {
-                                       confirm_suggestion(st->text, 0);
+                                       confirm_suggestion(st->text);
                                        text_update_line_edited(st->text->curl);
                                        swallow = 1;
                                }
@@ -369,7 +370,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, wmEvent *event)
                case RETKEY:
                        if (event->val == KM_PRESS) {
                                if (tools & TOOL_SUGG_LIST) {
-                                       confirm_suggestion(st->text, 0);
+                                       confirm_suggestion(st->text);
                                        text_update_line_edited(st->text->curl);
                                        swallow = 1;
                                        draw = 1;