Merge pull request #464 from eht16/undeprecate_plugins
[geany-mirror.git] / src / symbols.c
blob311205bdd4e2205c7fc4f5b6a77507c80b9ddb17
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 case GEANY_FILETYPES_ZEPHIR:
307 return "::";
309 /* avoid confusion with other possible separators in group/section name */
310 case GEANY_FILETYPES_CONF:
311 case GEANY_FILETYPES_REST:
312 return ":::";
314 /* no context separator */
315 case GEANY_FILETYPES_ASCIIDOC:
316 case GEANY_FILETYPES_TXT2TAGS:
317 return "\x03";
319 default:
320 return ".";
325 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
326 static TMTag *
327 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
329 guint i;
330 g_return_val_if_fail(tags != NULL, NULL);
332 for (i = 0; i < tags->len; ++i)
334 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
335 return TM_TAG(tags->pdata[i]);
337 return NULL;
341 static TMTag *find_source_file_tag(GPtrArray *tags_array,
342 const gchar *tag_name, guint type)
344 GPtrArray *tags;
345 TMTag *tmtag;
347 tags = tm_tags_extract(tags_array, type);
348 if (tags != NULL)
350 tmtag = symbols_find_tm_tag(tags, tag_name);
352 g_ptr_array_free(tags, TRUE);
354 if (tmtag != NULL)
355 return tmtag;
357 return NULL; /* not found */
361 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
363 guint j;
364 const GPtrArray *source_files = NULL;
366 if (app->tm_workspace != NULL)
367 source_files = app->tm_workspace->source_files;
369 if (source_files != NULL)
371 for (j = 0; j < source_files->len; j++)
373 TMSourceFile *srcfile = source_files->pdata[j];
374 TMTag *tmtag;
376 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
377 if (tmtag != NULL)
378 return tmtag;
381 return NULL; /* not found */
385 const gchar **symbols_get_html_entities(void)
387 if (html_entities == NULL)
388 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
390 return (const gchar **) html_entities;
394 /* sort by name, then line */
395 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
397 gint ret;
399 if (tag_a == NULL || tag_b == NULL)
400 return 0;
402 if (tag_a->name == NULL)
403 return -(tag_a->name != tag_b->name);
405 if (tag_b->name == NULL)
406 return tag_a->name != tag_b->name;
408 ret = strcmp(tag_a->name, tag_b->name);
409 if (ret == 0)
411 return tag_a->line - tag_b->line;
413 return ret;
417 /* sort by line, then scope */
418 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
420 const TMTag *tag_a = TM_TAG(a);
421 const TMTag *tag_b = TM_TAG(b);
422 gint ret;
424 if (a == NULL || b == NULL)
425 return 0;
427 ret = tag_a->line - tag_b->line;
428 if (ret == 0)
430 if (tag_a->scope == NULL)
431 return -(tag_a->scope != tag_b->scope);
432 if (tag_b->scope == NULL)
433 return tag_a->scope != tag_b->scope;
434 else
435 return strcmp(tag_a->scope, tag_b->scope);
437 return ret;
441 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
443 GList *tag_names = NULL;
444 TMTag *tag;
445 guint i;
447 g_return_val_if_fail(doc, NULL);
449 if (! doc->tm_file || ! doc->tm_file->tags_array)
450 return NULL;
452 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
454 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
455 if (G_UNLIKELY(tag == NULL))
456 return NULL;
458 if (tag->type & tag_types)
460 tag_names = g_list_prepend(tag_names, tag);
463 tag_names = g_list_sort(tag_names, compare_symbol_lines);
464 return tag_names;
468 /* amount of types in the symbol list (currently max. 8 are used) */
469 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
471 struct TreeviewSymbols
473 GtkTreeIter tag_function;
474 GtkTreeIter tag_class;
475 GtkTreeIter tag_macro;
476 GtkTreeIter tag_member;
477 GtkTreeIter tag_variable;
478 GtkTreeIter tag_externvar;
479 GtkTreeIter tag_namespace;
480 GtkTreeIter tag_struct;
481 GtkTreeIter tag_interface;
482 GtkTreeIter tag_type;
483 GtkTreeIter tag_other;
484 } tv_iters;
487 static void init_tag_iters(void)
489 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
490 * filetypes(e.g. config file to Python crashes Geany without this) */
491 tv_iters.tag_function.stamp = -1;
492 tv_iters.tag_class.stamp = -1;
493 tv_iters.tag_member.stamp = -1;
494 tv_iters.tag_macro.stamp = -1;
495 tv_iters.tag_variable.stamp = -1;
496 tv_iters.tag_externvar.stamp = -1;
497 tv_iters.tag_namespace.stamp = -1;
498 tv_iters.tag_struct.stamp = -1;
499 tv_iters.tag_interface.stamp = -1;
500 tv_iters.tag_type.stamp = -1;
501 tv_iters.tag_other.stamp = -1;
505 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
507 static GtkIconTheme *icon_theme = NULL;
508 static gint x = -1;
510 if (G_UNLIKELY(x < 0))
512 gint dummy;
513 icon_theme = gtk_icon_theme_get_default();
514 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
516 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
520 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
522 GtkTreeModel *model = GTK_TREE_MODEL(store);
524 if (!gtk_tree_model_get_iter_first(model, iter))
525 return FALSE;
528 gchar *candidate;
530 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
531 /* FIXME: what if 2 different items have the same name?
532 * this should never happen, but might be caused by a typo in a translation */
533 if (utils_str_equal(candidate, title))
535 g_free(candidate);
536 return TRUE;
538 else
539 g_free(candidate);
541 while (gtk_tree_model_iter_next(model, iter));
543 return FALSE;
547 /* Adds symbol list groups in (iter*, title) pairs.
548 * The list must be ended with NULL. */
549 static void G_GNUC_NULL_TERMINATED
550 tag_list_add_groups(GtkTreeStore *tree_store, ...)
552 va_list args;
553 GtkTreeIter *iter;
555 g_return_if_fail(top_level_iter_names);
557 va_start(args, tree_store);
558 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
560 gchar *title = va_arg(args, gchar*);
561 gchar *icon_name = va_arg(args, gchar *);
562 GdkPixbuf *icon = NULL;
564 if (icon_name)
566 icon = get_tag_icon(icon_name);
569 g_assert(title != NULL);
570 g_ptr_array_add(top_level_iter_names, title);
572 if (!find_toplevel_iter(tree_store, iter, title))
573 gtk_tree_store_append(tree_store, iter, NULL);
575 if (G_IS_OBJECT(icon))
577 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
578 g_object_unref(icon);
580 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
582 va_end(args);
586 static void add_top_level_items(GeanyDocument *doc)
588 filetype_id ft_id = doc->file_type->id;
589 GtkTreeStore *tag_store = doc->priv->tag_store;
591 if (top_level_iter_names == NULL)
592 top_level_iter_names = g_ptr_array_new();
593 else
594 g_ptr_array_set_size(top_level_iter_names, 0);
596 init_tag_iters();
598 switch (ft_id)
600 case GEANY_FILETYPES_DIFF:
602 tag_list_add_groups(tag_store,
603 &(tv_iters.tag_function), _("Files"), NULL, NULL);
604 break;
606 case GEANY_FILETYPES_DOCBOOK:
608 tag_list_add_groups(tag_store,
609 &(tv_iters.tag_function), _("Chapter"), NULL,
610 &(tv_iters.tag_class), _("Section"), NULL,
611 &(tv_iters.tag_member), _("Sect1"), NULL,
612 &(tv_iters.tag_macro), _("Sect2"), NULL,
613 &(tv_iters.tag_variable), _("Sect3"), NULL,
614 &(tv_iters.tag_struct), _("Appendix"), NULL,
615 &(tv_iters.tag_other), _("Other"), NULL,
616 NULL);
617 break;
619 case GEANY_FILETYPES_HASKELL:
620 tag_list_add_groups(tag_store,
621 &tv_iters.tag_namespace, _("Module"), NULL,
622 &tv_iters.tag_type, _("Types"), NULL,
623 &tv_iters.tag_macro, _("Type constructors"), NULL,
624 &tv_iters.tag_function, _("Functions"), "classviewer-method",
625 NULL);
626 break;
627 case GEANY_FILETYPES_COBOL:
628 tag_list_add_groups(tag_store,
629 &tv_iters.tag_class, _("Program"), "classviewer-class",
630 &tv_iters.tag_function, _("File"), "classviewer-method",
631 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
632 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
633 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
634 &tv_iters.tag_variable, _("Data"), "classviewer-var",
635 NULL);
636 break;
637 case GEANY_FILETYPES_CONF:
638 tag_list_add_groups(tag_store,
639 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
640 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
641 NULL);
642 break;
643 case GEANY_FILETYPES_NSIS:
644 tag_list_add_groups(tag_store,
645 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
646 &tv_iters.tag_function, _("Functions"), "classviewer-method",
647 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
648 NULL);
649 break;
650 case GEANY_FILETYPES_LATEX:
652 tag_list_add_groups(tag_store,
653 &(tv_iters.tag_function), _("Command"), NULL,
654 &(tv_iters.tag_class), _("Environment"), NULL,
655 &(tv_iters.tag_member), _("Section"), NULL,
656 &(tv_iters.tag_macro), _("Subsection"), NULL,
657 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
658 &(tv_iters.tag_struct), _("Label"), NULL,
659 &(tv_iters.tag_namespace), _("Chapter"), NULL,
660 &(tv_iters.tag_other), _("Other"), NULL,
661 NULL);
662 break;
664 case GEANY_FILETYPES_MATLAB:
666 tag_list_add_groups(tag_store,
667 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
668 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
669 NULL);
670 break;
672 case GEANY_FILETYPES_ABAQUS:
674 tag_list_add_groups(tag_store,
675 &(tv_iters.tag_class), _("Parts"), NULL,
676 &(tv_iters.tag_member), _("Assembly"), NULL,
677 &(tv_iters.tag_namespace), _("Steps"), NULL,
678 NULL);
679 break;
681 case GEANY_FILETYPES_R:
683 tag_list_add_groups(tag_store,
684 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
685 &(tv_iters.tag_other), _("Other"), NULL,
686 NULL);
687 break;
689 case GEANY_FILETYPES_RUST:
691 tag_list_add_groups(tag_store,
692 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
693 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
694 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
695 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
696 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
697 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
698 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
699 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
700 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
701 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
702 NULL);
703 break;
705 case GEANY_FILETYPES_GO:
707 tag_list_add_groups(tag_store,
708 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
709 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
710 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
711 &(tv_iters.tag_type), _("Types"), "classviewer-struct",
712 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
713 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
714 NULL);
715 break;
717 case GEANY_FILETYPES_PERL:
719 tag_list_add_groups(tag_store,
720 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
721 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
722 &(tv_iters.tag_macro), _("Labels"), NULL,
723 &(tv_iters.tag_type), _("Constants"), NULL,
724 &(tv_iters.tag_other), _("Other"), "classviewer-other",
725 NULL);
726 break;
728 case GEANY_FILETYPES_PHP:
729 case GEANY_FILETYPES_ZEPHIR:
731 tag_list_add_groups(tag_store,
732 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
733 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
734 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
735 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
736 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
737 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
738 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
739 NULL);
740 break;
742 case GEANY_FILETYPES_HTML:
744 tag_list_add_groups(tag_store,
745 &(tv_iters.tag_function), _("Functions"), NULL,
746 &(tv_iters.tag_member), _("Anchors"), NULL,
747 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
748 &(tv_iters.tag_class), _("H2 Headings"), NULL,
749 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
750 NULL);
751 break;
753 case GEANY_FILETYPES_CSS:
755 tag_list_add_groups(tag_store,
756 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
757 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
758 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
759 break;
761 case GEANY_FILETYPES_REST:
762 case GEANY_FILETYPES_TXT2TAGS:
763 case GEANY_FILETYPES_ABC:
765 tag_list_add_groups(tag_store,
766 &(tv_iters.tag_namespace), _("Chapter"), NULL,
767 &(tv_iters.tag_member), _("Section"), NULL,
768 &(tv_iters.tag_macro), _("Subsection"), NULL,
769 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
770 NULL);
771 break;
773 case GEANY_FILETYPES_ASCIIDOC:
775 tag_list_add_groups(tag_store,
776 &(tv_iters.tag_namespace), _("Document"), NULL,
777 &(tv_iters.tag_member), _("Section Level 1"), NULL,
778 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
779 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
780 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
781 NULL);
782 break;
784 case GEANY_FILETYPES_RUBY:
786 tag_list_add_groups(tag_store,
787 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
788 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
789 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
790 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
791 NULL);
792 break;
794 case GEANY_FILETYPES_TCL:
796 tag_list_add_groups(tag_store,
797 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
798 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
799 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
800 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
801 NULL);
802 break;
804 case GEANY_FILETYPES_PYTHON:
806 tag_list_add_groups(tag_store,
807 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
808 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
809 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
810 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
811 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
812 NULL);
813 break;
815 case GEANY_FILETYPES_VHDL:
817 tag_list_add_groups(tag_store,
818 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
819 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
820 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
821 &(tv_iters.tag_type), _("Types"), "classviewer-other",
822 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
823 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
824 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
825 &(tv_iters.tag_other), _("Other"), "classviewer-other",
826 NULL);
827 break;
829 case GEANY_FILETYPES_VERILOG:
831 tag_list_add_groups(tag_store,
832 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
833 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
834 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
835 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
836 &(tv_iters.tag_other), _("Other"), "classviewer-other",
837 NULL);
838 break;
840 case GEANY_FILETYPES_JAVA:
842 tag_list_add_groups(tag_store,
843 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
844 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
845 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
846 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
847 &(tv_iters.tag_member), _("Members"), "classviewer-member",
848 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
849 &(tv_iters.tag_other), _("Other"), "classviewer-other",
850 NULL);
851 break;
853 case GEANY_FILETYPES_AS:
855 tag_list_add_groups(tag_store,
856 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
857 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
858 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
859 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
860 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
861 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
862 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
863 &(tv_iters.tag_other), _("Other"), "classviewer-other",
864 NULL);
865 break;
867 case GEANY_FILETYPES_HAXE:
869 tag_list_add_groups(tag_store,
870 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
871 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
872 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
873 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
874 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
875 &(tv_iters.tag_other), _("Other"), "classviewer-other",
876 NULL);
877 break;
879 case GEANY_FILETYPES_BASIC:
881 tag_list_add_groups(tag_store,
882 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
883 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
884 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
885 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
886 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
887 &(tv_iters.tag_other), _("Other"), "classviewer-other",
888 NULL);
889 break;
891 case GEANY_FILETYPES_F77:
892 case GEANY_FILETYPES_FORTRAN:
894 tag_list_add_groups(tag_store,
895 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
896 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
897 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
898 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
899 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
900 &(tv_iters.tag_class), _("Types"), "classviewer-class",
901 &(tv_iters.tag_member), _("Components"), "classviewer-member",
902 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
903 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
904 &(tv_iters.tag_other), _("Other"), "classviewer-other",
905 NULL);
906 break;
908 case GEANY_FILETYPES_ASM:
910 tag_list_add_groups(tag_store,
911 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
912 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
913 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
914 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
915 NULL);
916 break;
918 case GEANY_FILETYPES_MAKE:
919 tag_list_add_groups(tag_store,
920 &tv_iters.tag_function, _("Targets"), "classviewer-method",
921 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
922 NULL);
923 break;
924 case GEANY_FILETYPES_SQL:
926 tag_list_add_groups(tag_store,
927 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
928 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
929 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
930 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
931 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
932 &(tv_iters.tag_member), _("Views"), "classviewer-var",
933 &(tv_iters.tag_other), _("Other"), "classviewer-other",
934 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
935 NULL);
936 break;
938 case GEANY_FILETYPES_D:
939 default:
941 if (ft_id == GEANY_FILETYPES_D)
942 tag_list_add_groups(tag_store,
943 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
944 else
945 tag_list_add_groups(tag_store,
946 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
948 tag_list_add_groups(tag_store,
949 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
950 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
951 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
952 &(tv_iters.tag_member), _("Members"), "classviewer-member",
953 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
954 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
955 NULL);
957 if (ft_id != GEANY_FILETYPES_D)
959 tag_list_add_groups(tag_store,
960 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
962 tag_list_add_groups(tag_store,
963 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
964 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
965 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
971 /* removes toplevel items that have no children */
972 static void hide_empty_rows(GtkTreeStore *store)
974 GtkTreeIter iter;
975 gboolean cont = TRUE;
977 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
978 return; /* stop when first iter is invalid, i.e. no elements */
980 while (cont)
982 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
983 cont = gtk_tree_store_remove(store, &iter);
984 else
985 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
990 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
992 gchar *utf8_name;
993 const gchar *scope = tag->scope;
994 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
995 gboolean doc_is_utf8 = FALSE;
997 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
998 * for None at this point completely */
999 if (utils_str_equal(doc->encoding, "UTF-8") ||
1000 utils_str_equal(doc->encoding, "None"))
1001 doc_is_utf8 = TRUE;
1002 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1003 * plugin might have called tm_source_file_update(), so check to be sure */
1004 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1006 if (! doc_is_utf8)
1007 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1008 -1, doc->encoding, TRUE);
1009 else
1010 utf8_name = tag->name;
1012 if (utf8_name == NULL)
1013 return NULL;
1015 if (! buffer)
1016 buffer = g_string_new(NULL);
1017 else
1018 g_string_truncate(buffer, 0);
1020 /* check first char of scope is a wordchar */
1021 if (!found_parent && scope &&
1022 strpbrk(scope, GEANY_WORDCHARS) == scope)
1024 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1026 g_string_append(buffer, scope);
1027 g_string_append(buffer, sep);
1029 g_string_append(buffer, utf8_name);
1031 if (! doc_is_utf8)
1032 g_free(utf8_name);
1034 g_string_append_printf(buffer, " [%lu]", tag->line);
1036 return buffer->str;
1040 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1042 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1044 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1045 * for None at this point completely */
1046 if (utf8_name != NULL &&
1047 ! utils_str_equal(doc->encoding, "UTF-8") &&
1048 ! utils_str_equal(doc->encoding, "None"))
1050 SETPTR(utf8_name,
1051 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1054 return utf8_name;
1058 /* find the last word in "foo::bar::blah", e.g. "blah" */
1059 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1061 const gchar *scope = tag->scope;
1062 const gchar *separator = symbols_get_context_separator(ft_id);
1063 const gchar *str, *ptr;
1065 if (!scope)
1066 return NULL;
1068 str = scope;
1070 while (1)
1072 ptr = strstr(str, separator);
1073 if (ptr)
1075 str = ptr + strlen(separator);
1077 else
1078 break;
1081 return !EMPTY(str) ? str : NULL;
1085 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type)
1087 GtkTreeIter *iter = NULL;
1089 switch (tag_type)
1091 case tm_tag_prototype_t:
1092 case tm_tag_method_t:
1093 case tm_tag_function_t:
1095 iter = &tv_iters.tag_function;
1096 break;
1098 case tm_tag_externvar_t:
1100 iter = &tv_iters.tag_externvar;
1101 break;
1103 case tm_tag_macro_t:
1104 case tm_tag_macro_with_arg_t:
1106 iter = &tv_iters.tag_macro;
1107 break;
1109 case tm_tag_class_t:
1111 iter = &tv_iters.tag_class;
1112 break;
1114 case tm_tag_member_t:
1115 case tm_tag_field_t:
1117 iter = &tv_iters.tag_member;
1118 break;
1120 case tm_tag_typedef_t:
1121 case tm_tag_enum_t:
1123 iter = &tv_iters.tag_type;
1124 break;
1126 case tm_tag_union_t:
1127 case tm_tag_struct_t:
1129 iter = &tv_iters.tag_struct;
1130 break;
1132 case tm_tag_interface_t:
1133 iter = &tv_iters.tag_interface;
1134 break;
1135 case tm_tag_variable_t:
1137 iter = &tv_iters.tag_variable;
1138 break;
1140 case tm_tag_namespace_t:
1141 case tm_tag_package_t:
1143 iter = &tv_iters.tag_namespace;
1144 break;
1146 default:
1148 iter = &tv_iters.tag_other;
1151 if (G_LIKELY(iter->stamp != -1))
1152 return iter;
1153 else
1154 return NULL;
1158 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1160 GdkPixbuf *icon = NULL;
1162 if (parent == &tv_iters.tag_other)
1164 return get_tag_icon("classviewer-var");
1166 /* copy parent icon */
1167 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1168 SYMBOLS_COLUMN_ICON, &icon, -1);
1169 return icon;
1173 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1175 const TMTag *t1 = v1;
1176 const TMTag *t2 = v2;
1178 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1179 utils_str_equal(t1->scope, t2->scope) &&
1180 /* include arglist in match to support e.g. C++ overloading */
1181 utils_str_equal(t1->arglist, t2->arglist));
1185 /* inspired from g_str_hash() */
1186 static guint tag_hash(gconstpointer v)
1188 const TMTag *tag = v;
1189 const gchar *p;
1190 guint32 h = 5381;
1192 h = (h << 5) + h + tag->type;
1193 for (p = tag->name; *p != '\0'; p++)
1194 h = (h << 5) + h + *p;
1195 if (tag->scope)
1197 for (p = tag->scope; *p != '\0'; p++)
1198 h = (h << 5) + h + *p;
1200 /* for e.g. C++ overloading */
1201 if (tag->arglist)
1203 for (p = tag->arglist; *p != '\0'; p++)
1204 h = (h << 5) + h + *p;
1207 return h;
1211 /* like gtk_tree_view_expand_to_path() but with an iter */
1212 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1214 GtkTreeModel *model = gtk_tree_view_get_model(view);
1215 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1217 gtk_tree_view_expand_to_path(view, path);
1218 gtk_tree_path_free(path);
1222 /* like gtk_tree_store_remove() but finds the next iter at any level */
1223 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1225 GtkTreeIter parent;
1226 gboolean has_parent;
1227 gboolean cont;
1229 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1230 cont = gtk_tree_store_remove(store, iter);
1231 /* if there is no next at this level but there is a parent iter, continue from it */
1232 if (! cont && has_parent)
1234 *iter = parent;
1235 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1238 return cont;
1242 /* adds a new element in the parent table if it's key is known.
1243 * duplicates are kept */
1244 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1245 const GtkTreeIter *iter)
1247 GList **list;
1248 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1249 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1251 if (! list)
1253 list = g_slice_alloc(sizeof *list);
1254 *list = NULL;
1255 g_hash_table_insert(table, tag->name, list);
1257 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1262 static void free_iter_slice_list(gpointer data)
1264 GList **list = data;
1266 if (list)
1268 GList *node;
1269 foreach_list(node, *list)
1270 g_slice_free(GtkTreeIter, node->data);
1271 g_list_free(*list);
1272 g_slice_free1(sizeof *list, list);
1277 /* inserts a @data in @table on key @tag.
1278 * previous data is not overwritten if the key is duplicated, but rather the
1279 * two values are kept in a list
1281 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1282 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1284 GList *list = g_hash_table_lookup(table, tag);
1285 list = g_list_prepend(list, data);
1286 g_hash_table_insert(table, tag, list);
1290 /* looks up the entry in @table that better matches @tag.
1291 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1292 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1294 GList *data = NULL;
1295 GList *node = g_hash_table_lookup(table, tag);
1296 if (node)
1298 glong delta;
1299 data = node->data;
1301 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1303 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1304 for (node = node->next; node; node = node->next)
1306 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1308 if (d < delta)
1310 data = node->data;
1311 delta = d;
1315 #undef TAG_DELTA
1318 return data;
1322 /* removes the element at @tag from @table.
1323 * @tag must be the exact pointer used at insertion time */
1324 static void tags_table_remove(GHashTable *table, TMTag *tag)
1326 GList *list = g_hash_table_lookup(table, tag);
1327 if (list)
1329 GList *node;
1330 foreach_list(node, list)
1332 if (((GList *) node->data)->data == tag)
1333 break;
1335 list = g_list_delete_link(list, node);
1336 if (list)
1337 g_hash_table_insert(table, tag, list);
1338 else
1339 g_hash_table_remove(table, tag);
1344 static void tags_table_destroy(GHashTable *table)
1346 /* free any leftover elements. note that we can't register a value_free_func when
1347 * creating the hash table because we only want to free it when destroying the table,
1348 * not when inserting a duplicate (we handle this manually) */
1349 GHashTableIter iter;
1350 gpointer value;
1352 g_hash_table_iter_init(&iter, table);
1353 while (g_hash_table_iter_next(&iter, NULL, &value))
1354 g_list_free(value);
1355 g_hash_table_destroy(table);
1360 * Updates the tag tree for a document with the tags in *list.
1361 * @param doc a document
1362 * @param tags a pointer to a GList* holding the tags to add/update. This
1363 * list may be updated, removing updated elements.
1365 * The update is done in two passes:
1366 * 1) walking the current tree, update tags that still exist and remove the
1367 * obsolescent ones;
1368 * 2) walking the remaining (non updated) tags, adds them in the list.
1370 * For better performances, we use 2 hash tables:
1371 * - one containing all the tags for lookup in the first pass (actually stores a
1372 * reference in the tags list for removing it efficiently), avoiding list search
1373 * on each tag;
1374 * - the other holding "tag-name":row references for tags having children, used to
1375 * lookup for a parent in both passes, avoiding tree traversal.
1377 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1379 GtkTreeStore *store = doc->priv->tag_store;
1380 GtkTreeModel *model = GTK_TREE_MODEL(store);
1381 GHashTable *parents_table;
1382 GHashTable *tags_table;
1383 GtkTreeIter iter;
1384 gboolean cont;
1385 GList *item;
1387 /* Build hash tables holding tags and parents */
1388 /* parent table holds "tag-name":GtkTreeIter */
1389 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1390 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1391 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1392 foreach_list(item, *tags)
1394 TMTag *tag = item->data;
1395 const gchar *name;
1397 tags_table_insert(tags_table, tag, item);
1399 name = get_parent_name(tag, doc->file_type->id);
1400 if (name)
1401 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1404 /* First pass, update existing rows or delete them.
1405 * It is OK to delete them since we walk top down so we would remove
1406 * parents before checking for their children, thus never implicitly
1407 * deleting an updated child */
1408 cont = gtk_tree_model_get_iter_first(model, &iter);
1409 while (cont)
1411 TMTag *tag;
1413 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1414 if (! tag) /* most probably a toplevel, skip it */
1415 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1416 else
1418 GList *found_item;
1420 found_item = tags_table_lookup(tags_table, tag);
1421 if (! found_item) /* tag doesn't exist, remove it */
1422 cont = tree_store_remove_row(store, &iter);
1423 else /* tag still exist, update it */
1425 const gchar *name;
1426 const gchar *parent_name;
1427 TMTag *found = found_item->data;
1429 parent_name = get_parent_name(found, doc->file_type->id);
1430 /* if parent is unknown, ignore it */
1431 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1432 parent_name = NULL;
1434 /* only update fields that (can) have changed (name that holds line
1435 * number, and the tag itself) */
1436 name = get_symbol_name(doc, found, parent_name != NULL);
1437 gtk_tree_store_set(store, &iter,
1438 SYMBOLS_COLUMN_NAME, name,
1439 SYMBOLS_COLUMN_TAG, found,
1440 -1);
1442 update_parents_table(parents_table, found, parent_name, &iter);
1444 /* remove the updated tag from the table and list */
1445 tags_table_remove(tags_table, found);
1446 *tags = g_list_delete_link(*tags, found_item);
1448 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1451 tm_tag_unref(tag);
1455 /* Second pass, now we have a tree cleaned up from invalid rows,
1456 * we simply add new ones */
1457 foreach_list (item, *tags)
1459 TMTag *tag = item->data;
1460 GtkTreeIter *parent;
1462 parent = get_tag_type_iter(tag->type);
1463 if (G_UNLIKELY(! parent))
1464 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1465 else
1467 gboolean expand;
1468 const gchar *name;
1469 const gchar *parent_name;
1470 gchar *tooltip;
1471 GdkPixbuf *icon = get_child_icon(store, parent);
1473 parent_name = get_parent_name(tag, doc->file_type->id);
1474 if (parent_name)
1476 GList **candidates;
1477 GtkTreeIter *parent_search = NULL;
1479 /* walk parent candidates to find the better one.
1480 * if there are more than one, take the one that has the closest line number
1481 * after the tag we're searching the parent for */
1482 candidates = g_hash_table_lookup(parents_table, parent_name);
1483 if (candidates)
1485 GList *node;
1486 glong delta = G_MAXLONG;
1487 foreach_list(node, *candidates)
1489 TMTag *parent_tag;
1490 glong d;
1492 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1493 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1495 d = tag->line - parent_tag->line;
1496 if (! parent_search || (d >= 0 && d < delta))
1498 delta = d;
1499 parent_search = node->data;
1501 tm_tag_unref(parent_tag);
1505 if (parent_search)
1506 parent = parent_search;
1507 else
1508 parent_name = NULL;
1511 /* only expand to the iter if the parent was empty, otherwise we let the
1512 * folding as it was before (already expanded, or closed by the user) */
1513 expand = ! gtk_tree_model_iter_has_child(model, parent);
1515 /* insert the new element */
1516 gtk_tree_store_append(store, &iter, parent);
1517 name = get_symbol_name(doc, tag, parent_name != NULL);
1518 tooltip = get_symbol_tooltip(doc, tag);
1519 gtk_tree_store_set(store, &iter,
1520 SYMBOLS_COLUMN_NAME, name,
1521 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1522 SYMBOLS_COLUMN_ICON, icon,
1523 SYMBOLS_COLUMN_TAG, tag,
1524 -1);
1525 g_free(tooltip);
1526 if (G_LIKELY(icon))
1527 g_object_unref(icon);
1529 update_parents_table(parents_table, tag, parent_name, &iter);
1531 if (expand)
1532 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1536 g_hash_table_destroy(parents_table);
1537 tags_table_destroy(tags_table);
1541 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1542 * is not stable, so the order is already lost. */
1543 static gint compare_top_level_names(const gchar *a, const gchar *b)
1545 guint i;
1546 const gchar *name;
1548 /* This should never happen as it would mean that two or more top
1549 * level items have the same name but it can happen by typos in the translations. */
1550 if (utils_str_equal(a, b))
1551 return 1;
1553 foreach_ptr_array(name, i, top_level_iter_names)
1555 if (utils_str_equal(name, a))
1556 return -1;
1557 if (utils_str_equal(name, b))
1558 return 1;
1560 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1561 return 0;
1565 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1566 GtkTreeIter *iter)
1568 /* if the tag has a parent tag, it should be at depth >= 2 */
1569 return !EMPTY(tag->scope) &&
1570 gtk_tree_store_iter_depth(store, iter) == 1;
1574 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1575 gpointer user_data)
1577 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1578 TMTag *tag_a, *tag_b;
1579 gint cmp;
1581 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1582 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1584 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1585 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1586 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1587 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1589 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1590 compare_symbol_lines(tag_a, tag_b);
1592 else
1594 gchar *astr, *bstr;
1596 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1597 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1599 /* if a is toplevel, b must be also */
1600 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1602 cmp = compare_top_level_names(astr, bstr);
1604 else
1606 /* this is what g_strcmp0() does */
1607 if (! astr)
1608 cmp = -(astr != bstr);
1609 else if (! bstr)
1610 cmp = astr != bstr;
1611 else
1613 cmp = strcmp(astr, bstr);
1615 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1616 if (tag_a && tag_b)
1617 if (!sort_by_name ||
1618 (utils_str_equal(tag_a->name, tag_b->name) &&
1619 utils_str_equal(tag_a->scope, tag_b->scope)))
1620 cmp = compare_symbol_lines(tag_a, tag_b);
1623 g_free(astr);
1624 g_free(bstr);
1626 tm_tag_unref(tag_a);
1627 tm_tag_unref(tag_b);
1629 return cmp;
1633 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1635 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1636 GINT_TO_POINTER(sort_by_name), NULL);
1638 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1642 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1644 GList *tags;
1646 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1648 tags = get_tag_list(doc, tm_tag_max_t);
1649 if (tags == NULL)
1650 return FALSE;
1652 /* FIXME: Not sure why we detached the model here? */
1654 /* disable sorting during update because the code doesn't support correctly
1655 * models that are currently being built */
1656 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1658 /* add grandparent type iters */
1659 add_top_level_items(doc);
1661 update_tree_tags(doc, &tags);
1662 g_list_free(tags);
1664 hide_empty_rows(doc->priv->tag_store);
1666 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1667 sort_mode = doc->priv->symbol_list_sort_mode;
1669 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1670 doc->priv->symbol_list_sort_mode = sort_mode;
1672 return TRUE;
1676 /* Detects a global tags filetype from the *.lang.* language extension.
1677 * Returns NULL if there was no matching TM language. */
1678 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1680 gchar *tags_ext;
1681 gchar *shortname = utils_strdupa(utf8_filename);
1682 GeanyFiletype *ft = NULL;
1684 tags_ext = g_strrstr(shortname, ".tags");
1685 if (tags_ext)
1687 *tags_ext = '\0'; /* remove .tags extension */
1688 ft = filetypes_detect_from_extension(shortname);
1689 if (ft->id != GEANY_FILETYPES_NONE)
1690 return ft;
1692 return NULL;
1696 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1697 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1698 * the relevant path.
1699 * Example:
1700 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1701 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1703 /* -E pre-process, -dD output user macros, -p prof info (?) */
1704 const char pre_process[] = "gcc -E -dD -p -I.";
1706 if (argc > 2)
1708 /* Create global taglist */
1709 int status;
1710 char *command;
1711 const char *tags_file = argv[1];
1712 char *utf8_fname;
1713 GeanyFiletype *ft;
1715 utf8_fname = utils_get_utf8_from_locale(tags_file);
1716 ft = detect_global_tags_filetype(utf8_fname);
1717 g_free(utf8_fname);
1719 if (ft == NULL)
1721 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1722 return 1;
1724 /* load config in case of custom filetypes */
1725 filetypes_load_config(ft->id, FALSE);
1727 /* load ignore list for C/C++ parser */
1728 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1729 load_c_ignore_tags();
1731 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1733 const gchar *cflags = getenv("CFLAGS");
1734 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1736 else
1737 command = NULL; /* don't preprocess */
1739 geany_debug("Generating %s tags file.", ft->name);
1740 tm_get_workspace();
1741 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1742 argc - 2, tags_file, ft->lang);
1743 g_free(command);
1744 symbols_finalize(); /* free c_tags_ignore data */
1745 if (! status)
1747 g_printerr(_("Failed to create tags file, perhaps because no tags "
1748 "were found.\n"));
1749 return 1;
1752 else
1754 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1755 g_printerr(_("Example:\n"
1756 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1757 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1758 return 1;
1760 return 0;
1764 void symbols_show_load_tags_dialog(void)
1766 GtkWidget *dialog;
1767 GtkFileFilter *filter;
1769 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1770 GTK_FILE_CHOOSER_ACTION_OPEN,
1771 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1772 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1773 NULL);
1774 gtk_widget_set_name(dialog, "GeanyDialog");
1775 filter = gtk_file_filter_new();
1776 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1777 gtk_file_filter_add_pattern(filter, "*.*.tags");
1778 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1780 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1782 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1783 GSList *item;
1785 for (item = flist; item != NULL; item = g_slist_next(item))
1787 gchar *fname = item->data;
1788 gchar *utf8_fname;
1789 GeanyFiletype *ft;
1791 utf8_fname = utils_get_utf8_from_locale(fname);
1792 ft = detect_global_tags_filetype(utf8_fname);
1794 if (ft != NULL && symbols_load_global_tags(fname, ft))
1795 /* For translators: the first wildcard is the filetype, the second the filename */
1796 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1797 filetypes_get_display_name(ft), utf8_fname);
1798 else
1799 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1801 g_free(utf8_fname);
1802 g_free(fname);
1804 g_slist_free(flist);
1806 gtk_widget_destroy(dialog);
1810 static void init_user_tags(void)
1812 GSList *file_list = NULL, *list = NULL;
1813 const GSList *node;
1814 gchar *dir;
1816 dir = g_build_filename(app->configdir, "tags", NULL);
1817 /* create the user tags dir for next time if it doesn't exist */
1818 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1819 utils_mkdir(dir, FALSE);
1820 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1822 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1823 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1824 g_free(dir);
1826 file_list = g_slist_concat(file_list, list);
1828 /* populate the filetype-specific tag files lists */
1829 for (node = file_list; node != NULL; node = node->next)
1831 gchar *fname = node->data;
1832 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1833 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1835 g_free(utf8_fname);
1837 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1838 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1839 else
1841 geany_debug("Unknown filetype for file '%s'.", fname);
1842 g_free(fname);
1846 /* don't need to delete list contents because they are now stored in
1847 * ft->priv->tag_files */
1848 g_slist_free(file_list);
1852 static void load_user_tags(filetype_id ft_id)
1854 static guchar *tags_loaded = NULL;
1855 static gboolean init_tags = FALSE;
1856 const GSList *node;
1857 GeanyFiletype *ft = filetypes[ft_id];
1859 g_return_if_fail(ft_id > 0);
1861 if (!tags_loaded)
1862 tags_loaded = g_new0(guchar, filetypes_array->len);
1863 if (tags_loaded[ft_id])
1864 return;
1865 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1867 if (!init_tags)
1869 init_user_tags();
1870 init_tags = TRUE;
1873 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1875 const gchar *fname = node->data;
1877 symbols_load_global_tags(fname, ft);
1882 static gboolean goto_tag(const gchar *name, gboolean definition)
1884 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1885 TMTagType type;
1886 TMTag *tmtag = NULL;
1887 GeanyDocument *old_doc = document_get_current();
1889 /* goto tag definition: all except prototypes / forward declarations / externs */
1890 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1892 /* first look in the current document */
1893 if (old_doc != NULL && old_doc->tm_file)
1894 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1896 /* if not found, look in the workspace */
1897 if (tmtag == NULL)
1898 tmtag = find_workspace_tag(name, type);
1900 if (tmtag != NULL)
1902 GeanyDocument *new_doc = document_find_by_real_path(
1903 tmtag->file->file_name);
1905 if (new_doc)
1907 /* If we are already on the tag line, swap definition/declaration */
1908 if (new_doc == old_doc &&
1909 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1911 if (goto_tag(name, !definition))
1912 return TRUE;
1915 else
1917 /* not found in opened document, should open */
1918 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1921 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1922 return TRUE;
1924 return FALSE;
1928 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1930 if (goto_tag(name, definition))
1931 return TRUE;
1933 /* if we are here, there was no match and we are beeping ;-) */
1934 utils_beep();
1936 if (!definition)
1937 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1938 else
1939 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1940 return FALSE;
1944 /* This could perhaps be improved to check for #if, class etc. */
1945 static gint get_function_fold_number(GeanyDocument *doc)
1947 /* for Java the functions are always one fold level above the class scope */
1948 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1949 return SC_FOLDLEVELBASE + 1;
1950 else
1951 return SC_FOLDLEVELBASE;
1955 /* Should be used only with get_current_tag_cached.
1956 * tag_types caching might trigger recomputation too often but this isn't used differently often
1957 * enough to be an issue for now */
1958 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1960 static gint old_line = -2;
1961 static GeanyDocument *old_doc = NULL;
1962 static gint old_fold_num = -1;
1963 static guint old_tag_types = 0;
1964 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1965 gboolean ret;
1967 /* check if the cached line and file index have changed since last time: */
1968 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1969 ret = TRUE;
1970 else if (cur_line == old_line)
1971 ret = FALSE;
1972 else
1974 /* if the line has only changed by 1 */
1975 if (abs(cur_line - old_line) == 1)
1977 /* It's the same function if the fold number hasn't changed */
1978 ret = (fold_num != old_fold_num);
1980 else ret = TRUE;
1983 /* record current line and file index for next time */
1984 old_line = cur_line;
1985 old_doc = doc;
1986 old_fold_num = fold_num;
1987 old_tag_types = tag_types;
1988 return ret;
1992 /* Parse the function name up to 2 lines before tag_line.
1993 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1994 * type or argument names can be confused with the function name. */
1995 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1997 gint start, end, max_pos;
1998 gint fn_style;
2000 switch (sci_get_lexer(sci))
2002 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2003 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2004 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2006 start = sci_get_position_from_line(sci, tag_line - 2);
2007 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2008 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2009 start++;
2011 end = start;
2012 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2013 end++;
2015 if (start == end)
2016 return NULL;
2017 return sci_get_contents_range(sci, start, end);
2021 /* Parse the function name */
2022 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2024 gint start, end, first_pos, max_pos;
2025 gint tmp;
2026 gchar c;
2028 first_pos = end = sci_get_position_from_line(sci, tag_line);
2029 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2030 tmp = 0;
2031 /* goto the begin of function body */
2032 while (end < max_pos &&
2033 (tmp = sci_get_char_at(sci, end)) != '{' &&
2034 tmp != 0) end++;
2035 if (tmp == 0) end --;
2037 /* go back to the end of function identifier */
2038 while (end > 0 && end > first_pos - 500 &&
2039 (tmp = sci_get_char_at(sci, end)) != '(' &&
2040 tmp != 0) end--;
2041 end--;
2042 if (end < 0) end = 0;
2044 /* skip whitespaces between identifier and ( */
2045 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2047 start = end;
2048 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2049 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2050 || tmp == SCE_C_GLOBALCLASS
2051 || (c = sci_get_char_at(sci, start)) == '~'
2052 || c == ':'))
2053 start--;
2054 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2056 if (start == end) return NULL;
2057 return sci_get_contents_range(sci, start, end + 1);
2061 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2063 gint line_count = sci_get_line_count(sci);
2065 for (; line < line_count; line++)
2067 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2068 return line;
2071 return -1;
2075 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2077 gint line;
2078 gint parent;
2080 line = sci_get_current_line(doc->editor->sci);
2081 parent = sci_get_fold_parent(doc->editor->sci, line);
2082 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2083 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2084 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2086 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2088 if (tag)
2090 gint tag_line = tag->line - 1;
2091 gint last_child = line + 1;
2093 /* if it may be a false positive because we're inside a fold level not inside anything
2094 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2095 * right after the tag we got from TM */
2096 if (abs(tag_line - parent) > 1)
2098 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2099 if (tag_fold >= 0)
2100 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2103 if (line <= last_child)
2105 if (tag->scope)
2106 *tagname = g_strconcat(tag->scope,
2107 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2108 else
2109 *tagname = g_strdup(tag->name);
2111 return tag_line;
2115 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2116 * to dirty and inaccurate hand-parsing */
2117 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2119 const gint fn_fold = get_function_fold_number(doc);
2120 gint tag_line = parent;
2121 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2123 /* find the top level fold point */
2124 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2126 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2127 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2130 if (tag_line >= 0)
2132 gchar *cur_tag;
2134 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2135 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2136 else
2137 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2139 if (cur_tag != NULL)
2141 *tagname = cur_tag;
2142 return tag_line;
2147 *tagname = g_strdup(_("unknown"));
2148 return -1;
2152 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2154 static gint tag_line = -1;
2155 static gchar *cur_tag = NULL;
2157 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2159 if (doc == NULL) /* reset current function */
2161 current_tag_changed(NULL, -1, -1, 0);
2162 g_free(cur_tag);
2163 cur_tag = g_strdup(_("unknown"));
2164 if (tagname != NULL)
2165 *tagname = cur_tag;
2166 tag_line = -1;
2168 else
2170 gint line = sci_get_current_line(doc->editor->sci);
2171 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2173 if (current_tag_changed(doc, line, fold_level, tag_types))
2175 g_free(cur_tag);
2176 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2178 *tagname = cur_tag;
2181 return tag_line;
2185 /* Sets *tagname to point at the current function or tag name.
2186 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2187 * call to this function.
2188 * Returns: line number of the current tag, or -1 if unknown. */
2189 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2191 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2195 /* same as symbols_get_current_function() but finds class, namespaces and more */
2196 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2198 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2199 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2201 /* Python parser reports imports as namespaces which confuses the scope detection */
2202 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2203 tag_types |= tm_tag_namespace_t;
2205 return get_current_tag_name_cached(doc, tagname, tag_types);
2209 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2211 gint sort_mode = GPOINTER_TO_INT(user_data);
2212 GeanyDocument *doc = document_get_current();
2214 if (ignore_callback)
2215 return;
2217 if (doc != NULL)
2218 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2222 static void on_symbol_tree_menu_show(GtkWidget *widget,
2223 gpointer user_data)
2225 GeanyDocument *doc = document_get_current();
2226 gboolean enable;
2228 enable = doc && doc->has_tags;
2229 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2230 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2231 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2232 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2233 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2234 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2236 if (! doc)
2237 return;
2239 ignore_callback = TRUE;
2241 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2242 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2243 else
2244 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2246 ignore_callback = FALSE;
2250 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2252 gboolean expand = GPOINTER_TO_INT(user_data);
2253 GeanyDocument *doc = document_get_current();
2255 if (! doc)
2256 return;
2258 g_return_if_fail(doc->priv->tag_tree);
2260 if (expand)
2261 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2262 else
2263 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2267 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2269 GtkTreeIter iter;
2270 GtkTreeSelection *selection;
2271 GtkTreeModel *model;
2272 GeanyDocument *doc;
2273 TMTag *tag = NULL;
2275 doc = document_get_current();
2276 if (!doc)
2277 return;
2279 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2280 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2281 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2282 if (tag)
2284 if (widget == symbol_menu.find_in_files)
2285 search_show_find_in_files_dialog_full(tag->name, NULL);
2286 else
2287 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2288 widget == symbol_menu.find_usage);
2290 tm_tag_unref(tag);
2295 static void create_taglist_popup_menu(void)
2297 GtkWidget *item, *menu;
2299 tv.popup_taglist = menu = gtk_menu_new();
2301 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2302 gtk_widget_show(item);
2303 gtk_container_add(GTK_CONTAINER(menu), item);
2304 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2306 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2307 gtk_widget_show(item);
2308 gtk_container_add(GTK_CONTAINER(menu), item);
2309 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2311 item = gtk_separator_menu_item_new();
2312 gtk_widget_show(item);
2313 gtk_container_add(GTK_CONTAINER(menu), item);
2315 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2316 _("Sort by _Name"));
2317 gtk_widget_show(item);
2318 gtk_container_add(GTK_CONTAINER(menu), item);
2319 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2320 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2322 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2323 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2324 gtk_widget_show(item);
2325 gtk_container_add(GTK_CONTAINER(menu), item);
2326 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2327 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2329 item = gtk_separator_menu_item_new();
2330 gtk_widget_show(item);
2331 gtk_container_add(GTK_CONTAINER(menu), item);
2333 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2334 gtk_widget_show(item);
2335 gtk_container_add(GTK_CONTAINER(menu), item);
2336 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2338 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2339 gtk_widget_show(item);
2340 gtk_container_add(GTK_CONTAINER(menu), item);
2341 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2343 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2344 gtk_widget_show(item);
2345 gtk_container_add(GTK_CONTAINER(menu), item);
2346 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2348 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2350 sidebar_add_common_menu_items(GTK_MENU(menu));
2354 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2356 gchar *f;
2358 g_return_if_fail(!EMPTY(doc->real_path));
2360 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2361 if (utils_str_equal(doc->real_path, f))
2362 load_c_ignore_tags();
2364 g_free(f);
2368 void symbols_init(void)
2370 gchar *f;
2372 create_taglist_popup_menu();
2374 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2375 ui_add_config_file_menu_item(f, NULL, NULL);
2376 g_free(f);
2378 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2382 void symbols_finalize(void)
2384 g_strfreev(html_entities);
2385 g_strfreev(c_tags_ignore);