Fix various TMTag leaks
[geany-mirror.git] / src / symbols.c
blobddb6336605fb48598e7b10a5156355f6655cba13
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 #include "SciLexer.h"
36 #include "geany.h"
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdlib.h>
42 #include "prefix.h"
43 #include "symbols.h"
44 #include "utils.h"
45 #include "filetypes.h"
46 #include "encodings.h"
47 #include "document.h"
48 #include "documentprivate.h"
49 #include "support.h"
50 #include "msgwindow.h"
51 #include "sidebar.h"
52 #include "main.h"
53 #include "navqueue.h"
54 #include "ui_utils.h"
55 #include "editor.h"
56 #include "sciwrappers.h"
57 #include "filetypesprivate.h"
58 #include "search.h"
61 const guint TM_GLOBAL_TYPE_MASK =
62 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
63 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
66 static gchar **html_entities = NULL;
68 typedef struct
70 gboolean tags_loaded;
71 const gchar *tag_file;
72 } TagFileInfo;
74 /* Check before adding any more tags files, usually they should be downloaded separately. */
75 enum /* Geany tag files */
77 GTF_C,
78 GTF_PASCAL,
79 GTF_PHP,
80 GTF_HTML_ENTITIES,
81 GTF_LATEX,
82 GTF_PYTHON,
83 GTF_MAX
86 static TagFileInfo tag_file_info[GTF_MAX] =
88 {FALSE, "c99.tags"},
89 {FALSE, "pascal.tags"},
90 {FALSE, "php.tags"},
91 {FALSE, "html_entities.tags"},
92 {FALSE, "latex.tags"},
93 {FALSE, "python.tags"}
96 static GPtrArray *top_level_iter_names = NULL;
98 static struct
100 GtkWidget *expand_all;
101 GtkWidget *collapse_all;
102 GtkWidget *sort_by_name;
103 GtkWidget *sort_by_appearance;
104 GtkWidget *find_usage;
105 GtkWidget *find_doc_usage;
107 symbol_menu;
110 static void html_tags_loaded(void);
111 static void load_user_tags(filetype_id ft_id);
113 /* get the tags_ignore list, exported by tagmanager's options.c */
114 extern gchar **c_tags_ignore;
116 /* ignore certain tokens when parsing C-like syntax.
117 * Also works for reloading. */
118 static void load_c_ignore_tags(void)
120 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
121 gchar *content;
123 if (g_file_get_contents(path, &content, NULL, NULL))
125 /* historically we ignore the glib _DECLS for tag generation */
126 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
128 g_strfreev(c_tags_ignore);
129 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
130 g_free(content);
132 g_free(path);
136 void symbols_reload_config_files(void)
138 load_c_ignore_tags();
142 static gsize get_tag_count(void)
144 GPtrArray *tags = tm_get_workspace()->global_tags;
145 gsize count = tags ? tags->len : 0;
147 return count;
151 /* wrapper for tm_workspace_load_global_tags().
152 * note that the tag count only counts new global tags added - if a tag has the same name,
153 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
154 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
156 gboolean result;
157 gsize old_tag_count = get_tag_count();
159 result = tm_workspace_load_global_tags(tags_file, ft->lang);
160 if (result)
162 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
163 (guint) (get_tag_count() - old_tag_count));
165 return result;
169 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
170 * This provides autocompletion, calltips, etc. */
171 void symbols_global_tags_loaded(guint file_type_idx)
173 TagFileInfo *tfi;
174 gint tag_type;
176 /* load ignore list for C/C++ parser */
177 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
178 c_tags_ignore == NULL)
180 load_c_ignore_tags();
183 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
184 return;
186 /* load config in case of custom filetypes */
187 filetypes_load_config(file_type_idx, FALSE);
189 load_user_tags(file_type_idx);
191 switch (file_type_idx)
193 case GEANY_FILETYPES_PHP:
194 case GEANY_FILETYPES_HTML:
195 html_tags_loaded();
197 switch (file_type_idx)
199 case GEANY_FILETYPES_CPP:
200 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
201 /* no C++ tagfile yet */
202 return;
203 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
204 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
205 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
206 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
207 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
208 default:
209 return;
211 tfi = &tag_file_info[tag_type];
213 if (! tfi->tags_loaded)
215 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
217 symbols_load_global_tags(fname, filetypes[file_type_idx]);
218 tfi->tags_loaded = TRUE;
219 g_free(fname);
224 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
225 static void html_tags_loaded(void)
227 TagFileInfo *tfi;
229 if (cl_options.ignore_global_tags)
230 return;
232 tfi = &tag_file_info[GTF_HTML_ENTITIES];
233 if (! tfi->tags_loaded)
235 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
237 html_entities = utils_read_file_in_array(file);
238 tfi->tags_loaded = TRUE;
239 g_free(file);
244 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
246 guint j;
247 TMTag *tag;
248 GString *s = NULL;
249 GPtrArray *typedefs;
250 gint tag_lang;
252 g_return_val_if_fail(tags_array != NULL, NULL);
254 typedefs = tm_tags_extract(tags_array, tag_types);
256 if ((typedefs) && (typedefs->len > 0))
258 s = g_string_sized_new(typedefs->len * 10);
259 for (j = 0; j < typedefs->len; ++j)
261 tag = TM_TAG(typedefs->pdata[j]);
262 /* tag->atts.file.lang contains (for some reason) the line of the tag if
263 * tag->atts.entry.file is not NULL */
264 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
266 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
267 * e.g. PHP classes in C++ files
268 * lang = -2 disables the check */
269 if (tag->name && (tag_lang == lang || lang == -2))
271 if (j != 0)
272 g_string_append_c(s, ' ');
273 g_string_append(s, tag->name);
277 if (typedefs)
278 g_ptr_array_free(typedefs, TRUE);
279 return s;
283 /** Gets the context separator used by the tag manager for a particular file
284 * type.
285 * @param ft_id File type identifier.
286 * @return The context separator string.
288 * @since 0.19
290 const gchar *symbols_get_context_separator(gint ft_id)
292 switch (ft_id)
294 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
295 case GEANY_FILETYPES_CPP:
296 case GEANY_FILETYPES_GLSL: /* for structs */
297 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
298 return "::";
300 /* avoid confusion with other possible separators in group/section name */
301 case GEANY_FILETYPES_CONF:
302 case GEANY_FILETYPES_REST:
303 return ":::";
305 default:
306 return ".";
311 GString *symbols_get_macro_list(gint lang)
313 guint j, i;
314 GPtrArray *ftags;
315 GString *words;
316 gint tag_lang;
317 TMTag *tag;
319 if (app->tm_workspace->work_objects == NULL)
320 return NULL;
322 ftags = g_ptr_array_sized_new(50);
323 words = g_string_sized_new(200);
325 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
327 GPtrArray *tags;
329 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
330 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
331 if (NULL != tags)
333 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
335 tag = TM_TAG(tags->pdata[i]);
336 tag_lang = (tag->atts.entry.file) ?
337 tag->atts.entry.file->lang : tag->atts.file.lang;
339 if (tag_lang == lang)
340 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
342 g_ptr_array_free(tags, TRUE);
346 if (ftags->len == 0)
348 g_ptr_array_free(ftags, TRUE);
349 g_string_free(words, TRUE);
350 return NULL;
353 tm_tags_sort(ftags, NULL, FALSE);
354 for (j = 0; j < ftags->len; j++)
356 if (j > 0)
357 g_string_append_c(words, '\n');
358 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
360 g_ptr_array_free(ftags, TRUE);
361 return words;
365 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
366 static TMTag *
367 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
369 guint i;
370 g_return_val_if_fail(tags != NULL, NULL);
372 for (i = 0; i < tags->len; ++i)
374 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
375 return TM_TAG(tags->pdata[i]);
377 return NULL;
381 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
382 const gchar *tag_name, guint type)
384 GPtrArray *tags;
385 TMTag *tmtag;
387 if (G_LIKELY(workobj != NULL))
389 tags = tm_tags_extract(workobj->tags_array, type);
390 if (tags != NULL)
392 tmtag = symbols_find_tm_tag(tags, tag_name);
394 g_ptr_array_free(tags, TRUE);
396 if (tmtag != NULL)
397 return tmtag;
400 return NULL; /* not found */
404 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
406 guint j;
407 const GPtrArray *work_objects = NULL;
409 if (app->tm_workspace != NULL)
410 work_objects = app->tm_workspace->work_objects;
412 if (work_objects != NULL)
414 for (j = 0; j < work_objects->len; j++)
416 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
417 TMTag *tmtag;
419 tmtag = find_work_object_tag(workobj, tag_name, type);
420 if (tmtag != NULL)
421 return tmtag;
424 return NULL; /* not found */
428 const gchar **symbols_get_html_entities(void)
430 if (html_entities == NULL)
431 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
433 return (const gchar **) html_entities;
437 /* sort by name, then line */
438 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
440 gint ret;
442 if (tag_a == NULL || tag_b == NULL)
443 return 0;
445 if (tag_a->name == NULL)
446 return -(tag_a->name != tag_b->name);
448 if (tag_b->name == NULL)
449 return tag_a->name != tag_b->name;
451 ret = strcmp(tag_a->name, tag_b->name);
452 if (ret == 0)
454 return tag_a->atts.entry.line - tag_b->atts.entry.line;
456 return ret;
460 /* sort by line, then scope */
461 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
463 const TMTag *tag_a = TM_TAG(a);
464 const TMTag *tag_b = TM_TAG(b);
465 gint ret;
467 if (a == NULL || b == NULL)
468 return 0;
470 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
471 if (ret == 0)
473 if (tag_a->atts.entry.scope == NULL)
474 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
475 if (tag_b->atts.entry.scope == NULL)
476 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
477 else
478 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
480 return ret;
484 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
486 GList *tag_names = NULL;
487 TMTag *tag;
488 guint i;
490 g_return_val_if_fail(doc, NULL);
492 if (! doc->tm_file || ! doc->tm_file->tags_array)
493 return NULL;
495 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
497 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
498 if (G_UNLIKELY(tag == NULL))
499 return NULL;
501 if (tag->type & tag_types)
503 tag_names = g_list_prepend(tag_names, tag);
506 tag_names = g_list_sort(tag_names, compare_symbol_lines);
507 return tag_names;
511 /* amount of types in the symbol list (currently max. 8 are used) */
512 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
514 struct TreeviewSymbols
516 GtkTreeIter tag_function;
517 GtkTreeIter tag_class;
518 GtkTreeIter tag_macro;
519 GtkTreeIter tag_member;
520 GtkTreeIter tag_variable;
521 GtkTreeIter tag_namespace;
522 GtkTreeIter tag_struct;
523 GtkTreeIter tag_interface;
524 GtkTreeIter tag_type;
525 GtkTreeIter tag_other;
526 } tv_iters;
529 static void init_tag_iters(void)
531 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
532 * filetypes(e.g. config file to Python crashes Geany without this) */
533 tv_iters.tag_function.stamp = -1;
534 tv_iters.tag_class.stamp = -1;
535 tv_iters.tag_member.stamp = -1;
536 tv_iters.tag_macro.stamp = -1;
537 tv_iters.tag_variable.stamp = -1;
538 tv_iters.tag_namespace.stamp = -1;
539 tv_iters.tag_struct.stamp = -1;
540 tv_iters.tag_interface.stamp = -1;
541 tv_iters.tag_type.stamp = -1;
542 tv_iters.tag_other.stamp = -1;
546 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
548 static GtkIconTheme *icon_theme = NULL;
549 static gint x = -1;
551 if (G_UNLIKELY(x < 0))
553 gint dummy;
554 icon_theme = gtk_icon_theme_get_default();
555 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
557 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
561 /* finds the next iter at any level
562 * @param iter in/out, the current iter, will be changed to the next one
563 * @param down whether to try the child iter
564 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
565 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
567 GtkTreeIter guess;
568 GtkTreeIter copy = *iter;
570 /* go down if the item has children */
571 if (down && gtk_tree_model_iter_children(model, &guess, iter))
572 *iter = guess;
573 /* or to the next item at the same level */
574 else if (gtk_tree_model_iter_next(model, &copy))
575 *iter = copy;
576 /* or to the next item at a parent level */
577 else if (gtk_tree_model_iter_parent(model, &guess, iter))
579 copy = guess;
580 while(TRUE)
582 if (gtk_tree_model_iter_next(model, &copy))
584 *iter = copy;
585 return TRUE;
587 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
588 guess = copy;
589 else
590 return FALSE;
593 else
594 return FALSE;
596 return TRUE;
600 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
602 GtkTreeModel *model = GTK_TREE_MODEL(store);
604 if (!gtk_tree_model_get_iter_first(model, iter))
605 return FALSE;
608 gchar *candidate;
610 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
611 /* FIXME: what if 2 different items have the same name?
612 * this should never happen, but might be caused by a typo in a translation */
613 if (utils_str_equal(candidate, title))
615 g_free(candidate);
616 return TRUE;
618 else
619 g_free(candidate);
621 while (gtk_tree_model_iter_next(model, iter));
623 return FALSE;
627 /* Adds symbol list groups in (iter*, title) pairs.
628 * The list must be ended with NULL. */
629 static void G_GNUC_NULL_TERMINATED
630 tag_list_add_groups(GtkTreeStore *tree_store, ...)
632 va_list args;
633 GtkTreeIter *iter;
635 g_return_if_fail(top_level_iter_names);
637 va_start(args, tree_store);
638 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
640 gchar *title = va_arg(args, gchar*);
641 gchar *icon_name = va_arg(args, gchar *);
642 GdkPixbuf *icon = NULL;
644 if (icon_name)
646 icon = get_tag_icon(icon_name);
649 g_assert(title != NULL);
650 g_ptr_array_add(top_level_iter_names, title);
652 if (!find_toplevel_iter(tree_store, iter, title))
653 gtk_tree_store_append(tree_store, iter, NULL);
655 if (G_IS_OBJECT(icon))
657 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
658 g_object_unref(icon);
660 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
662 va_end(args);
666 static void add_top_level_items(GeanyDocument *doc)
668 filetype_id ft_id = doc->file_type->id;
669 GtkTreeStore *tag_store = doc->priv->tag_store;
671 if (top_level_iter_names == NULL)
672 top_level_iter_names = g_ptr_array_new();
673 else
674 g_ptr_array_set_size(top_level_iter_names, 0);
676 init_tag_iters();
678 switch (ft_id)
680 case GEANY_FILETYPES_DIFF:
682 tag_list_add_groups(tag_store,
683 &(tv_iters.tag_function), _("Files"), NULL, NULL);
684 break;
686 case GEANY_FILETYPES_DOCBOOK:
688 tag_list_add_groups(tag_store,
689 &(tv_iters.tag_function), _("Chapter"), NULL,
690 &(tv_iters.tag_class), _("Section"), NULL,
691 &(tv_iters.tag_member), _("Sect1"), NULL,
692 &(tv_iters.tag_macro), _("Sect2"), NULL,
693 &(tv_iters.tag_variable), _("Sect3"), NULL,
694 &(tv_iters.tag_struct), _("Appendix"), NULL,
695 &(tv_iters.tag_other), _("Other"), NULL,
696 NULL);
697 break;
699 case GEANY_FILETYPES_HASKELL:
700 tag_list_add_groups(tag_store,
701 &tv_iters.tag_namespace, _("Module"), NULL,
702 &tv_iters.tag_type, _("Types"), NULL,
703 &tv_iters.tag_macro, _("Type constructors"), NULL,
704 &tv_iters.tag_function, _("Functions"), "classviewer-method",
705 NULL);
706 break;
707 case GEANY_FILETYPES_COBOL:
708 tag_list_add_groups(tag_store,
709 &tv_iters.tag_class, _("Program"), "classviewer-class",
710 &tv_iters.tag_function, _("File"), "classviewer-method",
711 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
712 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
713 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
714 &tv_iters.tag_variable, _("Data"), "classviewer-var",
715 NULL);
716 break;
717 case GEANY_FILETYPES_CONF:
718 tag_list_add_groups(tag_store,
719 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
720 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
721 NULL);
722 break;
723 case GEANY_FILETYPES_NSIS:
724 tag_list_add_groups(tag_store,
725 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
726 &tv_iters.tag_function, _("Functions"), "classviewer-method",
727 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
728 NULL);
729 break;
730 case GEANY_FILETYPES_LATEX:
732 tag_list_add_groups(tag_store,
733 &(tv_iters.tag_function), _("Command"), NULL,
734 &(tv_iters.tag_class), _("Environment"), NULL,
735 &(tv_iters.tag_member), _("Section"), NULL,
736 &(tv_iters.tag_macro), _("Subsection"), NULL,
737 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
738 &(tv_iters.tag_struct), _("Label"), NULL,
739 &(tv_iters.tag_namespace), _("Chapter"), NULL,
740 &(tv_iters.tag_other), _("Other"), NULL,
741 NULL);
742 break;
744 case GEANY_FILETYPES_MATLAB:
746 tag_list_add_groups(tag_store,
747 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
748 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
749 NULL);
750 break;
752 case GEANY_FILETYPES_ABAQUS:
754 tag_list_add_groups(tag_store,
755 &(tv_iters.tag_class), _("Parts"), NULL,
756 &(tv_iters.tag_member), _("Assembly"), NULL,
757 &(tv_iters.tag_namespace), _("Steps"), NULL,
758 NULL);
759 break;
761 case GEANY_FILETYPES_R:
763 tag_list_add_groups(tag_store,
764 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
765 &(tv_iters.tag_struct), _("Other"), NULL,
766 NULL);
767 break;
769 case GEANY_FILETYPES_PERL:
771 tag_list_add_groups(tag_store,
772 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
773 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
774 &(tv_iters.tag_macro), _("Labels"), NULL,
775 &(tv_iters.tag_type), _("Constants"), NULL,
776 &(tv_iters.tag_other), _("Other"), "classviewer-other",
777 NULL);
778 break;
780 case GEANY_FILETYPES_PHP:
782 tag_list_add_groups(tag_store,
783 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
784 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
785 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
786 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
787 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
788 NULL);
789 break;
791 case GEANY_FILETYPES_HTML:
793 tag_list_add_groups(tag_store,
794 &(tv_iters.tag_function), _("Functions"), NULL,
795 &(tv_iters.tag_member), _("Anchors"), NULL,
796 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
797 &(tv_iters.tag_class), _("H2 Headings"), NULL,
798 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
799 NULL);
800 break;
802 case GEANY_FILETYPES_CSS:
804 tag_list_add_groups(tag_store,
805 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
806 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
807 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
808 break;
810 case GEANY_FILETYPES_REST:
811 case GEANY_FILETYPES_TXT2TAGS:
812 case GEANY_FILETYPES_ABC:
814 tag_list_add_groups(tag_store,
815 &(tv_iters.tag_namespace), _("Chapter"), NULL,
816 &(tv_iters.tag_member), _("Section"), NULL,
817 &(tv_iters.tag_macro), _("Subsection"), NULL,
818 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
819 NULL);
820 break;
822 case GEANY_FILETYPES_ASCIIDOC:
824 tag_list_add_groups(tag_store,
825 &(tv_iters.tag_namespace), _("Document"), NULL,
826 &(tv_iters.tag_member), _("Section Level 1"), NULL,
827 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
828 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
829 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
830 NULL);
831 break;
833 case GEANY_FILETYPES_RUBY:
835 tag_list_add_groups(tag_store,
836 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
837 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
838 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
839 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
840 NULL);
841 break;
843 case GEANY_FILETYPES_TCL:
845 tag_list_add_groups(tag_store,
846 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
847 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
848 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
849 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
850 NULL);
851 break;
853 case GEANY_FILETYPES_PYTHON:
855 tag_list_add_groups(tag_store,
856 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
857 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
858 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
859 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
860 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
861 NULL);
862 break;
864 case GEANY_FILETYPES_VHDL:
866 tag_list_add_groups(tag_store,
867 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
868 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
869 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
870 &(tv_iters.tag_type), _("Types"), "classviewer-other",
871 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
872 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
873 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
874 &(tv_iters.tag_other), _("Other"), "classviewer-other",
875 NULL);
876 break;
878 case GEANY_FILETYPES_VERILOG:
880 tag_list_add_groups(tag_store,
881 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
882 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
883 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
884 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
885 &(tv_iters.tag_other), _("Other"), "classviewer-other",
886 NULL);
887 break;
889 case GEANY_FILETYPES_JAVA:
891 tag_list_add_groups(tag_store,
892 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
893 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
894 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
895 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
896 &(tv_iters.tag_member), _("Members"), "classviewer-member",
897 &(tv_iters.tag_other), _("Other"), "classviewer-other",
898 NULL);
899 break;
901 case GEANY_FILETYPES_AS:
903 tag_list_add_groups(tag_store,
904 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
905 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
906 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
907 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
908 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
909 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
910 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
911 &(tv_iters.tag_other), _("Other"), "classviewer-other",
912 NULL);
913 break;
915 case GEANY_FILETYPES_HAXE:
917 tag_list_add_groups(tag_store,
918 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
919 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
920 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
921 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
922 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
923 &(tv_iters.tag_other), _("Other"), "classviewer-other",
924 NULL);
925 break;
927 case GEANY_FILETYPES_BASIC:
929 tag_list_add_groups(tag_store,
930 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
931 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
932 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
933 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
934 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
935 &(tv_iters.tag_other), _("Other"), "classviewer-other",
936 NULL);
937 break;
939 case GEANY_FILETYPES_F77:
940 case GEANY_FILETYPES_FORTRAN:
942 tag_list_add_groups(tag_store,
943 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
944 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
945 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
946 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
947 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
948 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
949 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
950 &(tv_iters.tag_other), _("Other"), "classviewer-other",
951 NULL);
952 break;
954 case GEANY_FILETYPES_ASM:
956 tag_list_add_groups(tag_store,
957 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
958 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
959 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
960 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
961 NULL);
962 break;
964 case GEANY_FILETYPES_MAKE:
965 tag_list_add_groups(tag_store,
966 &tv_iters.tag_function, _("Targets"), "classviewer-method",
967 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
968 NULL);
969 break;
970 case GEANY_FILETYPES_SQL:
972 tag_list_add_groups(tag_store,
973 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
974 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
975 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
976 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
977 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
978 &(tv_iters.tag_member), _("Views"), "classviewer-var",
979 &(tv_iters.tag_other), _("Other"), "classviewer-other",
980 NULL);
981 break;
983 case GEANY_FILETYPES_D:
984 default:
986 if (ft_id == GEANY_FILETYPES_D)
987 tag_list_add_groups(tag_store,
988 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
989 else
990 tag_list_add_groups(tag_store,
991 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
993 tag_list_add_groups(tag_store,
994 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
995 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
996 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
997 &(tv_iters.tag_member), _("Members"), "classviewer-member",
998 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
999 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
1000 NULL);
1002 if (ft_id != GEANY_FILETYPES_D)
1004 tag_list_add_groups(tag_store,
1005 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1007 tag_list_add_groups(tag_store,
1008 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1009 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1015 /* removes toplevel items that have no children */
1016 static void hide_empty_rows(GtkTreeStore *store)
1018 GtkTreeIter iter;
1019 gboolean cont = TRUE;
1021 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1022 return; /* stop when first iter is invalid, i.e. no elements */
1024 while (cont)
1026 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1027 cont = gtk_tree_store_remove(store, &iter);
1028 else
1029 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1034 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1036 gchar *utf8_name;
1037 const gchar *scope = tag->atts.entry.scope;
1038 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1039 gboolean doc_is_utf8 = FALSE;
1041 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1042 * for None at this point completely */
1043 if (utils_str_equal(doc->encoding, "UTF-8") ||
1044 utils_str_equal(doc->encoding, "None"))
1045 doc_is_utf8 = TRUE;
1046 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1047 * plugin might have called tm_source_file_update(), so check to be sure */
1048 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1050 if (! doc_is_utf8)
1051 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1052 -1, doc->encoding, TRUE);
1053 else
1054 utf8_name = tag->name;
1056 if (utf8_name == NULL)
1057 return NULL;
1059 if (! buffer)
1060 buffer = g_string_new(NULL);
1061 else
1062 g_string_truncate(buffer, 0);
1064 /* check first char of scope is a wordchar */
1065 if (!found_parent && scope &&
1066 strpbrk(scope, GEANY_WORDCHARS) == scope)
1068 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1070 g_string_append(buffer, scope);
1071 g_string_append(buffer, sep);
1073 g_string_append(buffer, utf8_name);
1075 if (! doc_is_utf8)
1076 g_free(utf8_name);
1078 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1080 return buffer->str;
1084 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1086 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1088 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1089 * for None at this point completely */
1090 if (utf8_name != NULL &&
1091 ! utils_str_equal(doc->encoding, "UTF-8") &&
1092 ! utils_str_equal(doc->encoding, "None"))
1094 SETPTR(utf8_name,
1095 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1098 if (utf8_name != NULL)
1099 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1101 return utf8_name;
1105 /* find the last word in "foo::bar::blah", e.g. "blah" */
1106 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1108 const gchar *scope = tag->atts.entry.scope;
1109 const gchar *separator = symbols_get_context_separator(ft_id);
1110 const gchar *str, *ptr;
1112 if (!scope)
1113 return NULL;
1115 str = scope;
1117 while (1)
1119 ptr = strstr(str, separator);
1120 if (ptr)
1122 str = ptr + strlen(separator);
1124 else
1125 break;
1128 return NZV(str) ? str : NULL;
1132 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1134 GtkTreeIter *iter = NULL;
1136 switch (tag_type)
1138 case tm_tag_prototype_t:
1139 case tm_tag_method_t:
1140 case tm_tag_function_t:
1142 iter = &tv_iters.tag_function;
1143 break;
1145 case tm_tag_macro_t:
1146 case tm_tag_macro_with_arg_t:
1148 iter = &tv_iters.tag_macro;
1149 break;
1151 case tm_tag_class_t:
1153 iter = &tv_iters.tag_class;
1154 break;
1156 case tm_tag_member_t:
1157 case tm_tag_field_t:
1159 iter = &tv_iters.tag_member;
1160 break;
1162 case tm_tag_typedef_t:
1163 case tm_tag_enum_t:
1165 iter = &tv_iters.tag_type;
1166 break;
1168 case tm_tag_union_t:
1169 case tm_tag_struct_t:
1171 iter = &tv_iters.tag_struct;
1172 break;
1174 case tm_tag_interface_t:
1175 iter = &tv_iters.tag_interface;
1176 break;
1177 case tm_tag_variable_t:
1179 iter = &tv_iters.tag_variable;
1180 break;
1182 case tm_tag_namespace_t:
1183 case tm_tag_package_t:
1185 iter = &tv_iters.tag_namespace;
1186 break;
1188 default:
1190 iter = &tv_iters.tag_other;
1193 if (G_LIKELY(iter->stamp != -1))
1194 return iter;
1195 else
1196 return NULL;
1200 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1202 GdkPixbuf *icon = NULL;
1204 if (parent == &tv_iters.tag_other)
1206 return get_tag_icon("classviewer-var");
1208 /* copy parent icon */
1209 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1210 SYMBOLS_COLUMN_ICON, &icon, -1);
1211 return icon;
1215 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1217 const TMTag *t1 = v1;
1218 const TMTag *t2 = v2;
1220 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1221 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1222 /* include arglist in match to support e.g. C++ overloading */
1223 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1227 /* inspired from g_str_hash() */
1228 static guint tag_hash(gconstpointer v)
1230 const TMTag *tag = v;
1231 const gchar *p;
1232 guint32 h = 5381;
1234 h = (h << 5) + h + tag->type;
1235 for (p = tag->name; *p != '\0'; p++)
1236 h = (h << 5) + h + *p;
1237 if (tag->atts.entry.scope)
1239 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1240 h = (h << 5) + h + *p;
1242 /* for e.g. C++ overloading */
1243 if (tag->atts.entry.arglist)
1245 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1246 h = (h << 5) + h + *p;
1249 return h;
1253 /* like gtk_tree_view_expand_to_path() but with an iter */
1254 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1256 GtkTreeModel *model = gtk_tree_view_get_model(view);
1257 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1259 gtk_tree_view_expand_to_path(view, path);
1260 gtk_tree_path_free(path);
1264 /* like gtk_tree_store_remove() but finds the next iter at any level */
1265 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1267 GtkTreeIter parent;
1268 gboolean has_parent;
1269 gboolean cont;
1271 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1272 cont = gtk_tree_store_remove(store, iter);
1273 /* if there is no next at this level but there is a parent iter, continue from it */
1274 if (! cont && has_parent)
1276 *iter = parent;
1277 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1280 return cont;
1284 /* adds a new element in the parent table if it's key is known.
1285 * duplicates are kept */
1286 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1287 const GtkTreeIter *iter)
1289 GList **list;
1290 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1291 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1293 if (! list)
1295 list = g_slice_alloc(sizeof *list);
1296 *list = NULL;
1297 g_hash_table_insert(table, tag->name, list);
1299 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1304 static void free_iter_slice_list(gpointer data)
1306 GList **list = data;
1308 if (list)
1310 GList *node;
1311 foreach_list(node, *list)
1312 g_slice_free(GtkTreeIter, node->data);
1313 g_list_free(*list);
1314 g_slice_free1(sizeof *list, list);
1319 /* inserts a @data in @table on key @tag.
1320 * previous data is not overwritten if the key is duplicated, but rather the
1321 * two values are kept in a list
1323 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1324 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1326 GList *list = g_hash_table_lookup(table, tag);
1327 list = g_list_prepend(list, data);
1328 g_hash_table_insert(table, tag, list);
1332 /* looks up the entry in @table that better matches @tag.
1333 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1334 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1336 GList *data = NULL;
1337 GList *node = g_hash_table_lookup(table, tag);
1338 if (node)
1340 glong delta;
1341 data = node->data;
1343 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1345 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1346 for (node = node->next; node; node = node->next)
1348 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1350 if (d < delta)
1352 data = node->data;
1353 delta = d;
1357 #undef TAG_DELTA
1360 return data;
1364 /* removes the element at @tag from @table.
1365 * @tag must be the exact pointer used at insertion time */
1366 static void tags_table_remove(GHashTable *table, TMTag *tag)
1368 GList *list = g_hash_table_lookup(table, tag);
1369 if (list)
1371 GList *node;
1372 foreach_list(node, list)
1374 if (((GList *) node->data)->data == tag)
1375 break;
1377 list = g_list_delete_link(list, node);
1378 if (list)
1379 g_hash_table_insert(table, tag, list);
1380 else
1381 g_hash_table_remove(table, tag);
1387 * Updates the tag tree for a document with the tags in *list.
1388 * @param doc a document
1389 * @param tags a pointer to a GList* holding the tags to add/update. This
1390 * list may be updated, removing updated elements.
1392 * The update is done in two passes:
1393 * 1) walking the current tree, update tags that still exist and remove the
1394 * obsolescent ones;
1395 * 2) walking the remaining (non updated) tags, adds them in the list.
1397 * For better performances, we use 2 hash tables:
1398 * - one containing all the tags for lookup in the first pass (actually stores a
1399 * reference in the tags list for removing it efficiently), avoiding list search
1400 * on each tag;
1401 * - the other holding "tag-name":row references for tags having children, used to
1402 * lookup for a parent in both passes, avoiding tree traversal.
1404 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1406 GtkTreeStore *store = doc->priv->tag_store;
1407 GtkTreeModel *model = GTK_TREE_MODEL(store);
1408 GHashTable *parents_table;
1409 GHashTable *tags_table;
1410 GtkTreeIter iter;
1411 gboolean cont;
1412 GList *item;
1414 /* Build hash tables holding tags and parents */
1415 /* parent table holds "tag-name":GtkTreeIter */
1416 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1417 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1418 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1419 foreach_list(item, *tags)
1421 TMTag *tag = item->data;
1422 const gchar *name;
1424 tags_table_insert(tags_table, tag, item);
1426 name = get_parent_name(tag, doc->file_type->id);
1427 if (name)
1428 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1431 /* First pass, update existing rows or delete them.
1432 * It is OK to delete them since we walk top down so we would remove
1433 * parents before checking for their children, thus never implicitly
1434 * deleting an updated child */
1435 cont = gtk_tree_model_get_iter_first(model, &iter);
1436 while (cont)
1438 TMTag *tag;
1440 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1441 if (! tag) /* most probably a toplevel, skip it */
1442 cont = next_iter(model, &iter, TRUE);
1443 else
1445 GList *found_item;
1447 found_item = tags_table_lookup(tags_table, tag);
1448 if (! found_item) /* tag doesn't exist, remove it */
1449 cont = tree_store_remove_row(store, &iter);
1450 else /* tag still exist, update it */
1452 const gchar *name;
1453 const gchar *parent_name;
1454 TMTag *found = found_item->data;
1456 parent_name = get_parent_name(found, doc->file_type->id);
1457 /* if parent is unknown, ignore it */
1458 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1459 parent_name = NULL;
1461 /* only update fields that (can) have changed (name that holds line
1462 * number, and the tag itself) */
1463 name = get_symbol_name(doc, found, parent_name != NULL);
1464 gtk_tree_store_set(store, &iter,
1465 SYMBOLS_COLUMN_NAME, name,
1466 SYMBOLS_COLUMN_TAG, found,
1467 -1);
1469 update_parents_table(parents_table, found, parent_name, &iter);
1471 /* remove the updated tag from the table and list */
1472 tags_table_remove(tags_table, found);
1473 *tags = g_list_delete_link(*tags, found_item);
1475 cont = next_iter(model, &iter, TRUE);
1478 tm_tag_unref(tag);
1482 /* Second pass, now we have a tree cleaned up from invalid rows,
1483 * we simply add new ones */
1484 foreach_list (item, *tags)
1486 TMTag *tag = item->data;
1487 GtkTreeIter *parent;
1489 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1490 if (G_UNLIKELY(! parent))
1491 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1492 else
1494 gboolean expand;
1495 const gchar *name;
1496 const gchar *parent_name;
1497 gchar *tooltip;
1498 GdkPixbuf *icon = get_child_icon(store, parent);
1500 parent_name = get_parent_name(tag, doc->file_type->id);
1501 if (parent_name)
1503 GList **candidates;
1504 GtkTreeIter *parent_search = NULL;
1506 /* walk parent candidates to find the better one.
1507 * if there are more than one, take the one that has the closest line number
1508 * after the tag we're searching the parent for */
1509 candidates = g_hash_table_lookup(parents_table, parent_name);
1510 if (candidates)
1512 GList *node;
1513 glong delta = G_MAXLONG;
1514 foreach_list(node, *candidates)
1516 TMTag *parent_tag;
1517 glong d;
1519 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1520 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1522 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1523 if (! parent_search || (d >= 0 && d < delta))
1525 delta = d;
1526 parent_search = node->data;
1528 tm_tag_unref(parent_tag);
1532 if (parent_search)
1533 parent = parent_search;
1534 else
1535 parent_name = NULL;
1538 /* only expand to the iter if the parent was empty, otherwise we let the
1539 * folding as it was before (already expanded, or closed by the user) */
1540 expand = ! gtk_tree_model_iter_has_child(model, parent);
1542 /* insert the new element */
1543 gtk_tree_store_append(store, &iter, parent);
1544 name = get_symbol_name(doc, tag, parent_name != NULL);
1545 tooltip = get_symbol_tooltip(doc, tag);
1546 gtk_tree_store_set(store, &iter,
1547 SYMBOLS_COLUMN_NAME, name,
1548 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1549 SYMBOLS_COLUMN_ICON, icon,
1550 SYMBOLS_COLUMN_TAG, tag,
1551 -1);
1552 g_free(tooltip);
1553 if (G_LIKELY(icon))
1554 g_object_unref(icon);
1556 update_parents_table(parents_table, tag, parent_name, &iter);
1558 if (expand)
1559 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1563 g_hash_table_destroy(parents_table);
1564 g_hash_table_destroy(tags_table);
1568 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1569 * is not stable, so the order is already lost. */
1570 static gint compare_top_level_names(const gchar *a, const gchar *b)
1572 guint i;
1573 const gchar *name;
1575 /* This should never happen as it would mean that two or more top
1576 * level items have the same name but it can happen by typos in the translations. */
1577 if (utils_str_equal(a, b))
1578 return 1;
1580 foreach_ptr_array(name, i, top_level_iter_names)
1582 if (utils_str_equal(name, a))
1583 return -1;
1584 if (utils_str_equal(name, b))
1585 return 1;
1587 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1588 return 0;
1592 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1593 GtkTreeIter *iter)
1595 /* if the tag has a parent tag, it should be at depth >= 2 */
1596 return NZV(tag->atts.entry.scope) &&
1597 gtk_tree_store_iter_depth(store, iter) == 1;
1601 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1602 gpointer user_data)
1604 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1605 TMTag *tag_a, *tag_b;
1606 gint cmp;
1608 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1609 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1611 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1612 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1613 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1614 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1616 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1617 compare_symbol_lines(tag_a, tag_b);
1619 else
1621 gchar *astr, *bstr;
1623 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1624 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1626 /* if a is toplevel, b must be also */
1627 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1629 cmp = compare_top_level_names(astr, bstr);
1631 else
1633 /* this is what g_strcmp0() does */
1634 if (! astr)
1635 cmp = -(astr != bstr);
1636 else if (! bstr)
1637 cmp = astr != bstr;
1638 else
1640 cmp = strcmp(astr, bstr);
1642 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1643 if (tag_a && tag_b)
1644 if (!sort_by_name ||
1645 (utils_str_equal(tag_a->name, tag_b->name) &&
1646 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1647 cmp = compare_symbol_lines(tag_a, tag_b);
1650 g_free(astr);
1651 g_free(bstr);
1653 tm_tag_unref(tag_a);
1654 tm_tag_unref(tag_b);
1656 return cmp;
1660 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1662 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1663 GINT_TO_POINTER(sort_by_name), NULL);
1665 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1669 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1671 GList *tags;
1673 g_return_val_if_fail(doc != NULL, FALSE);
1675 tags = get_tag_list(doc, tm_tag_max_t);
1676 if (tags == NULL)
1677 return FALSE;
1679 /* FIXME: Not sure why we detached the model here? */
1681 /* disable sorting during update because the code doesn't support correctly
1682 * models that are currently being built */
1683 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1685 /* add grandparent type iters */
1686 add_top_level_items(doc);
1688 update_tree_tags(doc, &tags);
1689 g_list_free(tags);
1691 hide_empty_rows(doc->priv->tag_store);
1693 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1694 sort_mode = doc->priv->symbol_list_sort_mode;
1696 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1697 doc->priv->symbol_list_sort_mode = sort_mode;
1699 return TRUE;
1703 /* Detects a global tags filetype from the *.lang.* language extension.
1704 * Returns NULL if there was no matching TM language. */
1705 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1707 gchar *tags_ext;
1708 gchar *shortname = utils_strdupa(utf8_filename);
1709 GeanyFiletype *ft = NULL;
1711 tags_ext = g_strrstr(shortname, ".tags");
1712 if (tags_ext)
1714 *tags_ext = '\0'; /* remove .tags extension */
1715 ft = filetypes_detect_from_extension(shortname);
1716 if (ft->id != GEANY_FILETYPES_NONE)
1717 return ft;
1719 return NULL;
1723 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1724 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1725 * the relevant path.
1726 * Example:
1727 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1728 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1730 /* -E pre-process, -dD output user macros, -p prof info (?) */
1731 const char pre_process[] = "gcc -E -dD -p -I.";
1733 if (argc > 2)
1735 /* Create global taglist */
1736 int status;
1737 char *command;
1738 const char *tags_file = argv[1];
1739 char *utf8_fname;
1740 GeanyFiletype *ft;
1742 utf8_fname = utils_get_utf8_from_locale(tags_file);
1743 ft = detect_global_tags_filetype(utf8_fname);
1744 g_free(utf8_fname);
1746 if (ft == NULL)
1748 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1749 return 1;
1751 /* load config in case of custom filetypes */
1752 filetypes_load_config(ft->id, FALSE);
1754 /* load ignore list for C/C++ parser */
1755 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1756 load_c_ignore_tags();
1758 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1759 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1760 else
1761 command = NULL; /* don't preprocess */
1763 geany_debug("Generating %s tags file.", ft->name);
1764 tm_get_workspace();
1765 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1766 argc - 2, tags_file, ft->lang);
1767 g_free(command);
1768 symbols_finalize(); /* free c_tags_ignore data */
1769 if (! status)
1771 g_printerr(_("Failed to create tags file, perhaps because no tags "
1772 "were found.\n"));
1773 return 1;
1776 else
1778 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1779 g_printerr(_("Example:\n"
1780 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1781 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1782 return 1;
1784 return 0;
1788 void symbols_show_load_tags_dialog(void)
1790 GtkWidget *dialog;
1791 GtkFileFilter *filter;
1793 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1794 GTK_FILE_CHOOSER_ACTION_OPEN,
1795 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1796 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1797 NULL);
1798 gtk_widget_set_name(dialog, "GeanyDialog");
1799 filter = gtk_file_filter_new();
1800 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1801 gtk_file_filter_add_pattern(filter, "*.*.tags");
1802 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1804 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1806 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1807 GSList *item;
1809 for (item = flist; item != NULL; item = g_slist_next(item))
1811 gchar *fname = item->data;
1812 gchar *utf8_fname;
1813 GeanyFiletype *ft;
1815 utf8_fname = utils_get_utf8_from_locale(fname);
1816 ft = detect_global_tags_filetype(utf8_fname);
1818 if (ft != NULL && symbols_load_global_tags(fname, ft))
1819 /* For translators: the first wildcard is the filetype, the second the filename */
1820 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1821 filetypes_get_display_name(ft), utf8_fname);
1822 else
1823 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1825 g_free(utf8_fname);
1826 g_free(fname);
1828 g_slist_free(flist);
1830 gtk_widget_destroy(dialog);
1834 static void detect_tag_files(const GSList *file_list)
1836 const GSList *node;
1838 for (node = file_list; node != NULL; node = g_slist_next(node))
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
1849 geany_debug("Unknown filetype for file '%s'.", fname);
1854 static void init_user_tags(void)
1856 GSList *file_list = NULL, *list = NULL;
1857 gchar *dir;
1859 dir = g_build_filename(app->configdir, "tags", NULL);
1860 /* create the user tags dir for next time if it doesn't exist */
1861 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1863 utils_mkdir(dir, FALSE);
1865 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1867 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1868 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1869 g_free(dir);
1871 file_list = g_slist_concat(file_list, list);
1872 detect_tag_files(file_list);
1873 /* don't need to delete list contents because they are stored in ft->priv->tag_files */
1874 g_slist_free(file_list);
1878 static void load_user_tags(filetype_id ft_id)
1880 static guchar *tags_loaded = NULL;
1881 static gboolean init_tags = FALSE;
1882 const GSList *node;
1883 GeanyFiletype *ft = filetypes[ft_id];
1885 g_return_if_fail(ft_id > 0);
1887 if (!tags_loaded)
1888 tags_loaded = g_new0(guchar, filetypes_array->len);
1889 if (tags_loaded[ft_id])
1890 return;
1891 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1893 if (!init_tags)
1895 init_user_tags();
1896 init_tags = TRUE;
1899 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1901 const gchar *fname = node->data;
1903 symbols_load_global_tags(fname, ft);
1908 static gboolean goto_tag(const gchar *name, gboolean definition)
1910 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1911 guint type;
1912 TMTag *tmtag = NULL;
1913 GeanyDocument *old_doc = document_get_current();
1915 /* goto tag definition: all except prototypes / forward declarations / externs */
1916 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1918 /* first look in the current document */
1919 if (old_doc != NULL && old_doc->tm_file)
1920 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1922 /* if not found, look in the workspace */
1923 if (tmtag == NULL)
1924 tmtag = find_workspace_tag(name, type);
1926 if (tmtag != NULL)
1928 GeanyDocument *new_doc = document_find_by_real_path(
1929 tmtag->atts.entry.file->work_object.file_name);
1931 if (new_doc)
1933 /* If we are already on the tag line, swap definition/declaration */
1934 if (new_doc == old_doc &&
1935 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1937 if (goto_tag(name, !definition))
1938 return TRUE;
1941 else
1943 /* not found in opened document, should open */
1944 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1947 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1948 return TRUE;
1950 return FALSE;
1954 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1956 if (goto_tag(name, definition))
1957 return TRUE;
1959 /* if we are here, there was no match and we are beeping ;-) */
1960 utils_beep();
1962 if (!definition)
1963 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1964 else
1965 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1966 return FALSE;
1970 /* This could perhaps be improved to check for #if, class etc. */
1971 static gint get_function_fold_number(GeanyDocument *doc)
1973 /* for Java the functions are always one fold level above the class scope */
1974 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1975 return SC_FOLDLEVELBASE + 1;
1976 else
1977 return SC_FOLDLEVELBASE;
1981 /* Should be used only with get_current_tag_cached.
1982 * tag_types caching might trigger recomputation too often but this isn't used differently often
1983 * enough to be an issue for now */
1984 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1986 static gint old_line = -2;
1987 static GeanyDocument *old_doc = NULL;
1988 static gint old_fold_num = -1;
1989 static guint old_tag_types = 0;
1990 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1991 gboolean ret;
1993 /* check if the cached line and file index have changed since last time: */
1994 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1995 ret = TRUE;
1996 else if (cur_line == old_line)
1997 ret = FALSE;
1998 else
2000 /* if the line has only changed by 1 */
2001 if (abs(cur_line - old_line) == 1)
2003 /* It's the same function if the fold number hasn't changed */
2004 ret = (fold_num != old_fold_num);
2006 else ret = TRUE;
2009 /* record current line and file index for next time */
2010 old_line = cur_line;
2011 old_doc = doc;
2012 old_fold_num = fold_num;
2013 old_tag_types = tag_types;
2014 return ret;
2018 /* Parse the function name up to 2 lines before tag_line.
2019 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2020 * type or argument names can be confused with the function name. */
2021 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2023 gint start, end, max_pos;
2024 gchar *cur_tag;
2025 gint fn_style;
2027 switch (sci_get_lexer(sci))
2029 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2030 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2031 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2033 start = sci_get_position_from_line(sci, tag_line - 2);
2034 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2035 while (sci_get_style_at(sci, start) != fn_style
2036 && start < max_pos) start++;
2038 end = start;
2039 while (sci_get_style_at(sci, end) == fn_style
2040 && end < max_pos) end++;
2042 if (start == end)
2043 return NULL;
2044 cur_tag = g_malloc(end - start + 1);
2045 sci_get_text_range(sci, start, end, cur_tag);
2046 return cur_tag;
2050 /* Parse the function name */
2051 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2053 gint start, end, first_pos, max_pos;
2054 gint tmp;
2055 gchar c;
2056 gchar *cur_tag;
2058 first_pos = end = sci_get_position_from_line(sci, tag_line);
2059 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2060 tmp = 0;
2061 /* goto the begin of function body */
2062 while (end < max_pos &&
2063 (tmp = sci_get_char_at(sci, end)) != '{' &&
2064 tmp != 0) end++;
2065 if (tmp == 0) end --;
2067 /* go back to the end of function identifier */
2068 while (end > 0 && end > first_pos - 500 &&
2069 (tmp = sci_get_char_at(sci, end)) != '(' &&
2070 tmp != 0) end--;
2071 end--;
2072 if (end < 0) end = 0;
2074 /* skip whitespaces between identifier and ( */
2075 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2077 start = end;
2078 c = 0;
2079 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2080 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2081 || tmp == SCE_C_GLOBALCLASS
2082 || (c = sci_get_char_at(sci, start)) == '~'
2083 || c == ':'))
2084 start--;
2085 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2087 if (start == end) return NULL;
2088 cur_tag = g_malloc(end - start + 2);
2089 sci_get_text_range(sci, start, end + 1, cur_tag);
2090 return cur_tag;
2094 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2096 gint line_count = sci_get_line_count(sci);
2098 for (; line < line_count; line++)
2100 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2101 return line;
2104 return -1;
2108 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2110 gint line;
2111 gint parent;
2113 line = sci_get_current_line(doc->editor->sci);
2114 parent = sci_get_fold_parent(doc->editor->sci, line);
2115 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2116 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2117 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2119 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2121 if (tag)
2123 gint tag_line = tag->atts.entry.line - 1;
2124 gint last_child = line + 1;
2126 /* if it may be a false positive because we're inside a fold level not inside anything
2127 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2128 * right after the tag we got from TM */
2129 if (abs(tag_line - parent) > 1)
2131 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2132 if (tag_fold >= 0)
2133 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2136 if (line <= last_child)
2138 if (tag->atts.entry.scope)
2139 *tagname = g_strconcat(tag->atts.entry.scope,
2140 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2141 else
2142 *tagname = g_strdup(tag->name);
2144 return tag_line;
2148 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2149 * to dirty and inaccurate hand-parsing */
2150 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2152 const gint fn_fold = get_function_fold_number(doc);
2153 gint tag_line = parent;
2154 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2156 /* find the top level fold point */
2157 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2159 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2160 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2163 if (tag_line >= 0)
2165 gchar *cur_tag;
2167 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2168 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2169 else
2170 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2172 if (cur_tag != NULL)
2174 *tagname = cur_tag;
2175 return tag_line;
2180 *tagname = g_strdup(_("unknown"));
2181 return -1;
2185 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2187 static gint tag_line = -1;
2188 static gchar *cur_tag = NULL;
2190 if (doc == NULL) /* reset current function */
2192 current_tag_changed(NULL, -1, -1, 0);
2193 g_free(cur_tag);
2194 cur_tag = g_strdup(_("unknown"));
2195 if (tagname != NULL)
2196 *tagname = cur_tag;
2197 tag_line = -1;
2199 else
2201 gint line = sci_get_current_line(doc->editor->sci);
2202 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2204 if (current_tag_changed(doc, line, fold_level, tag_types))
2206 g_free(cur_tag);
2207 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2209 *tagname = cur_tag;
2212 return tag_line;
2216 /* Sets *tagname to point at the current function or tag name.
2217 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2218 * call to this function.
2219 * Returns: line number of the current tag, or -1 if unknown. */
2220 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2222 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2226 /* same as symbols_get_current_function() but finds class, namespaces and more */
2227 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2229 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2230 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2232 /* Python parser reports imports as namespaces which confuses the scope detection */
2233 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2234 tag_types |= tm_tag_namespace_t;
2236 return get_current_tag_name_cached(doc, tagname, tag_types);
2240 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2242 gint sort_mode = GPOINTER_TO_INT(user_data);
2243 GeanyDocument *doc = document_get_current();
2245 if (ignore_callback)
2246 return;
2248 if (doc != NULL)
2249 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2253 static void on_symbol_tree_menu_show(GtkWidget *widget,
2254 gpointer user_data)
2256 GeanyDocument *doc = document_get_current();
2257 gboolean enable;
2259 enable = doc && doc->has_tags;
2260 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2261 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2262 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2263 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2264 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2265 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2267 if (! doc)
2268 return;
2270 ignore_callback = TRUE;
2272 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2273 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2274 else
2275 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2277 ignore_callback = FALSE;
2281 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2283 gboolean expand = GPOINTER_TO_INT(user_data);
2284 GeanyDocument *doc = document_get_current();
2286 if (! doc)
2287 return;
2289 g_return_if_fail(doc->priv->tag_tree);
2291 if (expand)
2292 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2293 else
2294 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2298 static void on_find_usage(GtkWidget *widget, gboolean in_session)
2300 GtkTreeIter iter;
2301 GtkTreeSelection *selection;
2302 GtkTreeModel *model;
2303 GeanyDocument *doc;
2304 TMTag *tag = NULL;
2306 doc = document_get_current();
2307 if (!doc)
2308 return;
2310 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2311 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2312 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2313 if (tag)
2315 search_find_usage(tag->name, tag->name, SCFIND_WHOLEWORD | SCFIND_MATCHCASE, in_session);
2316 tm_tag_unref(tag);
2321 static void create_taglist_popup_menu(void)
2323 GtkWidget *item, *menu;
2325 tv.popup_taglist = menu = gtk_menu_new();
2327 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2328 gtk_widget_show(item);
2329 gtk_container_add(GTK_CONTAINER(menu), item);
2330 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2332 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2333 gtk_widget_show(item);
2334 gtk_container_add(GTK_CONTAINER(menu), item);
2335 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2337 item = gtk_separator_menu_item_new();
2338 gtk_widget_show(item);
2339 gtk_container_add(GTK_CONTAINER(menu), item);
2341 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2342 _("Sort by _Name"));
2343 gtk_widget_show(item);
2344 gtk_container_add(GTK_CONTAINER(menu), item);
2345 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2346 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2348 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2349 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2350 gtk_widget_show(item);
2351 gtk_container_add(GTK_CONTAINER(menu), item);
2352 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2353 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2355 item = gtk_separator_menu_item_new();
2356 gtk_widget_show(item);
2357 gtk_container_add(GTK_CONTAINER(menu), item);
2359 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2360 gtk_widget_show(item);
2361 gtk_container_add(GTK_CONTAINER(menu), item);
2362 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), GINT_TO_POINTER(TRUE));
2364 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2365 gtk_widget_show(item);
2366 gtk_container_add(GTK_CONTAINER(menu), item);
2367 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), GINT_TO_POINTER(FALSE));
2369 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2371 sidebar_add_common_menu_items(GTK_MENU(menu));
2375 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2377 gchar *f = g_build_filename(app->configdir, "ignore.tags", NULL);
2379 g_return_if_fail(NZV(doc->real_path));
2381 if (utils_str_equal(doc->real_path, f))
2382 load_c_ignore_tags();
2384 g_free(f);
2388 void symbols_init(void)
2390 gchar *f;
2392 create_taglist_popup_menu();
2394 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2395 ui_add_config_file_menu_item(f, NULL, NULL);
2396 g_free(f);
2398 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2402 void symbols_finalize(void)
2404 g_strfreev(html_entities);
2405 g_strfreev(c_tags_ignore);