c: Add missing size_t type to the C99 tag file
[geany-mirror.git] / src / symbols.c
bloba7fb597e064f0a34a8b0d0e728ae91901d3fcef3
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include "symbols.h"
41 #include "app.h"
42 #include "callbacks.h" /* FIXME: for ignore_callback */
43 #include "documentprivate.h"
44 #include "editor.h"
45 #include "encodings.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "sciwrappers.h"
51 #include "sidebar.h"
52 #include "support.h"
53 #include "tm_parser.h"
54 #include "tm_tag.h"
55 #include "ui_utils.h"
56 #include "utils.h"
58 #include "SciLexer.h"
60 #include "gtkcompat.h"
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdlib.h>
67 static gchar **html_entities = NULL;
69 typedef struct
71 gboolean tags_loaded;
72 const gchar *tag_file;
73 } TagFileInfo;
75 /* Check before adding any more tags files, usually they should be downloaded separately. */
76 enum /* Geany tag files */
78 GTF_C,
79 GTF_PASCAL,
80 GTF_PHP,
81 GTF_HTML_ENTITIES,
82 GTF_LATEX,
83 GTF_PYTHON,
84 GTF_MAX
87 static TagFileInfo tag_file_info[GTF_MAX] =
89 {FALSE, "c99.tags"},
90 {FALSE, "pascal.tags"},
91 {FALSE, "php.tags"},
92 {FALSE, "html_entities.tags"},
93 {FALSE, "latex.tags"},
94 {FALSE, "python.tags"}
97 static GPtrArray *top_level_iter_names = NULL;
99 static struct
101 GtkWidget *expand_all;
102 GtkWidget *collapse_all;
103 GtkWidget *sort_by_name;
104 GtkWidget *sort_by_appearance;
105 GtkWidget *find_usage;
106 GtkWidget *find_doc_usage;
107 GtkWidget *find_in_files;
109 symbol_menu;
112 static void html_tags_loaded(void);
113 static void load_user_tags(filetype_id ft_id);
115 /* get the tags_ignore list, exported by tagmanager's options.c */
116 extern gchar **c_tags_ignore;
118 /* ignore certain tokens when parsing C-like syntax.
119 * Also works for reloading. */
120 static void load_c_ignore_tags(void)
122 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
123 gchar *content;
125 if (g_file_get_contents(path, &content, NULL, NULL))
127 /* historically we ignore the glib _DECLS for tag generation */
128 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
130 g_strfreev(c_tags_ignore);
131 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
132 g_free(content);
134 g_free(path);
138 void symbols_reload_config_files(void)
140 load_c_ignore_tags();
144 static gsize get_tag_count(void)
146 GPtrArray *tags = tm_get_workspace()->global_tags;
147 gsize count = tags ? tags->len : 0;
149 return count;
153 /* wrapper for tm_workspace_load_global_tags().
154 * note that the tag count only counts new global tags added - if a tag has the same name,
155 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
156 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
158 gboolean result;
159 gsize old_tag_count = get_tag_count();
161 result = tm_workspace_load_global_tags(tags_file, ft->lang);
162 if (result)
164 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
165 (guint) (get_tag_count() - old_tag_count));
167 return result;
171 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
172 * This provides autocompletion, calltips, etc. */
173 void symbols_global_tags_loaded(guint file_type_idx)
175 TagFileInfo *tfi;
176 gint tag_type;
178 /* load ignore list for C/C++ parser */
179 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
180 c_tags_ignore == NULL)
182 load_c_ignore_tags();
185 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
186 return;
188 /* load config in case of custom filetypes */
189 filetypes_load_config(file_type_idx, FALSE);
191 load_user_tags(file_type_idx);
193 switch (file_type_idx)
195 case GEANY_FILETYPES_PHP:
196 case GEANY_FILETYPES_HTML:
197 html_tags_loaded();
199 switch (file_type_idx)
201 case GEANY_FILETYPES_CPP:
202 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
203 /* no C++ tagfile yet */
204 return;
205 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
206 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
207 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
208 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
209 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
210 default:
211 return;
213 tfi = &tag_file_info[tag_type];
215 if (! tfi->tags_loaded)
217 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
219 symbols_load_global_tags(fname, filetypes[file_type_idx]);
220 tfi->tags_loaded = TRUE;
221 g_free(fname);
226 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
227 static void html_tags_loaded(void)
229 TagFileInfo *tfi;
231 if (cl_options.ignore_global_tags)
232 return;
234 tfi = &tag_file_info[GTF_HTML_ENTITIES];
235 if (! tfi->tags_loaded)
237 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
239 html_entities = utils_read_file_in_array(file);
240 tfi->tags_loaded = TRUE;
241 g_free(file);
246 GString *symbols_find_typenames_as_string(gint lang, gboolean global)
248 guint j;
249 TMTag *tag;
250 GString *s = NULL;
251 GPtrArray *typedefs;
252 gint tag_lang;
254 if (global)
255 typedefs = tm_tags_extract(app->tm_workspace->global_tags, TM_GLOBAL_TYPE_MASK);
256 else
257 typedefs = app->tm_workspace->typename_array;
259 if ((typedefs) && (typedefs->len > 0))
261 s = g_string_sized_new(typedefs->len * 10);
262 for (j = 0; j < typedefs->len; ++j)
264 tag = TM_TAG(typedefs->pdata[j]);
265 tag_lang = tag->lang;
267 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
268 * e.g. PHP classes in C++ files
269 * lang = -2 disables the check */
270 if (tag->name && (tag_lang == lang || lang == -2 ||
271 (lang == TM_PARSER_CPP && tag_lang == TM_PARSER_C)))
273 if (j != 0)
274 g_string_append_c(s, ' ');
275 g_string_append(s, tag->name);
279 if (typedefs && global)
280 g_ptr_array_free(typedefs, TRUE);
281 return s;
285 /** Gets the context separator used by the tag manager for a particular file
286 * type.
287 * @param ft_id File type identifier.
288 * @return The context separator string.
290 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
291 * without a context separator.
293 * @since 0.19
295 const gchar *symbols_get_context_separator(gint ft_id)
297 switch (ft_id)
299 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
300 case GEANY_FILETYPES_CPP:
301 case GEANY_FILETYPES_GLSL: /* for structs */
302 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
303 case GEANY_FILETYPES_PHP:
304 case GEANY_FILETYPES_RUST:
305 return "::";
307 /* avoid confusion with other possible separators in group/section name */
308 case GEANY_FILETYPES_CONF:
309 case GEANY_FILETYPES_REST:
310 return ":::";
312 /* no context separator */
313 case GEANY_FILETYPES_ASCIIDOC:
314 case GEANY_FILETYPES_TXT2TAGS:
315 return "\x03";
317 default:
318 return ".";
323 GString *symbols_get_macro_list(gint lang)
325 guint j, i;
326 GPtrArray *ftags;
327 GString *words;
328 gint tag_lang;
329 TMTag *tag;
331 if (app->tm_workspace->source_files == NULL)
332 return NULL;
334 ftags = g_ptr_array_sized_new(50);
335 words = g_string_sized_new(200);
337 for (j = 0; j < app->tm_workspace->source_files->len; j++)
339 GPtrArray *tags;
341 tags = tm_tags_extract(TM_SOURCE_FILE(app->tm_workspace->source_files->pdata[j])->tags_array,
342 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
343 if (NULL != tags)
345 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
347 tag = TM_TAG(tags->pdata[i]);
348 tag_lang = (tag->file) ?
349 tag->file->lang : tag->lang;
351 if (tag_lang == lang)
352 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
354 g_ptr_array_free(tags, TRUE);
358 if (ftags->len == 0)
360 g_ptr_array_free(ftags, TRUE);
361 g_string_free(words, TRUE);
362 return NULL;
365 tm_tags_sort(ftags, NULL, FALSE, FALSE);
366 for (j = 0; j < ftags->len; j++)
368 if (j > 0)
369 g_string_append_c(words, '\n');
370 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
372 g_ptr_array_free(ftags, TRUE);
373 return words;
377 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
378 static TMTag *
379 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
381 guint i;
382 g_return_val_if_fail(tags != NULL, NULL);
384 for (i = 0; i < tags->len; ++i)
386 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
387 return TM_TAG(tags->pdata[i]);
389 return NULL;
393 static TMTag *find_source_file_tag(GPtrArray *tags_array,
394 const gchar *tag_name, guint type)
396 GPtrArray *tags;
397 TMTag *tmtag;
399 tags = tm_tags_extract(tags_array, type);
400 if (tags != NULL)
402 tmtag = symbols_find_tm_tag(tags, tag_name);
404 g_ptr_array_free(tags, TRUE);
406 if (tmtag != NULL)
407 return tmtag;
409 return NULL; /* not found */
413 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
415 guint j;
416 const GPtrArray *source_files = NULL;
418 if (app->tm_workspace != NULL)
419 source_files = app->tm_workspace->source_files;
421 if (source_files != NULL)
423 for (j = 0; j < source_files->len; j++)
425 TMSourceFile *srcfile = source_files->pdata[j];
426 TMTag *tmtag;
428 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
429 if (tmtag != NULL)
430 return tmtag;
433 return NULL; /* not found */
437 const gchar **symbols_get_html_entities(void)
439 if (html_entities == NULL)
440 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
442 return (const gchar **) html_entities;
446 /* sort by name, then line */
447 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
449 gint ret;
451 if (tag_a == NULL || tag_b == NULL)
452 return 0;
454 if (tag_a->name == NULL)
455 return -(tag_a->name != tag_b->name);
457 if (tag_b->name == NULL)
458 return tag_a->name != tag_b->name;
460 ret = strcmp(tag_a->name, tag_b->name);
461 if (ret == 0)
463 return tag_a->line - tag_b->line;
465 return ret;
469 /* sort by line, then scope */
470 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
472 const TMTag *tag_a = TM_TAG(a);
473 const TMTag *tag_b = TM_TAG(b);
474 gint ret;
476 if (a == NULL || b == NULL)
477 return 0;
479 ret = tag_a->line - tag_b->line;
480 if (ret == 0)
482 if (tag_a->scope == NULL)
483 return -(tag_a->scope != tag_b->scope);
484 if (tag_b->scope == NULL)
485 return tag_a->scope != tag_b->scope;
486 else
487 return strcmp(tag_a->scope, tag_b->scope);
489 return ret;
493 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
495 GList *tag_names = NULL;
496 TMTag *tag;
497 guint i;
499 g_return_val_if_fail(doc, NULL);
501 if (! doc->tm_file || ! doc->tm_file->tags_array)
502 return NULL;
504 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
506 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
507 if (G_UNLIKELY(tag == NULL))
508 return NULL;
510 if (tag->type & tag_types)
512 tag_names = g_list_prepend(tag_names, tag);
515 tag_names = g_list_sort(tag_names, compare_symbol_lines);
516 return tag_names;
520 /* amount of types in the symbol list (currently max. 8 are used) */
521 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
523 struct TreeviewSymbols
525 GtkTreeIter tag_function;
526 GtkTreeIter tag_class;
527 GtkTreeIter tag_macro;
528 GtkTreeIter tag_member;
529 GtkTreeIter tag_variable;
530 GtkTreeIter tag_externvar;
531 GtkTreeIter tag_namespace;
532 GtkTreeIter tag_struct;
533 GtkTreeIter tag_interface;
534 GtkTreeIter tag_type;
535 GtkTreeIter tag_other;
536 } tv_iters;
539 static void init_tag_iters(void)
541 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
542 * filetypes(e.g. config file to Python crashes Geany without this) */
543 tv_iters.tag_function.stamp = -1;
544 tv_iters.tag_class.stamp = -1;
545 tv_iters.tag_member.stamp = -1;
546 tv_iters.tag_macro.stamp = -1;
547 tv_iters.tag_variable.stamp = -1;
548 tv_iters.tag_externvar.stamp = -1;
549 tv_iters.tag_namespace.stamp = -1;
550 tv_iters.tag_struct.stamp = -1;
551 tv_iters.tag_interface.stamp = -1;
552 tv_iters.tag_type.stamp = -1;
553 tv_iters.tag_other.stamp = -1;
557 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
559 static GtkIconTheme *icon_theme = NULL;
560 static gint x = -1;
562 if (G_UNLIKELY(x < 0))
564 gint dummy;
565 icon_theme = gtk_icon_theme_get_default();
566 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
568 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
572 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
574 GtkTreeModel *model = GTK_TREE_MODEL(store);
576 if (!gtk_tree_model_get_iter_first(model, iter))
577 return FALSE;
580 gchar *candidate;
582 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
583 /* FIXME: what if 2 different items have the same name?
584 * this should never happen, but might be caused by a typo in a translation */
585 if (utils_str_equal(candidate, title))
587 g_free(candidate);
588 return TRUE;
590 else
591 g_free(candidate);
593 while (gtk_tree_model_iter_next(model, iter));
595 return FALSE;
599 /* Adds symbol list groups in (iter*, title) pairs.
600 * The list must be ended with NULL. */
601 static void G_GNUC_NULL_TERMINATED
602 tag_list_add_groups(GtkTreeStore *tree_store, ...)
604 va_list args;
605 GtkTreeIter *iter;
607 g_return_if_fail(top_level_iter_names);
609 va_start(args, tree_store);
610 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
612 gchar *title = va_arg(args, gchar*);
613 gchar *icon_name = va_arg(args, gchar *);
614 GdkPixbuf *icon = NULL;
616 if (icon_name)
618 icon = get_tag_icon(icon_name);
621 g_assert(title != NULL);
622 g_ptr_array_add(top_level_iter_names, title);
624 if (!find_toplevel_iter(tree_store, iter, title))
625 gtk_tree_store_append(tree_store, iter, NULL);
627 if (G_IS_OBJECT(icon))
629 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
630 g_object_unref(icon);
632 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
634 va_end(args);
638 static void add_top_level_items(GeanyDocument *doc)
640 filetype_id ft_id = doc->file_type->id;
641 GtkTreeStore *tag_store = doc->priv->tag_store;
643 if (top_level_iter_names == NULL)
644 top_level_iter_names = g_ptr_array_new();
645 else
646 g_ptr_array_set_size(top_level_iter_names, 0);
648 init_tag_iters();
650 switch (ft_id)
652 case GEANY_FILETYPES_DIFF:
654 tag_list_add_groups(tag_store,
655 &(tv_iters.tag_function), _("Files"), NULL, NULL);
656 break;
658 case GEANY_FILETYPES_DOCBOOK:
660 tag_list_add_groups(tag_store,
661 &(tv_iters.tag_function), _("Chapter"), NULL,
662 &(tv_iters.tag_class), _("Section"), NULL,
663 &(tv_iters.tag_member), _("Sect1"), NULL,
664 &(tv_iters.tag_macro), _("Sect2"), NULL,
665 &(tv_iters.tag_variable), _("Sect3"), NULL,
666 &(tv_iters.tag_struct), _("Appendix"), NULL,
667 &(tv_iters.tag_other), _("Other"), NULL,
668 NULL);
669 break;
671 case GEANY_FILETYPES_HASKELL:
672 tag_list_add_groups(tag_store,
673 &tv_iters.tag_namespace, _("Module"), NULL,
674 &tv_iters.tag_type, _("Types"), NULL,
675 &tv_iters.tag_macro, _("Type constructors"), NULL,
676 &tv_iters.tag_function, _("Functions"), "classviewer-method",
677 NULL);
678 break;
679 case GEANY_FILETYPES_COBOL:
680 tag_list_add_groups(tag_store,
681 &tv_iters.tag_class, _("Program"), "classviewer-class",
682 &tv_iters.tag_function, _("File"), "classviewer-method",
683 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
684 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
685 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
686 &tv_iters.tag_variable, _("Data"), "classviewer-var",
687 NULL);
688 break;
689 case GEANY_FILETYPES_CONF:
690 tag_list_add_groups(tag_store,
691 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
692 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
693 NULL);
694 break;
695 case GEANY_FILETYPES_NSIS:
696 tag_list_add_groups(tag_store,
697 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
698 &tv_iters.tag_function, _("Functions"), "classviewer-method",
699 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
700 NULL);
701 break;
702 case GEANY_FILETYPES_LATEX:
704 tag_list_add_groups(tag_store,
705 &(tv_iters.tag_function), _("Command"), NULL,
706 &(tv_iters.tag_class), _("Environment"), NULL,
707 &(tv_iters.tag_member), _("Section"), NULL,
708 &(tv_iters.tag_macro), _("Subsection"), NULL,
709 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
710 &(tv_iters.tag_struct), _("Label"), NULL,
711 &(tv_iters.tag_namespace), _("Chapter"), NULL,
712 &(tv_iters.tag_other), _("Other"), NULL,
713 NULL);
714 break;
716 case GEANY_FILETYPES_MATLAB:
718 tag_list_add_groups(tag_store,
719 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
720 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
721 NULL);
722 break;
724 case GEANY_FILETYPES_ABAQUS:
726 tag_list_add_groups(tag_store,
727 &(tv_iters.tag_class), _("Parts"), NULL,
728 &(tv_iters.tag_member), _("Assembly"), NULL,
729 &(tv_iters.tag_namespace), _("Steps"), NULL,
730 NULL);
731 break;
733 case GEANY_FILETYPES_R:
735 tag_list_add_groups(tag_store,
736 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
737 &(tv_iters.tag_other), _("Other"), NULL,
738 NULL);
739 break;
741 case GEANY_FILETYPES_RUST:
743 tag_list_add_groups(tag_store,
744 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
745 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
746 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
747 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
748 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
749 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
750 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
751 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
752 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
753 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
754 NULL);
755 break;
757 case GEANY_FILETYPES_GO:
759 tag_list_add_groups(tag_store,
760 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
761 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
762 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
763 &(tv_iters.tag_type), _("Types"), "classviewer-struct",
764 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
765 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
766 NULL);
767 break;
769 case GEANY_FILETYPES_PERL:
771 tag_list_add_groups(tag_store,
772 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
773 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
774 &(tv_iters.tag_macro), _("Labels"), NULL,
775 &(tv_iters.tag_type), _("Constants"), NULL,
776 &(tv_iters.tag_other), _("Other"), "classviewer-other",
777 NULL);
778 break;
780 case GEANY_FILETYPES_PHP:
782 tag_list_add_groups(tag_store,
783 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
784 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
785 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
786 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
787 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
788 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
789 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
790 NULL);
791 break;
793 case GEANY_FILETYPES_HTML:
795 tag_list_add_groups(tag_store,
796 &(tv_iters.tag_function), _("Functions"), NULL,
797 &(tv_iters.tag_member), _("Anchors"), NULL,
798 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
799 &(tv_iters.tag_class), _("H2 Headings"), NULL,
800 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
801 NULL);
802 break;
804 case GEANY_FILETYPES_CSS:
806 tag_list_add_groups(tag_store,
807 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
808 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
809 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
810 break;
812 case GEANY_FILETYPES_REST:
813 case GEANY_FILETYPES_TXT2TAGS:
814 case GEANY_FILETYPES_ABC:
816 tag_list_add_groups(tag_store,
817 &(tv_iters.tag_namespace), _("Chapter"), NULL,
818 &(tv_iters.tag_member), _("Section"), NULL,
819 &(tv_iters.tag_macro), _("Subsection"), NULL,
820 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
821 NULL);
822 break;
824 case GEANY_FILETYPES_ASCIIDOC:
826 tag_list_add_groups(tag_store,
827 &(tv_iters.tag_namespace), _("Document"), NULL,
828 &(tv_iters.tag_member), _("Section Level 1"), NULL,
829 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
830 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
831 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
832 NULL);
833 break;
835 case GEANY_FILETYPES_RUBY:
837 tag_list_add_groups(tag_store,
838 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
839 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
840 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
841 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
842 NULL);
843 break;
845 case GEANY_FILETYPES_TCL:
847 tag_list_add_groups(tag_store,
848 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
849 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
850 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
851 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
852 NULL);
853 break;
855 case GEANY_FILETYPES_PYTHON:
857 tag_list_add_groups(tag_store,
858 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
859 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
860 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
861 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
862 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
863 NULL);
864 break;
866 case GEANY_FILETYPES_VHDL:
868 tag_list_add_groups(tag_store,
869 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
870 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
871 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
872 &(tv_iters.tag_type), _("Types"), "classviewer-other",
873 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
874 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
875 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
876 &(tv_iters.tag_other), _("Other"), "classviewer-other",
877 NULL);
878 break;
880 case GEANY_FILETYPES_VERILOG:
882 tag_list_add_groups(tag_store,
883 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
884 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
885 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
886 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
887 &(tv_iters.tag_other), _("Other"), "classviewer-other",
888 NULL);
889 break;
891 case GEANY_FILETYPES_JAVA:
893 tag_list_add_groups(tag_store,
894 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
895 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
896 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
897 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
898 &(tv_iters.tag_member), _("Members"), "classviewer-member",
899 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
900 &(tv_iters.tag_other), _("Other"), "classviewer-other",
901 NULL);
902 break;
904 case GEANY_FILETYPES_AS:
906 tag_list_add_groups(tag_store,
907 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
908 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
909 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
910 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
911 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
912 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
913 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
914 &(tv_iters.tag_other), _("Other"), "classviewer-other",
915 NULL);
916 break;
918 case GEANY_FILETYPES_HAXE:
920 tag_list_add_groups(tag_store,
921 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
922 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
923 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
924 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
925 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
926 &(tv_iters.tag_other), _("Other"), "classviewer-other",
927 NULL);
928 break;
930 case GEANY_FILETYPES_BASIC:
932 tag_list_add_groups(tag_store,
933 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
934 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
935 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
936 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
937 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
938 &(tv_iters.tag_other), _("Other"), "classviewer-other",
939 NULL);
940 break;
942 case GEANY_FILETYPES_F77:
943 case GEANY_FILETYPES_FORTRAN:
945 tag_list_add_groups(tag_store,
946 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
947 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
948 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
949 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
950 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
951 &(tv_iters.tag_class), _("Types"), "classviewer-class",
952 &(tv_iters.tag_member), _("Components"), "classviewer-member",
953 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
954 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
955 &(tv_iters.tag_other), _("Other"), "classviewer-other",
956 NULL);
957 break;
959 case GEANY_FILETYPES_ASM:
961 tag_list_add_groups(tag_store,
962 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
963 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
964 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
965 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
966 NULL);
967 break;
969 case GEANY_FILETYPES_MAKE:
970 tag_list_add_groups(tag_store,
971 &tv_iters.tag_function, _("Targets"), "classviewer-method",
972 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
973 NULL);
974 break;
975 case GEANY_FILETYPES_SQL:
977 tag_list_add_groups(tag_store,
978 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
979 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
980 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
981 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
982 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
983 &(tv_iters.tag_member), _("Views"), "classviewer-var",
984 &(tv_iters.tag_other), _("Other"), "classviewer-other",
985 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
986 NULL);
987 break;
989 case GEANY_FILETYPES_D:
990 default:
992 if (ft_id == GEANY_FILETYPES_D)
993 tag_list_add_groups(tag_store,
994 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
995 else
996 tag_list_add_groups(tag_store,
997 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
999 tag_list_add_groups(tag_store,
1000 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
1001 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
1002 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
1003 &(tv_iters.tag_member), _("Members"), "classviewer-member",
1004 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
1005 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
1006 NULL);
1008 if (ft_id != GEANY_FILETYPES_D)
1010 tag_list_add_groups(tag_store,
1011 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1013 tag_list_add_groups(tag_store,
1014 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1015 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1016 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1022 /* removes toplevel items that have no children */
1023 static void hide_empty_rows(GtkTreeStore *store)
1025 GtkTreeIter iter;
1026 gboolean cont = TRUE;
1028 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1029 return; /* stop when first iter is invalid, i.e. no elements */
1031 while (cont)
1033 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1034 cont = gtk_tree_store_remove(store, &iter);
1035 else
1036 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1041 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1043 gchar *utf8_name;
1044 const gchar *scope = tag->scope;
1045 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1046 gboolean doc_is_utf8 = FALSE;
1048 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1049 * for None at this point completely */
1050 if (utils_str_equal(doc->encoding, "UTF-8") ||
1051 utils_str_equal(doc->encoding, "None"))
1052 doc_is_utf8 = TRUE;
1053 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1054 * plugin might have called tm_source_file_update(), so check to be sure */
1055 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1057 if (! doc_is_utf8)
1058 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1059 -1, doc->encoding, TRUE);
1060 else
1061 utf8_name = tag->name;
1063 if (utf8_name == NULL)
1064 return NULL;
1066 if (! buffer)
1067 buffer = g_string_new(NULL);
1068 else
1069 g_string_truncate(buffer, 0);
1071 /* check first char of scope is a wordchar */
1072 if (!found_parent && scope &&
1073 strpbrk(scope, GEANY_WORDCHARS) == scope)
1075 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1077 g_string_append(buffer, scope);
1078 g_string_append(buffer, sep);
1080 g_string_append(buffer, utf8_name);
1082 if (! doc_is_utf8)
1083 g_free(utf8_name);
1085 g_string_append_printf(buffer, " [%lu]", tag->line);
1087 return buffer->str;
1091 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1093 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1095 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1096 * for None at this point completely */
1097 if (utf8_name != NULL &&
1098 ! utils_str_equal(doc->encoding, "UTF-8") &&
1099 ! utils_str_equal(doc->encoding, "None"))
1101 SETPTR(utf8_name,
1102 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1105 if (utf8_name != NULL)
1106 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1108 return utf8_name;
1112 /* find the last word in "foo::bar::blah", e.g. "blah" */
1113 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1115 const gchar *scope = tag->scope;
1116 const gchar *separator = symbols_get_context_separator(ft_id);
1117 const gchar *str, *ptr;
1119 if (!scope)
1120 return NULL;
1122 str = scope;
1124 while (1)
1126 ptr = strstr(str, separator);
1127 if (ptr)
1129 str = ptr + strlen(separator);
1131 else
1132 break;
1135 return !EMPTY(str) ? str : NULL;
1139 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1141 GtkTreeIter *iter = NULL;
1143 switch (tag_type)
1145 case tm_tag_prototype_t:
1146 case tm_tag_method_t:
1147 case tm_tag_function_t:
1149 iter = &tv_iters.tag_function;
1150 break;
1152 case tm_tag_externvar_t:
1154 iter = &tv_iters.tag_externvar;
1155 break;
1157 case tm_tag_macro_t:
1158 case tm_tag_macro_with_arg_t:
1160 iter = &tv_iters.tag_macro;
1161 break;
1163 case tm_tag_class_t:
1165 iter = &tv_iters.tag_class;
1166 break;
1168 case tm_tag_member_t:
1169 case tm_tag_field_t:
1171 iter = &tv_iters.tag_member;
1172 break;
1174 case tm_tag_typedef_t:
1175 case tm_tag_enum_t:
1177 iter = &tv_iters.tag_type;
1178 break;
1180 case tm_tag_union_t:
1181 case tm_tag_struct_t:
1183 iter = &tv_iters.tag_struct;
1184 break;
1186 case tm_tag_interface_t:
1187 iter = &tv_iters.tag_interface;
1188 break;
1189 case tm_tag_variable_t:
1191 iter = &tv_iters.tag_variable;
1192 break;
1194 case tm_tag_namespace_t:
1195 case tm_tag_package_t:
1197 iter = &tv_iters.tag_namespace;
1198 break;
1200 default:
1202 iter = &tv_iters.tag_other;
1205 if (G_LIKELY(iter->stamp != -1))
1206 return iter;
1207 else
1208 return NULL;
1212 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1214 GdkPixbuf *icon = NULL;
1216 if (parent == &tv_iters.tag_other)
1218 return get_tag_icon("classviewer-var");
1220 /* copy parent icon */
1221 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1222 SYMBOLS_COLUMN_ICON, &icon, -1);
1223 return icon;
1227 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1229 const TMTag *t1 = v1;
1230 const TMTag *t2 = v2;
1232 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1233 utils_str_equal(t1->scope, t2->scope) &&
1234 /* include arglist in match to support e.g. C++ overloading */
1235 utils_str_equal(t1->arglist, t2->arglist));
1239 /* inspired from g_str_hash() */
1240 static guint tag_hash(gconstpointer v)
1242 const TMTag *tag = v;
1243 const gchar *p;
1244 guint32 h = 5381;
1246 h = (h << 5) + h + tag->type;
1247 for (p = tag->name; *p != '\0'; p++)
1248 h = (h << 5) + h + *p;
1249 if (tag->scope)
1251 for (p = tag->scope; *p != '\0'; p++)
1252 h = (h << 5) + h + *p;
1254 /* for e.g. C++ overloading */
1255 if (tag->arglist)
1257 for (p = tag->arglist; *p != '\0'; p++)
1258 h = (h << 5) + h + *p;
1261 return h;
1265 /* like gtk_tree_view_expand_to_path() but with an iter */
1266 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1268 GtkTreeModel *model = gtk_tree_view_get_model(view);
1269 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1271 gtk_tree_view_expand_to_path(view, path);
1272 gtk_tree_path_free(path);
1276 /* like gtk_tree_store_remove() but finds the next iter at any level */
1277 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1279 GtkTreeIter parent;
1280 gboolean has_parent;
1281 gboolean cont;
1283 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1284 cont = gtk_tree_store_remove(store, iter);
1285 /* if there is no next at this level but there is a parent iter, continue from it */
1286 if (! cont && has_parent)
1288 *iter = parent;
1289 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1292 return cont;
1296 /* adds a new element in the parent table if it's key is known.
1297 * duplicates are kept */
1298 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1299 const GtkTreeIter *iter)
1301 GList **list;
1302 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1303 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1305 if (! list)
1307 list = g_slice_alloc(sizeof *list);
1308 *list = NULL;
1309 g_hash_table_insert(table, tag->name, list);
1311 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1316 static void free_iter_slice_list(gpointer data)
1318 GList **list = data;
1320 if (list)
1322 GList *node;
1323 foreach_list(node, *list)
1324 g_slice_free(GtkTreeIter, node->data);
1325 g_list_free(*list);
1326 g_slice_free1(sizeof *list, list);
1331 /* inserts a @data in @table on key @tag.
1332 * previous data is not overwritten if the key is duplicated, but rather the
1333 * two values are kept in a list
1335 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1336 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1338 GList *list = g_hash_table_lookup(table, tag);
1339 list = g_list_prepend(list, data);
1340 g_hash_table_insert(table, tag, list);
1344 /* looks up the entry in @table that better matches @tag.
1345 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1346 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1348 GList *data = NULL;
1349 GList *node = g_hash_table_lookup(table, tag);
1350 if (node)
1352 glong delta;
1353 data = node->data;
1355 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1357 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1358 for (node = node->next; node; node = node->next)
1360 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1362 if (d < delta)
1364 data = node->data;
1365 delta = d;
1369 #undef TAG_DELTA
1372 return data;
1376 /* removes the element at @tag from @table.
1377 * @tag must be the exact pointer used at insertion time */
1378 static void tags_table_remove(GHashTable *table, TMTag *tag)
1380 GList *list = g_hash_table_lookup(table, tag);
1381 if (list)
1383 GList *node;
1384 foreach_list(node, list)
1386 if (((GList *) node->data)->data == tag)
1387 break;
1389 list = g_list_delete_link(list, node);
1390 if (list)
1391 g_hash_table_insert(table, tag, list);
1392 else
1393 g_hash_table_remove(table, tag);
1398 static void tags_table_destroy(GHashTable *table)
1400 /* free any leftover elements. note that we can't register a value_free_func when
1401 * creating the hash table because we only want to free it when destroying the table,
1402 * not when inserting a duplicate (we handle this manually) */
1403 GHashTableIter iter;
1404 gpointer value;
1406 g_hash_table_iter_init(&iter, table);
1407 while (g_hash_table_iter_next(&iter, NULL, &value))
1408 g_list_free(value);
1409 g_hash_table_destroy(table);
1414 * Updates the tag tree for a document with the tags in *list.
1415 * @param doc a document
1416 * @param tags a pointer to a GList* holding the tags to add/update. This
1417 * list may be updated, removing updated elements.
1419 * The update is done in two passes:
1420 * 1) walking the current tree, update tags that still exist and remove the
1421 * obsolescent ones;
1422 * 2) walking the remaining (non updated) tags, adds them in the list.
1424 * For better performances, we use 2 hash tables:
1425 * - one containing all the tags for lookup in the first pass (actually stores a
1426 * reference in the tags list for removing it efficiently), avoiding list search
1427 * on each tag;
1428 * - the other holding "tag-name":row references for tags having children, used to
1429 * lookup for a parent in both passes, avoiding tree traversal.
1431 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1433 GtkTreeStore *store = doc->priv->tag_store;
1434 GtkTreeModel *model = GTK_TREE_MODEL(store);
1435 GHashTable *parents_table;
1436 GHashTable *tags_table;
1437 GtkTreeIter iter;
1438 gboolean cont;
1439 GList *item;
1441 /* Build hash tables holding tags and parents */
1442 /* parent table holds "tag-name":GtkTreeIter */
1443 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1444 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1445 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1446 foreach_list(item, *tags)
1448 TMTag *tag = item->data;
1449 const gchar *name;
1451 tags_table_insert(tags_table, tag, item);
1453 name = get_parent_name(tag, doc->file_type->id);
1454 if (name)
1455 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1458 /* First pass, update existing rows or delete them.
1459 * It is OK to delete them since we walk top down so we would remove
1460 * parents before checking for their children, thus never implicitly
1461 * deleting an updated child */
1462 cont = gtk_tree_model_get_iter_first(model, &iter);
1463 while (cont)
1465 TMTag *tag;
1467 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1468 if (! tag) /* most probably a toplevel, skip it */
1469 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1470 else
1472 GList *found_item;
1474 found_item = tags_table_lookup(tags_table, tag);
1475 if (! found_item) /* tag doesn't exist, remove it */
1476 cont = tree_store_remove_row(store, &iter);
1477 else /* tag still exist, update it */
1479 const gchar *name;
1480 const gchar *parent_name;
1481 TMTag *found = found_item->data;
1483 parent_name = get_parent_name(found, doc->file_type->id);
1484 /* if parent is unknown, ignore it */
1485 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1486 parent_name = NULL;
1488 /* only update fields that (can) have changed (name that holds line
1489 * number, and the tag itself) */
1490 name = get_symbol_name(doc, found, parent_name != NULL);
1491 gtk_tree_store_set(store, &iter,
1492 SYMBOLS_COLUMN_NAME, name,
1493 SYMBOLS_COLUMN_TAG, found,
1494 -1);
1496 update_parents_table(parents_table, found, parent_name, &iter);
1498 /* remove the updated tag from the table and list */
1499 tags_table_remove(tags_table, found);
1500 *tags = g_list_delete_link(*tags, found_item);
1502 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1505 tm_tag_unref(tag);
1509 /* Second pass, now we have a tree cleaned up from invalid rows,
1510 * we simply add new ones */
1511 foreach_list (item, *tags)
1513 TMTag *tag = item->data;
1514 GtkTreeIter *parent;
1516 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1517 if (G_UNLIKELY(! parent))
1518 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1519 else
1521 gboolean expand;
1522 const gchar *name;
1523 const gchar *parent_name;
1524 gchar *tooltip;
1525 GdkPixbuf *icon = get_child_icon(store, parent);
1527 parent_name = get_parent_name(tag, doc->file_type->id);
1528 if (parent_name)
1530 GList **candidates;
1531 GtkTreeIter *parent_search = NULL;
1533 /* walk parent candidates to find the better one.
1534 * if there are more than one, take the one that has the closest line number
1535 * after the tag we're searching the parent for */
1536 candidates = g_hash_table_lookup(parents_table, parent_name);
1537 if (candidates)
1539 GList *node;
1540 glong delta = G_MAXLONG;
1541 foreach_list(node, *candidates)
1543 TMTag *parent_tag;
1544 glong d;
1546 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1547 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1549 d = tag->line - parent_tag->line;
1550 if (! parent_search || (d >= 0 && d < delta))
1552 delta = d;
1553 parent_search = node->data;
1555 tm_tag_unref(parent_tag);
1559 if (parent_search)
1560 parent = parent_search;
1561 else
1562 parent_name = NULL;
1565 /* only expand to the iter if the parent was empty, otherwise we let the
1566 * folding as it was before (already expanded, or closed by the user) */
1567 expand = ! gtk_tree_model_iter_has_child(model, parent);
1569 /* insert the new element */
1570 gtk_tree_store_append(store, &iter, parent);
1571 name = get_symbol_name(doc, tag, parent_name != NULL);
1572 tooltip = get_symbol_tooltip(doc, tag);
1573 gtk_tree_store_set(store, &iter,
1574 SYMBOLS_COLUMN_NAME, name,
1575 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1576 SYMBOLS_COLUMN_ICON, icon,
1577 SYMBOLS_COLUMN_TAG, tag,
1578 -1);
1579 g_free(tooltip);
1580 if (G_LIKELY(icon))
1581 g_object_unref(icon);
1583 update_parents_table(parents_table, tag, parent_name, &iter);
1585 if (expand)
1586 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1590 g_hash_table_destroy(parents_table);
1591 tags_table_destroy(tags_table);
1595 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1596 * is not stable, so the order is already lost. */
1597 static gint compare_top_level_names(const gchar *a, const gchar *b)
1599 guint i;
1600 const gchar *name;
1602 /* This should never happen as it would mean that two or more top
1603 * level items have the same name but it can happen by typos in the translations. */
1604 if (utils_str_equal(a, b))
1605 return 1;
1607 foreach_ptr_array(name, i, top_level_iter_names)
1609 if (utils_str_equal(name, a))
1610 return -1;
1611 if (utils_str_equal(name, b))
1612 return 1;
1614 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1615 return 0;
1619 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1620 GtkTreeIter *iter)
1622 /* if the tag has a parent tag, it should be at depth >= 2 */
1623 return !EMPTY(tag->scope) &&
1624 gtk_tree_store_iter_depth(store, iter) == 1;
1628 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1629 gpointer user_data)
1631 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1632 TMTag *tag_a, *tag_b;
1633 gint cmp;
1635 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1636 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1638 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1639 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1640 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1641 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1643 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1644 compare_symbol_lines(tag_a, tag_b);
1646 else
1648 gchar *astr, *bstr;
1650 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1651 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1653 /* if a is toplevel, b must be also */
1654 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1656 cmp = compare_top_level_names(astr, bstr);
1658 else
1660 /* this is what g_strcmp0() does */
1661 if (! astr)
1662 cmp = -(astr != bstr);
1663 else if (! bstr)
1664 cmp = astr != bstr;
1665 else
1667 cmp = strcmp(astr, bstr);
1669 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1670 if (tag_a && tag_b)
1671 if (!sort_by_name ||
1672 (utils_str_equal(tag_a->name, tag_b->name) &&
1673 utils_str_equal(tag_a->scope, tag_b->scope)))
1674 cmp = compare_symbol_lines(tag_a, tag_b);
1677 g_free(astr);
1678 g_free(bstr);
1680 tm_tag_unref(tag_a);
1681 tm_tag_unref(tag_b);
1683 return cmp;
1687 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1689 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1690 GINT_TO_POINTER(sort_by_name), NULL);
1692 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1696 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1698 GList *tags;
1700 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1702 tags = get_tag_list(doc, tm_tag_max_t);
1703 if (tags == NULL)
1704 return FALSE;
1706 /* FIXME: Not sure why we detached the model here? */
1708 /* disable sorting during update because the code doesn't support correctly
1709 * models that are currently being built */
1710 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1712 /* add grandparent type iters */
1713 add_top_level_items(doc);
1715 update_tree_tags(doc, &tags);
1716 g_list_free(tags);
1718 hide_empty_rows(doc->priv->tag_store);
1720 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1721 sort_mode = doc->priv->symbol_list_sort_mode;
1723 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1724 doc->priv->symbol_list_sort_mode = sort_mode;
1726 return TRUE;
1730 /* Detects a global tags filetype from the *.lang.* language extension.
1731 * Returns NULL if there was no matching TM language. */
1732 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1734 gchar *tags_ext;
1735 gchar *shortname = utils_strdupa(utf8_filename);
1736 GeanyFiletype *ft = NULL;
1738 tags_ext = g_strrstr(shortname, ".tags");
1739 if (tags_ext)
1741 *tags_ext = '\0'; /* remove .tags extension */
1742 ft = filetypes_detect_from_extension(shortname);
1743 if (ft->id != GEANY_FILETYPES_NONE)
1744 return ft;
1746 return NULL;
1750 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1751 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1752 * the relevant path.
1753 * Example:
1754 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1755 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1757 /* -E pre-process, -dD output user macros, -p prof info (?) */
1758 const char pre_process[] = "gcc -E -dD -p -I.";
1760 if (argc > 2)
1762 /* Create global taglist */
1763 int status;
1764 char *command;
1765 const char *tags_file = argv[1];
1766 char *utf8_fname;
1767 GeanyFiletype *ft;
1769 utf8_fname = utils_get_utf8_from_locale(tags_file);
1770 ft = detect_global_tags_filetype(utf8_fname);
1771 g_free(utf8_fname);
1773 if (ft == NULL)
1775 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1776 return 1;
1778 /* load config in case of custom filetypes */
1779 filetypes_load_config(ft->id, FALSE);
1781 /* load ignore list for C/C++ parser */
1782 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1783 load_c_ignore_tags();
1785 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1787 const gchar *cflags = getenv("CFLAGS");
1788 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1790 else
1791 command = NULL; /* don't preprocess */
1793 geany_debug("Generating %s tags file.", ft->name);
1794 tm_get_workspace();
1795 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1796 argc - 2, tags_file, ft->lang);
1797 g_free(command);
1798 symbols_finalize(); /* free c_tags_ignore data */
1799 if (! status)
1801 g_printerr(_("Failed to create tags file, perhaps because no tags "
1802 "were found.\n"));
1803 return 1;
1806 else
1808 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1809 g_printerr(_("Example:\n"
1810 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1811 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1812 return 1;
1814 return 0;
1818 void symbols_show_load_tags_dialog(void)
1820 GtkWidget *dialog;
1821 GtkFileFilter *filter;
1823 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1824 GTK_FILE_CHOOSER_ACTION_OPEN,
1825 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1826 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1827 NULL);
1828 gtk_widget_set_name(dialog, "GeanyDialog");
1829 filter = gtk_file_filter_new();
1830 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1831 gtk_file_filter_add_pattern(filter, "*.*.tags");
1832 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1834 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1836 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1837 GSList *item;
1839 for (item = flist; item != NULL; item = g_slist_next(item))
1841 gchar *fname = item->data;
1842 gchar *utf8_fname;
1843 GeanyFiletype *ft;
1845 utf8_fname = utils_get_utf8_from_locale(fname);
1846 ft = detect_global_tags_filetype(utf8_fname);
1848 if (ft != NULL && symbols_load_global_tags(fname, ft))
1849 /* For translators: the first wildcard is the filetype, the second the filename */
1850 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1851 filetypes_get_display_name(ft), utf8_fname);
1852 else
1853 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1855 g_free(utf8_fname);
1856 g_free(fname);
1858 g_slist_free(flist);
1860 gtk_widget_destroy(dialog);
1864 static void init_user_tags(void)
1866 GSList *file_list = NULL, *list = NULL;
1867 const GSList *node;
1868 gchar *dir;
1870 dir = g_build_filename(app->configdir, "tags", NULL);
1871 /* create the user tags dir for next time if it doesn't exist */
1872 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1873 utils_mkdir(dir, FALSE);
1874 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1876 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1877 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1878 g_free(dir);
1880 file_list = g_slist_concat(file_list, list);
1882 /* populate the filetype-specific tag files lists */
1883 for (node = file_list; node != NULL; node = node->next)
1885 gchar *fname = node->data;
1886 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1887 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1889 g_free(utf8_fname);
1891 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1892 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1893 else
1895 geany_debug("Unknown filetype for file '%s'.", fname);
1896 g_free(fname);
1900 /* don't need to delete list contents because they are now stored in
1901 * ft->priv->tag_files */
1902 g_slist_free(file_list);
1906 static void load_user_tags(filetype_id ft_id)
1908 static guchar *tags_loaded = NULL;
1909 static gboolean init_tags = FALSE;
1910 const GSList *node;
1911 GeanyFiletype *ft = filetypes[ft_id];
1913 g_return_if_fail(ft_id > 0);
1915 if (!tags_loaded)
1916 tags_loaded = g_new0(guchar, filetypes_array->len);
1917 if (tags_loaded[ft_id])
1918 return;
1919 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1921 if (!init_tags)
1923 init_user_tags();
1924 init_tags = TRUE;
1927 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1929 const gchar *fname = node->data;
1931 symbols_load_global_tags(fname, ft);
1936 static gboolean goto_tag(const gchar *name, gboolean definition)
1938 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1939 TMTagType type;
1940 TMTag *tmtag = NULL;
1941 GeanyDocument *old_doc = document_get_current();
1943 /* goto tag definition: all except prototypes / forward declarations / externs */
1944 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1946 /* first look in the current document */
1947 if (old_doc != NULL && old_doc->tm_file)
1948 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1950 /* if not found, look in the workspace */
1951 if (tmtag == NULL)
1952 tmtag = find_workspace_tag(name, type);
1954 if (tmtag != NULL)
1956 GeanyDocument *new_doc = document_find_by_real_path(
1957 tmtag->file->file_name);
1959 if (new_doc)
1961 /* If we are already on the tag line, swap definition/declaration */
1962 if (new_doc == old_doc &&
1963 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1965 if (goto_tag(name, !definition))
1966 return TRUE;
1969 else
1971 /* not found in opened document, should open */
1972 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1975 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1976 return TRUE;
1978 return FALSE;
1982 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1984 if (goto_tag(name, definition))
1985 return TRUE;
1987 /* if we are here, there was no match and we are beeping ;-) */
1988 utils_beep();
1990 if (!definition)
1991 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1992 else
1993 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1994 return FALSE;
1998 /* This could perhaps be improved to check for #if, class etc. */
1999 static gint get_function_fold_number(GeanyDocument *doc)
2001 /* for Java the functions are always one fold level above the class scope */
2002 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
2003 return SC_FOLDLEVELBASE + 1;
2004 else
2005 return SC_FOLDLEVELBASE;
2009 /* Should be used only with get_current_tag_cached.
2010 * tag_types caching might trigger recomputation too often but this isn't used differently often
2011 * enough to be an issue for now */
2012 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2014 static gint old_line = -2;
2015 static GeanyDocument *old_doc = NULL;
2016 static gint old_fold_num = -1;
2017 static guint old_tag_types = 0;
2018 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2019 gboolean ret;
2021 /* check if the cached line and file index have changed since last time: */
2022 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2023 ret = TRUE;
2024 else if (cur_line == old_line)
2025 ret = FALSE;
2026 else
2028 /* if the line has only changed by 1 */
2029 if (abs(cur_line - old_line) == 1)
2031 /* It's the same function if the fold number hasn't changed */
2032 ret = (fold_num != old_fold_num);
2034 else ret = TRUE;
2037 /* record current line and file index for next time */
2038 old_line = cur_line;
2039 old_doc = doc;
2040 old_fold_num = fold_num;
2041 old_tag_types = tag_types;
2042 return ret;
2046 /* Parse the function name up to 2 lines before tag_line.
2047 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2048 * type or argument names can be confused with the function name. */
2049 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2051 gint start, end, max_pos;
2052 gint fn_style;
2054 switch (sci_get_lexer(sci))
2056 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2057 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2058 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2060 start = sci_get_position_from_line(sci, tag_line - 2);
2061 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2062 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2063 start++;
2065 end = start;
2066 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2067 end++;
2069 if (start == end)
2070 return NULL;
2071 return sci_get_contents_range(sci, start, end);
2075 /* Parse the function name */
2076 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2078 gint start, end, first_pos, max_pos;
2079 gint tmp;
2080 gchar c;
2082 first_pos = end = sci_get_position_from_line(sci, tag_line);
2083 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2084 tmp = 0;
2085 /* goto the begin of function body */
2086 while (end < max_pos &&
2087 (tmp = sci_get_char_at(sci, end)) != '{' &&
2088 tmp != 0) end++;
2089 if (tmp == 0) end --;
2091 /* go back to the end of function identifier */
2092 while (end > 0 && end > first_pos - 500 &&
2093 (tmp = sci_get_char_at(sci, end)) != '(' &&
2094 tmp != 0) end--;
2095 end--;
2096 if (end < 0) end = 0;
2098 /* skip whitespaces between identifier and ( */
2099 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2101 start = end;
2102 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2103 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2104 || tmp == SCE_C_GLOBALCLASS
2105 || (c = sci_get_char_at(sci, start)) == '~'
2106 || c == ':'))
2107 start--;
2108 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2110 if (start == end) return NULL;
2111 return sci_get_contents_range(sci, start, end + 1);
2115 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2117 gint line_count = sci_get_line_count(sci);
2119 for (; line < line_count; line++)
2121 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2122 return line;
2125 return -1;
2129 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2131 gint line;
2132 gint parent;
2134 line = sci_get_current_line(doc->editor->sci);
2135 parent = sci_get_fold_parent(doc->editor->sci, line);
2136 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2137 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2138 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2140 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2142 if (tag)
2144 gint tag_line = tag->line - 1;
2145 gint last_child = line + 1;
2147 /* if it may be a false positive because we're inside a fold level not inside anything
2148 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2149 * right after the tag we got from TM */
2150 if (abs(tag_line - parent) > 1)
2152 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2153 if (tag_fold >= 0)
2154 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2157 if (line <= last_child)
2159 if (tag->scope)
2160 *tagname = g_strconcat(tag->scope,
2161 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2162 else
2163 *tagname = g_strdup(tag->name);
2165 return tag_line;
2169 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2170 * to dirty and inaccurate hand-parsing */
2171 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2173 const gint fn_fold = get_function_fold_number(doc);
2174 gint tag_line = parent;
2175 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2177 /* find the top level fold point */
2178 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2180 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2181 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2184 if (tag_line >= 0)
2186 gchar *cur_tag;
2188 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2189 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2190 else
2191 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2193 if (cur_tag != NULL)
2195 *tagname = cur_tag;
2196 return tag_line;
2201 *tagname = g_strdup(_("unknown"));
2202 return -1;
2206 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2208 static gint tag_line = -1;
2209 static gchar *cur_tag = NULL;
2211 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2213 if (doc == NULL) /* reset current function */
2215 current_tag_changed(NULL, -1, -1, 0);
2216 g_free(cur_tag);
2217 cur_tag = g_strdup(_("unknown"));
2218 if (tagname != NULL)
2219 *tagname = cur_tag;
2220 tag_line = -1;
2222 else
2224 gint line = sci_get_current_line(doc->editor->sci);
2225 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2227 if (current_tag_changed(doc, line, fold_level, tag_types))
2229 g_free(cur_tag);
2230 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2232 *tagname = cur_tag;
2235 return tag_line;
2239 /* Sets *tagname to point at the current function or tag name.
2240 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2241 * call to this function.
2242 * Returns: line number of the current tag, or -1 if unknown. */
2243 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2245 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2249 /* same as symbols_get_current_function() but finds class, namespaces and more */
2250 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2252 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2253 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2255 /* Python parser reports imports as namespaces which confuses the scope detection */
2256 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2257 tag_types |= tm_tag_namespace_t;
2259 return get_current_tag_name_cached(doc, tagname, tag_types);
2263 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2265 gint sort_mode = GPOINTER_TO_INT(user_data);
2266 GeanyDocument *doc = document_get_current();
2268 if (ignore_callback)
2269 return;
2271 if (doc != NULL)
2272 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2276 static void on_symbol_tree_menu_show(GtkWidget *widget,
2277 gpointer user_data)
2279 GeanyDocument *doc = document_get_current();
2280 gboolean enable;
2282 enable = doc && doc->has_tags;
2283 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2284 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2285 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2286 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2287 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2288 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2290 if (! doc)
2291 return;
2293 ignore_callback = TRUE;
2295 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2296 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2297 else
2298 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2300 ignore_callback = FALSE;
2304 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2306 gboolean expand = GPOINTER_TO_INT(user_data);
2307 GeanyDocument *doc = document_get_current();
2309 if (! doc)
2310 return;
2312 g_return_if_fail(doc->priv->tag_tree);
2314 if (expand)
2315 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2316 else
2317 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2321 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2323 GtkTreeIter iter;
2324 GtkTreeSelection *selection;
2325 GtkTreeModel *model;
2326 GeanyDocument *doc;
2327 TMTag *tag = NULL;
2329 doc = document_get_current();
2330 if (!doc)
2331 return;
2333 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2334 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2335 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2336 if (tag)
2338 if (widget == symbol_menu.find_in_files)
2339 search_show_find_in_files_dialog_full(tag->name, NULL);
2340 else
2341 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2342 widget == symbol_menu.find_usage);
2344 tm_tag_unref(tag);
2349 static void create_taglist_popup_menu(void)
2351 GtkWidget *item, *menu;
2353 tv.popup_taglist = menu = gtk_menu_new();
2355 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2356 gtk_widget_show(item);
2357 gtk_container_add(GTK_CONTAINER(menu), item);
2358 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2360 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2361 gtk_widget_show(item);
2362 gtk_container_add(GTK_CONTAINER(menu), item);
2363 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2365 item = gtk_separator_menu_item_new();
2366 gtk_widget_show(item);
2367 gtk_container_add(GTK_CONTAINER(menu), item);
2369 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2370 _("Sort by _Name"));
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_NAME));
2376 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2377 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2378 gtk_widget_show(item);
2379 gtk_container_add(GTK_CONTAINER(menu), item);
2380 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2381 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2383 item = gtk_separator_menu_item_new();
2384 gtk_widget_show(item);
2385 gtk_container_add(GTK_CONTAINER(menu), item);
2387 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2388 gtk_widget_show(item);
2389 gtk_container_add(GTK_CONTAINER(menu), item);
2390 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2392 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2393 gtk_widget_show(item);
2394 gtk_container_add(GTK_CONTAINER(menu), item);
2395 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2397 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2398 gtk_widget_show(item);
2399 gtk_container_add(GTK_CONTAINER(menu), item);
2400 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2402 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2404 sidebar_add_common_menu_items(GTK_MENU(menu));
2408 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2410 gchar *f;
2412 g_return_if_fail(!EMPTY(doc->real_path));
2414 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2415 if (utils_str_equal(doc->real_path, f))
2416 load_c_ignore_tags();
2418 g_free(f);
2422 void symbols_init(void)
2424 gchar *f;
2426 create_taglist_popup_menu();
2428 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2429 ui_add_config_file_menu_item(f, NULL, NULL);
2430 g_free(f);
2432 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2436 void symbols_finalize(void)
2438 g_strfreev(html_entities);
2439 g_strfreev(c_tags_ignore);