c family: Add support for digraphs
[geany-mirror.git] / src / symbols.c
blob3c3f85228814d215d1fb0f53da5309d930b5fa4b
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 GdkPixbuf *class_icon = NULL;
100 static GdkPixbuf *macro_icon = NULL;
101 static GdkPixbuf *member_icon = NULL;
102 static GdkPixbuf *method_icon = NULL;
103 static GdkPixbuf *namespace_icon = NULL;
104 static GdkPixbuf *other_icon = NULL;
105 static GdkPixbuf *struct_icon = NULL;
106 static GdkPixbuf *var_icon = NULL;
108 static struct
110 GtkWidget *expand_all;
111 GtkWidget *collapse_all;
112 GtkWidget *sort_by_name;
113 GtkWidget *sort_by_appearance;
114 GtkWidget *find_usage;
115 GtkWidget *find_doc_usage;
116 GtkWidget *find_in_files;
118 symbol_menu;
120 static void html_tags_loaded(void);
121 static void load_user_tags(filetype_id ft_id);
123 /* get the tags_ignore list, exported by tagmanager's options.c */
124 extern gchar **c_tags_ignore;
126 /* ignore certain tokens when parsing C-like syntax.
127 * Also works for reloading. */
128 static void load_c_ignore_tags(void)
130 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
131 gchar *content;
133 if (g_file_get_contents(path, &content, NULL, NULL))
135 /* historically we ignore the glib _DECLS for tag generation */
136 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
138 g_strfreev(c_tags_ignore);
139 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
140 g_free(content);
142 g_free(path);
146 void symbols_reload_config_files(void)
148 load_c_ignore_tags();
152 static gsize get_tag_count(void)
154 GPtrArray *tags = tm_get_workspace()->global_tags;
155 gsize count = tags ? tags->len : 0;
157 return count;
161 /* wrapper for tm_workspace_load_global_tags().
162 * note that the tag count only counts new global tags added - if a tag has the same name,
163 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
164 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
166 gboolean result;
167 gsize old_tag_count = get_tag_count();
169 result = tm_workspace_load_global_tags(tags_file, ft->lang);
170 if (result)
172 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
173 (guint) (get_tag_count() - old_tag_count));
175 return result;
179 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
180 * This provides autocompletion, calltips, etc. */
181 void symbols_global_tags_loaded(guint file_type_idx)
183 TagFileInfo *tfi;
184 gint tag_type;
186 /* load ignore list for C/C++ parser */
187 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
188 c_tags_ignore == NULL)
190 load_c_ignore_tags();
193 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
194 return;
196 /* load config in case of custom filetypes */
197 filetypes_load_config(file_type_idx, FALSE);
199 load_user_tags(file_type_idx);
201 switch (file_type_idx)
203 case GEANY_FILETYPES_PHP:
204 case GEANY_FILETYPES_HTML:
205 html_tags_loaded();
207 switch (file_type_idx)
209 case GEANY_FILETYPES_CPP:
210 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
211 /* no C++ tagfile yet */
212 return;
213 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
214 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
215 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
216 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
217 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
218 default:
219 return;
221 tfi = &tag_file_info[tag_type];
223 if (! tfi->tags_loaded)
225 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
227 symbols_load_global_tags(fname, filetypes[file_type_idx]);
228 tfi->tags_loaded = TRUE;
229 g_free(fname);
234 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
235 static void html_tags_loaded(void)
237 TagFileInfo *tfi;
239 if (cl_options.ignore_global_tags)
240 return;
242 tfi = &tag_file_info[GTF_HTML_ENTITIES];
243 if (! tfi->tags_loaded)
245 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
247 html_entities = utils_read_file_in_array(file);
248 tfi->tags_loaded = TRUE;
249 g_free(file);
254 GString *symbols_find_typenames_as_string(gint lang, gboolean global)
256 guint j;
257 TMTag *tag;
258 GString *s = NULL;
259 GPtrArray *typedefs;
260 gint tag_lang;
262 if (global)
263 typedefs = tm_tags_extract(app->tm_workspace->global_tags, TM_GLOBAL_TYPE_MASK);
264 else
265 typedefs = app->tm_workspace->typename_array;
267 if ((typedefs) && (typedefs->len > 0))
269 s = g_string_sized_new(typedefs->len * 10);
270 for (j = 0; j < typedefs->len; ++j)
272 tag = TM_TAG(typedefs->pdata[j]);
273 tag_lang = tag->lang;
275 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
276 * e.g. PHP classes in C++ files
277 * lang = -2 disables the check */
278 if (tag->name && (tag_lang == lang || lang == -2 ||
279 (lang == TM_PARSER_CPP && tag_lang == TM_PARSER_C)))
281 if (j != 0)
282 g_string_append_c(s, ' ');
283 g_string_append(s, tag->name);
287 if (typedefs && global)
288 g_ptr_array_free(typedefs, TRUE);
289 return s;
293 /** Gets the context separator used by the tag manager for a particular file
294 * type.
295 * @param ft_id File type identifier.
296 * @return The context separator string.
298 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
299 * without a context separator.
301 * @since 0.19
303 GEANY_API_SYMBOL
304 const gchar *symbols_get_context_separator(gint ft_id)
306 switch (ft_id)
308 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
309 case GEANY_FILETYPES_CPP:
310 case GEANY_FILETYPES_GLSL: /* for structs */
311 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
312 case GEANY_FILETYPES_PHP:
313 case GEANY_FILETYPES_RUST:
314 case GEANY_FILETYPES_ZEPHIR:
315 return "::";
317 /* avoid confusion with other possible separators in group/section name */
318 case GEANY_FILETYPES_CONF:
319 case GEANY_FILETYPES_REST:
320 return ":::";
322 /* no context separator */
323 case GEANY_FILETYPES_ASCIIDOC:
324 case GEANY_FILETYPES_TXT2TAGS:
325 return "\x03";
327 default:
328 return ".";
333 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
334 static TMTag *
335 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
337 guint i;
338 g_return_val_if_fail(tags != NULL, NULL);
340 for (i = 0; i < tags->len; ++i)
342 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
343 return TM_TAG(tags->pdata[i]);
345 return NULL;
349 static TMTag *find_source_file_tag(GPtrArray *tags_array,
350 const gchar *tag_name, guint type)
352 GPtrArray *tags;
353 TMTag *tmtag;
355 tags = tm_tags_extract(tags_array, type);
356 if (tags != NULL)
358 tmtag = symbols_find_tm_tag(tags, tag_name);
360 g_ptr_array_free(tags, TRUE);
362 if (tmtag != NULL)
363 return tmtag;
365 return NULL; /* not found */
369 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
371 guint j;
372 const GPtrArray *source_files = NULL;
374 if (app->tm_workspace != NULL)
375 source_files = app->tm_workspace->source_files;
377 if (source_files != NULL)
379 for (j = 0; j < source_files->len; j++)
381 TMSourceFile *srcfile = source_files->pdata[j];
382 TMTag *tmtag;
384 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
385 if (tmtag != NULL)
386 return tmtag;
389 return NULL; /* not found */
393 const gchar **symbols_get_html_entities(void)
395 if (html_entities == NULL)
396 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
398 return (const gchar **) html_entities;
402 /* sort by name, then line */
403 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
405 gint ret;
407 if (tag_a == NULL || tag_b == NULL)
408 return 0;
410 if (tag_a->name == NULL)
411 return -(tag_a->name != tag_b->name);
413 if (tag_b->name == NULL)
414 return tag_a->name != tag_b->name;
416 ret = strcmp(tag_a->name, tag_b->name);
417 if (ret == 0)
419 return tag_a->line - tag_b->line;
421 return ret;
425 /* sort by line, then scope */
426 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
428 const TMTag *tag_a = TM_TAG(a);
429 const TMTag *tag_b = TM_TAG(b);
430 gint ret;
432 if (a == NULL || b == NULL)
433 return 0;
435 ret = tag_a->line - tag_b->line;
436 if (ret == 0)
438 if (tag_a->scope == NULL)
439 return -(tag_a->scope != tag_b->scope);
440 if (tag_b->scope == NULL)
441 return tag_a->scope != tag_b->scope;
442 else
443 return strcmp(tag_a->scope, tag_b->scope);
445 return ret;
449 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
451 GList *tag_names = NULL;
452 TMTag *tag;
453 guint i;
455 g_return_val_if_fail(doc, NULL);
457 if (! doc->tm_file || ! doc->tm_file->tags_array)
458 return NULL;
460 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
462 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
463 if (G_UNLIKELY(tag == NULL))
464 return NULL;
466 if (tag->type & tag_types)
468 tag_names = g_list_prepend(tag_names, tag);
471 tag_names = g_list_sort(tag_names, compare_symbol_lines);
472 return tag_names;
476 /* amount of types in the symbol list (currently max. 8 are used) */
477 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
479 struct TreeviewSymbols
481 GtkTreeIter tag_function;
482 GtkTreeIter tag_class;
483 GtkTreeIter tag_macro;
484 GtkTreeIter tag_member;
485 GtkTreeIter tag_variable;
486 GtkTreeIter tag_externvar;
487 GtkTreeIter tag_namespace;
488 GtkTreeIter tag_struct;
489 GtkTreeIter tag_interface;
490 GtkTreeIter tag_type;
491 GtkTreeIter tag_other;
492 } tv_iters;
495 static void init_tag_iters(void)
497 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
498 * filetypes(e.g. config file to Python crashes Geany without this) */
499 tv_iters.tag_function.stamp = -1;
500 tv_iters.tag_class.stamp = -1;
501 tv_iters.tag_member.stamp = -1;
502 tv_iters.tag_macro.stamp = -1;
503 tv_iters.tag_variable.stamp = -1;
504 tv_iters.tag_externvar.stamp = -1;
505 tv_iters.tag_namespace.stamp = -1;
506 tv_iters.tag_struct.stamp = -1;
507 tv_iters.tag_interface.stamp = -1;
508 tv_iters.tag_type.stamp = -1;
509 tv_iters.tag_other.stamp = -1;
513 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
515 static GtkIconTheme *icon_theme = NULL;
516 static gint x = -1;
518 if (G_UNLIKELY(x < 0))
520 gint dummy;
521 icon_theme = gtk_icon_theme_get_default();
522 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
524 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
528 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
530 GtkTreeModel *model = GTK_TREE_MODEL(store);
532 if (!gtk_tree_model_get_iter_first(model, iter))
533 return FALSE;
536 gchar *candidate;
538 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
539 /* FIXME: what if 2 different items have the same name?
540 * this should never happen, but might be caused by a typo in a translation */
541 if (utils_str_equal(candidate, title))
543 g_free(candidate);
544 return TRUE;
546 else
547 g_free(candidate);
549 while (gtk_tree_model_iter_next(model, iter));
551 return FALSE;
555 /* Adds symbol list groups in (iter*, title) pairs.
556 * The list must be ended with NULL. */
557 static void G_GNUC_NULL_TERMINATED
558 tag_list_add_groups(GtkTreeStore *tree_store, ...)
560 va_list args;
561 GtkTreeIter *iter;
563 g_return_if_fail(top_level_iter_names);
565 va_start(args, tree_store);
566 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
568 gchar *title = va_arg(args, gchar*);
569 GdkPixbuf *icon = va_arg(args, gchar *);
571 g_assert(title != NULL);
572 g_ptr_array_add(top_level_iter_names, title);
574 if (!find_toplevel_iter(tree_store, iter, title))
575 gtk_tree_store_append(tree_store, iter, NULL);
577 if (icon)
578 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
579 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
581 va_end(args);
585 static void add_top_level_items(GeanyDocument *doc)
587 filetype_id ft_id = doc->file_type->id;
588 GtkTreeStore *tag_store = doc->priv->tag_store;
590 if (top_level_iter_names == NULL)
591 top_level_iter_names = g_ptr_array_new();
592 else
593 g_ptr_array_set_size(top_level_iter_names, 0);
595 init_tag_iters();
597 switch (ft_id)
599 case GEANY_FILETYPES_DIFF:
601 tag_list_add_groups(tag_store,
602 &(tv_iters.tag_function), _("Files"), NULL, NULL);
603 break;
605 case GEANY_FILETYPES_DOCBOOK:
607 tag_list_add_groups(tag_store,
608 &(tv_iters.tag_function), _("Chapter"), NULL,
609 &(tv_iters.tag_class), _("Section"), NULL,
610 &(tv_iters.tag_member), _("Sect1"), NULL,
611 &(tv_iters.tag_macro), _("Sect2"), NULL,
612 &(tv_iters.tag_variable), _("Sect3"), NULL,
613 &(tv_iters.tag_struct), _("Appendix"), NULL,
614 &(tv_iters.tag_other), _("Other"), NULL,
615 NULL);
616 break;
618 case GEANY_FILETYPES_HASKELL:
619 tag_list_add_groups(tag_store,
620 &tv_iters.tag_namespace, _("Module"), NULL,
621 &tv_iters.tag_type, _("Types"), NULL,
622 &tv_iters.tag_macro, _("Type constructors"), NULL,
623 &tv_iters.tag_function, _("Functions"), method_icon,
624 NULL);
625 break;
626 case GEANY_FILETYPES_COBOL:
627 tag_list_add_groups(tag_store,
628 &tv_iters.tag_class, _("Program"), class_icon,
629 &tv_iters.tag_function, _("File"), method_icon,
630 &tv_iters.tag_namespace, _("Sections"), namespace_icon,
631 &tv_iters.tag_macro, _("Paragraph"), other_icon,
632 &tv_iters.tag_struct, _("Group"), struct_icon,
633 &tv_iters.tag_variable, _("Data"), var_icon,
634 NULL);
635 break;
636 case GEANY_FILETYPES_CONF:
637 tag_list_add_groups(tag_store,
638 &tv_iters.tag_namespace, _("Sections"), other_icon,
639 &tv_iters.tag_macro, _("Keys"), var_icon,
640 NULL);
641 break;
642 case GEANY_FILETYPES_NSIS:
643 tag_list_add_groups(tag_store,
644 &tv_iters.tag_namespace, _("Sections"), other_icon,
645 &tv_iters.tag_function, _("Functions"), method_icon,
646 &(tv_iters.tag_variable), _("Variables"), var_icon,
647 NULL);
648 break;
649 case GEANY_FILETYPES_LATEX:
651 tag_list_add_groups(tag_store,
652 &(tv_iters.tag_function), _("Command"), NULL,
653 &(tv_iters.tag_class), _("Environment"), NULL,
654 &(tv_iters.tag_member), _("Section"), NULL,
655 &(tv_iters.tag_macro), _("Subsection"), NULL,
656 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
657 &(tv_iters.tag_struct), _("Label"), NULL,
658 &(tv_iters.tag_namespace), _("Chapter"), NULL,
659 &(tv_iters.tag_other), _("Other"), NULL,
660 NULL);
661 break;
663 case GEANY_FILETYPES_MATLAB:
665 tag_list_add_groups(tag_store,
666 &(tv_iters.tag_function), _("Functions"), method_icon,
667 &(tv_iters.tag_struct), _("Structures"), struct_icon,
668 NULL);
669 break;
671 case GEANY_FILETYPES_ABAQUS:
673 tag_list_add_groups(tag_store,
674 &(tv_iters.tag_class), _("Parts"), NULL,
675 &(tv_iters.tag_member), _("Assembly"), NULL,
676 &(tv_iters.tag_namespace), _("Steps"), NULL,
677 NULL);
678 break;
680 case GEANY_FILETYPES_R:
682 tag_list_add_groups(tag_store,
683 &(tv_iters.tag_function), _("Functions"), method_icon,
684 &(tv_iters.tag_other), _("Other"), NULL,
685 NULL);
686 break;
688 case GEANY_FILETYPES_RUST:
690 tag_list_add_groups(tag_store,
691 &(tv_iters.tag_namespace), _("Modules"), namespace_icon,
692 &(tv_iters.tag_struct), _("Structures"), struct_icon,
693 &(tv_iters.tag_interface), _("Traits"), class_icon,
694 &(tv_iters.tag_class), _("Implementations"), class_icon,
695 &(tv_iters.tag_function), _("Functions"), method_icon,
696 &(tv_iters.tag_type), _("Typedefs / Enums"), struct_icon,
697 &(tv_iters.tag_variable), _("Variables"), var_icon,
698 &(tv_iters.tag_macro), _("Macros"), macro_icon,
699 &(tv_iters.tag_member), _("Methods"), member_icon,
700 &(tv_iters.tag_other), _("Other"), other_icon,
701 NULL);
702 break;
704 case GEANY_FILETYPES_GO:
706 tag_list_add_groups(tag_store,
707 &(tv_iters.tag_namespace), _("Package"), namespace_icon,
708 &(tv_iters.tag_function), _("Functions"), method_icon,
709 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
710 &(tv_iters.tag_struct), _("Structs"), struct_icon,
711 &(tv_iters.tag_type), _("Types"), struct_icon,
712 &(tv_iters.tag_macro), _("Constants"), macro_icon,
713 &(tv_iters.tag_variable), _("Variables"), var_icon,
714 &(tv_iters.tag_member), _("Members"), member_icon,
715 &(tv_iters.tag_other), _("Other"), other_icon,
716 NULL);
717 break;
719 case GEANY_FILETYPES_PERL:
721 tag_list_add_groups(tag_store,
722 &(tv_iters.tag_namespace), _("Package"), namespace_icon,
723 &(tv_iters.tag_function), _("Functions"), method_icon,
724 &(tv_iters.tag_macro), _("Labels"), NULL,
725 &(tv_iters.tag_type), _("Constants"), NULL,
726 &(tv_iters.tag_other), _("Other"), other_icon,
727 NULL);
728 break;
730 case GEANY_FILETYPES_PHP:
731 case GEANY_FILETYPES_ZEPHIR:
733 tag_list_add_groups(tag_store,
734 &(tv_iters.tag_namespace), _("Namespaces"), namespace_icon,
735 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
736 &(tv_iters.tag_class), _("Classes"), class_icon,
737 &(tv_iters.tag_function), _("Functions"), method_icon,
738 &(tv_iters.tag_macro), _("Constants"), macro_icon,
739 &(tv_iters.tag_variable), _("Variables"), var_icon,
740 &(tv_iters.tag_struct), _("Traits"), struct_icon,
741 NULL);
742 break;
744 case GEANY_FILETYPES_HTML:
746 tag_list_add_groups(tag_store,
747 &(tv_iters.tag_function), _("Functions"), NULL,
748 &(tv_iters.tag_member), _("Anchors"), NULL,
749 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
750 &(tv_iters.tag_class), _("H2 Headings"), NULL,
751 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
752 NULL);
753 break;
755 case GEANY_FILETYPES_CSS:
757 tag_list_add_groups(tag_store,
758 &(tv_iters.tag_class), _("Classes"), class_icon,
759 &(tv_iters.tag_variable), _("ID Selectors"), var_icon,
760 &(tv_iters.tag_struct), _("Type Selectors"), struct_icon, NULL);
761 break;
763 case GEANY_FILETYPES_REST:
764 case GEANY_FILETYPES_TXT2TAGS:
765 case GEANY_FILETYPES_ABC:
767 tag_list_add_groups(tag_store,
768 &(tv_iters.tag_namespace), _("Chapter"), NULL,
769 &(tv_iters.tag_member), _("Section"), NULL,
770 &(tv_iters.tag_macro), _("Subsection"), NULL,
771 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
772 NULL);
773 break;
775 case GEANY_FILETYPES_ASCIIDOC:
777 tag_list_add_groups(tag_store,
778 &(tv_iters.tag_namespace), _("Document"), NULL,
779 &(tv_iters.tag_member), _("Section Level 1"), NULL,
780 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
781 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
782 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
783 NULL);
784 break;
786 case GEANY_FILETYPES_RUBY:
788 tag_list_add_groups(tag_store,
789 &(tv_iters.tag_namespace), _("Modules"), namespace_icon,
790 &(tv_iters.tag_class), _("Classes"), class_icon,
791 &(tv_iters.tag_member), _("Singletons"), struct_icon,
792 &(tv_iters.tag_function), _("Methods"), method_icon,
793 NULL);
794 break;
796 case GEANY_FILETYPES_TCL:
798 tag_list_add_groups(tag_store,
799 &(tv_iters.tag_namespace), _("Namespaces"), namespace_icon,
800 &(tv_iters.tag_class), _("Classes"), class_icon,
801 &(tv_iters.tag_member), _("Methods"), method_icon,
802 &(tv_iters.tag_function), _("Procedures"), other_icon,
803 NULL);
804 break;
806 case GEANY_FILETYPES_PYTHON:
808 tag_list_add_groups(tag_store,
809 &(tv_iters.tag_class), _("Classes"), class_icon,
810 &(tv_iters.tag_member), _("Methods"), macro_icon,
811 &(tv_iters.tag_function), _("Functions"), method_icon,
812 &(tv_iters.tag_variable), _("Variables"), var_icon,
813 &(tv_iters.tag_externvar), _("Imports"), namespace_icon,
814 NULL);
815 break;
817 case GEANY_FILETYPES_VHDL:
819 tag_list_add_groups(tag_store,
820 &(tv_iters.tag_namespace), _("Package"), namespace_icon,
821 &(tv_iters.tag_class), _("Entities"), class_icon,
822 &(tv_iters.tag_struct), _("Architectures"), struct_icon,
823 &(tv_iters.tag_type), _("Types"), other_icon,
824 &(tv_iters.tag_function), _("Functions / Procedures"), method_icon,
825 &(tv_iters.tag_variable), _("Variables / Signals"), var_icon,
826 &(tv_iters.tag_member), _("Processes / Blocks / Components"), member_icon,
827 &(tv_iters.tag_other), _("Other"), other_icon,
828 NULL);
829 break;
831 case GEANY_FILETYPES_VERILOG:
833 tag_list_add_groups(tag_store,
834 &(tv_iters.tag_type), _("Events"), macro_icon,
835 &(tv_iters.tag_class), _("Modules"), class_icon,
836 &(tv_iters.tag_function), _("Functions / Tasks"), method_icon,
837 &(tv_iters.tag_variable), _("Variables"), var_icon,
838 &(tv_iters.tag_other), _("Other"), other_icon,
839 NULL);
840 break;
842 case GEANY_FILETYPES_JAVA:
844 tag_list_add_groups(tag_store,
845 &(tv_iters.tag_namespace), _("Package"), namespace_icon,
846 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
847 &(tv_iters.tag_class), _("Classes"), class_icon,
848 &(tv_iters.tag_function), _("Methods"), method_icon,
849 &(tv_iters.tag_member), _("Members"), member_icon,
850 &(tv_iters.tag_type), _("Enums"), struct_icon,
851 &(tv_iters.tag_other), _("Other"), other_icon,
852 NULL);
853 break;
855 case GEANY_FILETYPES_AS:
857 tag_list_add_groups(tag_store,
858 &(tv_iters.tag_namespace), _("Package"), namespace_icon,
859 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
860 &(tv_iters.tag_class), _("Classes"), class_icon,
861 &(tv_iters.tag_function), _("Functions"), method_icon,
862 &(tv_iters.tag_member), _("Properties"), member_icon,
863 &(tv_iters.tag_variable), _("Variables"), var_icon,
864 &(tv_iters.tag_macro), _("Constants"), macro_icon,
865 &(tv_iters.tag_other), _("Other"), other_icon,
866 NULL);
867 break;
869 case GEANY_FILETYPES_HAXE:
871 tag_list_add_groups(tag_store,
872 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
873 &(tv_iters.tag_class), _("Classes"), class_icon,
874 &(tv_iters.tag_function), _("Methods"), method_icon,
875 &(tv_iters.tag_type), _("Types"), macro_icon,
876 &(tv_iters.tag_variable), _("Variables"), var_icon,
877 &(tv_iters.tag_other), _("Other"), other_icon,
878 NULL);
879 break;
881 case GEANY_FILETYPES_BASIC:
883 tag_list_add_groups(tag_store,
884 &(tv_iters.tag_function), _("Functions"), method_icon,
885 &(tv_iters.tag_variable), _("Variables"), var_icon,
886 &(tv_iters.tag_macro), _("Constants"), macro_icon,
887 &(tv_iters.tag_struct), _("Types"), namespace_icon,
888 &(tv_iters.tag_namespace), _("Labels"), member_icon,
889 &(tv_iters.tag_other), _("Other"), other_icon,
890 NULL);
891 break;
893 case GEANY_FILETYPES_F77:
894 case GEANY_FILETYPES_FORTRAN:
896 tag_list_add_groups(tag_store,
897 &(tv_iters.tag_namespace), _("Module"), class_icon,
898 &(tv_iters.tag_struct), _("Programs"), class_icon,
899 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
900 &(tv_iters.tag_function), _("Functions / Subroutines"), method_icon,
901 &(tv_iters.tag_variable), _("Variables"), var_icon,
902 &(tv_iters.tag_class), _("Types"), class_icon,
903 &(tv_iters.tag_member), _("Components"), member_icon,
904 &(tv_iters.tag_macro), _("Blocks"), member_icon,
905 &(tv_iters.tag_type), _("Enums"), struct_icon,
906 &(tv_iters.tag_other), _("Other"), other_icon,
907 NULL);
908 break;
910 case GEANY_FILETYPES_ASM:
912 tag_list_add_groups(tag_store,
913 &(tv_iters.tag_namespace), _("Labels"), namespace_icon,
914 &(tv_iters.tag_function), _("Macros"), method_icon,
915 &(tv_iters.tag_macro), _("Defines"), macro_icon,
916 &(tv_iters.tag_struct), _("Types"), struct_icon,
917 NULL);
918 break;
920 case GEANY_FILETYPES_MAKE:
921 tag_list_add_groups(tag_store,
922 &tv_iters.tag_function, _("Targets"), method_icon,
923 &tv_iters.tag_macro, _("Macros"), macro_icon,
924 NULL);
925 break;
926 case GEANY_FILETYPES_SQL:
928 tag_list_add_groups(tag_store,
929 &(tv_iters.tag_function), _("Functions"), method_icon,
930 &(tv_iters.tag_namespace), _("Procedures"), namespace_icon,
931 &(tv_iters.tag_struct), _("Indexes"), struct_icon,
932 &(tv_iters.tag_class), _("Tables"), class_icon,
933 &(tv_iters.tag_macro), _("Triggers"), macro_icon,
934 &(tv_iters.tag_member), _("Views"), var_icon,
935 &(tv_iters.tag_other), _("Other"), other_icon,
936 &(tv_iters.tag_variable), _("Variables"), var_icon,
937 NULL);
938 break;
940 case GEANY_FILETYPES_D:
941 default:
943 if (ft_id == GEANY_FILETYPES_D)
944 tag_list_add_groups(tag_store,
945 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
946 else
947 tag_list_add_groups(tag_store,
948 &(tv_iters.tag_namespace), _("Namespaces"), namespace_icon, NULL);
950 tag_list_add_groups(tag_store,
951 &(tv_iters.tag_class), _("Classes"), class_icon,
952 &(tv_iters.tag_interface), _("Interfaces"), struct_icon,
953 &(tv_iters.tag_function), _("Functions"), method_icon,
954 &(tv_iters.tag_member), _("Members"), member_icon,
955 &(tv_iters.tag_struct), _("Structs"), struct_icon,
956 &(tv_iters.tag_type), _("Typedefs / Enums"), struct_icon,
957 NULL);
959 if (ft_id != GEANY_FILETYPES_D)
961 tag_list_add_groups(tag_store,
962 &(tv_iters.tag_macro), _("Macros"), macro_icon, NULL);
964 tag_list_add_groups(tag_store,
965 &(tv_iters.tag_variable), _("Variables"), var_icon,
966 &(tv_iters.tag_externvar), _("Extern Variables"), var_icon,
967 &(tv_iters.tag_other), _("Other"), other_icon, NULL);
973 /* removes toplevel items that have no children */
974 static void hide_empty_rows(GtkTreeStore *store)
976 GtkTreeIter iter;
977 gboolean cont = TRUE;
979 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
980 return; /* stop when first iter is invalid, i.e. no elements */
982 while (cont)
984 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
985 cont = gtk_tree_store_remove(store, &iter);
986 else
987 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
992 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
994 gchar *utf8_name;
995 const gchar *scope = tag->scope;
996 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
997 gboolean doc_is_utf8 = FALSE;
999 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1000 * for None at this point completely */
1001 if (utils_str_equal(doc->encoding, "UTF-8") ||
1002 utils_str_equal(doc->encoding, "None"))
1003 doc_is_utf8 = TRUE;
1004 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1005 * plugin might have called tm_source_file_update(), so check to be sure */
1006 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1008 if (! doc_is_utf8)
1009 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1010 -1, doc->encoding, TRUE);
1011 else
1012 utf8_name = tag->name;
1014 if (utf8_name == NULL)
1015 return NULL;
1017 if (! buffer)
1018 buffer = g_string_new(NULL);
1019 else
1020 g_string_truncate(buffer, 0);
1022 /* check first char of scope is a wordchar */
1023 if (!found_parent && scope &&
1024 strpbrk(scope, GEANY_WORDCHARS) == scope)
1026 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1028 g_string_append(buffer, scope);
1029 g_string_append(buffer, sep);
1031 g_string_append(buffer, utf8_name);
1033 if (! doc_is_utf8)
1034 g_free(utf8_name);
1036 g_string_append_printf(buffer, " [%lu]", tag->line);
1038 return buffer->str;
1042 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1044 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1046 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1047 * for None at this point completely */
1048 if (utf8_name != NULL &&
1049 ! utils_str_equal(doc->encoding, "UTF-8") &&
1050 ! utils_str_equal(doc->encoding, "None"))
1052 SETPTR(utf8_name,
1053 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1056 return utf8_name;
1060 /* find the last word in "foo::bar::blah", e.g. "blah" */
1061 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1063 const gchar *scope = tag->scope;
1064 const gchar *separator = symbols_get_context_separator(ft_id);
1065 const gchar *str, *ptr;
1067 if (!scope)
1068 return NULL;
1070 str = scope;
1072 while (1)
1074 ptr = strstr(str, separator);
1075 if (ptr)
1077 str = ptr + strlen(separator);
1079 else
1080 break;
1083 return !EMPTY(str) ? str : NULL;
1087 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type)
1089 GtkTreeIter *iter = NULL;
1091 switch (tag_type)
1093 case tm_tag_prototype_t:
1094 case tm_tag_method_t:
1095 case tm_tag_function_t:
1097 iter = &tv_iters.tag_function;
1098 break;
1100 case tm_tag_externvar_t:
1102 iter = &tv_iters.tag_externvar;
1103 break;
1105 case tm_tag_macro_t:
1106 case tm_tag_macro_with_arg_t:
1108 iter = &tv_iters.tag_macro;
1109 break;
1111 case tm_tag_class_t:
1113 iter = &tv_iters.tag_class;
1114 break;
1116 case tm_tag_member_t:
1117 case tm_tag_field_t:
1119 iter = &tv_iters.tag_member;
1120 break;
1122 case tm_tag_typedef_t:
1123 case tm_tag_enum_t:
1125 iter = &tv_iters.tag_type;
1126 break;
1128 case tm_tag_union_t:
1129 case tm_tag_struct_t:
1131 iter = &tv_iters.tag_struct;
1132 break;
1134 case tm_tag_interface_t:
1135 iter = &tv_iters.tag_interface;
1136 break;
1137 case tm_tag_variable_t:
1139 iter = &tv_iters.tag_variable;
1140 break;
1142 case tm_tag_namespace_t:
1143 case tm_tag_package_t:
1145 iter = &tv_iters.tag_namespace;
1146 break;
1148 default:
1150 iter = &tv_iters.tag_other;
1153 if (G_LIKELY(iter->stamp != -1))
1154 return iter;
1155 else
1156 return NULL;
1160 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1162 GdkPixbuf *icon = NULL;
1164 if (parent == &tv_iters.tag_other)
1166 return g_object_ref(var_icon);
1168 /* copy parent icon */
1169 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1170 SYMBOLS_COLUMN_ICON, &icon, -1);
1171 return icon;
1175 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1177 const TMTag *t1 = v1;
1178 const TMTag *t2 = v2;
1180 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1181 utils_str_equal(t1->scope, t2->scope) &&
1182 /* include arglist in match to support e.g. C++ overloading */
1183 utils_str_equal(t1->arglist, t2->arglist));
1187 /* inspired from g_str_hash() */
1188 static guint tag_hash(gconstpointer v)
1190 const TMTag *tag = v;
1191 const gchar *p;
1192 guint32 h = 5381;
1194 h = (h << 5) + h + tag->type;
1195 for (p = tag->name; *p != '\0'; p++)
1196 h = (h << 5) + h + *p;
1197 if (tag->scope)
1199 for (p = tag->scope; *p != '\0'; p++)
1200 h = (h << 5) + h + *p;
1202 /* for e.g. C++ overloading */
1203 if (tag->arglist)
1205 for (p = tag->arglist; *p != '\0'; p++)
1206 h = (h << 5) + h + *p;
1209 return h;
1213 /* like gtk_tree_view_expand_to_path() but with an iter */
1214 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1216 GtkTreeModel *model = gtk_tree_view_get_model(view);
1217 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1219 gtk_tree_view_expand_to_path(view, path);
1220 gtk_tree_path_free(path);
1224 /* like gtk_tree_store_remove() but finds the next iter at any level */
1225 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1227 GtkTreeIter parent;
1228 gboolean has_parent;
1229 gboolean cont;
1231 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1232 cont = gtk_tree_store_remove(store, iter);
1233 /* if there is no next at this level but there is a parent iter, continue from it */
1234 if (! cont && has_parent)
1236 *iter = parent;
1237 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1240 return cont;
1244 /* adds a new element in the parent table if it's key is known.
1245 * duplicates are kept */
1246 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1247 const GtkTreeIter *iter)
1249 GList **list;
1250 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1251 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1253 if (! list)
1255 list = g_slice_alloc(sizeof *list);
1256 *list = NULL;
1257 g_hash_table_insert(table, tag->name, list);
1259 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1264 static void free_iter_slice_list(gpointer data)
1266 GList **list = data;
1268 if (list)
1270 GList *node;
1271 foreach_list(node, *list)
1272 g_slice_free(GtkTreeIter, node->data);
1273 g_list_free(*list);
1274 g_slice_free1(sizeof *list, list);
1279 /* inserts a @data in @table on key @tag.
1280 * previous data is not overwritten if the key is duplicated, but rather the
1281 * two values are kept in a list
1283 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1284 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1286 GList *list = g_hash_table_lookup(table, tag);
1287 list = g_list_prepend(list, data);
1288 g_hash_table_insert(table, tag, list);
1292 /* looks up the entry in @table that better matches @tag.
1293 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1294 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1296 GList *data = NULL;
1297 GList *node = g_hash_table_lookup(table, tag);
1298 if (node)
1300 glong delta;
1301 data = node->data;
1303 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1305 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1306 for (node = node->next; node; node = node->next)
1308 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1310 if (d < delta)
1312 data = node->data;
1313 delta = d;
1317 #undef TAG_DELTA
1320 return data;
1324 /* removes the element at @tag from @table.
1325 * @tag must be the exact pointer used at insertion time */
1326 static void tags_table_remove(GHashTable *table, TMTag *tag)
1328 GList *list = g_hash_table_lookup(table, tag);
1329 if (list)
1331 GList *node;
1332 foreach_list(node, list)
1334 if (((GList *) node->data)->data == tag)
1335 break;
1337 list = g_list_delete_link(list, node);
1338 if (list)
1339 g_hash_table_insert(table, tag, list);
1340 else
1341 g_hash_table_remove(table, tag);
1346 static void tags_table_destroy(GHashTable *table)
1348 /* free any leftover elements. note that we can't register a value_free_func when
1349 * creating the hash table because we only want to free it when destroying the table,
1350 * not when inserting a duplicate (we handle this manually) */
1351 GHashTableIter iter;
1352 gpointer value;
1354 g_hash_table_iter_init(&iter, table);
1355 while (g_hash_table_iter_next(&iter, NULL, &value))
1356 g_list_free(value);
1357 g_hash_table_destroy(table);
1362 * Updates the tag tree for a document with the tags in *list.
1363 * @param doc a document
1364 * @param tags a pointer to a GList* holding the tags to add/update. This
1365 * list may be updated, removing updated elements.
1367 * The update is done in two passes:
1368 * 1) walking the current tree, update tags that still exist and remove the
1369 * obsolescent ones;
1370 * 2) walking the remaining (non updated) tags, adds them in the list.
1372 * For better performances, we use 2 hash tables:
1373 * - one containing all the tags for lookup in the first pass (actually stores a
1374 * reference in the tags list for removing it efficiently), avoiding list search
1375 * on each tag;
1376 * - the other holding "tag-name":row references for tags having children, used to
1377 * lookup for a parent in both passes, avoiding tree traversal.
1379 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1381 GtkTreeStore *store = doc->priv->tag_store;
1382 GtkTreeModel *model = GTK_TREE_MODEL(store);
1383 GHashTable *parents_table;
1384 GHashTable *tags_table;
1385 GtkTreeIter iter;
1386 gboolean cont;
1387 GList *item;
1389 /* Build hash tables holding tags and parents */
1390 /* parent table holds "tag-name":GtkTreeIter */
1391 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1392 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1393 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1394 foreach_list(item, *tags)
1396 TMTag *tag = item->data;
1397 const gchar *name;
1399 tags_table_insert(tags_table, tag, item);
1401 name = get_parent_name(tag, doc->file_type->id);
1402 if (name)
1403 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1406 /* First pass, update existing rows or delete them.
1407 * It is OK to delete them since we walk top down so we would remove
1408 * parents before checking for their children, thus never implicitly
1409 * deleting an updated child */
1410 cont = gtk_tree_model_get_iter_first(model, &iter);
1411 while (cont)
1413 TMTag *tag;
1415 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1416 if (! tag) /* most probably a toplevel, skip it */
1417 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1418 else
1420 GList *found_item;
1422 found_item = tags_table_lookup(tags_table, tag);
1423 if (! found_item) /* tag doesn't exist, remove it */
1424 cont = tree_store_remove_row(store, &iter);
1425 else /* tag still exist, update it */
1427 const gchar *parent_name;
1428 TMTag *found = found_item->data;
1430 parent_name = get_parent_name(found, doc->file_type->id);
1431 /* if parent is unknown, ignore it */
1432 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1433 parent_name = NULL;
1435 if (!tm_tags_equal(tag, found))
1437 const gchar *name;
1438 gchar *tooltip;
1440 /* only update fields that (can) have changed (name that holds line
1441 * number, tooltip, and the tag itself) */
1442 name = get_symbol_name(doc, found, parent_name != NULL);
1443 tooltip = get_symbol_tooltip(doc, found);
1444 gtk_tree_store_set(store, &iter,
1445 SYMBOLS_COLUMN_NAME, name,
1446 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1447 SYMBOLS_COLUMN_TAG, found,
1448 -1);
1449 g_free(tooltip);
1452 update_parents_table(parents_table, found, parent_name, &iter);
1454 /* remove the updated tag from the table and list */
1455 tags_table_remove(tags_table, found);
1456 *tags = g_list_delete_link(*tags, found_item);
1458 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1461 tm_tag_unref(tag);
1465 /* Second pass, now we have a tree cleaned up from invalid rows,
1466 * we simply add new ones */
1467 foreach_list (item, *tags)
1469 TMTag *tag = item->data;
1470 GtkTreeIter *parent;
1472 parent = get_tag_type_iter(tag->type);
1473 if (G_UNLIKELY(! parent))
1474 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1475 else
1477 gboolean expand;
1478 const gchar *name;
1479 const gchar *parent_name;
1480 gchar *tooltip;
1481 GdkPixbuf *icon = get_child_icon(store, parent);
1483 parent_name = get_parent_name(tag, doc->file_type->id);
1484 if (parent_name)
1486 GList **candidates;
1487 GtkTreeIter *parent_search = NULL;
1489 /* walk parent candidates to find the better one.
1490 * if there are more than one, take the one that has the closest line number
1491 * after the tag we're searching the parent for */
1492 candidates = g_hash_table_lookup(parents_table, parent_name);
1493 if (candidates)
1495 GList *node;
1496 glong delta = G_MAXLONG;
1497 foreach_list(node, *candidates)
1499 TMTag *parent_tag;
1500 glong d;
1502 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1503 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1505 d = tag->line - parent_tag->line;
1506 if (! parent_search || (d >= 0 && d < delta))
1508 delta = d;
1509 parent_search = node->data;
1511 tm_tag_unref(parent_tag);
1515 if (parent_search)
1516 parent = parent_search;
1517 else
1518 parent_name = NULL;
1521 /* only expand to the iter if the parent was empty, otherwise we let the
1522 * folding as it was before (already expanded, or closed by the user) */
1523 expand = ! gtk_tree_model_iter_has_child(model, parent);
1525 /* insert the new element */
1526 name = get_symbol_name(doc, tag, parent_name != NULL);
1527 tooltip = get_symbol_tooltip(doc, tag);
1528 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1529 SYMBOLS_COLUMN_NAME, name,
1530 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1531 SYMBOLS_COLUMN_ICON, icon,
1532 SYMBOLS_COLUMN_TAG, tag,
1533 -1);
1534 g_free(tooltip);
1535 if (G_LIKELY(icon))
1536 g_object_unref(icon);
1538 update_parents_table(parents_table, tag, parent_name, &iter);
1540 if (expand)
1541 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1545 g_hash_table_destroy(parents_table);
1546 tags_table_destroy(tags_table);
1550 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1551 * is not stable, so the order is already lost. */
1552 static gint compare_top_level_names(const gchar *a, const gchar *b)
1554 guint i;
1555 const gchar *name;
1557 /* This should never happen as it would mean that two or more top
1558 * level items have the same name but it can happen by typos in the translations. */
1559 if (utils_str_equal(a, b))
1560 return 1;
1562 foreach_ptr_array(name, i, top_level_iter_names)
1564 if (utils_str_equal(name, a))
1565 return -1;
1566 if (utils_str_equal(name, b))
1567 return 1;
1569 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1570 return 0;
1574 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1575 GtkTreeIter *iter)
1577 /* if the tag has a parent tag, it should be at depth >= 2 */
1578 return !EMPTY(tag->scope) &&
1579 gtk_tree_store_iter_depth(store, iter) == 1;
1583 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1584 gpointer user_data)
1586 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1587 TMTag *tag_a, *tag_b;
1588 gint cmp;
1590 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1591 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1593 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1594 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1595 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1596 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1598 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1599 compare_symbol_lines(tag_a, tag_b);
1601 else
1603 gchar *astr, *bstr;
1605 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1606 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1608 /* if a is toplevel, b must be also */
1609 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1611 cmp = compare_top_level_names(astr, bstr);
1613 else
1615 /* this is what g_strcmp0() does */
1616 if (! astr)
1617 cmp = -(astr != bstr);
1618 else if (! bstr)
1619 cmp = astr != bstr;
1620 else
1622 cmp = strcmp(astr, bstr);
1624 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1625 if (tag_a && tag_b)
1626 if (!sort_by_name ||
1627 (utils_str_equal(tag_a->name, tag_b->name) &&
1628 utils_str_equal(tag_a->scope, tag_b->scope)))
1629 cmp = compare_symbol_lines(tag_a, tag_b);
1632 g_free(astr);
1633 g_free(bstr);
1635 tm_tag_unref(tag_a);
1636 tm_tag_unref(tag_b);
1638 return cmp;
1642 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1644 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1645 GINT_TO_POINTER(sort_by_name), NULL);
1647 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1651 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1653 GList *tags;
1655 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1657 tags = get_tag_list(doc, tm_tag_max_t);
1658 if (tags == NULL)
1659 return FALSE;
1661 /* FIXME: Not sure why we detached the model here? */
1663 /* disable sorting during update because the code doesn't support correctly
1664 * models that are currently being built */
1665 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1667 /* add grandparent type iters */
1668 add_top_level_items(doc);
1670 update_tree_tags(doc, &tags);
1671 g_list_free(tags);
1673 hide_empty_rows(doc->priv->tag_store);
1675 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1676 sort_mode = doc->priv->symbol_list_sort_mode;
1678 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1679 doc->priv->symbol_list_sort_mode = sort_mode;
1681 return TRUE;
1685 /* Detects a global tags filetype from the *.lang.* language extension.
1686 * Returns NULL if there was no matching TM language. */
1687 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1689 gchar *tags_ext;
1690 gchar *shortname = utils_strdupa(utf8_filename);
1691 GeanyFiletype *ft = NULL;
1693 tags_ext = g_strrstr(shortname, ".tags");
1694 if (tags_ext)
1696 *tags_ext = '\0'; /* remove .tags extension */
1697 ft = filetypes_detect_from_extension(shortname);
1698 if (ft->id != GEANY_FILETYPES_NONE)
1699 return ft;
1701 return NULL;
1705 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1706 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1707 * the relevant path.
1708 * Example:
1709 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1710 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1712 /* -E pre-process, -dD output user macros, -p prof info (?) */
1713 const char pre_process[] = "gcc -E -dD -p -I.";
1715 if (argc > 2)
1717 /* Create global taglist */
1718 int status;
1719 char *command;
1720 const char *tags_file = argv[1];
1721 char *utf8_fname;
1722 GeanyFiletype *ft;
1724 utf8_fname = utils_get_utf8_from_locale(tags_file);
1725 ft = detect_global_tags_filetype(utf8_fname);
1726 g_free(utf8_fname);
1728 if (ft == NULL)
1730 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1731 return 1;
1733 /* load config in case of custom filetypes */
1734 filetypes_load_config(ft->id, FALSE);
1736 /* load ignore list for C/C++ parser */
1737 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1738 load_c_ignore_tags();
1740 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1742 const gchar *cflags = getenv("CFLAGS");
1743 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1745 else
1746 command = NULL; /* don't preprocess */
1748 geany_debug("Generating %s tags file.", ft->name);
1749 tm_get_workspace();
1750 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1751 argc - 2, tags_file, ft->lang);
1752 g_free(command);
1753 symbols_finalize(); /* free c_tags_ignore data */
1754 if (! status)
1756 g_printerr(_("Failed to create tags file, perhaps because no tags "
1757 "were found.\n"));
1758 return 1;
1761 else
1763 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1764 g_printerr(_("Example:\n"
1765 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1766 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1767 return 1;
1769 return 0;
1773 void symbols_show_load_tags_dialog(void)
1775 GtkWidget *dialog;
1776 GtkFileFilter *filter;
1778 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1779 GTK_FILE_CHOOSER_ACTION_OPEN,
1780 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1781 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1782 NULL);
1783 gtk_widget_set_name(dialog, "GeanyDialog");
1784 filter = gtk_file_filter_new();
1785 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1786 gtk_file_filter_add_pattern(filter, "*.*.tags");
1787 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1789 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1791 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1792 GSList *item;
1794 for (item = flist; item != NULL; item = g_slist_next(item))
1796 gchar *fname = item->data;
1797 gchar *utf8_fname;
1798 GeanyFiletype *ft;
1800 utf8_fname = utils_get_utf8_from_locale(fname);
1801 ft = detect_global_tags_filetype(utf8_fname);
1803 if (ft != NULL && symbols_load_global_tags(fname, ft))
1804 /* For translators: the first wildcard is the filetype, the second the filename */
1805 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1806 filetypes_get_display_name(ft), utf8_fname);
1807 else
1808 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1810 g_free(utf8_fname);
1811 g_free(fname);
1813 g_slist_free(flist);
1815 gtk_widget_destroy(dialog);
1819 static void init_user_tags(void)
1821 GSList *file_list = NULL, *list = NULL;
1822 const GSList *node;
1823 gchar *dir;
1825 dir = g_build_filename(app->configdir, "tags", NULL);
1826 /* create the user tags dir for next time if it doesn't exist */
1827 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1828 utils_mkdir(dir, FALSE);
1829 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1831 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1832 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1833 g_free(dir);
1835 file_list = g_slist_concat(file_list, list);
1837 /* populate the filetype-specific tag files lists */
1838 for (node = file_list; node != NULL; node = node->next)
1840 gchar *fname = node->data;
1841 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1842 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1844 g_free(utf8_fname);
1846 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1847 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1848 else
1850 geany_debug("Unknown filetype for file '%s'.", fname);
1851 g_free(fname);
1855 /* don't need to delete list contents because they are now stored in
1856 * ft->priv->tag_files */
1857 g_slist_free(file_list);
1861 static void load_user_tags(filetype_id ft_id)
1863 static guchar *tags_loaded = NULL;
1864 static gboolean init_tags = FALSE;
1865 const GSList *node;
1866 GeanyFiletype *ft = filetypes[ft_id];
1868 g_return_if_fail(ft_id > 0);
1870 if (!tags_loaded)
1871 tags_loaded = g_new0(guchar, filetypes_array->len);
1872 if (tags_loaded[ft_id])
1873 return;
1874 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1876 if (!init_tags)
1878 init_user_tags();
1879 init_tags = TRUE;
1882 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1884 const gchar *fname = node->data;
1886 symbols_load_global_tags(fname, ft);
1891 static gboolean goto_tag(const gchar *name, gboolean definition)
1893 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1894 TMTagType type;
1895 TMTag *tmtag = NULL;
1896 GeanyDocument *old_doc = document_get_current();
1898 /* goto tag definition: all except prototypes / forward declarations / externs */
1899 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1901 /* first look in the current document */
1902 if (old_doc != NULL && old_doc->tm_file)
1903 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1905 /* if not found, look in the workspace */
1906 if (tmtag == NULL)
1907 tmtag = find_workspace_tag(name, type);
1909 if (tmtag != NULL)
1911 GeanyDocument *new_doc = document_find_by_real_path(
1912 tmtag->file->file_name);
1914 if (new_doc)
1916 /* If we are already on the tag line, swap definition/declaration */
1917 if (new_doc == old_doc &&
1918 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1920 if (goto_tag(name, !definition))
1921 return TRUE;
1924 else
1926 /* not found in opened document, should open */
1927 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1930 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1931 return TRUE;
1933 return FALSE;
1937 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1939 if (goto_tag(name, definition))
1940 return TRUE;
1942 /* if we are here, there was no match and we are beeping ;-) */
1943 utils_beep();
1945 if (!definition)
1946 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1947 else
1948 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1949 return FALSE;
1953 /* This could perhaps be improved to check for #if, class etc. */
1954 static gint get_function_fold_number(GeanyDocument *doc)
1956 /* for Java the functions are always one fold level above the class scope */
1957 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1958 return SC_FOLDLEVELBASE + 1;
1959 else
1960 return SC_FOLDLEVELBASE;
1964 /* Should be used only with get_current_tag_cached.
1965 * tag_types caching might trigger recomputation too often but this isn't used differently often
1966 * enough to be an issue for now */
1967 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1969 static gint old_line = -2;
1970 static GeanyDocument *old_doc = NULL;
1971 static gint old_fold_num = -1;
1972 static guint old_tag_types = 0;
1973 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1974 gboolean ret;
1976 /* check if the cached line and file index have changed since last time: */
1977 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1978 ret = TRUE;
1979 else if (cur_line == old_line)
1980 ret = FALSE;
1981 else
1983 /* if the line has only changed by 1 */
1984 if (abs(cur_line - old_line) == 1)
1986 /* It's the same function if the fold number hasn't changed */
1987 ret = (fold_num != old_fold_num);
1989 else ret = TRUE;
1992 /* record current line and file index for next time */
1993 old_line = cur_line;
1994 old_doc = doc;
1995 old_fold_num = fold_num;
1996 old_tag_types = tag_types;
1997 return ret;
2001 /* Parse the function name up to 2 lines before tag_line.
2002 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2003 * type or argument names can be confused with the function name. */
2004 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2006 gint start, end, max_pos;
2007 gint fn_style;
2009 switch (sci_get_lexer(sci))
2011 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2012 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2013 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2015 start = sci_get_position_from_line(sci, tag_line - 2);
2016 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2017 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2018 start++;
2020 end = start;
2021 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2022 end++;
2024 if (start == end)
2025 return NULL;
2026 return sci_get_contents_range(sci, start, end);
2030 /* Parse the function name */
2031 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2033 gint start, end, first_pos, max_pos;
2034 gint tmp;
2035 gchar c;
2037 first_pos = end = sci_get_position_from_line(sci, tag_line);
2038 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2039 tmp = 0;
2040 /* goto the begin of function body */
2041 while (end < max_pos &&
2042 (tmp = sci_get_char_at(sci, end)) != '{' &&
2043 tmp != 0) end++;
2044 if (tmp == 0) end --;
2046 /* go back to the end of function identifier */
2047 while (end > 0 && end > first_pos - 500 &&
2048 (tmp = sci_get_char_at(sci, end)) != '(' &&
2049 tmp != 0) end--;
2050 end--;
2051 if (end < 0) end = 0;
2053 /* skip whitespaces between identifier and ( */
2054 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2056 start = end;
2057 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2058 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2059 || tmp == SCE_C_GLOBALCLASS
2060 || (c = sci_get_char_at(sci, start)) == '~'
2061 || c == ':'))
2062 start--;
2063 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2065 if (start == end) return NULL;
2066 return sci_get_contents_range(sci, start, end + 1);
2070 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2072 gint line_count = sci_get_line_count(sci);
2074 for (; line < line_count; line++)
2076 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2077 return line;
2080 return -1;
2084 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2086 gint line;
2087 gint parent;
2089 line = sci_get_current_line(doc->editor->sci);
2090 parent = sci_get_fold_parent(doc->editor->sci, line);
2091 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2092 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2093 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2095 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2097 if (tag)
2099 gint tag_line = tag->line - 1;
2100 gint last_child = line + 1;
2102 /* if it may be a false positive because we're inside a fold level not inside anything
2103 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2104 * right after the tag we got from TM */
2105 if (abs(tag_line - parent) > 1)
2107 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2108 if (tag_fold >= 0)
2109 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2112 if (line <= last_child)
2114 if (tag->scope)
2115 *tagname = g_strconcat(tag->scope,
2116 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2117 else
2118 *tagname = g_strdup(tag->name);
2120 return tag_line;
2124 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2125 * to dirty and inaccurate hand-parsing */
2126 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2128 const gint fn_fold = get_function_fold_number(doc);
2129 gint tag_line = parent;
2130 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2132 /* find the top level fold point */
2133 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2135 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2136 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2139 if (tag_line >= 0)
2141 gchar *cur_tag;
2143 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2144 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2145 else
2146 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2148 if (cur_tag != NULL)
2150 *tagname = cur_tag;
2151 return tag_line;
2156 *tagname = g_strdup(_("unknown"));
2157 return -1;
2161 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2163 static gint tag_line = -1;
2164 static gchar *cur_tag = NULL;
2166 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2168 if (doc == NULL) /* reset current function */
2170 current_tag_changed(NULL, -1, -1, 0);
2171 g_free(cur_tag);
2172 cur_tag = g_strdup(_("unknown"));
2173 if (tagname != NULL)
2174 *tagname = cur_tag;
2175 tag_line = -1;
2177 else
2179 gint line = sci_get_current_line(doc->editor->sci);
2180 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2182 if (current_tag_changed(doc, line, fold_level, tag_types))
2184 g_free(cur_tag);
2185 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2187 *tagname = cur_tag;
2190 return tag_line;
2194 /* Sets *tagname to point at the current function or tag name.
2195 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2196 * call to this function.
2197 * Returns: line number of the current tag, or -1 if unknown. */
2198 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2200 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2204 /* same as symbols_get_current_function() but finds class, namespaces and more */
2205 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2207 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2208 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2210 /* Python parser reports imports as namespaces which confuses the scope detection */
2211 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2212 tag_types |= tm_tag_namespace_t;
2214 return get_current_tag_name_cached(doc, tagname, tag_types);
2218 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2220 gint sort_mode = GPOINTER_TO_INT(user_data);
2221 GeanyDocument *doc = document_get_current();
2223 if (ignore_callback)
2224 return;
2226 if (doc != NULL)
2227 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2231 static void on_symbol_tree_menu_show(GtkWidget *widget,
2232 gpointer user_data)
2234 GeanyDocument *doc = document_get_current();
2235 gboolean enable;
2237 enable = doc && doc->has_tags;
2238 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2239 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2240 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2241 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2242 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2243 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2245 if (! doc)
2246 return;
2248 ignore_callback = TRUE;
2250 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2251 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2252 else
2253 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2255 ignore_callback = FALSE;
2259 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2261 gboolean expand = GPOINTER_TO_INT(user_data);
2262 GeanyDocument *doc = document_get_current();
2264 if (! doc)
2265 return;
2267 g_return_if_fail(doc->priv->tag_tree);
2269 if (expand)
2270 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2271 else
2272 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2276 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2278 GtkTreeIter iter;
2279 GtkTreeSelection *selection;
2280 GtkTreeModel *model;
2281 GeanyDocument *doc;
2282 TMTag *tag = NULL;
2284 doc = document_get_current();
2285 if (!doc)
2286 return;
2288 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2289 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2290 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2291 if (tag)
2293 if (widget == symbol_menu.find_in_files)
2294 search_show_find_in_files_dialog_full(tag->name, NULL);
2295 else
2296 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2297 widget == symbol_menu.find_usage);
2299 tm_tag_unref(tag);
2304 static void create_taglist_popup_menu(void)
2306 GtkWidget *item, *menu;
2308 tv.popup_taglist = menu = gtk_menu_new();
2310 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2311 gtk_widget_show(item);
2312 gtk_container_add(GTK_CONTAINER(menu), item);
2313 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2315 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2316 gtk_widget_show(item);
2317 gtk_container_add(GTK_CONTAINER(menu), item);
2318 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2320 item = gtk_separator_menu_item_new();
2321 gtk_widget_show(item);
2322 gtk_container_add(GTK_CONTAINER(menu), item);
2324 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2325 _("Sort by _Name"));
2326 gtk_widget_show(item);
2327 gtk_container_add(GTK_CONTAINER(menu), item);
2328 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2329 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2331 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2332 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2333 gtk_widget_show(item);
2334 gtk_container_add(GTK_CONTAINER(menu), item);
2335 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2336 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2338 item = gtk_separator_menu_item_new();
2339 gtk_widget_show(item);
2340 gtk_container_add(GTK_CONTAINER(menu), item);
2342 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2343 gtk_widget_show(item);
2344 gtk_container_add(GTK_CONTAINER(menu), item);
2345 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2347 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2348 gtk_widget_show(item);
2349 gtk_container_add(GTK_CONTAINER(menu), item);
2350 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2352 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2353 gtk_widget_show(item);
2354 gtk_container_add(GTK_CONTAINER(menu), item);
2355 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2357 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2359 sidebar_add_common_menu_items(GTK_MENU(menu));
2363 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2365 gchar *f;
2367 g_return_if_fail(!EMPTY(doc->real_path));
2369 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2370 if (utils_str_equal(doc->real_path, f))
2371 load_c_ignore_tags();
2373 g_free(f);
2377 void symbols_init(void)
2379 gchar *f;
2381 create_taglist_popup_menu();
2383 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2384 ui_add_config_file_menu_item(f, NULL, NULL);
2385 g_free(f);
2387 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2389 class_icon = get_tag_icon("classviewer-class");
2390 macro_icon = get_tag_icon("classviewer-macro");
2391 member_icon = get_tag_icon("classviewer-member");
2392 method_icon = get_tag_icon("classviewer-method");
2393 namespace_icon = get_tag_icon("classviewer-namespace");
2394 other_icon = get_tag_icon("classviewer-other");
2395 struct_icon = get_tag_icon("classviewer-struct");
2396 var_icon = get_tag_icon("classviewer-var");
2400 void symbols_finalize(void)
2402 g_strfreev(html_entities);
2403 g_strfreev(c_tags_ignore);
2405 g_object_unref(class_icon);
2406 g_object_unref(macro_icon);
2407 g_object_unref(member_icon);
2408 g_object_unref(method_icon);
2409 g_object_unref(namespace_icon);
2410 g_object_unref(other_icon);
2411 g_object_unref(struct_icon);
2412 g_object_unref(var_icon);