The mkstemps special case for windows is not necessary
[geany-mirror.git] / src / symbols.c
blob9f8d7dcc6eac2e100dc3355b439725c81cc75306
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 GEANY_API_SYMBOL
296 const gchar *symbols_get_context_separator(gint ft_id)
298 switch (ft_id)
300 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
301 case GEANY_FILETYPES_CPP:
302 case GEANY_FILETYPES_GLSL: /* for structs */
303 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
304 case GEANY_FILETYPES_PHP:
305 case GEANY_FILETYPES_RUST:
306 return "::";
308 /* avoid confusion with other possible separators in group/section name */
309 case GEANY_FILETYPES_CONF:
310 case GEANY_FILETYPES_REST:
311 return ":::";
313 /* no context separator */
314 case GEANY_FILETYPES_ASCIIDOC:
315 case GEANY_FILETYPES_TXT2TAGS:
316 return "\x03";
318 default:
319 return ".";
324 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
325 static TMTag *
326 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
328 guint i;
329 g_return_val_if_fail(tags != NULL, NULL);
331 for (i = 0; i < tags->len; ++i)
333 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
334 return TM_TAG(tags->pdata[i]);
336 return NULL;
340 static TMTag *find_source_file_tag(GPtrArray *tags_array,
341 const gchar *tag_name, guint type)
343 GPtrArray *tags;
344 TMTag *tmtag;
346 tags = tm_tags_extract(tags_array, type);
347 if (tags != NULL)
349 tmtag = symbols_find_tm_tag(tags, tag_name);
351 g_ptr_array_free(tags, TRUE);
353 if (tmtag != NULL)
354 return tmtag;
356 return NULL; /* not found */
360 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
362 guint j;
363 const GPtrArray *source_files = NULL;
365 if (app->tm_workspace != NULL)
366 source_files = app->tm_workspace->source_files;
368 if (source_files != NULL)
370 for (j = 0; j < source_files->len; j++)
372 TMSourceFile *srcfile = source_files->pdata[j];
373 TMTag *tmtag;
375 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
376 if (tmtag != NULL)
377 return tmtag;
380 return NULL; /* not found */
384 const gchar **symbols_get_html_entities(void)
386 if (html_entities == NULL)
387 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
389 return (const gchar **) html_entities;
393 /* sort by name, then line */
394 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
396 gint ret;
398 if (tag_a == NULL || tag_b == NULL)
399 return 0;
401 if (tag_a->name == NULL)
402 return -(tag_a->name != tag_b->name);
404 if (tag_b->name == NULL)
405 return tag_a->name != tag_b->name;
407 ret = strcmp(tag_a->name, tag_b->name);
408 if (ret == 0)
410 return tag_a->line - tag_b->line;
412 return ret;
416 /* sort by line, then scope */
417 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
419 const TMTag *tag_a = TM_TAG(a);
420 const TMTag *tag_b = TM_TAG(b);
421 gint ret;
423 if (a == NULL || b == NULL)
424 return 0;
426 ret = tag_a->line - tag_b->line;
427 if (ret == 0)
429 if (tag_a->scope == NULL)
430 return -(tag_a->scope != tag_b->scope);
431 if (tag_b->scope == NULL)
432 return tag_a->scope != tag_b->scope;
433 else
434 return strcmp(tag_a->scope, tag_b->scope);
436 return ret;
440 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
442 GList *tag_names = NULL;
443 TMTag *tag;
444 guint i;
446 g_return_val_if_fail(doc, NULL);
448 if (! doc->tm_file || ! doc->tm_file->tags_array)
449 return NULL;
451 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
453 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
454 if (G_UNLIKELY(tag == NULL))
455 return NULL;
457 if (tag->type & tag_types)
459 tag_names = g_list_prepend(tag_names, tag);
462 tag_names = g_list_sort(tag_names, compare_symbol_lines);
463 return tag_names;
467 /* amount of types in the symbol list (currently max. 8 are used) */
468 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
470 struct TreeviewSymbols
472 GtkTreeIter tag_function;
473 GtkTreeIter tag_class;
474 GtkTreeIter tag_macro;
475 GtkTreeIter tag_member;
476 GtkTreeIter tag_variable;
477 GtkTreeIter tag_externvar;
478 GtkTreeIter tag_namespace;
479 GtkTreeIter tag_struct;
480 GtkTreeIter tag_interface;
481 GtkTreeIter tag_type;
482 GtkTreeIter tag_other;
483 } tv_iters;
486 static void init_tag_iters(void)
488 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
489 * filetypes(e.g. config file to Python crashes Geany without this) */
490 tv_iters.tag_function.stamp = -1;
491 tv_iters.tag_class.stamp = -1;
492 tv_iters.tag_member.stamp = -1;
493 tv_iters.tag_macro.stamp = -1;
494 tv_iters.tag_variable.stamp = -1;
495 tv_iters.tag_externvar.stamp = -1;
496 tv_iters.tag_namespace.stamp = -1;
497 tv_iters.tag_struct.stamp = -1;
498 tv_iters.tag_interface.stamp = -1;
499 tv_iters.tag_type.stamp = -1;
500 tv_iters.tag_other.stamp = -1;
504 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
506 static GtkIconTheme *icon_theme = NULL;
507 static gint x = -1;
509 if (G_UNLIKELY(x < 0))
511 gint dummy;
512 icon_theme = gtk_icon_theme_get_default();
513 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
515 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
519 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
521 GtkTreeModel *model = GTK_TREE_MODEL(store);
523 if (!gtk_tree_model_get_iter_first(model, iter))
524 return FALSE;
527 gchar *candidate;
529 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
530 /* FIXME: what if 2 different items have the same name?
531 * this should never happen, but might be caused by a typo in a translation */
532 if (utils_str_equal(candidate, title))
534 g_free(candidate);
535 return TRUE;
537 else
538 g_free(candidate);
540 while (gtk_tree_model_iter_next(model, iter));
542 return FALSE;
546 /* Adds symbol list groups in (iter*, title) pairs.
547 * The list must be ended with NULL. */
548 static void G_GNUC_NULL_TERMINATED
549 tag_list_add_groups(GtkTreeStore *tree_store, ...)
551 va_list args;
552 GtkTreeIter *iter;
554 g_return_if_fail(top_level_iter_names);
556 va_start(args, tree_store);
557 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
559 gchar *title = va_arg(args, gchar*);
560 gchar *icon_name = va_arg(args, gchar *);
561 GdkPixbuf *icon = NULL;
563 if (icon_name)
565 icon = get_tag_icon(icon_name);
568 g_assert(title != NULL);
569 g_ptr_array_add(top_level_iter_names, title);
571 if (!find_toplevel_iter(tree_store, iter, title))
572 gtk_tree_store_append(tree_store, iter, NULL);
574 if (G_IS_OBJECT(icon))
576 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
577 g_object_unref(icon);
579 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
581 va_end(args);
585 static void add_top_level_items(GeanyDocument *doc)
587 filetype_id ft_id = doc->file_type->id;
588 GtkTreeStore *tag_store = doc->priv->tag_store;
590 if (top_level_iter_names == NULL)
591 top_level_iter_names = g_ptr_array_new();
592 else
593 g_ptr_array_set_size(top_level_iter_names, 0);
595 init_tag_iters();
597 switch (ft_id)
599 case GEANY_FILETYPES_DIFF:
601 tag_list_add_groups(tag_store,
602 &(tv_iters.tag_function), _("Files"), NULL, NULL);
603 break;
605 case GEANY_FILETYPES_DOCBOOK:
607 tag_list_add_groups(tag_store,
608 &(tv_iters.tag_function), _("Chapter"), NULL,
609 &(tv_iters.tag_class), _("Section"), NULL,
610 &(tv_iters.tag_member), _("Sect1"), NULL,
611 &(tv_iters.tag_macro), _("Sect2"), NULL,
612 &(tv_iters.tag_variable), _("Sect3"), NULL,
613 &(tv_iters.tag_struct), _("Appendix"), NULL,
614 &(tv_iters.tag_other), _("Other"), NULL,
615 NULL);
616 break;
618 case GEANY_FILETYPES_HASKELL:
619 tag_list_add_groups(tag_store,
620 &tv_iters.tag_namespace, _("Module"), NULL,
621 &tv_iters.tag_type, _("Types"), NULL,
622 &tv_iters.tag_macro, _("Type constructors"), NULL,
623 &tv_iters.tag_function, _("Functions"), "classviewer-method",
624 NULL);
625 break;
626 case GEANY_FILETYPES_COBOL:
627 tag_list_add_groups(tag_store,
628 &tv_iters.tag_class, _("Program"), "classviewer-class",
629 &tv_iters.tag_function, _("File"), "classviewer-method",
630 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
631 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
632 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
633 &tv_iters.tag_variable, _("Data"), "classviewer-var",
634 NULL);
635 break;
636 case GEANY_FILETYPES_CONF:
637 tag_list_add_groups(tag_store,
638 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
639 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
640 NULL);
641 break;
642 case GEANY_FILETYPES_NSIS:
643 tag_list_add_groups(tag_store,
644 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
645 &tv_iters.tag_function, _("Functions"), "classviewer-method",
646 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
647 NULL);
648 break;
649 case GEANY_FILETYPES_LATEX:
651 tag_list_add_groups(tag_store,
652 &(tv_iters.tag_function), _("Command"), NULL,
653 &(tv_iters.tag_class), _("Environment"), NULL,
654 &(tv_iters.tag_member), _("Section"), NULL,
655 &(tv_iters.tag_macro), _("Subsection"), NULL,
656 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
657 &(tv_iters.tag_struct), _("Label"), NULL,
658 &(tv_iters.tag_namespace), _("Chapter"), NULL,
659 &(tv_iters.tag_other), _("Other"), NULL,
660 NULL);
661 break;
663 case GEANY_FILETYPES_MATLAB:
665 tag_list_add_groups(tag_store,
666 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
667 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
668 NULL);
669 break;
671 case GEANY_FILETYPES_ABAQUS:
673 tag_list_add_groups(tag_store,
674 &(tv_iters.tag_class), _("Parts"), NULL,
675 &(tv_iters.tag_member), _("Assembly"), NULL,
676 &(tv_iters.tag_namespace), _("Steps"), NULL,
677 NULL);
678 break;
680 case GEANY_FILETYPES_R:
682 tag_list_add_groups(tag_store,
683 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
684 &(tv_iters.tag_other), _("Other"), NULL,
685 NULL);
686 break;
688 case GEANY_FILETYPES_RUST:
690 tag_list_add_groups(tag_store,
691 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
692 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
693 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
694 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
695 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
696 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
697 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
698 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
699 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
700 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
701 NULL);
702 break;
704 case GEANY_FILETYPES_GO:
706 tag_list_add_groups(tag_store,
707 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
708 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
709 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
710 &(tv_iters.tag_type), _("Types"), "classviewer-struct",
711 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
712 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
713 NULL);
714 break;
716 case GEANY_FILETYPES_PERL:
718 tag_list_add_groups(tag_store,
719 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
720 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
721 &(tv_iters.tag_macro), _("Labels"), NULL,
722 &(tv_iters.tag_type), _("Constants"), NULL,
723 &(tv_iters.tag_other), _("Other"), "classviewer-other",
724 NULL);
725 break;
727 case GEANY_FILETYPES_PHP:
729 tag_list_add_groups(tag_store,
730 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
731 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
732 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
733 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
734 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
735 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
736 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
737 NULL);
738 break;
740 case GEANY_FILETYPES_HTML:
742 tag_list_add_groups(tag_store,
743 &(tv_iters.tag_function), _("Functions"), NULL,
744 &(tv_iters.tag_member), _("Anchors"), NULL,
745 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
746 &(tv_iters.tag_class), _("H2 Headings"), NULL,
747 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
748 NULL);
749 break;
751 case GEANY_FILETYPES_CSS:
753 tag_list_add_groups(tag_store,
754 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
755 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
756 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
757 break;
759 case GEANY_FILETYPES_REST:
760 case GEANY_FILETYPES_TXT2TAGS:
761 case GEANY_FILETYPES_ABC:
763 tag_list_add_groups(tag_store,
764 &(tv_iters.tag_namespace), _("Chapter"), NULL,
765 &(tv_iters.tag_member), _("Section"), NULL,
766 &(tv_iters.tag_macro), _("Subsection"), NULL,
767 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
768 NULL);
769 break;
771 case GEANY_FILETYPES_ASCIIDOC:
773 tag_list_add_groups(tag_store,
774 &(tv_iters.tag_namespace), _("Document"), NULL,
775 &(tv_iters.tag_member), _("Section Level 1"), NULL,
776 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
777 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
778 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
779 NULL);
780 break;
782 case GEANY_FILETYPES_RUBY:
784 tag_list_add_groups(tag_store,
785 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
786 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
787 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
788 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
789 NULL);
790 break;
792 case GEANY_FILETYPES_TCL:
794 tag_list_add_groups(tag_store,
795 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
796 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
797 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
798 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
799 NULL);
800 break;
802 case GEANY_FILETYPES_PYTHON:
804 tag_list_add_groups(tag_store,
805 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
806 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
807 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
808 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
809 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
810 NULL);
811 break;
813 case GEANY_FILETYPES_VHDL:
815 tag_list_add_groups(tag_store,
816 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
817 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
818 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
819 &(tv_iters.tag_type), _("Types"), "classviewer-other",
820 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
821 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
822 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
823 &(tv_iters.tag_other), _("Other"), "classviewer-other",
824 NULL);
825 break;
827 case GEANY_FILETYPES_VERILOG:
829 tag_list_add_groups(tag_store,
830 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
831 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
832 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
833 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
834 &(tv_iters.tag_other), _("Other"), "classviewer-other",
835 NULL);
836 break;
838 case GEANY_FILETYPES_JAVA:
840 tag_list_add_groups(tag_store,
841 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
842 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
843 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
844 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
845 &(tv_iters.tag_member), _("Members"), "classviewer-member",
846 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
847 &(tv_iters.tag_other), _("Other"), "classviewer-other",
848 NULL);
849 break;
851 case GEANY_FILETYPES_AS:
853 tag_list_add_groups(tag_store,
854 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
855 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
856 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
857 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
858 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
859 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
860 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
861 &(tv_iters.tag_other), _("Other"), "classviewer-other",
862 NULL);
863 break;
865 case GEANY_FILETYPES_HAXE:
867 tag_list_add_groups(tag_store,
868 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
869 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
870 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
871 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
872 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
873 &(tv_iters.tag_other), _("Other"), "classviewer-other",
874 NULL);
875 break;
877 case GEANY_FILETYPES_BASIC:
879 tag_list_add_groups(tag_store,
880 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
881 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
882 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
883 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
884 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
885 &(tv_iters.tag_other), _("Other"), "classviewer-other",
886 NULL);
887 break;
889 case GEANY_FILETYPES_F77:
890 case GEANY_FILETYPES_FORTRAN:
892 tag_list_add_groups(tag_store,
893 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
894 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
895 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
896 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
897 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
898 &(tv_iters.tag_class), _("Types"), "classviewer-class",
899 &(tv_iters.tag_member), _("Components"), "classviewer-member",
900 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
901 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
902 &(tv_iters.tag_other), _("Other"), "classviewer-other",
903 NULL);
904 break;
906 case GEANY_FILETYPES_ASM:
908 tag_list_add_groups(tag_store,
909 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
910 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
911 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
912 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
913 NULL);
914 break;
916 case GEANY_FILETYPES_MAKE:
917 tag_list_add_groups(tag_store,
918 &tv_iters.tag_function, _("Targets"), "classviewer-method",
919 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
920 NULL);
921 break;
922 case GEANY_FILETYPES_SQL:
924 tag_list_add_groups(tag_store,
925 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
926 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
927 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
928 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
929 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
930 &(tv_iters.tag_member), _("Views"), "classviewer-var",
931 &(tv_iters.tag_other), _("Other"), "classviewer-other",
932 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
933 NULL);
934 break;
936 case GEANY_FILETYPES_D:
937 default:
939 if (ft_id == GEANY_FILETYPES_D)
940 tag_list_add_groups(tag_store,
941 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
942 else
943 tag_list_add_groups(tag_store,
944 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
946 tag_list_add_groups(tag_store,
947 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
948 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
949 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
950 &(tv_iters.tag_member), _("Members"), "classviewer-member",
951 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
952 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
953 NULL);
955 if (ft_id != GEANY_FILETYPES_D)
957 tag_list_add_groups(tag_store,
958 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
960 tag_list_add_groups(tag_store,
961 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
962 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
963 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
969 /* removes toplevel items that have no children */
970 static void hide_empty_rows(GtkTreeStore *store)
972 GtkTreeIter iter;
973 gboolean cont = TRUE;
975 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
976 return; /* stop when first iter is invalid, i.e. no elements */
978 while (cont)
980 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
981 cont = gtk_tree_store_remove(store, &iter);
982 else
983 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
988 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
990 gchar *utf8_name;
991 const gchar *scope = tag->scope;
992 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
993 gboolean doc_is_utf8 = FALSE;
995 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
996 * for None at this point completely */
997 if (utils_str_equal(doc->encoding, "UTF-8") ||
998 utils_str_equal(doc->encoding, "None"))
999 doc_is_utf8 = TRUE;
1000 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1001 * plugin might have called tm_source_file_update(), so check to be sure */
1002 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1004 if (! doc_is_utf8)
1005 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1006 -1, doc->encoding, TRUE);
1007 else
1008 utf8_name = tag->name;
1010 if (utf8_name == NULL)
1011 return NULL;
1013 if (! buffer)
1014 buffer = g_string_new(NULL);
1015 else
1016 g_string_truncate(buffer, 0);
1018 /* check first char of scope is a wordchar */
1019 if (!found_parent && scope &&
1020 strpbrk(scope, GEANY_WORDCHARS) == scope)
1022 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1024 g_string_append(buffer, scope);
1025 g_string_append(buffer, sep);
1027 g_string_append(buffer, utf8_name);
1029 if (! doc_is_utf8)
1030 g_free(utf8_name);
1032 g_string_append_printf(buffer, " [%lu]", tag->line);
1034 return buffer->str;
1038 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1040 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1042 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1043 * for None at this point completely */
1044 if (utf8_name != NULL &&
1045 ! utils_str_equal(doc->encoding, "UTF-8") &&
1046 ! utils_str_equal(doc->encoding, "None"))
1048 SETPTR(utf8_name,
1049 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1052 return utf8_name;
1056 /* find the last word in "foo::bar::blah", e.g. "blah" */
1057 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1059 const gchar *scope = tag->scope;
1060 const gchar *separator = symbols_get_context_separator(ft_id);
1061 const gchar *str, *ptr;
1063 if (!scope)
1064 return NULL;
1066 str = scope;
1068 while (1)
1070 ptr = strstr(str, separator);
1071 if (ptr)
1073 str = ptr + strlen(separator);
1075 else
1076 break;
1079 return !EMPTY(str) ? str : NULL;
1083 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1085 GtkTreeIter *iter = NULL;
1087 switch (tag_type)
1089 case tm_tag_prototype_t:
1090 case tm_tag_method_t:
1091 case tm_tag_function_t:
1093 iter = &tv_iters.tag_function;
1094 break;
1096 case tm_tag_externvar_t:
1098 iter = &tv_iters.tag_externvar;
1099 break;
1101 case tm_tag_macro_t:
1102 case tm_tag_macro_with_arg_t:
1104 iter = &tv_iters.tag_macro;
1105 break;
1107 case tm_tag_class_t:
1109 iter = &tv_iters.tag_class;
1110 break;
1112 case tm_tag_member_t:
1113 case tm_tag_field_t:
1115 iter = &tv_iters.tag_member;
1116 break;
1118 case tm_tag_typedef_t:
1119 case tm_tag_enum_t:
1121 iter = &tv_iters.tag_type;
1122 break;
1124 case tm_tag_union_t:
1125 case tm_tag_struct_t:
1127 iter = &tv_iters.tag_struct;
1128 break;
1130 case tm_tag_interface_t:
1131 iter = &tv_iters.tag_interface;
1132 break;
1133 case tm_tag_variable_t:
1135 iter = &tv_iters.tag_variable;
1136 break;
1138 case tm_tag_namespace_t:
1139 case tm_tag_package_t:
1141 iter = &tv_iters.tag_namespace;
1142 break;
1144 default:
1146 iter = &tv_iters.tag_other;
1149 if (G_LIKELY(iter->stamp != -1))
1150 return iter;
1151 else
1152 return NULL;
1156 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1158 GdkPixbuf *icon = NULL;
1160 if (parent == &tv_iters.tag_other)
1162 return get_tag_icon("classviewer-var");
1164 /* copy parent icon */
1165 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1166 SYMBOLS_COLUMN_ICON, &icon, -1);
1167 return icon;
1171 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1173 const TMTag *t1 = v1;
1174 const TMTag *t2 = v2;
1176 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1177 utils_str_equal(t1->scope, t2->scope) &&
1178 /* include arglist in match to support e.g. C++ overloading */
1179 utils_str_equal(t1->arglist, t2->arglist));
1183 /* inspired from g_str_hash() */
1184 static guint tag_hash(gconstpointer v)
1186 const TMTag *tag = v;
1187 const gchar *p;
1188 guint32 h = 5381;
1190 h = (h << 5) + h + tag->type;
1191 for (p = tag->name; *p != '\0'; p++)
1192 h = (h << 5) + h + *p;
1193 if (tag->scope)
1195 for (p = tag->scope; *p != '\0'; p++)
1196 h = (h << 5) + h + *p;
1198 /* for e.g. C++ overloading */
1199 if (tag->arglist)
1201 for (p = tag->arglist; *p != '\0'; p++)
1202 h = (h << 5) + h + *p;
1205 return h;
1209 /* like gtk_tree_view_expand_to_path() but with an iter */
1210 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1212 GtkTreeModel *model = gtk_tree_view_get_model(view);
1213 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1215 gtk_tree_view_expand_to_path(view, path);
1216 gtk_tree_path_free(path);
1220 /* like gtk_tree_store_remove() but finds the next iter at any level */
1221 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1223 GtkTreeIter parent;
1224 gboolean has_parent;
1225 gboolean cont;
1227 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1228 cont = gtk_tree_store_remove(store, iter);
1229 /* if there is no next at this level but there is a parent iter, continue from it */
1230 if (! cont && has_parent)
1232 *iter = parent;
1233 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1236 return cont;
1240 /* adds a new element in the parent table if it's key is known.
1241 * duplicates are kept */
1242 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1243 const GtkTreeIter *iter)
1245 GList **list;
1246 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1247 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1249 if (! list)
1251 list = g_slice_alloc(sizeof *list);
1252 *list = NULL;
1253 g_hash_table_insert(table, tag->name, list);
1255 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1260 static void free_iter_slice_list(gpointer data)
1262 GList **list = data;
1264 if (list)
1266 GList *node;
1267 foreach_list(node, *list)
1268 g_slice_free(GtkTreeIter, node->data);
1269 g_list_free(*list);
1270 g_slice_free1(sizeof *list, list);
1275 /* inserts a @data in @table on key @tag.
1276 * previous data is not overwritten if the key is duplicated, but rather the
1277 * two values are kept in a list
1279 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1280 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1282 GList *list = g_hash_table_lookup(table, tag);
1283 list = g_list_prepend(list, data);
1284 g_hash_table_insert(table, tag, list);
1288 /* looks up the entry in @table that better matches @tag.
1289 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1290 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1292 GList *data = NULL;
1293 GList *node = g_hash_table_lookup(table, tag);
1294 if (node)
1296 glong delta;
1297 data = node->data;
1299 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1301 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1302 for (node = node->next; node; node = node->next)
1304 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1306 if (d < delta)
1308 data = node->data;
1309 delta = d;
1313 #undef TAG_DELTA
1316 return data;
1320 /* removes the element at @tag from @table.
1321 * @tag must be the exact pointer used at insertion time */
1322 static void tags_table_remove(GHashTable *table, TMTag *tag)
1324 GList *list = g_hash_table_lookup(table, tag);
1325 if (list)
1327 GList *node;
1328 foreach_list(node, list)
1330 if (((GList *) node->data)->data == tag)
1331 break;
1333 list = g_list_delete_link(list, node);
1334 if (list)
1335 g_hash_table_insert(table, tag, list);
1336 else
1337 g_hash_table_remove(table, tag);
1342 static void tags_table_destroy(GHashTable *table)
1344 /* free any leftover elements. note that we can't register a value_free_func when
1345 * creating the hash table because we only want to free it when destroying the table,
1346 * not when inserting a duplicate (we handle this manually) */
1347 GHashTableIter iter;
1348 gpointer value;
1350 g_hash_table_iter_init(&iter, table);
1351 while (g_hash_table_iter_next(&iter, NULL, &value))
1352 g_list_free(value);
1353 g_hash_table_destroy(table);
1358 * Updates the tag tree for a document with the tags in *list.
1359 * @param doc a document
1360 * @param tags a pointer to a GList* holding the tags to add/update. This
1361 * list may be updated, removing updated elements.
1363 * The update is done in two passes:
1364 * 1) walking the current tree, update tags that still exist and remove the
1365 * obsolescent ones;
1366 * 2) walking the remaining (non updated) tags, adds them in the list.
1368 * For better performances, we use 2 hash tables:
1369 * - one containing all the tags for lookup in the first pass (actually stores a
1370 * reference in the tags list for removing it efficiently), avoiding list search
1371 * on each tag;
1372 * - the other holding "tag-name":row references for tags having children, used to
1373 * lookup for a parent in both passes, avoiding tree traversal.
1375 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1377 GtkTreeStore *store = doc->priv->tag_store;
1378 GtkTreeModel *model = GTK_TREE_MODEL(store);
1379 GHashTable *parents_table;
1380 GHashTable *tags_table;
1381 GtkTreeIter iter;
1382 gboolean cont;
1383 GList *item;
1385 /* Build hash tables holding tags and parents */
1386 /* parent table holds "tag-name":GtkTreeIter */
1387 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1388 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1389 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1390 foreach_list(item, *tags)
1392 TMTag *tag = item->data;
1393 const gchar *name;
1395 tags_table_insert(tags_table, tag, item);
1397 name = get_parent_name(tag, doc->file_type->id);
1398 if (name)
1399 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1402 /* First pass, update existing rows or delete them.
1403 * It is OK to delete them since we walk top down so we would remove
1404 * parents before checking for their children, thus never implicitly
1405 * deleting an updated child */
1406 cont = gtk_tree_model_get_iter_first(model, &iter);
1407 while (cont)
1409 TMTag *tag;
1411 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1412 if (! tag) /* most probably a toplevel, skip it */
1413 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1414 else
1416 GList *found_item;
1418 found_item = tags_table_lookup(tags_table, tag);
1419 if (! found_item) /* tag doesn't exist, remove it */
1420 cont = tree_store_remove_row(store, &iter);
1421 else /* tag still exist, update it */
1423 const gchar *name;
1424 const gchar *parent_name;
1425 TMTag *found = found_item->data;
1427 parent_name = get_parent_name(found, doc->file_type->id);
1428 /* if parent is unknown, ignore it */
1429 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1430 parent_name = NULL;
1432 /* only update fields that (can) have changed (name that holds line
1433 * number, and the tag itself) */
1434 name = get_symbol_name(doc, found, parent_name != NULL);
1435 gtk_tree_store_set(store, &iter,
1436 SYMBOLS_COLUMN_NAME, name,
1437 SYMBOLS_COLUMN_TAG, found,
1438 -1);
1440 update_parents_table(parents_table, found, parent_name, &iter);
1442 /* remove the updated tag from the table and list */
1443 tags_table_remove(tags_table, found);
1444 *tags = g_list_delete_link(*tags, found_item);
1446 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1449 tm_tag_unref(tag);
1453 /* Second pass, now we have a tree cleaned up from invalid rows,
1454 * we simply add new ones */
1455 foreach_list (item, *tags)
1457 TMTag *tag = item->data;
1458 GtkTreeIter *parent;
1460 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1461 if (G_UNLIKELY(! parent))
1462 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1463 else
1465 gboolean expand;
1466 const gchar *name;
1467 const gchar *parent_name;
1468 gchar *tooltip;
1469 GdkPixbuf *icon = get_child_icon(store, parent);
1471 parent_name = get_parent_name(tag, doc->file_type->id);
1472 if (parent_name)
1474 GList **candidates;
1475 GtkTreeIter *parent_search = NULL;
1477 /* walk parent candidates to find the better one.
1478 * if there are more than one, take the one that has the closest line number
1479 * after the tag we're searching the parent for */
1480 candidates = g_hash_table_lookup(parents_table, parent_name);
1481 if (candidates)
1483 GList *node;
1484 glong delta = G_MAXLONG;
1485 foreach_list(node, *candidates)
1487 TMTag *parent_tag;
1488 glong d;
1490 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1491 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1493 d = tag->line - parent_tag->line;
1494 if (! parent_search || (d >= 0 && d < delta))
1496 delta = d;
1497 parent_search = node->data;
1499 tm_tag_unref(parent_tag);
1503 if (parent_search)
1504 parent = parent_search;
1505 else
1506 parent_name = NULL;
1509 /* only expand to the iter if the parent was empty, otherwise we let the
1510 * folding as it was before (already expanded, or closed by the user) */
1511 expand = ! gtk_tree_model_iter_has_child(model, parent);
1513 /* insert the new element */
1514 gtk_tree_store_append(store, &iter, parent);
1515 name = get_symbol_name(doc, tag, parent_name != NULL);
1516 tooltip = get_symbol_tooltip(doc, tag);
1517 gtk_tree_store_set(store, &iter,
1518 SYMBOLS_COLUMN_NAME, name,
1519 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1520 SYMBOLS_COLUMN_ICON, icon,
1521 SYMBOLS_COLUMN_TAG, tag,
1522 -1);
1523 g_free(tooltip);
1524 if (G_LIKELY(icon))
1525 g_object_unref(icon);
1527 update_parents_table(parents_table, tag, parent_name, &iter);
1529 if (expand)
1530 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1534 g_hash_table_destroy(parents_table);
1535 tags_table_destroy(tags_table);
1539 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1540 * is not stable, so the order is already lost. */
1541 static gint compare_top_level_names(const gchar *a, const gchar *b)
1543 guint i;
1544 const gchar *name;
1546 /* This should never happen as it would mean that two or more top
1547 * level items have the same name but it can happen by typos in the translations. */
1548 if (utils_str_equal(a, b))
1549 return 1;
1551 foreach_ptr_array(name, i, top_level_iter_names)
1553 if (utils_str_equal(name, a))
1554 return -1;
1555 if (utils_str_equal(name, b))
1556 return 1;
1558 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1559 return 0;
1563 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1564 GtkTreeIter *iter)
1566 /* if the tag has a parent tag, it should be at depth >= 2 */
1567 return !EMPTY(tag->scope) &&
1568 gtk_tree_store_iter_depth(store, iter) == 1;
1572 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1573 gpointer user_data)
1575 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1576 TMTag *tag_a, *tag_b;
1577 gint cmp;
1579 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1580 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1582 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1583 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1584 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1585 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1587 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1588 compare_symbol_lines(tag_a, tag_b);
1590 else
1592 gchar *astr, *bstr;
1594 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1595 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1597 /* if a is toplevel, b must be also */
1598 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1600 cmp = compare_top_level_names(astr, bstr);
1602 else
1604 /* this is what g_strcmp0() does */
1605 if (! astr)
1606 cmp = -(astr != bstr);
1607 else if (! bstr)
1608 cmp = astr != bstr;
1609 else
1611 cmp = strcmp(astr, bstr);
1613 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1614 if (tag_a && tag_b)
1615 if (!sort_by_name ||
1616 (utils_str_equal(tag_a->name, tag_b->name) &&
1617 utils_str_equal(tag_a->scope, tag_b->scope)))
1618 cmp = compare_symbol_lines(tag_a, tag_b);
1621 g_free(astr);
1622 g_free(bstr);
1624 tm_tag_unref(tag_a);
1625 tm_tag_unref(tag_b);
1627 return cmp;
1631 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1633 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1634 GINT_TO_POINTER(sort_by_name), NULL);
1636 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1640 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1642 GList *tags;
1644 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1646 tags = get_tag_list(doc, tm_tag_max_t);
1647 if (tags == NULL)
1648 return FALSE;
1650 /* FIXME: Not sure why we detached the model here? */
1652 /* disable sorting during update because the code doesn't support correctly
1653 * models that are currently being built */
1654 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1656 /* add grandparent type iters */
1657 add_top_level_items(doc);
1659 update_tree_tags(doc, &tags);
1660 g_list_free(tags);
1662 hide_empty_rows(doc->priv->tag_store);
1664 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1665 sort_mode = doc->priv->symbol_list_sort_mode;
1667 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1668 doc->priv->symbol_list_sort_mode = sort_mode;
1670 return TRUE;
1674 /* Detects a global tags filetype from the *.lang.* language extension.
1675 * Returns NULL if there was no matching TM language. */
1676 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1678 gchar *tags_ext;
1679 gchar *shortname = utils_strdupa(utf8_filename);
1680 GeanyFiletype *ft = NULL;
1682 tags_ext = g_strrstr(shortname, ".tags");
1683 if (tags_ext)
1685 *tags_ext = '\0'; /* remove .tags extension */
1686 ft = filetypes_detect_from_extension(shortname);
1687 if (ft->id != GEANY_FILETYPES_NONE)
1688 return ft;
1690 return NULL;
1694 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1695 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1696 * the relevant path.
1697 * Example:
1698 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1699 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1701 /* -E pre-process, -dD output user macros, -p prof info (?) */
1702 const char pre_process[] = "gcc -E -dD -p -I.";
1704 if (argc > 2)
1706 /* Create global taglist */
1707 int status;
1708 char *command;
1709 const char *tags_file = argv[1];
1710 char *utf8_fname;
1711 GeanyFiletype *ft;
1713 utf8_fname = utils_get_utf8_from_locale(tags_file);
1714 ft = detect_global_tags_filetype(utf8_fname);
1715 g_free(utf8_fname);
1717 if (ft == NULL)
1719 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1720 return 1;
1722 /* load config in case of custom filetypes */
1723 filetypes_load_config(ft->id, FALSE);
1725 /* load ignore list for C/C++ parser */
1726 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1727 load_c_ignore_tags();
1729 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1731 const gchar *cflags = getenv("CFLAGS");
1732 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1734 else
1735 command = NULL; /* don't preprocess */
1737 geany_debug("Generating %s tags file.", ft->name);
1738 tm_get_workspace();
1739 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1740 argc - 2, tags_file, ft->lang);
1741 g_free(command);
1742 symbols_finalize(); /* free c_tags_ignore data */
1743 if (! status)
1745 g_printerr(_("Failed to create tags file, perhaps because no tags "
1746 "were found.\n"));
1747 return 1;
1750 else
1752 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1753 g_printerr(_("Example:\n"
1754 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1755 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1756 return 1;
1758 return 0;
1762 void symbols_show_load_tags_dialog(void)
1764 GtkWidget *dialog;
1765 GtkFileFilter *filter;
1767 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1768 GTK_FILE_CHOOSER_ACTION_OPEN,
1769 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1770 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1771 NULL);
1772 gtk_widget_set_name(dialog, "GeanyDialog");
1773 filter = gtk_file_filter_new();
1774 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1775 gtk_file_filter_add_pattern(filter, "*.*.tags");
1776 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1778 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1780 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1781 GSList *item;
1783 for (item = flist; item != NULL; item = g_slist_next(item))
1785 gchar *fname = item->data;
1786 gchar *utf8_fname;
1787 GeanyFiletype *ft;
1789 utf8_fname = utils_get_utf8_from_locale(fname);
1790 ft = detect_global_tags_filetype(utf8_fname);
1792 if (ft != NULL && symbols_load_global_tags(fname, ft))
1793 /* For translators: the first wildcard is the filetype, the second the filename */
1794 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1795 filetypes_get_display_name(ft), utf8_fname);
1796 else
1797 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1799 g_free(utf8_fname);
1800 g_free(fname);
1802 g_slist_free(flist);
1804 gtk_widget_destroy(dialog);
1808 static void init_user_tags(void)
1810 GSList *file_list = NULL, *list = NULL;
1811 const GSList *node;
1812 gchar *dir;
1814 dir = g_build_filename(app->configdir, "tags", NULL);
1815 /* create the user tags dir for next time if it doesn't exist */
1816 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1817 utils_mkdir(dir, FALSE);
1818 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1820 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1821 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1822 g_free(dir);
1824 file_list = g_slist_concat(file_list, list);
1826 /* populate the filetype-specific tag files lists */
1827 for (node = file_list; node != NULL; node = node->next)
1829 gchar *fname = node->data;
1830 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1831 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1833 g_free(utf8_fname);
1835 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1836 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1837 else
1839 geany_debug("Unknown filetype for file '%s'.", fname);
1840 g_free(fname);
1844 /* don't need to delete list contents because they are now stored in
1845 * ft->priv->tag_files */
1846 g_slist_free(file_list);
1850 static void load_user_tags(filetype_id ft_id)
1852 static guchar *tags_loaded = NULL;
1853 static gboolean init_tags = FALSE;
1854 const GSList *node;
1855 GeanyFiletype *ft = filetypes[ft_id];
1857 g_return_if_fail(ft_id > 0);
1859 if (!tags_loaded)
1860 tags_loaded = g_new0(guchar, filetypes_array->len);
1861 if (tags_loaded[ft_id])
1862 return;
1863 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1865 if (!init_tags)
1867 init_user_tags();
1868 init_tags = TRUE;
1871 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1873 const gchar *fname = node->data;
1875 symbols_load_global_tags(fname, ft);
1880 static gboolean goto_tag(const gchar *name, gboolean definition)
1882 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1883 TMTagType type;
1884 TMTag *tmtag = NULL;
1885 GeanyDocument *old_doc = document_get_current();
1887 /* goto tag definition: all except prototypes / forward declarations / externs */
1888 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1890 /* first look in the current document */
1891 if (old_doc != NULL && old_doc->tm_file)
1892 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1894 /* if not found, look in the workspace */
1895 if (tmtag == NULL)
1896 tmtag = find_workspace_tag(name, type);
1898 if (tmtag != NULL)
1900 GeanyDocument *new_doc = document_find_by_real_path(
1901 tmtag->file->file_name);
1903 if (new_doc)
1905 /* If we are already on the tag line, swap definition/declaration */
1906 if (new_doc == old_doc &&
1907 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1909 if (goto_tag(name, !definition))
1910 return TRUE;
1913 else
1915 /* not found in opened document, should open */
1916 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1919 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1920 return TRUE;
1922 return FALSE;
1926 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1928 if (goto_tag(name, definition))
1929 return TRUE;
1931 /* if we are here, there was no match and we are beeping ;-) */
1932 utils_beep();
1934 if (!definition)
1935 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1936 else
1937 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1938 return FALSE;
1942 /* This could perhaps be improved to check for #if, class etc. */
1943 static gint get_function_fold_number(GeanyDocument *doc)
1945 /* for Java the functions are always one fold level above the class scope */
1946 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1947 return SC_FOLDLEVELBASE + 1;
1948 else
1949 return SC_FOLDLEVELBASE;
1953 /* Should be used only with get_current_tag_cached.
1954 * tag_types caching might trigger recomputation too often but this isn't used differently often
1955 * enough to be an issue for now */
1956 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1958 static gint old_line = -2;
1959 static GeanyDocument *old_doc = NULL;
1960 static gint old_fold_num = -1;
1961 static guint old_tag_types = 0;
1962 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1963 gboolean ret;
1965 /* check if the cached line and file index have changed since last time: */
1966 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1967 ret = TRUE;
1968 else if (cur_line == old_line)
1969 ret = FALSE;
1970 else
1972 /* if the line has only changed by 1 */
1973 if (abs(cur_line - old_line) == 1)
1975 /* It's the same function if the fold number hasn't changed */
1976 ret = (fold_num != old_fold_num);
1978 else ret = TRUE;
1981 /* record current line and file index for next time */
1982 old_line = cur_line;
1983 old_doc = doc;
1984 old_fold_num = fold_num;
1985 old_tag_types = tag_types;
1986 return ret;
1990 /* Parse the function name up to 2 lines before tag_line.
1991 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1992 * type or argument names can be confused with the function name. */
1993 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1995 gint start, end, max_pos;
1996 gint fn_style;
1998 switch (sci_get_lexer(sci))
2000 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2001 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2002 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2004 start = sci_get_position_from_line(sci, tag_line - 2);
2005 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2006 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2007 start++;
2009 end = start;
2010 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2011 end++;
2013 if (start == end)
2014 return NULL;
2015 return sci_get_contents_range(sci, start, end);
2019 /* Parse the function name */
2020 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2022 gint start, end, first_pos, max_pos;
2023 gint tmp;
2024 gchar c;
2026 first_pos = end = sci_get_position_from_line(sci, tag_line);
2027 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2028 tmp = 0;
2029 /* goto the begin of function body */
2030 while (end < max_pos &&
2031 (tmp = sci_get_char_at(sci, end)) != '{' &&
2032 tmp != 0) end++;
2033 if (tmp == 0) end --;
2035 /* go back to the end of function identifier */
2036 while (end > 0 && end > first_pos - 500 &&
2037 (tmp = sci_get_char_at(sci, end)) != '(' &&
2038 tmp != 0) end--;
2039 end--;
2040 if (end < 0) end = 0;
2042 /* skip whitespaces between identifier and ( */
2043 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2045 start = end;
2046 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2047 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2048 || tmp == SCE_C_GLOBALCLASS
2049 || (c = sci_get_char_at(sci, start)) == '~'
2050 || c == ':'))
2051 start--;
2052 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2054 if (start == end) return NULL;
2055 return sci_get_contents_range(sci, start, end + 1);
2059 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2061 gint line_count = sci_get_line_count(sci);
2063 for (; line < line_count; line++)
2065 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2066 return line;
2069 return -1;
2073 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2075 gint line;
2076 gint parent;
2078 line = sci_get_current_line(doc->editor->sci);
2079 parent = sci_get_fold_parent(doc->editor->sci, line);
2080 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2081 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2082 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2084 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2086 if (tag)
2088 gint tag_line = tag->line - 1;
2089 gint last_child = line + 1;
2091 /* if it may be a false positive because we're inside a fold level not inside anything
2092 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2093 * right after the tag we got from TM */
2094 if (abs(tag_line - parent) > 1)
2096 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2097 if (tag_fold >= 0)
2098 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2101 if (line <= last_child)
2103 if (tag->scope)
2104 *tagname = g_strconcat(tag->scope,
2105 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2106 else
2107 *tagname = g_strdup(tag->name);
2109 return tag_line;
2113 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2114 * to dirty and inaccurate hand-parsing */
2115 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2117 const gint fn_fold = get_function_fold_number(doc);
2118 gint tag_line = parent;
2119 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2121 /* find the top level fold point */
2122 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2124 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2125 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2128 if (tag_line >= 0)
2130 gchar *cur_tag;
2132 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2133 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2134 else
2135 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2137 if (cur_tag != NULL)
2139 *tagname = cur_tag;
2140 return tag_line;
2145 *tagname = g_strdup(_("unknown"));
2146 return -1;
2150 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2152 static gint tag_line = -1;
2153 static gchar *cur_tag = NULL;
2155 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2157 if (doc == NULL) /* reset current function */
2159 current_tag_changed(NULL, -1, -1, 0);
2160 g_free(cur_tag);
2161 cur_tag = g_strdup(_("unknown"));
2162 if (tagname != NULL)
2163 *tagname = cur_tag;
2164 tag_line = -1;
2166 else
2168 gint line = sci_get_current_line(doc->editor->sci);
2169 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2171 if (current_tag_changed(doc, line, fold_level, tag_types))
2173 g_free(cur_tag);
2174 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2176 *tagname = cur_tag;
2179 return tag_line;
2183 /* Sets *tagname to point at the current function or tag name.
2184 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2185 * call to this function.
2186 * Returns: line number of the current tag, or -1 if unknown. */
2187 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2189 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2193 /* same as symbols_get_current_function() but finds class, namespaces and more */
2194 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2196 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2197 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2199 /* Python parser reports imports as namespaces which confuses the scope detection */
2200 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2201 tag_types |= tm_tag_namespace_t;
2203 return get_current_tag_name_cached(doc, tagname, tag_types);
2207 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2209 gint sort_mode = GPOINTER_TO_INT(user_data);
2210 GeanyDocument *doc = document_get_current();
2212 if (ignore_callback)
2213 return;
2215 if (doc != NULL)
2216 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2220 static void on_symbol_tree_menu_show(GtkWidget *widget,
2221 gpointer user_data)
2223 GeanyDocument *doc = document_get_current();
2224 gboolean enable;
2226 enable = doc && doc->has_tags;
2227 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2228 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2229 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2230 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2231 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2232 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2234 if (! doc)
2235 return;
2237 ignore_callback = TRUE;
2239 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2240 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2241 else
2242 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2244 ignore_callback = FALSE;
2248 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2250 gboolean expand = GPOINTER_TO_INT(user_data);
2251 GeanyDocument *doc = document_get_current();
2253 if (! doc)
2254 return;
2256 g_return_if_fail(doc->priv->tag_tree);
2258 if (expand)
2259 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2260 else
2261 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2265 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2267 GtkTreeIter iter;
2268 GtkTreeSelection *selection;
2269 GtkTreeModel *model;
2270 GeanyDocument *doc;
2271 TMTag *tag = NULL;
2273 doc = document_get_current();
2274 if (!doc)
2275 return;
2277 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2278 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2279 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2280 if (tag)
2282 if (widget == symbol_menu.find_in_files)
2283 search_show_find_in_files_dialog_full(tag->name, NULL);
2284 else
2285 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2286 widget == symbol_menu.find_usage);
2288 tm_tag_unref(tag);
2293 static void create_taglist_popup_menu(void)
2295 GtkWidget *item, *menu;
2297 tv.popup_taglist = menu = gtk_menu_new();
2299 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2300 gtk_widget_show(item);
2301 gtk_container_add(GTK_CONTAINER(menu), item);
2302 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2304 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2305 gtk_widget_show(item);
2306 gtk_container_add(GTK_CONTAINER(menu), item);
2307 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2309 item = gtk_separator_menu_item_new();
2310 gtk_widget_show(item);
2311 gtk_container_add(GTK_CONTAINER(menu), item);
2313 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2314 _("Sort by _Name"));
2315 gtk_widget_show(item);
2316 gtk_container_add(GTK_CONTAINER(menu), item);
2317 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2318 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2320 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2321 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2322 gtk_widget_show(item);
2323 gtk_container_add(GTK_CONTAINER(menu), item);
2324 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2325 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2327 item = gtk_separator_menu_item_new();
2328 gtk_widget_show(item);
2329 gtk_container_add(GTK_CONTAINER(menu), item);
2331 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2332 gtk_widget_show(item);
2333 gtk_container_add(GTK_CONTAINER(menu), item);
2334 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2336 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2337 gtk_widget_show(item);
2338 gtk_container_add(GTK_CONTAINER(menu), item);
2339 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2341 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2342 gtk_widget_show(item);
2343 gtk_container_add(GTK_CONTAINER(menu), item);
2344 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2346 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2348 sidebar_add_common_menu_items(GTK_MENU(menu));
2352 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2354 gchar *f;
2356 g_return_if_fail(!EMPTY(doc->real_path));
2358 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2359 if (utils_str_equal(doc->real_path, f))
2360 load_c_ignore_tags();
2362 g_free(f);
2366 void symbols_init(void)
2368 gchar *f;
2370 create_taglist_popup_menu();
2372 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2373 ui_add_config_file_menu_item(f, NULL, NULL);
2374 g_free(f);
2376 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2380 void symbols_finalize(void)
2382 g_strfreev(html_entities);
2383 g_strfreev(c_tags_ignore);