Blender V5.3
text_autocomplete.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cctype>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_text_types.h"
15
16#include "BLI_ghash.hh"
17#include "BLI_listbase.hh"
18#include "BLI_string.hh"
19#include "BLI_string_utf8.hh"
20
21#include "BKE_context.hh"
22#include "BKE_screen.hh"
23#include "BKE_text.h"
25
26#include "WM_api.hh"
27#include "WM_types.hh"
28
29#include "ED_screen.hh"
30#include "ED_text.hh"
31#include "ED_undo.hh"
32
33#include "text_format.hh"
34#include "text_intern.hh" /* Own include. */
35
36namespace blender {
37
38/* -------------------------------------------------------------------- */
41
42bool space_text_do_suggest_select(SpaceText *st, const ARegion *region, const int mval[2])
43{
44 const int line_height = TXT_LINE_HEIGHT(st);
45 SuggItem *item, *first, *last /* , *sel */ /* UNUSED. */;
46 TextLine *tmp;
47 int l, x, y, w, h, i;
48 int tgti, *top;
49
50 if (!st->text) {
51 return false;
52 }
53 if (!texttool_text_is_active(st->text)) {
54 return false;
55 }
56
57 first = texttool_suggest_first();
58 last = texttool_suggest_last();
59 // sel = texttool_suggest_selected(); /* UNUSED. */
61
62 if (!last || !first) {
63 return false;
64 }
65
66 /* Count the visible lines to the cursor. */
67 for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) {
68 /* Pass. */
69 }
70 if (l < 0) {
71 return false;
72 }
73
75
76 x = TXT_BODY_LEFT(st) + (st->runtime->char_width_px * (st->text->curc - st->left));
77 y = region->winy - line_height * l - 2;
78
79 w = SUGG_LIST_WIDTH * st->runtime->char_width_px + U.widget_unit;
80 h = SUGG_LIST_SIZE * line_height + 0.4f * U.widget_unit;
81
82 if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1]) {
83 return false;
84 }
85
86 /* Work out which of the items is at the top of the visible list. */
87 for (i = 0, item = first; i < *top && item->next; i++, item = item->next) {
88 /* Pass. */
89 }
90
91 /* Work out the target item index in the visible list. */
92 tgti = (y - mval[1] - 4) / line_height;
93 if (tgti < 0 || tgti > SUGG_LIST_SIZE) {
94 return true;
95 }
96
97 for (i = tgti; i > 0 && item->next; i--, item = item->next) {
98 /* Pass. */
99 }
100 if (item) {
102 }
103 return true;
104}
105
107{
108 SuggItem *item, *sel;
109 int *top, i;
110
111 item = texttool_suggest_first();
113 top = texttool_suggest_top();
114
115 i = 0;
116 while (item && item != sel) {
117 item = item->next;
118 i++;
119 }
120 if (i > *top + SUGG_LIST_SIZE - 1) {
121 *top = i - SUGG_LIST_SIZE + 1;
122 }
123 else if (i < *top) {
124 *top = i;
125 }
126}
127
129
130/* -------------------------------------------------------------------- */
133
134static void text_autocomplete_free(bContext *C, wmOperator *op);
135
137{
138 GHash *gh;
139 int seek_len = 0;
140 const char *seek;
142
144
145 /* First get the word we're at. */
146 {
147 const int i = text_find_identifier_start(text->curl->line, text->curc);
148 seek_len = text->curc - i;
149 seek = text->curl->line + i;
150
151 // BLI_strncpy_utf8(seek, seek_ptr, seek_len);
152 }
153
154 /* Now walk over entire doc and suggest words. */
155 {
156 gh = BLI_ghash_str_new(__func__);
157
158 for (TextLine &linep : text->lines) {
159 size_t i_start = 0;
160 size_t i_end = 0;
161 size_t i_pos = 0;
162
163 while (i_start < linep.len) {
164 /* Seek identifier beginning. */
165 i_pos = i_start;
166 while ((i_start < linep.len) &&
168 BLI_str_utf8_as_unicode_step_safe(linep.line, linep.len, &i_pos)))
169 {
170 i_start = i_pos;
171 }
172 i_pos = i_end = i_start;
173 while ((i_end < linep.len) &&
175 BLI_str_utf8_as_unicode_step_safe(linep.line, linep.len, &i_pos)))
176 {
177 i_end = i_pos;
178 }
179
180 if ((i_start != i_end) &&
181 /* Check we're at the beginning of a line or that the previous char is not an
182 * identifier this prevents digits from being added. */
183 ((i_start < 1) || !text_check_identifier_unicode(
184 BLI_str_utf8_as_unicode_or_error(&linep.line[i_start - 1]))))
185 {
186 char *str_sub = &linep.line[i_start];
187 const int choice_len = i_end - i_start;
188
189 if ((choice_len > seek_len) && (seek_len == 0 || STREQLEN(seek, str_sub, seek_len)) &&
190 (seek != str_sub))
191 {
192 // printf("Adding: %s\n", s);
193 char str_sub_last = str_sub[choice_len];
194 str_sub[choice_len] = '\0';
195 if (!BLI_ghash_lookup(gh, str_sub)) {
196 char *str_dup = BLI_strdupn(str_sub, choice_len);
197 /* A `set` would make more sense here. */
198 BLI_ghash_insert(gh, str_dup, str_dup);
199 }
200 str_sub[choice_len] = str_sub_last;
201 }
202 }
203 if (i_end != i_start) {
204 i_start = i_end;
205 }
206 else {
207 /* Highly unlikely, but prevent eternal loop. */
208 i_start++;
209 }
210 }
211 }
212
213 {
214 GHashIterator gh_iter;
215
216 /* Get the formatter for highlighting. */
217 TextFormatType *tft;
218 tft = ED_text_format_get(text);
219
220 GHASH_ITER (gh_iter, gh) {
221 const char *s = static_cast<char *>(BLI_ghashIterator_getValue(&gh_iter));
223 }
224 }
225 }
226
227 texttool_suggest_prefix(seek, seek_len);
228
229 return gh;
230}
231
232/* -- */
233
234static void get_suggest_prefix(Text *text, int offset)
235{
236 int i, len;
237 const char *line;
238
239 if (!text) {
240 return;
241 }
242 if (!texttool_text_is_active(text)) {
243 return;
244 }
245
246 line = text->curl->line;
247 i = text_find_identifier_start(line, text->curc + offset);
248 len = text->curc - i + offset;
249 texttool_suggest_prefix(line + i, len);
250}
251
252static void confirm_suggestion(Text *text)
253{
254 SuggItem *sel;
255 int i, over = 0;
256 const char *line;
257
258 if (!text) {
259 return;
260 }
261 if (!texttool_text_is_active(text)) {
262 return;
263 }
264
266 if (!sel) {
267 return;
268 }
269
270 line = text->curl->line;
271 i = text_find_identifier_start(line, text->curc /* - skipleft. */);
272 over = text->curc - i;
273
274 // for (i = 0; i < skipleft; i++)
275 // txt_move_left(text, 0);
276 BLI_assert(memcmp(sel->name, &line[i], over) == 0);
277 const char *buf = sel->name + over;
278 txt_insert_buf(text, buf, strlen(buf));
279
280 // for (i = 0; i < skipleft; i++)
281 // txt_move_right(text, 0);
282
284}
285
287
288/* -------------------------------------------------------------------- */
291
293 wmOperator *op,
294 const wmEvent * /*event*/)
295{
297 Text *text = CTX_data_edit_text(C);
298
299 st->doplugins = true;
301
303
305
309 text_update_line_edited(st->text->curl);
311 ED_undo_push(C, op->type->name);
312 return OPERATOR_FINISHED;
313 }
314
317 }
319 return OPERATOR_CANCELLED;
320}
321
323{
324 /* NOTE(@ideasman42): this code could be refactored or rewritten. */
326 ScrArea *area = CTX_wm_area(C);
328
329 int draw = 0, tools = 0, swallow = 0, scroll = 1;
331
332 if (st->doplugins && texttool_text_is_active(st->text)) {
334 tools |= TOOL_SUGG_LIST;
335 }
336 }
337
338 switch (event->type) {
339 case MOUSEMOVE: {
340 if (space_text_do_suggest_select(st, region, event->mval)) {
341 draw = 1;
342 }
343 swallow = 1;
344 break;
345 }
346 case LEFTMOUSE:
347 if (event->val == KM_PRESS) {
348 if (space_text_do_suggest_select(st, region, event->mval)) {
349 if (tools & TOOL_SUGG_LIST) {
352 text_update_line_edited(st->text->curl);
353 ED_undo_push(C, op->type->name);
354 swallow = 1;
355 draw = 1;
356 }
357 retval = OPERATOR_FINISHED;
358 }
359 else {
360 if (tools & TOOL_SUGG_LIST) {
362 }
363 retval = OPERATOR_CANCELLED;
364 }
365 draw = 1;
366 }
367 break;
368 case EVT_ESCKEY:
369 if (event->val == KM_PRESS) {
370 draw = swallow = 1;
371 if (tools & TOOL_SUGG_LIST) {
373 }
374 else {
375 draw = swallow = 0;
376 }
377 retval = OPERATOR_CANCELLED;
378 }
379 break;
380 case EVT_RETKEY:
381 case EVT_PADENTER:
382 if (event->val == KM_PRESS) {
383 if (tools & TOOL_SUGG_LIST) {
386 text_update_line_edited(st->text->curl);
387 ED_undo_push(C, op->type->name);
388 swallow = 1;
389 draw = 1;
390 }
391 retval = OPERATOR_FINISHED;
392 }
393 break;
394 case EVT_LEFTARROWKEY:
395 case EVT_BACKSPACEKEY:
396 if (event->val == KM_PRESS) {
397 if (tools & TOOL_SUGG_LIST) {
398 if (event->modifier & KM_CTRL) {
400 retval = OPERATOR_CANCELLED;
401 draw = 1;
402 }
403 else {
404 /* Work out which char we are about to delete/pass. */
405 if (st->text->curl && st->text->curc > 0) {
406 char ch = st->text->curl->line[st->text->curc - 1];
407 if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
408 get_suggest_prefix(st->text, -1);
410 txt_move_left(st->text, false);
411 draw = 1;
412 }
413 else {
415 retval = OPERATOR_CANCELLED;
416 draw = 1;
417 }
418 }
419 else {
421 retval = OPERATOR_CANCELLED;
422 draw = 1;
423 }
424 }
425 }
426 }
427 break;
429 if (event->val == KM_PRESS) {
430 if (tools & TOOL_SUGG_LIST) {
431 if (event->modifier & KM_CTRL) {
433 retval = OPERATOR_CANCELLED;
434 draw = 1;
435 }
436 else {
437 /* Work out which char we are about to pass. */
438 if (st->text->curl && st->text->curc < st->text->curl->len) {
439 char ch = st->text->curl->line[st->text->curc];
440 if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
441 get_suggest_prefix(st->text, 1);
443 txt_move_right(st->text, false);
444 draw = 1;
445 }
446 else {
448 retval = OPERATOR_CANCELLED;
449 draw = 1;
450 }
451 }
452 else {
454 retval = OPERATOR_CANCELLED;
455 draw = 1;
456 }
457 }
458 }
459 }
460 break;
461 case EVT_PAGEDOWNKEY:
462 scroll = SUGG_LIST_SIZE - 1;
464 case WHEELDOWNMOUSE:
465 case EVT_DOWNARROWKEY:
466 if (event->val == KM_PRESS) {
467 if (tools & TOOL_SUGG_LIST) {
469 if (!sel) {
471 }
472 else {
473 while (sel && scroll--) {
474 if (sel != texttool_suggest_last() && sel->next) {
476 sel = sel->next;
477 }
478 else {
481 }
482 }
483 }
485 swallow = 1;
486 draw = 1;
487 }
488 }
489 break;
490 case EVT_PAGEUPKEY:
491 scroll = SUGG_LIST_SIZE - 1;
493 case WHEELUPMOUSE:
494 case EVT_UPARROWKEY:
495 if (event->val == KM_PRESS) {
496 if (tools & TOOL_SUGG_LIST) {
498 while (sel && scroll--) {
499 if (sel != texttool_suggest_first() && sel->prev) {
501 sel = sel->prev;
502 }
503 else {
505 sel = texttool_suggest_last();
506 }
507 }
509 swallow = 1;
510 draw = 1;
511 }
512 }
513 break;
515 case EVT_LEFTSHIFTKEY:
516 break;
517 default: {
518#if 0
519 if (tools & TOOL_SUGG_LIST) {
521 draw = 1;
522 }
523#endif
524 break;
525 }
526 }
527
528 if (draw) {
529 ED_area_tag_redraw(area);
530 }
531
532 if (swallow) {
533 // retval = OPERATOR_RUNNING_MODAL;
534 }
535
537 if (retval != OPERATOR_RUNNING_MODAL) {
539 }
540 return retval;
541 }
543 return OPERATOR_FINISHED;
544}
545
547{
548 GHash *gh = static_cast<GHash *>(op->customdata);
549 if (gh) {
550 BLI_ghash_free(gh, nullptr, MEM_delete_void);
551 op->customdata = nullptr;
552 }
553
554 /* Other stuff. */
555 {
557 st->doplugins = false;
559 }
560}
561
566
568{
569 /* Identifiers. */
570 ot->name = "Text Auto Complete";
571 ot->description = "Show a list of used text in the open document";
572 ot->idname = "TEXT_OT_autocomplete";
573
574 /* API callbacks. */
579
580 /* Flags. */
581 /* Undo is handled conditionally by this operator. */
582 ot->flag = OPTYPE_BLOCKING;
583}
584
586
587} // namespace blender
#define ATTR_FALLTHROUGH
#define GHASH_ITER(gh_iter_, ghash_)
Definition BLI_ghash.hh:320
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define STREQLEN(a, b, n)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
UserDef U
Definition blender.cc:58
bool text_space_edit_poll(bContext *C)
Definition text_ops.cc:283
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
static wmOperatorStatus text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *)
void texttool_suggest_prefix(const char *prefix, int prefix_len)
void txt_insert_buf(struct Text *text, const char *in_buffer, int in_buffer_len) ATTR_NONNULL(1
Definition text.cc:1545
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const BMLoop * l
void text_update_line_edited(TextLine *line)
Definition text_ops.cc:328
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
void TEXT_OT_autocomplete(wmOperatorType *ot)
SuggItem * texttool_suggest_first()
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void texttool_suggest_clear()
SuggItem * texttool_suggest_last()
static void text_autocomplete_cancel(bContext *C, wmOperator *op)
TextFormatType * ED_text_format_get(Text *text)
unsigned int BLI_str_utf8_as_unicode_step_safe(const char *__restrict p, size_t p_len, size_t *__restrict index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static GHash * text_autocomplete_build(Text *text)
Text * CTX_data_edit_text(const bContext *C)
int text_find_identifier_start(const char *str, int i)
Definition text.cc:2386
void MEM_delete_void(void *vmemh)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.hh:301
SpaceText * CTX_wm_space_text(const bContext *C)
int text_check_identifier_nodigit_unicode(unsigned int ch)
Definition text.cc:2372
bool space_text_do_suggest_select(SpaceText *st, const ARegion *region, const int mval[2])
static void confirm_suggestion(Text *text)
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
short texttool_text_is_active(struct Text *text)
void texttool_suggest_add(const char *name, char type)
static void text_autocomplete_free(bContext *C, wmOperator *op)
bool text_check_whitespace(char ch)
Definition text.cc:2378
int text_check_identifier_unicode(unsigned int ch)
Definition text.cc:2367
void txt_move_left(struct Text *text, bool sel)
Definition text.cc:874
static wmOperatorStatus text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *event)
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
int * texttool_suggest_top()
void txt_move_right(struct Text *text, bool sel)
Definition text.cc:917
SuggItem * texttool_suggest_selected()
UndoStep * ED_text_undo_push_init(bContext *C)
Definition text_undo.cc:250
unsigned int BLI_str_utf8_as_unicode_or_error(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
static void get_suggest_prefix(Text *text, int offset)
void text_pop_suggest_list()
void space_text_update_character_width(SpaceText *st)
void texttool_suggest_select(SuggItem *sel)
void texttool_text_set_active(struct Text *text)
void texttool_text_clear()
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
ed::text::SpaceText_Runtime * runtime
struct SuggItem * prev
struct SuggItem * next
char(*) format_identifier(const char *string)
struct TextLine * prev
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type
#define SUGG_LIST_WIDTH
#define TXT_LINE_HEIGHT(st)
#define SUGG_LIST_SIZE
#define TXT_BODY_LEFT(st)
#define TOOL_SUGG_LIST