Update of Japanese translation
[geany-mirror.git] / src / symbols.c
blobfc4685c76770fd00a93bd3eab243653e2f0e4d58
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 static struct
101 GtkWidget *expand_all;
102 GtkWidget *collapse_all;
103 GtkWidget *sort_by_name;
104 GtkWidget *sort_by_appearance;
105 GtkWidget *find_usage;
106 GtkWidget *find_doc_usage;
107 GtkWidget *find_in_files;
109 symbol_menu;
112 static void html_tags_loaded(void);
113 static void load_user_tags(filetype_id ft_id);
115 /* get the tags_ignore list, exported by tagmanager's options.c */
116 extern gchar **c_tags_ignore;
118 /* ignore certain tokens when parsing C-like syntax.
119 * Also works for reloading. */
120 static void load_c_ignore_tags(void)
122 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
123 gchar *content;
125 if (g_file_get_contents(path, &content, NULL, NULL))
127 /* historically we ignore the glib _DECLS for tag generation */
128 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
130 g_strfreev(c_tags_ignore);
131 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
132 g_free(content);
134 g_free(path);
138 void symbols_reload_config_files(void)
140 load_c_ignore_tags();
144 static gsize get_tag_count(void)
146 GPtrArray *tags = tm_get_workspace()->global_tags;
147 gsize count = tags ? tags->len : 0;
149 return count;
153 /* wrapper for tm_workspace_load_global_tags().
154 * note that the tag count only counts new global tags added - if a tag has the same name,
155 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
156 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
158 gboolean result;
159 gsize old_tag_count = get_tag_count();
161 result = tm_workspace_load_global_tags(tags_file, ft->lang);
162 if (result)
164 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
165 (guint) (get_tag_count() - old_tag_count));
167 return result;
171 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
172 * This provides autocompletion, calltips, etc. */
173 void symbols_global_tags_loaded(guint file_type_idx)
175 TagFileInfo *tfi;
176 gint tag_type;
178 /* load ignore list for C/C++ parser */
179 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
180 c_tags_ignore == NULL)
182 load_c_ignore_tags();
185 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
186 return;
188 /* load config in case of custom filetypes */
189 filetypes_load_config(file_type_idx, FALSE);
191 load_user_tags(file_type_idx);
193 switch (file_type_idx)
195 case GEANY_FILETYPES_PHP:
196 case GEANY_FILETYPES_HTML:
197 html_tags_loaded();
199 switch (file_type_idx)
201 case GEANY_FILETYPES_CPP:
202 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
203 /* no C++ tagfile yet */
204 return;
205 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
206 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
207 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
208 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
209 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
210 default:
211 return;
213 tfi = &tag_file_info[tag_type];
215 if (! tfi->tags_loaded)
217 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
219 symbols_load_global_tags(fname, filetypes[file_type_idx]);
220 tfi->tags_loaded = TRUE;
221 g_free(fname);
226 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
227 static void html_tags_loaded(void)
229 TagFileInfo *tfi;
231 if (cl_options.ignore_global_tags)
232 return;
234 tfi = &tag_file_info[GTF_HTML_ENTITIES];
235 if (! tfi->tags_loaded)
237 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
239 html_entities = utils_read_file_in_array(file);
240 tfi->tags_loaded = TRUE;
241 g_free(file);
246 GString *symbols_find_typenames_as_string(gint lang, gboolean global)
248 guint j;
249 TMTag *tag;
250 GString *s = NULL;
251 GPtrArray *typedefs;
252 gint tag_lang;
254 if (global)
255 typedefs = tm_tags_extract(app->tm_workspace->global_tags, TM_GLOBAL_TYPE_MASK);
256 else
257 typedefs = app->tm_workspace->typename_array;
259 if ((typedefs) && (typedefs->len > 0))
261 s = g_string_sized_new(typedefs->len * 10);
262 for (j = 0; j < typedefs->len; ++j)
264 tag = TM_TAG(typedefs->pdata[j]);
265 tag_lang = tag->lang;
267 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
268 * e.g. PHP classes in C++ files
269 * lang = -2 disables the check */
270 if (tag->name && (tag_lang == lang || lang == -2 ||
271 (lang == TM_PARSER_CPP && tag_lang == TM_PARSER_C)))
273 if (j != 0)
274 g_string_append_c(s, ' ');
275 g_string_append(s, tag->name);
279 if (typedefs && global)
280 g_ptr_array_free(typedefs, TRUE);
281 return s;
285 /** Gets the context separator used by the tag manager for a particular file
286 * type.
287 * @param ft_id File type identifier.
288 * @return The context separator string.
290 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
291 * without a context separator.
293 * @since 0.19
295 const gchar *symbols_get_context_separator(gint ft_id)
297 switch (ft_id)
299 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
300 case GEANY_FILETYPES_CPP:
301 case GEANY_FILETYPES_GLSL: /* for structs */
302 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
303 case GEANY_FILETYPES_PHP:
304 case GEANY_FILETYPES_RUST:
305 return "::";
307 /* avoid confusion with other possible separators in group/section name */
308 case GEANY_FILETYPES_CONF:
309 case GEANY_FILETYPES_REST:
310 return ":::";
312 /* no context separator */
313 case GEANY_FILETYPES_ASCIIDOC:
314 return "\x03";
316 default:
317 return ".";
322 GString *symbols_get_macro_list(gint lang)
324 guint j, i;
325 GPtrArray *ftags;
326 GString *words;
327 gint tag_lang;
328 TMTag *tag;
330 if (app->tm_workspace->source_files == NULL)
331 return NULL;
333 ftags = g_ptr_array_sized_new(50);
334 words = g_string_sized_new(200);
336 for (j = 0; j < app->tm_workspace->source_files->len; j++)
338 GPtrArray *tags;
340 tags = tm_tags_extract(TM_SOURCE_FILE(app->tm_workspace->source_files->pdata[j])->tags_array,
341 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
342 if (NULL != tags)
344 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
346 tag = TM_TAG(tags->pdata[i]);
347 tag_lang = (tag->file) ?
348 tag->file->lang : tag->lang;
350 if (tag_lang == lang)
351 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
353 g_ptr_array_free(tags, TRUE);
357 if (ftags->len == 0)
359 g_ptr_array_free(ftags, TRUE);
360 g_string_free(words, TRUE);
361 return NULL;
364 tm_tags_sort(ftags, NULL, FALSE, FALSE);
365 for (j = 0; j < ftags->len; j++)
367 if (j > 0)
368 g_string_append_c(words, '\n');
369 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
371 g_ptr_array_free(ftags, TRUE);
372 return words;
376 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
377 static TMTag *
378 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
380 guint i;
381 g_return_val_if_fail(tags != NULL, NULL);
383 for (i = 0; i < tags->len; ++i)
385 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
386 return TM_TAG(tags->pdata[i]);
388 return NULL;
392 static TMTag *find_source_file_tag(GPtrArray *tags_array,
393 const gchar *tag_name, guint type)
395 GPtrArray *tags;
396 TMTag *tmtag;
398 tags = tm_tags_extract(tags_array, type);
399 if (tags != NULL)
401 tmtag = symbols_find_tm_tag(tags, tag_name);
403 g_ptr_array_free(tags, TRUE);
405 if (tmtag != NULL)
406 return tmtag;
408 return NULL; /* not found */
412 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
414 guint j;
415 const GPtrArray *source_files = NULL;
417 if (app->tm_workspace != NULL)
418 source_files = app->tm_workspace->source_files;
420 if (source_files != NULL)
422 for (j = 0; j < source_files->len; j++)
424 TMSourceFile *srcfile = source_files->pdata[j];
425 TMTag *tmtag;
427 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
428 if (tmtag != NULL)
429 return tmtag;
432 return NULL; /* not found */
436 const gchar **symbols_get_html_entities(void)
438 if (html_entities == NULL)
439 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
441 return (const gchar **) html_entities;
445 /* sort by name, then line */
446 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
448 gint ret;
450 if (tag_a == NULL || tag_b == NULL)
451 return 0;
453 if (tag_a->name == NULL)
454 return -(tag_a->name != tag_b->name);
456 if (tag_b->name == NULL)
457 return tag_a->name != tag_b->name;
459 ret = strcmp(tag_a->name, tag_b->name);
460 if (ret == 0)
462 return tag_a->line - tag_b->line;
464 return ret;
468 /* sort by line, then scope */
469 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
471 const TMTag *tag_a = TM_TAG(a);
472 const TMTag *tag_b = TM_TAG(b);
473 gint ret;
475 if (a == NULL || b == NULL)
476 return 0;
478 ret = tag_a->line - tag_b->line;
479 if (ret == 0)
481 if (tag_a->scope == NULL)
482 return -(tag_a->scope != tag_b->scope);
483 if (tag_b->scope == NULL)
484 return tag_a->scope != tag_b->scope;
485 else
486 return strcmp(tag_a->scope, tag_b->scope);
488 return ret;
492 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
494 GList *tag_names = NULL;
495 TMTag *tag;
496 guint i;
498 g_return_val_if_fail(doc, NULL);
500 if (! doc->tm_file || ! doc->tm_file->tags_array)
501 return NULL;
503 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
505 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
506 if (G_UNLIKELY(tag == NULL))
507 return NULL;
509 if (tag->type & tag_types)
511 tag_names = g_list_prepend(tag_names, tag);
514 tag_names = g_list_sort(tag_names, compare_symbol_lines);
515 return tag_names;
519 /* amount of types in the symbol list (currently max. 8 are used) */
520 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
522 struct TreeviewSymbols
524 GtkTreeIter tag_function;
525 GtkTreeIter tag_class;
526 GtkTreeIter tag_macro;
527 GtkTreeIter tag_member;
528 GtkTreeIter tag_variable;
529 GtkTreeIter tag_externvar;
530 GtkTreeIter tag_namespace;
531 GtkTreeIter tag_struct;
532 GtkTreeIter tag_interface;
533 GtkTreeIter tag_type;
534 GtkTreeIter tag_other;
535 } tv_iters;
538 static void init_tag_iters(void)
540 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
541 * filetypes(e.g. config file to Python crashes Geany without this) */
542 tv_iters.tag_function.stamp = -1;
543 tv_iters.tag_class.stamp = -1;
544 tv_iters.tag_member.stamp = -1;
545 tv_iters.tag_macro.stamp = -1;
546 tv_iters.tag_variable.stamp = -1;
547 tv_iters.tag_externvar.stamp = -1;
548 tv_iters.tag_namespace.stamp = -1;
549 tv_iters.tag_struct.stamp = -1;
550 tv_iters.tag_interface.stamp = -1;
551 tv_iters.tag_type.stamp = -1;
552 tv_iters.tag_other.stamp = -1;
556 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
558 static GtkIconTheme *icon_theme = NULL;
559 static gint x = -1;
561 if (G_UNLIKELY(x < 0))
563 gint dummy;
564 icon_theme = gtk_icon_theme_get_default();
565 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
567 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
571 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
573 GtkTreeModel *model = GTK_TREE_MODEL(store);
575 if (!gtk_tree_model_get_iter_first(model, iter))
576 return FALSE;
579 gchar *candidate;
581 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
582 /* FIXME: what if 2 different items have the same name?
583 * this should never happen, but might be caused by a typo in a translation */
584 if (utils_str_equal(candidate, title))
586 g_free(candidate);
587 return TRUE;
589 else
590 g_free(candidate);
592 while (gtk_tree_model_iter_next(model, iter));
594 return FALSE;
598 /* Adds symbol list groups in (iter*, title) pairs.
599 * The list must be ended with NULL. */
600 static void G_GNUC_NULL_TERMINATED
601 tag_list_add_groups(GtkTreeStore *tree_store, ...)
603 va_list args;
604 GtkTreeIter *iter;
606 g_return_if_fail(top_level_iter_names);
608 va_start(args, tree_store);
609 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
611 gchar *title = va_arg(args, gchar*);
612 gchar *icon_name = va_arg(args, gchar *);
613 GdkPixbuf *icon = NULL;
615 if (icon_name)
617 icon = get_tag_icon(icon_name);
620 g_assert(title != NULL);
621 g_ptr_array_add(top_level_iter_names, title);
623 if (!find_toplevel_iter(tree_store, iter, title))
624 gtk_tree_store_append(tree_store, iter, NULL);
626 if (G_IS_OBJECT(icon))
628 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
629 g_object_unref(icon);
631 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
633 va_end(args);
637 static void add_top_level_items(GeanyDocument *doc)
639 filetype_id ft_id = doc->file_type->id;
640 GtkTreeStore *tag_store = doc->priv->tag_store;
642 if (top_level_iter_names == NULL)
643 top_level_iter_names = g_ptr_array_new();
644 else
645 g_ptr_array_set_size(top_level_iter_names, 0);
647 init_tag_iters();
649 switch (ft_id)
651 case GEANY_FILETYPES_DIFF:
653 tag_list_add_groups(tag_store,
654 &(tv_iters.tag_function), _("Files"), NULL, NULL);
655 break;
657 case GEANY_FILETYPES_DOCBOOK:
659 tag_list_add_groups(tag_store,
660 &(tv_iters.tag_function), _("Chapter"), NULL,
661 &(tv_iters.tag_class), _("Section"), NULL,
662 &(tv_iters.tag_member), _("Sect1"), NULL,
663 &(tv_iters.tag_macro), _("Sect2"), NULL,
664 &(tv_iters.tag_variable), _("Sect3"), NULL,
665 &(tv_iters.tag_struct), _("Appendix"), NULL,
666 &(tv_iters.tag_other), _("Other"), NULL,
667 NULL);
668 break;
670 case GEANY_FILETYPES_HASKELL:
671 tag_list_add_groups(tag_store,
672 &tv_iters.tag_namespace, _("Module"), NULL,
673 &tv_iters.tag_type, _("Types"), NULL,
674 &tv_iters.tag_macro, _("Type constructors"), NULL,
675 &tv_iters.tag_function, _("Functions"), "classviewer-method",
676 NULL);
677 break;
678 case GEANY_FILETYPES_COBOL:
679 tag_list_add_groups(tag_store,
680 &tv_iters.tag_class, _("Program"), "classviewer-class",
681 &tv_iters.tag_function, _("File"), "classviewer-method",
682 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
683 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
684 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
685 &tv_iters.tag_variable, _("Data"), "classviewer-var",
686 NULL);
687 break;
688 case GEANY_FILETYPES_CONF:
689 tag_list_add_groups(tag_store,
690 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
691 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
692 NULL);
693 break;
694 case GEANY_FILETYPES_NSIS:
695 tag_list_add_groups(tag_store,
696 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
697 &tv_iters.tag_function, _("Functions"), "classviewer-method",
698 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
699 NULL);
700 break;
701 case GEANY_FILETYPES_LATEX:
703 tag_list_add_groups(tag_store,
704 &(tv_iters.tag_function), _("Command"), NULL,
705 &(tv_iters.tag_class), _("Environment"), NULL,
706 &(tv_iters.tag_member), _("Section"), NULL,
707 &(tv_iters.tag_macro), _("Subsection"), NULL,
708 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
709 &(tv_iters.tag_struct), _("Label"), NULL,
710 &(tv_iters.tag_namespace), _("Chapter"), NULL,
711 &(tv_iters.tag_other), _("Other"), NULL,
712 NULL);
713 break;
715 case GEANY_FILETYPES_MATLAB:
717 tag_list_add_groups(tag_store,
718 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
719 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
720 NULL);
721 break;
723 case GEANY_FILETYPES_ABAQUS:
725 tag_list_add_groups(tag_store,
726 &(tv_iters.tag_class), _("Parts"), NULL,
727 &(tv_iters.tag_member), _("Assembly"), NULL,
728 &(tv_iters.tag_namespace), _("Steps"), NULL,
729 NULL);
730 break;
732 case GEANY_FILETYPES_R:
734 tag_list_add_groups(tag_store,
735 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
736 &(tv_iters.tag_other), _("Other"), NULL,
737 NULL);
738 break;
740 case GEANY_FILETYPES_RUST:
742 tag_list_add_groups(tag_store,
743 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
744 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
745 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
746 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
747 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
748 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
749 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
750 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
751 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
752 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
753 NULL);
754 break;
756 case GEANY_FILETYPES_PERL:
758 tag_list_add_groups(tag_store,
759 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
760 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
761 &(tv_iters.tag_macro), _("Labels"), NULL,
762 &(tv_iters.tag_type), _("Constants"), NULL,
763 &(tv_iters.tag_other), _("Other"), "classviewer-other",
764 NULL);
765 break;
767 case GEANY_FILETYPES_PHP:
769 tag_list_add_groups(tag_store,
770 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
771 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
772 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
773 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
774 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
775 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
776 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
777 NULL);
778 break;
780 case GEANY_FILETYPES_HTML:
782 tag_list_add_groups(tag_store,
783 &(tv_iters.tag_function), _("Functions"), NULL,
784 &(tv_iters.tag_member), _("Anchors"), NULL,
785 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
786 &(tv_iters.tag_class), _("H2 Headings"), NULL,
787 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
788 NULL);
789 break;
791 case GEANY_FILETYPES_CSS:
793 tag_list_add_groups(tag_store,
794 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
795 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
796 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
797 break;
799 case GEANY_FILETYPES_REST:
800 case GEANY_FILETYPES_TXT2TAGS:
801 case GEANY_FILETYPES_ABC:
803 tag_list_add_groups(tag_store,
804 &(tv_iters.tag_namespace), _("Chapter"), NULL,
805 &(tv_iters.tag_member), _("Section"), NULL,
806 &(tv_iters.tag_macro), _("Subsection"), NULL,
807 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
808 NULL);
809 break;
811 case GEANY_FILETYPES_ASCIIDOC:
813 tag_list_add_groups(tag_store,
814 &(tv_iters.tag_namespace), _("Document"), NULL,
815 &(tv_iters.tag_member), _("Section Level 1"), NULL,
816 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
817 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
818 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
819 NULL);
820 break;
822 case GEANY_FILETYPES_RUBY:
824 tag_list_add_groups(tag_store,
825 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
826 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
827 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
828 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
829 NULL);
830 break;
832 case GEANY_FILETYPES_TCL:
834 tag_list_add_groups(tag_store,
835 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
836 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
837 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
838 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
839 NULL);
840 break;
842 case GEANY_FILETYPES_PYTHON:
844 tag_list_add_groups(tag_store,
845 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
846 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
847 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
848 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
849 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
850 NULL);
851 break;
853 case GEANY_FILETYPES_VHDL:
855 tag_list_add_groups(tag_store,
856 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
857 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
858 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
859 &(tv_iters.tag_type), _("Types"), "classviewer-other",
860 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
861 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
862 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
863 &(tv_iters.tag_other), _("Other"), "classviewer-other",
864 NULL);
865 break;
867 case GEANY_FILETYPES_VERILOG:
869 tag_list_add_groups(tag_store,
870 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
871 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
872 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
873 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
874 &(tv_iters.tag_other), _("Other"), "classviewer-other",
875 NULL);
876 break;
878 case GEANY_FILETYPES_JAVA:
880 tag_list_add_groups(tag_store,
881 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
882 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
883 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
884 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
885 &(tv_iters.tag_member), _("Members"), "classviewer-member",
886 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
887 &(tv_iters.tag_other), _("Other"), "classviewer-other",
888 NULL);
889 break;
891 case GEANY_FILETYPES_AS:
893 tag_list_add_groups(tag_store,
894 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
895 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
896 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
897 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
898 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
899 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
900 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
901 &(tv_iters.tag_other), _("Other"), "classviewer-other",
902 NULL);
903 break;
905 case GEANY_FILETYPES_HAXE:
907 tag_list_add_groups(tag_store,
908 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
909 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
910 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
911 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
912 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
913 &(tv_iters.tag_other), _("Other"), "classviewer-other",
914 NULL);
915 break;
917 case GEANY_FILETYPES_BASIC:
919 tag_list_add_groups(tag_store,
920 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
921 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
922 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
923 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
924 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
925 &(tv_iters.tag_other), _("Other"), "classviewer-other",
926 NULL);
927 break;
929 case GEANY_FILETYPES_F77:
930 case GEANY_FILETYPES_FORTRAN:
932 tag_list_add_groups(tag_store,
933 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
934 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
935 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
936 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
937 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
938 &(tv_iters.tag_class), _("Types"), "classviewer-class",
939 &(tv_iters.tag_member), _("Components"), "classviewer-member",
940 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
941 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
942 &(tv_iters.tag_other), _("Other"), "classviewer-other",
943 NULL);
944 break;
946 case GEANY_FILETYPES_ASM:
948 tag_list_add_groups(tag_store,
949 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
950 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
951 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
952 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
953 NULL);
954 break;
956 case GEANY_FILETYPES_MAKE:
957 tag_list_add_groups(tag_store,
958 &tv_iters.tag_function, _("Targets"), "classviewer-method",
959 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
960 NULL);
961 break;
962 case GEANY_FILETYPES_SQL:
964 tag_list_add_groups(tag_store,
965 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
966 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
967 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
968 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
969 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
970 &(tv_iters.tag_member), _("Views"), "classviewer-var",
971 &(tv_iters.tag_other), _("Other"), "classviewer-other",
972 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
973 NULL);
974 break;
976 case GEANY_FILETYPES_D:
977 default:
979 if (ft_id == GEANY_FILETYPES_D)
980 tag_list_add_groups(tag_store,
981 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
982 else
983 tag_list_add_groups(tag_store,
984 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
986 tag_list_add_groups(tag_store,
987 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
988 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
989 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
990 &(tv_iters.tag_member), _("Members"), "classviewer-member",
991 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
992 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
993 NULL);
995 if (ft_id != GEANY_FILETYPES_D)
997 tag_list_add_groups(tag_store,
998 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1000 tag_list_add_groups(tag_store,
1001 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1002 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1003 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1009 /* removes toplevel items that have no children */
1010 static void hide_empty_rows(GtkTreeStore *store)
1012 GtkTreeIter iter;
1013 gboolean cont = TRUE;
1015 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1016 return; /* stop when first iter is invalid, i.e. no elements */
1018 while (cont)
1020 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1021 cont = gtk_tree_store_remove(store, &iter);
1022 else
1023 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1028 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1030 gchar *utf8_name;
1031 const gchar *scope = tag->scope;
1032 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1033 gboolean doc_is_utf8 = FALSE;
1035 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1036 * for None at this point completely */
1037 if (utils_str_equal(doc->encoding, "UTF-8") ||
1038 utils_str_equal(doc->encoding, "None"))
1039 doc_is_utf8 = TRUE;
1040 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1041 * plugin might have called tm_source_file_update(), so check to be sure */
1042 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1044 if (! doc_is_utf8)
1045 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1046 -1, doc->encoding, TRUE);
1047 else
1048 utf8_name = tag->name;
1050 if (utf8_name == NULL)
1051 return NULL;
1053 if (! buffer)
1054 buffer = g_string_new(NULL);
1055 else
1056 g_string_truncate(buffer, 0);
1058 /* check first char of scope is a wordchar */
1059 if (!found_parent && scope &&
1060 strpbrk(scope, GEANY_WORDCHARS) == scope)
1062 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1064 g_string_append(buffer, scope);
1065 g_string_append(buffer, sep);
1067 g_string_append(buffer, utf8_name);
1069 if (! doc_is_utf8)
1070 g_free(utf8_name);
1072 g_string_append_printf(buffer, " [%lu]", tag->line);
1074 return buffer->str;
1078 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1080 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1082 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1083 * for None at this point completely */
1084 if (utf8_name != NULL &&
1085 ! utils_str_equal(doc->encoding, "UTF-8") &&
1086 ! utils_str_equal(doc->encoding, "None"))
1088 SETPTR(utf8_name,
1089 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1092 if (utf8_name != NULL)
1093 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1095 return utf8_name;
1099 /* find the last word in "foo::bar::blah", e.g. "blah" */
1100 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1102 const gchar *scope = tag->scope;
1103 const gchar *separator = symbols_get_context_separator(ft_id);
1104 const gchar *str, *ptr;
1106 if (!scope)
1107 return NULL;
1109 str = scope;
1111 while (1)
1113 ptr = strstr(str, separator);
1114 if (ptr)
1116 str = ptr + strlen(separator);
1118 else
1119 break;
1122 return !EMPTY(str) ? str : NULL;
1126 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1128 GtkTreeIter *iter = NULL;
1130 switch (tag_type)
1132 case tm_tag_prototype_t:
1133 case tm_tag_method_t:
1134 case tm_tag_function_t:
1136 iter = &tv_iters.tag_function;
1137 break;
1139 case tm_tag_externvar_t:
1141 iter = &tv_iters.tag_externvar;
1142 break;
1144 case tm_tag_macro_t:
1145 case tm_tag_macro_with_arg_t:
1147 iter = &tv_iters.tag_macro;
1148 break;
1150 case tm_tag_class_t:
1152 iter = &tv_iters.tag_class;
1153 break;
1155 case tm_tag_member_t:
1156 case tm_tag_field_t:
1158 iter = &tv_iters.tag_member;
1159 break;
1161 case tm_tag_typedef_t:
1162 case tm_tag_enum_t:
1164 iter = &tv_iters.tag_type;
1165 break;
1167 case tm_tag_union_t:
1168 case tm_tag_struct_t:
1170 iter = &tv_iters.tag_struct;
1171 break;
1173 case tm_tag_interface_t:
1174 iter = &tv_iters.tag_interface;
1175 break;
1176 case tm_tag_variable_t:
1178 iter = &tv_iters.tag_variable;
1179 break;
1181 case tm_tag_namespace_t:
1182 case tm_tag_package_t:
1184 iter = &tv_iters.tag_namespace;
1185 break;
1187 default:
1189 iter = &tv_iters.tag_other;
1192 if (G_LIKELY(iter->stamp != -1))
1193 return iter;
1194 else
1195 return NULL;
1199 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1201 GdkPixbuf *icon = NULL;
1203 if (parent == &tv_iters.tag_other)
1205 return get_tag_icon("classviewer-var");
1207 /* copy parent icon */
1208 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1209 SYMBOLS_COLUMN_ICON, &icon, -1);
1210 return icon;
1214 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1216 const TMTag *t1 = v1;
1217 const TMTag *t2 = v2;
1219 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1220 utils_str_equal(t1->scope, t2->scope) &&
1221 /* include arglist in match to support e.g. C++ overloading */
1222 utils_str_equal(t1->arglist, t2->arglist));
1226 /* inspired from g_str_hash() */
1227 static guint tag_hash(gconstpointer v)
1229 const TMTag *tag = v;
1230 const gchar *p;
1231 guint32 h = 5381;
1233 h = (h << 5) + h + tag->type;
1234 for (p = tag->name; *p != '\0'; p++)
1235 h = (h << 5) + h + *p;
1236 if (tag->scope)
1238 for (p = tag->scope; *p != '\0'; p++)
1239 h = (h << 5) + h + *p;
1241 /* for e.g. C++ overloading */
1242 if (tag->arglist)
1244 for (p = tag->arglist; *p != '\0'; p++)
1245 h = (h << 5) + h + *p;
1248 return h;
1252 /* like gtk_tree_view_expand_to_path() but with an iter */
1253 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1255 GtkTreeModel *model = gtk_tree_view_get_model(view);
1256 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1258 gtk_tree_view_expand_to_path(view, path);
1259 gtk_tree_path_free(path);
1263 /* like gtk_tree_store_remove() but finds the next iter at any level */
1264 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1266 GtkTreeIter parent;
1267 gboolean has_parent;
1268 gboolean cont;
1270 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1271 cont = gtk_tree_store_remove(store, iter);
1272 /* if there is no next at this level but there is a parent iter, continue from it */
1273 if (! cont && has_parent)
1275 *iter = parent;
1276 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1279 return cont;
1283 /* adds a new element in the parent table if it's key is known.
1284 * duplicates are kept */
1285 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1286 const GtkTreeIter *iter)
1288 GList **list;
1289 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1290 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1292 if (! list)
1294 list = g_slice_alloc(sizeof *list);
1295 *list = NULL;
1296 g_hash_table_insert(table, tag->name, list);
1298 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1303 static void free_iter_slice_list(gpointer data)
1305 GList **list = data;
1307 if (list)
1309 GList *node;
1310 foreach_list(node, *list)
1311 g_slice_free(GtkTreeIter, node->data);
1312 g_list_free(*list);
1313 g_slice_free1(sizeof *list, list);
1318 /* inserts a @data in @table on key @tag.
1319 * previous data is not overwritten if the key is duplicated, but rather the
1320 * two values are kept in a list
1322 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1323 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1325 GList *list = g_hash_table_lookup(table, tag);
1326 list = g_list_prepend(list, data);
1327 g_hash_table_insert(table, tag, list);
1331 /* looks up the entry in @table that better matches @tag.
1332 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1333 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1335 GList *data = NULL;
1336 GList *node = g_hash_table_lookup(table, tag);
1337 if (node)
1339 glong delta;
1340 data = node->data;
1342 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1344 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1345 for (node = node->next; node; node = node->next)
1347 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1349 if (d < delta)
1351 data = node->data;
1352 delta = d;
1356 #undef TAG_DELTA
1359 return data;
1363 /* removes the element at @tag from @table.
1364 * @tag must be the exact pointer used at insertion time */
1365 static void tags_table_remove(GHashTable *table, TMTag *tag)
1367 GList *list = g_hash_table_lookup(table, tag);
1368 if (list)
1370 GList *node;
1371 foreach_list(node, list)
1373 if (((GList *) node->data)->data == tag)
1374 break;
1376 list = g_list_delete_link(list, node);
1377 if (list)
1378 g_hash_table_insert(table, tag, list);
1379 else
1380 g_hash_table_remove(table, tag);
1385 static void tags_table_destroy(GHashTable *table)
1387 /* free any leftover elements. note that we can't register a value_free_func when
1388 * creating the hash table because we only want to free it when destroying the table,
1389 * not when inserting a duplicate (we handle this manually) */
1390 GHashTableIter iter;
1391 gpointer value;
1393 g_hash_table_iter_init(&iter, table);
1394 while (g_hash_table_iter_next(&iter, NULL, &value))
1395 g_list_free(value);
1396 g_hash_table_destroy(table);
1401 * Updates the tag tree for a document with the tags in *list.
1402 * @param doc a document
1403 * @param tags a pointer to a GList* holding the tags to add/update. This
1404 * list may be updated, removing updated elements.
1406 * The update is done in two passes:
1407 * 1) walking the current tree, update tags that still exist and remove the
1408 * obsolescent ones;
1409 * 2) walking the remaining (non updated) tags, adds them in the list.
1411 * For better performances, we use 2 hash tables:
1412 * - one containing all the tags for lookup in the first pass (actually stores a
1413 * reference in the tags list for removing it efficiently), avoiding list search
1414 * on each tag;
1415 * - the other holding "tag-name":row references for tags having children, used to
1416 * lookup for a parent in both passes, avoiding tree traversal.
1418 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1420 GtkTreeStore *store = doc->priv->tag_store;
1421 GtkTreeModel *model = GTK_TREE_MODEL(store);
1422 GHashTable *parents_table;
1423 GHashTable *tags_table;
1424 GtkTreeIter iter;
1425 gboolean cont;
1426 GList *item;
1428 /* Build hash tables holding tags and parents */
1429 /* parent table holds "tag-name":GtkTreeIter */
1430 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1431 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1432 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1433 foreach_list(item, *tags)
1435 TMTag *tag = item->data;
1436 const gchar *name;
1438 tags_table_insert(tags_table, tag, item);
1440 name = get_parent_name(tag, doc->file_type->id);
1441 if (name)
1442 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1445 /* First pass, update existing rows or delete them.
1446 * It is OK to delete them since we walk top down so we would remove
1447 * parents before checking for their children, thus never implicitly
1448 * deleting an updated child */
1449 cont = gtk_tree_model_get_iter_first(model, &iter);
1450 while (cont)
1452 TMTag *tag;
1454 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1455 if (! tag) /* most probably a toplevel, skip it */
1456 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1457 else
1459 GList *found_item;
1461 found_item = tags_table_lookup(tags_table, tag);
1462 if (! found_item) /* tag doesn't exist, remove it */
1463 cont = tree_store_remove_row(store, &iter);
1464 else /* tag still exist, update it */
1466 const gchar *name;
1467 const gchar *parent_name;
1468 TMTag *found = found_item->data;
1470 parent_name = get_parent_name(found, doc->file_type->id);
1471 /* if parent is unknown, ignore it */
1472 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1473 parent_name = NULL;
1475 /* only update fields that (can) have changed (name that holds line
1476 * number, and the tag itself) */
1477 name = get_symbol_name(doc, found, parent_name != NULL);
1478 gtk_tree_store_set(store, &iter,
1479 SYMBOLS_COLUMN_NAME, name,
1480 SYMBOLS_COLUMN_TAG, found,
1481 -1);
1483 update_parents_table(parents_table, found, parent_name, &iter);
1485 /* remove the updated tag from the table and list */
1486 tags_table_remove(tags_table, found);
1487 *tags = g_list_delete_link(*tags, found_item);
1489 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1492 tm_tag_unref(tag);
1496 /* Second pass, now we have a tree cleaned up from invalid rows,
1497 * we simply add new ones */
1498 foreach_list (item, *tags)
1500 TMTag *tag = item->data;
1501 GtkTreeIter *parent;
1503 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1504 if (G_UNLIKELY(! parent))
1505 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1506 else
1508 gboolean expand;
1509 const gchar *name;
1510 const gchar *parent_name;
1511 gchar *tooltip;
1512 GdkPixbuf *icon = get_child_icon(store, parent);
1514 parent_name = get_parent_name(tag, doc->file_type->id);
1515 if (parent_name)
1517 GList **candidates;
1518 GtkTreeIter *parent_search = NULL;
1520 /* walk parent candidates to find the better one.
1521 * if there are more than one, take the one that has the closest line number
1522 * after the tag we're searching the parent for */
1523 candidates = g_hash_table_lookup(parents_table, parent_name);
1524 if (candidates)
1526 GList *node;
1527 glong delta = G_MAXLONG;
1528 foreach_list(node, *candidates)
1530 TMTag *parent_tag;
1531 glong d;
1533 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1534 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1536 d = tag->line - parent_tag->line;
1537 if (! parent_search || (d >= 0 && d < delta))
1539 delta = d;
1540 parent_search = node->data;
1542 tm_tag_unref(parent_tag);
1546 if (parent_search)
1547 parent = parent_search;
1548 else
1549 parent_name = NULL;
1552 /* only expand to the iter if the parent was empty, otherwise we let the
1553 * folding as it was before (already expanded, or closed by the user) */
1554 expand = ! gtk_tree_model_iter_has_child(model, parent);
1556 /* insert the new element */
1557 gtk_tree_store_append(store, &iter, parent);
1558 name = get_symbol_name(doc, tag, parent_name != NULL);
1559 tooltip = get_symbol_tooltip(doc, tag);
1560 gtk_tree_store_set(store, &iter,
1561 SYMBOLS_COLUMN_NAME, name,
1562 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1563 SYMBOLS_COLUMN_ICON, icon,
1564 SYMBOLS_COLUMN_TAG, tag,
1565 -1);
1566 g_free(tooltip);
1567 if (G_LIKELY(icon))
1568 g_object_unref(icon);
1570 update_parents_table(parents_table, tag, parent_name, &iter);
1572 if (expand)
1573 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1577 g_hash_table_destroy(parents_table);
1578 tags_table_destroy(tags_table);
1582 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1583 * is not stable, so the order is already lost. */
1584 static gint compare_top_level_names(const gchar *a, const gchar *b)
1586 guint i;
1587 const gchar *name;
1589 /* This should never happen as it would mean that two or more top
1590 * level items have the same name but it can happen by typos in the translations. */
1591 if (utils_str_equal(a, b))
1592 return 1;
1594 foreach_ptr_array(name, i, top_level_iter_names)
1596 if (utils_str_equal(name, a))
1597 return -1;
1598 if (utils_str_equal(name, b))
1599 return 1;
1601 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1602 return 0;
1606 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1607 GtkTreeIter *iter)
1609 /* if the tag has a parent tag, it should be at depth >= 2 */
1610 return !EMPTY(tag->scope) &&
1611 gtk_tree_store_iter_depth(store, iter) == 1;
1615 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1616 gpointer user_data)
1618 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1619 TMTag *tag_a, *tag_b;
1620 gint cmp;
1622 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1623 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1625 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1626 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1627 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1628 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1630 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1631 compare_symbol_lines(tag_a, tag_b);
1633 else
1635 gchar *astr, *bstr;
1637 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1638 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1640 /* if a is toplevel, b must be also */
1641 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1643 cmp = compare_top_level_names(astr, bstr);
1645 else
1647 /* this is what g_strcmp0() does */
1648 if (! astr)
1649 cmp = -(astr != bstr);
1650 else if (! bstr)
1651 cmp = astr != bstr;
1652 else
1654 cmp = strcmp(astr, bstr);
1656 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1657 if (tag_a && tag_b)
1658 if (!sort_by_name ||
1659 (utils_str_equal(tag_a->name, tag_b->name) &&
1660 utils_str_equal(tag_a->scope, tag_b->scope)))
1661 cmp = compare_symbol_lines(tag_a, tag_b);
1664 g_free(astr);
1665 g_free(bstr);
1667 tm_tag_unref(tag_a);
1668 tm_tag_unref(tag_b);
1670 return cmp;
1674 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1676 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1677 GINT_TO_POINTER(sort_by_name), NULL);
1679 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1683 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1685 GList *tags;
1687 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1689 tags = get_tag_list(doc, tm_tag_max_t);
1690 if (tags == NULL)
1691 return FALSE;
1693 /* FIXME: Not sure why we detached the model here? */
1695 /* disable sorting during update because the code doesn't support correctly
1696 * models that are currently being built */
1697 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1699 /* add grandparent type iters */
1700 add_top_level_items(doc);
1702 update_tree_tags(doc, &tags);
1703 g_list_free(tags);
1705 hide_empty_rows(doc->priv->tag_store);
1707 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1708 sort_mode = doc->priv->symbol_list_sort_mode;
1710 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1711 doc->priv->symbol_list_sort_mode = sort_mode;
1713 return TRUE;
1717 /* Detects a global tags filetype from the *.lang.* language extension.
1718 * Returns NULL if there was no matching TM language. */
1719 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1721 gchar *tags_ext;
1722 gchar *shortname = utils_strdupa(utf8_filename);
1723 GeanyFiletype *ft = NULL;
1725 tags_ext = g_strrstr(shortname, ".tags");
1726 if (tags_ext)
1728 *tags_ext = '\0'; /* remove .tags extension */
1729 ft = filetypes_detect_from_extension(shortname);
1730 if (ft->id != GEANY_FILETYPES_NONE)
1731 return ft;
1733 return NULL;
1737 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1738 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1739 * the relevant path.
1740 * Example:
1741 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1742 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1744 /* -E pre-process, -dD output user macros, -p prof info (?) */
1745 const char pre_process[] = "gcc -E -dD -p -I.";
1747 if (argc > 2)
1749 /* Create global taglist */
1750 int status;
1751 char *command;
1752 const char *tags_file = argv[1];
1753 char *utf8_fname;
1754 GeanyFiletype *ft;
1756 utf8_fname = utils_get_utf8_from_locale(tags_file);
1757 ft = detect_global_tags_filetype(utf8_fname);
1758 g_free(utf8_fname);
1760 if (ft == NULL)
1762 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1763 return 1;
1765 /* load config in case of custom filetypes */
1766 filetypes_load_config(ft->id, FALSE);
1768 /* load ignore list for C/C++ parser */
1769 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1770 load_c_ignore_tags();
1772 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1774 const gchar *cflags = getenv("CFLAGS");
1775 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1777 else
1778 command = NULL; /* don't preprocess */
1780 geany_debug("Generating %s tags file.", ft->name);
1781 tm_get_workspace();
1782 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1783 argc - 2, tags_file, ft->lang);
1784 g_free(command);
1785 symbols_finalize(); /* free c_tags_ignore data */
1786 if (! status)
1788 g_printerr(_("Failed to create tags file, perhaps because no tags "
1789 "were found.\n"));
1790 return 1;
1793 else
1795 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1796 g_printerr(_("Example:\n"
1797 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1798 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1799 return 1;
1801 return 0;
1805 void symbols_show_load_tags_dialog(void)
1807 GtkWidget *dialog;
1808 GtkFileFilter *filter;
1810 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1811 GTK_FILE_CHOOSER_ACTION_OPEN,
1812 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1813 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1814 NULL);
1815 gtk_widget_set_name(dialog, "GeanyDialog");
1816 filter = gtk_file_filter_new();
1817 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1818 gtk_file_filter_add_pattern(filter, "*.*.tags");
1819 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1821 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1823 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1824 GSList *item;
1826 for (item = flist; item != NULL; item = g_slist_next(item))
1828 gchar *fname = item->data;
1829 gchar *utf8_fname;
1830 GeanyFiletype *ft;
1832 utf8_fname = utils_get_utf8_from_locale(fname);
1833 ft = detect_global_tags_filetype(utf8_fname);
1835 if (ft != NULL && symbols_load_global_tags(fname, ft))
1836 /* For translators: the first wildcard is the filetype, the second the filename */
1837 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1838 filetypes_get_display_name(ft), utf8_fname);
1839 else
1840 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1842 g_free(utf8_fname);
1843 g_free(fname);
1845 g_slist_free(flist);
1847 gtk_widget_destroy(dialog);
1851 static void init_user_tags(void)
1853 GSList *file_list = NULL, *list = NULL;
1854 const GSList *node;
1855 gchar *dir;
1857 dir = g_build_filename(app->configdir, "tags", NULL);
1858 /* create the user tags dir for next time if it doesn't exist */
1859 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1860 utils_mkdir(dir, FALSE);
1861 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1863 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1864 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1865 g_free(dir);
1867 file_list = g_slist_concat(file_list, list);
1869 /* populate the filetype-specific tag files lists */
1870 for (node = file_list; node != NULL; node = node->next)
1872 gchar *fname = node->data;
1873 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1874 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1876 g_free(utf8_fname);
1878 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1879 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1880 else
1882 geany_debug("Unknown filetype for file '%s'.", fname);
1883 g_free(fname);
1887 /* don't need to delete list contents because they are now stored in
1888 * ft->priv->tag_files */
1889 g_slist_free(file_list);
1893 static void load_user_tags(filetype_id ft_id)
1895 static guchar *tags_loaded = NULL;
1896 static gboolean init_tags = FALSE;
1897 const GSList *node;
1898 GeanyFiletype *ft = filetypes[ft_id];
1900 g_return_if_fail(ft_id > 0);
1902 if (!tags_loaded)
1903 tags_loaded = g_new0(guchar, filetypes_array->len);
1904 if (tags_loaded[ft_id])
1905 return;
1906 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1908 if (!init_tags)
1910 init_user_tags();
1911 init_tags = TRUE;
1914 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1916 const gchar *fname = node->data;
1918 symbols_load_global_tags(fname, ft);
1923 static gboolean goto_tag(const gchar *name, gboolean definition)
1925 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1926 TMTagType type;
1927 TMTag *tmtag = NULL;
1928 GeanyDocument *old_doc = document_get_current();
1930 /* goto tag definition: all except prototypes / forward declarations / externs */
1931 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1933 /* first look in the current document */
1934 if (old_doc != NULL && old_doc->tm_file)
1935 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1937 /* if not found, look in the workspace */
1938 if (tmtag == NULL)
1939 tmtag = find_workspace_tag(name, type);
1941 if (tmtag != NULL)
1943 GeanyDocument *new_doc = document_find_by_real_path(
1944 tmtag->file->file_name);
1946 if (new_doc)
1948 /* If we are already on the tag line, swap definition/declaration */
1949 if (new_doc == old_doc &&
1950 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1952 if (goto_tag(name, !definition))
1953 return TRUE;
1956 else
1958 /* not found in opened document, should open */
1959 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1962 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1963 return TRUE;
1965 return FALSE;
1969 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1971 if (goto_tag(name, definition))
1972 return TRUE;
1974 /* if we are here, there was no match and we are beeping ;-) */
1975 utils_beep();
1977 if (!definition)
1978 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1979 else
1980 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1981 return FALSE;
1985 /* This could perhaps be improved to check for #if, class etc. */
1986 static gint get_function_fold_number(GeanyDocument *doc)
1988 /* for Java the functions are always one fold level above the class scope */
1989 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1990 return SC_FOLDLEVELBASE + 1;
1991 else
1992 return SC_FOLDLEVELBASE;
1996 /* Should be used only with get_current_tag_cached.
1997 * tag_types caching might trigger recomputation too often but this isn't used differently often
1998 * enough to be an issue for now */
1999 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2001 static gint old_line = -2;
2002 static GeanyDocument *old_doc = NULL;
2003 static gint old_fold_num = -1;
2004 static guint old_tag_types = 0;
2005 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2006 gboolean ret;
2008 /* check if the cached line and file index have changed since last time: */
2009 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2010 ret = TRUE;
2011 else if (cur_line == old_line)
2012 ret = FALSE;
2013 else
2015 /* if the line has only changed by 1 */
2016 if (abs(cur_line - old_line) == 1)
2018 /* It's the same function if the fold number hasn't changed */
2019 ret = (fold_num != old_fold_num);
2021 else ret = TRUE;
2024 /* record current line and file index for next time */
2025 old_line = cur_line;
2026 old_doc = doc;
2027 old_fold_num = fold_num;
2028 old_tag_types = tag_types;
2029 return ret;
2033 /* Parse the function name up to 2 lines before tag_line.
2034 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2035 * type or argument names can be confused with the function name. */
2036 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2038 gint start, end, max_pos;
2039 gint fn_style;
2041 switch (sci_get_lexer(sci))
2043 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2044 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2045 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2047 start = sci_get_position_from_line(sci, tag_line - 2);
2048 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2049 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2050 start++;
2052 end = start;
2053 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2054 end++;
2056 if (start == end)
2057 return NULL;
2058 return sci_get_contents_range(sci, start, end);
2062 /* Parse the function name */
2063 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2065 gint start, end, first_pos, max_pos;
2066 gint tmp;
2067 gchar c;
2069 first_pos = end = sci_get_position_from_line(sci, tag_line);
2070 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2071 tmp = 0;
2072 /* goto the begin of function body */
2073 while (end < max_pos &&
2074 (tmp = sci_get_char_at(sci, end)) != '{' &&
2075 tmp != 0) end++;
2076 if (tmp == 0) end --;
2078 /* go back to the end of function identifier */
2079 while (end > 0 && end > first_pos - 500 &&
2080 (tmp = sci_get_char_at(sci, end)) != '(' &&
2081 tmp != 0) end--;
2082 end--;
2083 if (end < 0) end = 0;
2085 /* skip whitespaces between identifier and ( */
2086 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2088 start = end;
2089 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2090 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2091 || tmp == SCE_C_GLOBALCLASS
2092 || (c = sci_get_char_at(sci, start)) == '~'
2093 || c == ':'))
2094 start--;
2095 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2097 if (start == end) return NULL;
2098 return sci_get_contents_range(sci, start, end + 1);
2102 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2104 gint line_count = sci_get_line_count(sci);
2106 for (; line < line_count; line++)
2108 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2109 return line;
2112 return -1;
2116 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2118 gint line;
2119 gint parent;
2121 line = sci_get_current_line(doc->editor->sci);
2122 parent = sci_get_fold_parent(doc->editor->sci, line);
2123 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2124 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2125 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2127 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2129 if (tag)
2131 gint tag_line = tag->line - 1;
2132 gint last_child = line + 1;
2134 /* if it may be a false positive because we're inside a fold level not inside anything
2135 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2136 * right after the tag we got from TM */
2137 if (abs(tag_line - parent) > 1)
2139 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2140 if (tag_fold >= 0)
2141 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2144 if (line <= last_child)
2146 if (tag->scope)
2147 *tagname = g_strconcat(tag->scope,
2148 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2149 else
2150 *tagname = g_strdup(tag->name);
2152 return tag_line;
2156 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2157 * to dirty and inaccurate hand-parsing */
2158 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2160 const gint fn_fold = get_function_fold_number(doc);
2161 gint tag_line = parent;
2162 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2164 /* find the top level fold point */
2165 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2167 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2168 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2171 if (tag_line >= 0)
2173 gchar *cur_tag;
2175 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2176 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2177 else
2178 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2180 if (cur_tag != NULL)
2182 *tagname = cur_tag;
2183 return tag_line;
2188 *tagname = g_strdup(_("unknown"));
2189 return -1;
2193 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2195 static gint tag_line = -1;
2196 static gchar *cur_tag = NULL;
2198 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2200 if (doc == NULL) /* reset current function */
2202 current_tag_changed(NULL, -1, -1, 0);
2203 g_free(cur_tag);
2204 cur_tag = g_strdup(_("unknown"));
2205 if (tagname != NULL)
2206 *tagname = cur_tag;
2207 tag_line = -1;
2209 else
2211 gint line = sci_get_current_line(doc->editor->sci);
2212 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2214 if (current_tag_changed(doc, line, fold_level, tag_types))
2216 g_free(cur_tag);
2217 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2219 *tagname = cur_tag;
2222 return tag_line;
2226 /* Sets *tagname to point at the current function or tag name.
2227 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2228 * call to this function.
2229 * Returns: line number of the current tag, or -1 if unknown. */
2230 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2232 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2236 /* same as symbols_get_current_function() but finds class, namespaces and more */
2237 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2239 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2240 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2242 /* Python parser reports imports as namespaces which confuses the scope detection */
2243 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2244 tag_types |= tm_tag_namespace_t;
2246 return get_current_tag_name_cached(doc, tagname, tag_types);
2250 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2252 gint sort_mode = GPOINTER_TO_INT(user_data);
2253 GeanyDocument *doc = document_get_current();
2255 if (ignore_callback)
2256 return;
2258 if (doc != NULL)
2259 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2263 static void on_symbol_tree_menu_show(GtkWidget *widget,
2264 gpointer user_data)
2266 GeanyDocument *doc = document_get_current();
2267 gboolean enable;
2269 enable = doc && doc->has_tags;
2270 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2271 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2272 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2273 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2274 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2275 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2277 if (! doc)
2278 return;
2280 ignore_callback = TRUE;
2282 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2283 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2284 else
2285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2287 ignore_callback = FALSE;
2291 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2293 gboolean expand = GPOINTER_TO_INT(user_data);
2294 GeanyDocument *doc = document_get_current();
2296 if (! doc)
2297 return;
2299 g_return_if_fail(doc->priv->tag_tree);
2301 if (expand)
2302 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2303 else
2304 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2308 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2310 GtkTreeIter iter;
2311 GtkTreeSelection *selection;
2312 GtkTreeModel *model;
2313 GeanyDocument *doc;
2314 TMTag *tag = NULL;
2316 doc = document_get_current();
2317 if (!doc)
2318 return;
2320 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2321 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2322 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2323 if (tag)
2325 if (widget == symbol_menu.find_in_files)
2326 search_show_find_in_files_dialog_full(tag->name, NULL);
2327 else
2328 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2329 widget == symbol_menu.find_usage);
2331 tm_tag_unref(tag);
2336 static void create_taglist_popup_menu(void)
2338 GtkWidget *item, *menu;
2340 tv.popup_taglist = menu = gtk_menu_new();
2342 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2343 gtk_widget_show(item);
2344 gtk_container_add(GTK_CONTAINER(menu), item);
2345 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2347 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2348 gtk_widget_show(item);
2349 gtk_container_add(GTK_CONTAINER(menu), item);
2350 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2352 item = gtk_separator_menu_item_new();
2353 gtk_widget_show(item);
2354 gtk_container_add(GTK_CONTAINER(menu), item);
2356 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2357 _("Sort by _Name"));
2358 gtk_widget_show(item);
2359 gtk_container_add(GTK_CONTAINER(menu), item);
2360 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2361 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2363 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2364 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2365 gtk_widget_show(item);
2366 gtk_container_add(GTK_CONTAINER(menu), item);
2367 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2368 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2370 item = gtk_separator_menu_item_new();
2371 gtk_widget_show(item);
2372 gtk_container_add(GTK_CONTAINER(menu), item);
2374 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2375 gtk_widget_show(item);
2376 gtk_container_add(GTK_CONTAINER(menu), item);
2377 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2379 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2380 gtk_widget_show(item);
2381 gtk_container_add(GTK_CONTAINER(menu), item);
2382 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2384 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2385 gtk_widget_show(item);
2386 gtk_container_add(GTK_CONTAINER(menu), item);
2387 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2389 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2391 sidebar_add_common_menu_items(GTK_MENU(menu));
2395 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2397 gchar *f;
2399 g_return_if_fail(!EMPTY(doc->real_path));
2401 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2402 if (utils_str_equal(doc->real_path, f))
2403 load_c_ignore_tags();
2405 g_free(f);
2409 void symbols_init(void)
2411 gchar *f;
2413 create_taglist_popup_menu();
2415 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2416 ui_add_config_file_menu_item(f, NULL, NULL);
2417 g_free(f);
2419 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2423 void symbols_finalize(void)
2425 g_strfreev(html_entities);
2426 g_strfreev(c_tags_ignore);