Merge pull request #343 from ntrel/more-project-prefs
[geany-mirror.git] / src / symbols.c
blobd2055e87d0d072a19f6c0e000d5000dd3454aa97
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include "symbols.h"
41 #include "app.h"
42 #include "callbacks.h" /* FIXME: for ignore_callback */
43 #include "documentprivate.h"
44 #include "editor.h"
45 #include "encodings.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "sciwrappers.h"
51 #include "sidebar.h"
52 #include "support.h"
53 #include "tm_parser.h"
54 #include "tm_tag.h"
55 #include "ui_utils.h"
56 #include "utils.h"
58 #include "SciLexer.h"
60 #include "gtkcompat.h"
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdlib.h>
67 const guint TM_GLOBAL_TYPE_MASK =
68 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
69 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
72 static gchar **html_entities = NULL;
74 typedef struct
76 gboolean tags_loaded;
77 const gchar *tag_file;
78 } TagFileInfo;
80 /* Check before adding any more tags files, usually they should be downloaded separately. */
81 enum /* Geany tag files */
83 GTF_C,
84 GTF_PASCAL,
85 GTF_PHP,
86 GTF_HTML_ENTITIES,
87 GTF_LATEX,
88 GTF_PYTHON,
89 GTF_MAX
92 static TagFileInfo tag_file_info[GTF_MAX] =
94 {FALSE, "c99.tags"},
95 {FALSE, "pascal.tags"},
96 {FALSE, "php.tags"},
97 {FALSE, "html_entities.tags"},
98 {FALSE, "latex.tags"},
99 {FALSE, "python.tags"}
102 static GPtrArray *top_level_iter_names = NULL;
104 static struct
106 GtkWidget *expand_all;
107 GtkWidget *collapse_all;
108 GtkWidget *sort_by_name;
109 GtkWidget *sort_by_appearance;
110 GtkWidget *find_usage;
111 GtkWidget *find_doc_usage;
112 GtkWidget *find_in_files;
114 symbol_menu;
117 static void html_tags_loaded(void);
118 static void load_user_tags(filetype_id ft_id);
120 /* get the tags_ignore list, exported by tagmanager's options.c */
121 extern gchar **c_tags_ignore;
123 /* ignore certain tokens when parsing C-like syntax.
124 * Also works for reloading. */
125 static void load_c_ignore_tags(void)
127 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
128 gchar *content;
130 if (g_file_get_contents(path, &content, NULL, NULL))
132 /* historically we ignore the glib _DECLS for tag generation */
133 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
135 g_strfreev(c_tags_ignore);
136 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
137 g_free(content);
139 g_free(path);
143 void symbols_reload_config_files(void)
145 load_c_ignore_tags();
149 static gsize get_tag_count(void)
151 GPtrArray *tags = tm_get_workspace()->global_tags;
152 gsize count = tags ? tags->len : 0;
154 return count;
158 /* wrapper for tm_workspace_load_global_tags().
159 * note that the tag count only counts new global tags added - if a tag has the same name,
160 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
161 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
163 gboolean result;
164 gsize old_tag_count = get_tag_count();
166 result = tm_workspace_load_global_tags(tags_file, ft->lang);
167 if (result)
169 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
170 (guint) (get_tag_count() - old_tag_count));
172 return result;
176 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
177 * This provides autocompletion, calltips, etc. */
178 void symbols_global_tags_loaded(guint file_type_idx)
180 TagFileInfo *tfi;
181 gint tag_type;
183 /* load ignore list for C/C++ parser */
184 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
185 c_tags_ignore == NULL)
187 load_c_ignore_tags();
190 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
191 return;
193 /* load config in case of custom filetypes */
194 filetypes_load_config(file_type_idx, FALSE);
196 load_user_tags(file_type_idx);
198 switch (file_type_idx)
200 case GEANY_FILETYPES_PHP:
201 case GEANY_FILETYPES_HTML:
202 html_tags_loaded();
204 switch (file_type_idx)
206 case GEANY_FILETYPES_CPP:
207 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
208 /* no C++ tagfile yet */
209 return;
210 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
211 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
212 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
213 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
214 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
215 default:
216 return;
218 tfi = &tag_file_info[tag_type];
220 if (! tfi->tags_loaded)
222 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
224 symbols_load_global_tags(fname, filetypes[file_type_idx]);
225 tfi->tags_loaded = TRUE;
226 g_free(fname);
231 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
232 static void html_tags_loaded(void)
234 TagFileInfo *tfi;
236 if (cl_options.ignore_global_tags)
237 return;
239 tfi = &tag_file_info[GTF_HTML_ENTITIES];
240 if (! tfi->tags_loaded)
242 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
244 html_entities = utils_read_file_in_array(file);
245 tfi->tags_loaded = TRUE;
246 g_free(file);
251 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
253 guint j;
254 TMTag *tag;
255 GString *s = NULL;
256 GPtrArray *typedefs;
257 gint tag_lang;
259 g_return_val_if_fail(tags_array != NULL, NULL);
261 typedefs = tm_tags_extract(tags_array, tag_types);
263 if ((typedefs) && (typedefs->len > 0))
265 s = g_string_sized_new(typedefs->len * 10);
266 for (j = 0; j < typedefs->len; ++j)
268 tag = TM_TAG(typedefs->pdata[j]);
269 /* tag->atts.file.lang contains (for some reason) the line of the tag if
270 * tag->atts.entry.file is not NULL */
271 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
273 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
274 * e.g. PHP classes in C++ files
275 * lang = -2 disables the check */
276 if (tag->name && (tag_lang == lang || lang == -2 ||
277 (lang == TM_PARSER_CPP && tag_lang == TM_PARSER_C)))
279 if (j != 0)
280 g_string_append_c(s, ' ');
281 g_string_append(s, tag->name);
285 if (typedefs)
286 g_ptr_array_free(typedefs, TRUE);
287 return s;
291 /** Gets the context separator used by the tag manager for a particular file
292 * type.
293 * @param ft_id File type identifier.
294 * @return The context separator string.
296 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
297 * without a context separator.
299 * @since 0.19
301 const gchar *symbols_get_context_separator(gint ft_id)
303 switch (ft_id)
305 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
306 case GEANY_FILETYPES_CPP:
307 case GEANY_FILETYPES_GLSL: /* for structs */
308 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
309 case GEANY_FILETYPES_PHP:
310 case GEANY_FILETYPES_RUST:
311 return "::";
313 /* avoid confusion with other possible separators in group/section name */
314 case GEANY_FILETYPES_CONF:
315 case GEANY_FILETYPES_REST:
316 return ":::";
318 /* no context separator */
319 case GEANY_FILETYPES_ASCIIDOC:
320 return "\x03";
322 default:
323 return ".";
328 GString *symbols_get_macro_list(gint lang)
330 guint j, i;
331 GPtrArray *ftags;
332 GString *words;
333 gint tag_lang;
334 TMTag *tag;
336 if (app->tm_workspace->work_objects == NULL)
337 return NULL;
339 ftags = g_ptr_array_sized_new(50);
340 words = g_string_sized_new(200);
342 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
344 GPtrArray *tags;
346 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
347 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
348 if (NULL != tags)
350 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
352 tag = TM_TAG(tags->pdata[i]);
353 tag_lang = (tag->atts.entry.file) ?
354 tag->atts.entry.file->lang : tag->atts.file.lang;
356 if (tag_lang == lang)
357 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
359 g_ptr_array_free(tags, TRUE);
363 if (ftags->len == 0)
365 g_ptr_array_free(ftags, TRUE);
366 g_string_free(words, TRUE);
367 return NULL;
370 tm_tags_sort(ftags, NULL, FALSE);
371 for (j = 0; j < ftags->len; j++)
373 if (j > 0)
374 g_string_append_c(words, '\n');
375 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
377 g_ptr_array_free(ftags, TRUE);
378 return words;
382 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
383 static TMTag *
384 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
386 guint i;
387 g_return_val_if_fail(tags != NULL, NULL);
389 for (i = 0; i < tags->len; ++i)
391 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
392 return TM_TAG(tags->pdata[i]);
394 return NULL;
398 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
399 const gchar *tag_name, guint type)
401 GPtrArray *tags;
402 TMTag *tmtag;
404 if (G_LIKELY(workobj != NULL))
406 tags = tm_tags_extract(workobj->tags_array, type);
407 if (tags != NULL)
409 tmtag = symbols_find_tm_tag(tags, tag_name);
411 g_ptr_array_free(tags, TRUE);
413 if (tmtag != NULL)
414 return tmtag;
417 return NULL; /* not found */
421 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
423 guint j;
424 const GPtrArray *work_objects = NULL;
426 if (app->tm_workspace != NULL)
427 work_objects = app->tm_workspace->work_objects;
429 if (work_objects != NULL)
431 for (j = 0; j < work_objects->len; j++)
433 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
434 TMTag *tmtag;
436 tmtag = find_work_object_tag(workobj, tag_name, type);
437 if (tmtag != NULL)
438 return tmtag;
441 return NULL; /* not found */
445 const gchar **symbols_get_html_entities(void)
447 if (html_entities == NULL)
448 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
450 return (const gchar **) html_entities;
454 /* sort by name, then line */
455 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
457 gint ret;
459 if (tag_a == NULL || tag_b == NULL)
460 return 0;
462 if (tag_a->name == NULL)
463 return -(tag_a->name != tag_b->name);
465 if (tag_b->name == NULL)
466 return tag_a->name != tag_b->name;
468 ret = strcmp(tag_a->name, tag_b->name);
469 if (ret == 0)
471 return tag_a->atts.entry.line - tag_b->atts.entry.line;
473 return ret;
477 /* sort by line, then scope */
478 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
480 const TMTag *tag_a = TM_TAG(a);
481 const TMTag *tag_b = TM_TAG(b);
482 gint ret;
484 if (a == NULL || b == NULL)
485 return 0;
487 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
488 if (ret == 0)
490 if (tag_a->atts.entry.scope == NULL)
491 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
492 if (tag_b->atts.entry.scope == NULL)
493 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
494 else
495 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
497 return ret;
501 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
503 GList *tag_names = NULL;
504 TMTag *tag;
505 guint i;
507 g_return_val_if_fail(doc, NULL);
509 if (! doc->tm_file || ! doc->tm_file->tags_array)
510 return NULL;
512 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
514 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
515 if (G_UNLIKELY(tag == NULL))
516 return NULL;
518 if (tag->type & tag_types)
520 tag_names = g_list_prepend(tag_names, tag);
523 tag_names = g_list_sort(tag_names, compare_symbol_lines);
524 return tag_names;
528 /* amount of types in the symbol list (currently max. 8 are used) */
529 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
531 struct TreeviewSymbols
533 GtkTreeIter tag_function;
534 GtkTreeIter tag_class;
535 GtkTreeIter tag_macro;
536 GtkTreeIter tag_member;
537 GtkTreeIter tag_variable;
538 GtkTreeIter tag_externvar;
539 GtkTreeIter tag_namespace;
540 GtkTreeIter tag_struct;
541 GtkTreeIter tag_interface;
542 GtkTreeIter tag_type;
543 GtkTreeIter tag_other;
544 } tv_iters;
547 static void init_tag_iters(void)
549 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
550 * filetypes(e.g. config file to Python crashes Geany without this) */
551 tv_iters.tag_function.stamp = -1;
552 tv_iters.tag_class.stamp = -1;
553 tv_iters.tag_member.stamp = -1;
554 tv_iters.tag_macro.stamp = -1;
555 tv_iters.tag_variable.stamp = -1;
556 tv_iters.tag_externvar.stamp = -1;
557 tv_iters.tag_namespace.stamp = -1;
558 tv_iters.tag_struct.stamp = -1;
559 tv_iters.tag_interface.stamp = -1;
560 tv_iters.tag_type.stamp = -1;
561 tv_iters.tag_other.stamp = -1;
565 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
567 static GtkIconTheme *icon_theme = NULL;
568 static gint x = -1;
570 if (G_UNLIKELY(x < 0))
572 gint dummy;
573 icon_theme = gtk_icon_theme_get_default();
574 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
576 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
580 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
582 GtkTreeModel *model = GTK_TREE_MODEL(store);
584 if (!gtk_tree_model_get_iter_first(model, iter))
585 return FALSE;
588 gchar *candidate;
590 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
591 /* FIXME: what if 2 different items have the same name?
592 * this should never happen, but might be caused by a typo in a translation */
593 if (utils_str_equal(candidate, title))
595 g_free(candidate);
596 return TRUE;
598 else
599 g_free(candidate);
601 while (gtk_tree_model_iter_next(model, iter));
603 return FALSE;
607 /* Adds symbol list groups in (iter*, title) pairs.
608 * The list must be ended with NULL. */
609 static void G_GNUC_NULL_TERMINATED
610 tag_list_add_groups(GtkTreeStore *tree_store, ...)
612 va_list args;
613 GtkTreeIter *iter;
615 g_return_if_fail(top_level_iter_names);
617 va_start(args, tree_store);
618 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
620 gchar *title = va_arg(args, gchar*);
621 gchar *icon_name = va_arg(args, gchar *);
622 GdkPixbuf *icon = NULL;
624 if (icon_name)
626 icon = get_tag_icon(icon_name);
629 g_assert(title != NULL);
630 g_ptr_array_add(top_level_iter_names, title);
632 if (!find_toplevel_iter(tree_store, iter, title))
633 gtk_tree_store_append(tree_store, iter, NULL);
635 if (G_IS_OBJECT(icon))
637 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
638 g_object_unref(icon);
640 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
642 va_end(args);
646 static void add_top_level_items(GeanyDocument *doc)
648 filetype_id ft_id = doc->file_type->id;
649 GtkTreeStore *tag_store = doc->priv->tag_store;
651 if (top_level_iter_names == NULL)
652 top_level_iter_names = g_ptr_array_new();
653 else
654 g_ptr_array_set_size(top_level_iter_names, 0);
656 init_tag_iters();
658 switch (ft_id)
660 case GEANY_FILETYPES_DIFF:
662 tag_list_add_groups(tag_store,
663 &(tv_iters.tag_function), _("Files"), NULL, NULL);
664 break;
666 case GEANY_FILETYPES_DOCBOOK:
668 tag_list_add_groups(tag_store,
669 &(tv_iters.tag_function), _("Chapter"), NULL,
670 &(tv_iters.tag_class), _("Section"), NULL,
671 &(tv_iters.tag_member), _("Sect1"), NULL,
672 &(tv_iters.tag_macro), _("Sect2"), NULL,
673 &(tv_iters.tag_variable), _("Sect3"), NULL,
674 &(tv_iters.tag_struct), _("Appendix"), NULL,
675 &(tv_iters.tag_other), _("Other"), NULL,
676 NULL);
677 break;
679 case GEANY_FILETYPES_HASKELL:
680 tag_list_add_groups(tag_store,
681 &tv_iters.tag_namespace, _("Module"), NULL,
682 &tv_iters.tag_type, _("Types"), NULL,
683 &tv_iters.tag_macro, _("Type constructors"), NULL,
684 &tv_iters.tag_function, _("Functions"), "classviewer-method",
685 NULL);
686 break;
687 case GEANY_FILETYPES_COBOL:
688 tag_list_add_groups(tag_store,
689 &tv_iters.tag_class, _("Program"), "classviewer-class",
690 &tv_iters.tag_function, _("File"), "classviewer-method",
691 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
692 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
693 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
694 &tv_iters.tag_variable, _("Data"), "classviewer-var",
695 NULL);
696 break;
697 case GEANY_FILETYPES_CONF:
698 tag_list_add_groups(tag_store,
699 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
700 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
701 NULL);
702 break;
703 case GEANY_FILETYPES_NSIS:
704 tag_list_add_groups(tag_store,
705 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
706 &tv_iters.tag_function, _("Functions"), "classviewer-method",
707 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
708 NULL);
709 break;
710 case GEANY_FILETYPES_LATEX:
712 tag_list_add_groups(tag_store,
713 &(tv_iters.tag_function), _("Command"), NULL,
714 &(tv_iters.tag_class), _("Environment"), NULL,
715 &(tv_iters.tag_member), _("Section"), NULL,
716 &(tv_iters.tag_macro), _("Subsection"), NULL,
717 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
718 &(tv_iters.tag_struct), _("Label"), NULL,
719 &(tv_iters.tag_namespace), _("Chapter"), NULL,
720 &(tv_iters.tag_other), _("Other"), NULL,
721 NULL);
722 break;
724 case GEANY_FILETYPES_MATLAB:
726 tag_list_add_groups(tag_store,
727 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
728 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
729 NULL);
730 break;
732 case GEANY_FILETYPES_ABAQUS:
734 tag_list_add_groups(tag_store,
735 &(tv_iters.tag_class), _("Parts"), NULL,
736 &(tv_iters.tag_member), _("Assembly"), NULL,
737 &(tv_iters.tag_namespace), _("Steps"), NULL,
738 NULL);
739 break;
741 case GEANY_FILETYPES_R:
743 tag_list_add_groups(tag_store,
744 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
745 &(tv_iters.tag_other), _("Other"), NULL,
746 NULL);
747 break;
749 case GEANY_FILETYPES_RUST:
751 tag_list_add_groups(tag_store,
752 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
753 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
754 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
755 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
756 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
757 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
758 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
759 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
760 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
761 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
762 NULL);
763 break;
765 case GEANY_FILETYPES_PERL:
767 tag_list_add_groups(tag_store,
768 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
769 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
770 &(tv_iters.tag_macro), _("Labels"), NULL,
771 &(tv_iters.tag_type), _("Constants"), NULL,
772 &(tv_iters.tag_other), _("Other"), "classviewer-other",
773 NULL);
774 break;
776 case GEANY_FILETYPES_PHP:
778 tag_list_add_groups(tag_store,
779 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
780 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
781 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
782 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
783 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
784 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
785 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
786 NULL);
787 break;
789 case GEANY_FILETYPES_HTML:
791 tag_list_add_groups(tag_store,
792 &(tv_iters.tag_function), _("Functions"), NULL,
793 &(tv_iters.tag_member), _("Anchors"), NULL,
794 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
795 &(tv_iters.tag_class), _("H2 Headings"), NULL,
796 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
797 NULL);
798 break;
800 case GEANY_FILETYPES_CSS:
802 tag_list_add_groups(tag_store,
803 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
804 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
805 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
806 break;
808 case GEANY_FILETYPES_REST:
809 case GEANY_FILETYPES_TXT2TAGS:
810 case GEANY_FILETYPES_ABC:
812 tag_list_add_groups(tag_store,
813 &(tv_iters.tag_namespace), _("Chapter"), NULL,
814 &(tv_iters.tag_member), _("Section"), NULL,
815 &(tv_iters.tag_macro), _("Subsection"), NULL,
816 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
817 NULL);
818 break;
820 case GEANY_FILETYPES_ASCIIDOC:
822 tag_list_add_groups(tag_store,
823 &(tv_iters.tag_namespace), _("Document"), NULL,
824 &(tv_iters.tag_member), _("Section Level 1"), NULL,
825 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
826 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
827 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
828 NULL);
829 break;
831 case GEANY_FILETYPES_RUBY:
833 tag_list_add_groups(tag_store,
834 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
835 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
836 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
837 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
838 NULL);
839 break;
841 case GEANY_FILETYPES_TCL:
843 tag_list_add_groups(tag_store,
844 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
845 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
846 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
847 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
848 NULL);
849 break;
851 case GEANY_FILETYPES_PYTHON:
853 tag_list_add_groups(tag_store,
854 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
855 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
856 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
857 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
858 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
859 NULL);
860 break;
862 case GEANY_FILETYPES_VHDL:
864 tag_list_add_groups(tag_store,
865 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
866 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
867 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
868 &(tv_iters.tag_type), _("Types"), "classviewer-other",
869 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
870 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
871 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
872 &(tv_iters.tag_other), _("Other"), "classviewer-other",
873 NULL);
874 break;
876 case GEANY_FILETYPES_VERILOG:
878 tag_list_add_groups(tag_store,
879 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
880 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
881 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
882 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
883 &(tv_iters.tag_other), _("Other"), "classviewer-other",
884 NULL);
885 break;
887 case GEANY_FILETYPES_JAVA:
889 tag_list_add_groups(tag_store,
890 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
891 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
892 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
893 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
894 &(tv_iters.tag_member), _("Members"), "classviewer-member",
895 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
896 &(tv_iters.tag_other), _("Other"), "classviewer-other",
897 NULL);
898 break;
900 case GEANY_FILETYPES_AS:
902 tag_list_add_groups(tag_store,
903 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
904 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
905 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
906 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
907 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
908 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
909 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
910 &(tv_iters.tag_other), _("Other"), "classviewer-other",
911 NULL);
912 break;
914 case GEANY_FILETYPES_HAXE:
916 tag_list_add_groups(tag_store,
917 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
918 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
919 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
920 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
921 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
922 &(tv_iters.tag_other), _("Other"), "classviewer-other",
923 NULL);
924 break;
926 case GEANY_FILETYPES_BASIC:
928 tag_list_add_groups(tag_store,
929 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
930 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
931 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
932 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
933 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
934 &(tv_iters.tag_other), _("Other"), "classviewer-other",
935 NULL);
936 break;
938 case GEANY_FILETYPES_F77:
939 case GEANY_FILETYPES_FORTRAN:
941 tag_list_add_groups(tag_store,
942 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
943 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
944 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
945 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
946 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
947 &(tv_iters.tag_class), _("Types"), "classviewer-class",
948 &(tv_iters.tag_member), _("Components"), "classviewer-member",
949 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
950 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
951 &(tv_iters.tag_other), _("Other"), "classviewer-other",
952 NULL);
953 break;
955 case GEANY_FILETYPES_ASM:
957 tag_list_add_groups(tag_store,
958 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
959 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
960 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
961 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
962 NULL);
963 break;
965 case GEANY_FILETYPES_MAKE:
966 tag_list_add_groups(tag_store,
967 &tv_iters.tag_function, _("Targets"), "classviewer-method",
968 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
969 NULL);
970 break;
971 case GEANY_FILETYPES_SQL:
973 tag_list_add_groups(tag_store,
974 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
975 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
976 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
977 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
978 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
979 &(tv_iters.tag_member), _("Views"), "classviewer-var",
980 &(tv_iters.tag_other), _("Other"), "classviewer-other",
981 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
982 NULL);
983 break;
985 case GEANY_FILETYPES_D:
986 default:
988 if (ft_id == GEANY_FILETYPES_D)
989 tag_list_add_groups(tag_store,
990 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
991 else
992 tag_list_add_groups(tag_store,
993 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
995 tag_list_add_groups(tag_store,
996 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
997 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
998 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
999 &(tv_iters.tag_member), _("Members"), "classviewer-member",
1000 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
1001 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
1002 NULL);
1004 if (ft_id != GEANY_FILETYPES_D)
1006 tag_list_add_groups(tag_store,
1007 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1009 tag_list_add_groups(tag_store,
1010 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1011 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1012 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1018 /* removes toplevel items that have no children */
1019 static void hide_empty_rows(GtkTreeStore *store)
1021 GtkTreeIter iter;
1022 gboolean cont = TRUE;
1024 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1025 return; /* stop when first iter is invalid, i.e. no elements */
1027 while (cont)
1029 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1030 cont = gtk_tree_store_remove(store, &iter);
1031 else
1032 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1037 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1039 gchar *utf8_name;
1040 const gchar *scope = tag->atts.entry.scope;
1041 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1042 gboolean doc_is_utf8 = FALSE;
1044 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1045 * for None at this point completely */
1046 if (utils_str_equal(doc->encoding, "UTF-8") ||
1047 utils_str_equal(doc->encoding, "None"))
1048 doc_is_utf8 = TRUE;
1049 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1050 * plugin might have called tm_source_file_update(), so check to be sure */
1051 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1053 if (! doc_is_utf8)
1054 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1055 -1, doc->encoding, TRUE);
1056 else
1057 utf8_name = tag->name;
1059 if (utf8_name == NULL)
1060 return NULL;
1062 if (! buffer)
1063 buffer = g_string_new(NULL);
1064 else
1065 g_string_truncate(buffer, 0);
1067 /* check first char of scope is a wordchar */
1068 if (!found_parent && scope &&
1069 strpbrk(scope, GEANY_WORDCHARS) == scope)
1071 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1073 g_string_append(buffer, scope);
1074 g_string_append(buffer, sep);
1076 g_string_append(buffer, utf8_name);
1078 if (! doc_is_utf8)
1079 g_free(utf8_name);
1081 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1083 return buffer->str;
1087 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1089 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1091 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1092 * for None at this point completely */
1093 if (utf8_name != NULL &&
1094 ! utils_str_equal(doc->encoding, "UTF-8") &&
1095 ! utils_str_equal(doc->encoding, "None"))
1097 SETPTR(utf8_name,
1098 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1101 if (utf8_name != NULL)
1102 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1104 return utf8_name;
1108 /* find the last word in "foo::bar::blah", e.g. "blah" */
1109 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1111 const gchar *scope = tag->atts.entry.scope;
1112 const gchar *separator = symbols_get_context_separator(ft_id);
1113 const gchar *str, *ptr;
1115 if (!scope)
1116 return NULL;
1118 str = scope;
1120 while (1)
1122 ptr = strstr(str, separator);
1123 if (ptr)
1125 str = ptr + strlen(separator);
1127 else
1128 break;
1131 return !EMPTY(str) ? str : NULL;
1135 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1137 GtkTreeIter *iter = NULL;
1139 switch (tag_type)
1141 case tm_tag_prototype_t:
1142 case tm_tag_method_t:
1143 case tm_tag_function_t:
1145 iter = &tv_iters.tag_function;
1146 break;
1148 case tm_tag_externvar_t:
1150 iter = &tv_iters.tag_externvar;
1151 break;
1153 case tm_tag_macro_t:
1154 case tm_tag_macro_with_arg_t:
1156 iter = &tv_iters.tag_macro;
1157 break;
1159 case tm_tag_class_t:
1161 iter = &tv_iters.tag_class;
1162 break;
1164 case tm_tag_member_t:
1165 case tm_tag_field_t:
1167 iter = &tv_iters.tag_member;
1168 break;
1170 case tm_tag_typedef_t:
1171 case tm_tag_enum_t:
1173 iter = &tv_iters.tag_type;
1174 break;
1176 case tm_tag_union_t:
1177 case tm_tag_struct_t:
1179 iter = &tv_iters.tag_struct;
1180 break;
1182 case tm_tag_interface_t:
1183 iter = &tv_iters.tag_interface;
1184 break;
1185 case tm_tag_variable_t:
1187 iter = &tv_iters.tag_variable;
1188 break;
1190 case tm_tag_namespace_t:
1191 case tm_tag_package_t:
1193 iter = &tv_iters.tag_namespace;
1194 break;
1196 default:
1198 iter = &tv_iters.tag_other;
1201 if (G_LIKELY(iter->stamp != -1))
1202 return iter;
1203 else
1204 return NULL;
1208 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1210 GdkPixbuf *icon = NULL;
1212 if (parent == &tv_iters.tag_other)
1214 return get_tag_icon("classviewer-var");
1216 /* copy parent icon */
1217 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1218 SYMBOLS_COLUMN_ICON, &icon, -1);
1219 return icon;
1223 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1225 const TMTag *t1 = v1;
1226 const TMTag *t2 = v2;
1228 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1229 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1230 /* include arglist in match to support e.g. C++ overloading */
1231 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1235 /* inspired from g_str_hash() */
1236 static guint tag_hash(gconstpointer v)
1238 const TMTag *tag = v;
1239 const gchar *p;
1240 guint32 h = 5381;
1242 h = (h << 5) + h + tag->type;
1243 for (p = tag->name; *p != '\0'; p++)
1244 h = (h << 5) + h + *p;
1245 if (tag->atts.entry.scope)
1247 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1248 h = (h << 5) + h + *p;
1250 /* for e.g. C++ overloading */
1251 if (tag->atts.entry.arglist)
1253 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1254 h = (h << 5) + h + *p;
1257 return h;
1261 /* like gtk_tree_view_expand_to_path() but with an iter */
1262 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1264 GtkTreeModel *model = gtk_tree_view_get_model(view);
1265 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1267 gtk_tree_view_expand_to_path(view, path);
1268 gtk_tree_path_free(path);
1272 /* like gtk_tree_store_remove() but finds the next iter at any level */
1273 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1275 GtkTreeIter parent;
1276 gboolean has_parent;
1277 gboolean cont;
1279 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1280 cont = gtk_tree_store_remove(store, iter);
1281 /* if there is no next at this level but there is a parent iter, continue from it */
1282 if (! cont && has_parent)
1284 *iter = parent;
1285 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1288 return cont;
1292 /* adds a new element in the parent table if it's key is known.
1293 * duplicates are kept */
1294 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1295 const GtkTreeIter *iter)
1297 GList **list;
1298 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1299 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1301 if (! list)
1303 list = g_slice_alloc(sizeof *list);
1304 *list = NULL;
1305 g_hash_table_insert(table, tag->name, list);
1307 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1312 static void free_iter_slice_list(gpointer data)
1314 GList **list = data;
1316 if (list)
1318 GList *node;
1319 foreach_list(node, *list)
1320 g_slice_free(GtkTreeIter, node->data);
1321 g_list_free(*list);
1322 g_slice_free1(sizeof *list, list);
1327 /* inserts a @data in @table on key @tag.
1328 * previous data is not overwritten if the key is duplicated, but rather the
1329 * two values are kept in a list
1331 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1332 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1334 GList *list = g_hash_table_lookup(table, tag);
1335 list = g_list_prepend(list, data);
1336 g_hash_table_insert(table, tag, list);
1340 /* looks up the entry in @table that better matches @tag.
1341 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1342 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1344 GList *data = NULL;
1345 GList *node = g_hash_table_lookup(table, tag);
1346 if (node)
1348 glong delta;
1349 data = node->data;
1351 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1353 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1354 for (node = node->next; node; node = node->next)
1356 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1358 if (d < delta)
1360 data = node->data;
1361 delta = d;
1365 #undef TAG_DELTA
1368 return data;
1372 /* removes the element at @tag from @table.
1373 * @tag must be the exact pointer used at insertion time */
1374 static void tags_table_remove(GHashTable *table, TMTag *tag)
1376 GList *list = g_hash_table_lookup(table, tag);
1377 if (list)
1379 GList *node;
1380 foreach_list(node, list)
1382 if (((GList *) node->data)->data == tag)
1383 break;
1385 list = g_list_delete_link(list, node);
1386 if (list)
1387 g_hash_table_insert(table, tag, list);
1388 else
1389 g_hash_table_remove(table, tag);
1394 static void tags_table_destroy(GHashTable *table)
1396 /* free any leftover elements. note that we can't register a value_free_func when
1397 * creating the hash table because we only want to free it when destroying the table,
1398 * not when inserting a duplicate (we handle this manually) */
1399 GHashTableIter iter;
1400 gpointer value;
1402 g_hash_table_iter_init(&iter, table);
1403 while (g_hash_table_iter_next(&iter, NULL, &value))
1404 g_list_free(value);
1405 g_hash_table_destroy(table);
1410 * Updates the tag tree for a document with the tags in *list.
1411 * @param doc a document
1412 * @param tags a pointer to a GList* holding the tags to add/update. This
1413 * list may be updated, removing updated elements.
1415 * The update is done in two passes:
1416 * 1) walking the current tree, update tags that still exist and remove the
1417 * obsolescent ones;
1418 * 2) walking the remaining (non updated) tags, adds them in the list.
1420 * For better performances, we use 2 hash tables:
1421 * - one containing all the tags for lookup in the first pass (actually stores a
1422 * reference in the tags list for removing it efficiently), avoiding list search
1423 * on each tag;
1424 * - the other holding "tag-name":row references for tags having children, used to
1425 * lookup for a parent in both passes, avoiding tree traversal.
1427 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1429 GtkTreeStore *store = doc->priv->tag_store;
1430 GtkTreeModel *model = GTK_TREE_MODEL(store);
1431 GHashTable *parents_table;
1432 GHashTable *tags_table;
1433 GtkTreeIter iter;
1434 gboolean cont;
1435 GList *item;
1437 /* Build hash tables holding tags and parents */
1438 /* parent table holds "tag-name":GtkTreeIter */
1439 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1440 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1441 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1442 foreach_list(item, *tags)
1444 TMTag *tag = item->data;
1445 const gchar *name;
1447 tags_table_insert(tags_table, tag, item);
1449 name = get_parent_name(tag, doc->file_type->id);
1450 if (name)
1451 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1454 /* First pass, update existing rows or delete them.
1455 * It is OK to delete them since we walk top down so we would remove
1456 * parents before checking for their children, thus never implicitly
1457 * deleting an updated child */
1458 cont = gtk_tree_model_get_iter_first(model, &iter);
1459 while (cont)
1461 TMTag *tag;
1463 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1464 if (! tag) /* most probably a toplevel, skip it */
1465 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1466 else
1468 GList *found_item;
1470 found_item = tags_table_lookup(tags_table, tag);
1471 if (! found_item) /* tag doesn't exist, remove it */
1472 cont = tree_store_remove_row(store, &iter);
1473 else /* tag still exist, update it */
1475 const gchar *name;
1476 const gchar *parent_name;
1477 TMTag *found = found_item->data;
1479 parent_name = get_parent_name(found, doc->file_type->id);
1480 /* if parent is unknown, ignore it */
1481 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1482 parent_name = NULL;
1484 /* only update fields that (can) have changed (name that holds line
1485 * number, and the tag itself) */
1486 name = get_symbol_name(doc, found, parent_name != NULL);
1487 gtk_tree_store_set(store, &iter,
1488 SYMBOLS_COLUMN_NAME, name,
1489 SYMBOLS_COLUMN_TAG, found,
1490 -1);
1492 update_parents_table(parents_table, found, parent_name, &iter);
1494 /* remove the updated tag from the table and list */
1495 tags_table_remove(tags_table, found);
1496 *tags = g_list_delete_link(*tags, found_item);
1498 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1501 tm_tag_unref(tag);
1505 /* Second pass, now we have a tree cleaned up from invalid rows,
1506 * we simply add new ones */
1507 foreach_list (item, *tags)
1509 TMTag *tag = item->data;
1510 GtkTreeIter *parent;
1512 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1513 if (G_UNLIKELY(! parent))
1514 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1515 else
1517 gboolean expand;
1518 const gchar *name;
1519 const gchar *parent_name;
1520 gchar *tooltip;
1521 GdkPixbuf *icon = get_child_icon(store, parent);
1523 parent_name = get_parent_name(tag, doc->file_type->id);
1524 if (parent_name)
1526 GList **candidates;
1527 GtkTreeIter *parent_search = NULL;
1529 /* walk parent candidates to find the better one.
1530 * if there are more than one, take the one that has the closest line number
1531 * after the tag we're searching the parent for */
1532 candidates = g_hash_table_lookup(parents_table, parent_name);
1533 if (candidates)
1535 GList *node;
1536 glong delta = G_MAXLONG;
1537 foreach_list(node, *candidates)
1539 TMTag *parent_tag;
1540 glong d;
1542 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1543 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1545 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1546 if (! parent_search || (d >= 0 && d < delta))
1548 delta = d;
1549 parent_search = node->data;
1551 tm_tag_unref(parent_tag);
1555 if (parent_search)
1556 parent = parent_search;
1557 else
1558 parent_name = NULL;
1561 /* only expand to the iter if the parent was empty, otherwise we let the
1562 * folding as it was before (already expanded, or closed by the user) */
1563 expand = ! gtk_tree_model_iter_has_child(model, parent);
1565 /* insert the new element */
1566 gtk_tree_store_append(store, &iter, parent);
1567 name = get_symbol_name(doc, tag, parent_name != NULL);
1568 tooltip = get_symbol_tooltip(doc, tag);
1569 gtk_tree_store_set(store, &iter,
1570 SYMBOLS_COLUMN_NAME, name,
1571 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1572 SYMBOLS_COLUMN_ICON, icon,
1573 SYMBOLS_COLUMN_TAG, tag,
1574 -1);
1575 g_free(tooltip);
1576 if (G_LIKELY(icon))
1577 g_object_unref(icon);
1579 update_parents_table(parents_table, tag, parent_name, &iter);
1581 if (expand)
1582 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1586 g_hash_table_destroy(parents_table);
1587 tags_table_destroy(tags_table);
1591 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1592 * is not stable, so the order is already lost. */
1593 static gint compare_top_level_names(const gchar *a, const gchar *b)
1595 guint i;
1596 const gchar *name;
1598 /* This should never happen as it would mean that two or more top
1599 * level items have the same name but it can happen by typos in the translations. */
1600 if (utils_str_equal(a, b))
1601 return 1;
1603 foreach_ptr_array(name, i, top_level_iter_names)
1605 if (utils_str_equal(name, a))
1606 return -1;
1607 if (utils_str_equal(name, b))
1608 return 1;
1610 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1611 return 0;
1615 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1616 GtkTreeIter *iter)
1618 /* if the tag has a parent tag, it should be at depth >= 2 */
1619 return !EMPTY(tag->atts.entry.scope) &&
1620 gtk_tree_store_iter_depth(store, iter) == 1;
1624 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1625 gpointer user_data)
1627 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1628 TMTag *tag_a, *tag_b;
1629 gint cmp;
1631 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1632 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1634 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1635 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1636 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1637 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1639 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1640 compare_symbol_lines(tag_a, tag_b);
1642 else
1644 gchar *astr, *bstr;
1646 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1647 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1649 /* if a is toplevel, b must be also */
1650 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1652 cmp = compare_top_level_names(astr, bstr);
1654 else
1656 /* this is what g_strcmp0() does */
1657 if (! astr)
1658 cmp = -(astr != bstr);
1659 else if (! bstr)
1660 cmp = astr != bstr;
1661 else
1663 cmp = strcmp(astr, bstr);
1665 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1666 if (tag_a && tag_b)
1667 if (!sort_by_name ||
1668 (utils_str_equal(tag_a->name, tag_b->name) &&
1669 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1670 cmp = compare_symbol_lines(tag_a, tag_b);
1673 g_free(astr);
1674 g_free(bstr);
1676 tm_tag_unref(tag_a);
1677 tm_tag_unref(tag_b);
1679 return cmp;
1683 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1685 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1686 GINT_TO_POINTER(sort_by_name), NULL);
1688 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1692 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1694 GList *tags;
1696 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1698 tags = get_tag_list(doc, tm_tag_max_t);
1699 if (tags == NULL)
1700 return FALSE;
1702 /* FIXME: Not sure why we detached the model here? */
1704 /* disable sorting during update because the code doesn't support correctly
1705 * models that are currently being built */
1706 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1708 /* add grandparent type iters */
1709 add_top_level_items(doc);
1711 update_tree_tags(doc, &tags);
1712 g_list_free(tags);
1714 hide_empty_rows(doc->priv->tag_store);
1716 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1717 sort_mode = doc->priv->symbol_list_sort_mode;
1719 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1720 doc->priv->symbol_list_sort_mode = sort_mode;
1722 return TRUE;
1726 /* Detects a global tags filetype from the *.lang.* language extension.
1727 * Returns NULL if there was no matching TM language. */
1728 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1730 gchar *tags_ext;
1731 gchar *shortname = utils_strdupa(utf8_filename);
1732 GeanyFiletype *ft = NULL;
1734 tags_ext = g_strrstr(shortname, ".tags");
1735 if (tags_ext)
1737 *tags_ext = '\0'; /* remove .tags extension */
1738 ft = filetypes_detect_from_extension(shortname);
1739 if (ft->id != GEANY_FILETYPES_NONE)
1740 return ft;
1742 return NULL;
1746 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1747 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1748 * the relevant path.
1749 * Example:
1750 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1751 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1753 /* -E pre-process, -dD output user macros, -p prof info (?) */
1754 const char pre_process[] = "gcc -E -dD -p -I.";
1756 if (argc > 2)
1758 /* Create global taglist */
1759 int status;
1760 char *command;
1761 const char *tags_file = argv[1];
1762 char *utf8_fname;
1763 GeanyFiletype *ft;
1765 utf8_fname = utils_get_utf8_from_locale(tags_file);
1766 ft = detect_global_tags_filetype(utf8_fname);
1767 g_free(utf8_fname);
1769 if (ft == NULL)
1771 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1772 return 1;
1774 /* load config in case of custom filetypes */
1775 filetypes_load_config(ft->id, FALSE);
1777 /* load ignore list for C/C++ parser */
1778 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1779 load_c_ignore_tags();
1781 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1783 const gchar *cflags = getenv("CFLAGS");
1784 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1786 else
1787 command = NULL; /* don't preprocess */
1789 geany_debug("Generating %s tags file.", ft->name);
1790 tm_get_workspace();
1791 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1792 argc - 2, tags_file, ft->lang);
1793 g_free(command);
1794 symbols_finalize(); /* free c_tags_ignore data */
1795 if (! status)
1797 g_printerr(_("Failed to create tags file, perhaps because no tags "
1798 "were found.\n"));
1799 return 1;
1802 else
1804 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1805 g_printerr(_("Example:\n"
1806 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1807 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1808 return 1;
1810 return 0;
1814 void symbols_show_load_tags_dialog(void)
1816 GtkWidget *dialog;
1817 GtkFileFilter *filter;
1819 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1820 GTK_FILE_CHOOSER_ACTION_OPEN,
1821 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1822 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1823 NULL);
1824 gtk_widget_set_name(dialog, "GeanyDialog");
1825 filter = gtk_file_filter_new();
1826 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1827 gtk_file_filter_add_pattern(filter, "*.*.tags");
1828 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1830 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1832 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1833 GSList *item;
1835 for (item = flist; item != NULL; item = g_slist_next(item))
1837 gchar *fname = item->data;
1838 gchar *utf8_fname;
1839 GeanyFiletype *ft;
1841 utf8_fname = utils_get_utf8_from_locale(fname);
1842 ft = detect_global_tags_filetype(utf8_fname);
1844 if (ft != NULL && symbols_load_global_tags(fname, ft))
1845 /* For translators: the first wildcard is the filetype, the second the filename */
1846 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1847 filetypes_get_display_name(ft), utf8_fname);
1848 else
1849 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1851 g_free(utf8_fname);
1852 g_free(fname);
1854 g_slist_free(flist);
1856 gtk_widget_destroy(dialog);
1860 static void init_user_tags(void)
1862 GSList *file_list = NULL, *list = NULL;
1863 const GSList *node;
1864 gchar *dir;
1866 dir = g_build_filename(app->configdir, "tags", NULL);
1867 /* create the user tags dir for next time if it doesn't exist */
1868 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1869 utils_mkdir(dir, FALSE);
1870 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1872 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1873 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1874 g_free(dir);
1876 file_list = g_slist_concat(file_list, list);
1878 /* populate the filetype-specific tag files lists */
1879 for (node = file_list; node != NULL; node = node->next)
1881 gchar *fname = node->data;
1882 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1883 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1885 g_free(utf8_fname);
1887 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1888 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1889 else
1891 geany_debug("Unknown filetype for file '%s'.", fname);
1892 g_free(fname);
1896 /* don't need to delete list contents because they are now stored in
1897 * ft->priv->tag_files */
1898 g_slist_free(file_list);
1902 static void load_user_tags(filetype_id ft_id)
1904 static guchar *tags_loaded = NULL;
1905 static gboolean init_tags = FALSE;
1906 const GSList *node;
1907 GeanyFiletype *ft = filetypes[ft_id];
1909 g_return_if_fail(ft_id > 0);
1911 if (!tags_loaded)
1912 tags_loaded = g_new0(guchar, filetypes_array->len);
1913 if (tags_loaded[ft_id])
1914 return;
1915 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1917 if (!init_tags)
1919 init_user_tags();
1920 init_tags = TRUE;
1923 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1925 const gchar *fname = node->data;
1927 symbols_load_global_tags(fname, ft);
1932 static gboolean goto_tag(const gchar *name, gboolean definition)
1934 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1935 guint type;
1936 TMTag *tmtag = NULL;
1937 GeanyDocument *old_doc = document_get_current();
1939 /* goto tag definition: all except prototypes / forward declarations / externs */
1940 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1942 /* first look in the current document */
1943 if (old_doc != NULL && old_doc->tm_file)
1944 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1946 /* if not found, look in the workspace */
1947 if (tmtag == NULL)
1948 tmtag = find_workspace_tag(name, type);
1950 if (tmtag != NULL)
1952 GeanyDocument *new_doc = document_find_by_real_path(
1953 tmtag->atts.entry.file->work_object.file_name);
1955 if (new_doc)
1957 /* If we are already on the tag line, swap definition/declaration */
1958 if (new_doc == old_doc &&
1959 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1961 if (goto_tag(name, !definition))
1962 return TRUE;
1965 else
1967 /* not found in opened document, should open */
1968 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1971 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1972 return TRUE;
1974 return FALSE;
1978 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1980 if (goto_tag(name, definition))
1981 return TRUE;
1983 /* if we are here, there was no match and we are beeping ;-) */
1984 utils_beep();
1986 if (!definition)
1987 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1988 else
1989 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1990 return FALSE;
1994 /* This could perhaps be improved to check for #if, class etc. */
1995 static gint get_function_fold_number(GeanyDocument *doc)
1997 /* for Java the functions are always one fold level above the class scope */
1998 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1999 return SC_FOLDLEVELBASE + 1;
2000 else
2001 return SC_FOLDLEVELBASE;
2005 /* Should be used only with get_current_tag_cached.
2006 * tag_types caching might trigger recomputation too often but this isn't used differently often
2007 * enough to be an issue for now */
2008 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2010 static gint old_line = -2;
2011 static GeanyDocument *old_doc = NULL;
2012 static gint old_fold_num = -1;
2013 static guint old_tag_types = 0;
2014 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2015 gboolean ret;
2017 /* check if the cached line and file index have changed since last time: */
2018 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2019 ret = TRUE;
2020 else if (cur_line == old_line)
2021 ret = FALSE;
2022 else
2024 /* if the line has only changed by 1 */
2025 if (abs(cur_line - old_line) == 1)
2027 /* It's the same function if the fold number hasn't changed */
2028 ret = (fold_num != old_fold_num);
2030 else ret = TRUE;
2033 /* record current line and file index for next time */
2034 old_line = cur_line;
2035 old_doc = doc;
2036 old_fold_num = fold_num;
2037 old_tag_types = tag_types;
2038 return ret;
2042 /* Parse the function name up to 2 lines before tag_line.
2043 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2044 * type or argument names can be confused with the function name. */
2045 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2047 gint start, end, max_pos;
2048 gint fn_style;
2050 switch (sci_get_lexer(sci))
2052 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2053 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2054 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2056 start = sci_get_position_from_line(sci, tag_line - 2);
2057 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2058 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2059 start++;
2061 end = start;
2062 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2063 end++;
2065 if (start == end)
2066 return NULL;
2067 return sci_get_contents_range(sci, start, end);
2071 /* Parse the function name */
2072 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2074 gint start, end, first_pos, max_pos;
2075 gint tmp;
2076 gchar c;
2078 first_pos = end = sci_get_position_from_line(sci, tag_line);
2079 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2080 tmp = 0;
2081 /* goto the begin of function body */
2082 while (end < max_pos &&
2083 (tmp = sci_get_char_at(sci, end)) != '{' &&
2084 tmp != 0) end++;
2085 if (tmp == 0) end --;
2087 /* go back to the end of function identifier */
2088 while (end > 0 && end > first_pos - 500 &&
2089 (tmp = sci_get_char_at(sci, end)) != '(' &&
2090 tmp != 0) end--;
2091 end--;
2092 if (end < 0) end = 0;
2094 /* skip whitespaces between identifier and ( */
2095 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2097 start = end;
2098 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2099 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2100 || tmp == SCE_C_GLOBALCLASS
2101 || (c = sci_get_char_at(sci, start)) == '~'
2102 || c == ':'))
2103 start--;
2104 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2106 if (start == end) return NULL;
2107 return sci_get_contents_range(sci, start, end + 1);
2111 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2113 gint line_count = sci_get_line_count(sci);
2115 for (; line < line_count; line++)
2117 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2118 return line;
2121 return -1;
2125 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2127 gint line;
2128 gint parent;
2130 line = sci_get_current_line(doc->editor->sci);
2131 parent = sci_get_fold_parent(doc->editor->sci, line);
2132 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2133 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2134 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2136 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2138 if (tag)
2140 gint tag_line = tag->atts.entry.line - 1;
2141 gint last_child = line + 1;
2143 /* if it may be a false positive because we're inside a fold level not inside anything
2144 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2145 * right after the tag we got from TM */
2146 if (abs(tag_line - parent) > 1)
2148 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2149 if (tag_fold >= 0)
2150 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2153 if (line <= last_child)
2155 if (tag->atts.entry.scope)
2156 *tagname = g_strconcat(tag->atts.entry.scope,
2157 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2158 else
2159 *tagname = g_strdup(tag->name);
2161 return tag_line;
2165 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2166 * to dirty and inaccurate hand-parsing */
2167 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2169 const gint fn_fold = get_function_fold_number(doc);
2170 gint tag_line = parent;
2171 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2173 /* find the top level fold point */
2174 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2176 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2177 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2180 if (tag_line >= 0)
2182 gchar *cur_tag;
2184 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2185 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2186 else
2187 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2189 if (cur_tag != NULL)
2191 *tagname = cur_tag;
2192 return tag_line;
2197 *tagname = g_strdup(_("unknown"));
2198 return -1;
2202 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2204 static gint tag_line = -1;
2205 static gchar *cur_tag = NULL;
2207 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2209 if (doc == NULL) /* reset current function */
2211 current_tag_changed(NULL, -1, -1, 0);
2212 g_free(cur_tag);
2213 cur_tag = g_strdup(_("unknown"));
2214 if (tagname != NULL)
2215 *tagname = cur_tag;
2216 tag_line = -1;
2218 else
2220 gint line = sci_get_current_line(doc->editor->sci);
2221 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2223 if (current_tag_changed(doc, line, fold_level, tag_types))
2225 g_free(cur_tag);
2226 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2228 *tagname = cur_tag;
2231 return tag_line;
2235 /* Sets *tagname to point at the current function or tag name.
2236 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2237 * call to this function.
2238 * Returns: line number of the current tag, or -1 if unknown. */
2239 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2241 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2245 /* same as symbols_get_current_function() but finds class, namespaces and more */
2246 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2248 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2249 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2251 /* Python parser reports imports as namespaces which confuses the scope detection */
2252 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2253 tag_types |= tm_tag_namespace_t;
2255 return get_current_tag_name_cached(doc, tagname, tag_types);
2259 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2261 gint sort_mode = GPOINTER_TO_INT(user_data);
2262 GeanyDocument *doc = document_get_current();
2264 if (ignore_callback)
2265 return;
2267 if (doc != NULL)
2268 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2272 static void on_symbol_tree_menu_show(GtkWidget *widget,
2273 gpointer user_data)
2275 GeanyDocument *doc = document_get_current();
2276 gboolean enable;
2278 enable = doc && doc->has_tags;
2279 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2280 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2281 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2282 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2283 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2284 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2286 if (! doc)
2287 return;
2289 ignore_callback = TRUE;
2291 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2292 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2293 else
2294 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2296 ignore_callback = FALSE;
2300 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2302 gboolean expand = GPOINTER_TO_INT(user_data);
2303 GeanyDocument *doc = document_get_current();
2305 if (! doc)
2306 return;
2308 g_return_if_fail(doc->priv->tag_tree);
2310 if (expand)
2311 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2312 else
2313 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2317 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2319 GtkTreeIter iter;
2320 GtkTreeSelection *selection;
2321 GtkTreeModel *model;
2322 GeanyDocument *doc;
2323 TMTag *tag = NULL;
2325 doc = document_get_current();
2326 if (!doc)
2327 return;
2329 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2330 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2331 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2332 if (tag)
2334 if (widget == symbol_menu.find_in_files)
2335 search_show_find_in_files_dialog_full(tag->name, NULL);
2336 else
2337 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2338 widget == symbol_menu.find_usage);
2340 tm_tag_unref(tag);
2345 static void create_taglist_popup_menu(void)
2347 GtkWidget *item, *menu;
2349 tv.popup_taglist = menu = gtk_menu_new();
2351 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2352 gtk_widget_show(item);
2353 gtk_container_add(GTK_CONTAINER(menu), item);
2354 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2356 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2357 gtk_widget_show(item);
2358 gtk_container_add(GTK_CONTAINER(menu), item);
2359 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2361 item = gtk_separator_menu_item_new();
2362 gtk_widget_show(item);
2363 gtk_container_add(GTK_CONTAINER(menu), item);
2365 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2366 _("Sort by _Name"));
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_NAME));
2372 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2373 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2374 gtk_widget_show(item);
2375 gtk_container_add(GTK_CONTAINER(menu), item);
2376 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2377 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2379 item = gtk_separator_menu_item_new();
2380 gtk_widget_show(item);
2381 gtk_container_add(GTK_CONTAINER(menu), item);
2383 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2384 gtk_widget_show(item);
2385 gtk_container_add(GTK_CONTAINER(menu), item);
2386 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2388 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2389 gtk_widget_show(item);
2390 gtk_container_add(GTK_CONTAINER(menu), item);
2391 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2393 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2394 gtk_widget_show(item);
2395 gtk_container_add(GTK_CONTAINER(menu), item);
2396 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2398 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2400 sidebar_add_common_menu_items(GTK_MENU(menu));
2404 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2406 gchar *f;
2408 g_return_if_fail(!EMPTY(doc->real_path));
2410 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2411 if (utils_str_equal(doc->real_path, f))
2412 load_c_ignore_tags();
2414 g_free(f);
2418 void symbols_init(void)
2420 gchar *f;
2422 create_taglist_popup_menu();
2424 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2425 ui_add_config_file_menu_item(f, NULL, NULL);
2426 g_free(f);
2428 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2432 void symbols_finalize(void)
2434 g_strfreev(html_entities);
2435 g_strfreev(c_tags_ignore);