prefs: Remove some global state in keybinding-related code
[geany-mirror.git] / src / symbols.c
blob6d9933ec2b3860ab1a820adaed80d06b7cf073ee
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_tag.h"
54 #include "ui_utils.h"
55 #include "utils.h"
57 #include "SciLexer.h"
59 #include "gtkcompat.h"
61 #include <ctype.h>
62 #include <string.h>
63 #include <stdlib.h>
66 const guint TM_GLOBAL_TYPE_MASK =
67 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
68 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
71 static gchar **html_entities = NULL;
73 typedef struct
75 gboolean tags_loaded;
76 const gchar *tag_file;
77 } TagFileInfo;
79 /* Check before adding any more tags files, usually they should be downloaded separately. */
80 enum /* Geany tag files */
82 GTF_C,
83 GTF_PASCAL,
84 GTF_PHP,
85 GTF_HTML_ENTITIES,
86 GTF_LATEX,
87 GTF_PYTHON,
88 GTF_MAX
91 static TagFileInfo tag_file_info[GTF_MAX] =
93 {FALSE, "c99.tags"},
94 {FALSE, "pascal.tags"},
95 {FALSE, "php.tags"},
96 {FALSE, "html_entities.tags"},
97 {FALSE, "latex.tags"},
98 {FALSE, "python.tags"}
101 static GPtrArray *top_level_iter_names = NULL;
103 static struct
105 GtkWidget *expand_all;
106 GtkWidget *collapse_all;
107 GtkWidget *sort_by_name;
108 GtkWidget *sort_by_appearance;
109 GtkWidget *find_usage;
110 GtkWidget *find_doc_usage;
111 GtkWidget *find_in_files;
113 symbol_menu;
116 static void html_tags_loaded(void);
117 static void load_user_tags(filetype_id ft_id);
119 /* get the tags_ignore list, exported by tagmanager's options.c */
120 extern gchar **c_tags_ignore;
122 /* ignore certain tokens when parsing C-like syntax.
123 * Also works for reloading. */
124 static void load_c_ignore_tags(void)
126 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
127 gchar *content;
129 if (g_file_get_contents(path, &content, NULL, NULL))
131 /* historically we ignore the glib _DECLS for tag generation */
132 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
134 g_strfreev(c_tags_ignore);
135 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
136 g_free(content);
138 g_free(path);
142 void symbols_reload_config_files(void)
144 load_c_ignore_tags();
148 static gsize get_tag_count(void)
150 GPtrArray *tags = tm_get_workspace()->global_tags;
151 gsize count = tags ? tags->len : 0;
153 return count;
157 /* wrapper for tm_workspace_load_global_tags().
158 * note that the tag count only counts new global tags added - if a tag has the same name,
159 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
160 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
162 gboolean result;
163 gsize old_tag_count = get_tag_count();
165 result = tm_workspace_load_global_tags(tags_file, ft->lang);
166 if (result)
168 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
169 (guint) (get_tag_count() - old_tag_count));
171 return result;
175 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
176 * This provides autocompletion, calltips, etc. */
177 void symbols_global_tags_loaded(guint file_type_idx)
179 TagFileInfo *tfi;
180 gint tag_type;
182 /* load ignore list for C/C++ parser */
183 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
184 c_tags_ignore == NULL)
186 load_c_ignore_tags();
189 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
190 return;
192 /* load config in case of custom filetypes */
193 filetypes_load_config(file_type_idx, FALSE);
195 load_user_tags(file_type_idx);
197 switch (file_type_idx)
199 case GEANY_FILETYPES_PHP:
200 case GEANY_FILETYPES_HTML:
201 html_tags_loaded();
203 switch (file_type_idx)
205 case GEANY_FILETYPES_CPP:
206 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
207 /* no C++ tagfile yet */
208 return;
209 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
210 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
211 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
212 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
213 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
214 default:
215 return;
217 tfi = &tag_file_info[tag_type];
219 if (! tfi->tags_loaded)
221 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
223 symbols_load_global_tags(fname, filetypes[file_type_idx]);
224 tfi->tags_loaded = TRUE;
225 g_free(fname);
230 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
231 static void html_tags_loaded(void)
233 TagFileInfo *tfi;
235 if (cl_options.ignore_global_tags)
236 return;
238 tfi = &tag_file_info[GTF_HTML_ENTITIES];
239 if (! tfi->tags_loaded)
241 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
243 html_entities = utils_read_file_in_array(file);
244 tfi->tags_loaded = TRUE;
245 g_free(file);
250 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
252 guint j;
253 TMTag *tag;
254 GString *s = NULL;
255 GPtrArray *typedefs;
256 gint tag_lang;
258 g_return_val_if_fail(tags_array != NULL, NULL);
260 typedefs = tm_tags_extract(tags_array, tag_types);
262 if ((typedefs) && (typedefs->len > 0))
264 s = g_string_sized_new(typedefs->len * 10);
265 for (j = 0; j < typedefs->len; ++j)
267 tag = TM_TAG(typedefs->pdata[j]);
268 /* tag->atts.file.lang contains (for some reason) the line of the tag if
269 * tag->atts.entry.file is not NULL */
270 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
272 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
273 * e.g. PHP classes in C++ files
274 * lang = -2 disables the check */
275 if (tag->name && (tag_lang == lang || lang == -2))
277 if (j != 0)
278 g_string_append_c(s, ' ');
279 g_string_append(s, tag->name);
283 if (typedefs)
284 g_ptr_array_free(typedefs, TRUE);
285 return s;
289 /** Gets the context separator used by the tag manager for a particular file
290 * type.
291 * @param ft_id File type identifier.
292 * @return The context separator string.
294 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
295 * without a context separator.
297 * @since 0.19
299 const gchar *symbols_get_context_separator(gint ft_id)
301 switch (ft_id)
303 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
304 case GEANY_FILETYPES_CPP:
305 case GEANY_FILETYPES_GLSL: /* for structs */
306 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
307 case GEANY_FILETYPES_PHP:
308 case GEANY_FILETYPES_RUST:
309 return "::";
311 /* avoid confusion with other possible separators in group/section name */
312 case GEANY_FILETYPES_CONF:
313 case GEANY_FILETYPES_REST:
314 return ":::";
316 /* no context separator */
317 case GEANY_FILETYPES_ASCIIDOC:
318 return "\x03";
320 default:
321 return ".";
326 GString *symbols_get_macro_list(gint lang)
328 guint j, i;
329 GPtrArray *ftags;
330 GString *words;
331 gint tag_lang;
332 TMTag *tag;
334 if (app->tm_workspace->work_objects == NULL)
335 return NULL;
337 ftags = g_ptr_array_sized_new(50);
338 words = g_string_sized_new(200);
340 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
342 GPtrArray *tags;
344 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
345 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
346 if (NULL != tags)
348 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
350 tag = TM_TAG(tags->pdata[i]);
351 tag_lang = (tag->atts.entry.file) ?
352 tag->atts.entry.file->lang : tag->atts.file.lang;
354 if (tag_lang == lang)
355 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
357 g_ptr_array_free(tags, TRUE);
361 if (ftags->len == 0)
363 g_ptr_array_free(ftags, TRUE);
364 g_string_free(words, TRUE);
365 return NULL;
368 tm_tags_sort(ftags, NULL, FALSE);
369 for (j = 0; j < ftags->len; j++)
371 if (j > 0)
372 g_string_append_c(words, '\n');
373 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
375 g_ptr_array_free(ftags, TRUE);
376 return words;
380 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
381 static TMTag *
382 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
384 guint i;
385 g_return_val_if_fail(tags != NULL, NULL);
387 for (i = 0; i < tags->len; ++i)
389 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
390 return TM_TAG(tags->pdata[i]);
392 return NULL;
396 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
397 const gchar *tag_name, guint type)
399 GPtrArray *tags;
400 TMTag *tmtag;
402 if (G_LIKELY(workobj != NULL))
404 tags = tm_tags_extract(workobj->tags_array, type);
405 if (tags != NULL)
407 tmtag = symbols_find_tm_tag(tags, tag_name);
409 g_ptr_array_free(tags, TRUE);
411 if (tmtag != NULL)
412 return tmtag;
415 return NULL; /* not found */
419 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
421 guint j;
422 const GPtrArray *work_objects = NULL;
424 if (app->tm_workspace != NULL)
425 work_objects = app->tm_workspace->work_objects;
427 if (work_objects != NULL)
429 for (j = 0; j < work_objects->len; j++)
431 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
432 TMTag *tmtag;
434 tmtag = find_work_object_tag(workobj, tag_name, type);
435 if (tmtag != NULL)
436 return tmtag;
439 return NULL; /* not found */
443 const gchar **symbols_get_html_entities(void)
445 if (html_entities == NULL)
446 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
448 return (const gchar **) html_entities;
452 /* sort by name, then line */
453 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
455 gint ret;
457 if (tag_a == NULL || tag_b == NULL)
458 return 0;
460 if (tag_a->name == NULL)
461 return -(tag_a->name != tag_b->name);
463 if (tag_b->name == NULL)
464 return tag_a->name != tag_b->name;
466 ret = strcmp(tag_a->name, tag_b->name);
467 if (ret == 0)
469 return tag_a->atts.entry.line - tag_b->atts.entry.line;
471 return ret;
475 /* sort by line, then scope */
476 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
478 const TMTag *tag_a = TM_TAG(a);
479 const TMTag *tag_b = TM_TAG(b);
480 gint ret;
482 if (a == NULL || b == NULL)
483 return 0;
485 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
486 if (ret == 0)
488 if (tag_a->atts.entry.scope == NULL)
489 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
490 if (tag_b->atts.entry.scope == NULL)
491 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
492 else
493 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
495 return ret;
499 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
501 GList *tag_names = NULL;
502 TMTag *tag;
503 guint i;
505 g_return_val_if_fail(doc, NULL);
507 if (! doc->tm_file || ! doc->tm_file->tags_array)
508 return NULL;
510 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
512 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
513 if (G_UNLIKELY(tag == NULL))
514 return NULL;
516 if (tag->type & tag_types)
518 tag_names = g_list_prepend(tag_names, tag);
521 tag_names = g_list_sort(tag_names, compare_symbol_lines);
522 return tag_names;
526 /* amount of types in the symbol list (currently max. 8 are used) */
527 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
529 struct TreeviewSymbols
531 GtkTreeIter tag_function;
532 GtkTreeIter tag_class;
533 GtkTreeIter tag_macro;
534 GtkTreeIter tag_member;
535 GtkTreeIter tag_variable;
536 GtkTreeIter tag_externvar;
537 GtkTreeIter tag_namespace;
538 GtkTreeIter tag_struct;
539 GtkTreeIter tag_interface;
540 GtkTreeIter tag_type;
541 GtkTreeIter tag_other;
542 } tv_iters;
545 static void init_tag_iters(void)
547 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
548 * filetypes(e.g. config file to Python crashes Geany without this) */
549 tv_iters.tag_function.stamp = -1;
550 tv_iters.tag_class.stamp = -1;
551 tv_iters.tag_member.stamp = -1;
552 tv_iters.tag_macro.stamp = -1;
553 tv_iters.tag_variable.stamp = -1;
554 tv_iters.tag_externvar.stamp = -1;
555 tv_iters.tag_namespace.stamp = -1;
556 tv_iters.tag_struct.stamp = -1;
557 tv_iters.tag_interface.stamp = -1;
558 tv_iters.tag_type.stamp = -1;
559 tv_iters.tag_other.stamp = -1;
563 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
565 static GtkIconTheme *icon_theme = NULL;
566 static gint x = -1;
568 if (G_UNLIKELY(x < 0))
570 gint dummy;
571 icon_theme = gtk_icon_theme_get_default();
572 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
574 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
578 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
580 GtkTreeModel *model = GTK_TREE_MODEL(store);
582 if (!gtk_tree_model_get_iter_first(model, iter))
583 return FALSE;
586 gchar *candidate;
588 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
589 /* FIXME: what if 2 different items have the same name?
590 * this should never happen, but might be caused by a typo in a translation */
591 if (utils_str_equal(candidate, title))
593 g_free(candidate);
594 return TRUE;
596 else
597 g_free(candidate);
599 while (gtk_tree_model_iter_next(model, iter));
601 return FALSE;
605 /* Adds symbol list groups in (iter*, title) pairs.
606 * The list must be ended with NULL. */
607 static void G_GNUC_NULL_TERMINATED
608 tag_list_add_groups(GtkTreeStore *tree_store, ...)
610 va_list args;
611 GtkTreeIter *iter;
613 g_return_if_fail(top_level_iter_names);
615 va_start(args, tree_store);
616 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
618 gchar *title = va_arg(args, gchar*);
619 gchar *icon_name = va_arg(args, gchar *);
620 GdkPixbuf *icon = NULL;
622 if (icon_name)
624 icon = get_tag_icon(icon_name);
627 g_assert(title != NULL);
628 g_ptr_array_add(top_level_iter_names, title);
630 if (!find_toplevel_iter(tree_store, iter, title))
631 gtk_tree_store_append(tree_store, iter, NULL);
633 if (G_IS_OBJECT(icon))
635 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
636 g_object_unref(icon);
638 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
640 va_end(args);
644 static void add_top_level_items(GeanyDocument *doc)
646 filetype_id ft_id = doc->file_type->id;
647 GtkTreeStore *tag_store = doc->priv->tag_store;
649 if (top_level_iter_names == NULL)
650 top_level_iter_names = g_ptr_array_new();
651 else
652 g_ptr_array_set_size(top_level_iter_names, 0);
654 init_tag_iters();
656 switch (ft_id)
658 case GEANY_FILETYPES_DIFF:
660 tag_list_add_groups(tag_store,
661 &(tv_iters.tag_function), _("Files"), NULL, NULL);
662 break;
664 case GEANY_FILETYPES_DOCBOOK:
666 tag_list_add_groups(tag_store,
667 &(tv_iters.tag_function), _("Chapter"), NULL,
668 &(tv_iters.tag_class), _("Section"), NULL,
669 &(tv_iters.tag_member), _("Sect1"), NULL,
670 &(tv_iters.tag_macro), _("Sect2"), NULL,
671 &(tv_iters.tag_variable), _("Sect3"), NULL,
672 &(tv_iters.tag_struct), _("Appendix"), NULL,
673 &(tv_iters.tag_other), _("Other"), NULL,
674 NULL);
675 break;
677 case GEANY_FILETYPES_HASKELL:
678 tag_list_add_groups(tag_store,
679 &tv_iters.tag_namespace, _("Module"), NULL,
680 &tv_iters.tag_type, _("Types"), NULL,
681 &tv_iters.tag_macro, _("Type constructors"), NULL,
682 &tv_iters.tag_function, _("Functions"), "classviewer-method",
683 NULL);
684 break;
685 case GEANY_FILETYPES_COBOL:
686 tag_list_add_groups(tag_store,
687 &tv_iters.tag_class, _("Program"), "classviewer-class",
688 &tv_iters.tag_function, _("File"), "classviewer-method",
689 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
690 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
691 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
692 &tv_iters.tag_variable, _("Data"), "classviewer-var",
693 NULL);
694 break;
695 case GEANY_FILETYPES_CONF:
696 tag_list_add_groups(tag_store,
697 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
698 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
699 NULL);
700 break;
701 case GEANY_FILETYPES_NSIS:
702 tag_list_add_groups(tag_store,
703 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
704 &tv_iters.tag_function, _("Functions"), "classviewer-method",
705 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
706 NULL);
707 break;
708 case GEANY_FILETYPES_LATEX:
710 tag_list_add_groups(tag_store,
711 &(tv_iters.tag_function), _("Command"), NULL,
712 &(tv_iters.tag_class), _("Environment"), NULL,
713 &(tv_iters.tag_member), _("Section"), NULL,
714 &(tv_iters.tag_macro), _("Subsection"), NULL,
715 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
716 &(tv_iters.tag_struct), _("Label"), NULL,
717 &(tv_iters.tag_namespace), _("Chapter"), NULL,
718 &(tv_iters.tag_other), _("Other"), NULL,
719 NULL);
720 break;
722 case GEANY_FILETYPES_MATLAB:
724 tag_list_add_groups(tag_store,
725 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
726 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
727 NULL);
728 break;
730 case GEANY_FILETYPES_ABAQUS:
732 tag_list_add_groups(tag_store,
733 &(tv_iters.tag_class), _("Parts"), NULL,
734 &(tv_iters.tag_member), _("Assembly"), NULL,
735 &(tv_iters.tag_namespace), _("Steps"), NULL,
736 NULL);
737 break;
739 case GEANY_FILETYPES_R:
741 tag_list_add_groups(tag_store,
742 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
743 &(tv_iters.tag_other), _("Other"), NULL,
744 NULL);
745 break;
747 case GEANY_FILETYPES_RUST:
749 tag_list_add_groups(tag_store,
750 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
751 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
752 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
753 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
754 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
755 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
756 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
757 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
758 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
759 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
760 NULL);
761 break;
763 case GEANY_FILETYPES_PERL:
765 tag_list_add_groups(tag_store,
766 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
767 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
768 &(tv_iters.tag_macro), _("Labels"), NULL,
769 &(tv_iters.tag_type), _("Constants"), NULL,
770 &(tv_iters.tag_other), _("Other"), "classviewer-other",
771 NULL);
772 break;
774 case GEANY_FILETYPES_PHP:
776 tag_list_add_groups(tag_store,
777 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
778 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
779 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
780 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
781 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
782 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
783 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
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_ASCIIDOC:
820 tag_list_add_groups(tag_store,
821 &(tv_iters.tag_namespace), _("Document"), NULL,
822 &(tv_iters.tag_member), _("Section Level 1"), NULL,
823 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
824 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
825 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
826 NULL);
827 break;
829 case GEANY_FILETYPES_RUBY:
831 tag_list_add_groups(tag_store,
832 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
833 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
834 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
835 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
836 NULL);
837 break;
839 case GEANY_FILETYPES_TCL:
841 tag_list_add_groups(tag_store,
842 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
843 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
844 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
845 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
846 NULL);
847 break;
849 case GEANY_FILETYPES_PYTHON:
851 tag_list_add_groups(tag_store,
852 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
853 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
854 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
855 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
856 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
857 NULL);
858 break;
860 case GEANY_FILETYPES_VHDL:
862 tag_list_add_groups(tag_store,
863 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
864 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
865 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
866 &(tv_iters.tag_type), _("Types"), "classviewer-other",
867 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
868 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
869 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
870 &(tv_iters.tag_other), _("Other"), "classviewer-other",
871 NULL);
872 break;
874 case GEANY_FILETYPES_VERILOG:
876 tag_list_add_groups(tag_store,
877 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
878 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
879 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
880 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
881 &(tv_iters.tag_other), _("Other"), "classviewer-other",
882 NULL);
883 break;
885 case GEANY_FILETYPES_JAVA:
887 tag_list_add_groups(tag_store,
888 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
889 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
890 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
891 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
892 &(tv_iters.tag_member), _("Members"), "classviewer-member",
893 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
894 &(tv_iters.tag_other), _("Other"), "classviewer-other",
895 NULL);
896 break;
898 case GEANY_FILETYPES_AS:
900 tag_list_add_groups(tag_store,
901 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
902 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
903 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
904 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
905 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
906 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
907 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
908 &(tv_iters.tag_other), _("Other"), "classviewer-other",
909 NULL);
910 break;
912 case GEANY_FILETYPES_HAXE:
914 tag_list_add_groups(tag_store,
915 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
916 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
917 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
918 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
919 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
920 &(tv_iters.tag_other), _("Other"), "classviewer-other",
921 NULL);
922 break;
924 case GEANY_FILETYPES_BASIC:
926 tag_list_add_groups(tag_store,
927 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
928 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
929 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
930 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
931 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
932 &(tv_iters.tag_other), _("Other"), "classviewer-other",
933 NULL);
934 break;
936 case GEANY_FILETYPES_F77:
937 case GEANY_FILETYPES_FORTRAN:
939 tag_list_add_groups(tag_store,
940 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
941 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
942 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
943 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
944 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
945 &(tv_iters.tag_class), _("Types"), "classviewer-class",
946 &(tv_iters.tag_member), _("Components"), "classviewer-member",
947 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
948 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
949 &(tv_iters.tag_other), _("Other"), "classviewer-other",
950 NULL);
951 break;
953 case GEANY_FILETYPES_ASM:
955 tag_list_add_groups(tag_store,
956 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
957 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
958 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
959 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
960 NULL);
961 break;
963 case GEANY_FILETYPES_MAKE:
964 tag_list_add_groups(tag_store,
965 &tv_iters.tag_function, _("Targets"), "classviewer-method",
966 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
967 NULL);
968 break;
969 case GEANY_FILETYPES_SQL:
971 tag_list_add_groups(tag_store,
972 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
973 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
974 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
975 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
976 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
977 &(tv_iters.tag_member), _("Views"), "classviewer-var",
978 &(tv_iters.tag_other), _("Other"), "classviewer-other",
979 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
980 NULL);
981 break;
983 case GEANY_FILETYPES_D:
984 default:
986 if (ft_id == GEANY_FILETYPES_D)
987 tag_list_add_groups(tag_store,
988 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
989 else
990 tag_list_add_groups(tag_store,
991 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
993 tag_list_add_groups(tag_store,
994 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
995 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
996 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
997 &(tv_iters.tag_member), _("Members"), "classviewer-member",
998 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
999 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
1000 NULL);
1002 if (ft_id != GEANY_FILETYPES_D)
1004 tag_list_add_groups(tag_store,
1005 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1007 tag_list_add_groups(tag_store,
1008 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1009 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1010 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1016 /* removes toplevel items that have no children */
1017 static void hide_empty_rows(GtkTreeStore *store)
1019 GtkTreeIter iter;
1020 gboolean cont = TRUE;
1022 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1023 return; /* stop when first iter is invalid, i.e. no elements */
1025 while (cont)
1027 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1028 cont = gtk_tree_store_remove(store, &iter);
1029 else
1030 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1035 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1037 gchar *utf8_name;
1038 const gchar *scope = tag->atts.entry.scope;
1039 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1040 gboolean doc_is_utf8 = FALSE;
1042 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1043 * for None at this point completely */
1044 if (utils_str_equal(doc->encoding, "UTF-8") ||
1045 utils_str_equal(doc->encoding, "None"))
1046 doc_is_utf8 = TRUE;
1047 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1048 * plugin might have called tm_source_file_update(), so check to be sure */
1049 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1051 if (! doc_is_utf8)
1052 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1053 -1, doc->encoding, TRUE);
1054 else
1055 utf8_name = tag->name;
1057 if (utf8_name == NULL)
1058 return NULL;
1060 if (! buffer)
1061 buffer = g_string_new(NULL);
1062 else
1063 g_string_truncate(buffer, 0);
1065 /* check first char of scope is a wordchar */
1066 if (!found_parent && scope &&
1067 strpbrk(scope, GEANY_WORDCHARS) == scope)
1069 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1071 g_string_append(buffer, scope);
1072 g_string_append(buffer, sep);
1074 g_string_append(buffer, utf8_name);
1076 if (! doc_is_utf8)
1077 g_free(utf8_name);
1079 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1081 return buffer->str;
1085 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1087 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1089 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1090 * for None at this point completely */
1091 if (utf8_name != NULL &&
1092 ! utils_str_equal(doc->encoding, "UTF-8") &&
1093 ! utils_str_equal(doc->encoding, "None"))
1095 SETPTR(utf8_name,
1096 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1099 if (utf8_name != NULL)
1100 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1102 return utf8_name;
1106 /* find the last word in "foo::bar::blah", e.g. "blah" */
1107 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1109 const gchar *scope = tag->atts.entry.scope;
1110 const gchar *separator = symbols_get_context_separator(ft_id);
1111 const gchar *str, *ptr;
1113 if (!scope)
1114 return NULL;
1116 str = scope;
1118 while (1)
1120 ptr = strstr(str, separator);
1121 if (ptr)
1123 str = ptr + strlen(separator);
1125 else
1126 break;
1129 return !EMPTY(str) ? str : NULL;
1133 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1135 GtkTreeIter *iter = NULL;
1137 switch (tag_type)
1139 case tm_tag_prototype_t:
1140 case tm_tag_method_t:
1141 case tm_tag_function_t:
1143 iter = &tv_iters.tag_function;
1144 break;
1146 case tm_tag_externvar_t:
1148 iter = &tv_iters.tag_externvar;
1149 break;
1151 case tm_tag_macro_t:
1152 case tm_tag_macro_with_arg_t:
1154 iter = &tv_iters.tag_macro;
1155 break;
1157 case tm_tag_class_t:
1159 iter = &tv_iters.tag_class;
1160 break;
1162 case tm_tag_member_t:
1163 case tm_tag_field_t:
1165 iter = &tv_iters.tag_member;
1166 break;
1168 case tm_tag_typedef_t:
1169 case tm_tag_enum_t:
1171 iter = &tv_iters.tag_type;
1172 break;
1174 case tm_tag_union_t:
1175 case tm_tag_struct_t:
1177 iter = &tv_iters.tag_struct;
1178 break;
1180 case tm_tag_interface_t:
1181 iter = &tv_iters.tag_interface;
1182 break;
1183 case tm_tag_variable_t:
1185 iter = &tv_iters.tag_variable;
1186 break;
1188 case tm_tag_namespace_t:
1189 case tm_tag_package_t:
1191 iter = &tv_iters.tag_namespace;
1192 break;
1194 default:
1196 iter = &tv_iters.tag_other;
1199 if (G_LIKELY(iter->stamp != -1))
1200 return iter;
1201 else
1202 return NULL;
1206 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1208 GdkPixbuf *icon = NULL;
1210 if (parent == &tv_iters.tag_other)
1212 return get_tag_icon("classviewer-var");
1214 /* copy parent icon */
1215 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1216 SYMBOLS_COLUMN_ICON, &icon, -1);
1217 return icon;
1221 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1223 const TMTag *t1 = v1;
1224 const TMTag *t2 = v2;
1226 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1227 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1228 /* include arglist in match to support e.g. C++ overloading */
1229 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1233 /* inspired from g_str_hash() */
1234 static guint tag_hash(gconstpointer v)
1236 const TMTag *tag = v;
1237 const gchar *p;
1238 guint32 h = 5381;
1240 h = (h << 5) + h + tag->type;
1241 for (p = tag->name; *p != '\0'; p++)
1242 h = (h << 5) + h + *p;
1243 if (tag->atts.entry.scope)
1245 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1246 h = (h << 5) + h + *p;
1248 /* for e.g. C++ overloading */
1249 if (tag->atts.entry.arglist)
1251 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1252 h = (h << 5) + h + *p;
1255 return h;
1259 /* like gtk_tree_view_expand_to_path() but with an iter */
1260 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1262 GtkTreeModel *model = gtk_tree_view_get_model(view);
1263 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1265 gtk_tree_view_expand_to_path(view, path);
1266 gtk_tree_path_free(path);
1270 /* like gtk_tree_store_remove() but finds the next iter at any level */
1271 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1273 GtkTreeIter parent;
1274 gboolean has_parent;
1275 gboolean cont;
1277 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1278 cont = gtk_tree_store_remove(store, iter);
1279 /* if there is no next at this level but there is a parent iter, continue from it */
1280 if (! cont && has_parent)
1282 *iter = parent;
1283 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1286 return cont;
1290 /* adds a new element in the parent table if it's key is known.
1291 * duplicates are kept */
1292 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1293 const GtkTreeIter *iter)
1295 GList **list;
1296 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1297 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1299 if (! list)
1301 list = g_slice_alloc(sizeof *list);
1302 *list = NULL;
1303 g_hash_table_insert(table, tag->name, list);
1305 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1310 static void free_iter_slice_list(gpointer data)
1312 GList **list = data;
1314 if (list)
1316 GList *node;
1317 foreach_list(node, *list)
1318 g_slice_free(GtkTreeIter, node->data);
1319 g_list_free(*list);
1320 g_slice_free1(sizeof *list, list);
1325 /* inserts a @data in @table on key @tag.
1326 * previous data is not overwritten if the key is duplicated, but rather the
1327 * two values are kept in a list
1329 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1330 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1332 GList *list = g_hash_table_lookup(table, tag);
1333 list = g_list_prepend(list, data);
1334 g_hash_table_insert(table, tag, list);
1338 /* looks up the entry in @table that better matches @tag.
1339 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1340 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1342 GList *data = NULL;
1343 GList *node = g_hash_table_lookup(table, tag);
1344 if (node)
1346 glong delta;
1347 data = node->data;
1349 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1351 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1352 for (node = node->next; node; node = node->next)
1354 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1356 if (d < delta)
1358 data = node->data;
1359 delta = d;
1363 #undef TAG_DELTA
1366 return data;
1370 /* removes the element at @tag from @table.
1371 * @tag must be the exact pointer used at insertion time */
1372 static void tags_table_remove(GHashTable *table, TMTag *tag)
1374 GList *list = g_hash_table_lookup(table, tag);
1375 if (list)
1377 GList *node;
1378 foreach_list(node, list)
1380 if (((GList *) node->data)->data == tag)
1381 break;
1383 list = g_list_delete_link(list, node);
1384 if (list)
1385 g_hash_table_insert(table, tag, list);
1386 else
1387 g_hash_table_remove(table, tag);
1392 static void tags_table_destroy(GHashTable *table)
1394 /* free any leftover elements. note that we can't register a value_free_func when
1395 * creating the hash table because we only want to free it when destroying the table,
1396 * not when inserting a duplicate (we handle this manually) */
1397 GHashTableIter iter;
1398 gpointer value;
1400 g_hash_table_iter_init(&iter, table);
1401 while (g_hash_table_iter_next(&iter, NULL, &value))
1402 g_list_free(value);
1403 g_hash_table_destroy(table);
1408 * Updates the tag tree for a document with the tags in *list.
1409 * @param doc a document
1410 * @param tags a pointer to a GList* holding the tags to add/update. This
1411 * list may be updated, removing updated elements.
1413 * The update is done in two passes:
1414 * 1) walking the current tree, update tags that still exist and remove the
1415 * obsolescent ones;
1416 * 2) walking the remaining (non updated) tags, adds them in the list.
1418 * For better performances, we use 2 hash tables:
1419 * - one containing all the tags for lookup in the first pass (actually stores a
1420 * reference in the tags list for removing it efficiently), avoiding list search
1421 * on each tag;
1422 * - the other holding "tag-name":row references for tags having children, used to
1423 * lookup for a parent in both passes, avoiding tree traversal.
1425 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1427 GtkTreeStore *store = doc->priv->tag_store;
1428 GtkTreeModel *model = GTK_TREE_MODEL(store);
1429 GHashTable *parents_table;
1430 GHashTable *tags_table;
1431 GtkTreeIter iter;
1432 gboolean cont;
1433 GList *item;
1435 /* Build hash tables holding tags and parents */
1436 /* parent table holds "tag-name":GtkTreeIter */
1437 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1438 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1439 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1440 foreach_list(item, *tags)
1442 TMTag *tag = item->data;
1443 const gchar *name;
1445 tags_table_insert(tags_table, tag, item);
1447 name = get_parent_name(tag, doc->file_type->id);
1448 if (name)
1449 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1452 /* First pass, update existing rows or delete them.
1453 * It is OK to delete them since we walk top down so we would remove
1454 * parents before checking for their children, thus never implicitly
1455 * deleting an updated child */
1456 cont = gtk_tree_model_get_iter_first(model, &iter);
1457 while (cont)
1459 TMTag *tag;
1461 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1462 if (! tag) /* most probably a toplevel, skip it */
1463 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1464 else
1466 GList *found_item;
1468 found_item = tags_table_lookup(tags_table, tag);
1469 if (! found_item) /* tag doesn't exist, remove it */
1470 cont = tree_store_remove_row(store, &iter);
1471 else /* tag still exist, update it */
1473 const gchar *name;
1474 const gchar *parent_name;
1475 TMTag *found = found_item->data;
1477 parent_name = get_parent_name(found, doc->file_type->id);
1478 /* if parent is unknown, ignore it */
1479 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1480 parent_name = NULL;
1482 /* only update fields that (can) have changed (name that holds line
1483 * number, and the tag itself) */
1484 name = get_symbol_name(doc, found, parent_name != NULL);
1485 gtk_tree_store_set(store, &iter,
1486 SYMBOLS_COLUMN_NAME, name,
1487 SYMBOLS_COLUMN_TAG, found,
1488 -1);
1490 update_parents_table(parents_table, found, parent_name, &iter);
1492 /* remove the updated tag from the table and list */
1493 tags_table_remove(tags_table, found);
1494 *tags = g_list_delete_link(*tags, found_item);
1496 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1499 tm_tag_unref(tag);
1503 /* Second pass, now we have a tree cleaned up from invalid rows,
1504 * we simply add new ones */
1505 foreach_list (item, *tags)
1507 TMTag *tag = item->data;
1508 GtkTreeIter *parent;
1510 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1511 if (G_UNLIKELY(! parent))
1512 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1513 else
1515 gboolean expand;
1516 const gchar *name;
1517 const gchar *parent_name;
1518 gchar *tooltip;
1519 GdkPixbuf *icon = get_child_icon(store, parent);
1521 parent_name = get_parent_name(tag, doc->file_type->id);
1522 if (parent_name)
1524 GList **candidates;
1525 GtkTreeIter *parent_search = NULL;
1527 /* walk parent candidates to find the better one.
1528 * if there are more than one, take the one that has the closest line number
1529 * after the tag we're searching the parent for */
1530 candidates = g_hash_table_lookup(parents_table, parent_name);
1531 if (candidates)
1533 GList *node;
1534 glong delta = G_MAXLONG;
1535 foreach_list(node, *candidates)
1537 TMTag *parent_tag;
1538 glong d;
1540 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1541 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1543 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1544 if (! parent_search || (d >= 0 && d < delta))
1546 delta = d;
1547 parent_search = node->data;
1549 tm_tag_unref(parent_tag);
1553 if (parent_search)
1554 parent = parent_search;
1555 else
1556 parent_name = NULL;
1559 /* only expand to the iter if the parent was empty, otherwise we let the
1560 * folding as it was before (already expanded, or closed by the user) */
1561 expand = ! gtk_tree_model_iter_has_child(model, parent);
1563 /* insert the new element */
1564 gtk_tree_store_append(store, &iter, parent);
1565 name = get_symbol_name(doc, tag, parent_name != NULL);
1566 tooltip = get_symbol_tooltip(doc, tag);
1567 gtk_tree_store_set(store, &iter,
1568 SYMBOLS_COLUMN_NAME, name,
1569 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1570 SYMBOLS_COLUMN_ICON, icon,
1571 SYMBOLS_COLUMN_TAG, tag,
1572 -1);
1573 g_free(tooltip);
1574 if (G_LIKELY(icon))
1575 g_object_unref(icon);
1577 update_parents_table(parents_table, tag, parent_name, &iter);
1579 if (expand)
1580 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1584 g_hash_table_destroy(parents_table);
1585 tags_table_destroy(tags_table);
1589 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1590 * is not stable, so the order is already lost. */
1591 static gint compare_top_level_names(const gchar *a, const gchar *b)
1593 guint i;
1594 const gchar *name;
1596 /* This should never happen as it would mean that two or more top
1597 * level items have the same name but it can happen by typos in the translations. */
1598 if (utils_str_equal(a, b))
1599 return 1;
1601 foreach_ptr_array(name, i, top_level_iter_names)
1603 if (utils_str_equal(name, a))
1604 return -1;
1605 if (utils_str_equal(name, b))
1606 return 1;
1608 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1609 return 0;
1613 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1614 GtkTreeIter *iter)
1616 /* if the tag has a parent tag, it should be at depth >= 2 */
1617 return !EMPTY(tag->atts.entry.scope) &&
1618 gtk_tree_store_iter_depth(store, iter) == 1;
1622 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1623 gpointer user_data)
1625 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1626 TMTag *tag_a, *tag_b;
1627 gint cmp;
1629 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1630 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1632 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1633 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1634 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1635 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1637 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1638 compare_symbol_lines(tag_a, tag_b);
1640 else
1642 gchar *astr, *bstr;
1644 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1645 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1647 /* if a is toplevel, b must be also */
1648 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1650 cmp = compare_top_level_names(astr, bstr);
1652 else
1654 /* this is what g_strcmp0() does */
1655 if (! astr)
1656 cmp = -(astr != bstr);
1657 else if (! bstr)
1658 cmp = astr != bstr;
1659 else
1661 cmp = strcmp(astr, bstr);
1663 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1664 if (tag_a && tag_b)
1665 if (!sort_by_name ||
1666 (utils_str_equal(tag_a->name, tag_b->name) &&
1667 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1668 cmp = compare_symbol_lines(tag_a, tag_b);
1671 g_free(astr);
1672 g_free(bstr);
1674 tm_tag_unref(tag_a);
1675 tm_tag_unref(tag_b);
1677 return cmp;
1681 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1683 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1684 GINT_TO_POINTER(sort_by_name), NULL);
1686 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1690 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1692 GList *tags;
1694 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1696 tags = get_tag_list(doc, tm_tag_max_t);
1697 if (tags == NULL)
1698 return FALSE;
1700 /* FIXME: Not sure why we detached the model here? */
1702 /* disable sorting during update because the code doesn't support correctly
1703 * models that are currently being built */
1704 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1706 /* add grandparent type iters */
1707 add_top_level_items(doc);
1709 update_tree_tags(doc, &tags);
1710 g_list_free(tags);
1712 hide_empty_rows(doc->priv->tag_store);
1714 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1715 sort_mode = doc->priv->symbol_list_sort_mode;
1717 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1718 doc->priv->symbol_list_sort_mode = sort_mode;
1720 return TRUE;
1724 /* Detects a global tags filetype from the *.lang.* language extension.
1725 * Returns NULL if there was no matching TM language. */
1726 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1728 gchar *tags_ext;
1729 gchar *shortname = utils_strdupa(utf8_filename);
1730 GeanyFiletype *ft = NULL;
1732 tags_ext = g_strrstr(shortname, ".tags");
1733 if (tags_ext)
1735 *tags_ext = '\0'; /* remove .tags extension */
1736 ft = filetypes_detect_from_extension(shortname);
1737 if (ft->id != GEANY_FILETYPES_NONE)
1738 return ft;
1740 return NULL;
1744 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1745 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1746 * the relevant path.
1747 * Example:
1748 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1749 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1751 /* -E pre-process, -dD output user macros, -p prof info (?) */
1752 const char pre_process[] = "gcc -E -dD -p -I.";
1754 if (argc > 2)
1756 /* Create global taglist */
1757 int status;
1758 char *command;
1759 const char *tags_file = argv[1];
1760 char *utf8_fname;
1761 GeanyFiletype *ft;
1763 utf8_fname = utils_get_utf8_from_locale(tags_file);
1764 ft = detect_global_tags_filetype(utf8_fname);
1765 g_free(utf8_fname);
1767 if (ft == NULL)
1769 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1770 return 1;
1772 /* load config in case of custom filetypes */
1773 filetypes_load_config(ft->id, FALSE);
1775 /* load ignore list for C/C++ parser */
1776 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1777 load_c_ignore_tags();
1779 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1781 const gchar *cflags = getenv("CFLAGS");
1782 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1784 else
1785 command = NULL; /* don't preprocess */
1787 geany_debug("Generating %s tags file.", ft->name);
1788 tm_get_workspace();
1789 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1790 argc - 2, tags_file, ft->lang);
1791 g_free(command);
1792 symbols_finalize(); /* free c_tags_ignore data */
1793 if (! status)
1795 g_printerr(_("Failed to create tags file, perhaps because no tags "
1796 "were found.\n"));
1797 return 1;
1800 else
1802 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1803 g_printerr(_("Example:\n"
1804 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1805 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1806 return 1;
1808 return 0;
1812 void symbols_show_load_tags_dialog(void)
1814 GtkWidget *dialog;
1815 GtkFileFilter *filter;
1817 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1818 GTK_FILE_CHOOSER_ACTION_OPEN,
1819 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1820 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1821 NULL);
1822 gtk_widget_set_name(dialog, "GeanyDialog");
1823 filter = gtk_file_filter_new();
1824 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1825 gtk_file_filter_add_pattern(filter, "*.*.tags");
1826 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1828 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1830 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1831 GSList *item;
1833 for (item = flist; item != NULL; item = g_slist_next(item))
1835 gchar *fname = item->data;
1836 gchar *utf8_fname;
1837 GeanyFiletype *ft;
1839 utf8_fname = utils_get_utf8_from_locale(fname);
1840 ft = detect_global_tags_filetype(utf8_fname);
1842 if (ft != NULL && symbols_load_global_tags(fname, ft))
1843 /* For translators: the first wildcard is the filetype, the second the filename */
1844 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1845 filetypes_get_display_name(ft), utf8_fname);
1846 else
1847 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1849 g_free(utf8_fname);
1850 g_free(fname);
1852 g_slist_free(flist);
1854 gtk_widget_destroy(dialog);
1858 static void init_user_tags(void)
1860 GSList *file_list = NULL, *list = NULL;
1861 const GSList *node;
1862 gchar *dir;
1864 dir = g_build_filename(app->configdir, "tags", NULL);
1865 /* create the user tags dir for next time if it doesn't exist */
1866 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1867 utils_mkdir(dir, FALSE);
1868 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1870 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1871 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1872 g_free(dir);
1874 file_list = g_slist_concat(file_list, list);
1876 /* populate the filetype-specific tag files lists */
1877 for (node = file_list; node != NULL; node = node->next)
1879 gchar *fname = node->data;
1880 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1881 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1883 g_free(utf8_fname);
1885 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1886 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1887 else
1889 geany_debug("Unknown filetype for file '%s'.", fname);
1890 g_free(fname);
1894 /* don't need to delete list contents because they are now stored in
1895 * ft->priv->tag_files */
1896 g_slist_free(file_list);
1900 static void load_user_tags(filetype_id ft_id)
1902 static guchar *tags_loaded = NULL;
1903 static gboolean init_tags = FALSE;
1904 const GSList *node;
1905 GeanyFiletype *ft = filetypes[ft_id];
1907 g_return_if_fail(ft_id > 0);
1909 if (!tags_loaded)
1910 tags_loaded = g_new0(guchar, filetypes_array->len);
1911 if (tags_loaded[ft_id])
1912 return;
1913 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1915 if (!init_tags)
1917 init_user_tags();
1918 init_tags = TRUE;
1921 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1923 const gchar *fname = node->data;
1925 symbols_load_global_tags(fname, ft);
1930 static gboolean goto_tag(const gchar *name, gboolean definition)
1932 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1933 guint type;
1934 TMTag *tmtag = NULL;
1935 GeanyDocument *old_doc = document_get_current();
1937 /* goto tag definition: all except prototypes / forward declarations / externs */
1938 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1940 /* first look in the current document */
1941 if (old_doc != NULL && old_doc->tm_file)
1942 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1944 /* if not found, look in the workspace */
1945 if (tmtag == NULL)
1946 tmtag = find_workspace_tag(name, type);
1948 if (tmtag != NULL)
1950 GeanyDocument *new_doc = document_find_by_real_path(
1951 tmtag->atts.entry.file->work_object.file_name);
1953 if (new_doc)
1955 /* If we are already on the tag line, swap definition/declaration */
1956 if (new_doc == old_doc &&
1957 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1959 if (goto_tag(name, !definition))
1960 return TRUE;
1963 else
1965 /* not found in opened document, should open */
1966 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1969 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1970 return TRUE;
1972 return FALSE;
1976 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1978 if (goto_tag(name, definition))
1979 return TRUE;
1981 /* if we are here, there was no match and we are beeping ;-) */
1982 utils_beep();
1984 if (!definition)
1985 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1986 else
1987 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1988 return FALSE;
1992 /* This could perhaps be improved to check for #if, class etc. */
1993 static gint get_function_fold_number(GeanyDocument *doc)
1995 /* for Java the functions are always one fold level above the class scope */
1996 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1997 return SC_FOLDLEVELBASE + 1;
1998 else
1999 return SC_FOLDLEVELBASE;
2003 /* Should be used only with get_current_tag_cached.
2004 * tag_types caching might trigger recomputation too often but this isn't used differently often
2005 * enough to be an issue for now */
2006 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2008 static gint old_line = -2;
2009 static GeanyDocument *old_doc = NULL;
2010 static gint old_fold_num = -1;
2011 static guint old_tag_types = 0;
2012 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2013 gboolean ret;
2015 /* check if the cached line and file index have changed since last time: */
2016 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2017 ret = TRUE;
2018 else if (cur_line == old_line)
2019 ret = FALSE;
2020 else
2022 /* if the line has only changed by 1 */
2023 if (abs(cur_line - old_line) == 1)
2025 /* It's the same function if the fold number hasn't changed */
2026 ret = (fold_num != old_fold_num);
2028 else ret = TRUE;
2031 /* record current line and file index for next time */
2032 old_line = cur_line;
2033 old_doc = doc;
2034 old_fold_num = fold_num;
2035 old_tag_types = tag_types;
2036 return ret;
2040 /* Parse the function name up to 2 lines before tag_line.
2041 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2042 * type or argument names can be confused with the function name. */
2043 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2045 gint start, end, max_pos;
2046 gint fn_style;
2048 switch (sci_get_lexer(sci))
2050 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2051 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2052 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2054 start = sci_get_position_from_line(sci, tag_line - 2);
2055 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2056 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2057 start++;
2059 end = start;
2060 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2061 end++;
2063 if (start == end)
2064 return NULL;
2065 return sci_get_contents_range(sci, start, end);
2069 /* Parse the function name */
2070 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2072 gint start, end, first_pos, max_pos;
2073 gint tmp;
2074 gchar c;
2076 first_pos = end = sci_get_position_from_line(sci, tag_line);
2077 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2078 tmp = 0;
2079 /* goto the begin of function body */
2080 while (end < max_pos &&
2081 (tmp = sci_get_char_at(sci, end)) != '{' &&
2082 tmp != 0) end++;
2083 if (tmp == 0) end --;
2085 /* go back to the end of function identifier */
2086 while (end > 0 && end > first_pos - 500 &&
2087 (tmp = sci_get_char_at(sci, end)) != '(' &&
2088 tmp != 0) end--;
2089 end--;
2090 if (end < 0) end = 0;
2092 /* skip whitespaces between identifier and ( */
2093 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2095 start = end;
2096 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2097 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2098 || tmp == SCE_C_GLOBALCLASS
2099 || (c = sci_get_char_at(sci, start)) == '~'
2100 || c == ':'))
2101 start--;
2102 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2104 if (start == end) return NULL;
2105 return sci_get_contents_range(sci, start, end + 1);
2109 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2111 gint line_count = sci_get_line_count(sci);
2113 for (; line < line_count; line++)
2115 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2116 return line;
2119 return -1;
2123 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2125 gint line;
2126 gint parent;
2128 line = sci_get_current_line(doc->editor->sci);
2129 parent = sci_get_fold_parent(doc->editor->sci, line);
2130 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2131 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2132 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2134 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2136 if (tag)
2138 gint tag_line = tag->atts.entry.line - 1;
2139 gint last_child = line + 1;
2141 /* if it may be a false positive because we're inside a fold level not inside anything
2142 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2143 * right after the tag we got from TM */
2144 if (abs(tag_line - parent) > 1)
2146 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2147 if (tag_fold >= 0)
2148 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2151 if (line <= last_child)
2153 if (tag->atts.entry.scope)
2154 *tagname = g_strconcat(tag->atts.entry.scope,
2155 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2156 else
2157 *tagname = g_strdup(tag->name);
2159 return tag_line;
2163 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2164 * to dirty and inaccurate hand-parsing */
2165 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2167 const gint fn_fold = get_function_fold_number(doc);
2168 gint tag_line = parent;
2169 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2171 /* find the top level fold point */
2172 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2174 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2175 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2178 if (tag_line >= 0)
2180 gchar *cur_tag;
2182 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2183 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2184 else
2185 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2187 if (cur_tag != NULL)
2189 *tagname = cur_tag;
2190 return tag_line;
2195 *tagname = g_strdup(_("unknown"));
2196 return -1;
2200 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2202 static gint tag_line = -1;
2203 static gchar *cur_tag = NULL;
2205 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2207 if (doc == NULL) /* reset current function */
2209 current_tag_changed(NULL, -1, -1, 0);
2210 g_free(cur_tag);
2211 cur_tag = g_strdup(_("unknown"));
2212 if (tagname != NULL)
2213 *tagname = cur_tag;
2214 tag_line = -1;
2216 else
2218 gint line = sci_get_current_line(doc->editor->sci);
2219 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2221 if (current_tag_changed(doc, line, fold_level, tag_types))
2223 g_free(cur_tag);
2224 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2226 *tagname = cur_tag;
2229 return tag_line;
2233 /* Sets *tagname to point at the current function or tag name.
2234 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2235 * call to this function.
2236 * Returns: line number of the current tag, or -1 if unknown. */
2237 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2239 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2243 /* same as symbols_get_current_function() but finds class, namespaces and more */
2244 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2246 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2247 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2249 /* Python parser reports imports as namespaces which confuses the scope detection */
2250 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2251 tag_types |= tm_tag_namespace_t;
2253 return get_current_tag_name_cached(doc, tagname, tag_types);
2257 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2259 gint sort_mode = GPOINTER_TO_INT(user_data);
2260 GeanyDocument *doc = document_get_current();
2262 if (ignore_callback)
2263 return;
2265 if (doc != NULL)
2266 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2270 static void on_symbol_tree_menu_show(GtkWidget *widget,
2271 gpointer user_data)
2273 GeanyDocument *doc = document_get_current();
2274 gboolean enable;
2276 enable = doc && doc->has_tags;
2277 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2278 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2279 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2280 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2281 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2282 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2284 if (! doc)
2285 return;
2287 ignore_callback = TRUE;
2289 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2290 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2291 else
2292 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2294 ignore_callback = FALSE;
2298 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2300 gboolean expand = GPOINTER_TO_INT(user_data);
2301 GeanyDocument *doc = document_get_current();
2303 if (! doc)
2304 return;
2306 g_return_if_fail(doc->priv->tag_tree);
2308 if (expand)
2309 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2310 else
2311 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2315 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2317 GtkTreeIter iter;
2318 GtkTreeSelection *selection;
2319 GtkTreeModel *model;
2320 GeanyDocument *doc;
2321 TMTag *tag = NULL;
2323 doc = document_get_current();
2324 if (!doc)
2325 return;
2327 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2328 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2329 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2330 if (tag)
2332 if (widget == symbol_menu.find_in_files)
2333 search_show_find_in_files_dialog_full(tag->name, NULL);
2334 else
2335 search_find_usage(tag->name, tag->name, SCFIND_WHOLEWORD | SCFIND_MATCHCASE,
2336 widget == symbol_menu.find_usage);
2338 tm_tag_unref(tag);
2343 static void create_taglist_popup_menu(void)
2345 GtkWidget *item, *menu;
2347 tv.popup_taglist = menu = gtk_menu_new();
2349 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2350 gtk_widget_show(item);
2351 gtk_container_add(GTK_CONTAINER(menu), item);
2352 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2354 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2355 gtk_widget_show(item);
2356 gtk_container_add(GTK_CONTAINER(menu), item);
2357 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2359 item = gtk_separator_menu_item_new();
2360 gtk_widget_show(item);
2361 gtk_container_add(GTK_CONTAINER(menu), item);
2363 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2364 _("Sort by _Name"));
2365 gtk_widget_show(item);
2366 gtk_container_add(GTK_CONTAINER(menu), item);
2367 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2368 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2370 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2371 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2372 gtk_widget_show(item);
2373 gtk_container_add(GTK_CONTAINER(menu), item);
2374 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2375 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2377 item = gtk_separator_menu_item_new();
2378 gtk_widget_show(item);
2379 gtk_container_add(GTK_CONTAINER(menu), item);
2381 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2382 gtk_widget_show(item);
2383 gtk_container_add(GTK_CONTAINER(menu), item);
2384 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2386 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2387 gtk_widget_show(item);
2388 gtk_container_add(GTK_CONTAINER(menu), item);
2389 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2391 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2392 gtk_widget_show(item);
2393 gtk_container_add(GTK_CONTAINER(menu), item);
2394 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2396 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2398 sidebar_add_common_menu_items(GTK_MENU(menu));
2402 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2404 gchar *f;
2406 g_return_if_fail(!EMPTY(doc->real_path));
2408 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2409 if (utils_str_equal(doc->real_path, f))
2410 load_c_ignore_tags();
2412 g_free(f);
2416 void symbols_init(void)
2418 gchar *f;
2420 create_taglist_popup_menu();
2422 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2423 ui_add_config_file_menu_item(f, NULL, NULL);
2424 g_free(f);
2426 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2430 void symbols_finalize(void)
2432 g_strfreev(html_entities);
2433 g_strfreev(c_tags_ignore);