Update of Dutch translation
[geany-mirror.git] / src / symbols.c
blobc55a4ea675e47d95ace83827d1c146f6239b9332
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 #include "SciLexer.h"
36 #include "geany.h"
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdlib.h>
42 #include "prefix.h"
43 #include "symbols.h"
44 #include "utils.h"
45 #include "filetypes.h"
46 #include "encodings.h"
47 #include "document.h"
48 #include "documentprivate.h"
49 #include "support.h"
50 #include "msgwindow.h"
51 #include "sidebar.h"
52 #include "main.h"
53 #include "navqueue.h"
54 #include "ui_utils.h"
55 #include "editor.h"
56 #include "sciwrappers.h"
57 #include "filetypesprivate.h"
60 const guint TM_GLOBAL_TYPE_MASK =
61 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
62 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
65 static gchar **html_entities = NULL;
67 typedef struct
69 gboolean tags_loaded;
70 const gchar *tag_file;
71 } TagFileInfo;
73 /* Check before adding any more tags files, usually they should be downloaded separately. */
74 enum /* Geany tag files */
76 GTF_C,
77 GTF_PASCAL,
78 GTF_PHP,
79 GTF_HTML_ENTITIES,
80 GTF_LATEX,
81 GTF_PYTHON,
82 GTF_MAX
85 static TagFileInfo tag_file_info[GTF_MAX] =
87 {FALSE, "c99.tags"},
88 {FALSE, "pascal.tags"},
89 {FALSE, "php.tags"},
90 {FALSE, "html_entities.tags"},
91 {FALSE, "latex.tags"},
92 {FALSE, "python.tags"}
95 static GPtrArray *top_level_iter_names = NULL;
97 static struct
99 GtkWidget *expand_all;
100 GtkWidget *collapse_all;
101 GtkWidget *sort_by_name;
102 GtkWidget *sort_by_appearance;
104 symbol_menu = {NULL, NULL, NULL, NULL};
107 static void html_tags_loaded(void);
108 static void load_user_tags(filetype_id ft_id);
110 /* get the tags_ignore list, exported by tagmanager's options.c */
111 extern gchar **c_tags_ignore;
113 /* ignore certain tokens when parsing C-like syntax.
114 * Also works for reloading. */
115 static void load_c_ignore_tags(void)
117 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
118 gchar *content;
120 if (g_file_get_contents(path, &content, NULL, NULL))
122 /* historically we ignore the glib _DECLS for tag generation */
123 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
125 g_strfreev(c_tags_ignore);
126 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
127 g_free(content);
129 g_free(path);
133 void symbols_reload_config_files(void)
135 load_c_ignore_tags();
139 static gsize get_tag_count(void)
141 GPtrArray *tags = tm_get_workspace()->global_tags;
142 gsize count = tags ? tags->len : 0;
144 return count;
148 /* wrapper for tm_workspace_load_global_tags().
149 * note that the tag count only counts new global tags added - if a tag has the same name,
150 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
151 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
153 gboolean result;
154 gsize old_tag_count = get_tag_count();
156 result = tm_workspace_load_global_tags(tags_file, ft->lang);
157 if (result)
159 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
160 (guint) (get_tag_count() - old_tag_count));
162 return result;
166 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
167 * This provides autocompletion, calltips, etc. */
168 void symbols_global_tags_loaded(guint file_type_idx)
170 TagFileInfo *tfi;
171 gint tag_type;
173 /* load ignore list for C/C++ parser */
174 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
175 c_tags_ignore == NULL)
177 load_c_ignore_tags();
180 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
181 return;
183 /* load config in case of custom filetypes */
184 filetypes_load_config(file_type_idx, FALSE);
186 load_user_tags(file_type_idx);
188 switch (file_type_idx)
190 case GEANY_FILETYPES_PHP:
191 case GEANY_FILETYPES_HTML:
192 html_tags_loaded();
194 switch (file_type_idx)
196 case GEANY_FILETYPES_CPP:
197 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
198 /* no C++ tagfile yet */
199 return;
200 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
201 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
202 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
203 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
204 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
205 default:
206 return;
208 tfi = &tag_file_info[tag_type];
210 if (! tfi->tags_loaded)
212 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
214 symbols_load_global_tags(fname, filetypes[file_type_idx]);
215 tfi->tags_loaded = TRUE;
216 g_free(fname);
221 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
222 static void html_tags_loaded(void)
224 TagFileInfo *tfi;
226 if (cl_options.ignore_global_tags)
227 return;
229 tfi = &tag_file_info[GTF_HTML_ENTITIES];
230 if (! tfi->tags_loaded)
232 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
234 html_entities = utils_read_file_in_array(file);
235 tfi->tags_loaded = TRUE;
236 g_free(file);
241 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
243 guint j;
244 TMTag *tag;
245 GString *s = NULL;
246 GPtrArray *typedefs;
247 gint tag_lang;
249 g_return_val_if_fail(tags_array != NULL, NULL);
251 typedefs = tm_tags_extract(tags_array, tag_types);
253 if ((typedefs) && (typedefs->len > 0))
255 s = g_string_sized_new(typedefs->len * 10);
256 for (j = 0; j < typedefs->len; ++j)
258 tag = TM_TAG(typedefs->pdata[j]);
259 /* tag->atts.file.lang contains (for some reason) the line of the tag if
260 * tag->atts.entry.file is not NULL */
261 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
263 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
264 * e.g. PHP classes in C++ files
265 * lang = -2 disables the check */
266 if (tag->name && (tag_lang == lang || lang == -2))
268 if (j != 0)
269 g_string_append_c(s, ' ');
270 g_string_append(s, tag->name);
274 if (typedefs)
275 g_ptr_array_free(typedefs, TRUE);
276 return s;
280 /** Gets the context separator used by the tag manager for a particular file
281 * type.
282 * @param ft_id File type identifier.
283 * @return The context separator string.
285 * @since 0.19
287 const gchar *symbols_get_context_separator(gint ft_id)
289 switch (ft_id)
291 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
292 case GEANY_FILETYPES_CPP:
293 case GEANY_FILETYPES_GLSL: /* for structs */
294 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
295 return "::";
297 /* avoid confusion with other possible separators in group/section name */
298 case GEANY_FILETYPES_CONF:
299 case GEANY_FILETYPES_REST:
300 return ":::";
302 default:
303 return ".";
308 GString *symbols_get_macro_list(gint lang)
310 guint j, i;
311 GPtrArray *ftags;
312 GString *words;
313 gint tag_lang;
314 TMTag *tag;
316 if (app->tm_workspace->work_objects == NULL)
317 return NULL;
319 ftags = g_ptr_array_sized_new(50);
320 words = g_string_sized_new(200);
322 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
324 GPtrArray *tags;
326 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
327 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
328 if (NULL != tags)
330 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
332 tag = TM_TAG(tags->pdata[i]);
333 tag_lang = (tag->atts.entry.file) ?
334 tag->atts.entry.file->lang : tag->atts.file.lang;
336 if (tag_lang == lang)
337 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
339 g_ptr_array_free(tags, TRUE);
343 if (ftags->len == 0)
345 g_ptr_array_free(ftags, TRUE);
346 g_string_free(words, TRUE);
347 return NULL;
350 tm_tags_sort(ftags, NULL, FALSE);
351 for (j = 0; j < ftags->len; j++)
353 if (j > 0)
354 g_string_append_c(words, '\n');
355 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
357 g_ptr_array_free(ftags, TRUE);
358 return words;
362 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
363 static TMTag *
364 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
366 guint i;
367 g_return_val_if_fail(tags != NULL, NULL);
369 for (i = 0; i < tags->len; ++i)
371 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
372 return TM_TAG(tags->pdata[i]);
374 return NULL;
378 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
379 const gchar *tag_name, guint type)
381 GPtrArray *tags;
382 TMTag *tmtag;
384 if (G_LIKELY(workobj != NULL))
386 tags = tm_tags_extract(workobj->tags_array, type);
387 if (tags != NULL)
389 tmtag = symbols_find_tm_tag(tags, tag_name);
391 g_ptr_array_free(tags, TRUE);
393 if (tmtag != NULL)
394 return tmtag;
397 return NULL; /* not found */
401 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
403 guint j;
404 const GPtrArray *work_objects = NULL;
406 if (app->tm_workspace != NULL)
407 work_objects = app->tm_workspace->work_objects;
409 if (work_objects != NULL)
411 for (j = 0; j < work_objects->len; j++)
413 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
414 TMTag *tmtag;
416 tmtag = find_work_object_tag(workobj, tag_name, type);
417 if (tmtag != NULL)
418 return tmtag;
421 return NULL; /* not found */
425 const gchar **symbols_get_html_entities(void)
427 if (html_entities == NULL)
428 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
430 return (const gchar **) html_entities;
434 /* sort by name, then line */
435 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
437 gint ret;
439 if (tag_a == NULL || tag_b == NULL)
440 return 0;
442 if (tag_a->name == NULL)
443 return -(tag_a->name != tag_b->name);
445 if (tag_b->name == NULL)
446 return tag_a->name != tag_b->name;
448 ret = strcmp(tag_a->name, tag_b->name);
449 if (ret == 0)
451 return tag_a->atts.entry.line - tag_b->atts.entry.line;
453 return ret;
457 /* sort by line, then scope */
458 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
460 const TMTag *tag_a = TM_TAG(a);
461 const TMTag *tag_b = TM_TAG(b);
462 gint ret;
464 if (a == NULL || b == NULL)
465 return 0;
467 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
468 if (ret == 0)
470 if (tag_a->atts.entry.scope == NULL)
471 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
472 if (tag_b->atts.entry.scope == NULL)
473 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
474 else
475 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
477 return ret;
481 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
483 GList *tag_names = NULL;
484 TMTag *tag;
485 guint i;
487 g_return_val_if_fail(doc, NULL);
489 if (! doc->tm_file || ! doc->tm_file->tags_array)
490 return NULL;
492 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
494 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
495 if (G_UNLIKELY(tag == NULL))
496 return NULL;
498 if (tag->type & tag_types)
500 tag_names = g_list_prepend(tag_names, tag);
503 tag_names = g_list_sort(tag_names, compare_symbol_lines);
504 return tag_names;
508 /* amount of types in the symbol list (currently max. 8 are used) */
509 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
511 struct TreeviewSymbols
513 GtkTreeIter tag_function;
514 GtkTreeIter tag_class;
515 GtkTreeIter tag_macro;
516 GtkTreeIter tag_member;
517 GtkTreeIter tag_variable;
518 GtkTreeIter tag_namespace;
519 GtkTreeIter tag_struct;
520 GtkTreeIter tag_interface;
521 GtkTreeIter tag_type;
522 GtkTreeIter tag_other;
523 } tv_iters;
526 static void init_tag_iters(void)
528 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
529 * filetypes(e.g. config file to Python crashes Geany without this) */
530 tv_iters.tag_function.stamp = -1;
531 tv_iters.tag_class.stamp = -1;
532 tv_iters.tag_member.stamp = -1;
533 tv_iters.tag_macro.stamp = -1;
534 tv_iters.tag_variable.stamp = -1;
535 tv_iters.tag_namespace.stamp = -1;
536 tv_iters.tag_struct.stamp = -1;
537 tv_iters.tag_interface.stamp = -1;
538 tv_iters.tag_type.stamp = -1;
539 tv_iters.tag_other.stamp = -1;
543 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
545 static GtkIconTheme *icon_theme = NULL;
546 static gint x, y;
548 if (G_UNLIKELY(icon_theme == NULL))
550 #ifndef G_OS_WIN32
551 gchar *path = g_build_filename(GEANY_DATADIR, "icons", NULL);
552 #endif
553 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &y);
554 icon_theme = gtk_icon_theme_get_default();
555 #ifdef G_OS_WIN32
556 gtk_icon_theme_append_search_path(icon_theme, "share\\icons");
557 #else
558 gtk_icon_theme_append_search_path(icon_theme, path);
559 g_free(path);
560 #endif
562 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
566 /* finds the next iter at any level
567 * @param iter in/out, the current iter, will be changed to the next one
568 * @param down whether to try the child iter
569 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
570 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
572 GtkTreeIter guess;
573 GtkTreeIter copy = *iter;
575 /* go down if the item has children */
576 if (down && gtk_tree_model_iter_children(model, &guess, iter))
577 *iter = guess;
578 /* or to the next item at the same level */
579 else if (gtk_tree_model_iter_next(model, &copy))
580 *iter = copy;
581 /* or to the next item at a parent level */
582 else if (gtk_tree_model_iter_parent(model, &guess, iter))
584 copy = guess;
585 while(TRUE)
587 if (gtk_tree_model_iter_next(model, &copy))
589 *iter = copy;
590 return TRUE;
592 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
593 guess = copy;
594 else
595 return FALSE;
598 else
599 return FALSE;
601 return TRUE;
605 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
607 GtkTreeModel *model = GTK_TREE_MODEL(store);
609 if (!gtk_tree_model_get_iter_first(model, iter))
610 return FALSE;
613 gchar *candidate;
615 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
616 /* FIXME: what if 2 different items have the same name?
617 * this should never happen, but might be caused by a typo in a translation */
618 if (utils_str_equal(candidate, title))
620 g_free(candidate);
621 return TRUE;
623 else
624 g_free(candidate);
626 while (gtk_tree_model_iter_next(model, iter));
628 return FALSE;
632 /* Adds symbol list groups in (iter*, title) pairs.
633 * The list must be ended with NULL. */
634 static void G_GNUC_NULL_TERMINATED
635 tag_list_add_groups(GtkTreeStore *tree_store, ...)
637 va_list args;
638 GtkTreeIter *iter;
640 g_return_if_fail(top_level_iter_names);
642 va_start(args, tree_store);
643 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
645 gchar *title = va_arg(args, gchar*);
646 gchar *icon_name = va_arg(args, gchar *);
647 GdkPixbuf *icon = NULL;
649 if (icon_name)
651 icon = get_tag_icon(icon_name);
654 g_assert(title != NULL);
655 g_ptr_array_add(top_level_iter_names, title);
657 if (!find_toplevel_iter(tree_store, iter, title))
658 gtk_tree_store_append(tree_store, iter, NULL);
660 if (G_IS_OBJECT(icon))
662 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
663 g_object_unref(icon);
665 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
667 va_end(args);
671 static void add_top_level_items(GeanyDocument *doc)
673 filetype_id ft_id = doc->file_type->id;
674 GtkTreeStore *tag_store = doc->priv->tag_store;
676 if (top_level_iter_names == NULL)
677 top_level_iter_names = g_ptr_array_new();
678 else
679 g_ptr_array_set_size(top_level_iter_names, 0);
681 init_tag_iters();
683 switch (ft_id)
685 case GEANY_FILETYPES_DIFF:
687 tag_list_add_groups(tag_store,
688 &(tv_iters.tag_function), _("Files"), NULL, NULL);
689 break;
691 case GEANY_FILETYPES_DOCBOOK:
693 tag_list_add_groups(tag_store,
694 &(tv_iters.tag_function), _("Chapter"), NULL,
695 &(tv_iters.tag_class), _("Section"), NULL,
696 &(tv_iters.tag_member), _("Sect1"), NULL,
697 &(tv_iters.tag_macro), _("Sect2"), NULL,
698 &(tv_iters.tag_variable), _("Sect3"), NULL,
699 &(tv_iters.tag_struct), _("Appendix"), NULL,
700 &(tv_iters.tag_other), _("Other"), NULL,
701 NULL);
702 break;
704 case GEANY_FILETYPES_HASKELL:
705 tag_list_add_groups(tag_store,
706 &tv_iters.tag_namespace, _("Module"), NULL,
707 &tv_iters.tag_type, _("Types"), NULL,
708 &tv_iters.tag_macro, _("Type constructors"), NULL,
709 &tv_iters.tag_function, _("Functions"), "classviewer-method",
710 NULL);
711 break;
712 case GEANY_FILETYPES_COBOL:
713 tag_list_add_groups(tag_store,
714 &tv_iters.tag_class, _("Program"), "classviewer-class",
715 &tv_iters.tag_function, _("File"), "classviewer-method",
716 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
717 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
718 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
719 &tv_iters.tag_variable, _("Data"), "classviewer-var",
720 NULL);
721 break;
722 case GEANY_FILETYPES_CONF:
723 tag_list_add_groups(tag_store,
724 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
725 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
726 NULL);
727 break;
728 case GEANY_FILETYPES_NSIS:
729 tag_list_add_groups(tag_store,
730 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
731 &tv_iters.tag_function, _("Functions"), "classviewer-method",
732 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
733 NULL);
734 break;
735 case GEANY_FILETYPES_LATEX:
737 tag_list_add_groups(tag_store,
738 &(tv_iters.tag_function), _("Command"), NULL,
739 &(tv_iters.tag_class), _("Environment"), NULL,
740 &(tv_iters.tag_member), _("Section"), NULL,
741 &(tv_iters.tag_macro), _("Subsection"), NULL,
742 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
743 &(tv_iters.tag_struct), _("Label"), NULL,
744 &(tv_iters.tag_namespace), _("Chapter"), NULL,
745 &(tv_iters.tag_other), _("Other"), NULL,
746 NULL);
747 break;
749 case GEANY_FILETYPES_MATLAB:
751 tag_list_add_groups(tag_store,
752 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
753 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
754 NULL);
755 break;
757 case GEANY_FILETYPES_R:
759 tag_list_add_groups(tag_store,
760 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
761 &(tv_iters.tag_struct), _("Other"), NULL,
762 NULL);
763 break;
765 case GEANY_FILETYPES_PERL:
767 tag_list_add_groups(tag_store,
768 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
769 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
770 &(tv_iters.tag_macro), _("Labels"), NULL,
771 &(tv_iters.tag_type), _("Constants"), NULL,
772 &(tv_iters.tag_other), _("Other"), "classviewer-other",
773 NULL);
774 break;
776 case GEANY_FILETYPES_PHP:
778 tag_list_add_groups(tag_store,
779 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
780 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
781 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
782 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
783 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
784 NULL);
785 break;
787 case GEANY_FILETYPES_HTML:
789 tag_list_add_groups(tag_store,
790 &(tv_iters.tag_function), _("Functions"), NULL,
791 &(tv_iters.tag_member), _("Anchors"), NULL,
792 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
793 &(tv_iters.tag_class), _("H2 Headings"), NULL,
794 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
795 NULL);
796 break;
798 case GEANY_FILETYPES_CSS:
800 tag_list_add_groups(tag_store,
801 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
802 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
803 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
804 break;
806 case GEANY_FILETYPES_REST:
807 case GEANY_FILETYPES_TXT2TAGS:
808 case GEANY_FILETYPES_ABC:
810 tag_list_add_groups(tag_store,
811 &(tv_iters.tag_namespace), _("Chapter"), NULL,
812 &(tv_iters.tag_member), _("Section"), NULL,
813 &(tv_iters.tag_macro), _("Subsection"), NULL,
814 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
815 NULL);
816 break;
818 case GEANY_FILETYPES_RUBY:
820 tag_list_add_groups(tag_store,
821 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
822 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
823 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
824 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
825 NULL);
826 break;
828 case GEANY_FILETYPES_TCL:
830 tag_list_add_groups(tag_store,
831 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
832 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
833 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
834 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
835 NULL);
836 break;
838 case GEANY_FILETYPES_PYTHON:
840 tag_list_add_groups(tag_store,
841 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
842 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
843 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
844 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
845 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
846 NULL);
847 break;
849 case GEANY_FILETYPES_VHDL:
851 tag_list_add_groups(tag_store,
852 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
853 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
854 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
855 &(tv_iters.tag_type), _("Types"), "classviewer-other",
856 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
857 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
858 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
859 &(tv_iters.tag_other), _("Other"), "classviewer-other",
860 NULL);
861 break;
863 case GEANY_FILETYPES_VERILOG:
865 tag_list_add_groups(tag_store,
866 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
867 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
868 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
869 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
870 &(tv_iters.tag_other), _("Other"), "classviewer-other",
871 NULL);
872 break;
874 case GEANY_FILETYPES_JAVA:
876 tag_list_add_groups(tag_store,
877 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
878 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
879 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
880 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
881 &(tv_iters.tag_member), _("Members"), "classviewer-member",
882 &(tv_iters.tag_other), _("Other"), "classviewer-other",
883 NULL);
884 break;
886 case GEANY_FILETYPES_AS:
888 tag_list_add_groups(tag_store,
889 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
890 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
891 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
892 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
893 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
894 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
895 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
896 &(tv_iters.tag_other), _("Other"), "classviewer-other",
897 NULL);
898 break;
900 case GEANY_FILETYPES_HAXE:
902 tag_list_add_groups(tag_store,
903 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
904 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
905 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
906 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
907 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
908 &(tv_iters.tag_other), _("Other"), "classviewer-other",
909 NULL);
910 break;
912 case GEANY_FILETYPES_BASIC:
914 tag_list_add_groups(tag_store,
915 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
916 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
917 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
918 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
919 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
920 &(tv_iters.tag_other), _("Other"), "classviewer-other",
921 NULL);
922 break;
924 case GEANY_FILETYPES_F77:
925 case GEANY_FILETYPES_FORTRAN:
927 tag_list_add_groups(tag_store,
928 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
929 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
930 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
931 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
932 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
933 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
934 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
935 &(tv_iters.tag_other), _("Other"), "classviewer-other",
936 NULL);
937 break;
939 case GEANY_FILETYPES_ASM:
941 tag_list_add_groups(tag_store,
942 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
943 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
944 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
945 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
946 NULL);
947 break;
949 case GEANY_FILETYPES_MAKE:
950 tag_list_add_groups(tag_store,
951 &tv_iters.tag_function, _("Targets"), "classviewer-method",
952 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
953 NULL);
954 break;
955 case GEANY_FILETYPES_SQL:
957 tag_list_add_groups(tag_store,
958 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
959 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
960 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
961 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
962 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
963 &(tv_iters.tag_member), _("Views"), "classviewer-var",
964 &(tv_iters.tag_other), _("Other"), "classviewer-other",
965 NULL);
966 break;
968 case GEANY_FILETYPES_D:
969 default:
971 if (ft_id == GEANY_FILETYPES_D)
972 tag_list_add_groups(tag_store,
973 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
974 else
975 tag_list_add_groups(tag_store,
976 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
978 tag_list_add_groups(tag_store,
979 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
980 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
981 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
982 &(tv_iters.tag_member), _("Members"), "classviewer-member",
983 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
984 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
985 NULL);
987 if (ft_id != GEANY_FILETYPES_D)
989 tag_list_add_groups(tag_store,
990 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
992 tag_list_add_groups(tag_store,
993 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
994 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1000 /* removes toplevel items that have no children */
1001 static void hide_empty_rows(GtkTreeStore *store)
1003 GtkTreeIter iter;
1004 gboolean cont = TRUE;
1006 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1007 return; /* stop when first iter is invalid, i.e. no elements */
1009 while (cont)
1011 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1012 cont = gtk_tree_store_remove(store, &iter);
1013 else
1014 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1019 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1021 gchar *utf8_name;
1022 const gchar *scope = tag->atts.entry.scope;
1023 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1024 gboolean doc_is_utf8 = FALSE;
1026 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1027 * for None at this point completely */
1028 if (utils_str_equal(doc->encoding, "UTF-8") ||
1029 utils_str_equal(doc->encoding, "None"))
1030 doc_is_utf8 = TRUE;
1031 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1032 * plugin might have called tm_source_file_update(), so check to be sure */
1033 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1035 if (! doc_is_utf8)
1036 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1037 -1, doc->encoding, TRUE);
1038 else
1039 utf8_name = tag->name;
1041 if (utf8_name == NULL)
1042 return NULL;
1044 if (! buffer)
1045 buffer = g_string_new(NULL);
1046 else
1047 g_string_truncate(buffer, 0);
1049 /* check first char of scope is a wordchar */
1050 if (!found_parent && scope &&
1051 strpbrk(scope, GEANY_WORDCHARS) == scope)
1053 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1055 g_string_append(buffer, scope);
1056 g_string_append(buffer, sep);
1058 g_string_append(buffer, utf8_name);
1060 if (! doc_is_utf8)
1061 g_free(utf8_name);
1063 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1065 return buffer->str;
1069 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1071 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1073 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1074 * for None at this point completely */
1075 if (utf8_name != NULL &&
1076 ! utils_str_equal(doc->encoding, "UTF-8") &&
1077 ! utils_str_equal(doc->encoding, "None"))
1079 SETPTR(utf8_name,
1080 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1083 if (utf8_name != NULL)
1084 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1086 return utf8_name;
1090 /* find the last word in "foo::bar::blah", e.g. "blah" */
1091 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1093 const gchar *scope = tag->atts.entry.scope;
1094 const gchar *separator = symbols_get_context_separator(ft_id);
1095 const gchar *str, *ptr;
1097 if (!scope)
1098 return NULL;
1100 str = scope;
1102 while (1)
1104 ptr = strstr(str, separator);
1105 if (ptr)
1107 str = ptr + strlen(separator);
1109 else
1110 break;
1113 return NZV(str) ? str : NULL;
1117 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1119 GtkTreeIter *iter = NULL;
1121 switch (tag_type)
1123 case tm_tag_prototype_t:
1124 case tm_tag_method_t:
1125 case tm_tag_function_t:
1127 iter = &tv_iters.tag_function;
1128 break;
1130 case tm_tag_macro_t:
1131 case tm_tag_macro_with_arg_t:
1133 iter = &tv_iters.tag_macro;
1134 break;
1136 case tm_tag_class_t:
1138 iter = &tv_iters.tag_class;
1139 break;
1141 case tm_tag_member_t:
1142 case tm_tag_field_t:
1144 iter = &tv_iters.tag_member;
1145 break;
1147 case tm_tag_typedef_t:
1148 case tm_tag_enum_t:
1150 iter = &tv_iters.tag_type;
1151 break;
1153 case tm_tag_union_t:
1154 case tm_tag_struct_t:
1156 iter = &tv_iters.tag_struct;
1157 break;
1159 case tm_tag_interface_t:
1160 iter = &tv_iters.tag_interface;
1161 break;
1162 case tm_tag_variable_t:
1164 iter = &tv_iters.tag_variable;
1165 break;
1167 case tm_tag_namespace_t:
1168 case tm_tag_package_t:
1170 iter = &tv_iters.tag_namespace;
1171 break;
1173 default:
1175 iter = &tv_iters.tag_other;
1178 if (G_LIKELY(iter->stamp != -1))
1179 return iter;
1180 else
1181 return NULL;
1185 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1187 GdkPixbuf *icon = NULL;
1189 if (parent == &tv_iters.tag_other)
1191 return get_tag_icon("classviewer-var");
1193 /* copy parent icon */
1194 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1195 SYMBOLS_COLUMN_ICON, &icon, -1);
1196 return icon;
1200 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1202 const TMTag *t1 = v1;
1203 const TMTag *t2 = v2;
1205 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1206 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1207 /* include arglist in match to support e.g. C++ overloading */
1208 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1212 /* inspired from g_str_hash() */
1213 static guint tag_hash(gconstpointer v)
1215 const TMTag *tag = v;
1216 const gchar *p;
1217 guint32 h = 5381;
1219 h = (h << 5) + h + tag->type;
1220 for (p = tag->name; *p != '\0'; p++)
1221 h = (h << 5) + h + *p;
1222 if (tag->atts.entry.scope)
1224 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1225 h = (h << 5) + h + *p;
1227 /* for e.g. C++ overloading */
1228 if (tag->atts.entry.arglist)
1230 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1231 h = (h << 5) + h + *p;
1234 return h;
1238 /* like gtk_tree_view_expand_to_path() but with an iter */
1239 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1241 GtkTreeModel *model = gtk_tree_view_get_model(view);
1242 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1244 gtk_tree_view_expand_to_path(view, path);
1245 gtk_tree_path_free(path);
1249 /* like gtk_tree_store_remove() but finds the next iter at any level */
1250 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1252 GtkTreeIter parent;
1253 gboolean has_parent;
1254 gboolean cont;
1256 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1257 cont = gtk_tree_store_remove(store, iter);
1258 /* if there is no next at this level but there is a parent iter, continue from it */
1259 if (! cont && has_parent)
1261 *iter = parent;
1262 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1265 return cont;
1269 /* adds a new element in the parent table if it's key is known.
1270 * duplicates are kept */
1271 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1272 const GtkTreeIter *iter)
1274 GList **list;
1275 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1276 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1278 if (! list)
1280 list = g_slice_alloc(sizeof *list);
1281 *list = NULL;
1282 g_hash_table_insert(table, tag->name, list);
1284 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1289 static void free_iter_slice_list(gpointer data)
1291 GList **list = data;
1293 if (list)
1295 GList *node;
1296 foreach_list(node, *list)
1297 g_slice_free(GtkTreeIter, node->data);
1298 g_list_free(*list);
1299 g_slice_free1(sizeof *list, list);
1304 /* inserts a @data in @table on key @tag.
1305 * previous data is not overwritten if the key is duplicated, but rather the
1306 * two values are kept in a list
1308 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1309 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1311 GList *list = g_hash_table_lookup(table, tag);
1312 list = g_list_prepend(list, data);
1313 g_hash_table_insert(table, tag, list);
1317 /* looks up the entry in @table that better matches @tag.
1318 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1319 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1321 GList *data = NULL;
1322 GList *node = g_hash_table_lookup(table, tag);
1323 if (node)
1325 glong delta;
1326 data = node->data;
1328 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1330 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1331 for (node = node->next; node; node = node->next)
1333 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1335 if (d < delta)
1337 data = node->data;
1338 delta = d;
1342 #undef TAG_DELTA
1345 return data;
1349 /* removes the element at @tag from @table.
1350 * @tag must be the exact pointer used at insertion time */
1351 static void tags_table_remove(GHashTable *table, TMTag *tag)
1353 GList *list = g_hash_table_lookup(table, tag);
1354 if (list)
1356 GList *node;
1357 foreach_list(node, list)
1359 if (((GList *) node->data)->data == tag)
1360 break;
1362 list = g_list_delete_link(list, node);
1363 if (list)
1364 g_hash_table_insert(table, tag, list);
1365 else
1366 g_hash_table_remove(table, tag);
1372 * Updates the tag tree for a document with the tags in *list.
1373 * @param doc a document
1374 * @param tags a pointer to a GList* holding the tags to add/update. This
1375 * list may be updated, removing updated elements.
1377 * The update is done in two passes:
1378 * 1) walking the current tree, update tags that still exist and remove the
1379 * obsolescent ones;
1380 * 2) walking the remaining (non updated) tags, adds them in the list.
1382 * For better performances, we use 2 hash tables:
1383 * - one containing all the tags for lookup in the first pass (actually stores a
1384 * reference in the tags list for removing it efficiently), avoiding list search
1385 * on each tag;
1386 * - the other holding "tag-name":row references for tags having children, used to
1387 * lookup for a parent in both passes, avoiding tree traversal.
1389 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1391 GtkTreeStore *store = doc->priv->tag_store;
1392 GtkTreeModel *model = GTK_TREE_MODEL(store);
1393 GHashTable *parents_table;
1394 GHashTable *tags_table;
1395 GtkTreeIter iter;
1396 gboolean cont;
1397 GList *item;
1399 /* Build hash tables holding tags and parents */
1400 /* parent table holds "tag-name":GtkTreeIter */
1401 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1402 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1403 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1404 foreach_list(item, *tags)
1406 TMTag *tag = item->data;
1407 const gchar *name;
1409 tags_table_insert(tags_table, tag, item);
1411 name = get_parent_name(tag, doc->file_type->id);
1412 if (name)
1413 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1416 /* First pass, update existing rows or delete them.
1417 * It is OK to delete them since we walk top down so we would remove
1418 * parents before checking for their children, thus never implicitly
1419 * deleting an updated child */
1420 cont = gtk_tree_model_get_iter_first(model, &iter);
1421 while (cont)
1423 TMTag *tag;
1425 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1426 if (! tag) /* most probably a toplevel, skip it */
1427 cont = next_iter(model, &iter, TRUE);
1428 else
1430 GList *found_item;
1432 found_item = tags_table_lookup(tags_table, tag);
1433 if (! found_item) /* tag doesn't exist, remove it */
1434 cont = tree_store_remove_row(store, &iter);
1435 else /* tag still exist, update it */
1437 const gchar *name;
1438 const gchar *parent_name;
1439 TMTag *found = found_item->data;
1441 parent_name = get_parent_name(found, doc->file_type->id);
1442 /* if parent is unknown, ignore it */
1443 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1444 parent_name = NULL;
1446 /* only update fields that (can) have changed (name that holds line
1447 * number, and the tag itself) */
1448 name = get_symbol_name(doc, found, parent_name != NULL);
1449 gtk_tree_store_set(store, &iter,
1450 SYMBOLS_COLUMN_NAME, name,
1451 SYMBOLS_COLUMN_TAG, found,
1452 -1);
1454 update_parents_table(parents_table, found, parent_name, &iter);
1456 /* remove the updated tag from the table and list */
1457 tags_table_remove(tags_table, found);
1458 *tags = g_list_delete_link(*tags, found_item);
1460 cont = next_iter(model, &iter, TRUE);
1463 tm_tag_unref(tag);
1467 /* Second pass, now we have a tree cleaned up from invalid rows,
1468 * we simply add new ones */
1469 foreach_list (item, *tags)
1471 TMTag *tag = item->data;
1472 GtkTreeIter *parent;
1474 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1475 if (G_UNLIKELY(! parent))
1476 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1477 else
1479 gboolean expand;
1480 const gchar *name;
1481 const gchar *parent_name;
1482 gchar *tooltip;
1483 GdkPixbuf *icon = get_child_icon(store, parent);
1485 parent_name = get_parent_name(tag, doc->file_type->id);
1486 if (parent_name)
1488 GList **candidates;
1489 GtkTreeIter *parent_search = NULL;
1491 /* walk parent candidates to find the better one.
1492 * if there are more than one, take the one that has the closest line number
1493 * after the tag we're searching the parent for */
1494 candidates = g_hash_table_lookup(parents_table, parent_name);
1495 if (candidates)
1497 GList *node;
1498 glong delta = G_MAXLONG;
1499 foreach_list(node, *candidates)
1501 TMTag *parent_tag;
1502 glong d;
1504 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1505 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1507 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1508 if (! parent_search || (d >= 0 && d < delta))
1510 delta = d;
1511 parent_search = node->data;
1516 if (parent_search)
1517 parent = parent_search;
1518 else
1519 parent_name = NULL;
1522 /* only expand to the iter if the parent was empty, otherwise we let the
1523 * folding as it was before (already expanded, or closed by the user) */
1524 expand = ! gtk_tree_model_iter_has_child(model, parent);
1526 /* insert the new element */
1527 gtk_tree_store_append(store, &iter, parent);
1528 name = get_symbol_name(doc, tag, parent_name != NULL);
1529 tooltip = get_symbol_tooltip(doc, tag);
1530 gtk_tree_store_set(store, &iter,
1531 SYMBOLS_COLUMN_NAME, name,
1532 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1533 SYMBOLS_COLUMN_ICON, icon,
1534 SYMBOLS_COLUMN_TAG, tag,
1535 -1);
1536 g_free(tooltip);
1537 if (G_LIKELY(icon))
1538 g_object_unref(icon);
1540 update_parents_table(parents_table, tag, parent_name, &iter);
1542 if (expand)
1543 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1547 g_hash_table_destroy(parents_table);
1548 g_hash_table_destroy(tags_table);
1552 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1553 * is not stable, so the order is already lost. */
1554 static gint compare_top_level_names(const gchar *a, const gchar *b)
1556 guint i;
1557 const gchar *name;
1559 /* This should never happen as it would mean that two or more top
1560 * level items have the same name but it can happen by typos in the translations. */
1561 if (utils_str_equal(a, b))
1562 return 1;
1564 foreach_ptr_array(name, i, top_level_iter_names)
1566 if (utils_str_equal(name, a))
1567 return -1;
1568 if (utils_str_equal(name, b))
1569 return 1;
1571 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1572 return 0;
1576 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1577 GtkTreeIter *iter)
1579 /* if the tag has a parent tag, it should be at depth >= 2 */
1580 return NZV(tag->atts.entry.scope) &&
1581 gtk_tree_store_iter_depth(store, iter) == 1;
1585 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1586 gpointer user_data)
1588 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1589 TMTag *tag_a, *tag_b;
1590 gint cmp;
1592 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1593 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1595 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1596 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1597 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1598 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1600 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1601 compare_symbol_lines(tag_a, tag_b);
1603 else
1605 gchar *astr, *bstr;
1607 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1608 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1610 /* if a is toplevel, b must be also */
1611 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1613 cmp = compare_top_level_names(astr, bstr);
1615 else
1617 /* this is what g_strcmp0() does */
1618 if (! astr)
1619 cmp = -(astr != bstr);
1620 else if (! bstr)
1621 cmp = astr != bstr;
1622 else
1624 cmp = strcmp(astr, bstr);
1626 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1627 if (tag_a && tag_b)
1628 if (!sort_by_name ||
1629 (utils_str_equal(tag_a->name, tag_b->name) &&
1630 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1631 cmp = compare_symbol_lines(tag_a, tag_b);
1634 g_free(astr);
1635 g_free(bstr);
1637 tm_tag_unref(tag_a);
1638 tm_tag_unref(tag_b);
1640 return cmp;
1644 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1646 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1647 GINT_TO_POINTER(sort_by_name), NULL);
1649 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1653 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1655 GList *tags;
1657 g_return_val_if_fail(doc != NULL, FALSE);
1659 tags = get_tag_list(doc, tm_tag_max_t);
1660 if (tags == NULL)
1661 return FALSE;
1663 /* FIXME: Not sure why we detached the model here? */
1665 /* disable sorting during update because the code doesn't support correctly
1666 * models that are currently being built */
1667 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1669 /* add grandparent type iters */
1670 add_top_level_items(doc);
1672 update_tree_tags(doc, &tags);
1673 g_list_free(tags);
1675 hide_empty_rows(doc->priv->tag_store);
1677 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1678 sort_mode = doc->priv->symbol_list_sort_mode;
1680 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1681 doc->priv->symbol_list_sort_mode = sort_mode;
1683 return TRUE;
1687 /* Detects a global tags filetype from the *.lang.* language extension.
1688 * Returns NULL if there was no matching TM language. */
1689 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1691 gchar *tags_ext;
1692 gchar *shortname = utils_strdupa(utf8_filename);
1693 GeanyFiletype *ft = NULL;
1695 tags_ext = g_strrstr(shortname, ".tags");
1696 if (tags_ext)
1698 *tags_ext = '\0'; /* remove .tags extension */
1699 ft = filetypes_detect_from_extension(shortname);
1700 if (ft->id != GEANY_FILETYPES_NONE)
1701 return ft;
1703 return NULL;
1707 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1708 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1709 * the relevant path.
1710 * Example:
1711 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1712 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1714 /* -E pre-process, -dD output user macros, -p prof info (?) */
1715 const char pre_process[] = "gcc -E -dD -p -I.";
1717 if (argc > 2)
1719 /* Create global taglist */
1720 int status;
1721 char *command;
1722 const char *tags_file = argv[1];
1723 char *utf8_fname;
1724 GeanyFiletype *ft;
1726 utf8_fname = utils_get_utf8_from_locale(tags_file);
1727 ft = detect_global_tags_filetype(utf8_fname);
1728 g_free(utf8_fname);
1730 if (ft == NULL)
1732 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1733 return 1;
1735 /* load config in case of custom filetypes */
1736 filetypes_load_config(ft->id, FALSE);
1738 /* load ignore list for C/C++ parser */
1739 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1740 load_c_ignore_tags();
1742 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1743 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1744 else
1745 command = NULL; /* don't preprocess */
1747 geany_debug("Generating %s tags file.", ft->name);
1748 tm_get_workspace();
1749 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1750 argc - 2, tags_file, ft->lang);
1751 g_free(command);
1752 symbols_finalize(); /* free c_tags_ignore data */
1753 if (! status)
1755 g_printerr(_("Failed to create tags file, perhaps because no tags "
1756 "were found.\n"));
1757 return 1;
1760 else
1762 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1763 g_printerr(_("Example:\n"
1764 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1765 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1766 return 1;
1768 return 0;
1772 void symbols_show_load_tags_dialog(void)
1774 GtkWidget *dialog;
1775 GtkFileFilter *filter;
1777 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1778 GTK_FILE_CHOOSER_ACTION_OPEN,
1779 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1780 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1781 NULL);
1782 gtk_widget_set_name(dialog, "GeanyDialog");
1783 filter = gtk_file_filter_new();
1784 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1785 gtk_file_filter_add_pattern(filter, "*.*.tags");
1786 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1788 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1790 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1791 GSList *item;
1793 for (item = flist; item != NULL; item = g_slist_next(item))
1795 gchar *fname = item->data;
1796 gchar *utf8_fname;
1797 GeanyFiletype *ft;
1799 utf8_fname = utils_get_utf8_from_locale(fname);
1800 ft = detect_global_tags_filetype(utf8_fname);
1802 if (ft != NULL && symbols_load_global_tags(fname, ft))
1803 /* For translators: the first wildcard is the filetype, the second the filename */
1804 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1805 filetypes_get_display_name(ft), utf8_fname);
1806 else
1807 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1809 g_free(utf8_fname);
1810 g_free(fname);
1812 g_slist_free(flist);
1814 gtk_widget_destroy(dialog);
1818 static void detect_tag_files(const GSList *file_list)
1820 const GSList *node;
1822 for (node = file_list; node != NULL; node = g_slist_next(node))
1824 gchar *fname = node->data;
1825 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1826 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1828 g_free(utf8_fname);
1830 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1831 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1832 else
1833 geany_debug("Unknown filetype for file '%s'.", fname);
1838 static void init_user_tags(void)
1840 GSList *file_list = NULL, *list = NULL;
1841 gchar *dir;
1843 dir = g_build_filename(app->configdir, "tags", NULL);
1844 /* create the user tags dir for next time if it doesn't exist */
1845 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1847 utils_mkdir(dir, FALSE);
1849 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1851 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1852 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1853 g_free(dir);
1855 file_list = g_slist_concat(file_list, list);
1856 detect_tag_files(file_list);
1857 /* don't need to delete list contents because they are stored in ft->priv->tag_files */
1858 g_slist_free(file_list);
1862 static void load_user_tags(filetype_id ft_id)
1864 static guchar *tags_loaded = NULL;
1865 static gboolean init_tags = FALSE;
1866 const GSList *node;
1867 GeanyFiletype *ft = filetypes[ft_id];
1869 g_return_if_fail(ft_id > 0);
1871 if (!tags_loaded)
1872 tags_loaded = g_new0(guchar, filetypes_array->len);
1873 if (tags_loaded[ft_id])
1874 return;
1875 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1877 if (!init_tags)
1879 init_user_tags();
1880 init_tags = TRUE;
1883 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1885 const gchar *fname = node->data;
1887 symbols_load_global_tags(fname, ft);
1892 static gboolean goto_tag(const gchar *name, gboolean definition)
1894 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1895 guint type;
1896 TMTag *tmtag = NULL;
1897 GeanyDocument *old_doc = document_get_current();
1899 /* goto tag definition: all except prototypes / forward declarations / externs */
1900 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1902 /* first look in the current document */
1903 if (old_doc != NULL && old_doc->tm_file)
1904 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1906 /* if not found, look in the workspace */
1907 if (tmtag == NULL)
1908 tmtag = find_workspace_tag(name, type);
1910 if (tmtag != NULL)
1912 GeanyDocument *new_doc = document_find_by_real_path(
1913 tmtag->atts.entry.file->work_object.file_name);
1915 if (new_doc)
1917 /* If we are already on the tag line, swap definition/declaration */
1918 if (new_doc == old_doc &&
1919 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1921 if (goto_tag(name, !definition))
1922 return TRUE;
1925 else
1927 /* not found in opened document, should open */
1928 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1931 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1932 return TRUE;
1934 return FALSE;
1938 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1940 if (goto_tag(name, definition))
1941 return TRUE;
1943 /* if we are here, there was no match and we are beeping ;-) */
1944 utils_beep();
1946 if (!definition)
1947 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1948 else
1949 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1950 return FALSE;
1954 /* This could perhaps be improved to check for #if, class etc. */
1955 static gint get_function_fold_number(GeanyDocument *doc)
1957 /* for Java the functions are always one fold level above the class scope */
1958 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1959 return SC_FOLDLEVELBASE + 1;
1960 else
1961 return SC_FOLDLEVELBASE;
1965 /* Should be used only with get_current_tag_cached.
1966 * tag_types caching might trigger recomputation too often but this isn't used differently often
1967 * enough to be an issue for now */
1968 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1970 static gint old_line = -2;
1971 static GeanyDocument *old_doc = NULL;
1972 static gint old_fold_num = -1;
1973 static guint old_tag_types = 0;
1974 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1975 gboolean ret;
1977 /* check if the cached line and file index have changed since last time: */
1978 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1979 ret = TRUE;
1980 else if (cur_line == old_line)
1981 ret = FALSE;
1982 else
1984 /* if the line has only changed by 1 */
1985 if (abs(cur_line - old_line) == 1)
1987 /* It's the same function if the fold number hasn't changed */
1988 ret = (fold_num != old_fold_num);
1990 else ret = TRUE;
1993 /* record current line and file index for next time */
1994 old_line = cur_line;
1995 old_doc = doc;
1996 old_fold_num = fold_num;
1997 old_tag_types = tag_types;
1998 return ret;
2002 /* Parse the function name up to 2 lines before tag_line.
2003 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2004 * type or argument names can be confused with the function name. */
2005 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2007 gint start, end, max_pos;
2008 gchar *cur_tag;
2009 gint fn_style;
2011 switch (sci_get_lexer(sci))
2013 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2014 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2015 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2017 start = sci_get_position_from_line(sci, tag_line - 2);
2018 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2019 while (sci_get_style_at(sci, start) != fn_style
2020 && start < max_pos) start++;
2022 end = start;
2023 while (sci_get_style_at(sci, end) == fn_style
2024 && end < max_pos) end++;
2026 if (start == end)
2027 return NULL;
2028 cur_tag = g_malloc(end - start + 1);
2029 sci_get_text_range(sci, start, end, cur_tag);
2030 return cur_tag;
2034 /* Parse the function name */
2035 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2037 gint start, end, first_pos, max_pos;
2038 gint tmp;
2039 gchar c;
2040 gchar *cur_tag;
2042 first_pos = end = sci_get_position_from_line(sci, tag_line);
2043 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2044 tmp = 0;
2045 /* goto the begin of function body */
2046 while (end < max_pos &&
2047 (tmp = sci_get_char_at(sci, end)) != '{' &&
2048 tmp != 0) end++;
2049 if (tmp == 0) end --;
2051 /* go back to the end of function identifier */
2052 while (end > 0 && end > first_pos - 500 &&
2053 (tmp = sci_get_char_at(sci, end)) != '(' &&
2054 tmp != 0) end--;
2055 end--;
2056 if (end < 0) end = 0;
2058 /* skip whitespaces between identifier and ( */
2059 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2061 start = end;
2062 c = 0;
2063 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2064 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2065 || tmp == SCE_C_GLOBALCLASS
2066 || (c = sci_get_char_at(sci, start)) == '~'
2067 || c == ':'))
2068 start--;
2069 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2071 if (start == end) return NULL;
2072 cur_tag = g_malloc(end - start + 2);
2073 sci_get_text_range(sci, start, end + 1, cur_tag);
2074 return cur_tag;
2078 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2080 gint line_count = sci_get_line_count(sci);
2082 for (; line < line_count; line++)
2084 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2085 return line;
2088 return -1;
2092 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2094 gint line;
2095 gint parent;
2097 line = sci_get_current_line(doc->editor->sci);
2098 parent = sci_get_fold_parent(doc->editor->sci, line);
2099 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2100 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2101 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2103 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2105 if (tag)
2107 gint tag_line = tag->atts.entry.line - 1;
2108 gint last_child = line + 1;
2110 /* if it may be a false positive because we're inside a fold level not inside anything
2111 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2112 * right after the tag we got from TM */
2113 if (abs(tag_line - parent) > 1)
2115 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2116 if (tag_fold >= 0)
2117 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2120 if (line <= last_child)
2122 if (tag->atts.entry.scope)
2123 *tagname = g_strconcat(tag->atts.entry.scope,
2124 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2125 else
2126 *tagname = g_strdup(tag->name);
2128 return tag_line;
2132 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2133 * to dirty and inaccurate hand-parsing */
2134 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2136 const gint fn_fold = get_function_fold_number(doc);
2137 gint tag_line = parent;
2138 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2140 /* find the top level fold point */
2141 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2143 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2144 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2147 if (tag_line >= 0)
2149 gchar *cur_tag;
2151 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2152 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2153 else
2154 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2156 if (cur_tag != NULL)
2158 *tagname = cur_tag;
2159 return tag_line;
2164 *tagname = g_strdup(_("unknown"));
2165 return -1;
2169 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2171 static gint tag_line = -1;
2172 static gchar *cur_tag = NULL;
2174 if (doc == NULL) /* reset current function */
2176 current_tag_changed(NULL, -1, -1, 0);
2177 g_free(cur_tag);
2178 cur_tag = g_strdup(_("unknown"));
2179 if (tagname != NULL)
2180 *tagname = cur_tag;
2181 tag_line = -1;
2183 else
2185 gint line = sci_get_current_line(doc->editor->sci);
2186 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2188 if (current_tag_changed(doc, line, fold_level, tag_types))
2190 g_free(cur_tag);
2191 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2193 *tagname = cur_tag;
2196 return tag_line;
2200 /* Sets *tagname to point at the current function or tag name.
2201 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2202 * call to this function.
2203 * Returns: line number of the current tag, or -1 if unknown. */
2204 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2206 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2210 /* same as symbols_get_current_function() but finds class, namespaces and more */
2211 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2213 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2214 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2216 /* Python parser reports imports as namespaces which confuses the scope detection */
2217 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2218 tag_types |= tm_tag_namespace_t;
2220 return get_current_tag_name_cached(doc, tagname, tag_types);
2224 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2226 gint sort_mode = GPOINTER_TO_INT(user_data);
2227 GeanyDocument *doc = document_get_current();
2229 if (ignore_callback)
2230 return;
2232 if (doc != NULL)
2233 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2237 static void on_symbol_tree_menu_show(GtkWidget *widget,
2238 gpointer user_data)
2240 GeanyDocument *doc = document_get_current();
2241 gboolean enable;
2243 enable = doc && doc->has_tags;
2244 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2245 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2246 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2247 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2249 if (! doc)
2250 return;
2252 ignore_callback = TRUE;
2254 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2255 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2256 else
2257 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2259 ignore_callback = FALSE;
2263 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2265 gboolean expand = GPOINTER_TO_INT(user_data);
2266 GeanyDocument *doc = document_get_current();
2268 if (! doc)
2269 return;
2271 g_return_if_fail(doc->priv->tag_tree);
2273 if (expand)
2274 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2275 else
2276 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2280 static void create_taglist_popup_menu(void)
2282 GtkWidget *item, *menu;
2284 tv.popup_taglist = menu = gtk_menu_new();
2286 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2287 gtk_widget_show(item);
2288 gtk_container_add(GTK_CONTAINER(menu), item);
2289 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2291 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2292 gtk_widget_show(item);
2293 gtk_container_add(GTK_CONTAINER(menu), item);
2294 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2296 item = gtk_separator_menu_item_new();
2297 gtk_widget_show(item);
2298 gtk_container_add(GTK_CONTAINER(menu), item);
2300 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2301 _("Sort by _Name"));
2302 gtk_widget_show(item);
2303 gtk_container_add(GTK_CONTAINER(menu), item);
2304 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2305 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2307 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2308 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2309 gtk_widget_show(item);
2310 gtk_container_add(GTK_CONTAINER(menu), item);
2311 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2312 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2314 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2316 sidebar_add_common_menu_items(GTK_MENU(menu));
2320 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2322 gchar *f = g_build_filename(app->configdir, "ignore.tags", NULL);
2324 g_return_if_fail(NZV(doc->real_path));
2326 if (utils_str_equal(doc->real_path, f))
2327 load_c_ignore_tags();
2329 g_free(f);
2333 void symbols_init(void)
2335 gchar *f;
2337 create_taglist_popup_menu();
2339 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2340 ui_add_config_file_menu_item(f, NULL, NULL);
2341 g_free(f);
2343 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2347 void symbols_finalize(void)
2349 g_strfreev(html_entities);
2350 g_strfreev(c_tags_ignore);