static void confirm_suggestion(Text *text) { SuggItem *sel; int i, over = 0; const char *line; if (!text) return; if (!texttool_text_is_active(text)) return; sel = texttool_suggest_selected(); if (!sel) return; line = text->curl->line; 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 < over; i++) 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(); }
void texttool_text_set_active(Text *text) { if (activeToolText == text) { return; } texttool_text_clear(); activeToolText = text; }
static void text_autocomplete_free(bContext *C, wmOperator *op) { GHash *gh = op->customdata; if (gh) { BLI_ghash_free(gh, NULL, MEM_freeN); op->customdata = NULL; } /* other stuff */ { SpaceText *st = CTX_wm_space_text(C); st->doplugins = false; texttool_text_clear(); } }
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 */ { const int i = text_find_identifier_start(text->curl->line, text->curc); 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) { size_t i_start = 0; size_t i_end = 0; size_t i_pos = 0; while (i_start < linep->len) { /* seek identifier beginning */ 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_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) && /* 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; if ((choice_len > seek_len) && (seek_len == 0 || STREQLEN(seek, str_sub, seek_len)) && (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; } } if (i_end != i_start) { i_start = i_end; } else { /* highly unlikely, but prevent eternal loop */ i_start++; } } } { GHashIterator gh_iter; /* get the formatter for highlighting */ TextFormatType *tft; tft = ED_text_format_get(text); GHASH_ITER (gh_iter, gh) { const char *s = BLI_ghashIterator_getValue(&gh_iter); texttool_suggest_add(s, tft->format_identifier(s)); } } } texttool_suggest_prefix(seek, seek_len); return gh; }