Start to make it easier to compile the core in isolation
[geany-mirror.git] / src / symbols.c
blob3d05f896d87c37d53531ab780b52cc5f2fb04453
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;
106 GtkWidget *find_in_files;
108 symbol_menu;
111 static void html_tags_loaded(void);
112 static void load_user_tags(filetype_id ft_id);
114 /* get the tags_ignore list, exported by tagmanager's options.c */
115 extern gchar **c_tags_ignore;
117 /* ignore certain tokens when parsing C-like syntax.
118 * Also works for reloading. */
119 static void load_c_ignore_tags(void)
121 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
122 gchar *content;
124 if (g_file_get_contents(path, &content, NULL, NULL))
126 /* historically we ignore the glib _DECLS for tag generation */
127 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
129 g_strfreev(c_tags_ignore);
130 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
131 g_free(content);
133 g_free(path);
137 void symbols_reload_config_files(void)
139 load_c_ignore_tags();
143 static gsize get_tag_count(void)
145 GPtrArray *tags = tm_get_workspace()->global_tags;
146 gsize count = tags ? tags->len : 0;
148 return count;
152 /* wrapper for tm_workspace_load_global_tags().
153 * note that the tag count only counts new global tags added - if a tag has the same name,
154 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
155 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
157 gboolean result;
158 gsize old_tag_count = get_tag_count();
160 result = tm_workspace_load_global_tags(tags_file, ft->lang);
161 if (result)
163 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
164 (guint) (get_tag_count() - old_tag_count));
166 return result;
170 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
171 * This provides autocompletion, calltips, etc. */
172 void symbols_global_tags_loaded(guint file_type_idx)
174 TagFileInfo *tfi;
175 gint tag_type;
177 /* load ignore list for C/C++ parser */
178 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
179 c_tags_ignore == NULL)
181 load_c_ignore_tags();
184 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
185 return;
187 /* load config in case of custom filetypes */
188 filetypes_load_config(file_type_idx, FALSE);
190 load_user_tags(file_type_idx);
192 switch (file_type_idx)
194 case GEANY_FILETYPES_PHP:
195 case GEANY_FILETYPES_HTML:
196 html_tags_loaded();
198 switch (file_type_idx)
200 case GEANY_FILETYPES_CPP:
201 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
202 /* no C++ tagfile yet */
203 return;
204 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
205 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
206 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
207 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
208 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
209 default:
210 return;
212 tfi = &tag_file_info[tag_type];
214 if (! tfi->tags_loaded)
216 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
218 symbols_load_global_tags(fname, filetypes[file_type_idx]);
219 tfi->tags_loaded = TRUE;
220 g_free(fname);
225 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
226 static void html_tags_loaded(void)
228 TagFileInfo *tfi;
230 if (cl_options.ignore_global_tags)
231 return;
233 tfi = &tag_file_info[GTF_HTML_ENTITIES];
234 if (! tfi->tags_loaded)
236 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
238 html_entities = utils_read_file_in_array(file);
239 tfi->tags_loaded = TRUE;
240 g_free(file);
245 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
247 guint j;
248 TMTag *tag;
249 GString *s = NULL;
250 GPtrArray *typedefs;
251 gint tag_lang;
253 g_return_val_if_fail(tags_array != NULL, NULL);
255 typedefs = tm_tags_extract(tags_array, tag_types);
257 if ((typedefs) && (typedefs->len > 0))
259 s = g_string_sized_new(typedefs->len * 10);
260 for (j = 0; j < typedefs->len; ++j)
262 tag = TM_TAG(typedefs->pdata[j]);
263 /* tag->atts.file.lang contains (for some reason) the line of the tag if
264 * tag->atts.entry.file is not NULL */
265 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
267 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
268 * e.g. PHP classes in C++ files
269 * lang = -2 disables the check */
270 if (tag->name && (tag_lang == lang || lang == -2))
272 if (j != 0)
273 g_string_append_c(s, ' ');
274 g_string_append(s, tag->name);
278 if (typedefs)
279 g_ptr_array_free(typedefs, TRUE);
280 return s;
284 /** Gets the context separator used by the tag manager for a particular file
285 * type.
286 * @param ft_id File type identifier.
287 * @return The context separator string.
289 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
290 * without a context separator.
292 * @since 0.19
294 const gchar *symbols_get_context_separator(gint ft_id)
296 switch (ft_id)
298 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
299 case GEANY_FILETYPES_CPP:
300 case GEANY_FILETYPES_GLSL: /* for structs */
301 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
302 case GEANY_FILETYPES_PHP:
303 case GEANY_FILETYPES_RUST:
304 return "::";
306 /* avoid confusion with other possible separators in group/section name */
307 case GEANY_FILETYPES_CONF:
308 case GEANY_FILETYPES_REST:
309 return ":::";
311 /* no context separator */
312 case GEANY_FILETYPES_ASCIIDOC:
313 return "\x03";
315 default:
316 return ".";
321 GString *symbols_get_macro_list(gint lang)
323 guint j, i;
324 GPtrArray *ftags;
325 GString *words;
326 gint tag_lang;
327 TMTag *tag;
329 if (app->tm_workspace->work_objects == NULL)
330 return NULL;
332 ftags = g_ptr_array_sized_new(50);
333 words = g_string_sized_new(200);
335 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
337 GPtrArray *tags;
339 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
340 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
341 if (NULL != tags)
343 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
345 tag = TM_TAG(tags->pdata[i]);
346 tag_lang = (tag->atts.entry.file) ?
347 tag->atts.entry.file->lang : tag->atts.file.lang;
349 if (tag_lang == lang)
350 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
352 g_ptr_array_free(tags, TRUE);
356 if (ftags->len == 0)
358 g_ptr_array_free(ftags, TRUE);
359 g_string_free(words, TRUE);
360 return NULL;
363 tm_tags_sort(ftags, NULL, FALSE);
364 for (j = 0; j < ftags->len; j++)
366 if (j > 0)
367 g_string_append_c(words, '\n');
368 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
370 g_ptr_array_free(ftags, TRUE);
371 return words;
375 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
376 static TMTag *
377 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
379 guint i;
380 g_return_val_if_fail(tags != NULL, NULL);
382 for (i = 0; i < tags->len; ++i)
384 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
385 return TM_TAG(tags->pdata[i]);
387 return NULL;
391 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
392 const gchar *tag_name, guint type)
394 GPtrArray *tags;
395 TMTag *tmtag;
397 if (G_LIKELY(workobj != NULL))
399 tags = tm_tags_extract(workobj->tags_array, type);
400 if (tags != NULL)
402 tmtag = symbols_find_tm_tag(tags, tag_name);
404 g_ptr_array_free(tags, TRUE);
406 if (tmtag != NULL)
407 return tmtag;
410 return NULL; /* not found */
414 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
416 guint j;
417 const GPtrArray *work_objects = NULL;
419 if (app->tm_workspace != NULL)
420 work_objects = app->tm_workspace->work_objects;
422 if (work_objects != NULL)
424 for (j = 0; j < work_objects->len; j++)
426 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
427 TMTag *tmtag;
429 tmtag = find_work_object_tag(workobj, tag_name, type);
430 if (tmtag != NULL)
431 return tmtag;
434 return NULL; /* not found */
438 const gchar **symbols_get_html_entities(void)
440 if (html_entities == NULL)
441 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
443 return (const gchar **) html_entities;
447 /* sort by name, then line */
448 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
450 gint ret;
452 if (tag_a == NULL || tag_b == NULL)
453 return 0;
455 if (tag_a->name == NULL)
456 return -(tag_a->name != tag_b->name);
458 if (tag_b->name == NULL)
459 return tag_a->name != tag_b->name;
461 ret = strcmp(tag_a->name, tag_b->name);
462 if (ret == 0)
464 return tag_a->atts.entry.line - tag_b->atts.entry.line;
466 return ret;
470 /* sort by line, then scope */
471 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
473 const TMTag *tag_a = TM_TAG(a);
474 const TMTag *tag_b = TM_TAG(b);
475 gint ret;
477 if (a == NULL || b == NULL)
478 return 0;
480 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
481 if (ret == 0)
483 if (tag_a->atts.entry.scope == NULL)
484 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
485 if (tag_b->atts.entry.scope == NULL)
486 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
487 else
488 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
490 return ret;
494 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
496 GList *tag_names = NULL;
497 TMTag *tag;
498 guint i;
500 g_return_val_if_fail(doc, NULL);
502 if (! doc->tm_file || ! doc->tm_file->tags_array)
503 return NULL;
505 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
507 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
508 if (G_UNLIKELY(tag == NULL))
509 return NULL;
511 if (tag->type & tag_types)
513 tag_names = g_list_prepend(tag_names, tag);
516 tag_names = g_list_sort(tag_names, compare_symbol_lines);
517 return tag_names;
521 /* amount of types in the symbol list (currently max. 8 are used) */
522 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
524 struct TreeviewSymbols
526 GtkTreeIter tag_function;
527 GtkTreeIter tag_class;
528 GtkTreeIter tag_macro;
529 GtkTreeIter tag_member;
530 GtkTreeIter tag_variable;
531 GtkTreeIter tag_externvar;
532 GtkTreeIter tag_namespace;
533 GtkTreeIter tag_struct;
534 GtkTreeIter tag_interface;
535 GtkTreeIter tag_type;
536 GtkTreeIter tag_other;
537 } tv_iters;
540 static void init_tag_iters(void)
542 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
543 * filetypes(e.g. config file to Python crashes Geany without this) */
544 tv_iters.tag_function.stamp = -1;
545 tv_iters.tag_class.stamp = -1;
546 tv_iters.tag_member.stamp = -1;
547 tv_iters.tag_macro.stamp = -1;
548 tv_iters.tag_variable.stamp = -1;
549 tv_iters.tag_externvar.stamp = -1;
550 tv_iters.tag_namespace.stamp = -1;
551 tv_iters.tag_struct.stamp = -1;
552 tv_iters.tag_interface.stamp = -1;
553 tv_iters.tag_type.stamp = -1;
554 tv_iters.tag_other.stamp = -1;
558 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
560 static GtkIconTheme *icon_theme = NULL;
561 static gint x = -1;
563 if (G_UNLIKELY(x < 0))
565 gint dummy;
566 icon_theme = gtk_icon_theme_get_default();
567 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
569 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
573 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
575 GtkTreeModel *model = GTK_TREE_MODEL(store);
577 if (!gtk_tree_model_get_iter_first(model, iter))
578 return FALSE;
581 gchar *candidate;
583 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
584 /* FIXME: what if 2 different items have the same name?
585 * this should never happen, but might be caused by a typo in a translation */
586 if (utils_str_equal(candidate, title))
588 g_free(candidate);
589 return TRUE;
591 else
592 g_free(candidate);
594 while (gtk_tree_model_iter_next(model, iter));
596 return FALSE;
600 /* Adds symbol list groups in (iter*, title) pairs.
601 * The list must be ended with NULL. */
602 static void G_GNUC_NULL_TERMINATED
603 tag_list_add_groups(GtkTreeStore *tree_store, ...)
605 va_list args;
606 GtkTreeIter *iter;
608 g_return_if_fail(top_level_iter_names);
610 va_start(args, tree_store);
611 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
613 gchar *title = va_arg(args, gchar*);
614 gchar *icon_name = va_arg(args, gchar *);
615 GdkPixbuf *icon = NULL;
617 if (icon_name)
619 icon = get_tag_icon(icon_name);
622 g_assert(title != NULL);
623 g_ptr_array_add(top_level_iter_names, title);
625 if (!find_toplevel_iter(tree_store, iter, title))
626 gtk_tree_store_append(tree_store, iter, NULL);
628 if (G_IS_OBJECT(icon))
630 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
631 g_object_unref(icon);
633 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
635 va_end(args);
639 static void add_top_level_items(GeanyDocument *doc)
641 filetype_id ft_id = doc->file_type->id;
642 GtkTreeStore *tag_store = doc->priv->tag_store;
644 if (top_level_iter_names == NULL)
645 top_level_iter_names = g_ptr_array_new();
646 else
647 g_ptr_array_set_size(top_level_iter_names, 0);
649 init_tag_iters();
651 switch (ft_id)
653 case GEANY_FILETYPES_DIFF:
655 tag_list_add_groups(tag_store,
656 &(tv_iters.tag_function), _("Files"), NULL, NULL);
657 break;
659 case GEANY_FILETYPES_DOCBOOK:
661 tag_list_add_groups(tag_store,
662 &(tv_iters.tag_function), _("Chapter"), NULL,
663 &(tv_iters.tag_class), _("Section"), NULL,
664 &(tv_iters.tag_member), _("Sect1"), NULL,
665 &(tv_iters.tag_macro), _("Sect2"), NULL,
666 &(tv_iters.tag_variable), _("Sect3"), NULL,
667 &(tv_iters.tag_struct), _("Appendix"), NULL,
668 &(tv_iters.tag_other), _("Other"), NULL,
669 NULL);
670 break;
672 case GEANY_FILETYPES_HASKELL:
673 tag_list_add_groups(tag_store,
674 &tv_iters.tag_namespace, _("Module"), NULL,
675 &tv_iters.tag_type, _("Types"), NULL,
676 &tv_iters.tag_macro, _("Type constructors"), NULL,
677 &tv_iters.tag_function, _("Functions"), "classviewer-method",
678 NULL);
679 break;
680 case GEANY_FILETYPES_COBOL:
681 tag_list_add_groups(tag_store,
682 &tv_iters.tag_class, _("Program"), "classviewer-class",
683 &tv_iters.tag_function, _("File"), "classviewer-method",
684 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
685 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
686 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
687 &tv_iters.tag_variable, _("Data"), "classviewer-var",
688 NULL);
689 break;
690 case GEANY_FILETYPES_CONF:
691 tag_list_add_groups(tag_store,
692 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
693 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
694 NULL);
695 break;
696 case GEANY_FILETYPES_NSIS:
697 tag_list_add_groups(tag_store,
698 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
699 &tv_iters.tag_function, _("Functions"), "classviewer-method",
700 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
701 NULL);
702 break;
703 case GEANY_FILETYPES_LATEX:
705 tag_list_add_groups(tag_store,
706 &(tv_iters.tag_function), _("Command"), NULL,
707 &(tv_iters.tag_class), _("Environment"), NULL,
708 &(tv_iters.tag_member), _("Section"), NULL,
709 &(tv_iters.tag_macro), _("Subsection"), NULL,
710 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
711 &(tv_iters.tag_struct), _("Label"), NULL,
712 &(tv_iters.tag_namespace), _("Chapter"), NULL,
713 &(tv_iters.tag_other), _("Other"), NULL,
714 NULL);
715 break;
717 case GEANY_FILETYPES_MATLAB:
719 tag_list_add_groups(tag_store,
720 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
721 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
722 NULL);
723 break;
725 case GEANY_FILETYPES_ABAQUS:
727 tag_list_add_groups(tag_store,
728 &(tv_iters.tag_class), _("Parts"), NULL,
729 &(tv_iters.tag_member), _("Assembly"), NULL,
730 &(tv_iters.tag_namespace), _("Steps"), NULL,
731 NULL);
732 break;
734 case GEANY_FILETYPES_R:
736 tag_list_add_groups(tag_store,
737 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
738 &(tv_iters.tag_other), _("Other"), NULL,
739 NULL);
740 break;
742 case GEANY_FILETYPES_RUST:
744 tag_list_add_groups(tag_store,
745 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
746 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
747 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
748 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
749 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
750 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
751 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
752 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
753 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
754 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
755 NULL);
756 break;
758 case GEANY_FILETYPES_PERL:
760 tag_list_add_groups(tag_store,
761 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
762 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
763 &(tv_iters.tag_macro), _("Labels"), NULL,
764 &(tv_iters.tag_type), _("Constants"), NULL,
765 &(tv_iters.tag_other), _("Other"), "classviewer-other",
766 NULL);
767 break;
769 case GEANY_FILETYPES_PHP:
771 tag_list_add_groups(tag_store,
772 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
773 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
774 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
775 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
776 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
777 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
778 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
779 NULL);
780 break;
782 case GEANY_FILETYPES_HTML:
784 tag_list_add_groups(tag_store,
785 &(tv_iters.tag_function), _("Functions"), NULL,
786 &(tv_iters.tag_member), _("Anchors"), NULL,
787 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
788 &(tv_iters.tag_class), _("H2 Headings"), NULL,
789 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
790 NULL);
791 break;
793 case GEANY_FILETYPES_CSS:
795 tag_list_add_groups(tag_store,
796 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
797 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
798 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
799 break;
801 case GEANY_FILETYPES_REST:
802 case GEANY_FILETYPES_TXT2TAGS:
803 case GEANY_FILETYPES_ABC:
805 tag_list_add_groups(tag_store,
806 &(tv_iters.tag_namespace), _("Chapter"), NULL,
807 &(tv_iters.tag_member), _("Section"), NULL,
808 &(tv_iters.tag_macro), _("Subsection"), NULL,
809 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
810 NULL);
811 break;
813 case GEANY_FILETYPES_ASCIIDOC:
815 tag_list_add_groups(tag_store,
816 &(tv_iters.tag_namespace), _("Document"), NULL,
817 &(tv_iters.tag_member), _("Section Level 1"), NULL,
818 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
819 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
820 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
821 NULL);
822 break;
824 case GEANY_FILETYPES_RUBY:
826 tag_list_add_groups(tag_store,
827 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
828 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
829 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
830 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
831 NULL);
832 break;
834 case GEANY_FILETYPES_TCL:
836 tag_list_add_groups(tag_store,
837 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
838 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
839 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
840 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
841 NULL);
842 break;
844 case GEANY_FILETYPES_PYTHON:
846 tag_list_add_groups(tag_store,
847 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
848 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
849 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
850 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
851 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
852 NULL);
853 break;
855 case GEANY_FILETYPES_VHDL:
857 tag_list_add_groups(tag_store,
858 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
859 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
860 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
861 &(tv_iters.tag_type), _("Types"), "classviewer-other",
862 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
863 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
864 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
865 &(tv_iters.tag_other), _("Other"), "classviewer-other",
866 NULL);
867 break;
869 case GEANY_FILETYPES_VERILOG:
871 tag_list_add_groups(tag_store,
872 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
873 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
874 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
875 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
876 &(tv_iters.tag_other), _("Other"), "classviewer-other",
877 NULL);
878 break;
880 case GEANY_FILETYPES_JAVA:
882 tag_list_add_groups(tag_store,
883 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
884 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
885 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
886 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
887 &(tv_iters.tag_member), _("Members"), "classviewer-member",
888 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
889 &(tv_iters.tag_other), _("Other"), "classviewer-other",
890 NULL);
891 break;
893 case GEANY_FILETYPES_AS:
895 tag_list_add_groups(tag_store,
896 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
897 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
898 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
899 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
900 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
901 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
902 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
903 &(tv_iters.tag_other), _("Other"), "classviewer-other",
904 NULL);
905 break;
907 case GEANY_FILETYPES_HAXE:
909 tag_list_add_groups(tag_store,
910 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
911 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
912 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
913 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
914 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
915 &(tv_iters.tag_other), _("Other"), "classviewer-other",
916 NULL);
917 break;
919 case GEANY_FILETYPES_BASIC:
921 tag_list_add_groups(tag_store,
922 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
923 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
924 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
925 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
926 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
927 &(tv_iters.tag_other), _("Other"), "classviewer-other",
928 NULL);
929 break;
931 case GEANY_FILETYPES_F77:
932 case GEANY_FILETYPES_FORTRAN:
934 tag_list_add_groups(tag_store,
935 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
936 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
937 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
938 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
939 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
940 &(tv_iters.tag_class), _("Types"), "classviewer-class",
941 &(tv_iters.tag_member), _("Components"), "classviewer-member",
942 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
943 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
944 &(tv_iters.tag_other), _("Other"), "classviewer-other",
945 NULL);
946 break;
948 case GEANY_FILETYPES_ASM:
950 tag_list_add_groups(tag_store,
951 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
952 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
953 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
954 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
955 NULL);
956 break;
958 case GEANY_FILETYPES_MAKE:
959 tag_list_add_groups(tag_store,
960 &tv_iters.tag_function, _("Targets"), "classviewer-method",
961 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
962 NULL);
963 break;
964 case GEANY_FILETYPES_SQL:
966 tag_list_add_groups(tag_store,
967 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
968 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
969 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
970 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
971 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
972 &(tv_iters.tag_member), _("Views"), "classviewer-var",
973 &(tv_iters.tag_other), _("Other"), "classviewer-other",
974 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
975 NULL);
976 break;
978 case GEANY_FILETYPES_D:
979 default:
981 if (ft_id == GEANY_FILETYPES_D)
982 tag_list_add_groups(tag_store,
983 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
984 else
985 tag_list_add_groups(tag_store,
986 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
988 tag_list_add_groups(tag_store,
989 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
990 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
991 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
992 &(tv_iters.tag_member), _("Members"), "classviewer-member",
993 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
994 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
995 NULL);
997 if (ft_id != GEANY_FILETYPES_D)
999 tag_list_add_groups(tag_store,
1000 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1002 tag_list_add_groups(tag_store,
1003 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1004 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1005 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1011 /* removes toplevel items that have no children */
1012 static void hide_empty_rows(GtkTreeStore *store)
1014 GtkTreeIter iter;
1015 gboolean cont = TRUE;
1017 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1018 return; /* stop when first iter is invalid, i.e. no elements */
1020 while (cont)
1022 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1023 cont = gtk_tree_store_remove(store, &iter);
1024 else
1025 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1030 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1032 gchar *utf8_name;
1033 const gchar *scope = tag->atts.entry.scope;
1034 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1035 gboolean doc_is_utf8 = FALSE;
1037 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1038 * for None at this point completely */
1039 if (utils_str_equal(doc->encoding, "UTF-8") ||
1040 utils_str_equal(doc->encoding, "None"))
1041 doc_is_utf8 = TRUE;
1042 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1043 * plugin might have called tm_source_file_update(), so check to be sure */
1044 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1046 if (! doc_is_utf8)
1047 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1048 -1, doc->encoding, TRUE);
1049 else
1050 utf8_name = tag->name;
1052 if (utf8_name == NULL)
1053 return NULL;
1055 if (! buffer)
1056 buffer = g_string_new(NULL);
1057 else
1058 g_string_truncate(buffer, 0);
1060 /* check first char of scope is a wordchar */
1061 if (!found_parent && scope &&
1062 strpbrk(scope, GEANY_WORDCHARS) == scope)
1064 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1066 g_string_append(buffer, scope);
1067 g_string_append(buffer, sep);
1069 g_string_append(buffer, utf8_name);
1071 if (! doc_is_utf8)
1072 g_free(utf8_name);
1074 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1076 return buffer->str;
1080 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1082 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1084 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1085 * for None at this point completely */
1086 if (utf8_name != NULL &&
1087 ! utils_str_equal(doc->encoding, "UTF-8") &&
1088 ! utils_str_equal(doc->encoding, "None"))
1090 SETPTR(utf8_name,
1091 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1094 if (utf8_name != NULL)
1095 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1097 return utf8_name;
1101 /* find the last word in "foo::bar::blah", e.g. "blah" */
1102 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1104 const gchar *scope = tag->atts.entry.scope;
1105 const gchar *separator = symbols_get_context_separator(ft_id);
1106 const gchar *str, *ptr;
1108 if (!scope)
1109 return NULL;
1111 str = scope;
1113 while (1)
1115 ptr = strstr(str, separator);
1116 if (ptr)
1118 str = ptr + strlen(separator);
1120 else
1121 break;
1124 return !EMPTY(str) ? str : NULL;
1128 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1130 GtkTreeIter *iter = NULL;
1132 switch (tag_type)
1134 case tm_tag_prototype_t:
1135 case tm_tag_method_t:
1136 case tm_tag_function_t:
1138 iter = &tv_iters.tag_function;
1139 break;
1141 case tm_tag_externvar_t:
1143 iter = &tv_iters.tag_externvar;
1144 break;
1146 case tm_tag_macro_t:
1147 case tm_tag_macro_with_arg_t:
1149 iter = &tv_iters.tag_macro;
1150 break;
1152 case tm_tag_class_t:
1154 iter = &tv_iters.tag_class;
1155 break;
1157 case tm_tag_member_t:
1158 case tm_tag_field_t:
1160 iter = &tv_iters.tag_member;
1161 break;
1163 case tm_tag_typedef_t:
1164 case tm_tag_enum_t:
1166 iter = &tv_iters.tag_type;
1167 break;
1169 case tm_tag_union_t:
1170 case tm_tag_struct_t:
1172 iter = &tv_iters.tag_struct;
1173 break;
1175 case tm_tag_interface_t:
1176 iter = &tv_iters.tag_interface;
1177 break;
1178 case tm_tag_variable_t:
1180 iter = &tv_iters.tag_variable;
1181 break;
1183 case tm_tag_namespace_t:
1184 case tm_tag_package_t:
1186 iter = &tv_iters.tag_namespace;
1187 break;
1189 default:
1191 iter = &tv_iters.tag_other;
1194 if (G_LIKELY(iter->stamp != -1))
1195 return iter;
1196 else
1197 return NULL;
1201 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1203 GdkPixbuf *icon = NULL;
1205 if (parent == &tv_iters.tag_other)
1207 return get_tag_icon("classviewer-var");
1209 /* copy parent icon */
1210 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1211 SYMBOLS_COLUMN_ICON, &icon, -1);
1212 return icon;
1216 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1218 const TMTag *t1 = v1;
1219 const TMTag *t2 = v2;
1221 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1222 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1223 /* include arglist in match to support e.g. C++ overloading */
1224 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1228 /* inspired from g_str_hash() */
1229 static guint tag_hash(gconstpointer v)
1231 const TMTag *tag = v;
1232 const gchar *p;
1233 guint32 h = 5381;
1235 h = (h << 5) + h + tag->type;
1236 for (p = tag->name; *p != '\0'; p++)
1237 h = (h << 5) + h + *p;
1238 if (tag->atts.entry.scope)
1240 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1241 h = (h << 5) + h + *p;
1243 /* for e.g. C++ overloading */
1244 if (tag->atts.entry.arglist)
1246 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1247 h = (h << 5) + h + *p;
1250 return h;
1254 /* like gtk_tree_view_expand_to_path() but with an iter */
1255 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1257 GtkTreeModel *model = gtk_tree_view_get_model(view);
1258 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1260 gtk_tree_view_expand_to_path(view, path);
1261 gtk_tree_path_free(path);
1265 /* like gtk_tree_store_remove() but finds the next iter at any level */
1266 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1268 GtkTreeIter parent;
1269 gboolean has_parent;
1270 gboolean cont;
1272 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1273 cont = gtk_tree_store_remove(store, iter);
1274 /* if there is no next at this level but there is a parent iter, continue from it */
1275 if (! cont && has_parent)
1277 *iter = parent;
1278 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1281 return cont;
1285 /* adds a new element in the parent table if it's key is known.
1286 * duplicates are kept */
1287 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1288 const GtkTreeIter *iter)
1290 GList **list;
1291 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1292 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1294 if (! list)
1296 list = g_slice_alloc(sizeof *list);
1297 *list = NULL;
1298 g_hash_table_insert(table, tag->name, list);
1300 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1305 static void free_iter_slice_list(gpointer data)
1307 GList **list = data;
1309 if (list)
1311 GList *node;
1312 foreach_list(node, *list)
1313 g_slice_free(GtkTreeIter, node->data);
1314 g_list_free(*list);
1315 g_slice_free1(sizeof *list, list);
1320 /* inserts a @data in @table on key @tag.
1321 * previous data is not overwritten if the key is duplicated, but rather the
1322 * two values are kept in a list
1324 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1325 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1327 GList *list = g_hash_table_lookup(table, tag);
1328 list = g_list_prepend(list, data);
1329 g_hash_table_insert(table, tag, list);
1333 /* looks up the entry in @table that better matches @tag.
1334 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1335 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1337 GList *data = NULL;
1338 GList *node = g_hash_table_lookup(table, tag);
1339 if (node)
1341 glong delta;
1342 data = node->data;
1344 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1346 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1347 for (node = node->next; node; node = node->next)
1349 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1351 if (d < delta)
1353 data = node->data;
1354 delta = d;
1358 #undef TAG_DELTA
1361 return data;
1365 /* removes the element at @tag from @table.
1366 * @tag must be the exact pointer used at insertion time */
1367 static void tags_table_remove(GHashTable *table, TMTag *tag)
1369 GList *list = g_hash_table_lookup(table, tag);
1370 if (list)
1372 GList *node;
1373 foreach_list(node, list)
1375 if (((GList *) node->data)->data == tag)
1376 break;
1378 list = g_list_delete_link(list, node);
1379 if (list)
1380 g_hash_table_insert(table, tag, list);
1381 else
1382 g_hash_table_remove(table, tag);
1387 static void tags_table_destroy(GHashTable *table)
1389 /* free any leftover elements. note that we can't register a value_free_func when
1390 * creating the hash table because we only want to free it when destroying the table,
1391 * not when inserting a duplicate (we handle this manually) */
1392 GHashTableIter iter;
1393 gpointer value;
1395 g_hash_table_iter_init(&iter, table);
1396 while (g_hash_table_iter_next(&iter, NULL, &value))
1397 g_list_free(value);
1398 g_hash_table_destroy(table);
1403 * Updates the tag tree for a document with the tags in *list.
1404 * @param doc a document
1405 * @param tags a pointer to a GList* holding the tags to add/update. This
1406 * list may be updated, removing updated elements.
1408 * The update is done in two passes:
1409 * 1) walking the current tree, update tags that still exist and remove the
1410 * obsolescent ones;
1411 * 2) walking the remaining (non updated) tags, adds them in the list.
1413 * For better performances, we use 2 hash tables:
1414 * - one containing all the tags for lookup in the first pass (actually stores a
1415 * reference in the tags list for removing it efficiently), avoiding list search
1416 * on each tag;
1417 * - the other holding "tag-name":row references for tags having children, used to
1418 * lookup for a parent in both passes, avoiding tree traversal.
1420 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1422 GtkTreeStore *store = doc->priv->tag_store;
1423 GtkTreeModel *model = GTK_TREE_MODEL(store);
1424 GHashTable *parents_table;
1425 GHashTable *tags_table;
1426 GtkTreeIter iter;
1427 gboolean cont;
1428 GList *item;
1430 /* Build hash tables holding tags and parents */
1431 /* parent table holds "tag-name":GtkTreeIter */
1432 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1433 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1434 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1435 foreach_list(item, *tags)
1437 TMTag *tag = item->data;
1438 const gchar *name;
1440 tags_table_insert(tags_table, tag, item);
1442 name = get_parent_name(tag, doc->file_type->id);
1443 if (name)
1444 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1447 /* First pass, update existing rows or delete them.
1448 * It is OK to delete them since we walk top down so we would remove
1449 * parents before checking for their children, thus never implicitly
1450 * deleting an updated child */
1451 cont = gtk_tree_model_get_iter_first(model, &iter);
1452 while (cont)
1454 TMTag *tag;
1456 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1457 if (! tag) /* most probably a toplevel, skip it */
1458 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1459 else
1461 GList *found_item;
1463 found_item = tags_table_lookup(tags_table, tag);
1464 if (! found_item) /* tag doesn't exist, remove it */
1465 cont = tree_store_remove_row(store, &iter);
1466 else /* tag still exist, update it */
1468 const gchar *name;
1469 const gchar *parent_name;
1470 TMTag *found = found_item->data;
1472 parent_name = get_parent_name(found, doc->file_type->id);
1473 /* if parent is unknown, ignore it */
1474 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1475 parent_name = NULL;
1477 /* only update fields that (can) have changed (name that holds line
1478 * number, and the tag itself) */
1479 name = get_symbol_name(doc, found, parent_name != NULL);
1480 gtk_tree_store_set(store, &iter,
1481 SYMBOLS_COLUMN_NAME, name,
1482 SYMBOLS_COLUMN_TAG, found,
1483 -1);
1485 update_parents_table(parents_table, found, parent_name, &iter);
1487 /* remove the updated tag from the table and list */
1488 tags_table_remove(tags_table, found);
1489 *tags = g_list_delete_link(*tags, found_item);
1491 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1494 tm_tag_unref(tag);
1498 /* Second pass, now we have a tree cleaned up from invalid rows,
1499 * we simply add new ones */
1500 foreach_list (item, *tags)
1502 TMTag *tag = item->data;
1503 GtkTreeIter *parent;
1505 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1506 if (G_UNLIKELY(! parent))
1507 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1508 else
1510 gboolean expand;
1511 const gchar *name;
1512 const gchar *parent_name;
1513 gchar *tooltip;
1514 GdkPixbuf *icon = get_child_icon(store, parent);
1516 parent_name = get_parent_name(tag, doc->file_type->id);
1517 if (parent_name)
1519 GList **candidates;
1520 GtkTreeIter *parent_search = NULL;
1522 /* walk parent candidates to find the better one.
1523 * if there are more than one, take the one that has the closest line number
1524 * after the tag we're searching the parent for */
1525 candidates = g_hash_table_lookup(parents_table, parent_name);
1526 if (candidates)
1528 GList *node;
1529 glong delta = G_MAXLONG;
1530 foreach_list(node, *candidates)
1532 TMTag *parent_tag;
1533 glong d;
1535 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1536 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1538 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1539 if (! parent_search || (d >= 0 && d < delta))
1541 delta = d;
1542 parent_search = node->data;
1544 tm_tag_unref(parent_tag);
1548 if (parent_search)
1549 parent = parent_search;
1550 else
1551 parent_name = NULL;
1554 /* only expand to the iter if the parent was empty, otherwise we let the
1555 * folding as it was before (already expanded, or closed by the user) */
1556 expand = ! gtk_tree_model_iter_has_child(model, parent);
1558 /* insert the new element */
1559 gtk_tree_store_append(store, &iter, parent);
1560 name = get_symbol_name(doc, tag, parent_name != NULL);
1561 tooltip = get_symbol_tooltip(doc, tag);
1562 gtk_tree_store_set(store, &iter,
1563 SYMBOLS_COLUMN_NAME, name,
1564 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1565 SYMBOLS_COLUMN_ICON, icon,
1566 SYMBOLS_COLUMN_TAG, tag,
1567 -1);
1568 g_free(tooltip);
1569 if (G_LIKELY(icon))
1570 g_object_unref(icon);
1572 update_parents_table(parents_table, tag, parent_name, &iter);
1574 if (expand)
1575 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1579 g_hash_table_destroy(parents_table);
1580 tags_table_destroy(tags_table);
1584 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1585 * is not stable, so the order is already lost. */
1586 static gint compare_top_level_names(const gchar *a, const gchar *b)
1588 guint i;
1589 const gchar *name;
1591 /* This should never happen as it would mean that two or more top
1592 * level items have the same name but it can happen by typos in the translations. */
1593 if (utils_str_equal(a, b))
1594 return 1;
1596 foreach_ptr_array(name, i, top_level_iter_names)
1598 if (utils_str_equal(name, a))
1599 return -1;
1600 if (utils_str_equal(name, b))
1601 return 1;
1603 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1604 return 0;
1608 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1609 GtkTreeIter *iter)
1611 /* if the tag has a parent tag, it should be at depth >= 2 */
1612 return !EMPTY(tag->atts.entry.scope) &&
1613 gtk_tree_store_iter_depth(store, iter) == 1;
1617 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1618 gpointer user_data)
1620 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1621 TMTag *tag_a, *tag_b;
1622 gint cmp;
1624 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1625 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1627 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1628 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1629 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1630 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1632 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1633 compare_symbol_lines(tag_a, tag_b);
1635 else
1637 gchar *astr, *bstr;
1639 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1640 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1642 /* if a is toplevel, b must be also */
1643 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1645 cmp = compare_top_level_names(astr, bstr);
1647 else
1649 /* this is what g_strcmp0() does */
1650 if (! astr)
1651 cmp = -(astr != bstr);
1652 else if (! bstr)
1653 cmp = astr != bstr;
1654 else
1656 cmp = strcmp(astr, bstr);
1658 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1659 if (tag_a && tag_b)
1660 if (!sort_by_name ||
1661 (utils_str_equal(tag_a->name, tag_b->name) &&
1662 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1663 cmp = compare_symbol_lines(tag_a, tag_b);
1666 g_free(astr);
1667 g_free(bstr);
1669 tm_tag_unref(tag_a);
1670 tm_tag_unref(tag_b);
1672 return cmp;
1676 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1678 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1679 GINT_TO_POINTER(sort_by_name), NULL);
1681 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1685 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1687 GList *tags;
1689 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1691 tags = get_tag_list(doc, tm_tag_max_t);
1692 if (tags == NULL)
1693 return FALSE;
1695 /* FIXME: Not sure why we detached the model here? */
1697 /* disable sorting during update because the code doesn't support correctly
1698 * models that are currently being built */
1699 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1701 /* add grandparent type iters */
1702 add_top_level_items(doc);
1704 update_tree_tags(doc, &tags);
1705 g_list_free(tags);
1707 hide_empty_rows(doc->priv->tag_store);
1709 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1710 sort_mode = doc->priv->symbol_list_sort_mode;
1712 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1713 doc->priv->symbol_list_sort_mode = sort_mode;
1715 return TRUE;
1719 /* Detects a global tags filetype from the *.lang.* language extension.
1720 * Returns NULL if there was no matching TM language. */
1721 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1723 gchar *tags_ext;
1724 gchar *shortname = utils_strdupa(utf8_filename);
1725 GeanyFiletype *ft = NULL;
1727 tags_ext = g_strrstr(shortname, ".tags");
1728 if (tags_ext)
1730 *tags_ext = '\0'; /* remove .tags extension */
1731 ft = filetypes_detect_from_extension(shortname);
1732 if (ft->id != GEANY_FILETYPES_NONE)
1733 return ft;
1735 return NULL;
1739 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1740 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1741 * the relevant path.
1742 * Example:
1743 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1744 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1746 /* -E pre-process, -dD output user macros, -p prof info (?) */
1747 const char pre_process[] = "gcc -E -dD -p -I.";
1749 if (argc > 2)
1751 /* Create global taglist */
1752 int status;
1753 char *command;
1754 const char *tags_file = argv[1];
1755 char *utf8_fname;
1756 GeanyFiletype *ft;
1758 utf8_fname = utils_get_utf8_from_locale(tags_file);
1759 ft = detect_global_tags_filetype(utf8_fname);
1760 g_free(utf8_fname);
1762 if (ft == NULL)
1764 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1765 return 1;
1767 /* load config in case of custom filetypes */
1768 filetypes_load_config(ft->id, FALSE);
1770 /* load ignore list for C/C++ parser */
1771 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1772 load_c_ignore_tags();
1774 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1776 const gchar *cflags = getenv("CFLAGS");
1777 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1779 else
1780 command = NULL; /* don't preprocess */
1782 geany_debug("Generating %s tags file.", ft->name);
1783 tm_get_workspace();
1784 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1785 argc - 2, tags_file, ft->lang);
1786 g_free(command);
1787 symbols_finalize(); /* free c_tags_ignore data */
1788 if (! status)
1790 g_printerr(_("Failed to create tags file, perhaps because no tags "
1791 "were found.\n"));
1792 return 1;
1795 else
1797 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1798 g_printerr(_("Example:\n"
1799 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1800 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1801 return 1;
1803 return 0;
1807 void symbols_show_load_tags_dialog(void)
1809 GtkWidget *dialog;
1810 GtkFileFilter *filter;
1812 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1813 GTK_FILE_CHOOSER_ACTION_OPEN,
1814 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1815 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1816 NULL);
1817 gtk_widget_set_name(dialog, "GeanyDialog");
1818 filter = gtk_file_filter_new();
1819 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1820 gtk_file_filter_add_pattern(filter, "*.*.tags");
1821 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1823 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1825 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1826 GSList *item;
1828 for (item = flist; item != NULL; item = g_slist_next(item))
1830 gchar *fname = item->data;
1831 gchar *utf8_fname;
1832 GeanyFiletype *ft;
1834 utf8_fname = utils_get_utf8_from_locale(fname);
1835 ft = detect_global_tags_filetype(utf8_fname);
1837 if (ft != NULL && symbols_load_global_tags(fname, ft))
1838 /* For translators: the first wildcard is the filetype, the second the filename */
1839 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1840 filetypes_get_display_name(ft), utf8_fname);
1841 else
1842 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1844 g_free(utf8_fname);
1845 g_free(fname);
1847 g_slist_free(flist);
1849 gtk_widget_destroy(dialog);
1853 static void init_user_tags(void)
1855 GSList *file_list = NULL, *list = NULL;
1856 const GSList *node;
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))
1862 utils_mkdir(dir, FALSE);
1863 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1865 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1866 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1867 g_free(dir);
1869 file_list = g_slist_concat(file_list, list);
1871 /* populate the filetype-specific tag files lists */
1872 for (node = file_list; node != NULL; node = node->next)
1874 gchar *fname = node->data;
1875 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1876 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1878 g_free(utf8_fname);
1880 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1881 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1882 else
1884 geany_debug("Unknown filetype for file '%s'.", fname);
1885 g_free(fname);
1889 /* don't need to delete list contents because they are now stored in
1890 * ft->priv->tag_files */
1891 g_slist_free(file_list);
1895 static void load_user_tags(filetype_id ft_id)
1897 static guchar *tags_loaded = NULL;
1898 static gboolean init_tags = FALSE;
1899 const GSList *node;
1900 GeanyFiletype *ft = filetypes[ft_id];
1902 g_return_if_fail(ft_id > 0);
1904 if (!tags_loaded)
1905 tags_loaded = g_new0(guchar, filetypes_array->len);
1906 if (tags_loaded[ft_id])
1907 return;
1908 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1910 if (!init_tags)
1912 init_user_tags();
1913 init_tags = TRUE;
1916 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1918 const gchar *fname = node->data;
1920 symbols_load_global_tags(fname, ft);
1925 static gboolean goto_tag(const gchar *name, gboolean definition)
1927 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1928 guint type;
1929 TMTag *tmtag = NULL;
1930 GeanyDocument *old_doc = document_get_current();
1932 /* goto tag definition: all except prototypes / forward declarations / externs */
1933 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1935 /* first look in the current document */
1936 if (old_doc != NULL && old_doc->tm_file)
1937 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1939 /* if not found, look in the workspace */
1940 if (tmtag == NULL)
1941 tmtag = find_workspace_tag(name, type);
1943 if (tmtag != NULL)
1945 GeanyDocument *new_doc = document_find_by_real_path(
1946 tmtag->atts.entry.file->work_object.file_name);
1948 if (new_doc)
1950 /* If we are already on the tag line, swap definition/declaration */
1951 if (new_doc == old_doc &&
1952 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1954 if (goto_tag(name, !definition))
1955 return TRUE;
1958 else
1960 /* not found in opened document, should open */
1961 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1964 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1965 return TRUE;
1967 return FALSE;
1971 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1973 if (goto_tag(name, definition))
1974 return TRUE;
1976 /* if we are here, there was no match and we are beeping ;-) */
1977 utils_beep();
1979 if (!definition)
1980 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1981 else
1982 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1983 return FALSE;
1987 /* This could perhaps be improved to check for #if, class etc. */
1988 static gint get_function_fold_number(GeanyDocument *doc)
1990 /* for Java the functions are always one fold level above the class scope */
1991 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1992 return SC_FOLDLEVELBASE + 1;
1993 else
1994 return SC_FOLDLEVELBASE;
1998 /* Should be used only with get_current_tag_cached.
1999 * tag_types caching might trigger recomputation too often but this isn't used differently often
2000 * enough to be an issue for now */
2001 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2003 static gint old_line = -2;
2004 static GeanyDocument *old_doc = NULL;
2005 static gint old_fold_num = -1;
2006 static guint old_tag_types = 0;
2007 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2008 gboolean ret;
2010 /* check if the cached line and file index have changed since last time: */
2011 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2012 ret = TRUE;
2013 else if (cur_line == old_line)
2014 ret = FALSE;
2015 else
2017 /* if the line has only changed by 1 */
2018 if (abs(cur_line - old_line) == 1)
2020 /* It's the same function if the fold number hasn't changed */
2021 ret = (fold_num != old_fold_num);
2023 else ret = TRUE;
2026 /* record current line and file index for next time */
2027 old_line = cur_line;
2028 old_doc = doc;
2029 old_fold_num = fold_num;
2030 old_tag_types = tag_types;
2031 return ret;
2035 /* Parse the function name up to 2 lines before tag_line.
2036 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2037 * type or argument names can be confused with the function name. */
2038 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2040 gint start, end, max_pos;
2041 gint fn_style;
2043 switch (sci_get_lexer(sci))
2045 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2046 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2047 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2049 start = sci_get_position_from_line(sci, tag_line - 2);
2050 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2051 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2052 start++;
2054 end = start;
2055 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2056 end++;
2058 if (start == end)
2059 return NULL;
2060 return sci_get_contents_range(sci, start, end);
2064 /* Parse the function name */
2065 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2067 gint start, end, first_pos, max_pos;
2068 gint tmp;
2069 gchar c;
2071 first_pos = end = sci_get_position_from_line(sci, tag_line);
2072 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2073 tmp = 0;
2074 /* goto the begin of function body */
2075 while (end < max_pos &&
2076 (tmp = sci_get_char_at(sci, end)) != '{' &&
2077 tmp != 0) end++;
2078 if (tmp == 0) end --;
2080 /* go back to the end of function identifier */
2081 while (end > 0 && end > first_pos - 500 &&
2082 (tmp = sci_get_char_at(sci, end)) != '(' &&
2083 tmp != 0) end--;
2084 end--;
2085 if (end < 0) end = 0;
2087 /* skip whitespaces between identifier and ( */
2088 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2090 start = end;
2091 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2092 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2093 || tmp == SCE_C_GLOBALCLASS
2094 || (c = sci_get_char_at(sci, start)) == '~'
2095 || c == ':'))
2096 start--;
2097 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2099 if (start == end) return NULL;
2100 return sci_get_contents_range(sci, start, end + 1);
2104 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2106 gint line_count = sci_get_line_count(sci);
2108 for (; line < line_count; line++)
2110 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2111 return line;
2114 return -1;
2118 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2120 gint line;
2121 gint parent;
2123 line = sci_get_current_line(doc->editor->sci);
2124 parent = sci_get_fold_parent(doc->editor->sci, line);
2125 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2126 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2127 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2129 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2131 if (tag)
2133 gint tag_line = tag->atts.entry.line - 1;
2134 gint last_child = line + 1;
2136 /* if it may be a false positive because we're inside a fold level not inside anything
2137 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2138 * right after the tag we got from TM */
2139 if (abs(tag_line - parent) > 1)
2141 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2142 if (tag_fold >= 0)
2143 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2146 if (line <= last_child)
2148 if (tag->atts.entry.scope)
2149 *tagname = g_strconcat(tag->atts.entry.scope,
2150 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2151 else
2152 *tagname = g_strdup(tag->name);
2154 return tag_line;
2158 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2159 * to dirty and inaccurate hand-parsing */
2160 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2162 const gint fn_fold = get_function_fold_number(doc);
2163 gint tag_line = parent;
2164 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2166 /* find the top level fold point */
2167 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2169 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2170 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2173 if (tag_line >= 0)
2175 gchar *cur_tag;
2177 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2178 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2179 else
2180 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2182 if (cur_tag != NULL)
2184 *tagname = cur_tag;
2185 return tag_line;
2190 *tagname = g_strdup(_("unknown"));
2191 return -1;
2195 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2197 static gint tag_line = -1;
2198 static gchar *cur_tag = NULL;
2200 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2202 if (doc == NULL) /* reset current function */
2204 current_tag_changed(NULL, -1, -1, 0);
2205 g_free(cur_tag);
2206 cur_tag = g_strdup(_("unknown"));
2207 if (tagname != NULL)
2208 *tagname = cur_tag;
2209 tag_line = -1;
2211 else
2213 gint line = sci_get_current_line(doc->editor->sci);
2214 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2216 if (current_tag_changed(doc, line, fold_level, tag_types))
2218 g_free(cur_tag);
2219 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2221 *tagname = cur_tag;
2224 return tag_line;
2228 /* Sets *tagname to point at the current function or tag name.
2229 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2230 * call to this function.
2231 * Returns: line number of the current tag, or -1 if unknown. */
2232 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2234 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2238 /* same as symbols_get_current_function() but finds class, namespaces and more */
2239 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2241 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2242 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2244 /* Python parser reports imports as namespaces which confuses the scope detection */
2245 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2246 tag_types |= tm_tag_namespace_t;
2248 return get_current_tag_name_cached(doc, tagname, tag_types);
2252 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2254 gint sort_mode = GPOINTER_TO_INT(user_data);
2255 GeanyDocument *doc = document_get_current();
2257 if (ignore_callback)
2258 return;
2260 if (doc != NULL)
2261 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2265 static void on_symbol_tree_menu_show(GtkWidget *widget,
2266 gpointer user_data)
2268 GeanyDocument *doc = document_get_current();
2269 gboolean enable;
2271 enable = doc && doc->has_tags;
2272 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2273 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2274 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2275 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2276 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2277 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2279 if (! doc)
2280 return;
2282 ignore_callback = TRUE;
2284 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2286 else
2287 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2289 ignore_callback = FALSE;
2293 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2295 gboolean expand = GPOINTER_TO_INT(user_data);
2296 GeanyDocument *doc = document_get_current();
2298 if (! doc)
2299 return;
2301 g_return_if_fail(doc->priv->tag_tree);
2303 if (expand)
2304 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2305 else
2306 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2310 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2312 GtkTreeIter iter;
2313 GtkTreeSelection *selection;
2314 GtkTreeModel *model;
2315 GeanyDocument *doc;
2316 TMTag *tag = NULL;
2318 doc = document_get_current();
2319 if (!doc)
2320 return;
2322 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2323 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2324 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2325 if (tag)
2327 if (widget == symbol_menu.find_in_files)
2328 search_show_find_in_files_dialog_full(tag->name, NULL);
2329 else
2330 search_find_usage(tag->name, tag->name, SCFIND_WHOLEWORD | SCFIND_MATCHCASE,
2331 widget == symbol_menu.find_usage);
2333 tm_tag_unref(tag);
2338 static void create_taglist_popup_menu(void)
2340 GtkWidget *item, *menu;
2342 tv.popup_taglist = menu = gtk_menu_new();
2344 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2345 gtk_widget_show(item);
2346 gtk_container_add(GTK_CONTAINER(menu), item);
2347 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2349 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2350 gtk_widget_show(item);
2351 gtk_container_add(GTK_CONTAINER(menu), item);
2352 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2354 item = gtk_separator_menu_item_new();
2355 gtk_widget_show(item);
2356 gtk_container_add(GTK_CONTAINER(menu), item);
2358 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2359 _("Sort by _Name"));
2360 gtk_widget_show(item);
2361 gtk_container_add(GTK_CONTAINER(menu), item);
2362 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2363 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2365 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2366 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2367 gtk_widget_show(item);
2368 gtk_container_add(GTK_CONTAINER(menu), item);
2369 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2370 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2372 item = gtk_separator_menu_item_new();
2373 gtk_widget_show(item);
2374 gtk_container_add(GTK_CONTAINER(menu), item);
2376 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2377 gtk_widget_show(item);
2378 gtk_container_add(GTK_CONTAINER(menu), item);
2379 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2381 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2382 gtk_widget_show(item);
2383 gtk_container_add(GTK_CONTAINER(menu), item);
2384 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2386 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2387 gtk_widget_show(item);
2388 gtk_container_add(GTK_CONTAINER(menu), item);
2389 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2391 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2393 sidebar_add_common_menu_items(GTK_MENU(menu));
2397 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2399 gchar *f;
2401 g_return_if_fail(!EMPTY(doc->real_path));
2403 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2404 if (utils_str_equal(doc->real_path, f))
2405 load_c_ignore_tags();
2407 g_free(f);
2411 void symbols_init(void)
2413 gchar *f;
2415 create_taglist_popup_menu();
2417 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2418 ui_add_config_file_menu_item(f, NULL, NULL);
2419 g_free(f);
2421 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2425 void symbols_finalize(void)
2427 g_strfreev(html_entities);
2428 g_strfreev(c_tags_ignore);