Rewrite PHP tag creation script using up to date upstream tag definitions
[geany-mirror.git] / src / symbols.c
blobe85a7f69008412f9c3d2c98de70d2f72754b69b9
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include "symbols.h"
41 #include "app.h"
42 #include "callbacks.h" /* FIXME: for ignore_callback */
43 #include "documentprivate.h"
44 #include "editor.h"
45 #include "encodings.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "sciwrappers.h"
51 #include "sidebar.h"
52 #include "support.h"
53 #include "tm_parser.h"
54 #include "tm_tag.h"
55 #include "ui_utils.h"
56 #include "utils.h"
58 #include "SciLexer.h"
60 #include "gtkcompat.h"
62 #include <ctype.h>
63 #include <string.h>
64 #include <stdlib.h>
67 static gchar **html_entities = NULL;
69 typedef struct
71 gboolean tags_loaded;
72 const gchar *tag_file;
73 } TagFileInfo;
75 /* Check before adding any more tags files, usually they should be downloaded separately. */
76 enum /* Geany tag files */
78 GTF_C,
79 GTF_PASCAL,
80 GTF_PHP,
81 GTF_HTML_ENTITIES,
82 GTF_LATEX,
83 GTF_PYTHON,
84 GTF_MAX
87 static TagFileInfo tag_file_info[GTF_MAX] =
89 {FALSE, "c99.tags"},
90 {FALSE, "pascal.tags"},
91 {FALSE, "php.tags"},
92 {FALSE, "html_entities.tags"},
93 {FALSE, "latex.tags"},
94 {FALSE, "python.tags"}
97 static GPtrArray *top_level_iter_names = NULL;
99 enum
101 ICON_CLASS,
102 ICON_MACRO,
103 ICON_MEMBER,
104 ICON_METHOD,
105 ICON_NAMESPACE,
106 ICON_OTHER,
107 ICON_STRUCT,
108 ICON_VAR,
109 ICON_NONE,
110 N_ICONS = ICON_NONE
113 static struct
115 const gchar *icon_name;
116 GdkPixbuf *pixbuf;
118 symbols_icons[N_ICONS] = {
119 [ICON_CLASS] = { "classviewer-class", NULL },
120 [ICON_MACRO] = { "classviewer-macro", NULL },
121 [ICON_MEMBER] = { "classviewer-member", NULL },
122 [ICON_METHOD] = { "classviewer-method", NULL },
123 [ICON_NAMESPACE] = { "classviewer-namespace", NULL },
124 [ICON_OTHER] = { "classviewer-other", NULL },
125 [ICON_STRUCT] = { "classviewer-struct", NULL },
126 [ICON_VAR] = { "classviewer-var", NULL },
129 static struct
131 GtkWidget *expand_all;
132 GtkWidget *collapse_all;
133 GtkWidget *sort_by_name;
134 GtkWidget *sort_by_appearance;
135 GtkWidget *find_usage;
136 GtkWidget *find_doc_usage;
137 GtkWidget *find_in_files;
139 symbol_menu;
141 static void html_tags_loaded(void);
142 static void load_user_tags(filetype_id ft_id);
144 /* get the tags_ignore list, exported by tagmanager's options.c */
145 extern gchar **c_tags_ignore;
147 /* ignore certain tokens when parsing C-like syntax.
148 * Also works for reloading. */
149 static void load_c_ignore_tags(void)
151 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
152 gchar *content;
154 if (g_file_get_contents(path, &content, NULL, NULL))
156 /* historically we ignore the glib _DECLS for tag generation */
157 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
159 g_strfreev(c_tags_ignore);
160 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
161 g_free(content);
163 g_free(path);
167 void symbols_reload_config_files(void)
169 load_c_ignore_tags();
173 static gsize get_tag_count(void)
175 GPtrArray *tags = tm_get_workspace()->global_tags;
176 gsize count = tags ? tags->len : 0;
178 return count;
182 /* wrapper for tm_workspace_load_global_tags().
183 * note that the tag count only counts new global tags added - if a tag has the same name,
184 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
185 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
187 gboolean result;
188 gsize old_tag_count = get_tag_count();
190 result = tm_workspace_load_global_tags(tags_file, ft->lang);
191 if (result)
193 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
194 (guint) (get_tag_count() - old_tag_count));
196 return result;
200 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
201 * This provides autocompletion, calltips, etc. */
202 void symbols_global_tags_loaded(guint file_type_idx)
204 TagFileInfo *tfi;
205 gint tag_type;
207 /* load ignore list for C/C++ parser */
208 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
209 c_tags_ignore == NULL)
211 load_c_ignore_tags();
214 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
215 return;
217 /* load config in case of custom filetypes */
218 filetypes_load_config(file_type_idx, FALSE);
220 load_user_tags(file_type_idx);
222 switch (file_type_idx)
224 case GEANY_FILETYPES_PHP:
225 case GEANY_FILETYPES_HTML:
226 html_tags_loaded();
228 switch (file_type_idx)
230 case GEANY_FILETYPES_CPP:
231 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
232 /* no C++ tagfile yet */
233 return;
234 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
235 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
236 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
237 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
238 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
239 default:
240 return;
242 tfi = &tag_file_info[tag_type];
244 if (! tfi->tags_loaded)
246 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
248 symbols_load_global_tags(fname, filetypes[file_type_idx]);
249 tfi->tags_loaded = TRUE;
250 g_free(fname);
255 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
256 static void html_tags_loaded(void)
258 TagFileInfo *tfi;
260 if (cl_options.ignore_global_tags)
261 return;
263 tfi = &tag_file_info[GTF_HTML_ENTITIES];
264 if (! tfi->tags_loaded)
266 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
268 html_entities = utils_read_file_in_array(file);
269 tfi->tags_loaded = TRUE;
270 g_free(file);
275 GString *symbols_find_typenames_as_string(gint lang, gboolean global)
277 guint j;
278 TMTag *tag;
279 GString *s = NULL;
280 GPtrArray *typedefs;
281 gint tag_lang;
283 if (global)
284 typedefs = tm_tags_extract(app->tm_workspace->global_tags, TM_GLOBAL_TYPE_MASK);
285 else
286 typedefs = app->tm_workspace->typename_array;
288 if ((typedefs) && (typedefs->len > 0))
290 s = g_string_sized_new(typedefs->len * 10);
291 for (j = 0; j < typedefs->len; ++j)
293 tag = TM_TAG(typedefs->pdata[j]);
294 tag_lang = tag->lang;
296 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
297 * e.g. PHP classes in C++ files
298 * lang = -2 disables the check */
299 if (tag->name && (tag_lang == lang || lang == -2 ||
300 (lang == TM_PARSER_CPP && tag_lang == TM_PARSER_C)))
302 if (j != 0)
303 g_string_append_c(s, ' ');
304 g_string_append(s, tag->name);
308 if (typedefs && global)
309 g_ptr_array_free(typedefs, TRUE);
310 return s;
314 /** Gets the context separator used by the tag manager for a particular file
315 * type.
316 * @param ft_id File type identifier.
317 * @return The context separator string.
319 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
320 * without a context separator.
322 * @since 0.19
324 GEANY_API_SYMBOL
325 const gchar *symbols_get_context_separator(gint ft_id)
327 switch (ft_id)
329 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
330 case GEANY_FILETYPES_CPP:
331 case GEANY_FILETYPES_GLSL: /* for structs */
332 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
333 case GEANY_FILETYPES_PHP:
334 case GEANY_FILETYPES_POWERSHELL:
335 case GEANY_FILETYPES_RUST:
336 case GEANY_FILETYPES_ZEPHIR:
337 return "::";
339 /* avoid confusion with other possible separators in group/section name */
340 case GEANY_FILETYPES_CONF:
341 case GEANY_FILETYPES_REST:
342 return ":::";
344 /* no context separator */
345 case GEANY_FILETYPES_ASCIIDOC:
346 case GEANY_FILETYPES_TXT2TAGS:
347 return "\x03";
349 default:
350 return ".";
355 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
356 static TMTag *
357 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
359 guint i;
360 g_return_val_if_fail(tags != NULL, NULL);
362 for (i = 0; i < tags->len; ++i)
364 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
365 return TM_TAG(tags->pdata[i]);
367 return NULL;
371 static TMTag *find_source_file_tag(GPtrArray *tags_array,
372 const gchar *tag_name, guint type)
374 GPtrArray *tags;
375 TMTag *tmtag;
377 tags = tm_tags_extract(tags_array, type);
378 if (tags != NULL)
380 tmtag = symbols_find_tm_tag(tags, tag_name);
382 g_ptr_array_free(tags, TRUE);
384 if (tmtag != NULL)
385 return tmtag;
387 return NULL; /* not found */
391 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
393 guint j;
394 const GPtrArray *source_files = NULL;
396 if (app->tm_workspace != NULL)
397 source_files = app->tm_workspace->source_files;
399 if (source_files != NULL)
401 for (j = 0; j < source_files->len; j++)
403 TMSourceFile *srcfile = source_files->pdata[j];
404 TMTag *tmtag;
406 tmtag = find_source_file_tag(srcfile->tags_array, tag_name, type);
407 if (tmtag != NULL)
408 return tmtag;
411 return NULL; /* not found */
415 const gchar **symbols_get_html_entities(void)
417 if (html_entities == NULL)
418 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
420 return (const gchar **) html_entities;
424 /* sort by name, then line */
425 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
427 gint ret;
429 if (tag_a == NULL || tag_b == NULL)
430 return 0;
432 if (tag_a->name == NULL)
433 return -(tag_a->name != tag_b->name);
435 if (tag_b->name == NULL)
436 return tag_a->name != tag_b->name;
438 ret = strcmp(tag_a->name, tag_b->name);
439 if (ret == 0)
441 return tag_a->line - tag_b->line;
443 return ret;
447 /* sort by line, then scope */
448 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
450 const TMTag *tag_a = TM_TAG(a);
451 const TMTag *tag_b = TM_TAG(b);
452 gint ret;
454 if (a == NULL || b == NULL)
455 return 0;
457 ret = tag_a->line - tag_b->line;
458 if (ret == 0)
460 if (tag_a->scope == NULL)
461 return -(tag_a->scope != tag_b->scope);
462 if (tag_b->scope == NULL)
463 return tag_a->scope != tag_b->scope;
464 else
465 return strcmp(tag_a->scope, tag_b->scope);
467 return ret;
471 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
473 GList *tag_names = NULL;
474 TMTag *tag;
475 guint i;
477 g_return_val_if_fail(doc, NULL);
479 if (! doc->tm_file || ! doc->tm_file->tags_array)
480 return NULL;
482 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
484 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
485 if (G_UNLIKELY(tag == NULL))
486 return NULL;
488 if (tag->type & tag_types)
490 tag_names = g_list_prepend(tag_names, tag);
493 tag_names = g_list_sort(tag_names, compare_symbol_lines);
494 return tag_names;
498 /* amount of types in the symbol list (currently max. 8 are used) */
499 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
501 struct TreeviewSymbols
503 GtkTreeIter tag_function;
504 GtkTreeIter tag_class;
505 GtkTreeIter tag_macro;
506 GtkTreeIter tag_member;
507 GtkTreeIter tag_variable;
508 GtkTreeIter tag_externvar;
509 GtkTreeIter tag_namespace;
510 GtkTreeIter tag_struct;
511 GtkTreeIter tag_interface;
512 GtkTreeIter tag_type;
513 GtkTreeIter tag_other;
514 } tv_iters;
517 static void init_tag_iters(void)
519 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
520 * filetypes(e.g. config file to Python crashes Geany without this) */
521 tv_iters.tag_function.stamp = -1;
522 tv_iters.tag_class.stamp = -1;
523 tv_iters.tag_member.stamp = -1;
524 tv_iters.tag_macro.stamp = -1;
525 tv_iters.tag_variable.stamp = -1;
526 tv_iters.tag_externvar.stamp = -1;
527 tv_iters.tag_namespace.stamp = -1;
528 tv_iters.tag_struct.stamp = -1;
529 tv_iters.tag_interface.stamp = -1;
530 tv_iters.tag_type.stamp = -1;
531 tv_iters.tag_other.stamp = -1;
535 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
537 static GtkIconTheme *icon_theme = NULL;
538 static gint x = -1;
540 if (G_UNLIKELY(x < 0))
542 gint dummy;
543 icon_theme = gtk_icon_theme_get_default();
544 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
546 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
550 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
552 GtkTreeModel *model = GTK_TREE_MODEL(store);
554 if (!gtk_tree_model_get_iter_first(model, iter))
555 return FALSE;
558 gchar *candidate;
560 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
561 /* FIXME: what if 2 different items have the same name?
562 * this should never happen, but might be caused by a typo in a translation */
563 if (utils_str_equal(candidate, title))
565 g_free(candidate);
566 return TRUE;
568 else
569 g_free(candidate);
571 while (gtk_tree_model_iter_next(model, iter));
573 return FALSE;
577 /* Adds symbol list groups in (iter*, title) pairs.
578 * The list must be ended with NULL. */
579 static void G_GNUC_NULL_TERMINATED
580 tag_list_add_groups(GtkTreeStore *tree_store, ...)
582 va_list args;
583 GtkTreeIter *iter;
585 g_return_if_fail(top_level_iter_names);
587 va_start(args, tree_store);
588 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
590 gchar *title = va_arg(args, gchar*);
591 guint icon_id = va_arg(args, guint);
592 GdkPixbuf *icon = NULL;
594 if (icon_id < N_ICONS)
595 icon = symbols_icons[icon_id].pixbuf;
597 g_assert(title != NULL);
598 g_ptr_array_add(top_level_iter_names, title);
600 if (!find_toplevel_iter(tree_store, iter, title))
601 gtk_tree_store_append(tree_store, iter, NULL);
603 if (icon)
604 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
605 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
607 va_end(args);
611 static void add_top_level_items(GeanyDocument *doc)
613 filetype_id ft_id = doc->file_type->id;
614 GtkTreeStore *tag_store = doc->priv->tag_store;
616 if (top_level_iter_names == NULL)
617 top_level_iter_names = g_ptr_array_new();
618 else
619 g_ptr_array_set_size(top_level_iter_names, 0);
621 init_tag_iters();
623 switch (ft_id)
625 case GEANY_FILETYPES_DIFF:
627 tag_list_add_groups(tag_store,
628 &(tv_iters.tag_function), _("Files"), ICON_NONE, NULL);
629 break;
631 case GEANY_FILETYPES_DOCBOOK:
633 tag_list_add_groups(tag_store,
634 &(tv_iters.tag_function), _("Chapter"), ICON_NONE,
635 &(tv_iters.tag_class), _("Section"), ICON_NONE,
636 &(tv_iters.tag_member), _("Sect1"), ICON_NONE,
637 &(tv_iters.tag_macro), _("Sect2"), ICON_NONE,
638 &(tv_iters.tag_variable), _("Sect3"), ICON_NONE,
639 &(tv_iters.tag_struct), _("Appendix"), ICON_NONE,
640 &(tv_iters.tag_other), _("Other"), ICON_NONE,
641 NULL);
642 break;
644 case GEANY_FILETYPES_HASKELL:
645 tag_list_add_groups(tag_store,
646 &tv_iters.tag_namespace, _("Module"), ICON_NONE,
647 &tv_iters.tag_type, _("Types"), ICON_NONE,
648 &tv_iters.tag_macro, _("Type constructors"), ICON_NONE,
649 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
650 NULL);
651 break;
652 case GEANY_FILETYPES_COBOL:
653 tag_list_add_groups(tag_store,
654 &tv_iters.tag_class, _("Program"), ICON_CLASS,
655 &tv_iters.tag_function, _("File"), ICON_METHOD,
656 &tv_iters.tag_namespace, _("Sections"), ICON_NAMESPACE,
657 &tv_iters.tag_macro, _("Paragraph"), ICON_OTHER,
658 &tv_iters.tag_struct, _("Group"), ICON_STRUCT,
659 &tv_iters.tag_variable, _("Data"), ICON_VAR,
660 NULL);
661 break;
662 case GEANY_FILETYPES_CONF:
663 tag_list_add_groups(tag_store,
664 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
665 &tv_iters.tag_macro, _("Keys"), ICON_VAR,
666 NULL);
667 break;
668 case GEANY_FILETYPES_NSIS:
669 tag_list_add_groups(tag_store,
670 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
671 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
672 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
673 NULL);
674 break;
675 case GEANY_FILETYPES_LATEX:
677 tag_list_add_groups(tag_store,
678 &(tv_iters.tag_function), _("Command"), ICON_NONE,
679 &(tv_iters.tag_class), _("Environment"), ICON_NONE,
680 &(tv_iters.tag_member), _("Section"), ICON_NONE,
681 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
682 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
683 &(tv_iters.tag_struct), _("Label"), ICON_NONE,
684 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
685 &(tv_iters.tag_other), _("Other"), ICON_NONE,
686 NULL);
687 break;
689 case GEANY_FILETYPES_MATLAB:
691 tag_list_add_groups(tag_store,
692 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
693 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
694 NULL);
695 break;
697 case GEANY_FILETYPES_ABAQUS:
699 tag_list_add_groups(tag_store,
700 &(tv_iters.tag_class), _("Parts"), ICON_NONE,
701 &(tv_iters.tag_member), _("Assembly"), ICON_NONE,
702 &(tv_iters.tag_namespace), _("Steps"), ICON_NONE,
703 NULL);
704 break;
706 case GEANY_FILETYPES_R:
708 tag_list_add_groups(tag_store,
709 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
710 &(tv_iters.tag_other), _("Other"), ICON_NONE,
711 NULL);
712 break;
714 case GEANY_FILETYPES_RUST:
716 tag_list_add_groups(tag_store,
717 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
718 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
719 &(tv_iters.tag_interface), _("Traits"), ICON_CLASS,
720 &(tv_iters.tag_class), _("Implementations"), ICON_CLASS,
721 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
722 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
723 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
724 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO,
725 &(tv_iters.tag_member), _("Methods"), ICON_MEMBER,
726 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
727 NULL);
728 break;
730 case GEANY_FILETYPES_GO:
732 tag_list_add_groups(tag_store,
733 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
734 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
735 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
736 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
737 &(tv_iters.tag_type), _("Types"), ICON_STRUCT,
738 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
739 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
740 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
741 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
742 NULL);
743 break;
745 case GEANY_FILETYPES_PERL:
747 tag_list_add_groups(tag_store,
748 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
749 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
750 &(tv_iters.tag_macro), _("Labels"), ICON_NONE,
751 &(tv_iters.tag_type), _("Constants"), ICON_NONE,
752 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
753 NULL);
754 break;
756 case GEANY_FILETYPES_PHP:
757 case GEANY_FILETYPES_ZEPHIR:
759 tag_list_add_groups(tag_store,
760 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
761 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
762 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
763 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
764 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
765 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
766 &(tv_iters.tag_struct), _("Traits"), ICON_STRUCT,
767 NULL);
768 break;
770 case GEANY_FILETYPES_HTML:
772 tag_list_add_groups(tag_store,
773 &(tv_iters.tag_function), _("Functions"), ICON_NONE,
774 &(tv_iters.tag_member), _("Anchors"), ICON_NONE,
775 &(tv_iters.tag_namespace), _("H1 Headings"), ICON_NONE,
776 &(tv_iters.tag_class), _("H2 Headings"), ICON_NONE,
777 &(tv_iters.tag_variable), _("H3 Headings"), ICON_NONE,
778 NULL);
779 break;
781 case GEANY_FILETYPES_CSS:
783 tag_list_add_groups(tag_store,
784 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
785 &(tv_iters.tag_variable), _("ID Selectors"), ICON_VAR,
786 &(tv_iters.tag_struct), _("Type Selectors"), ICON_STRUCT, NULL);
787 break;
789 case GEANY_FILETYPES_REST:
790 case GEANY_FILETYPES_TXT2TAGS:
791 case GEANY_FILETYPES_ABC:
793 tag_list_add_groups(tag_store,
794 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
795 &(tv_iters.tag_member), _("Section"), ICON_NONE,
796 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
797 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
798 NULL);
799 break;
801 case GEANY_FILETYPES_ASCIIDOC:
803 tag_list_add_groups(tag_store,
804 &(tv_iters.tag_namespace), _("Document"), ICON_NONE,
805 &(tv_iters.tag_member), _("Section Level 1"), ICON_NONE,
806 &(tv_iters.tag_macro), _("Section Level 2"), ICON_NONE,
807 &(tv_iters.tag_variable), _("Section Level 3"), ICON_NONE,
808 &(tv_iters.tag_struct), _("Section Level 4"), ICON_NONE,
809 NULL);
810 break;
812 case GEANY_FILETYPES_RUBY:
814 tag_list_add_groups(tag_store,
815 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
816 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
817 &(tv_iters.tag_member), _("Singletons"), ICON_STRUCT,
818 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
819 NULL);
820 break;
822 case GEANY_FILETYPES_TCL:
824 tag_list_add_groups(tag_store,
825 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
826 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
827 &(tv_iters.tag_member), _("Methods"), ICON_METHOD,
828 &(tv_iters.tag_function), _("Procedures"), ICON_OTHER,
829 NULL);
830 break;
832 case GEANY_FILETYPES_PYTHON:
834 tag_list_add_groups(tag_store,
835 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
836 &(tv_iters.tag_member), _("Methods"), ICON_MACRO,
837 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
838 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
839 &(tv_iters.tag_externvar), _("Imports"), ICON_NAMESPACE,
840 NULL);
841 break;
843 case GEANY_FILETYPES_VHDL:
845 tag_list_add_groups(tag_store,
846 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
847 &(tv_iters.tag_class), _("Entities"), ICON_CLASS,
848 &(tv_iters.tag_struct), _("Architectures"), ICON_STRUCT,
849 &(tv_iters.tag_type), _("Types"), ICON_OTHER,
850 &(tv_iters.tag_function), _("Functions / Procedures"), ICON_METHOD,
851 &(tv_iters.tag_variable), _("Variables / Signals"), ICON_VAR,
852 &(tv_iters.tag_member), _("Processes / Blocks / Components"), ICON_MEMBER,
853 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
854 NULL);
855 break;
857 case GEANY_FILETYPES_VERILOG:
859 tag_list_add_groups(tag_store,
860 &(tv_iters.tag_type), _("Events"), ICON_MACRO,
861 &(tv_iters.tag_class), _("Modules"), ICON_CLASS,
862 &(tv_iters.tag_function), _("Functions / Tasks"), ICON_METHOD,
863 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
864 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
865 NULL);
866 break;
868 case GEANY_FILETYPES_JAVA:
870 tag_list_add_groups(tag_store,
871 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
872 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
873 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
874 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
875 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
876 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
877 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
878 NULL);
879 break;
881 case GEANY_FILETYPES_AS:
883 tag_list_add_groups(tag_store,
884 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
885 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
886 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
887 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
888 &(tv_iters.tag_member), _("Properties"), ICON_MEMBER,
889 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
890 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
891 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
892 NULL);
893 break;
895 case GEANY_FILETYPES_HAXE:
897 tag_list_add_groups(tag_store,
898 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
899 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
900 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
901 &(tv_iters.tag_type), _("Types"), ICON_MACRO,
902 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
903 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
904 NULL);
905 break;
907 case GEANY_FILETYPES_BASIC:
909 tag_list_add_groups(tag_store,
910 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
911 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
912 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
913 &(tv_iters.tag_struct), _("Types"), ICON_NAMESPACE,
914 &(tv_iters.tag_namespace), _("Labels"), ICON_MEMBER,
915 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
916 NULL);
917 break;
919 case GEANY_FILETYPES_F77:
920 case GEANY_FILETYPES_FORTRAN:
922 tag_list_add_groups(tag_store,
923 &(tv_iters.tag_namespace), _("Module"), ICON_CLASS,
924 &(tv_iters.tag_struct), _("Programs"), ICON_CLASS,
925 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
926 &(tv_iters.tag_function), _("Functions / Subroutines"), ICON_METHOD,
927 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
928 &(tv_iters.tag_class), _("Types"), ICON_CLASS,
929 &(tv_iters.tag_member), _("Components"), ICON_MEMBER,
930 &(tv_iters.tag_macro), _("Blocks"), ICON_MEMBER,
931 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
932 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
933 NULL);
934 break;
936 case GEANY_FILETYPES_ASM:
938 tag_list_add_groups(tag_store,
939 &(tv_iters.tag_namespace), _("Labels"), ICON_NAMESPACE,
940 &(tv_iters.tag_function), _("Macros"), ICON_METHOD,
941 &(tv_iters.tag_macro), _("Defines"), ICON_MACRO,
942 &(tv_iters.tag_struct), _("Types"), ICON_STRUCT,
943 NULL);
944 break;
946 case GEANY_FILETYPES_MAKE:
947 tag_list_add_groups(tag_store,
948 &tv_iters.tag_function, _("Targets"), ICON_METHOD,
949 &tv_iters.tag_macro, _("Macros"), ICON_MACRO,
950 NULL);
951 break;
952 case GEANY_FILETYPES_SQL:
954 tag_list_add_groups(tag_store,
955 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
956 &(tv_iters.tag_namespace), _("Procedures"), ICON_NAMESPACE,
957 &(tv_iters.tag_struct), _("Indexes"), ICON_STRUCT,
958 &(tv_iters.tag_class), _("Tables"), ICON_CLASS,
959 &(tv_iters.tag_macro), _("Triggers"), ICON_MACRO,
960 &(tv_iters.tag_member), _("Views"), ICON_VAR,
961 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
962 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
963 NULL);
964 break;
966 case GEANY_FILETYPES_D:
967 default:
969 if (ft_id == GEANY_FILETYPES_D)
970 tag_list_add_groups(tag_store,
971 &(tv_iters.tag_namespace), _("Module"), ICON_NONE, NULL);
972 else
973 tag_list_add_groups(tag_store,
974 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE, NULL);
976 tag_list_add_groups(tag_store,
977 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
978 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
979 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
980 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
981 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
982 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
983 NULL);
985 if (ft_id != GEANY_FILETYPES_D)
987 tag_list_add_groups(tag_store,
988 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO, NULL);
990 tag_list_add_groups(tag_store,
991 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
992 &(tv_iters.tag_externvar), _("Extern Variables"), ICON_VAR,
993 &(tv_iters.tag_other), _("Other"), ICON_OTHER, NULL);
999 /* removes toplevel items that have no children */
1000 static void hide_empty_rows(GtkTreeStore *store)
1002 GtkTreeIter iter;
1003 gboolean cont = TRUE;
1005 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1006 return; /* stop when first iter is invalid, i.e. no elements */
1008 while (cont)
1010 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1011 cont = gtk_tree_store_remove(store, &iter);
1012 else
1013 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1018 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1020 gchar *utf8_name;
1021 const gchar *scope = tag->scope;
1022 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1023 gboolean doc_is_utf8 = FALSE;
1025 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1026 * for None at this point completely */
1027 if (utils_str_equal(doc->encoding, "UTF-8") ||
1028 utils_str_equal(doc->encoding, "None"))
1029 doc_is_utf8 = TRUE;
1030 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1031 * plugin might have called tm_source_file_update(), so check to be sure */
1032 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1034 if (! doc_is_utf8)
1035 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1036 -1, doc->encoding, TRUE);
1037 else
1038 utf8_name = tag->name;
1040 if (utf8_name == NULL)
1041 return NULL;
1043 if (! buffer)
1044 buffer = g_string_new(NULL);
1045 else
1046 g_string_truncate(buffer, 0);
1048 /* check first char of scope is a wordchar */
1049 if (!found_parent && scope &&
1050 strpbrk(scope, GEANY_WORDCHARS) == scope)
1052 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1054 g_string_append(buffer, scope);
1055 g_string_append(buffer, sep);
1057 g_string_append(buffer, utf8_name);
1059 if (! doc_is_utf8)
1060 g_free(utf8_name);
1062 g_string_append_printf(buffer, " [%lu]", tag->line);
1064 return buffer->str;
1068 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1070 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1072 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1073 * for None at this point completely */
1074 if (utf8_name != NULL &&
1075 ! utils_str_equal(doc->encoding, "UTF-8") &&
1076 ! utils_str_equal(doc->encoding, "None"))
1078 SETPTR(utf8_name,
1079 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1082 return utf8_name;
1086 /* find the last word in "foo::bar::blah", e.g. "blah" */
1087 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1089 const gchar *scope = tag->scope;
1090 const gchar *separator = symbols_get_context_separator(ft_id);
1091 const gchar *str, *ptr;
1093 if (!scope)
1094 return NULL;
1096 str = scope;
1098 while (1)
1100 ptr = strstr(str, separator);
1101 if (ptr)
1103 str = ptr + strlen(separator);
1105 else
1106 break;
1109 return !EMPTY(str) ? str : NULL;
1113 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type)
1115 GtkTreeIter *iter = NULL;
1117 switch (tag_type)
1119 case tm_tag_prototype_t:
1120 case tm_tag_method_t:
1121 case tm_tag_function_t:
1123 iter = &tv_iters.tag_function;
1124 break;
1126 case tm_tag_externvar_t:
1128 iter = &tv_iters.tag_externvar;
1129 break;
1131 case tm_tag_macro_t:
1132 case tm_tag_macro_with_arg_t:
1134 iter = &tv_iters.tag_macro;
1135 break;
1137 case tm_tag_class_t:
1139 iter = &tv_iters.tag_class;
1140 break;
1142 case tm_tag_member_t:
1143 case tm_tag_field_t:
1145 iter = &tv_iters.tag_member;
1146 break;
1148 case tm_tag_typedef_t:
1149 case tm_tag_enum_t:
1151 iter = &tv_iters.tag_type;
1152 break;
1154 case tm_tag_union_t:
1155 case tm_tag_struct_t:
1157 iter = &tv_iters.tag_struct;
1158 break;
1160 case tm_tag_interface_t:
1161 iter = &tv_iters.tag_interface;
1162 break;
1163 case tm_tag_variable_t:
1165 iter = &tv_iters.tag_variable;
1166 break;
1168 case tm_tag_namespace_t:
1169 case tm_tag_package_t:
1171 iter = &tv_iters.tag_namespace;
1172 break;
1174 default:
1176 iter = &tv_iters.tag_other;
1179 if (G_LIKELY(iter->stamp != -1))
1180 return iter;
1181 else
1182 return NULL;
1186 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1188 GdkPixbuf *icon = NULL;
1190 if (parent == &tv_iters.tag_other)
1192 return g_object_ref(symbols_icons[ICON_VAR].pixbuf);
1194 /* copy parent icon */
1195 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1196 SYMBOLS_COLUMN_ICON, &icon, -1);
1197 return icon;
1201 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1203 const TMTag *t1 = v1;
1204 const TMTag *t2 = v2;
1206 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1207 utils_str_equal(t1->scope, t2->scope) &&
1208 /* include arglist in match to support e.g. C++ overloading */
1209 utils_str_equal(t1->arglist, t2->arglist));
1213 /* inspired from g_str_hash() */
1214 static guint tag_hash(gconstpointer v)
1216 const TMTag *tag = v;
1217 const gchar *p;
1218 guint32 h = 5381;
1220 h = (h << 5) + h + tag->type;
1221 for (p = tag->name; *p != '\0'; p++)
1222 h = (h << 5) + h + *p;
1223 if (tag->scope)
1225 for (p = tag->scope; *p != '\0'; p++)
1226 h = (h << 5) + h + *p;
1228 /* for e.g. C++ overloading */
1229 if (tag->arglist)
1231 for (p = tag->arglist; *p != '\0'; p++)
1232 h = (h << 5) + h + *p;
1235 return h;
1239 /* like gtk_tree_view_expand_to_path() but with an iter */
1240 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1242 GtkTreeModel *model = gtk_tree_view_get_model(view);
1243 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1245 gtk_tree_view_expand_to_path(view, path);
1246 gtk_tree_path_free(path);
1250 /* like gtk_tree_store_remove() but finds the next iter at any level */
1251 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1253 GtkTreeIter parent;
1254 gboolean has_parent;
1255 gboolean cont;
1257 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1258 cont = gtk_tree_store_remove(store, iter);
1259 /* if there is no next at this level but there is a parent iter, continue from it */
1260 if (! cont && has_parent)
1262 *iter = parent;
1263 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1266 return cont;
1270 /* adds a new element in the parent table if it's key is known.
1271 * duplicates are kept */
1272 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1273 const GtkTreeIter *iter)
1275 GList **list;
1276 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1277 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1279 if (! list)
1281 list = g_slice_alloc(sizeof *list);
1282 *list = NULL;
1283 g_hash_table_insert(table, tag->name, list);
1285 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1290 static void free_iter_slice_list(gpointer data)
1292 GList **list = data;
1294 if (list)
1296 GList *node;
1297 foreach_list(node, *list)
1298 g_slice_free(GtkTreeIter, node->data);
1299 g_list_free(*list);
1300 g_slice_free1(sizeof *list, list);
1305 /* inserts a @data in @table on key @tag.
1306 * previous data is not overwritten if the key is duplicated, but rather the
1307 * two values are kept in a list
1309 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1310 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1312 GList *list = g_hash_table_lookup(table, tag);
1313 list = g_list_prepend(list, data);
1314 g_hash_table_insert(table, tag, list);
1318 /* looks up the entry in @table that better matches @tag.
1319 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1320 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1322 GList *data = NULL;
1323 GList *node = g_hash_table_lookup(table, tag);
1324 if (node)
1326 glong delta;
1327 data = node->data;
1329 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1331 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1332 for (node = node->next; node; node = node->next)
1334 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1336 if (d < delta)
1338 data = node->data;
1339 delta = d;
1343 #undef TAG_DELTA
1346 return data;
1350 /* removes the element at @tag from @table.
1351 * @tag must be the exact pointer used at insertion time */
1352 static void tags_table_remove(GHashTable *table, TMTag *tag)
1354 GList *list = g_hash_table_lookup(table, tag);
1355 if (list)
1357 GList *node;
1358 foreach_list(node, list)
1360 if (((GList *) node->data)->data == tag)
1361 break;
1363 list = g_list_delete_link(list, node);
1364 if (list)
1365 g_hash_table_insert(table, tag, list);
1366 else
1367 g_hash_table_remove(table, tag);
1372 static void tags_table_destroy(GHashTable *table)
1374 /* free any leftover elements. note that we can't register a value_free_func when
1375 * creating the hash table because we only want to free it when destroying the table,
1376 * not when inserting a duplicate (we handle this manually) */
1377 GHashTableIter iter;
1378 gpointer value;
1380 g_hash_table_iter_init(&iter, table);
1381 while (g_hash_table_iter_next(&iter, NULL, &value))
1382 g_list_free(value);
1383 g_hash_table_destroy(table);
1388 * Updates the tag tree for a document with the tags in *list.
1389 * @param doc a document
1390 * @param tags a pointer to a GList* holding the tags to add/update. This
1391 * list may be updated, removing updated elements.
1393 * The update is done in two passes:
1394 * 1) walking the current tree, update tags that still exist and remove the
1395 * obsolescent ones;
1396 * 2) walking the remaining (non updated) tags, adds them in the list.
1398 * For better performances, we use 2 hash tables:
1399 * - one containing all the tags for lookup in the first pass (actually stores a
1400 * reference in the tags list for removing it efficiently), avoiding list search
1401 * on each tag;
1402 * - the other holding "tag-name":row references for tags having children, used to
1403 * lookup for a parent in both passes, avoiding tree traversal.
1405 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1407 GtkTreeStore *store = doc->priv->tag_store;
1408 GtkTreeModel *model = GTK_TREE_MODEL(store);
1409 GHashTable *parents_table;
1410 GHashTable *tags_table;
1411 GtkTreeIter iter;
1412 gboolean cont;
1413 GList *item;
1415 /* Build hash tables holding tags and parents */
1416 /* parent table holds "tag-name":GtkTreeIter */
1417 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1418 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1419 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1420 foreach_list(item, *tags)
1422 TMTag *tag = item->data;
1423 const gchar *name;
1425 tags_table_insert(tags_table, tag, item);
1427 name = get_parent_name(tag, doc->file_type->id);
1428 if (name)
1429 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1432 /* First pass, update existing rows or delete them.
1433 * It is OK to delete them since we walk top down so we would remove
1434 * parents before checking for their children, thus never implicitly
1435 * deleting an updated child */
1436 cont = gtk_tree_model_get_iter_first(model, &iter);
1437 while (cont)
1439 TMTag *tag;
1441 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1442 if (! tag) /* most probably a toplevel, skip it */
1443 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1444 else
1446 GList *found_item;
1448 found_item = tags_table_lookup(tags_table, tag);
1449 if (! found_item) /* tag doesn't exist, remove it */
1450 cont = tree_store_remove_row(store, &iter);
1451 else /* tag still exist, update it */
1453 const gchar *parent_name;
1454 TMTag *found = found_item->data;
1456 parent_name = get_parent_name(found, doc->file_type->id);
1457 /* if parent is unknown, ignore it */
1458 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1459 parent_name = NULL;
1461 if (!tm_tags_equal(tag, found))
1463 const gchar *name;
1464 gchar *tooltip;
1466 /* only update fields that (can) have changed (name that holds line
1467 * number, tooltip, and the tag itself) */
1468 name = get_symbol_name(doc, found, parent_name != NULL);
1469 tooltip = get_symbol_tooltip(doc, found);
1470 gtk_tree_store_set(store, &iter,
1471 SYMBOLS_COLUMN_NAME, name,
1472 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1473 SYMBOLS_COLUMN_TAG, found,
1474 -1);
1475 g_free(tooltip);
1478 update_parents_table(parents_table, found, parent_name, &iter);
1480 /* remove the updated tag from the table and list */
1481 tags_table_remove(tags_table, found);
1482 *tags = g_list_delete_link(*tags, found_item);
1484 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1487 tm_tag_unref(tag);
1491 /* Second pass, now we have a tree cleaned up from invalid rows,
1492 * we simply add new ones */
1493 foreach_list (item, *tags)
1495 TMTag *tag = item->data;
1496 GtkTreeIter *parent;
1498 parent = get_tag_type_iter(tag->type);
1499 if (G_UNLIKELY(! parent))
1500 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1501 else
1503 gboolean expand;
1504 const gchar *name;
1505 const gchar *parent_name;
1506 gchar *tooltip;
1507 GdkPixbuf *icon = get_child_icon(store, parent);
1509 parent_name = get_parent_name(tag, doc->file_type->id);
1510 if (parent_name)
1512 GList **candidates;
1513 GtkTreeIter *parent_search = NULL;
1515 /* walk parent candidates to find the better one.
1516 * if there are more than one, take the one that has the closest line number
1517 * after the tag we're searching the parent for */
1518 candidates = g_hash_table_lookup(parents_table, parent_name);
1519 if (candidates)
1521 GList *node;
1522 glong delta = G_MAXLONG;
1523 foreach_list(node, *candidates)
1525 TMTag *parent_tag;
1526 glong d;
1528 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1529 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1531 d = tag->line - parent_tag->line;
1532 if (! parent_search || (d >= 0 && d < delta))
1534 delta = d;
1535 parent_search = node->data;
1537 tm_tag_unref(parent_tag);
1541 if (parent_search)
1542 parent = parent_search;
1543 else
1544 parent_name = NULL;
1547 /* only expand to the iter if the parent was empty, otherwise we let the
1548 * folding as it was before (already expanded, or closed by the user) */
1549 expand = ! gtk_tree_model_iter_has_child(model, parent);
1551 /* insert the new element */
1552 name = get_symbol_name(doc, tag, parent_name != NULL);
1553 tooltip = get_symbol_tooltip(doc, tag);
1554 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1555 SYMBOLS_COLUMN_NAME, name,
1556 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1557 SYMBOLS_COLUMN_ICON, icon,
1558 SYMBOLS_COLUMN_TAG, tag,
1559 -1);
1560 g_free(tooltip);
1561 if (G_LIKELY(icon))
1562 g_object_unref(icon);
1564 update_parents_table(parents_table, tag, parent_name, &iter);
1566 if (expand)
1567 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1571 g_hash_table_destroy(parents_table);
1572 tags_table_destroy(tags_table);
1576 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1577 * is not stable, so the order is already lost. */
1578 static gint compare_top_level_names(const gchar *a, const gchar *b)
1580 guint i;
1581 const gchar *name;
1583 /* This should never happen as it would mean that two or more top
1584 * level items have the same name but it can happen by typos in the translations. */
1585 if (utils_str_equal(a, b))
1586 return 1;
1588 foreach_ptr_array(name, i, top_level_iter_names)
1590 if (utils_str_equal(name, a))
1591 return -1;
1592 if (utils_str_equal(name, b))
1593 return 1;
1595 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1596 return 0;
1600 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1601 GtkTreeIter *iter)
1603 /* if the tag has a parent tag, it should be at depth >= 2 */
1604 return !EMPTY(tag->scope) &&
1605 gtk_tree_store_iter_depth(store, iter) == 1;
1609 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1610 gpointer user_data)
1612 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1613 TMTag *tag_a, *tag_b;
1614 gint cmp;
1616 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1617 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1619 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1620 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1621 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1622 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1624 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1625 compare_symbol_lines(tag_a, tag_b);
1627 else
1629 gchar *astr, *bstr;
1631 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1632 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1634 /* if a is toplevel, b must be also */
1635 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1637 cmp = compare_top_level_names(astr, bstr);
1639 else
1641 /* this is what g_strcmp0() does */
1642 if (! astr)
1643 cmp = -(astr != bstr);
1644 else if (! bstr)
1645 cmp = astr != bstr;
1646 else
1648 cmp = strcmp(astr, bstr);
1650 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1651 if (tag_a && tag_b)
1652 if (!sort_by_name ||
1653 (utils_str_equal(tag_a->name, tag_b->name) &&
1654 utils_str_equal(tag_a->scope, tag_b->scope)))
1655 cmp = compare_symbol_lines(tag_a, tag_b);
1658 g_free(astr);
1659 g_free(bstr);
1661 tm_tag_unref(tag_a);
1662 tm_tag_unref(tag_b);
1664 return cmp;
1668 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1670 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1671 GINT_TO_POINTER(sort_by_name), NULL);
1673 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1677 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1679 GList *tags;
1681 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1683 tags = get_tag_list(doc, tm_tag_max_t);
1684 if (tags == NULL)
1685 return FALSE;
1687 /* FIXME: Not sure why we detached the model here? */
1689 /* disable sorting during update because the code doesn't support correctly
1690 * models that are currently being built */
1691 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1693 /* add grandparent type iters */
1694 add_top_level_items(doc);
1696 update_tree_tags(doc, &tags);
1697 g_list_free(tags);
1699 hide_empty_rows(doc->priv->tag_store);
1701 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1702 sort_mode = doc->priv->symbol_list_sort_mode;
1704 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1705 doc->priv->symbol_list_sort_mode = sort_mode;
1707 return TRUE;
1711 /* Detects a global tags filetype from the *.lang.* language extension.
1712 * Returns NULL if there was no matching TM language. */
1713 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1715 gchar *tags_ext;
1716 gchar *shortname = utils_strdupa(utf8_filename);
1717 GeanyFiletype *ft = NULL;
1719 tags_ext = g_strrstr(shortname, ".tags");
1720 if (tags_ext)
1722 *tags_ext = '\0'; /* remove .tags extension */
1723 ft = filetypes_detect_from_extension(shortname);
1724 if (ft->id != GEANY_FILETYPES_NONE)
1725 return ft;
1727 return NULL;
1731 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1732 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1733 * the relevant path.
1734 * Example:
1735 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1736 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1738 /* -E pre-process, -dD output user macros, -p prof info (?) */
1739 const char pre_process[] = "gcc -E -dD -p -I.";
1741 if (argc > 2)
1743 /* Create global taglist */
1744 int status;
1745 char *command;
1746 const char *tags_file = argv[1];
1747 char *utf8_fname;
1748 GeanyFiletype *ft;
1750 utf8_fname = utils_get_utf8_from_locale(tags_file);
1751 ft = detect_global_tags_filetype(utf8_fname);
1752 g_free(utf8_fname);
1754 if (ft == NULL)
1756 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1757 return 1;
1759 /* load config in case of custom filetypes */
1760 filetypes_load_config(ft->id, FALSE);
1762 /* load ignore list for C/C++ parser */
1763 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1764 load_c_ignore_tags();
1766 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1768 const gchar *cflags = getenv("CFLAGS");
1769 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1771 else
1772 command = NULL; /* don't preprocess */
1774 geany_debug("Generating %s tags file.", ft->name);
1775 tm_get_workspace();
1776 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1777 argc - 2, tags_file, ft->lang);
1778 g_free(command);
1779 symbols_finalize(); /* free c_tags_ignore data */
1780 if (! status)
1782 g_printerr(_("Failed to create tags file, perhaps because no tags "
1783 "were found.\n"));
1784 return 1;
1787 else
1789 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1790 g_printerr(_("Example:\n"
1791 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1792 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1793 return 1;
1795 return 0;
1799 void symbols_show_load_tags_dialog(void)
1801 GtkWidget *dialog;
1802 GtkFileFilter *filter;
1804 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1805 GTK_FILE_CHOOSER_ACTION_OPEN,
1806 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1807 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1808 NULL);
1809 gtk_widget_set_name(dialog, "GeanyDialog");
1810 filter = gtk_file_filter_new();
1811 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1812 gtk_file_filter_add_pattern(filter, "*.*.tags");
1813 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1815 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1817 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1818 GSList *item;
1820 for (item = flist; item != NULL; item = g_slist_next(item))
1822 gchar *fname = item->data;
1823 gchar *utf8_fname;
1824 GeanyFiletype *ft;
1826 utf8_fname = utils_get_utf8_from_locale(fname);
1827 ft = detect_global_tags_filetype(utf8_fname);
1829 if (ft != NULL && symbols_load_global_tags(fname, ft))
1830 /* For translators: the first wildcard is the filetype, the second the filename */
1831 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1832 filetypes_get_display_name(ft), utf8_fname);
1833 else
1834 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1836 g_free(utf8_fname);
1837 g_free(fname);
1839 g_slist_free(flist);
1841 gtk_widget_destroy(dialog);
1845 static void init_user_tags(void)
1847 GSList *file_list = NULL, *list = NULL;
1848 const GSList *node;
1849 gchar *dir;
1851 dir = g_build_filename(app->configdir, "tags", NULL);
1852 /* create the user tags dir for next time if it doesn't exist */
1853 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1854 utils_mkdir(dir, FALSE);
1855 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1857 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1858 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1859 g_free(dir);
1861 file_list = g_slist_concat(file_list, list);
1863 /* populate the filetype-specific tag files lists */
1864 for (node = file_list; node != NULL; node = node->next)
1866 gchar *fname = node->data;
1867 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1868 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1870 g_free(utf8_fname);
1872 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1873 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1874 else
1876 geany_debug("Unknown filetype for file '%s'.", fname);
1877 g_free(fname);
1881 /* don't need to delete list contents because they are now stored in
1882 * ft->priv->tag_files */
1883 g_slist_free(file_list);
1887 static void load_user_tags(filetype_id ft_id)
1889 static guchar *tags_loaded = NULL;
1890 static gboolean init_tags = FALSE;
1891 const GSList *node;
1892 GeanyFiletype *ft = filetypes[ft_id];
1894 g_return_if_fail(ft_id > 0);
1896 if (!tags_loaded)
1897 tags_loaded = g_new0(guchar, filetypes_array->len);
1898 if (tags_loaded[ft_id])
1899 return;
1900 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1902 if (!init_tags)
1904 init_user_tags();
1905 init_tags = TRUE;
1908 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1910 const gchar *fname = node->data;
1912 symbols_load_global_tags(fname, ft);
1917 static gboolean goto_tag(const gchar *name, gboolean definition)
1919 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1920 TMTagType type;
1921 TMTag *tmtag = NULL;
1922 GeanyDocument *old_doc = document_get_current();
1924 /* goto tag definition: all except prototypes / forward declarations / externs */
1925 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1927 /* first look in the current document */
1928 if (old_doc != NULL && old_doc->tm_file)
1929 tmtag = find_source_file_tag(old_doc->tm_file->tags_array, name, type);
1931 /* if not found, look in the workspace */
1932 if (tmtag == NULL)
1933 tmtag = find_workspace_tag(name, type);
1935 if (tmtag != NULL)
1937 GeanyDocument *new_doc = document_find_by_real_path(
1938 tmtag->file->file_name);
1940 if (new_doc)
1942 /* If we are already on the tag line, swap definition/declaration */
1943 if (new_doc == old_doc &&
1944 tmtag->line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1946 if (goto_tag(name, !definition))
1947 return TRUE;
1950 else
1952 /* not found in opened document, should open */
1953 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1956 if (navqueue_goto_line(old_doc, new_doc, tmtag->line))
1957 return TRUE;
1959 return FALSE;
1963 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1965 if (goto_tag(name, definition))
1966 return TRUE;
1968 /* if we are here, there was no match and we are beeping ;-) */
1969 utils_beep();
1971 if (!definition)
1972 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1973 else
1974 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1975 return FALSE;
1979 /* This could perhaps be improved to check for #if, class etc. */
1980 static gint get_function_fold_number(GeanyDocument *doc)
1982 /* for Java the functions are always one fold level above the class scope */
1983 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1984 return SC_FOLDLEVELBASE + 1;
1985 else
1986 return SC_FOLDLEVELBASE;
1990 /* Should be used only with get_current_tag_cached.
1991 * tag_types caching might trigger recomputation too often but this isn't used differently often
1992 * enough to be an issue for now */
1993 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1995 static gint old_line = -2;
1996 static GeanyDocument *old_doc = NULL;
1997 static gint old_fold_num = -1;
1998 static guint old_tag_types = 0;
1999 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2000 gboolean ret;
2002 /* check if the cached line and file index have changed since last time: */
2003 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2004 ret = TRUE;
2005 else if (cur_line == old_line)
2006 ret = FALSE;
2007 else
2009 /* if the line has only changed by 1 */
2010 if (abs(cur_line - old_line) == 1)
2012 /* It's the same function if the fold number hasn't changed */
2013 ret = (fold_num != old_fold_num);
2015 else ret = TRUE;
2018 /* record current line and file index for next time */
2019 old_line = cur_line;
2020 old_doc = doc;
2021 old_fold_num = fold_num;
2022 old_tag_types = tag_types;
2023 return ret;
2027 /* Parse the function name up to 2 lines before tag_line.
2028 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2029 * type or argument names can be confused with the function name. */
2030 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2032 gint start, end, max_pos;
2033 gint fn_style;
2035 switch (sci_get_lexer(sci))
2037 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2038 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2039 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2041 start = sci_get_position_from_line(sci, tag_line - 2);
2042 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2043 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2044 start++;
2046 end = start;
2047 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2048 end++;
2050 if (start == end)
2051 return NULL;
2052 return sci_get_contents_range(sci, start, end);
2056 /* Parse the function name */
2057 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2059 gint start, end, first_pos, max_pos;
2060 gint tmp;
2061 gchar c;
2063 first_pos = end = sci_get_position_from_line(sci, tag_line);
2064 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2065 tmp = 0;
2066 /* goto the begin of function body */
2067 while (end < max_pos &&
2068 (tmp = sci_get_char_at(sci, end)) != '{' &&
2069 tmp != 0) end++;
2070 if (tmp == 0) end --;
2072 /* go back to the end of function identifier */
2073 while (end > 0 && end > first_pos - 500 &&
2074 (tmp = sci_get_char_at(sci, end)) != '(' &&
2075 tmp != 0) end--;
2076 end--;
2077 if (end < 0) end = 0;
2079 /* skip whitespaces between identifier and ( */
2080 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2082 start = end;
2083 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2084 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2085 || tmp == SCE_C_GLOBALCLASS
2086 || (c = sci_get_char_at(sci, start)) == '~'
2087 || c == ':'))
2088 start--;
2089 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2091 if (start == end) return NULL;
2092 return sci_get_contents_range(sci, start, end + 1);
2096 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2098 gint line_count = sci_get_line_count(sci);
2100 for (; line < line_count; line++)
2102 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2103 return line;
2106 return -1;
2110 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2112 gint line;
2113 gint parent;
2115 line = sci_get_current_line(doc->editor->sci);
2116 parent = sci_get_fold_parent(doc->editor->sci, line);
2117 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2118 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2119 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2121 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2123 if (tag)
2125 gint tag_line = tag->line - 1;
2126 gint last_child = line + 1;
2128 /* if it may be a false positive because we're inside a fold level not inside anything
2129 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2130 * right after the tag we got from TM */
2131 if (abs(tag_line - parent) > 1)
2133 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2134 if (tag_fold >= 0)
2135 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2138 if (line <= last_child)
2140 if (tag->scope)
2141 *tagname = g_strconcat(tag->scope,
2142 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2143 else
2144 *tagname = g_strdup(tag->name);
2146 return tag_line;
2150 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2151 * to dirty and inaccurate hand-parsing */
2152 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2154 const gint fn_fold = get_function_fold_number(doc);
2155 gint tag_line = parent;
2156 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2158 /* find the top level fold point */
2159 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2161 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2162 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2165 if (tag_line >= 0)
2167 gchar *cur_tag;
2169 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2170 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2171 else
2172 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2174 if (cur_tag != NULL)
2176 *tagname = cur_tag;
2177 return tag_line;
2182 *tagname = g_strdup(_("unknown"));
2183 return -1;
2187 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2189 static gint tag_line = -1;
2190 static gchar *cur_tag = NULL;
2192 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2194 if (doc == NULL) /* reset current function */
2196 current_tag_changed(NULL, -1, -1, 0);
2197 g_free(cur_tag);
2198 cur_tag = g_strdup(_("unknown"));
2199 if (tagname != NULL)
2200 *tagname = cur_tag;
2201 tag_line = -1;
2203 else
2205 gint line = sci_get_current_line(doc->editor->sci);
2206 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2208 if (current_tag_changed(doc, line, fold_level, tag_types))
2210 g_free(cur_tag);
2211 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2213 *tagname = cur_tag;
2216 return tag_line;
2220 /* Sets *tagname to point at the current function or tag name.
2221 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2222 * call to this function.
2223 * Returns: line number of the current tag, or -1 if unknown. */
2224 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2226 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2230 /* same as symbols_get_current_function() but finds class, namespaces and more */
2231 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2233 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2234 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2236 /* Python parser reports imports as namespaces which confuses the scope detection */
2237 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2238 tag_types |= tm_tag_namespace_t;
2240 return get_current_tag_name_cached(doc, tagname, tag_types);
2244 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2246 gint sort_mode = GPOINTER_TO_INT(user_data);
2247 GeanyDocument *doc = document_get_current();
2249 if (ignore_callback)
2250 return;
2252 if (doc != NULL)
2253 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2257 static void on_symbol_tree_menu_show(GtkWidget *widget,
2258 gpointer user_data)
2260 GeanyDocument *doc = document_get_current();
2261 gboolean enable;
2263 enable = doc && doc->has_tags;
2264 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2265 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2266 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2267 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2268 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2269 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2271 if (! doc)
2272 return;
2274 ignore_callback = TRUE;
2276 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2277 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2278 else
2279 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2281 ignore_callback = FALSE;
2285 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2287 gboolean expand = GPOINTER_TO_INT(user_data);
2288 GeanyDocument *doc = document_get_current();
2290 if (! doc)
2291 return;
2293 g_return_if_fail(doc->priv->tag_tree);
2295 if (expand)
2296 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2297 else
2298 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2302 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2304 GtkTreeIter iter;
2305 GtkTreeSelection *selection;
2306 GtkTreeModel *model;
2307 GeanyDocument *doc;
2308 TMTag *tag = NULL;
2310 doc = document_get_current();
2311 if (!doc)
2312 return;
2314 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2315 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2316 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2317 if (tag)
2319 if (widget == symbol_menu.find_in_files)
2320 search_show_find_in_files_dialog_full(tag->name, NULL);
2321 else
2322 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2323 widget == symbol_menu.find_usage);
2325 tm_tag_unref(tag);
2330 static void create_taglist_popup_menu(void)
2332 GtkWidget *item, *menu;
2334 tv.popup_taglist = menu = gtk_menu_new();
2336 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2337 gtk_widget_show(item);
2338 gtk_container_add(GTK_CONTAINER(menu), item);
2339 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2341 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2342 gtk_widget_show(item);
2343 gtk_container_add(GTK_CONTAINER(menu), item);
2344 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2346 item = gtk_separator_menu_item_new();
2347 gtk_widget_show(item);
2348 gtk_container_add(GTK_CONTAINER(menu), item);
2350 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2351 _("Sort by _Name"));
2352 gtk_widget_show(item);
2353 gtk_container_add(GTK_CONTAINER(menu), item);
2354 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2355 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2357 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2358 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2359 gtk_widget_show(item);
2360 gtk_container_add(GTK_CONTAINER(menu), item);
2361 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2362 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2364 item = gtk_separator_menu_item_new();
2365 gtk_widget_show(item);
2366 gtk_container_add(GTK_CONTAINER(menu), item);
2368 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2369 gtk_widget_show(item);
2370 gtk_container_add(GTK_CONTAINER(menu), item);
2371 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2373 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2374 gtk_widget_show(item);
2375 gtk_container_add(GTK_CONTAINER(menu), item);
2376 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2378 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2379 gtk_widget_show(item);
2380 gtk_container_add(GTK_CONTAINER(menu), item);
2381 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2383 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2385 sidebar_add_common_menu_items(GTK_MENU(menu));
2389 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2391 gchar *f;
2393 g_return_if_fail(!EMPTY(doc->real_path));
2395 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2396 if (utils_str_equal(doc->real_path, f))
2397 load_c_ignore_tags();
2399 g_free(f);
2403 void symbols_init(void)
2405 gchar *f;
2406 guint i;
2408 create_taglist_popup_menu();
2410 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2411 ui_add_config_file_menu_item(f, NULL, NULL);
2412 g_free(f);
2414 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2416 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2417 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2421 void symbols_finalize(void)
2423 guint i;
2425 g_strfreev(html_entities);
2426 g_strfreev(c_tags_ignore);
2428 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2430 if (symbols_icons[i].pixbuf)
2431 g_object_unref(symbols_icons[i].pixbuf);