forked from geany/geany
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.c
2232 lines (1801 loc) · 61.2 KB
/
symbols.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* symbols.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2006 The Geany contributors
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file symbols.h
* Tag-related functions.
**/
/*
* Symbol Tree and TagManager-related convenience functions.
* TagManager parses tags for each document, and also adds them to the workspace (session).
* Global tags are lists of tags for each filetype, loaded when a document with a
* matching filetype is first loaded.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "symbols.h"
#include "app.h"
#include "callbacks.h" /* FIXME: for ignore_callback */
#include "dialogs.h"
#include "documentprivate.h"
#include "editor.h"
#include "encodings.h"
#include "filetypesprivate.h"
#include "geanyobject.h"
#include "highlighting.h"
#include "main.h"
#include "navqueue.h"
#include "sciwrappers.h"
#include "sidebar.h"
#include "support.h"
#include "tm_parser.h"
#include "tm_tag.h"
#include "tm_ctags.h"
#include "ui_utils.h"
#include "utils.h"
#include "SciLexer.h"
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
typedef struct
{
gint found_line; /* return: the nearest line found */
gint line; /* input: the line to look for */
gboolean lower /* input: search only for lines with lower number than @line */;
} TreeSearchData;
static GPtrArray *top_level_iter_names = NULL;
static struct
{
const gchar *icon_name;
GdkPixbuf *pixbuf;
}
/* keep in sync with enum in tm_parser.h */
symbols_icons[TM_N_ICONS] = {
[TM_ICON_CLASS] = { "classviewer-class", NULL },
[TM_ICON_MACRO] = { "classviewer-macro", NULL },
[TM_ICON_MEMBER] = { "classviewer-member", NULL },
[TM_ICON_METHOD] = { "classviewer-method", NULL },
[TM_ICON_NAMESPACE] = { "classviewer-namespace", NULL },
[TM_ICON_OTHER] = { "classviewer-other", NULL },
[TM_ICON_STRUCT] = { "classviewer-struct", NULL },
[TM_ICON_VAR] = { "classviewer-var", NULL },
};
static struct
{
GtkWidget *expand_all;
GtkWidget *collapse_all;
GtkWidget *sort_by_name;
GtkWidget *sort_by_appearance;
GtkWidget *find_usage;
GtkWidget *find_doc_usage;
GtkWidget *find_in_files;
GtkWidget *group_by_type;
}
symbol_menu;
static void load_user_tags(GeanyFiletypeID ft_id);
/* get the tags_ignore list, exported by geany_lcpp.c */
extern gchar **c_tags_ignore;
/* ignore certain tokens when parsing C-like syntax.
* Also works for reloading. */
static void load_c_ignore_tags(void)
{
gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
gchar *content;
if (g_file_get_contents(path, &content, NULL, NULL))
{
gchar **line;
/* historically we ignore the glib _DECLS for tag generation */
SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
g_strfreev(c_tags_ignore);
tm_ctags_clear_ignore_symbols();
/* for old c.c parser */
c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
/* for new cxx parser */
foreach_strv(line, c_tags_ignore)
{
tm_ctags_add_ignore_symbol(*line);
}
g_free(content);
}
g_free(path);
}
void symbols_reload_config_files(void)
{
load_c_ignore_tags();
}
static gsize get_tag_count(void)
{
GPtrArray *tags = tm_get_workspace()->global_tags;
gsize count = tags ? tags->len : 0;
return count;
}
/* wrapper for tm_workspace_load_global_tags().
* note that the tag count only counts new global tags added - if a tag has the same name,
* currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
{
gboolean result;
gsize old_tag_count = get_tag_count();
result = tm_workspace_load_global_tags(tags_file, ft->lang);
if (result)
{
geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
(guint) (get_tag_count() - old_tag_count));
}
return result;
}
/* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
* This provides autocompletion, calltips, etc. */
void symbols_global_tags_loaded(guint file_type_idx)
{
/* load ignore list for C/C++ parser */
if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
c_tags_ignore == NULL)
{
load_c_ignore_tags();
}
if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
return;
/* load config in case of custom filetypes */
filetypes_load_config(file_type_idx, FALSE);
load_user_tags(file_type_idx);
switch (file_type_idx)
{
case GEANY_FILETYPES_CPP:
symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
break;
case GEANY_FILETYPES_PHP:
symbols_global_tags_loaded(GEANY_FILETYPES_HTML); /* load HTML global tags */
break;
}
}
GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
{
guint j;
TMTag *tag;
GString *s = NULL;
GPtrArray *typedefs;
TMParserType tag_lang;
if (global)
typedefs = app->tm_workspace->global_typename_array;
else
typedefs = app->tm_workspace->typename_array;
if ((typedefs) && (typedefs->len > 0))
{
const gchar *last_name = "";
s = g_string_sized_new(typedefs->len * 10);
for (j = 0; j < typedefs->len; ++j)
{
tag = TM_TAG(typedefs->pdata[j]);
tag_lang = tag->lang;
if (tag->name && tm_parser_langs_compatible(lang, tag_lang) &&
strcmp(tag->name, last_name) != 0)
{
if (j != 0)
g_string_append_c(s, ' ');
g_string_append(s, tag->name);
last_name = tag->name;
}
}
}
return s;
}
/** Gets the context separator used by the tag manager for a particular file
* type.
* @param ft_id File type identifier.
* @return The context separator string.
*
* Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
* without a context separator.
*
* @since 0.19
*/
GEANY_API_SYMBOL
const gchar *symbols_get_context_separator(gint ft_id)
{
return tm_parser_scope_separator(filetypes[ft_id]->lang);
}
/* sort by name, then line */
static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
{
gint ret;
if (tag_a == NULL || tag_b == NULL)
return 0;
if (tag_a->name == NULL)
return -(tag_a->name != tag_b->name);
if (tag_b->name == NULL)
return tag_a->name != tag_b->name;
ret = strcmp(tag_a->name, tag_b->name);
if (ret == 0)
{
return tag_a->line - tag_b->line;
}
return ret;
}
/* sort by line, then scope */
static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
{
const TMTag *tag_a = TM_TAG(a);
const TMTag *tag_b = TM_TAG(b);
gint ret;
if (a == NULL || b == NULL)
return 0;
ret = tag_a->line - tag_b->line;
if (ret == 0)
{
if (tag_a->scope == NULL)
return -(tag_a->scope != tag_b->scope);
if (tag_b->scope == NULL)
return tag_a->scope != tag_b->scope;
else
return strcmp(tag_a->scope, tag_b->scope);
}
return ret;
}
static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
{
GList *tag_names = NULL;
guint i;
gchar **tf_strv;
g_return_val_if_fail(doc, NULL);
if (! doc->tm_file || ! doc->tm_file->tags_array)
return NULL;
tf_strv = g_strsplit_set(doc->priv->tag_filter, " ", -1);
for (i = 0; i < doc->tm_file->tags_array->len; ++i)
{
TMTag *tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
if (tag->type & tag_types)
{
gboolean filtered = FALSE;
gchar **val;
gchar *full_tagname = g_strconcat(tag->scope ? tag->scope : "",
tag->scope ? tm_parser_scope_separator_printable(tag->lang) : "",
tag->name, NULL);
gchar *normalized_tagname = g_utf8_normalize(full_tagname, -1, G_NORMALIZE_ALL);
foreach_strv(val, tf_strv)
{
gchar *normalized_val = g_utf8_normalize(*val, -1, G_NORMALIZE_ALL);
if (normalized_tagname != NULL && normalized_val != NULL)
{
gchar *case_normalized_tagname = g_utf8_casefold(normalized_tagname, -1);
gchar *case_normalized_val = g_utf8_casefold(normalized_val, -1);
filtered = strstr(case_normalized_tagname, case_normalized_val) == NULL;
g_free(case_normalized_tagname);
g_free(case_normalized_val);
}
g_free(normalized_val);
if (filtered)
break;
}
if (!filtered)
tag_names = g_list_prepend(tag_names, tag);
g_free(normalized_tagname);
g_free(full_tagname);
}
}
tag_names = g_list_sort(tag_names, compare_symbol_lines);
g_strfreev(tf_strv);
return tag_names;
}
/* amount of types in the symbol list - can be increased if needed */
#define MAX_SYMBOL_TYPES 15
GtkTreeIter tv_iters[MAX_SYMBOL_TYPES];
static void init_tag_iters(void)
{
guint i;
/* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
* filetypes(e.g. config file to Python crashes Geany without this) */
for (i = 0; i < MAX_SYMBOL_TYPES; i++)
tv_iters[i].stamp = -1;
}
static GdkPixbuf *get_tag_icon(const gchar *icon_name)
{
static GtkIconTheme *icon_theme = NULL;
static gint x = -1;
if (G_UNLIKELY(x < 0))
{
gint dummy;
icon_theme = gtk_icon_theme_get_default();
gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
}
return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
}
static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
{
GtkTreeModel *model = GTK_TREE_MODEL(store);
if (!gtk_tree_model_get_iter_first(model, iter))
return FALSE;
do
{
gchar *candidate;
gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
/* FIXME: what if 2 different items have the same name?
* this should never happen, but might be caused by a typo in a translation */
if (utils_str_equal(candidate, title))
{
g_free(candidate);
return TRUE;
}
else
g_free(candidate);
}
while (gtk_tree_model_iter_next(model, iter));
return FALSE;
}
static void tag_list_add_groups(GtkTreeStore *tree_store, TMParserType lang)
{
const gchar *title;
guint i;
guint icon_id;
g_return_if_fail(top_level_iter_names);
for (i = 0; (title = tm_parser_get_sidebar_info(lang, i, &icon_id)) != NULL; i++)
{
GtkTreeIter *iter = &tv_iters[i];
GdkPixbuf *icon = NULL;
if (icon_id < TM_N_ICONS)
icon = symbols_icons[icon_id].pixbuf;
g_assert(title != NULL);
g_ptr_array_add(top_level_iter_names, (gchar *)title);
if (!find_toplevel_iter(tree_store, iter, title))
gtk_tree_store_append(tree_store, iter, NULL);
if (icon)
gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
}
}
static void add_top_level_items(GeanyDocument *doc)
{
TMParserType lang = doc->file_type->lang;
GtkTreeStore *tag_store = doc->priv->tag_store;
if (top_level_iter_names == NULL)
top_level_iter_names = g_ptr_array_new();
else
g_ptr_array_set_size(top_level_iter_names, 0);
init_tag_iters();
tag_list_add_groups(tag_store, lang);
}
/* removes toplevel items that have no children */
static void hide_empty_rows(GtkTreeStore *store)
{
GtkTreeIter iter;
gboolean cont = TRUE;
if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
return; /* stop when first iter is invalid, i.e. no elements */
while (cont)
{
if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
cont = gtk_tree_store_remove(store, &iter);
else
cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
}
}
static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean include_scope,
gboolean include_line)
{
gchar *utf8_name;
const gchar *scope = tag->scope;
static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
gboolean doc_is_utf8 = FALSE;
/* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
* for None at this point completely */
if (utils_str_equal(doc->encoding, "UTF-8") ||
utils_str_equal(doc->encoding, "None"))
doc_is_utf8 = TRUE;
else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
* plugin might have called tm_source_file_update(), so check to be sure */
doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
if (! doc_is_utf8)
utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
-1, doc->encoding, TRUE);
else
utf8_name = tag->name;
if (utf8_name == NULL)
return NULL;
if (! buffer)
buffer = g_string_new(NULL);
else
g_string_truncate(buffer, 0);
/* check first char of scope is a wordchar */
if (include_scope && scope &&
strpbrk(scope, GEANY_WORDCHARS) == scope)
{
const gchar *sep = tm_parser_scope_separator_printable(tag->lang);
g_string_append(buffer, scope);
g_string_append(buffer, sep);
}
g_string_append(buffer, utf8_name);
if (! doc_is_utf8)
g_free(utf8_name);
if (include_line)
g_string_append_printf(buffer, " [%lu]", tag->line);
return buffer->str;
}
// Returns NULL if the tag is not a variable or callable
static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag, gboolean include_scope)
{
gchar *utf8_name = tm_parser_format_function(tag->lang, tag->name,
tag->arglist, tag->var_type, tag->scope);
if (!utf8_name && tag->var_type &&
tag->type & (tm_tag_field_t | tm_tag_member_t | tm_tag_variable_t | tm_tag_externvar_t))
{
gchar *scope = include_scope ? tag->scope : NULL;
utf8_name = tm_parser_format_variable(tag->lang, tag->name, tag->var_type, scope);
}
/* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
* for None at this point completely */
if (utf8_name != NULL &&
! utils_str_equal(doc->encoding, "UTF-8") &&
! utils_str_equal(doc->encoding, "None"))
{
SETPTR(utf8_name,
encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
}
return utf8_name;
}
static const gchar *get_parent_name(const TMTag *tag)
{
return !EMPTY(tag->scope) ? tag->scope : NULL;
}
static GtkTreeIter *get_tag_type_iter(TMParserType lang, TMTagType tag_type)
{
/* TODO: tm_parser_get_sidebar_group() goes through groups one by one.
* If this happens to be slow for tree construction, create a lookup
* table for them. */
gint group = tm_parser_get_sidebar_group(lang, tag_type);
if (group < 0)
return NULL;
return &tv_iters[group];
}
static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
{
GdkPixbuf *icon = NULL;
/* copy parent icon */
gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
SYMBOLS_COLUMN_ICON, &icon, -1);
return icon;
}
static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
{
const TMTag *t1 = v1;
const TMTag *t2 = v2;
return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
utils_str_equal(t1->scope, t2->scope) &&
/* include arglist in match to support e.g. C++ overloading */
utils_str_equal(t1->arglist, t2->arglist));
}
/* inspired from g_str_hash() */
static guint tag_hash(gconstpointer v)
{
const TMTag *tag = v;
const gchar *p;
guint32 h = 5381;
h = (h << 5) + h + tag->type;
for (p = tag->name; *p != '\0'; p++)
h = (h << 5) + h + *p;
if (tag->scope)
{
for (p = tag->scope; *p != '\0'; p++)
h = (h << 5) + h + *p;
}
/* for e.g. C++ overloading */
if (tag->arglist)
{
for (p = tag->arglist; *p != '\0'; p++)
h = (h << 5) + h + *p;
}
return h;
}
/* like gtk_tree_view_expand_to_path() but with an iter */
static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
{
GtkTreeModel *model = gtk_tree_view_get_model(view);
GtkTreePath *path = gtk_tree_model_get_path(model, iter);
gtk_tree_view_expand_to_path(view, path);
gtk_tree_path_free(path);
}
/* like gtk_tree_store_remove() but finds the next iter at any level */
static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
{
GtkTreeIter parent;
gboolean has_parent;
gboolean cont;
has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
cont = gtk_tree_store_remove(store, iter);
/* if there is no next at this level but there is a parent iter, continue from it */
if (! cont && has_parent)
{
*iter = parent;
cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
}
return cont;
}
static gint tree_search_func(gconstpointer key, gpointer user_data)
{
TreeSearchData *data = user_data;
gint parent_line = GPOINTER_TO_INT(key);
gboolean new_nearest;
if (data->found_line == -1)
data->found_line = parent_line; /* initial value */
new_nearest = ABS(data->line - parent_line) < ABS(data->line - data->found_line);
if (parent_line > data->line)
{
if (new_nearest && !data->lower)
data->found_line = parent_line;
return -1;
}
if (new_nearest)
data->found_line = parent_line;
if (parent_line < data->line)
return 1;
return 0;
}
static gint tree_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
{
return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
}
static void parents_table_tree_value_free(gpointer data)
{
g_slice_free(GtkTreeIter, data);
}
/* adds a new element in the parent table if its key is known. */
static void update_parents_table(GHashTable *table, const TMTag *tag, const GtkTreeIter *iter)
{
const gchar *name;
gchar *name_free = NULL;
GTree *tree;
if (EMPTY(tag->scope))
{
/* simple case, just use the tag name */
name = tag->name;
}
else if (! tm_parser_has_full_scope(tag->lang))
{
/* if the parser doesn't use fully qualified scope, use the name alone but
* prevent Foo::Foo from making parent = child */
if (utils_str_equal(tag->scope, tag->name))
name = NULL;
else
name = tag->name;
}
else
{
/* build the fully qualified scope as get_parent_name() would return it for a child tag */
name_free = g_strconcat(tag->scope, tm_parser_scope_separator(tag->lang), tag->name, NULL);
name = name_free;
}
if (name && g_hash_table_lookup_extended(table, name, NULL, (gpointer *) &tree))
{
if (!tree)
{
tree = g_tree_new_full(tree_cmp, NULL, NULL, parents_table_tree_value_free);
g_hash_table_insert(table, name_free ? name_free : g_strdup(name), tree);
name_free = NULL;
}
g_tree_insert(tree, GINT_TO_POINTER(tag->line), g_slice_dup(GtkTreeIter, iter));
}
g_free(name_free);
}
static GtkTreeIter *parents_table_lookup(GHashTable *table, const gchar *name, guint line)
{
GtkTreeIter *parent_search = NULL;
GTree *tree;
tree = g_hash_table_lookup(table, name);
if (tree)
{
TreeSearchData user_data = {-1, line, TRUE};
/* search parent candidates for the one with the nearest
* line number which is lower than the tag's line number */
g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
parent_search = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
}
return parent_search;
}
static void parents_table_value_free(gpointer data)
{
GTree *tree = data;
if (tree)
g_tree_destroy(tree);
}
/* inserts a @data in @table on key @tag.
* previous data is not overwritten if the key is duplicated, but rather the
* two values are kept in a list
*
* table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
{
GTree *tree = g_hash_table_lookup(table, tag);
if (!tree)
{
tree = g_tree_new_full(tree_cmp, NULL, NULL, NULL);
g_hash_table_insert(table, tag, tree);
}
GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
list = g_list_prepend(list, data);
g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
}
/* looks up the entry in @table that best matches @tag.
* if there is more than one candidate, the one that has closest line position to @tag is chosen */
static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
{
TreeSearchData user_data = {-1, tag->line, FALSE};
GTree *tree = g_hash_table_lookup(table, tag);
if (tree)
{
GList *list;
g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
list = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
/* return the first value in the list - we don't care which of the
* tags with identical names defined on the same line we get */
if (list)
return list->data;
}
return NULL;
}
/* removes the element at @tag from @table.
* @tag must be the exact pointer used at insertion time */
static void tags_table_remove(GHashTable *table, TMTag *tag)
{
GTree *tree = g_hash_table_lookup(table, tag);
if (tree)
{
GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
if (list)
{
GList *node;
/* should always be the first element as we returned the first one in
* tags_table_lookup() */
foreach_list(node, list)
{
if (((GList *) node->data)->data == tag)
break;
}
list = g_list_delete_link(list, node);
if (!list)
g_tree_remove(tree, GINT_TO_POINTER(tag->line));
else
g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
}
}
}
static gboolean tags_table_tree_value_free(gpointer key, gpointer value, gpointer data)
{
GList *list = value;
g_list_free(list);
return FALSE;
}
static void tags_table_value_free(gpointer data)
{
GTree *tree = data;
if (tree)
{
/* free any leftover elements. note that we can't register a value_free_func when
* creating the tree because we only want to free it when destroying the tree,
* not when inserting a duplicate (we handle this manually) */
g_tree_foreach(tree, tags_table_tree_value_free, NULL);
g_tree_destroy(tree);
}
}
/*
* Updates the tag tree for a document with the tags in *list.
* @param doc a document
* @param tags a pointer to a GList* holding the tags to add/update. This
* list may be updated, removing updated elements.
*
* The update is done in two passes:
* 1) walking the current tree, update tags that still exist and remove the
* obsolescent ones;
* 2) walking the remaining (non updated) tags, adds them in the list.
*
* For better performances, we use 2 hash tables:
* - one containing all the tags for lookup in the first pass (actually stores a
* reference in the tags list for removing it efficiently), avoiding list search
* on each tag;
* - the other holding "tag-name":row references for tags having children, used to
* lookup for a parent in both passes, avoiding tree traversal.
*/
static void update_tree_tags(GeanyDocument *doc, GList **tags)
{
GtkTreeStore *store = doc->priv->tag_store;
GtkTreeModel *model = GTK_TREE_MODEL(store);
GHashTable *parents_table;
GHashTable *tags_table;
GtkTreeIter iter;
gboolean cont;
GList *item;
/* Build hash tables holding tags and parents */
/* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>>
* where tag_name might be a fully qualified name (with scope) if the language
* parser reports scope properly (see tm_parser_has_full_scope()). */
parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, parents_table_value_free);
/* tags table is another representation of the @tags list,
* GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, tags_table_value_free);
foreach_list(item, *tags)
{
TMTag *tag = item->data;
const gchar *parent_name;
tags_table_insert(tags_table, tag, item);
parent_name = get_parent_name(tag);
if (parent_name)
g_hash_table_insert(parents_table, g_strdup(parent_name), NULL);
}
/* First pass, update existing rows or delete them.
* It is OK to delete them since we walk top down so we would remove
* parents before checking for their children, thus never implicitly
* deleting an updated child */
cont = gtk_tree_model_get_iter_first(model, &iter);
while (cont)
{
TMTag *tag;
gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
if (! tag) /* most probably a toplevel, skip it */
cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
else
{
GList *found_item;
found_item = tags_table_lookup(tags_table, tag);
if (! found_item) /* tag doesn't exist, remove it */
cont = tree_store_remove_row(store, &iter);
else /* tag still exist, update it */
{
const gchar *parent_name;
TMTag *found = found_item->data;
parent_name = get_parent_name(found);
/* if parent is unknown, ignore it */
if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
parent_name = NULL;
if (!tm_tags_equal(tag, found))
{
const gchar *name;
gchar *tooltip;
/* only update fields that (can) have changed (name that holds line
* number, tooltip, and the tag itself) */
name = get_symbol_name(doc, found, parent_name == NULL, TRUE);
tooltip = get_symbol_tooltip(doc, found, FALSE);
gtk_tree_store_set(store, &iter,
SYMBOLS_COLUMN_NAME, name,
SYMBOLS_COLUMN_TOOLTIP, tooltip,
SYMBOLS_COLUMN_TAG, found,
-1);
g_free(tooltip);
}
update_parents_table(parents_table, found, &iter);
/* remove the updated tag from the table and list */
tags_table_remove(tags_table, found);
*tags = g_list_delete_link(*tags, found_item);
cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
}
tm_tag_unref(tag);
}
}
/* Second pass, now we have a tree cleaned up from invalid rows,
* we simply add new ones */
foreach_list (item, *tags)
{
TMTag *tag = item->data;
GtkTreeIter *parent, *parent_group;
parent_group = get_tag_type_iter(tag->lang, tag->type);
/* tv_iters[0] is reserved for the "Symbols" group */
parent = ui_prefs.symbols_group_by_type ? parent_group : &tv_iters[0];
if (parent_group)
{
gboolean expand;
const gchar *name;
const gchar *parent_name;
gchar *tooltip;
GdkPixbuf *icon = get_child_icon(store, parent_group);
parent_name = get_parent_name(tag);
if (parent_name)
{
GtkTreeIter *parent_search = parents_table_lookup(parents_table, parent_name, tag->line);