Cleanup: comment line length (editors)
[blender.git] / source / blender / editors / space_text / text_autocomplete.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/space_text/text_autocomplete.c
22  *  \ingroup sptext
23  */
24
25 #include <ctype.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_text_types.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_ghash.h"
34
35 #include "BKE_context.h"
36 #include "BKE_text.h"
37 #include "BKE_screen.h"
38 #include "BKE_suggestions.h"
39
40 #include "WM_api.h"
41 #include "WM_types.h"
42
43 #include "ED_text.h"
44 #include "ED_undo.h"
45 #include "ED_screen.h"
46
47 #include "UI_interface.h"
48
49 #include "text_format.h"
50 #include "text_intern.h"  /* own include */
51
52
53 /* -------------------------------------------------------------------- */
54 /* Public API */
55
56 int text_do_suggest_select(SpaceText *st, ARegion *ar)
57 {
58         SuggItem *item, *first, *last /* , *sel */ /* UNUSED */;
59         TextLine *tmp;
60         int l, x, y, w, h, i;
61         int tgti, *top;
62         int mval[2] = {0, 0};
63
64         if (!st || !st->text) return 0;
65         if (!texttool_text_is_active(st->text)) return 0;
66
67         first = texttool_suggest_first();
68         last = texttool_suggest_last();
69         /* sel = texttool_suggest_selected(); */ /* UNUSED */
70         top = texttool_suggest_top();
71
72         if (!last || !first)
73                 return 0;
74
75         /* Count the visible lines to the cursor */
76         for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ;
77         if (l < 0) return 0;
78
79         text_update_character_width(st);
80
81         if (st->showlinenrs) {
82                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4;
83         }
84         else {
85                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4;
86         }
87         y = ar->winy - st->lheight_dpi * l - 2;
88
89         w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit;
90         h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit;
91
92         // XXX getmouseco_areawin(mval);
93
94         if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1])
95                 return 0;
96
97         /* Work out which of the items is at the top of the visible list */
98         for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ;
99
100         /* Work out the target item index in the visible list */
101         tgti = (y - mval[1] - 4) / st->lheight_dpi;
102         if (tgti < 0 || tgti > SUGG_LIST_SIZE)
103                 return 1;
104
105         for (i = tgti; i > 0 && item->next; i--, item = item->next) ;
106         if (item)
107                 texttool_suggest_select(item);
108         return 1;
109 }
110
111 void text_pop_suggest_list(void)
112 {
113         SuggItem *item, *sel;
114         int *top, i;
115
116         item = texttool_suggest_first();
117         sel = texttool_suggest_selected();
118         top = texttool_suggest_top();
119
120         i = 0;
121         while (item && item != sel) {
122                 item = item->next;
123                 i++;
124         }
125         if (i > *top + SUGG_LIST_SIZE - 1)
126                 *top = i - SUGG_LIST_SIZE + 1;
127         else if (i < *top)
128                 *top = i;
129 }
130
131 /* -------------------------------------------------------------------- */
132 /* Private API */
133
134 static void text_autocomplete_free(bContext *C, wmOperator *op);
135
136 static GHash *text_autocomplete_build(Text *text)
137 {
138         GHash *gh;
139         int seek_len = 0;
140         const char *seek;
141         texttool_text_clear();
142
143         texttool_text_set_active(text);
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(seek, seek_ptr, seek_len);
152         }
153
154         /* now walk over entire doc and suggest words */
155         {
156                 TextLine *linep;
157
158                 gh = BLI_ghash_str_new(__func__);
159
160                 for (linep = text->lines.first; linep; linep = linep->next) {
161                         size_t i_start = 0;
162                         size_t i_end = 0;
163                         size_t i_pos = 0;
164
165                         while (i_start < linep->len) {
166                                 /* seek identifier beginning */
167                                 i_pos = i_start;
168                                 while ((i_start < linep->len) &&
169                                        (!text_check_identifier_nodigit_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos))))
170                                 {
171                                         i_start = i_pos;
172                                 }
173                                 i_pos = i_end = i_start;
174                                 while ((i_end < linep->len) &&
175                                        (text_check_identifier_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &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 identifier
182                                      * this prevents digits from being added */
183                                     ((i_start < 1) || !text_check_identifier_unicode(BLI_str_utf8_as_unicode(&linep->line[i_start - 1]))))
184                                 {
185                                         char *str_sub = &linep->line[i_start];
186                                         const int choice_len = i_end - i_start;
187
188                                         if ((choice_len > seek_len) &&
189                                             (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 = BLI_ghashIterator_getValue(&gh_iter);
222                                 texttool_suggest_add(s, tft->format_identifier(s));
223                         }
224                 }
225         }
226
227         texttool_suggest_prefix(seek, seek_len);
228
229         return gh;
230 }
231
232 /* -- */
233
234 static void get_suggest_prefix(Text *text, int offset)
235 {
236         int i, len;
237         const char *line;
238
239         if (!text) return;
240         if (!texttool_text_is_active(text)) return;
241
242         line = text->curl->line;
243         i = text_find_identifier_start(line, text->curc + offset);
244         len = text->curc - i + offset;
245         texttool_suggest_prefix(line + i, len);
246 }
247
248 static void confirm_suggestion(Text *text, TextUndoBuf *utxt)
249 {
250         SuggItem *sel;
251         int i, over = 0;
252         const char *line;
253
254         if (!text) return;
255         if (!texttool_text_is_active(text)) return;
256
257         sel = texttool_suggest_selected();
258         if (!sel) return;
259
260         line = text->curl->line;
261         i = text_find_identifier_start(line, text->curc /* - skipleft */);
262         over = text->curc - i;
263
264 //      for (i = 0; i < skipleft; i++)
265 //              txt_move_left(text, 0);
266         BLI_assert(memcmp(sel->name, &line[i], over) == 0);
267         txt_insert_buf(text, utxt, sel->name + over);
268
269 //      for (i = 0; i < skipleft; i++)
270 //              txt_move_right(text, 0);
271
272         texttool_text_clear();
273 }
274
275 /* -- */
276
277
278 static int text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
279 {
280         SpaceText *st = CTX_wm_space_text(C);
281         Text *text = CTX_data_edit_text(C);
282
283         st->doplugins = true;
284         op->customdata = text_autocomplete_build(text);
285
286         if (texttool_suggest_first()) {
287
288                 ED_area_tag_redraw(CTX_wm_area(C));
289
290                 if (texttool_suggest_first() == texttool_suggest_last()) {
291                         TextUndoBuf *utxt = ED_text_undo_push_init(C);
292                         confirm_suggestion(st->text, utxt);
293                         text_update_line_edited(st->text->curl);
294                         text_autocomplete_free(C, op);
295                         ED_undo_push(C, op->type->name);
296                         return OPERATOR_FINISHED;
297                 }
298                 else {
299                         WM_event_add_modal_handler(C, op);
300                         return OPERATOR_RUNNING_MODAL;
301                 }
302         }
303         else {
304                 text_autocomplete_free(C, op);
305                 return OPERATOR_CANCELLED;
306         }
307 }
308
309 static int doc_scroll = 0;
310
311 static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *event)
312 {
313         SpaceText *st = CTX_wm_space_text(C);
314         ScrArea *sa = CTX_wm_area(C);
315         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
316
317         int draw = 0, tools = 0, swallow = 0, scroll = 1;
318         Text *text = CTX_data_edit_text(C);
319         int retval = OPERATOR_RUNNING_MODAL;
320
321         (void)text;
322
323         if (st->doplugins && texttool_text_is_active(st->text)) {
324                 if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST;
325                 if (texttool_docs_get()) tools |= TOOL_DOCUMENT;
326         }
327
328         switch (event->type) {
329                 case LEFTMOUSE:
330                         if (event->val == KM_PRESS) {
331                                 if (text_do_suggest_select(st, ar))
332                                         swallow = 1;
333                                 else {
334                                         if (tools & TOOL_SUGG_LIST) {
335                                                 texttool_suggest_clear();
336                                         }
337                                         if (tools & TOOL_DOCUMENT) {
338                                                 texttool_docs_clear();
339                                                 doc_scroll = 0;
340                                         }
341                                         retval = OPERATOR_FINISHED;
342                                 }
343                                 draw = 1;
344                         }
345                         break;
346                 case MIDDLEMOUSE:
347                         if (event->val == KM_PRESS) {
348                                 if (text_do_suggest_select(st, ar)) {
349                                         TextUndoBuf *utxt = ED_text_undo_push_init(C);
350                                         confirm_suggestion(st->text, utxt);
351                                         text_update_line_edited(st->text->curl);
352                                         ED_undo_push(C, op->type->name);
353                                         swallow = 1;
354                                 }
355                                 else {
356                                         if (tools & TOOL_SUGG_LIST) {
357                                                 texttool_suggest_clear();
358                                         }
359                                         if (tools & TOOL_DOCUMENT) {
360                                                 texttool_docs_clear();
361                                                 doc_scroll = 0;
362                                         }
363                                         retval = OPERATOR_FINISHED;
364                                 }
365                                 draw = 1;
366                         }
367                         break;
368                 case ESCKEY:
369                         if (event->val == KM_PRESS) {
370                                 draw = swallow = 1;
371                                 if (tools & TOOL_SUGG_LIST) {
372                                         texttool_suggest_clear();
373                                 }
374                                 else if (tools & TOOL_DOCUMENT) {
375                                         texttool_docs_clear();
376                                         doc_scroll = 0;
377                                 }
378                                 else draw = swallow = 0;
379                                 retval = OPERATOR_CANCELLED;
380                         }
381                         break;
382                 case RETKEY:
383                 case PADENTER:
384                         if (event->val == KM_PRESS) {
385                                 if (tools & TOOL_SUGG_LIST) {
386                                         TextUndoBuf *utxt = ED_text_undo_push_init(C);
387                                         confirm_suggestion(st->text, utxt);
388                                         text_update_line_edited(st->text->curl);
389                                         ED_undo_push(C, op->type->name);
390                                         swallow = 1;
391                                         draw = 1;
392                                 }
393                                 if (tools & TOOL_DOCUMENT) {
394                                         texttool_docs_clear();
395                                         doc_scroll = 0;
396                                         draw = 1;
397                                 }
398                                 retval = OPERATOR_FINISHED;
399                         }
400                         break;
401                 case LEFTARROWKEY:
402                 case BACKSPACEKEY:
403                         if (event->val == KM_PRESS) {
404                                 if (tools & TOOL_SUGG_LIST) {
405                                         if (event->ctrl) {
406                                                 texttool_suggest_clear();
407                                                 retval = OPERATOR_CANCELLED;
408                                         }
409                                         else {
410                                                 /* Work out which char we are about to delete/pass */
411                                                 if (st->text->curl && st->text->curc > 0) {
412                                                         char ch = st->text->curl->line[st->text->curc - 1];
413                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
414                                                                 get_suggest_prefix(st->text, -1);
415                                                                 text_pop_suggest_list();
416                                                         }
417                                                         else {
418                                                                 texttool_suggest_clear();
419                                                                 retval = OPERATOR_CANCELLED;
420                                                         }
421                                                 }
422                                                 else {
423                                                         texttool_suggest_clear();
424                                                         retval = OPERATOR_CANCELLED;
425                                                 }
426                                         }
427                                 }
428                                 if (tools & TOOL_DOCUMENT) {
429                                         texttool_docs_clear();
430                                         doc_scroll = 0;
431                                 }
432                         }
433                         break;
434                 case RIGHTARROWKEY:
435                         if (event->val == KM_PRESS) {
436                                 if (tools & TOOL_SUGG_LIST) {
437                                         if (event->ctrl) {
438                                                 texttool_suggest_clear();
439                                                 retval = OPERATOR_CANCELLED;
440                                         }
441                                         else {
442                                                 /* Work out which char we are about to pass */
443                                                 if (st->text->curl && st->text->curc < st->text->curl->len) {
444                                                         char ch = st->text->curl->line[st->text->curc + 1];
445                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
446                                                                 get_suggest_prefix(st->text, 1);
447                                                                 text_pop_suggest_list();
448                                                         }
449                                                         else {
450                                                                 texttool_suggest_clear();
451                                                                 retval = OPERATOR_CANCELLED;
452                                                         }
453                                                 }
454                                                 else {
455                                                         texttool_suggest_clear();
456                                                         retval = OPERATOR_CANCELLED;
457                                                 }
458                                         }
459                                 }
460                                 if (tools & TOOL_DOCUMENT) {
461                                         texttool_docs_clear();
462                                         doc_scroll = 0;
463                                 }
464                         }
465                         break;
466                 case PAGEDOWNKEY:
467                         scroll = SUGG_LIST_SIZE - 1;
468                         ATTR_FALLTHROUGH;
469                 case WHEELDOWNMOUSE:
470                 case DOWNARROWKEY:
471                         if (event->val == KM_PRESS) {
472                                 if (tools & TOOL_DOCUMENT) {
473                                         doc_scroll++;
474                                         swallow = 1;
475                                         draw = 1;
476                                 }
477                                 else if (tools & TOOL_SUGG_LIST) {
478                                         SuggItem *sel = texttool_suggest_selected();
479                                         if (!sel) {
480                                                 texttool_suggest_select(texttool_suggest_first());
481                                         }
482                                         else {
483                                                 while (sel && scroll--) {
484                                                         if (sel != texttool_suggest_last() && sel->next) {
485                                                                 texttool_suggest_select(sel->next);
486                                                                 sel = sel->next;
487                                                         }
488                                                         else {
489                                                                 texttool_suggest_select(texttool_suggest_first());
490                                                                 sel = texttool_suggest_first();
491                                                         }
492                                                 }
493                                         }
494                                         text_pop_suggest_list();
495                                         swallow = 1;
496                                         draw = 1;
497                                 }
498                         }
499                         break;
500                 case PAGEUPKEY:
501                         scroll = SUGG_LIST_SIZE - 1;
502                         ATTR_FALLTHROUGH;
503                 case WHEELUPMOUSE:
504                 case UPARROWKEY:
505                         if (event->val == KM_PRESS) {
506                                 if (tools & TOOL_DOCUMENT) {
507                                         if (doc_scroll > 0) doc_scroll--;
508                                         swallow = 1;
509                                         draw = 1;
510                                 }
511                                 else if (tools & TOOL_SUGG_LIST) {
512                                         SuggItem *sel = texttool_suggest_selected();
513                                         while (sel && scroll--) {
514                                                 if (sel != texttool_suggest_first() && sel->prev) {
515                                                         texttool_suggest_select(sel->prev);
516                                                         sel = sel->prev;
517                                                 }
518                                                 else {
519                                                         texttool_suggest_select(texttool_suggest_last());
520                                                         sel = texttool_suggest_last();
521                                                 }
522                                         }
523                                         text_pop_suggest_list();
524                                         swallow = 1;
525                                         draw = 1;
526                                 }
527                         }
528                         break;
529                 case RIGHTSHIFTKEY:
530                 case LEFTSHIFTKEY:
531                         break;
532 #if 0
533                 default:
534                         if (tools & TOOL_SUGG_LIST) {
535                                 texttool_suggest_clear();
536                                 draw = 1;
537                         }
538                         if (tools & TOOL_DOCUMENT) {
539                                 texttool_docs_clear();
540                                 doc_scroll = 0;
541                                 draw = 1;
542                         }
543 #endif
544         }
545
546         if (draw) {
547                 ED_area_tag_redraw(sa);
548         }
549
550 //      if (swallow) {
551 //              retval = OPERATOR_RUNNING_MODAL;
552 //      }
553
554         if (texttool_suggest_first()) {
555                 if (retval != OPERATOR_RUNNING_MODAL) {
556                         text_autocomplete_free(C, op);
557                 }
558                 return retval;
559         }
560         else {
561                 text_autocomplete_free(C, op);
562                 return OPERATOR_FINISHED;
563         }
564 }
565
566 static void text_autocomplete_free(bContext *C, wmOperator *op)
567 {
568         GHash *gh = op->customdata;
569         if (gh) {
570                 BLI_ghash_free(gh, NULL, MEM_freeN);
571                 op->customdata = NULL;
572         }
573
574         /* other stuff */
575         {
576                 SpaceText *st = CTX_wm_space_text(C);
577                 st->doplugins = false;
578                 texttool_text_clear();
579         }
580 }
581
582 static void text_autocomplete_cancel(bContext *C, wmOperator *op)
583 {
584         text_autocomplete_free(C, op);
585 }
586
587 void TEXT_OT_autocomplete(wmOperatorType *ot)
588 {
589         /* identifiers */
590         ot->name = "Text Auto Complete";
591         ot->description = "Show a list of used text in the open document";
592         ot->idname = "TEXT_OT_autocomplete";
593
594         /* api callbacks */
595         ot->invoke = text_autocomplete_invoke;
596         ot->cancel = text_autocomplete_cancel;
597         ot->modal = text_autocomplete_modal;
598         ot->poll = text_space_edit_poll;
599
600         /* flags */
601         /* Undo is handled conditionally by this operator. */
602         ot->flag = OPTYPE_BLOCKING;
603 }