Remove TmWorkObject and all the OO related stuff
[geany-mirror.git] / src / symbols.c
blob302a3e62ac27eb98acbe0504a27b25406bdf446b
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->source_files == 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->source_files->len; j++)
344 GPtrArray *tags;
346 tags = tm_tags_extract(TM_SOURCE_FILE(app->tm_workspace->source_files->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_source_file_tag(GPtrArray *tags_array,
399 const gchar *tag_name, guint type)
401 GPtrArray *tags;
402 TMTag *tmtag;
404 tags = tm_tags_extract(tags_array, type);
405 if (tags != NULL)
407 tmtag = symbols_find_tm_tag(tags, tag_name);
409 g_ptr_array_free(tags, TRUE);
411 if (tmtag != NULL)
412 return tmtag;
414 return NULL; /* not found */
418 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
420 guint j;
421 const GPtrArray *source_files = NULL;
423 if (app->tm_workspace != NULL)
424 source_files = app->tm_workspace->source_files;
426 if (source_files != NULL)
428 for (j = 0; j < source_files->len; j++)
430 TMSourceFile *srcfile = source_files->pdata[j];
431 TMTag *tmtag;
433 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
434 if (tmtag != NULL)
435 return tmtag;
438 return NULL; /* not found */
442 const gchar **symbols_get_html_entities(void)
444 if (html_entities == NULL)
445 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
447 return (const gchar **) html_entities;
451 /* sort by name, then line */
452 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
454 gint ret;
456 if (tag_a == NULL || tag_b == NULL)
457 return 0;
459 if (tag_a->name == NULL)
460 return -(tag_a->name != tag_b->name);
462 if (tag_b->name == NULL)
463 return tag_a->name != tag_b->name;
465 ret = strcmp(tag_a->name, tag_b->name);
466 if (ret == 0)
468 return tag_a->atts.entry.line - tag_b->atts.entry.line;
470 return ret;
474 /* sort by line, then scope */
475 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
477 const TMTag *tag_a = TM_TAG(a);
478 const TMTag *tag_b = TM_TAG(b);
479 gint ret;
481 if (a == NULL || b == NULL)
482 return 0;
484 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
485 if (ret == 0)
487 if (tag_a->atts.entry.scope == NULL)
488 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
489 if (tag_b->atts.entry.scope == NULL)
490 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
491 else
492 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
494 return ret;
498 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
500 GList *tag_names = NULL;
501 TMTag *tag;
502 guint i;
504 g_return_val_if_fail(doc, NULL);
506 if (! doc->tm_file || ! doc->tm_file->tags_array)
507 return NULL;
509 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
511 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
512 if (G_UNLIKELY(tag == NULL))
513 return NULL;
515 if (tag->type & tag_types)
517 tag_names = g_list_prepend(tag_names, tag);
520 tag_names = g_list_sort(tag_names, compare_symbol_lines);
521 return tag_names;
525 /* amount of types in the symbol list (currently max. 8 are used) */
526 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
528 struct TreeviewSymbols
530 GtkTreeIter tag_function;
531 GtkTreeIter tag_class;
532 GtkTreeIter tag_macro;
533 GtkTreeIter tag_member;
534 GtkTreeIter tag_variable;
535 GtkTreeIter tag_externvar;
536 GtkTreeIter tag_namespace;
537 GtkTreeIter tag_struct;
538 GtkTreeIter tag_interface;
539 GtkTreeIter tag_type;
540 GtkTreeIter tag_other;
541 } tv_iters;
544 static void init_tag_iters(void)
546 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
547 * filetypes(e.g. config file to Python crashes Geany without this) */
548 tv_iters.tag_function.stamp = -1;
549 tv_iters.tag_class.stamp = -1;
550 tv_iters.tag_member.stamp = -1;
551 tv_iters.tag_macro.stamp = -1;
552 tv_iters.tag_variable.stamp = -1;
553 tv_iters.tag_externvar.stamp = -1;
554 tv_iters.tag_namespace.stamp = -1;
555 tv_iters.tag_struct.stamp = -1;
556 tv_iters.tag_interface.stamp = -1;
557 tv_iters.tag_type.stamp = -1;
558 tv_iters.tag_other.stamp = -1;
562 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
564 static GtkIconTheme *icon_theme = NULL;
565 static gint x = -1;
567 if (G_UNLIKELY(x < 0))
569 gint dummy;
570 icon_theme = gtk_icon_theme_get_default();
571 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
573 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
577 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
579 GtkTreeModel *model = GTK_TREE_MODEL(store);
581 if (!gtk_tree_model_get_iter_first(model, iter))
582 return FALSE;
585 gchar *candidate;
587 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
588 /* FIXME: what if 2 different items have the same name?
589 * this should never happen, but might be caused by a typo in a translation */
590 if (utils_str_equal(candidate, title))
592 g_free(candidate);
593 return TRUE;
595 else
596 g_free(candidate);
598 while (gtk_tree_model_iter_next(model, iter));
600 return FALSE;
604 /* Adds symbol list groups in (iter*, title) pairs.
605 * The list must be ended with NULL. */
606 static void G_GNUC_NULL_TERMINATED
607 tag_list_add_groups(GtkTreeStore *tree_store, ...)
609 va_list args;
610 GtkTreeIter *iter;
612 g_return_if_fail(top_level_iter_names);
614 va_start(args, tree_store);
615 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
617 gchar *title = va_arg(args, gchar*);
618 gchar *icon_name = va_arg(args, gchar *);
619 GdkPixbuf *icon = NULL;
621 if (icon_name)
623 icon = get_tag_icon(icon_name);
626 g_assert(title != NULL);
627 g_ptr_array_add(top_level_iter_names, title);
629 if (!find_toplevel_iter(tree_store, iter, title))
630 gtk_tree_store_append(tree_store, iter, NULL);
632 if (G_IS_OBJECT(icon))
634 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
635 g_object_unref(icon);
637 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
639 va_end(args);
643 static void add_top_level_items(GeanyDocument *doc)
645 filetype_id ft_id = doc->file_type->id;
646 GtkTreeStore *tag_store = doc->priv->tag_store;
648 if (top_level_iter_names == NULL)
649 top_level_iter_names = g_ptr_array_new();
650 else
651 g_ptr_array_set_size(top_level_iter_names, 0);
653 init_tag_iters();
655 switch (ft_id)
657 case GEANY_FILETYPES_DIFF:
659 tag_list_add_groups(tag_store,
660 &(tv_iters.tag_function), _("Files"), NULL, NULL);
661 break;
663 case GEANY_FILETYPES_DOCBOOK:
665 tag_list_add_groups(tag_store,
666 &(tv_iters.tag_function), _("Chapter"), NULL,
667 &(tv_iters.tag_class), _("Section"), NULL,
668 &(tv_iters.tag_member), _("Sect1"), NULL,
669 &(tv_iters.tag_macro), _("Sect2"), NULL,
670 &(tv_iters.tag_variable), _("Sect3"), NULL,
671 &(tv_iters.tag_struct), _("Appendix"), NULL,
672 &(tv_iters.tag_other), _("Other"), NULL,
673 NULL);
674 break;
676 case GEANY_FILETYPES_HASKELL:
677 tag_list_add_groups(tag_store,
678 &tv_iters.tag_namespace, _("Module"), NULL,
679 &tv_iters.tag_type, _("Types"), NULL,
680 &tv_iters.tag_macro, _("Type constructors"), NULL,
681 &tv_iters.tag_function, _("Functions"), "classviewer-method",
682 NULL);
683 break;
684 case GEANY_FILETYPES_COBOL:
685 tag_list_add_groups(tag_store,
686 &tv_iters.tag_class, _("Program"), "classviewer-class",
687 &tv_iters.tag_function, _("File"), "classviewer-method",
688 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
689 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
690 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
691 &tv_iters.tag_variable, _("Data"), "classviewer-var",
692 NULL);
693 break;
694 case GEANY_FILETYPES_CONF:
695 tag_list_add_groups(tag_store,
696 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
697 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
698 NULL);
699 break;
700 case GEANY_FILETYPES_NSIS:
701 tag_list_add_groups(tag_store,
702 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
703 &tv_iters.tag_function, _("Functions"), "classviewer-method",
704 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
705 NULL);
706 break;
707 case GEANY_FILETYPES_LATEX:
709 tag_list_add_groups(tag_store,
710 &(tv_iters.tag_function), _("Command"), NULL,
711 &(tv_iters.tag_class), _("Environment"), NULL,
712 &(tv_iters.tag_member), _("Section"), NULL,
713 &(tv_iters.tag_macro), _("Subsection"), NULL,
714 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
715 &(tv_iters.tag_struct), _("Label"), NULL,
716 &(tv_iters.tag_namespace), _("Chapter"), NULL,
717 &(tv_iters.tag_other), _("Other"), NULL,
718 NULL);
719 break;
721 case GEANY_FILETYPES_MATLAB:
723 tag_list_add_groups(tag_store,
724 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
725 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
726 NULL);
727 break;
729 case GEANY_FILETYPES_ABAQUS:
731 tag_list_add_groups(tag_store,
732 &(tv_iters.tag_class), _("Parts"), NULL,
733 &(tv_iters.tag_member), _("Assembly"), NULL,
734 &(tv_iters.tag_namespace), _("Steps"), NULL,
735 NULL);
736 break;
738 case GEANY_FILETYPES_R:
740 tag_list_add_groups(tag_store,
741 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
742 &(tv_iters.tag_other), _("Other"), NULL,
743 NULL);
744 break;
746 case GEANY_FILETYPES_RUST:
748 tag_list_add_groups(tag_store,
749 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
750 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
751 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
752 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
753 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
754 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
755 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
756 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
757 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
758 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
759 NULL);
760 break;
762 case GEANY_FILETYPES_PERL:
764 tag_list_add_groups(tag_store,
765 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
766 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
767 &(tv_iters.tag_macro), _("Labels"), NULL,
768 &(tv_iters.tag_type), _("Constants"), NULL,
769 &(tv_iters.tag_other), _("Other"), "classviewer-other",
770 NULL);
771 break;
773 case GEANY_FILETYPES_PHP:
775 tag_list_add_groups(tag_store,
776 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
777 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
778 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
779 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
780 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
781 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
782 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
783 NULL);
784 break;
786 case GEANY_FILETYPES_HTML:
788 tag_list_add_groups(tag_store,
789 &(tv_iters.tag_function), _("Functions"), NULL,
790 &(tv_iters.tag_member), _("Anchors"), NULL,
791 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
792 &(tv_iters.tag_class), _("H2 Headings"), NULL,
793 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
794 NULL);
795 break;
797 case GEANY_FILETYPES_CSS:
799 tag_list_add_groups(tag_store,
800 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
801 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
802 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
803 break;
805 case GEANY_FILETYPES_REST:
806 case GEANY_FILETYPES_TXT2TAGS:
807 case GEANY_FILETYPES_ABC:
809 tag_list_add_groups(tag_store,
810 &(tv_iters.tag_namespace), _("Chapter"), NULL,
811 &(tv_iters.tag_member), _("Section"), NULL,
812 &(tv_iters.tag_macro), _("Subsection"), NULL,
813 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
814 NULL);
815 break;
817 case GEANY_FILETYPES_ASCIIDOC:
819 tag_list_add_groups(tag_store,
820 &(tv_iters.tag_namespace), _("Document"), NULL,
821 &(tv_iters.tag_member), _("Section Level 1"), NULL,
822 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
823 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
824 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
825 NULL);
826 break;
828 case GEANY_FILETYPES_RUBY:
830 tag_list_add_groups(tag_store,
831 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
832 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
833 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
834 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
835 NULL);
836 break;
838 case GEANY_FILETYPES_TCL:
840 tag_list_add_groups(tag_store,
841 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
842 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
843 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
844 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
845 NULL);
846 break;
848 case GEANY_FILETYPES_PYTHON:
850 tag_list_add_groups(tag_store,
851 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
852 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
853 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
854 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
855 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
856 NULL);
857 break;
859 case GEANY_FILETYPES_VHDL:
861 tag_list_add_groups(tag_store,
862 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
863 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
864 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
865 &(tv_iters.tag_type), _("Types"), "classviewer-other",
866 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
867 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
868 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
869 &(tv_iters.tag_other), _("Other"), "classviewer-other",
870 NULL);
871 break;
873 case GEANY_FILETYPES_VERILOG:
875 tag_list_add_groups(tag_store,
876 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
877 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
878 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
879 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
880 &(tv_iters.tag_other), _("Other"), "classviewer-other",
881 NULL);
882 break;
884 case GEANY_FILETYPES_JAVA:
886 tag_list_add_groups(tag_store,
887 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
888 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
889 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
890 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
891 &(tv_iters.tag_member), _("Members"), "classviewer-member",
892 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
893 &(tv_iters.tag_other), _("Other"), "classviewer-other",
894 NULL);
895 break;
897 case GEANY_FILETYPES_AS:
899 tag_list_add_groups(tag_store,
900 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
901 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
902 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
903 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
904 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
905 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
906 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
907 &(tv_iters.tag_other), _("Other"), "classviewer-other",
908 NULL);
909 break;
911 case GEANY_FILETYPES_HAXE:
913 tag_list_add_groups(tag_store,
914 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
915 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
916 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
917 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
918 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
919 &(tv_iters.tag_other), _("Other"), "classviewer-other",
920 NULL);
921 break;
923 case GEANY_FILETYPES_BASIC:
925 tag_list_add_groups(tag_store,
926 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
927 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
928 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
929 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
930 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
931 &(tv_iters.tag_other), _("Other"), "classviewer-other",
932 NULL);
933 break;
935 case GEANY_FILETYPES_F77:
936 case GEANY_FILETYPES_FORTRAN:
938 tag_list_add_groups(tag_store,
939 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
940 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
941 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
942 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
943 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
944 &(tv_iters.tag_class), _("Types"), "classviewer-class",
945 &(tv_iters.tag_member), _("Components"), "classviewer-member",
946 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
947 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
948 &(tv_iters.tag_other), _("Other"), "classviewer-other",
949 NULL);
950 break;
952 case GEANY_FILETYPES_ASM:
954 tag_list_add_groups(tag_store,
955 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
956 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
957 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
958 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
959 NULL);
960 break;
962 case GEANY_FILETYPES_MAKE:
963 tag_list_add_groups(tag_store,
964 &tv_iters.tag_function, _("Targets"), "classviewer-method",
965 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
966 NULL);
967 break;
968 case GEANY_FILETYPES_SQL:
970 tag_list_add_groups(tag_store,
971 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
972 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
973 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
974 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
975 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
976 &(tv_iters.tag_member), _("Views"), "classviewer-var",
977 &(tv_iters.tag_other), _("Other"), "classviewer-other",
978 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
979 NULL);
980 break;
982 case GEANY_FILETYPES_D:
983 default:
985 if (ft_id == GEANY_FILETYPES_D)
986 tag_list_add_groups(tag_store,
987 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
988 else
989 tag_list_add_groups(tag_store,
990 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
992 tag_list_add_groups(tag_store,
993 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
994 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
995 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
996 &(tv_iters.tag_member), _("Members"), "classviewer-member",
997 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
998 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
999 NULL);
1001 if (ft_id != GEANY_FILETYPES_D)
1003 tag_list_add_groups(tag_store,
1004 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1006 tag_list_add_groups(tag_store,
1007 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1008 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1009 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1015 /* removes toplevel items that have no children */
1016 static void hide_empty_rows(GtkTreeStore *store)
1018 GtkTreeIter iter;
1019 gboolean cont = TRUE;
1021 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1022 return; /* stop when first iter is invalid, i.e. no elements */
1024 while (cont)
1026 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1027 cont = gtk_tree_store_remove(store, &iter);
1028 else
1029 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1034 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1036 gchar *utf8_name;
1037 const gchar *scope = tag->atts.entry.scope;
1038 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1039 gboolean doc_is_utf8 = FALSE;
1041 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1042 * for None at this point completely */
1043 if (utils_str_equal(doc->encoding, "UTF-8") ||
1044 utils_str_equal(doc->encoding, "None"))
1045 doc_is_utf8 = TRUE;
1046 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1047 * plugin might have called tm_source_file_update(), so check to be sure */
1048 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1050 if (! doc_is_utf8)
1051 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1052 -1, doc->encoding, TRUE);
1053 else
1054 utf8_name = tag->name;
1056 if (utf8_name == NULL)
1057 return NULL;
1059 if (! buffer)
1060 buffer = g_string_new(NULL);
1061 else
1062 g_string_truncate(buffer, 0);
1064 /* check first char of scope is a wordchar */
1065 if (!found_parent && scope &&
1066 strpbrk(scope, GEANY_WORDCHARS) == scope)
1068 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1070 g_string_append(buffer, scope);
1071 g_string_append(buffer, sep);
1073 g_string_append(buffer, utf8_name);
1075 if (! doc_is_utf8)
1076 g_free(utf8_name);
1078 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1080 return buffer->str;
1084 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1086 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1088 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1089 * for None at this point completely */
1090 if (utf8_name != NULL &&
1091 ! utils_str_equal(doc->encoding, "UTF-8") &&
1092 ! utils_str_equal(doc->encoding, "None"))
1094 SETPTR(utf8_name,
1095 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1098 if (utf8_name != NULL)
1099 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1101 return utf8_name;
1105 /* find the last word in "foo::bar::blah", e.g. "blah" */
1106 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1108 const gchar *scope = tag->atts.entry.scope;
1109 const gchar *separator = symbols_get_context_separator(ft_id);
1110 const gchar *str, *ptr;
1112 if (!scope)
1113 return NULL;
1115 str = scope;
1117 while (1)
1119 ptr = strstr(str, separator);
1120 if (ptr)
1122 str = ptr + strlen(separator);
1124 else
1125 break;
1128 return !EMPTY(str) ? str : NULL;
1132 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1134 GtkTreeIter *iter = NULL;
1136 switch (tag_type)
1138 case tm_tag_prototype_t:
1139 case tm_tag_method_t:
1140 case tm_tag_function_t:
1142 iter = &tv_iters.tag_function;
1143 break;
1145 case tm_tag_externvar_t:
1147 iter = &tv_iters.tag_externvar;
1148 break;
1150 case tm_tag_macro_t:
1151 case tm_tag_macro_with_arg_t:
1153 iter = &tv_iters.tag_macro;
1154 break;
1156 case tm_tag_class_t:
1158 iter = &tv_iters.tag_class;
1159 break;
1161 case tm_tag_member_t:
1162 case tm_tag_field_t:
1164 iter = &tv_iters.tag_member;
1165 break;
1167 case tm_tag_typedef_t:
1168 case tm_tag_enum_t:
1170 iter = &tv_iters.tag_type;
1171 break;
1173 case tm_tag_union_t:
1174 case tm_tag_struct_t:
1176 iter = &tv_iters.tag_struct;
1177 break;
1179 case tm_tag_interface_t:
1180 iter = &tv_iters.tag_interface;
1181 break;
1182 case tm_tag_variable_t:
1184 iter = &tv_iters.tag_variable;
1185 break;
1187 case tm_tag_namespace_t:
1188 case tm_tag_package_t:
1190 iter = &tv_iters.tag_namespace;
1191 break;
1193 default:
1195 iter = &tv_iters.tag_other;
1198 if (G_LIKELY(iter->stamp != -1))
1199 return iter;
1200 else
1201 return NULL;
1205 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1207 GdkPixbuf *icon = NULL;
1209 if (parent == &tv_iters.tag_other)
1211 return get_tag_icon("classviewer-var");
1213 /* copy parent icon */
1214 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1215 SYMBOLS_COLUMN_ICON, &icon, -1);
1216 return icon;
1220 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1222 const TMTag *t1 = v1;
1223 const TMTag *t2 = v2;
1225 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1226 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1227 /* include arglist in match to support e.g. C++ overloading */
1228 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1232 /* inspired from g_str_hash() */
1233 static guint tag_hash(gconstpointer v)
1235 const TMTag *tag = v;
1236 const gchar *p;
1237 guint32 h = 5381;
1239 h = (h << 5) + h + tag->type;
1240 for (p = tag->name; *p != '\0'; p++)
1241 h = (h << 5) + h + *p;
1242 if (tag->atts.entry.scope)
1244 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1245 h = (h << 5) + h + *p;
1247 /* for e.g. C++ overloading */
1248 if (tag->atts.entry.arglist)
1250 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1251 h = (h << 5) + h + *p;
1254 return h;
1258 /* like gtk_tree_view_expand_to_path() but with an iter */
1259 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1261 GtkTreeModel *model = gtk_tree_view_get_model(view);
1262 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1264 gtk_tree_view_expand_to_path(view, path);
1265 gtk_tree_path_free(path);
1269 /* like gtk_tree_store_remove() but finds the next iter at any level */
1270 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1272 GtkTreeIter parent;
1273 gboolean has_parent;
1274 gboolean cont;
1276 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1277 cont = gtk_tree_store_remove(store, iter);
1278 /* if there is no next at this level but there is a parent iter, continue from it */
1279 if (! cont && has_parent)
1281 *iter = parent;
1282 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1285 return cont;
1289 /* adds a new element in the parent table if it's key is known.
1290 * duplicates are kept */
1291 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1292 const GtkTreeIter *iter)
1294 GList **list;
1295 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1296 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1298 if (! list)
1300 list = g_slice_alloc(sizeof *list);
1301 *list = NULL;
1302 g_hash_table_insert(table, tag->name, list);
1304 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1309 static void free_iter_slice_list(gpointer data)
1311 GList **list = data;
1313 if (list)
1315 GList *node;
1316 foreach_list(node, *list)
1317 g_slice_free(GtkTreeIter, node->data);
1318 g_list_free(*list);
1319 g_slice_free1(sizeof *list, list);
1324 /* inserts a @data in @table on key @tag.
1325 * previous data is not overwritten if the key is duplicated, but rather the
1326 * two values are kept in a list
1328 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1329 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1331 GList *list = g_hash_table_lookup(table, tag);
1332 list = g_list_prepend(list, data);
1333 g_hash_table_insert(table, tag, list);
1337 /* looks up the entry in @table that better matches @tag.
1338 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1339 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1341 GList *data = NULL;
1342 GList *node = g_hash_table_lookup(table, tag);
1343 if (node)
1345 glong delta;
1346 data = node->data;
1348 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1350 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1351 for (node = node->next; node; node = node->next)
1353 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1355 if (d < delta)
1357 data = node->data;
1358 delta = d;
1362 #undef TAG_DELTA
1365 return data;
1369 /* removes the element at @tag from @table.
1370 * @tag must be the exact pointer used at insertion time */
1371 static void tags_table_remove(GHashTable *table, TMTag *tag)
1373 GList *list = g_hash_table_lookup(table, tag);
1374 if (list)
1376 GList *node;
1377 foreach_list(node, list)
1379 if (((GList *) node->data)->data == tag)
1380 break;
1382 list = g_list_delete_link(list, node);
1383 if (list)
1384 g_hash_table_insert(table, tag, list);
1385 else
1386 g_hash_table_remove(table, tag);
1391 static void tags_table_destroy(GHashTable *table)
1393 /* free any leftover elements. note that we can't register a value_free_func when
1394 * creating the hash table because we only want to free it when destroying the table,
1395 * not when inserting a duplicate (we handle this manually) */
1396 GHashTableIter iter;
1397 gpointer value;
1399 g_hash_table_iter_init(&iter, table);
1400 while (g_hash_table_iter_next(&iter, NULL, &value))
1401 g_list_free(value);
1402 g_hash_table_destroy(table);
1407 * Updates the tag tree for a document with the tags in *list.
1408 * @param doc a document
1409 * @param tags a pointer to a GList* holding the tags to add/update. This
1410 * list may be updated, removing updated elements.
1412 * The update is done in two passes:
1413 * 1) walking the current tree, update tags that still exist and remove the
1414 * obsolescent ones;
1415 * 2) walking the remaining (non updated) tags, adds them in the list.
1417 * For better performances, we use 2 hash tables:
1418 * - one containing all the tags for lookup in the first pass (actually stores a
1419 * reference in the tags list for removing it efficiently), avoiding list search
1420 * on each tag;
1421 * - the other holding "tag-name":row references for tags having children, used to
1422 * lookup for a parent in both passes, avoiding tree traversal.
1424 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1426 GtkTreeStore *store = doc->priv->tag_store;
1427 GtkTreeModel *model = GTK_TREE_MODEL(store);
1428 GHashTable *parents_table;
1429 GHashTable *tags_table;
1430 GtkTreeIter iter;
1431 gboolean cont;
1432 GList *item;
1434 /* Build hash tables holding tags and parents */
1435 /* parent table holds "tag-name":GtkTreeIter */
1436 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1437 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1438 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1439 foreach_list(item, *tags)
1441 TMTag *tag = item->data;
1442 const gchar *name;
1444 tags_table_insert(tags_table, tag, item);
1446 name = get_parent_name(tag, doc->file_type->id);
1447 if (name)
1448 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1451 /* First pass, update existing rows or delete them.
1452 * It is OK to delete them since we walk top down so we would remove
1453 * parents before checking for their children, thus never implicitly
1454 * deleting an updated child */
1455 cont = gtk_tree_model_get_iter_first(model, &iter);
1456 while (cont)
1458 TMTag *tag;
1460 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1461 if (! tag) /* most probably a toplevel, skip it */
1462 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1463 else
1465 GList *found_item;
1467 found_item = tags_table_lookup(tags_table, tag);
1468 if (! found_item) /* tag doesn't exist, remove it */
1469 cont = tree_store_remove_row(store, &iter);
1470 else /* tag still exist, update it */
1472 const gchar *name;
1473 const gchar *parent_name;
1474 TMTag *found = found_item->data;
1476 parent_name = get_parent_name(found, doc->file_type->id);
1477 /* if parent is unknown, ignore it */
1478 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1479 parent_name = NULL;
1481 /* only update fields that (can) have changed (name that holds line
1482 * number, and the tag itself) */
1483 name = get_symbol_name(doc, found, parent_name != NULL);
1484 gtk_tree_store_set(store, &iter,
1485 SYMBOLS_COLUMN_NAME, name,
1486 SYMBOLS_COLUMN_TAG, found,
1487 -1);
1489 update_parents_table(parents_table, found, parent_name, &iter);
1491 /* remove the updated tag from the table and list */
1492 tags_table_remove(tags_table, found);
1493 *tags = g_list_delete_link(*tags, found_item);
1495 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1498 tm_tag_unref(tag);
1502 /* Second pass, now we have a tree cleaned up from invalid rows,
1503 * we simply add new ones */
1504 foreach_list (item, *tags)
1506 TMTag *tag = item->data;
1507 GtkTreeIter *parent;
1509 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1510 if (G_UNLIKELY(! parent))
1511 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1512 else
1514 gboolean expand;
1515 const gchar *name;
1516 const gchar *parent_name;
1517 gchar *tooltip;
1518 GdkPixbuf *icon = get_child_icon(store, parent);
1520 parent_name = get_parent_name(tag, doc->file_type->id);
1521 if (parent_name)
1523 GList **candidates;
1524 GtkTreeIter *parent_search = NULL;
1526 /* walk parent candidates to find the better one.
1527 * if there are more than one, take the one that has the closest line number
1528 * after the tag we're searching the parent for */
1529 candidates = g_hash_table_lookup(parents_table, parent_name);
1530 if (candidates)
1532 GList *node;
1533 glong delta = G_MAXLONG;
1534 foreach_list(node, *candidates)
1536 TMTag *parent_tag;
1537 glong d;
1539 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1540 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1542 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1543 if (! parent_search || (d >= 0 && d < delta))
1545 delta = d;
1546 parent_search = node->data;
1548 tm_tag_unref(parent_tag);
1552 if (parent_search)
1553 parent = parent_search;
1554 else
1555 parent_name = NULL;
1558 /* only expand to the iter if the parent was empty, otherwise we let the
1559 * folding as it was before (already expanded, or closed by the user) */
1560 expand = ! gtk_tree_model_iter_has_child(model, parent);
1562 /* insert the new element */
1563 gtk_tree_store_append(store, &iter, parent);
1564 name = get_symbol_name(doc, tag, parent_name != NULL);
1565 tooltip = get_symbol_tooltip(doc, tag);
1566 gtk_tree_store_set(store, &iter,
1567 SYMBOLS_COLUMN_NAME, name,
1568 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1569 SYMBOLS_COLUMN_ICON, icon,
1570 SYMBOLS_COLUMN_TAG, tag,
1571 -1);
1572 g_free(tooltip);
1573 if (G_LIKELY(icon))
1574 g_object_unref(icon);
1576 update_parents_table(parents_table, tag, parent_name, &iter);
1578 if (expand)
1579 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1583 g_hash_table_destroy(parents_table);
1584 tags_table_destroy(tags_table);
1588 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1589 * is not stable, so the order is already lost. */
1590 static gint compare_top_level_names(const gchar *a, const gchar *b)
1592 guint i;
1593 const gchar *name;
1595 /* This should never happen as it would mean that two or more top
1596 * level items have the same name but it can happen by typos in the translations. */
1597 if (utils_str_equal(a, b))
1598 return 1;
1600 foreach_ptr_array(name, i, top_level_iter_names)
1602 if (utils_str_equal(name, a))
1603 return -1;
1604 if (utils_str_equal(name, b))
1605 return 1;
1607 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1608 return 0;
1612 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1613 GtkTreeIter *iter)
1615 /* if the tag has a parent tag, it should be at depth >= 2 */
1616 return !EMPTY(tag->atts.entry.scope) &&
1617 gtk_tree_store_iter_depth(store, iter) == 1;
1621 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1622 gpointer user_data)
1624 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1625 TMTag *tag_a, *tag_b;
1626 gint cmp;
1628 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1629 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1631 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1632 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1633 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1634 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1636 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1637 compare_symbol_lines(tag_a, tag_b);
1639 else
1641 gchar *astr, *bstr;
1643 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1644 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1646 /* if a is toplevel, b must be also */
1647 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1649 cmp = compare_top_level_names(astr, bstr);
1651 else
1653 /* this is what g_strcmp0() does */
1654 if (! astr)
1655 cmp = -(astr != bstr);
1656 else if (! bstr)
1657 cmp = astr != bstr;
1658 else
1660 cmp = strcmp(astr, bstr);
1662 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1663 if (tag_a && tag_b)
1664 if (!sort_by_name ||
1665 (utils_str_equal(tag_a->name, tag_b->name) &&
1666 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1667 cmp = compare_symbol_lines(tag_a, tag_b);
1670 g_free(astr);
1671 g_free(bstr);
1673 tm_tag_unref(tag_a);
1674 tm_tag_unref(tag_b);
1676 return cmp;
1680 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1682 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1683 GINT_TO_POINTER(sort_by_name), NULL);
1685 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1689 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1691 GList *tags;
1693 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1695 tags = get_tag_list(doc, tm_tag_max_t);
1696 if (tags == NULL)
1697 return FALSE;
1699 /* FIXME: Not sure why we detached the model here? */
1701 /* disable sorting during update because the code doesn't support correctly
1702 * models that are currently being built */
1703 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1705 /* add grandparent type iters */
1706 add_top_level_items(doc);
1708 update_tree_tags(doc, &tags);
1709 g_list_free(tags);
1711 hide_empty_rows(doc->priv->tag_store);
1713 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1714 sort_mode = doc->priv->symbol_list_sort_mode;
1716 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1717 doc->priv->symbol_list_sort_mode = sort_mode;
1719 return TRUE;
1723 /* Detects a global tags filetype from the *.lang.* language extension.
1724 * Returns NULL if there was no matching TM language. */
1725 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1727 gchar *tags_ext;
1728 gchar *shortname = utils_strdupa(utf8_filename);
1729 GeanyFiletype *ft = NULL;
1731 tags_ext = g_strrstr(shortname, ".tags");
1732 if (tags_ext)
1734 *tags_ext = '\0'; /* remove .tags extension */
1735 ft = filetypes_detect_from_extension(shortname);
1736 if (ft->id != GEANY_FILETYPES_NONE)
1737 return ft;
1739 return NULL;
1743 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1744 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1745 * the relevant path.
1746 * Example:
1747 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1748 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1750 /* -E pre-process, -dD output user macros, -p prof info (?) */
1751 const char pre_process[] = "gcc -E -dD -p -I.";
1753 if (argc > 2)
1755 /* Create global taglist */
1756 int status;
1757 char *command;
1758 const char *tags_file = argv[1];
1759 char *utf8_fname;
1760 GeanyFiletype *ft;
1762 utf8_fname = utils_get_utf8_from_locale(tags_file);
1763 ft = detect_global_tags_filetype(utf8_fname);
1764 g_free(utf8_fname);
1766 if (ft == NULL)
1768 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1769 return 1;
1771 /* load config in case of custom filetypes */
1772 filetypes_load_config(ft->id, FALSE);
1774 /* load ignore list for C/C++ parser */
1775 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1776 load_c_ignore_tags();
1778 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1780 const gchar *cflags = getenv("CFLAGS");
1781 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1783 else
1784 command = NULL; /* don't preprocess */
1786 geany_debug("Generating %s tags file.", ft->name);
1787 tm_get_workspace();
1788 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1789 argc - 2, tags_file, ft->lang);
1790 g_free(command);
1791 symbols_finalize(); /* free c_tags_ignore data */
1792 if (! status)
1794 g_printerr(_("Failed to create tags file, perhaps because no tags "
1795 "were found.\n"));
1796 return 1;
1799 else
1801 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1802 g_printerr(_("Example:\n"
1803 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1804 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1805 return 1;
1807 return 0;
1811 void symbols_show_load_tags_dialog(void)
1813 GtkWidget *dialog;
1814 GtkFileFilter *filter;
1816 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1817 GTK_FILE_CHOOSER_ACTION_OPEN,
1818 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1819 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1820 NULL);
1821 gtk_widget_set_name(dialog, "GeanyDialog");
1822 filter = gtk_file_filter_new();
1823 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1824 gtk_file_filter_add_pattern(filter, "*.*.tags");
1825 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1827 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1829 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1830 GSList *item;
1832 for (item = flist; item != NULL; item = g_slist_next(item))
1834 gchar *fname = item->data;
1835 gchar *utf8_fname;
1836 GeanyFiletype *ft;
1838 utf8_fname = utils_get_utf8_from_locale(fname);
1839 ft = detect_global_tags_filetype(utf8_fname);
1841 if (ft != NULL && symbols_load_global_tags(fname, ft))
1842 /* For translators: the first wildcard is the filetype, the second the filename */
1843 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1844 filetypes_get_display_name(ft), utf8_fname);
1845 else
1846 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1848 g_free(utf8_fname);
1849 g_free(fname);
1851 g_slist_free(flist);
1853 gtk_widget_destroy(dialog);
1857 static void init_user_tags(void)
1859 GSList *file_list = NULL, *list = NULL;
1860 const GSList *node;
1861 gchar *dir;
1863 dir = g_build_filename(app->configdir, "tags", NULL);
1864 /* create the user tags dir for next time if it doesn't exist */
1865 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1866 utils_mkdir(dir, FALSE);
1867 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1869 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1870 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1871 g_free(dir);
1873 file_list = g_slist_concat(file_list, list);
1875 /* populate the filetype-specific tag files lists */
1876 for (node = file_list; node != NULL; node = node->next)
1878 gchar *fname = node->data;
1879 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1880 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1882 g_free(utf8_fname);
1884 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1885 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1886 else
1888 geany_debug("Unknown filetype for file '%s'.", fname);
1889 g_free(fname);
1893 /* don't need to delete list contents because they are now stored in
1894 * ft->priv->tag_files */
1895 g_slist_free(file_list);
1899 static void load_user_tags(filetype_id ft_id)
1901 static guchar *tags_loaded = NULL;
1902 static gboolean init_tags = FALSE;
1903 const GSList *node;
1904 GeanyFiletype *ft = filetypes[ft_id];
1906 g_return_if_fail(ft_id > 0);
1908 if (!tags_loaded)
1909 tags_loaded = g_new0(guchar, filetypes_array->len);
1910 if (tags_loaded[ft_id])
1911 return;
1912 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1914 if (!init_tags)
1916 init_user_tags();
1917 init_tags = TRUE;
1920 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1922 const gchar *fname = node->data;
1924 symbols_load_global_tags(fname, ft);
1929 static gboolean goto_tag(const gchar *name, gboolean definition)
1931 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1932 guint type;
1933 TMTag *tmtag = NULL;
1934 GeanyDocument *old_doc = document_get_current();
1936 /* goto tag definition: all except prototypes / forward declarations / externs */
1937 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1939 /* first look in the current document */
1940 if (old_doc != NULL && old_doc->tm_file)
1941 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1943 /* if not found, look in the workspace */
1944 if (tmtag == NULL)
1945 tmtag = find_workspace_tag(name, type);
1947 if (tmtag != NULL)
1949 GeanyDocument *new_doc = document_find_by_real_path(
1950 tmtag->atts.entry.file->file_name);
1952 if (new_doc)
1954 /* If we are already on the tag line, swap definition/declaration */
1955 if (new_doc == old_doc &&
1956 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1958 if (goto_tag(name, !definition))
1959 return TRUE;
1962 else
1964 /* not found in opened document, should open */
1965 new_doc = document_open_file(tmtag->atts.entry.file->file_name, FALSE, NULL, NULL);
1968 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1969 return TRUE;
1971 return FALSE;
1975 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1977 if (goto_tag(name, definition))
1978 return TRUE;
1980 /* if we are here, there was no match and we are beeping ;-) */
1981 utils_beep();
1983 if (!definition)
1984 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1985 else
1986 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1987 return FALSE;
1991 /* This could perhaps be improved to check for #if, class etc. */
1992 static gint get_function_fold_number(GeanyDocument *doc)
1994 /* for Java the functions are always one fold level above the class scope */
1995 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1996 return SC_FOLDLEVELBASE + 1;
1997 else
1998 return SC_FOLDLEVELBASE;
2002 /* Should be used only with get_current_tag_cached.
2003 * tag_types caching might trigger recomputation too often but this isn't used differently often
2004 * enough to be an issue for now */
2005 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2007 static gint old_line = -2;
2008 static GeanyDocument *old_doc = NULL;
2009 static gint old_fold_num = -1;
2010 static guint old_tag_types = 0;
2011 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2012 gboolean ret;
2014 /* check if the cached line and file index have changed since last time: */
2015 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2016 ret = TRUE;
2017 else if (cur_line == old_line)
2018 ret = FALSE;
2019 else
2021 /* if the line has only changed by 1 */
2022 if (abs(cur_line - old_line) == 1)
2024 /* It's the same function if the fold number hasn't changed */
2025 ret = (fold_num != old_fold_num);
2027 else ret = TRUE;
2030 /* record current line and file index for next time */
2031 old_line = cur_line;
2032 old_doc = doc;
2033 old_fold_num = fold_num;
2034 old_tag_types = tag_types;
2035 return ret;
2039 /* Parse the function name up to 2 lines before tag_line.
2040 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2041 * type or argument names can be confused with the function name. */
2042 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2044 gint start, end, max_pos;
2045 gint fn_style;
2047 switch (sci_get_lexer(sci))
2049 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2050 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2051 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2053 start = sci_get_position_from_line(sci, tag_line - 2);
2054 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2055 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2056 start++;
2058 end = start;
2059 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2060 end++;
2062 if (start == end)
2063 return NULL;
2064 return sci_get_contents_range(sci, start, end);
2068 /* Parse the function name */
2069 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2071 gint start, end, first_pos, max_pos;
2072 gint tmp;
2073 gchar c;
2075 first_pos = end = sci_get_position_from_line(sci, tag_line);
2076 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2077 tmp = 0;
2078 /* goto the begin of function body */
2079 while (end < max_pos &&
2080 (tmp = sci_get_char_at(sci, end)) != '{' &&
2081 tmp != 0) end++;
2082 if (tmp == 0) end --;
2084 /* go back to the end of function identifier */
2085 while (end > 0 && end > first_pos - 500 &&
2086 (tmp = sci_get_char_at(sci, end)) != '(' &&
2087 tmp != 0) end--;
2088 end--;
2089 if (end < 0) end = 0;
2091 /* skip whitespaces between identifier and ( */
2092 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2094 start = end;
2095 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2096 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2097 || tmp == SCE_C_GLOBALCLASS
2098 || (c = sci_get_char_at(sci, start)) == '~'
2099 || c == ':'))
2100 start--;
2101 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2103 if (start == end) return NULL;
2104 return sci_get_contents_range(sci, start, end + 1);
2108 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2110 gint line_count = sci_get_line_count(sci);
2112 for (; line < line_count; line++)
2114 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2115 return line;
2118 return -1;
2122 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2124 gint line;
2125 gint parent;
2127 line = sci_get_current_line(doc->editor->sci);
2128 parent = sci_get_fold_parent(doc->editor->sci, line);
2129 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2130 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2131 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2133 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2135 if (tag)
2137 gint tag_line = tag->atts.entry.line - 1;
2138 gint last_child = line + 1;
2140 /* if it may be a false positive because we're inside a fold level not inside anything
2141 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2142 * right after the tag we got from TM */
2143 if (abs(tag_line - parent) > 1)
2145 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2146 if (tag_fold >= 0)
2147 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2150 if (line <= last_child)
2152 if (tag->atts.entry.scope)
2153 *tagname = g_strconcat(tag->atts.entry.scope,
2154 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2155 else
2156 *tagname = g_strdup(tag->name);
2158 return tag_line;
2162 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2163 * to dirty and inaccurate hand-parsing */
2164 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2166 const gint fn_fold = get_function_fold_number(doc);
2167 gint tag_line = parent;
2168 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2170 /* find the top level fold point */
2171 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2173 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2174 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2177 if (tag_line >= 0)
2179 gchar *cur_tag;
2181 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2182 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2183 else
2184 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2186 if (cur_tag != NULL)
2188 *tagname = cur_tag;
2189 return tag_line;
2194 *tagname = g_strdup(_("unknown"));
2195 return -1;
2199 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2201 static gint tag_line = -1;
2202 static gchar *cur_tag = NULL;
2204 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2206 if (doc == NULL) /* reset current function */
2208 current_tag_changed(NULL, -1, -1, 0);
2209 g_free(cur_tag);
2210 cur_tag = g_strdup(_("unknown"));
2211 if (tagname != NULL)
2212 *tagname = cur_tag;
2213 tag_line = -1;
2215 else
2217 gint line = sci_get_current_line(doc->editor->sci);
2218 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2220 if (current_tag_changed(doc, line, fold_level, tag_types))
2222 g_free(cur_tag);
2223 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2225 *tagname = cur_tag;
2228 return tag_line;
2232 /* Sets *tagname to point at the current function or tag name.
2233 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2234 * call to this function.
2235 * Returns: line number of the current tag, or -1 if unknown. */
2236 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2238 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2242 /* same as symbols_get_current_function() but finds class, namespaces and more */
2243 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2245 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2246 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2248 /* Python parser reports imports as namespaces which confuses the scope detection */
2249 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2250 tag_types |= tm_tag_namespace_t;
2252 return get_current_tag_name_cached(doc, tagname, tag_types);
2256 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2258 gint sort_mode = GPOINTER_TO_INT(user_data);
2259 GeanyDocument *doc = document_get_current();
2261 if (ignore_callback)
2262 return;
2264 if (doc != NULL)
2265 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2269 static void on_symbol_tree_menu_show(GtkWidget *widget,
2270 gpointer user_data)
2272 GeanyDocument *doc = document_get_current();
2273 gboolean enable;
2275 enable = doc && doc->has_tags;
2276 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2277 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2278 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2279 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2280 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2281 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2283 if (! doc)
2284 return;
2286 ignore_callback = TRUE;
2288 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2289 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2290 else
2291 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2293 ignore_callback = FALSE;
2297 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2299 gboolean expand = GPOINTER_TO_INT(user_data);
2300 GeanyDocument *doc = document_get_current();
2302 if (! doc)
2303 return;
2305 g_return_if_fail(doc->priv->tag_tree);
2307 if (expand)
2308 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2309 else
2310 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2314 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2316 GtkTreeIter iter;
2317 GtkTreeSelection *selection;
2318 GtkTreeModel *model;
2319 GeanyDocument *doc;
2320 TMTag *tag = NULL;
2322 doc = document_get_current();
2323 if (!doc)
2324 return;
2326 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2327 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2328 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2329 if (tag)
2331 if (widget == symbol_menu.find_in_files)
2332 search_show_find_in_files_dialog_full(tag->name, NULL);
2333 else
2334 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2335 widget == symbol_menu.find_usage);
2337 tm_tag_unref(tag);
2342 static void create_taglist_popup_menu(void)
2344 GtkWidget *item, *menu;
2346 tv.popup_taglist = menu = gtk_menu_new();
2348 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2349 gtk_widget_show(item);
2350 gtk_container_add(GTK_CONTAINER(menu), item);
2351 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2353 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2354 gtk_widget_show(item);
2355 gtk_container_add(GTK_CONTAINER(menu), item);
2356 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2358 item = gtk_separator_menu_item_new();
2359 gtk_widget_show(item);
2360 gtk_container_add(GTK_CONTAINER(menu), item);
2362 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2363 _("Sort by _Name"));
2364 gtk_widget_show(item);
2365 gtk_container_add(GTK_CONTAINER(menu), item);
2366 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2367 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2369 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2370 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2371 gtk_widget_show(item);
2372 gtk_container_add(GTK_CONTAINER(menu), item);
2373 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2374 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2376 item = gtk_separator_menu_item_new();
2377 gtk_widget_show(item);
2378 gtk_container_add(GTK_CONTAINER(menu), item);
2380 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2381 gtk_widget_show(item);
2382 gtk_container_add(GTK_CONTAINER(menu), item);
2383 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2385 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2386 gtk_widget_show(item);
2387 gtk_container_add(GTK_CONTAINER(menu), item);
2388 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2390 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2391 gtk_widget_show(item);
2392 gtk_container_add(GTK_CONTAINER(menu), item);
2393 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2395 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2397 sidebar_add_common_menu_items(GTK_MENU(menu));
2401 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2403 gchar *f;
2405 g_return_if_fail(!EMPTY(doc->real_path));
2407 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2408 if (utils_str_equal(doc->real_path, f))
2409 load_c_ignore_tags();
2411 g_free(f);
2415 void symbols_init(void)
2417 gchar *f;
2419 create_taglist_popup_menu();
2421 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2422 ui_add_config_file_menu_item(f, NULL, NULL);
2423 g_free(f);
2425 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2429 void symbols_finalize(void)
2431 g_strfreev(html_entities);
2432 g_strfreev(c_tags_ignore);