Add parentheses in an if to make the condition more clear
[geany-mirror.git] / src / symbols.c
blob16a17b060f5f151f546e893c532d9dc6b75a369c
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include "symbols.h"
41 #include "app.h"
42 #include "callbacks.h" /* FIXME: for ignore_callback */
43 #include "documentprivate.h"
44 #include "editor.h"
45 #include "encodings.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "sciwrappers.h"
51 #include "sidebar.h"
52 #include "support.h"
53 #include "tm_parser.h"
54 #include "tm_tag.h"
55 #include "ui_utils.h"
56 #include "utils.h"
58 #include "SciLexer.h"
60 #include "gtkcompat.h"
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdlib.h>
67 static gchar **html_entities = NULL;
69 typedef struct
71 gboolean tags_loaded;
72 const gchar *tag_file;
73 } TagFileInfo;
75 /* Check before adding any more tags files, usually they should be downloaded separately. */
76 enum /* Geany tag files */
78 GTF_C,
79 GTF_PASCAL,
80 GTF_PHP,
81 GTF_HTML_ENTITIES,
82 GTF_LATEX,
83 GTF_PYTHON,
84 GTF_MAX
87 static TagFileInfo tag_file_info[GTF_MAX] =
89 {FALSE, "c99.tags"},
90 {FALSE, "pascal.tags"},
91 {FALSE, "php.tags"},
92 {FALSE, "html_entities.tags"},
93 {FALSE, "latex.tags"},
94 {FALSE, "python.tags"}
97 static GPtrArray *top_level_iter_names = NULL;
99 enum
101 ICON_CLASS,
102 ICON_MACRO,
103 ICON_MEMBER,
104 ICON_METHOD,
105 ICON_NAMESPACE,
106 ICON_OTHER,
107 ICON_STRUCT,
108 ICON_VAR,
109 ICON_NONE,
110 N_ICONS = ICON_NONE
113 static struct
115 const gchar *icon_name;
116 GdkPixbuf *pixbuf;
118 symbols_icons[N_ICONS] = {
119 [ICON_CLASS] = { "classviewer-class", NULL },
120 [ICON_MACRO] = { "classviewer-macro", NULL },
121 [ICON_MEMBER] = { "classviewer-member", NULL },
122 [ICON_METHOD] = { "classviewer-method", NULL },
123 [ICON_NAMESPACE] = { "classviewer-namespace", NULL },
124 [ICON_OTHER] = { "classviewer-other", NULL },
125 [ICON_STRUCT] = { "classviewer-struct", NULL },
126 [ICON_VAR] = { "classviewer-var", NULL },
129 static struct
131 GtkWidget *expand_all;
132 GtkWidget *collapse_all;
133 GtkWidget *sort_by_name;
134 GtkWidget *sort_by_appearance;
135 GtkWidget *find_usage;
136 GtkWidget *find_doc_usage;
137 GtkWidget *find_in_files;
139 symbol_menu;
141 static void html_tags_loaded(void);
142 static void load_user_tags(GeanyFiletypeID ft_id);
144 /* get the tags_ignore list, exported by tagmanager's options.c */
145 extern gchar **c_tags_ignore;
147 /* ignore certain tokens when parsing C-like syntax.
148 * Also works for reloading. */
149 static void load_c_ignore_tags(void)
151 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
152 gchar *content;
154 if (g_file_get_contents(path, &content, NULL, NULL))
156 /* historically we ignore the glib _DECLS for tag generation */
157 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
159 g_strfreev(c_tags_ignore);
160 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
161 g_free(content);
163 g_free(path);
167 void symbols_reload_config_files(void)
169 load_c_ignore_tags();
173 static gsize get_tag_count(void)
175 GPtrArray *tags = tm_get_workspace()->global_tags;
176 gsize count = tags ? tags->len : 0;
178 return count;
182 /* wrapper for tm_workspace_load_global_tags().
183 * note that the tag count only counts new global tags added - if a tag has the same name,
184 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
185 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
187 gboolean result;
188 gsize old_tag_count = get_tag_count();
190 result = tm_workspace_load_global_tags(tags_file, ft->lang);
191 if (result)
193 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
194 (guint) (get_tag_count() - old_tag_count));
196 return result;
200 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
201 * This provides autocompletion, calltips, etc. */
202 void symbols_global_tags_loaded(guint file_type_idx)
204 TagFileInfo *tfi;
205 gint tag_type;
207 /* load ignore list for C/C++ parser */
208 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
209 c_tags_ignore == NULL)
211 load_c_ignore_tags();
214 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
215 return;
217 /* load config in case of custom filetypes */
218 filetypes_load_config(file_type_idx, FALSE);
220 load_user_tags(file_type_idx);
222 switch (file_type_idx)
224 case GEANY_FILETYPES_PHP:
225 case GEANY_FILETYPES_HTML:
226 html_tags_loaded();
228 switch (file_type_idx)
230 case GEANY_FILETYPES_CPP:
231 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
232 /* no C++ tagfile yet */
233 return;
234 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
235 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
236 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
237 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
238 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
239 default:
240 return;
242 tfi = &tag_file_info[tag_type];
244 if (! tfi->tags_loaded)
246 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
248 symbols_load_global_tags(fname, filetypes[file_type_idx]);
249 tfi->tags_loaded = TRUE;
250 g_free(fname);
255 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
256 static void html_tags_loaded(void)
258 TagFileInfo *tfi;
260 if (cl_options.ignore_global_tags)
261 return;
263 tfi = &tag_file_info[GTF_HTML_ENTITIES];
264 if (! tfi->tags_loaded)
266 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
268 html_entities = utils_read_file_in_array(file);
269 tfi->tags_loaded = TRUE;
270 g_free(file);
275 GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
277 guint j;
278 TMTag *tag;
279 GString *s = NULL;
280 GPtrArray *typedefs;
281 TMParserType tag_lang;
283 if (global)
284 typedefs = app->tm_workspace->global_typename_array;
285 else
286 typedefs = app->tm_workspace->typename_array;
288 if ((typedefs) && (typedefs->len > 0))
290 const gchar *last_name = "";
292 s = g_string_sized_new(typedefs->len * 10);
293 for (j = 0; j < typedefs->len; ++j)
295 tag = TM_TAG(typedefs->pdata[j]);
296 tag_lang = tag->lang;
298 if (tag->name && tm_tag_langs_compatible(lang, tag_lang) &&
299 strcmp(tag->name, last_name) != 0)
301 if (j != 0)
302 g_string_append_c(s, ' ');
303 g_string_append(s, tag->name);
304 last_name = tag->name;
308 return s;
312 /** Gets the context separator used by the tag manager for a particular file
313 * type.
314 * @param ft_id File type identifier.
315 * @return The context separator string.
317 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
318 * without a context separator.
320 * @since 0.19
322 GEANY_API_SYMBOL
323 const gchar *symbols_get_context_separator(gint ft_id)
325 return tm_tag_context_separator(filetypes[ft_id]->lang);
329 const gchar **symbols_get_html_entities(void)
331 if (html_entities == NULL)
332 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
334 return (const gchar **) html_entities;
338 /* sort by name, then line */
339 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
341 gint ret;
343 if (tag_a == NULL || tag_b == NULL)
344 return 0;
346 if (tag_a->name == NULL)
347 return -(tag_a->name != tag_b->name);
349 if (tag_b->name == NULL)
350 return tag_a->name != tag_b->name;
352 ret = strcmp(tag_a->name, tag_b->name);
353 if (ret == 0)
355 return tag_a->line - tag_b->line;
357 return ret;
361 /* sort by line, then scope */
362 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
364 const TMTag *tag_a = TM_TAG(a);
365 const TMTag *tag_b = TM_TAG(b);
366 gint ret;
368 if (a == NULL || b == NULL)
369 return 0;
371 ret = tag_a->line - tag_b->line;
372 if (ret == 0)
374 if (tag_a->scope == NULL)
375 return -(tag_a->scope != tag_b->scope);
376 if (tag_b->scope == NULL)
377 return tag_a->scope != tag_b->scope;
378 else
379 return strcmp(tag_a->scope, tag_b->scope);
381 return ret;
385 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
387 GList *tag_names = NULL;
388 TMTag *tag;
389 guint i;
391 g_return_val_if_fail(doc, NULL);
393 if (! doc->tm_file || ! doc->tm_file->tags_array)
394 return NULL;
396 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
398 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
399 if (G_UNLIKELY(tag == NULL))
400 return NULL;
402 if (tag->type & tag_types)
404 tag_names = g_list_prepend(tag_names, tag);
407 tag_names = g_list_sort(tag_names, compare_symbol_lines);
408 return tag_names;
412 /* amount of types in the symbol list (currently max. 8 are used) */
413 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
415 struct TreeviewSymbols
417 GtkTreeIter tag_function;
418 GtkTreeIter tag_class;
419 GtkTreeIter tag_macro;
420 GtkTreeIter tag_member;
421 GtkTreeIter tag_variable;
422 GtkTreeIter tag_externvar;
423 GtkTreeIter tag_namespace;
424 GtkTreeIter tag_struct;
425 GtkTreeIter tag_interface;
426 GtkTreeIter tag_type;
427 GtkTreeIter tag_other;
428 } tv_iters;
431 static void init_tag_iters(void)
433 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
434 * filetypes(e.g. config file to Python crashes Geany without this) */
435 tv_iters.tag_function.stamp = -1;
436 tv_iters.tag_class.stamp = -1;
437 tv_iters.tag_member.stamp = -1;
438 tv_iters.tag_macro.stamp = -1;
439 tv_iters.tag_variable.stamp = -1;
440 tv_iters.tag_externvar.stamp = -1;
441 tv_iters.tag_namespace.stamp = -1;
442 tv_iters.tag_struct.stamp = -1;
443 tv_iters.tag_interface.stamp = -1;
444 tv_iters.tag_type.stamp = -1;
445 tv_iters.tag_other.stamp = -1;
449 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
451 static GtkIconTheme *icon_theme = NULL;
452 static gint x = -1;
454 if (G_UNLIKELY(x < 0))
456 gint dummy;
457 icon_theme = gtk_icon_theme_get_default();
458 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
460 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
464 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
466 GtkTreeModel *model = GTK_TREE_MODEL(store);
468 if (!gtk_tree_model_get_iter_first(model, iter))
469 return FALSE;
472 gchar *candidate;
474 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
475 /* FIXME: what if 2 different items have the same name?
476 * this should never happen, but might be caused by a typo in a translation */
477 if (utils_str_equal(candidate, title))
479 g_free(candidate);
480 return TRUE;
482 else
483 g_free(candidate);
485 while (gtk_tree_model_iter_next(model, iter));
487 return FALSE;
491 /* Adds symbol list groups in (iter*, title) pairs.
492 * The list must be ended with NULL. */
493 static void G_GNUC_NULL_TERMINATED
494 tag_list_add_groups(GtkTreeStore *tree_store, ...)
496 va_list args;
497 GtkTreeIter *iter;
499 g_return_if_fail(top_level_iter_names);
501 va_start(args, tree_store);
502 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
504 gchar *title = va_arg(args, gchar*);
505 guint icon_id = va_arg(args, guint);
506 GdkPixbuf *icon = NULL;
508 if (icon_id < N_ICONS)
509 icon = symbols_icons[icon_id].pixbuf;
511 g_assert(title != NULL);
512 g_ptr_array_add(top_level_iter_names, title);
514 if (!find_toplevel_iter(tree_store, iter, title))
515 gtk_tree_store_append(tree_store, iter, NULL);
517 if (icon)
518 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
519 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
521 va_end(args);
525 static void add_top_level_items(GeanyDocument *doc)
527 GeanyFiletypeID ft_id = doc->file_type->id;
528 GtkTreeStore *tag_store = doc->priv->tag_store;
530 if (top_level_iter_names == NULL)
531 top_level_iter_names = g_ptr_array_new();
532 else
533 g_ptr_array_set_size(top_level_iter_names, 0);
535 init_tag_iters();
537 switch (ft_id)
539 case GEANY_FILETYPES_DIFF:
541 tag_list_add_groups(tag_store,
542 &(tv_iters.tag_function), _("Files"), ICON_NONE, NULL);
543 break;
545 case GEANY_FILETYPES_DOCBOOK:
547 tag_list_add_groups(tag_store,
548 &(tv_iters.tag_function), _("Chapter"), ICON_NONE,
549 &(tv_iters.tag_class), _("Section"), ICON_NONE,
550 &(tv_iters.tag_member), _("Sect1"), ICON_NONE,
551 &(tv_iters.tag_macro), _("Sect2"), ICON_NONE,
552 &(tv_iters.tag_variable), _("Sect3"), ICON_NONE,
553 &(tv_iters.tag_struct), _("Appendix"), ICON_NONE,
554 &(tv_iters.tag_other), _("Other"), ICON_NONE,
555 NULL);
556 break;
558 case GEANY_FILETYPES_HASKELL:
559 tag_list_add_groups(tag_store,
560 &tv_iters.tag_namespace, _("Module"), ICON_NONE,
561 &tv_iters.tag_type, _("Types"), ICON_NONE,
562 &tv_iters.tag_macro, _("Type constructors"), ICON_NONE,
563 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
564 NULL);
565 break;
566 case GEANY_FILETYPES_COBOL:
567 tag_list_add_groups(tag_store,
568 &tv_iters.tag_class, _("Program"), ICON_CLASS,
569 &tv_iters.tag_function, _("File"), ICON_METHOD,
570 &tv_iters.tag_namespace, _("Sections"), ICON_NAMESPACE,
571 &tv_iters.tag_macro, _("Paragraph"), ICON_OTHER,
572 &tv_iters.tag_struct, _("Group"), ICON_STRUCT,
573 &tv_iters.tag_variable, _("Data"), ICON_VAR,
574 NULL);
575 break;
576 case GEANY_FILETYPES_CONF:
577 tag_list_add_groups(tag_store,
578 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
579 &tv_iters.tag_macro, _("Keys"), ICON_VAR,
580 NULL);
581 break;
582 case GEANY_FILETYPES_NSIS:
583 tag_list_add_groups(tag_store,
584 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
585 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
586 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
587 NULL);
588 break;
589 case GEANY_FILETYPES_LATEX:
591 tag_list_add_groups(tag_store,
592 &(tv_iters.tag_function), _("Command"), ICON_NONE,
593 &(tv_iters.tag_class), _("Environment"), ICON_NONE,
594 &(tv_iters.tag_member), _("Section"), ICON_NONE,
595 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
596 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
597 &(tv_iters.tag_struct), _("Label"), ICON_NONE,
598 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
599 &(tv_iters.tag_other), _("Other"), ICON_NONE,
600 NULL);
601 break;
603 case GEANY_FILETYPES_MATLAB:
605 tag_list_add_groups(tag_store,
606 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
607 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
608 NULL);
609 break;
611 case GEANY_FILETYPES_ABAQUS:
613 tag_list_add_groups(tag_store,
614 &(tv_iters.tag_class), _("Parts"), ICON_NONE,
615 &(tv_iters.tag_member), _("Assembly"), ICON_NONE,
616 &(tv_iters.tag_namespace), _("Steps"), ICON_NONE,
617 NULL);
618 break;
620 case GEANY_FILETYPES_R:
622 tag_list_add_groups(tag_store,
623 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
624 &(tv_iters.tag_other), _("Other"), ICON_NONE,
625 NULL);
626 break;
628 case GEANY_FILETYPES_RUST:
630 tag_list_add_groups(tag_store,
631 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
632 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
633 &(tv_iters.tag_interface), _("Traits"), ICON_CLASS,
634 &(tv_iters.tag_class), _("Implementations"), ICON_CLASS,
635 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
636 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
637 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
638 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO,
639 &(tv_iters.tag_member), _("Methods"), ICON_MEMBER,
640 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
641 NULL);
642 break;
644 case GEANY_FILETYPES_GO:
646 tag_list_add_groups(tag_store,
647 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
648 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
649 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
650 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
651 &(tv_iters.tag_type), _("Types"), ICON_STRUCT,
652 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
653 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
654 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
655 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
656 NULL);
657 break;
659 case GEANY_FILETYPES_PERL:
661 tag_list_add_groups(tag_store,
662 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
663 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
664 &(tv_iters.tag_macro), _("Labels"), ICON_NONE,
665 &(tv_iters.tag_type), _("Constants"), ICON_NONE,
666 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
667 NULL);
668 break;
670 case GEANY_FILETYPES_PHP:
671 case GEANY_FILETYPES_ZEPHIR:
673 tag_list_add_groups(tag_store,
674 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
675 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
676 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
677 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
678 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
679 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
680 &(tv_iters.tag_struct), _("Traits"), ICON_STRUCT,
681 NULL);
682 break;
684 case GEANY_FILETYPES_HTML:
686 tag_list_add_groups(tag_store,
687 &(tv_iters.tag_function), _("Functions"), ICON_NONE,
688 &(tv_iters.tag_member), _("Anchors"), ICON_NONE,
689 &(tv_iters.tag_namespace), _("H1 Headings"), ICON_NONE,
690 &(tv_iters.tag_class), _("H2 Headings"), ICON_NONE,
691 &(tv_iters.tag_variable), _("H3 Headings"), ICON_NONE,
692 NULL);
693 break;
695 case GEANY_FILETYPES_CSS:
697 tag_list_add_groups(tag_store,
698 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
699 &(tv_iters.tag_variable), _("ID Selectors"), ICON_VAR,
700 &(tv_iters.tag_struct), _("Type Selectors"), ICON_STRUCT, NULL);
701 break;
703 case GEANY_FILETYPES_REST:
704 case GEANY_FILETYPES_TXT2TAGS:
705 case GEANY_FILETYPES_ABC:
707 tag_list_add_groups(tag_store,
708 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
709 &(tv_iters.tag_member), _("Section"), ICON_NONE,
710 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
711 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
712 NULL);
713 break;
715 case GEANY_FILETYPES_ASCIIDOC:
717 tag_list_add_groups(tag_store,
718 &(tv_iters.tag_namespace), _("Document"), ICON_NONE,
719 &(tv_iters.tag_member), _("Section Level 1"), ICON_NONE,
720 &(tv_iters.tag_macro), _("Section Level 2"), ICON_NONE,
721 &(tv_iters.tag_variable), _("Section Level 3"), ICON_NONE,
722 &(tv_iters.tag_struct), _("Section Level 4"), ICON_NONE,
723 NULL);
724 break;
726 case GEANY_FILETYPES_RUBY:
728 tag_list_add_groups(tag_store,
729 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
730 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
731 &(tv_iters.tag_member), _("Singletons"), ICON_STRUCT,
732 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
733 NULL);
734 break;
736 case GEANY_FILETYPES_TCL:
738 tag_list_add_groups(tag_store,
739 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
740 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
741 &(tv_iters.tag_member), _("Methods"), ICON_METHOD,
742 &(tv_iters.tag_function), _("Procedures"), ICON_OTHER,
743 NULL);
744 break;
746 case GEANY_FILETYPES_PYTHON:
748 tag_list_add_groups(tag_store,
749 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
750 &(tv_iters.tag_member), _("Methods"), ICON_MACRO,
751 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
752 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
753 &(tv_iters.tag_externvar), _("Imports"), ICON_NAMESPACE,
754 NULL);
755 break;
757 case GEANY_FILETYPES_VHDL:
759 tag_list_add_groups(tag_store,
760 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
761 &(tv_iters.tag_class), _("Entities"), ICON_CLASS,
762 &(tv_iters.tag_struct), _("Architectures"), ICON_STRUCT,
763 &(tv_iters.tag_type), _("Types"), ICON_OTHER,
764 &(tv_iters.tag_function), _("Functions / Procedures"), ICON_METHOD,
765 &(tv_iters.tag_variable), _("Variables / Signals"), ICON_VAR,
766 &(tv_iters.tag_member), _("Processes / Blocks / Components"), ICON_MEMBER,
767 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
768 NULL);
769 break;
771 case GEANY_FILETYPES_VERILOG:
773 tag_list_add_groups(tag_store,
774 &(tv_iters.tag_type), _("Events"), ICON_MACRO,
775 &(tv_iters.tag_class), _("Modules"), ICON_CLASS,
776 &(tv_iters.tag_function), _("Functions / Tasks"), ICON_METHOD,
777 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
778 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
779 NULL);
780 break;
782 case GEANY_FILETYPES_JAVA:
784 tag_list_add_groups(tag_store,
785 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
786 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
787 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
788 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
789 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
790 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
791 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
792 NULL);
793 break;
795 case GEANY_FILETYPES_AS:
797 tag_list_add_groups(tag_store,
798 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
799 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
800 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
801 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
802 &(tv_iters.tag_member), _("Properties"), ICON_MEMBER,
803 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
804 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
805 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
806 NULL);
807 break;
809 case GEANY_FILETYPES_HAXE:
811 tag_list_add_groups(tag_store,
812 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
813 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
814 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
815 &(tv_iters.tag_type), _("Types"), ICON_MACRO,
816 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
817 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
818 NULL);
819 break;
821 case GEANY_FILETYPES_BASIC:
823 tag_list_add_groups(tag_store,
824 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
825 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
826 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
827 &(tv_iters.tag_struct), _("Types"), ICON_NAMESPACE,
828 &(tv_iters.tag_namespace), _("Labels"), ICON_MEMBER,
829 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
830 NULL);
831 break;
833 case GEANY_FILETYPES_F77:
834 case GEANY_FILETYPES_FORTRAN:
836 tag_list_add_groups(tag_store,
837 &(tv_iters.tag_namespace), _("Module"), ICON_CLASS,
838 &(tv_iters.tag_struct), _("Programs"), ICON_CLASS,
839 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
840 &(tv_iters.tag_function), _("Functions / Subroutines"), ICON_METHOD,
841 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
842 &(tv_iters.tag_class), _("Types"), ICON_CLASS,
843 &(tv_iters.tag_member), _("Components"), ICON_MEMBER,
844 &(tv_iters.tag_macro), _("Blocks"), ICON_MEMBER,
845 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
846 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
847 NULL);
848 break;
850 case GEANY_FILETYPES_ASM:
852 tag_list_add_groups(tag_store,
853 &(tv_iters.tag_namespace), _("Labels"), ICON_NAMESPACE,
854 &(tv_iters.tag_function), _("Macros"), ICON_METHOD,
855 &(tv_iters.tag_macro), _("Defines"), ICON_MACRO,
856 &(tv_iters.tag_struct), _("Types"), ICON_STRUCT,
857 NULL);
858 break;
860 case GEANY_FILETYPES_MAKE:
861 tag_list_add_groups(tag_store,
862 &tv_iters.tag_function, _("Targets"), ICON_METHOD,
863 &tv_iters.tag_macro, _("Macros"), ICON_MACRO,
864 NULL);
865 break;
866 case GEANY_FILETYPES_SQL:
868 tag_list_add_groups(tag_store,
869 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
870 &(tv_iters.tag_namespace), _("Procedures"), ICON_NAMESPACE,
871 &(tv_iters.tag_struct), _("Indexes"), ICON_STRUCT,
872 &(tv_iters.tag_class), _("Tables"), ICON_CLASS,
873 &(tv_iters.tag_macro), _("Triggers"), ICON_MACRO,
874 &(tv_iters.tag_member), _("Views"), ICON_VAR,
875 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
876 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
877 NULL);
878 break;
880 case GEANY_FILETYPES_D:
881 default:
883 if (ft_id == GEANY_FILETYPES_D)
884 tag_list_add_groups(tag_store,
885 &(tv_iters.tag_namespace), _("Module"), ICON_NONE, NULL);
886 else
887 tag_list_add_groups(tag_store,
888 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE, NULL);
890 tag_list_add_groups(tag_store,
891 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
892 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
893 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
894 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
895 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
896 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
897 NULL);
899 if (ft_id != GEANY_FILETYPES_D)
901 tag_list_add_groups(tag_store,
902 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO, NULL);
904 tag_list_add_groups(tag_store,
905 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
906 &(tv_iters.tag_externvar), _("Extern Variables"), ICON_VAR,
907 &(tv_iters.tag_other), _("Other"), ICON_OTHER, NULL);
913 /* removes toplevel items that have no children */
914 static void hide_empty_rows(GtkTreeStore *store)
916 GtkTreeIter iter;
917 gboolean cont = TRUE;
919 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
920 return; /* stop when first iter is invalid, i.e. no elements */
922 while (cont)
924 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
925 cont = gtk_tree_store_remove(store, &iter);
926 else
927 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
932 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
934 gchar *utf8_name;
935 const gchar *scope = tag->scope;
936 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
937 gboolean doc_is_utf8 = FALSE;
939 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
940 * for None at this point completely */
941 if (utils_str_equal(doc->encoding, "UTF-8") ||
942 utils_str_equal(doc->encoding, "None"))
943 doc_is_utf8 = TRUE;
944 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
945 * plugin might have called tm_source_file_update(), so check to be sure */
946 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
948 if (! doc_is_utf8)
949 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
950 -1, doc->encoding, TRUE);
951 else
952 utf8_name = tag->name;
954 if (utf8_name == NULL)
955 return NULL;
957 if (! buffer)
958 buffer = g_string_new(NULL);
959 else
960 g_string_truncate(buffer, 0);
962 /* check first char of scope is a wordchar */
963 if (!found_parent && scope &&
964 strpbrk(scope, GEANY_WORDCHARS) == scope)
966 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
968 g_string_append(buffer, scope);
969 g_string_append(buffer, sep);
971 g_string_append(buffer, utf8_name);
973 if (! doc_is_utf8)
974 g_free(utf8_name);
976 g_string_append_printf(buffer, " [%lu]", tag->line);
978 return buffer->str;
982 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
984 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
986 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
987 * for None at this point completely */
988 if (utf8_name != NULL &&
989 ! utils_str_equal(doc->encoding, "UTF-8") &&
990 ! utils_str_equal(doc->encoding, "None"))
992 SETPTR(utf8_name,
993 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
996 return utf8_name;
1000 /* find the last word in "foo::bar::blah", e.g. "blah" */
1001 static const gchar *get_parent_name(const TMTag *tag, GeanyFiletypeID ft_id)
1003 const gchar *scope = tag->scope;
1004 const gchar *separator = symbols_get_context_separator(ft_id);
1005 const gchar *str, *ptr;
1007 if (!scope)
1008 return NULL;
1010 str = scope;
1012 while (1)
1014 ptr = strstr(str, separator);
1015 if (ptr)
1017 str = ptr + strlen(separator);
1019 else
1020 break;
1023 return !EMPTY(str) ? str : NULL;
1027 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type)
1029 GtkTreeIter *iter = NULL;
1031 switch (tag_type)
1033 case tm_tag_prototype_t:
1034 case tm_tag_method_t:
1035 case tm_tag_function_t:
1037 iter = &tv_iters.tag_function;
1038 break;
1040 case tm_tag_externvar_t:
1042 iter = &tv_iters.tag_externvar;
1043 break;
1045 case tm_tag_macro_t:
1046 case tm_tag_macro_with_arg_t:
1048 iter = &tv_iters.tag_macro;
1049 break;
1051 case tm_tag_class_t:
1053 iter = &tv_iters.tag_class;
1054 break;
1056 case tm_tag_member_t:
1057 case tm_tag_field_t:
1059 iter = &tv_iters.tag_member;
1060 break;
1062 case tm_tag_typedef_t:
1063 case tm_tag_enum_t:
1065 iter = &tv_iters.tag_type;
1066 break;
1068 case tm_tag_union_t:
1069 case tm_tag_struct_t:
1071 iter = &tv_iters.tag_struct;
1072 break;
1074 case tm_tag_interface_t:
1075 iter = &tv_iters.tag_interface;
1076 break;
1077 case tm_tag_variable_t:
1079 iter = &tv_iters.tag_variable;
1080 break;
1082 case tm_tag_namespace_t:
1083 case tm_tag_package_t:
1085 iter = &tv_iters.tag_namespace;
1086 break;
1088 default:
1090 iter = &tv_iters.tag_other;
1093 if (G_LIKELY(iter->stamp != -1))
1094 return iter;
1095 else
1096 return NULL;
1100 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1102 GdkPixbuf *icon = NULL;
1104 if (parent == &tv_iters.tag_other)
1106 return g_object_ref(symbols_icons[ICON_VAR].pixbuf);
1108 /* copy parent icon */
1109 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1110 SYMBOLS_COLUMN_ICON, &icon, -1);
1111 return icon;
1115 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1117 const TMTag *t1 = v1;
1118 const TMTag *t2 = v2;
1120 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1121 utils_str_equal(t1->scope, t2->scope) &&
1122 /* include arglist in match to support e.g. C++ overloading */
1123 utils_str_equal(t1->arglist, t2->arglist));
1127 /* inspired from g_str_hash() */
1128 static guint tag_hash(gconstpointer v)
1130 const TMTag *tag = v;
1131 const gchar *p;
1132 guint32 h = 5381;
1134 h = (h << 5) + h + tag->type;
1135 for (p = tag->name; *p != '\0'; p++)
1136 h = (h << 5) + h + *p;
1137 if (tag->scope)
1139 for (p = tag->scope; *p != '\0'; p++)
1140 h = (h << 5) + h + *p;
1142 /* for e.g. C++ overloading */
1143 if (tag->arglist)
1145 for (p = tag->arglist; *p != '\0'; p++)
1146 h = (h << 5) + h + *p;
1149 return h;
1153 /* like gtk_tree_view_expand_to_path() but with an iter */
1154 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1156 GtkTreeModel *model = gtk_tree_view_get_model(view);
1157 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1159 gtk_tree_view_expand_to_path(view, path);
1160 gtk_tree_path_free(path);
1164 /* like gtk_tree_store_remove() but finds the next iter at any level */
1165 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1167 GtkTreeIter parent;
1168 gboolean has_parent;
1169 gboolean cont;
1171 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1172 cont = gtk_tree_store_remove(store, iter);
1173 /* if there is no next at this level but there is a parent iter, continue from it */
1174 if (! cont && has_parent)
1176 *iter = parent;
1177 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1180 return cont;
1184 /* adds a new element in the parent table if it's key is known.
1185 * duplicates are kept */
1186 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1187 const GtkTreeIter *iter)
1189 GList **list;
1190 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1191 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1193 if (! list)
1195 list = g_slice_alloc(sizeof *list);
1196 *list = NULL;
1197 g_hash_table_insert(table, tag->name, list);
1199 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1204 static void free_iter_slice_list(gpointer data)
1206 GList **list = data;
1208 if (list)
1210 GList *node;
1211 foreach_list(node, *list)
1212 g_slice_free(GtkTreeIter, node->data);
1213 g_list_free(*list);
1214 g_slice_free1(sizeof *list, list);
1219 /* inserts a @data in @table on key @tag.
1220 * previous data is not overwritten if the key is duplicated, but rather the
1221 * two values are kept in a list
1223 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1224 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1226 GList *list = g_hash_table_lookup(table, tag);
1227 list = g_list_prepend(list, data);
1228 g_hash_table_insert(table, tag, list);
1232 /* looks up the entry in @table that better matches @tag.
1233 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1234 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1236 GList *data = NULL;
1237 GList *node = g_hash_table_lookup(table, tag);
1238 if (node)
1240 glong delta;
1241 data = node->data;
1243 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1245 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1246 for (node = node->next; node; node = node->next)
1248 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1250 if (d < delta)
1252 data = node->data;
1253 delta = d;
1257 #undef TAG_DELTA
1260 return data;
1264 /* removes the element at @tag from @table.
1265 * @tag must be the exact pointer used at insertion time */
1266 static void tags_table_remove(GHashTable *table, TMTag *tag)
1268 GList *list = g_hash_table_lookup(table, tag);
1269 if (list)
1271 GList *node;
1272 foreach_list(node, list)
1274 if (((GList *) node->data)->data == tag)
1275 break;
1277 list = g_list_delete_link(list, node);
1278 if (list)
1279 g_hash_table_insert(table, tag, list);
1280 else
1281 g_hash_table_remove(table, tag);
1286 static void tags_table_destroy(GHashTable *table)
1288 /* free any leftover elements. note that we can't register a value_free_func when
1289 * creating the hash table because we only want to free it when destroying the table,
1290 * not when inserting a duplicate (we handle this manually) */
1291 GHashTableIter iter;
1292 gpointer value;
1294 g_hash_table_iter_init(&iter, table);
1295 while (g_hash_table_iter_next(&iter, NULL, &value))
1296 g_list_free(value);
1297 g_hash_table_destroy(table);
1302 * Updates the tag tree for a document with the tags in *list.
1303 * @param doc a document
1304 * @param tags a pointer to a GList* holding the tags to add/update. This
1305 * list may be updated, removing updated elements.
1307 * The update is done in two passes:
1308 * 1) walking the current tree, update tags that still exist and remove the
1309 * obsolescent ones;
1310 * 2) walking the remaining (non updated) tags, adds them in the list.
1312 * For better performances, we use 2 hash tables:
1313 * - one containing all the tags for lookup in the first pass (actually stores a
1314 * reference in the tags list for removing it efficiently), avoiding list search
1315 * on each tag;
1316 * - the other holding "tag-name":row references for tags having children, used to
1317 * lookup for a parent in both passes, avoiding tree traversal.
1319 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1321 GtkTreeStore *store = doc->priv->tag_store;
1322 GtkTreeModel *model = GTK_TREE_MODEL(store);
1323 GHashTable *parents_table;
1324 GHashTable *tags_table;
1325 GtkTreeIter iter;
1326 gboolean cont;
1327 GList *item;
1329 /* Build hash tables holding tags and parents */
1330 /* parent table holds "tag-name":GtkTreeIter */
1331 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1332 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1333 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1334 foreach_list(item, *tags)
1336 TMTag *tag = item->data;
1337 const gchar *name;
1339 tags_table_insert(tags_table, tag, item);
1341 name = get_parent_name(tag, doc->file_type->id);
1342 if (name)
1343 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1346 /* First pass, update existing rows or delete them.
1347 * It is OK to delete them since we walk top down so we would remove
1348 * parents before checking for their children, thus never implicitly
1349 * deleting an updated child */
1350 cont = gtk_tree_model_get_iter_first(model, &iter);
1351 while (cont)
1353 TMTag *tag;
1355 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1356 if (! tag) /* most probably a toplevel, skip it */
1357 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1358 else
1360 GList *found_item;
1362 found_item = tags_table_lookup(tags_table, tag);
1363 if (! found_item) /* tag doesn't exist, remove it */
1364 cont = tree_store_remove_row(store, &iter);
1365 else /* tag still exist, update it */
1367 const gchar *parent_name;
1368 TMTag *found = found_item->data;
1370 parent_name = get_parent_name(found, doc->file_type->id);
1371 /* if parent is unknown, ignore it */
1372 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1373 parent_name = NULL;
1375 if (!tm_tags_equal(tag, found))
1377 const gchar *name;
1378 gchar *tooltip;
1380 /* only update fields that (can) have changed (name that holds line
1381 * number, tooltip, and the tag itself) */
1382 name = get_symbol_name(doc, found, parent_name != NULL);
1383 tooltip = get_symbol_tooltip(doc, found);
1384 gtk_tree_store_set(store, &iter,
1385 SYMBOLS_COLUMN_NAME, name,
1386 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1387 SYMBOLS_COLUMN_TAG, found,
1388 -1);
1389 g_free(tooltip);
1392 update_parents_table(parents_table, found, parent_name, &iter);
1394 /* remove the updated tag from the table and list */
1395 tags_table_remove(tags_table, found);
1396 *tags = g_list_delete_link(*tags, found_item);
1398 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1401 tm_tag_unref(tag);
1405 /* Second pass, now we have a tree cleaned up from invalid rows,
1406 * we simply add new ones */
1407 foreach_list (item, *tags)
1409 TMTag *tag = item->data;
1410 GtkTreeIter *parent;
1412 parent = get_tag_type_iter(tag->type);
1413 if (G_UNLIKELY(! parent))
1414 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1415 else
1417 gboolean expand;
1418 const gchar *name;
1419 const gchar *parent_name;
1420 gchar *tooltip;
1421 GdkPixbuf *icon = get_child_icon(store, parent);
1423 parent_name = get_parent_name(tag, doc->file_type->id);
1424 if (parent_name)
1426 GList **candidates;
1427 GtkTreeIter *parent_search = NULL;
1429 /* walk parent candidates to find the better one.
1430 * if there are more than one, take the one that has the closest line number
1431 * after the tag we're searching the parent for */
1432 candidates = g_hash_table_lookup(parents_table, parent_name);
1433 if (candidates)
1435 GList *node;
1436 glong delta = G_MAXLONG;
1437 foreach_list(node, *candidates)
1439 TMTag *parent_tag;
1440 glong d;
1442 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1443 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1445 d = tag->line - parent_tag->line;
1446 if (! parent_search || (d >= 0 && d < delta))
1448 delta = d;
1449 parent_search = node->data;
1451 tm_tag_unref(parent_tag);
1455 if (parent_search)
1456 parent = parent_search;
1457 else
1458 parent_name = NULL;
1461 /* only expand to the iter if the parent was empty, otherwise we let the
1462 * folding as it was before (already expanded, or closed by the user) */
1463 expand = ! gtk_tree_model_iter_has_child(model, parent);
1465 /* insert the new element */
1466 name = get_symbol_name(doc, tag, parent_name != NULL);
1467 tooltip = get_symbol_tooltip(doc, tag);
1468 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1469 SYMBOLS_COLUMN_NAME, name,
1470 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1471 SYMBOLS_COLUMN_ICON, icon,
1472 SYMBOLS_COLUMN_TAG, tag,
1473 -1);
1474 g_free(tooltip);
1475 if (G_LIKELY(icon))
1476 g_object_unref(icon);
1478 update_parents_table(parents_table, tag, parent_name, &iter);
1480 if (expand)
1481 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1485 g_hash_table_destroy(parents_table);
1486 tags_table_destroy(tags_table);
1490 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1491 * is not stable, so the order is already lost. */
1492 static gint compare_top_level_names(const gchar *a, const gchar *b)
1494 guint i;
1495 const gchar *name;
1497 /* This should never happen as it would mean that two or more top
1498 * level items have the same name but it can happen by typos in the translations. */
1499 if (utils_str_equal(a, b))
1500 return 1;
1502 foreach_ptr_array(name, i, top_level_iter_names)
1504 if (utils_str_equal(name, a))
1505 return -1;
1506 if (utils_str_equal(name, b))
1507 return 1;
1509 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1510 return 0;
1514 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1515 GtkTreeIter *iter)
1517 /* if the tag has a parent tag, it should be at depth >= 2 */
1518 return !EMPTY(tag->scope) &&
1519 gtk_tree_store_iter_depth(store, iter) == 1;
1523 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1524 gpointer user_data)
1526 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1527 TMTag *tag_a, *tag_b;
1528 gint cmp;
1530 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1531 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1533 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1534 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1535 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1536 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1538 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1539 compare_symbol_lines(tag_a, tag_b);
1541 else
1543 gchar *astr, *bstr;
1545 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1546 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1548 /* if a is toplevel, b must be also */
1549 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1551 cmp = compare_top_level_names(astr, bstr);
1553 else
1555 /* this is what g_strcmp0() does */
1556 if (! astr)
1557 cmp = -(astr != bstr);
1558 else if (! bstr)
1559 cmp = astr != bstr;
1560 else
1562 cmp = strcmp(astr, bstr);
1564 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1565 if (tag_a && tag_b)
1566 if (!sort_by_name ||
1567 (utils_str_equal(tag_a->name, tag_b->name) &&
1568 utils_str_equal(tag_a->scope, tag_b->scope)))
1569 cmp = compare_symbol_lines(tag_a, tag_b);
1572 g_free(astr);
1573 g_free(bstr);
1575 tm_tag_unref(tag_a);
1576 tm_tag_unref(tag_b);
1578 return cmp;
1582 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1584 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1585 GINT_TO_POINTER(sort_by_name), NULL);
1587 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1591 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1593 GList *tags;
1595 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1597 tags = get_tag_list(doc, tm_tag_max_t);
1598 if (tags == NULL)
1599 return FALSE;
1601 /* FIXME: Not sure why we detached the model here? */
1603 /* disable sorting during update because the code doesn't support correctly
1604 * models that are currently being built */
1605 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1607 /* add grandparent type iters */
1608 add_top_level_items(doc);
1610 update_tree_tags(doc, &tags);
1611 g_list_free(tags);
1613 hide_empty_rows(doc->priv->tag_store);
1615 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1616 sort_mode = doc->priv->symbol_list_sort_mode;
1618 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1619 doc->priv->symbol_list_sort_mode = sort_mode;
1621 return TRUE;
1625 /* Detects a global tags filetype from the *.lang.* language extension.
1626 * Returns NULL if there was no matching TM language. */
1627 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1629 gchar *tags_ext;
1630 gchar *shortname = utils_strdupa(utf8_filename);
1631 GeanyFiletype *ft = NULL;
1633 tags_ext = g_strrstr(shortname, ".tags");
1634 if (tags_ext)
1636 *tags_ext = '\0'; /* remove .tags extension */
1637 ft = filetypes_detect_from_extension(shortname);
1638 if (ft->id != GEANY_FILETYPES_NONE)
1639 return ft;
1641 return NULL;
1645 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1646 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1647 * the relevant path.
1648 * Example:
1649 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1650 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1652 /* -E pre-process, -dD output user macros, -p prof info (?) */
1653 const char pre_process[] = "gcc -E -dD -p -I.";
1655 if (argc > 2)
1657 /* Create global taglist */
1658 int status;
1659 char *command;
1660 const char *tags_file = argv[1];
1661 char *utf8_fname;
1662 GeanyFiletype *ft;
1664 utf8_fname = utils_get_utf8_from_locale(tags_file);
1665 ft = detect_global_tags_filetype(utf8_fname);
1666 g_free(utf8_fname);
1668 if (ft == NULL)
1670 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1671 return 1;
1673 /* load config in case of custom filetypes */
1674 filetypes_load_config(ft->id, FALSE);
1676 /* load ignore list for C/C++ parser */
1677 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1678 load_c_ignore_tags();
1680 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1682 const gchar *cflags = getenv("CFLAGS");
1683 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1685 else
1686 command = NULL; /* don't preprocess */
1688 geany_debug("Generating %s tags file.", ft->name);
1689 tm_get_workspace();
1690 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1691 argc - 2, tags_file, ft->lang);
1692 g_free(command);
1693 symbols_finalize(); /* free c_tags_ignore data */
1694 if (! status)
1696 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1697 "were found.\n"));
1698 return 1;
1701 else
1703 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv[0]);
1704 g_printerr(_("Example:\n"
1705 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1706 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1707 return 1;
1709 return 0;
1713 void symbols_show_load_tags_dialog(void)
1715 GtkWidget *dialog;
1716 GtkFileFilter *filter;
1718 dialog = gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets.window),
1719 GTK_FILE_CHOOSER_ACTION_OPEN,
1720 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1721 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1722 NULL);
1723 gtk_widget_set_name(dialog, "GeanyDialog");
1724 filter = gtk_file_filter_new();
1725 gtk_file_filter_set_name(filter, _("Geany tags file (*.*.tags)"));
1726 gtk_file_filter_add_pattern(filter, "*.*.tags");
1727 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1729 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1731 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1732 GSList *item;
1734 for (item = flist; item != NULL; item = g_slist_next(item))
1736 gchar *fname = item->data;
1737 gchar *utf8_fname;
1738 GeanyFiletype *ft;
1740 utf8_fname = utils_get_utf8_from_locale(fname);
1741 ft = detect_global_tags_filetype(utf8_fname);
1743 if (ft != NULL && symbols_load_global_tags(fname, ft))
1744 /* For translators: the first wildcard is the filetype, the second the filename */
1745 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1746 filetypes_get_display_name(ft), utf8_fname);
1747 else
1748 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1750 g_free(utf8_fname);
1751 g_free(fname);
1753 g_slist_free(flist);
1755 gtk_widget_destroy(dialog);
1759 static void init_user_tags(void)
1761 GSList *file_list = NULL, *list = NULL;
1762 const GSList *node;
1763 gchar *dir;
1765 dir = g_build_filename(app->configdir, "tags", NULL);
1766 /* create the user tags dir for next time if it doesn't exist */
1767 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1768 utils_mkdir(dir, FALSE);
1769 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1771 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1772 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1773 g_free(dir);
1775 file_list = g_slist_concat(file_list, list);
1777 /* populate the filetype-specific tag files lists */
1778 for (node = file_list; node != NULL; node = node->next)
1780 gchar *fname = node->data;
1781 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1782 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1784 g_free(utf8_fname);
1786 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1787 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1788 else
1790 geany_debug("Unknown filetype for file '%s'.", fname);
1791 g_free(fname);
1795 /* don't need to delete list contents because they are now stored in
1796 * ft->priv->tag_files */
1797 g_slist_free(file_list);
1801 static void load_user_tags(GeanyFiletypeID ft_id)
1803 static guchar *tags_loaded = NULL;
1804 static gboolean init_tags = FALSE;
1805 const GSList *node;
1806 GeanyFiletype *ft = filetypes[ft_id];
1808 g_return_if_fail(ft_id > 0);
1810 if (!tags_loaded)
1811 tags_loaded = g_new0(guchar, filetypes_array->len);
1812 if (tags_loaded[ft_id])
1813 return;
1814 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1816 if (!init_tags)
1818 init_user_tags();
1819 init_tags = TRUE;
1822 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1824 const gchar *fname = node->data;
1826 symbols_load_global_tags(fname, ft);
1831 static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
1833 GeanyDocument *new_doc, *old_doc;
1835 g_return_if_fail(tag);
1837 old_doc = document_get_current();
1838 new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);
1840 if (new_doc)
1841 navqueue_goto_line(old_doc, new_doc, tag->line);
1845 /* FIXME: use the same icons as in the symbols tree defined in add_top_level_items() */
1846 static guint get_tag_class(const TMTag *tag)
1848 switch (tag->type)
1850 case tm_tag_prototype_t:
1851 case tm_tag_method_t:
1852 case tm_tag_function_t:
1853 return ICON_METHOD;
1854 case tm_tag_variable_t:
1855 case tm_tag_externvar_t:
1856 return ICON_VAR;
1857 case tm_tag_macro_t:
1858 case tm_tag_macro_with_arg_t:
1859 return ICON_MACRO;
1860 case tm_tag_class_t:
1861 return ICON_CLASS;
1862 case tm_tag_member_t:
1863 case tm_tag_field_t:
1864 return ICON_MEMBER;
1865 case tm_tag_typedef_t:
1866 case tm_tag_enum_t:
1867 case tm_tag_union_t:
1868 case tm_tag_struct_t:
1869 return ICON_STRUCT;
1870 case tm_tag_namespace_t:
1871 case tm_tag_package_t:
1872 return ICON_NAMESPACE;
1873 default:
1874 break;
1876 return ICON_STRUCT;
1880 /* positions a popup at the caret from the ScintillaObject in @p data */
1881 static void goto_popup_position_func(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1883 gint line_height;
1884 GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(menu));
1885 gint monitor_num;
1886 GdkRectangle monitor;
1887 GtkRequisition req;
1888 GdkEventButton *event_button = g_object_get_data(G_OBJECT(menu), "geany-button-event");
1890 if (event_button)
1892 /* if we got a mouse click, popup at that position */
1893 *x = (gint) event_button->x_root;
1894 *y = (gint) event_button->y_root;
1895 line_height = 0; /* we don't want to offset below the line or anything */
1897 else /* keyboard positioning */
1899 ScintillaObject *sci = data;
1900 GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(sci));
1901 gint pos = sci_get_current_position(sci);
1902 gint line = sci_get_line_from_position(sci, pos);
1903 gint pos_x = scintilla_send_message(sci, SCI_POINTXFROMPOSITION, 0, pos);
1904 gint pos_y = scintilla_send_message(sci, SCI_POINTYFROMPOSITION, 0, pos);
1906 line_height = scintilla_send_message(sci, SCI_TEXTHEIGHT, line, 0);
1908 gdk_window_get_origin(window, x, y);
1909 *x += pos_x;
1910 *y += pos_y;
1913 monitor_num = gdk_screen_get_monitor_at_point(screen, *x, *y);
1915 #if GTK_CHECK_VERSION(3, 0, 0)
1916 gtk_widget_get_preferred_size(GTK_WIDGET(menu), NULL, &req);
1917 #else
1918 gtk_widget_size_request(GTK_WIDGET(menu), &req);
1919 #endif
1921 #if GTK_CHECK_VERSION(3, 4, 0)
1922 gdk_screen_get_monitor_workarea(screen, monitor_num, &monitor);
1923 #else
1924 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
1925 #endif
1927 /* put on one size of the X position, but within the monitor */
1928 if (gtk_widget_get_direction(GTK_WIDGET(menu)) == GTK_TEXT_DIR_RTL)
1930 if (*x - req.width - 1 >= monitor.x)
1931 *x -= req.width + 1;
1932 else if (*x + req.width > monitor.x + monitor.width)
1933 *x = monitor.x;
1934 else
1935 *x += 1;
1937 else
1939 if (*x + req.width + 1 <= monitor.x + monitor.width)
1940 *x = MAX(monitor.x, *x + 1);
1941 else if (*x - req.width - 1 >= monitor.x)
1942 *x -= req.width + 1;
1943 else
1944 *x = monitor.x + MAX(0, monitor.width - req.width);
1947 /* try to put, in order:
1948 * 1. below the Y position, under the line
1949 * 2. above the Y position
1950 * 3. within the monitor */
1951 if (*y + line_height + req.height <= monitor.y + monitor.height)
1952 *y = MAX(monitor.y, *y + line_height);
1953 else if (*y - req.height >= monitor.y)
1954 *y = *y - req.height;
1955 else
1956 *y = monitor.y + MAX(0, monitor.height - req.height);
1958 *push_in = FALSE;
1962 static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_best)
1964 GtkWidget *first = NULL;
1965 GtkWidget *menu;
1966 GdkEvent *event;
1967 GdkEventButton *button_event = NULL;
1968 TMTag *tmtag;
1969 guint i;
1971 menu = gtk_menu_new();
1973 foreach_ptr_array(tmtag, i, tags)
1975 GtkWidget *item;
1976 GtkWidget *label;
1977 GtkWidget *image;
1978 gchar *fname = g_path_get_basename(tmtag->file->file_name);
1979 gchar *text;
1981 if (! first && have_best)
1982 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1983 text = g_markup_printf_escaped(_("<b>%s: %lu</b>"), fname, tmtag->line);
1984 else
1985 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1986 text = g_markup_printf_escaped(_("%s: %lu"), fname, tmtag->line);
1988 image = gtk_image_new_from_pixbuf(symbols_icons[get_tag_class(tmtag)].pixbuf);
1989 label = g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0, NULL);
1990 item = g_object_new(GTK_TYPE_IMAGE_MENU_ITEM, "image", image, "child", label, NULL);
1991 g_signal_connect_data(item, "activate", G_CALLBACK(on_goto_popup_item_activate),
1992 tm_tag_ref(tmtag), (GClosureNotify) tm_tag_unref, 0);
1993 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1995 if (! first)
1996 first = item;
1998 g_free(text);
1999 g_free(fname);
2002 gtk_widget_show_all(menu);
2004 if (first) /* always select the first item for better keyboard navigation */
2005 g_signal_connect(menu, "realize", G_CALLBACK(gtk_menu_shell_select_item), first);
2007 event = gtk_get_current_event();
2008 if (event && event->type == GDK_BUTTON_PRESS)
2009 button_event = (GdkEventButton *) event;
2010 else
2011 gdk_event_free(event);
2013 g_object_set_data_full(G_OBJECT(menu), "geany-button-event", button_event,
2014 button_event ? (GDestroyNotify) gdk_event_free : NULL);
2015 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, goto_popup_position_func, doc->editor->sci,
2016 button_event ? button_event->button : 0, gtk_get_current_event_time ());
2020 static gint compare_tags_by_name_line(gconstpointer ptr1, gconstpointer ptr2)
2022 gint res;
2023 TMTag *t1 = *((TMTag **) ptr1);
2024 TMTag *t2 = *((TMTag **) ptr2);
2026 res = g_strcmp0(t1->file->short_name, t2->file->short_name);
2027 if (res != 0)
2028 return res;
2029 return t1->line - t2->line;
2033 static TMTag *find_best_goto_tag(GeanyDocument *doc, GPtrArray *tags)
2035 TMTag *tag;
2036 guint i;
2038 /* first check if we have a tag in the current file */
2039 foreach_ptr_array(tag, i, tags)
2041 if (g_strcmp0(doc->real_path, tag->file->file_name) == 0)
2042 return tag;
2045 /* next check if we have a tag for some of the open documents */
2046 foreach_ptr_array(tag, i, tags)
2048 guint j;
2050 foreach_document(j)
2052 if (g_strcmp0(documents[j]->real_path, tag->file->file_name) == 0)
2053 return tag;
2057 /* next check if we have a tag for a file inside the current document's directory */
2058 foreach_ptr_array(tag, i, tags)
2060 gchar *dir = g_path_get_dirname(doc->real_path);
2062 if (g_str_has_prefix(tag->file->file_name, dir))
2064 g_free(dir);
2065 return tag;
2067 g_free(dir);
2070 return NULL;
2074 static GPtrArray *filter_tags(GPtrArray *tags, TMTag *current_tag, gboolean definition)
2076 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
2077 TMTag *tmtag, *last_tag = NULL;
2078 GPtrArray *filtered_tags = g_ptr_array_new();
2079 guint i;
2081 foreach_ptr_array(tmtag, i, tags)
2083 if ((definition && !(tmtag->type & forward_types)) ||
2084 (!definition && (tmtag->type & forward_types)))
2086 /* If there are typedefs of e.g. a struct such as
2087 * "typedef struct Foo {} Foo;", filter out the typedef unless
2088 * cursor is at the struct name. */
2089 if (last_tag != NULL && last_tag->file == tmtag->file &&
2090 last_tag->type != tm_tag_typedef_t && tmtag->type == tm_tag_typedef_t)
2092 if (last_tag == current_tag)
2093 g_ptr_array_add(filtered_tags, tmtag);
2095 else if (tmtag != current_tag)
2096 g_ptr_array_add(filtered_tags, tmtag);
2098 last_tag = tmtag;
2102 return filtered_tags;
2106 static gboolean goto_tag(const gchar *name, gboolean definition)
2108 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
2109 TMTag *tmtag, *current_tag = NULL;
2110 GeanyDocument *old_doc = document_get_current();
2111 gboolean found = FALSE;
2112 const GPtrArray *all_tags;
2113 GPtrArray *tags, *filtered_tags;
2114 guint i;
2115 guint current_line = sci_get_current_line(old_doc->editor->sci) + 1;
2117 all_tags = tm_workspace_find(name, NULL, tm_tag_max_t, NULL, old_doc->file_type->lang);
2119 /* get rid of global tags and find tag at current line */
2120 tags = g_ptr_array_new();
2121 foreach_ptr_array(tmtag, i, all_tags)
2123 if (tmtag->file)
2125 g_ptr_array_add(tags, tmtag);
2126 if (tmtag->file == old_doc->tm_file && tmtag->line == current_line)
2127 current_tag = tmtag;
2131 if (current_tag)
2132 /* swap definition/declaration search */
2133 definition = current_tag->type & forward_types;
2135 filtered_tags = filter_tags(tags, current_tag, definition);
2136 if (current_tag && filtered_tags->len == 0)
2138 /* if we previously swapped definition/declaration search and didn't
2139 * find anything, try again with the opposite type */
2140 g_ptr_array_free(filtered_tags, TRUE);
2141 filtered_tags = filter_tags(tags, current_tag, !definition);
2143 g_ptr_array_free(tags, TRUE);
2144 tags = filtered_tags;
2146 if (tags->len == 1)
2148 GeanyDocument *new_doc;
2150 tmtag = tags->pdata[0];
2151 new_doc = document_find_by_real_path(tmtag->file->file_name);
2153 if (!new_doc)
2154 /* not found in opened document, should open */
2155 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
2157 navqueue_goto_line(old_doc, new_doc, tmtag->line);
2159 else if (tags->len > 1)
2161 GPtrArray *tag_list;
2162 TMTag *tag, *best_tag;
2164 g_ptr_array_sort(tags, compare_tags_by_name_line);
2165 best_tag = find_best_goto_tag(old_doc, tags);
2167 tag_list = g_ptr_array_new();
2168 if (best_tag)
2169 g_ptr_array_add(tag_list, best_tag);
2170 foreach_ptr_array(tag, i, tags)
2172 if (tag != best_tag)
2173 g_ptr_array_add(tag_list, tag);
2175 show_goto_popup(old_doc, tag_list, best_tag != NULL);
2177 g_ptr_array_free(tag_list, TRUE);
2180 found = tags->len > 0;
2181 g_ptr_array_free(tags, TRUE);
2183 return found;
2187 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
2189 if (goto_tag(name, definition))
2190 return TRUE;
2192 /* if we are here, there was no match and we are beeping ;-) */
2193 utils_beep();
2195 if (!definition)
2196 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
2197 else
2198 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
2199 return FALSE;
2203 /* This could perhaps be improved to check for #if, class etc. */
2204 static gint get_function_fold_number(GeanyDocument *doc)
2206 /* for Java the functions are always one fold level above the class scope */
2207 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
2208 return SC_FOLDLEVELBASE + 1;
2209 else
2210 return SC_FOLDLEVELBASE;
2214 /* Should be used only with get_current_tag_cached.
2215 * tag_types caching might trigger recomputation too often but this isn't used differently often
2216 * enough to be an issue for now */
2217 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2219 static gint old_line = -2;
2220 static GeanyDocument *old_doc = NULL;
2221 static gint old_fold_num = -1;
2222 static guint old_tag_types = 0;
2223 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2224 gboolean ret;
2226 /* check if the cached line and file index have changed since last time: */
2227 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2228 ret = TRUE;
2229 else if (cur_line == old_line)
2230 ret = FALSE;
2231 else
2233 /* if the line has only changed by 1 */
2234 if (abs(cur_line - old_line) == 1)
2236 /* It's the same function if the fold number hasn't changed */
2237 ret = (fold_num != old_fold_num);
2239 else ret = TRUE;
2242 /* record current line and file index for next time */
2243 old_line = cur_line;
2244 old_doc = doc;
2245 old_fold_num = fold_num;
2246 old_tag_types = tag_types;
2247 return ret;
2251 /* Parse the function name up to 2 lines before tag_line.
2252 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2253 * type or argument names can be confused with the function name. */
2254 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2256 gint start, end, max_pos;
2257 gint fn_style;
2259 switch (sci_get_lexer(sci))
2261 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2262 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2263 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2265 start = sci_get_position_from_line(sci, tag_line - 2);
2266 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2267 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2268 start++;
2270 end = start;
2271 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2272 end++;
2274 if (start == end)
2275 return NULL;
2276 return sci_get_contents_range(sci, start, end);
2280 /* Parse the function name */
2281 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2283 gint start, end, first_pos, max_pos;
2284 gint tmp;
2285 gchar c;
2287 first_pos = end = sci_get_position_from_line(sci, tag_line);
2288 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2289 tmp = 0;
2290 /* goto the begin of function body */
2291 while (end < max_pos &&
2292 (tmp = sci_get_char_at(sci, end)) != '{' &&
2293 tmp != 0) end++;
2294 if (tmp == 0) end --;
2296 /* go back to the end of function identifier */
2297 while (end > 0 && end > first_pos - 500 &&
2298 (tmp = sci_get_char_at(sci, end)) != '(' &&
2299 tmp != 0) end--;
2300 end--;
2301 if (end < 0) end = 0;
2303 /* skip whitespaces between identifier and ( */
2304 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2306 start = end;
2307 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2308 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2309 || tmp == SCE_C_GLOBALCLASS
2310 || (c = sci_get_char_at(sci, start)) == '~'
2311 || c == ':'))
2312 start--;
2313 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2315 if (start == end) return NULL;
2316 return sci_get_contents_range(sci, start, end + 1);
2320 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2322 gint line_count = sci_get_line_count(sci);
2324 for (; line < line_count; line++)
2326 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2327 return line;
2330 return -1;
2334 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2336 gint line;
2337 gint parent;
2339 line = sci_get_current_line(doc->editor->sci);
2340 parent = sci_get_fold_parent(doc->editor->sci, line);
2341 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2342 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2343 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2345 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2347 if (tag)
2349 gint tag_line = tag->line - 1;
2350 gint last_child = line + 1;
2352 /* if it may be a false positive because we're inside a fold level not inside anything
2353 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2354 * right after the tag we got from TM */
2355 if (abs(tag_line - parent) > 1)
2357 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2358 if (tag_fold >= 0)
2359 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2362 if (line <= last_child)
2364 if (tag->scope)
2365 *tagname = g_strconcat(tag->scope,
2366 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2367 else
2368 *tagname = g_strdup(tag->name);
2370 return tag_line;
2374 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2375 * to dirty and inaccurate hand-parsing */
2376 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2378 const gint fn_fold = get_function_fold_number(doc);
2379 gint tag_line = parent;
2380 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2382 /* find the top level fold point */
2383 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2385 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2386 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2389 if (tag_line >= 0)
2391 gchar *cur_tag;
2393 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2394 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2395 else
2396 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2398 if (cur_tag != NULL)
2400 *tagname = cur_tag;
2401 return tag_line;
2406 *tagname = g_strdup(_("unknown"));
2407 return -1;
2411 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2413 static gint tag_line = -1;
2414 static gchar *cur_tag = NULL;
2416 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2418 if (doc == NULL) /* reset current function */
2420 current_tag_changed(NULL, -1, -1, 0);
2421 g_free(cur_tag);
2422 cur_tag = g_strdup(_("unknown"));
2423 if (tagname != NULL)
2424 *tagname = cur_tag;
2425 tag_line = -1;
2427 else
2429 gint line = sci_get_current_line(doc->editor->sci);
2430 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2432 if (current_tag_changed(doc, line, fold_level, tag_types))
2434 g_free(cur_tag);
2435 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2437 *tagname = cur_tag;
2440 return tag_line;
2444 /* Sets *tagname to point at the current function or tag name.
2445 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2446 * call to this function.
2447 * Returns: line number of the current tag, or -1 if unknown. */
2448 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2450 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2454 /* same as symbols_get_current_function() but finds class, namespaces and more */
2455 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2457 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2458 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2460 /* Python parser reports imports as namespaces which confuses the scope detection */
2461 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2462 tag_types |= tm_tag_namespace_t;
2464 return get_current_tag_name_cached(doc, tagname, tag_types);
2468 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2470 gint sort_mode = GPOINTER_TO_INT(user_data);
2471 GeanyDocument *doc = document_get_current();
2473 if (ignore_callback)
2474 return;
2476 if (doc != NULL)
2477 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2481 static void on_symbol_tree_menu_show(GtkWidget *widget,
2482 gpointer user_data)
2484 GeanyDocument *doc = document_get_current();
2485 gboolean enable;
2487 enable = doc && doc->has_tags;
2488 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2489 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2490 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2491 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2492 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2493 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2495 if (! doc)
2496 return;
2498 ignore_callback = TRUE;
2500 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2501 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2502 else
2503 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2505 ignore_callback = FALSE;
2509 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2511 gboolean expand = GPOINTER_TO_INT(user_data);
2512 GeanyDocument *doc = document_get_current();
2514 if (! doc)
2515 return;
2517 g_return_if_fail(doc->priv->tag_tree);
2519 if (expand)
2520 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2521 else
2522 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2526 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2528 GtkTreeIter iter;
2529 GtkTreeSelection *selection;
2530 GtkTreeModel *model;
2531 GeanyDocument *doc;
2532 TMTag *tag = NULL;
2534 doc = document_get_current();
2535 if (!doc)
2536 return;
2538 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2539 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2540 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2541 if (tag)
2543 if (widget == symbol_menu.find_in_files)
2544 search_show_find_in_files_dialog_full(tag->name, NULL);
2545 else
2546 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2547 widget == symbol_menu.find_usage);
2549 tm_tag_unref(tag);
2554 static void create_taglist_popup_menu(void)
2556 GtkWidget *item, *menu;
2558 tv.popup_taglist = menu = gtk_menu_new();
2560 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2561 gtk_widget_show(item);
2562 gtk_container_add(GTK_CONTAINER(menu), item);
2563 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2565 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2566 gtk_widget_show(item);
2567 gtk_container_add(GTK_CONTAINER(menu), item);
2568 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2570 item = gtk_separator_menu_item_new();
2571 gtk_widget_show(item);
2572 gtk_container_add(GTK_CONTAINER(menu), item);
2574 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2575 _("Sort by _Name"));
2576 gtk_widget_show(item);
2577 gtk_container_add(GTK_CONTAINER(menu), item);
2578 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2579 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2581 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2582 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2583 gtk_widget_show(item);
2584 gtk_container_add(GTK_CONTAINER(menu), item);
2585 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2586 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2588 item = gtk_separator_menu_item_new();
2589 gtk_widget_show(item);
2590 gtk_container_add(GTK_CONTAINER(menu), item);
2592 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2593 gtk_widget_show(item);
2594 gtk_container_add(GTK_CONTAINER(menu), item);
2595 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2597 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2598 gtk_widget_show(item);
2599 gtk_container_add(GTK_CONTAINER(menu), item);
2600 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2602 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2603 gtk_widget_show(item);
2604 gtk_container_add(GTK_CONTAINER(menu), item);
2605 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2607 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2609 sidebar_add_common_menu_items(GTK_MENU(menu));
2613 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2615 gchar *f;
2617 g_return_if_fail(!EMPTY(doc->real_path));
2619 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2620 if (utils_str_equal(doc->real_path, f))
2621 load_c_ignore_tags();
2623 g_free(f);
2627 void symbols_init(void)
2629 gchar *f;
2630 guint i;
2632 create_taglist_popup_menu();
2634 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2635 ui_add_config_file_menu_item(f, NULL, NULL);
2636 g_free(f);
2638 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2640 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2641 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2645 void symbols_finalize(void)
2647 guint i;
2649 g_strfreev(html_entities);
2650 g_strfreev(c_tags_ignore);
2652 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2654 if (symbols_icons[i].pixbuf)
2655 g_object_unref(symbols_icons[i].pixbuf);