Put Makefile comments at start of line.
[geany-mirror.git] / src / symbols.c
blob11cdeafeeb74f063ff0e2e988494380272f248f9
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #include "SciLexer.h"
36 #include "geany.h"
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdlib.h>
42 #include "prefix.h"
43 #include "symbols.h"
44 #include "utils.h"
45 #include "filetypes.h"
46 #include "encodings.h"
47 #include "document.h"
48 #include "documentprivate.h"
49 #include "support.h"
50 #include "msgwindow.h"
51 #include "sidebar.h"
52 #include "main.h"
53 #include "navqueue.h"
54 #include "ui_utils.h"
55 #include "editor.h"
56 #include "sciwrappers.h"
57 #include "filetypesprivate.h"
58 #include "search.h"
61 const guint TM_GLOBAL_TYPE_MASK =
62 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
63 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
66 static gchar **html_entities = NULL;
68 typedef struct
70 gboolean tags_loaded;
71 const gchar *tag_file;
72 } TagFileInfo;
74 /* Check before adding any more tags files, usually they should be downloaded separately. */
75 enum /* Geany tag files */
77 GTF_C,
78 GTF_PASCAL,
79 GTF_PHP,
80 GTF_HTML_ENTITIES,
81 GTF_LATEX,
82 GTF_PYTHON,
83 GTF_MAX
86 static TagFileInfo tag_file_info[GTF_MAX] =
88 {FALSE, "c99.tags"},
89 {FALSE, "pascal.tags"},
90 {FALSE, "php.tags"},
91 {FALSE, "html_entities.tags"},
92 {FALSE, "latex.tags"},
93 {FALSE, "python.tags"}
96 static GPtrArray *top_level_iter_names = NULL;
98 static struct
100 GtkWidget *expand_all;
101 GtkWidget *collapse_all;
102 GtkWidget *sort_by_name;
103 GtkWidget *sort_by_appearance;
104 GtkWidget *find_usage;
105 GtkWidget *find_doc_usage;
106 GtkWidget *find_in_files;
108 symbol_menu;
111 static void html_tags_loaded(void);
112 static void load_user_tags(filetype_id ft_id);
114 /* get the tags_ignore list, exported by tagmanager's options.c */
115 extern gchar **c_tags_ignore;
117 /* ignore certain tokens when parsing C-like syntax.
118 * Also works for reloading. */
119 static void load_c_ignore_tags(void)
121 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
122 gchar *content;
124 if (g_file_get_contents(path, &content, NULL, NULL))
126 /* historically we ignore the glib _DECLS for tag generation */
127 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
129 g_strfreev(c_tags_ignore);
130 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
131 g_free(content);
133 g_free(path);
137 void symbols_reload_config_files(void)
139 load_c_ignore_tags();
143 static gsize get_tag_count(void)
145 GPtrArray *tags = tm_get_workspace()->global_tags;
146 gsize count = tags ? tags->len : 0;
148 return count;
152 /* wrapper for tm_workspace_load_global_tags().
153 * note that the tag count only counts new global tags added - if a tag has the same name,
154 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
155 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
157 gboolean result;
158 gsize old_tag_count = get_tag_count();
160 result = tm_workspace_load_global_tags(tags_file, ft->lang);
161 if (result)
163 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
164 (guint) (get_tag_count() - old_tag_count));
166 return result;
170 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
171 * This provides autocompletion, calltips, etc. */
172 void symbols_global_tags_loaded(guint file_type_idx)
174 TagFileInfo *tfi;
175 gint tag_type;
177 /* load ignore list for C/C++ parser */
178 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
179 c_tags_ignore == NULL)
181 load_c_ignore_tags();
184 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
185 return;
187 /* load config in case of custom filetypes */
188 filetypes_load_config(file_type_idx, FALSE);
190 load_user_tags(file_type_idx);
192 switch (file_type_idx)
194 case GEANY_FILETYPES_PHP:
195 case GEANY_FILETYPES_HTML:
196 html_tags_loaded();
198 switch (file_type_idx)
200 case GEANY_FILETYPES_CPP:
201 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
202 /* no C++ tagfile yet */
203 return;
204 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
205 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
206 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
207 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
208 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
209 default:
210 return;
212 tfi = &tag_file_info[tag_type];
214 if (! tfi->tags_loaded)
216 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
218 symbols_load_global_tags(fname, filetypes[file_type_idx]);
219 tfi->tags_loaded = TRUE;
220 g_free(fname);
225 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
226 static void html_tags_loaded(void)
228 TagFileInfo *tfi;
230 if (cl_options.ignore_global_tags)
231 return;
233 tfi = &tag_file_info[GTF_HTML_ENTITIES];
234 if (! tfi->tags_loaded)
236 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
238 html_entities = utils_read_file_in_array(file);
239 tfi->tags_loaded = TRUE;
240 g_free(file);
245 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
247 guint j;
248 TMTag *tag;
249 GString *s = NULL;
250 GPtrArray *typedefs;
251 gint tag_lang;
253 g_return_val_if_fail(tags_array != NULL, NULL);
255 typedefs = tm_tags_extract(tags_array, tag_types);
257 if ((typedefs) && (typedefs->len > 0))
259 s = g_string_sized_new(typedefs->len * 10);
260 for (j = 0; j < typedefs->len; ++j)
262 tag = TM_TAG(typedefs->pdata[j]);
263 /* tag->atts.file.lang contains (for some reason) the line of the tag if
264 * tag->atts.entry.file is not NULL */
265 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
267 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
268 * e.g. PHP classes in C++ files
269 * lang = -2 disables the check */
270 if (tag->name && (tag_lang == lang || lang == -2))
272 if (j != 0)
273 g_string_append_c(s, ' ');
274 g_string_append(s, tag->name);
278 if (typedefs)
279 g_ptr_array_free(typedefs, TRUE);
280 return s;
284 /** Gets the context separator used by the tag manager for a particular file
285 * type.
286 * @param ft_id File type identifier.
287 * @return The context separator string.
289 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
290 * without a context separator.
292 * @since 0.19
294 const gchar *symbols_get_context_separator(gint ft_id)
296 switch (ft_id)
298 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
299 case GEANY_FILETYPES_CPP:
300 case GEANY_FILETYPES_GLSL: /* for structs */
301 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
302 case GEANY_FILETYPES_PHP:
303 case GEANY_FILETYPES_RUST:
304 return "::";
306 /* avoid confusion with other possible separators in group/section name */
307 case GEANY_FILETYPES_CONF:
308 case GEANY_FILETYPES_REST:
309 return ":::";
311 /* no context separator */
312 case GEANY_FILETYPES_ASCIIDOC:
313 return "\x03";
315 default:
316 return ".";
321 GString *symbols_get_macro_list(gint lang)
323 guint j, i;
324 GPtrArray *ftags;
325 GString *words;
326 gint tag_lang;
327 TMTag *tag;
329 if (app->tm_workspace->work_objects == NULL)
330 return NULL;
332 ftags = g_ptr_array_sized_new(50);
333 words = g_string_sized_new(200);
335 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
337 GPtrArray *tags;
339 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
340 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
341 if (NULL != tags)
343 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
345 tag = TM_TAG(tags->pdata[i]);
346 tag_lang = (tag->atts.entry.file) ?
347 tag->atts.entry.file->lang : tag->atts.file.lang;
349 if (tag_lang == lang)
350 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
352 g_ptr_array_free(tags, TRUE);
356 if (ftags->len == 0)
358 g_ptr_array_free(ftags, TRUE);
359 g_string_free(words, TRUE);
360 return NULL;
363 tm_tags_sort(ftags, NULL, FALSE);
364 for (j = 0; j < ftags->len; j++)
366 if (j > 0)
367 g_string_append_c(words, '\n');
368 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
370 g_ptr_array_free(ftags, TRUE);
371 return words;
375 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
376 static TMTag *
377 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
379 guint i;
380 g_return_val_if_fail(tags != NULL, NULL);
382 for (i = 0; i < tags->len; ++i)
384 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
385 return TM_TAG(tags->pdata[i]);
387 return NULL;
391 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
392 const gchar *tag_name, guint type)
394 GPtrArray *tags;
395 TMTag *tmtag;
397 if (G_LIKELY(workobj != NULL))
399 tags = tm_tags_extract(workobj->tags_array, type);
400 if (tags != NULL)
402 tmtag = symbols_find_tm_tag(tags, tag_name);
404 g_ptr_array_free(tags, TRUE);
406 if (tmtag != NULL)
407 return tmtag;
410 return NULL; /* not found */
414 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
416 guint j;
417 const GPtrArray *work_objects = NULL;
419 if (app->tm_workspace != NULL)
420 work_objects = app->tm_workspace->work_objects;
422 if (work_objects != NULL)
424 for (j = 0; j < work_objects->len; j++)
426 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
427 TMTag *tmtag;
429 tmtag = find_work_object_tag(workobj, tag_name, type);
430 if (tmtag != NULL)
431 return tmtag;
434 return NULL; /* not found */
438 const gchar **symbols_get_html_entities(void)
440 if (html_entities == NULL)
441 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
443 return (const gchar **) html_entities;
447 /* sort by name, then line */
448 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
450 gint ret;
452 if (tag_a == NULL || tag_b == NULL)
453 return 0;
455 if (tag_a->name == NULL)
456 return -(tag_a->name != tag_b->name);
458 if (tag_b->name == NULL)
459 return tag_a->name != tag_b->name;
461 ret = strcmp(tag_a->name, tag_b->name);
462 if (ret == 0)
464 return tag_a->atts.entry.line - tag_b->atts.entry.line;
466 return ret;
470 /* sort by line, then scope */
471 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
473 const TMTag *tag_a = TM_TAG(a);
474 const TMTag *tag_b = TM_TAG(b);
475 gint ret;
477 if (a == NULL || b == NULL)
478 return 0;
480 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
481 if (ret == 0)
483 if (tag_a->atts.entry.scope == NULL)
484 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
485 if (tag_b->atts.entry.scope == NULL)
486 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
487 else
488 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
490 return ret;
494 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
496 GList *tag_names = NULL;
497 TMTag *tag;
498 guint i;
500 g_return_val_if_fail(doc, NULL);
502 if (! doc->tm_file || ! doc->tm_file->tags_array)
503 return NULL;
505 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
507 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
508 if (G_UNLIKELY(tag == NULL))
509 return NULL;
511 if (tag->type & tag_types)
513 tag_names = g_list_prepend(tag_names, tag);
516 tag_names = g_list_sort(tag_names, compare_symbol_lines);
517 return tag_names;
521 /* amount of types in the symbol list (currently max. 8 are used) */
522 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
524 struct TreeviewSymbols
526 GtkTreeIter tag_function;
527 GtkTreeIter tag_class;
528 GtkTreeIter tag_macro;
529 GtkTreeIter tag_member;
530 GtkTreeIter tag_variable;
531 GtkTreeIter tag_externvar;
532 GtkTreeIter tag_namespace;
533 GtkTreeIter tag_struct;
534 GtkTreeIter tag_interface;
535 GtkTreeIter tag_type;
536 GtkTreeIter tag_other;
537 } tv_iters;
540 static void init_tag_iters(void)
542 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
543 * filetypes(e.g. config file to Python crashes Geany without this) */
544 tv_iters.tag_function.stamp = -1;
545 tv_iters.tag_class.stamp = -1;
546 tv_iters.tag_member.stamp = -1;
547 tv_iters.tag_macro.stamp = -1;
548 tv_iters.tag_variable.stamp = -1;
549 tv_iters.tag_externvar.stamp = -1;
550 tv_iters.tag_namespace.stamp = -1;
551 tv_iters.tag_struct.stamp = -1;
552 tv_iters.tag_interface.stamp = -1;
553 tv_iters.tag_type.stamp = -1;
554 tv_iters.tag_other.stamp = -1;
558 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
560 static GtkIconTheme *icon_theme = NULL;
561 static gint x = -1;
563 if (G_UNLIKELY(x < 0))
565 gint dummy;
566 icon_theme = gtk_icon_theme_get_default();
567 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
569 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
573 /* finds the next iter at any level
574 * @param iter in/out, the current iter, will be changed to the next one
575 * @param down whether to try the child iter
576 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
577 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
579 GtkTreeIter guess;
580 GtkTreeIter copy = *iter;
582 /* go down if the item has children */
583 if (down && gtk_tree_model_iter_children(model, &guess, iter))
584 *iter = guess;
585 /* or to the next item at the same level */
586 else if (gtk_tree_model_iter_next(model, &copy))
587 *iter = copy;
588 /* or to the next item at a parent level */
589 else if (gtk_tree_model_iter_parent(model, &guess, iter))
591 copy = guess;
592 while(TRUE)
594 if (gtk_tree_model_iter_next(model, &copy))
596 *iter = copy;
597 return TRUE;
599 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
600 guess = copy;
601 else
602 return FALSE;
605 else
606 return FALSE;
608 return TRUE;
612 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
614 GtkTreeModel *model = GTK_TREE_MODEL(store);
616 if (!gtk_tree_model_get_iter_first(model, iter))
617 return FALSE;
620 gchar *candidate;
622 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
623 /* FIXME: what if 2 different items have the same name?
624 * this should never happen, but might be caused by a typo in a translation */
625 if (utils_str_equal(candidate, title))
627 g_free(candidate);
628 return TRUE;
630 else
631 g_free(candidate);
633 while (gtk_tree_model_iter_next(model, iter));
635 return FALSE;
639 /* Adds symbol list groups in (iter*, title) pairs.
640 * The list must be ended with NULL. */
641 static void G_GNUC_NULL_TERMINATED
642 tag_list_add_groups(GtkTreeStore *tree_store, ...)
644 va_list args;
645 GtkTreeIter *iter;
647 g_return_if_fail(top_level_iter_names);
649 va_start(args, tree_store);
650 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
652 gchar *title = va_arg(args, gchar*);
653 gchar *icon_name = va_arg(args, gchar *);
654 GdkPixbuf *icon = NULL;
656 if (icon_name)
658 icon = get_tag_icon(icon_name);
661 g_assert(title != NULL);
662 g_ptr_array_add(top_level_iter_names, title);
664 if (!find_toplevel_iter(tree_store, iter, title))
665 gtk_tree_store_append(tree_store, iter, NULL);
667 if (G_IS_OBJECT(icon))
669 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
670 g_object_unref(icon);
672 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
674 va_end(args);
678 static void add_top_level_items(GeanyDocument *doc)
680 filetype_id ft_id = doc->file_type->id;
681 GtkTreeStore *tag_store = doc->priv->tag_store;
683 if (top_level_iter_names == NULL)
684 top_level_iter_names = g_ptr_array_new();
685 else
686 g_ptr_array_set_size(top_level_iter_names, 0);
688 init_tag_iters();
690 switch (ft_id)
692 case GEANY_FILETYPES_DIFF:
694 tag_list_add_groups(tag_store,
695 &(tv_iters.tag_function), _("Files"), NULL, NULL);
696 break;
698 case GEANY_FILETYPES_DOCBOOK:
700 tag_list_add_groups(tag_store,
701 &(tv_iters.tag_function), _("Chapter"), NULL,
702 &(tv_iters.tag_class), _("Section"), NULL,
703 &(tv_iters.tag_member), _("Sect1"), NULL,
704 &(tv_iters.tag_macro), _("Sect2"), NULL,
705 &(tv_iters.tag_variable), _("Sect3"), NULL,
706 &(tv_iters.tag_struct), _("Appendix"), NULL,
707 &(tv_iters.tag_other), _("Other"), NULL,
708 NULL);
709 break;
711 case GEANY_FILETYPES_HASKELL:
712 tag_list_add_groups(tag_store,
713 &tv_iters.tag_namespace, _("Module"), NULL,
714 &tv_iters.tag_type, _("Types"), NULL,
715 &tv_iters.tag_macro, _("Type constructors"), NULL,
716 &tv_iters.tag_function, _("Functions"), "classviewer-method",
717 NULL);
718 break;
719 case GEANY_FILETYPES_COBOL:
720 tag_list_add_groups(tag_store,
721 &tv_iters.tag_class, _("Program"), "classviewer-class",
722 &tv_iters.tag_function, _("File"), "classviewer-method",
723 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
724 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
725 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
726 &tv_iters.tag_variable, _("Data"), "classviewer-var",
727 NULL);
728 break;
729 case GEANY_FILETYPES_CONF:
730 tag_list_add_groups(tag_store,
731 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
732 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
733 NULL);
734 break;
735 case GEANY_FILETYPES_NSIS:
736 tag_list_add_groups(tag_store,
737 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
738 &tv_iters.tag_function, _("Functions"), "classviewer-method",
739 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
740 NULL);
741 break;
742 case GEANY_FILETYPES_LATEX:
744 tag_list_add_groups(tag_store,
745 &(tv_iters.tag_function), _("Command"), NULL,
746 &(tv_iters.tag_class), _("Environment"), NULL,
747 &(tv_iters.tag_member), _("Section"), NULL,
748 &(tv_iters.tag_macro), _("Subsection"), NULL,
749 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
750 &(tv_iters.tag_struct), _("Label"), NULL,
751 &(tv_iters.tag_namespace), _("Chapter"), NULL,
752 &(tv_iters.tag_other), _("Other"), NULL,
753 NULL);
754 break;
756 case GEANY_FILETYPES_MATLAB:
758 tag_list_add_groups(tag_store,
759 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
760 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
761 NULL);
762 break;
764 case GEANY_FILETYPES_ABAQUS:
766 tag_list_add_groups(tag_store,
767 &(tv_iters.tag_class), _("Parts"), NULL,
768 &(tv_iters.tag_member), _("Assembly"), NULL,
769 &(tv_iters.tag_namespace), _("Steps"), NULL,
770 NULL);
771 break;
773 case GEANY_FILETYPES_R:
775 tag_list_add_groups(tag_store,
776 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
777 &(tv_iters.tag_other), _("Other"), NULL,
778 NULL);
779 break;
781 case GEANY_FILETYPES_RUST:
783 tag_list_add_groups(tag_store,
784 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
785 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
786 &(tv_iters.tag_interface), _("Traits"), "classviewer-class",
787 &(tv_iters.tag_class), _("Implementations"), "classviewer-class",
788 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
789 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
790 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
791 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro",
792 &(tv_iters.tag_member), _("Methods"), "classviewer-member",
793 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL,
794 NULL);
795 break;
797 case GEANY_FILETYPES_PERL:
799 tag_list_add_groups(tag_store,
800 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
801 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
802 &(tv_iters.tag_macro), _("Labels"), NULL,
803 &(tv_iters.tag_type), _("Constants"), NULL,
804 &(tv_iters.tag_other), _("Other"), "classviewer-other",
805 NULL);
806 break;
808 case GEANY_FILETYPES_PHP:
810 tag_list_add_groups(tag_store,
811 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
812 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
813 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
814 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
815 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
816 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
817 &(tv_iters.tag_struct), _("Traits"), "classviewer-struct",
818 NULL);
819 break;
821 case GEANY_FILETYPES_HTML:
823 tag_list_add_groups(tag_store,
824 &(tv_iters.tag_function), _("Functions"), NULL,
825 &(tv_iters.tag_member), _("Anchors"), NULL,
826 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
827 &(tv_iters.tag_class), _("H2 Headings"), NULL,
828 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
829 NULL);
830 break;
832 case GEANY_FILETYPES_CSS:
834 tag_list_add_groups(tag_store,
835 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
836 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
837 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
838 break;
840 case GEANY_FILETYPES_REST:
841 case GEANY_FILETYPES_TXT2TAGS:
842 case GEANY_FILETYPES_ABC:
844 tag_list_add_groups(tag_store,
845 &(tv_iters.tag_namespace), _("Chapter"), NULL,
846 &(tv_iters.tag_member), _("Section"), NULL,
847 &(tv_iters.tag_macro), _("Subsection"), NULL,
848 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
849 NULL);
850 break;
852 case GEANY_FILETYPES_ASCIIDOC:
854 tag_list_add_groups(tag_store,
855 &(tv_iters.tag_namespace), _("Document"), NULL,
856 &(tv_iters.tag_member), _("Section Level 1"), NULL,
857 &(tv_iters.tag_macro), _("Section Level 2"), NULL,
858 &(tv_iters.tag_variable), _("Section Level 3"), NULL,
859 &(tv_iters.tag_struct), _("Section Level 4"), NULL,
860 NULL);
861 break;
863 case GEANY_FILETYPES_RUBY:
865 tag_list_add_groups(tag_store,
866 &(tv_iters.tag_namespace), _("Modules"), "classviewer-namespace",
867 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
868 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
869 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
870 NULL);
871 break;
873 case GEANY_FILETYPES_TCL:
875 tag_list_add_groups(tag_store,
876 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
877 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
878 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
879 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
880 NULL);
881 break;
883 case GEANY_FILETYPES_PYTHON:
885 tag_list_add_groups(tag_store,
886 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
887 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
888 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
889 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
890 &(tv_iters.tag_externvar), _("Imports"), "classviewer-namespace",
891 NULL);
892 break;
894 case GEANY_FILETYPES_VHDL:
896 tag_list_add_groups(tag_store,
897 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
898 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
899 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
900 &(tv_iters.tag_type), _("Types"), "classviewer-other",
901 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
902 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
903 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
904 &(tv_iters.tag_other), _("Other"), "classviewer-other",
905 NULL);
906 break;
908 case GEANY_FILETYPES_VERILOG:
910 tag_list_add_groups(tag_store,
911 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
912 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
913 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
914 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
915 &(tv_iters.tag_other), _("Other"), "classviewer-other",
916 NULL);
917 break;
919 case GEANY_FILETYPES_JAVA:
921 tag_list_add_groups(tag_store,
922 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
923 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
924 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
925 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
926 &(tv_iters.tag_member), _("Members"), "classviewer-member",
927 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
928 &(tv_iters.tag_other), _("Other"), "classviewer-other",
929 NULL);
930 break;
932 case GEANY_FILETYPES_AS:
934 tag_list_add_groups(tag_store,
935 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
936 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
937 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
938 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
939 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
940 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
941 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
942 &(tv_iters.tag_other), _("Other"), "classviewer-other",
943 NULL);
944 break;
946 case GEANY_FILETYPES_HAXE:
948 tag_list_add_groups(tag_store,
949 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
950 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
951 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
952 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
953 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
954 &(tv_iters.tag_other), _("Other"), "classviewer-other",
955 NULL);
956 break;
958 case GEANY_FILETYPES_BASIC:
960 tag_list_add_groups(tag_store,
961 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
962 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
963 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
964 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
965 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
966 &(tv_iters.tag_other), _("Other"), "classviewer-other",
967 NULL);
968 break;
970 case GEANY_FILETYPES_F77:
971 case GEANY_FILETYPES_FORTRAN:
973 tag_list_add_groups(tag_store,
974 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
975 &(tv_iters.tag_struct), _("Programs"), "classviewer-class",
976 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
977 &(tv_iters.tag_function), _("Functions / Subroutines"), "classviewer-method",
978 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
979 &(tv_iters.tag_class), _("Types"), "classviewer-class",
980 &(tv_iters.tag_member), _("Components"), "classviewer-member",
981 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
982 &(tv_iters.tag_type), _("Enums"), "classviewer-struct",
983 &(tv_iters.tag_other), _("Other"), "classviewer-other",
984 NULL);
985 break;
987 case GEANY_FILETYPES_ASM:
989 tag_list_add_groups(tag_store,
990 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
991 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
992 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
993 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
994 NULL);
995 break;
997 case GEANY_FILETYPES_MAKE:
998 tag_list_add_groups(tag_store,
999 &tv_iters.tag_function, _("Targets"), "classviewer-method",
1000 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
1001 NULL);
1002 break;
1003 case GEANY_FILETYPES_SQL:
1005 tag_list_add_groups(tag_store,
1006 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
1007 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
1008 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
1009 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
1010 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
1011 &(tv_iters.tag_member), _("Views"), "classviewer-var",
1012 &(tv_iters.tag_other), _("Other"), "classviewer-other",
1013 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1014 NULL);
1015 break;
1017 case GEANY_FILETYPES_D:
1018 default:
1020 if (ft_id == GEANY_FILETYPES_D)
1021 tag_list_add_groups(tag_store,
1022 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
1023 else
1024 tag_list_add_groups(tag_store,
1025 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
1027 tag_list_add_groups(tag_store,
1028 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
1029 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
1030 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
1031 &(tv_iters.tag_member), _("Members"), "classviewer-member",
1032 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
1033 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
1034 NULL);
1036 if (ft_id != GEANY_FILETYPES_D)
1038 tag_list_add_groups(tag_store,
1039 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
1041 tag_list_add_groups(tag_store,
1042 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
1043 &(tv_iters.tag_externvar), _("Extern Variables"), "classviewer-var",
1044 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1050 /* removes toplevel items that have no children */
1051 static void hide_empty_rows(GtkTreeStore *store)
1053 GtkTreeIter iter;
1054 gboolean cont = TRUE;
1056 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1057 return; /* stop when first iter is invalid, i.e. no elements */
1059 while (cont)
1061 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1062 cont = gtk_tree_store_remove(store, &iter);
1063 else
1064 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1069 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1071 gchar *utf8_name;
1072 const gchar *scope = tag->atts.entry.scope;
1073 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1074 gboolean doc_is_utf8 = FALSE;
1076 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1077 * for None at this point completely */
1078 if (utils_str_equal(doc->encoding, "UTF-8") ||
1079 utils_str_equal(doc->encoding, "None"))
1080 doc_is_utf8 = TRUE;
1081 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
1082 * plugin might have called tm_source_file_update(), so check to be sure */
1083 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
1085 if (! doc_is_utf8)
1086 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1087 -1, doc->encoding, TRUE);
1088 else
1089 utf8_name = tag->name;
1091 if (utf8_name == NULL)
1092 return NULL;
1094 if (! buffer)
1095 buffer = g_string_new(NULL);
1096 else
1097 g_string_truncate(buffer, 0);
1099 /* check first char of scope is a wordchar */
1100 if (!found_parent && scope &&
1101 strpbrk(scope, GEANY_WORDCHARS) == scope)
1103 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1105 g_string_append(buffer, scope);
1106 g_string_append(buffer, sep);
1108 g_string_append(buffer, utf8_name);
1110 if (! doc_is_utf8)
1111 g_free(utf8_name);
1113 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1115 return buffer->str;
1119 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1121 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1123 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1124 * for None at this point completely */
1125 if (utf8_name != NULL &&
1126 ! utils_str_equal(doc->encoding, "UTF-8") &&
1127 ! utils_str_equal(doc->encoding, "None"))
1129 SETPTR(utf8_name,
1130 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1133 if (utf8_name != NULL)
1134 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1136 return utf8_name;
1140 /* find the last word in "foo::bar::blah", e.g. "blah" */
1141 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1143 const gchar *scope = tag->atts.entry.scope;
1144 const gchar *separator = symbols_get_context_separator(ft_id);
1145 const gchar *str, *ptr;
1147 if (!scope)
1148 return NULL;
1150 str = scope;
1152 while (1)
1154 ptr = strstr(str, separator);
1155 if (ptr)
1157 str = ptr + strlen(separator);
1159 else
1160 break;
1163 return !EMPTY(str) ? str : NULL;
1167 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1169 GtkTreeIter *iter = NULL;
1171 switch (tag_type)
1173 case tm_tag_prototype_t:
1174 case tm_tag_method_t:
1175 case tm_tag_function_t:
1177 iter = &tv_iters.tag_function;
1178 break;
1180 case tm_tag_externvar_t:
1182 iter = &tv_iters.tag_externvar;
1183 break;
1185 case tm_tag_macro_t:
1186 case tm_tag_macro_with_arg_t:
1188 iter = &tv_iters.tag_macro;
1189 break;
1191 case tm_tag_class_t:
1193 iter = &tv_iters.tag_class;
1194 break;
1196 case tm_tag_member_t:
1197 case tm_tag_field_t:
1199 iter = &tv_iters.tag_member;
1200 break;
1202 case tm_tag_typedef_t:
1203 case tm_tag_enum_t:
1205 iter = &tv_iters.tag_type;
1206 break;
1208 case tm_tag_union_t:
1209 case tm_tag_struct_t:
1211 iter = &tv_iters.tag_struct;
1212 break;
1214 case tm_tag_interface_t:
1215 iter = &tv_iters.tag_interface;
1216 break;
1217 case tm_tag_variable_t:
1219 iter = &tv_iters.tag_variable;
1220 break;
1222 case tm_tag_namespace_t:
1223 case tm_tag_package_t:
1225 iter = &tv_iters.tag_namespace;
1226 break;
1228 default:
1230 iter = &tv_iters.tag_other;
1233 if (G_LIKELY(iter->stamp != -1))
1234 return iter;
1235 else
1236 return NULL;
1240 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1242 GdkPixbuf *icon = NULL;
1244 if (parent == &tv_iters.tag_other)
1246 return get_tag_icon("classviewer-var");
1248 /* copy parent icon */
1249 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1250 SYMBOLS_COLUMN_ICON, &icon, -1);
1251 return icon;
1255 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1257 const TMTag *t1 = v1;
1258 const TMTag *t2 = v2;
1260 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1261 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1262 /* include arglist in match to support e.g. C++ overloading */
1263 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1267 /* inspired from g_str_hash() */
1268 static guint tag_hash(gconstpointer v)
1270 const TMTag *tag = v;
1271 const gchar *p;
1272 guint32 h = 5381;
1274 h = (h << 5) + h + tag->type;
1275 for (p = tag->name; *p != '\0'; p++)
1276 h = (h << 5) + h + *p;
1277 if (tag->atts.entry.scope)
1279 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1280 h = (h << 5) + h + *p;
1282 /* for e.g. C++ overloading */
1283 if (tag->atts.entry.arglist)
1285 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1286 h = (h << 5) + h + *p;
1289 return h;
1293 /* like gtk_tree_view_expand_to_path() but with an iter */
1294 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1296 GtkTreeModel *model = gtk_tree_view_get_model(view);
1297 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1299 gtk_tree_view_expand_to_path(view, path);
1300 gtk_tree_path_free(path);
1304 /* like gtk_tree_store_remove() but finds the next iter at any level */
1305 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1307 GtkTreeIter parent;
1308 gboolean has_parent;
1309 gboolean cont;
1311 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1312 cont = gtk_tree_store_remove(store, iter);
1313 /* if there is no next at this level but there is a parent iter, continue from it */
1314 if (! cont && has_parent)
1316 *iter = parent;
1317 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1320 return cont;
1324 /* adds a new element in the parent table if it's key is known.
1325 * duplicates are kept */
1326 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1327 const GtkTreeIter *iter)
1329 GList **list;
1330 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1331 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1333 if (! list)
1335 list = g_slice_alloc(sizeof *list);
1336 *list = NULL;
1337 g_hash_table_insert(table, tag->name, list);
1339 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1344 static void free_iter_slice_list(gpointer data)
1346 GList **list = data;
1348 if (list)
1350 GList *node;
1351 foreach_list(node, *list)
1352 g_slice_free(GtkTreeIter, node->data);
1353 g_list_free(*list);
1354 g_slice_free1(sizeof *list, list);
1359 /* inserts a @data in @table on key @tag.
1360 * previous data is not overwritten if the key is duplicated, but rather the
1361 * two values are kept in a list
1363 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1364 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1366 GList *list = g_hash_table_lookup(table, tag);
1367 list = g_list_prepend(list, data);
1368 g_hash_table_insert(table, tag, list);
1372 /* looks up the entry in @table that better matches @tag.
1373 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1374 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1376 GList *data = NULL;
1377 GList *node = g_hash_table_lookup(table, tag);
1378 if (node)
1380 glong delta;
1381 data = node->data;
1383 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1385 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1386 for (node = node->next; node; node = node->next)
1388 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1390 if (d < delta)
1392 data = node->data;
1393 delta = d;
1397 #undef TAG_DELTA
1400 return data;
1404 /* removes the element at @tag from @table.
1405 * @tag must be the exact pointer used at insertion time */
1406 static void tags_table_remove(GHashTable *table, TMTag *tag)
1408 GList *list = g_hash_table_lookup(table, tag);
1409 if (list)
1411 GList *node;
1412 foreach_list(node, list)
1414 if (((GList *) node->data)->data == tag)
1415 break;
1417 list = g_list_delete_link(list, node);
1418 if (list)
1419 g_hash_table_insert(table, tag, list);
1420 else
1421 g_hash_table_remove(table, tag);
1426 static void tags_table_destroy(GHashTable *table)
1428 /* free any leftover elements. note that we can't register a value_free_func when
1429 * creating the hash table because we only want to free it when destroying the table,
1430 * not when inserting a duplicate (we handle this manually) */
1431 GHashTableIter iter;
1432 gpointer value;
1434 g_hash_table_iter_init(&iter, table);
1435 while (g_hash_table_iter_next(&iter, NULL, &value))
1436 g_list_free(value);
1437 g_hash_table_destroy(table);
1442 * Updates the tag tree for a document with the tags in *list.
1443 * @param doc a document
1444 * @param tags a pointer to a GList* holding the tags to add/update. This
1445 * list may be updated, removing updated elements.
1447 * The update is done in two passes:
1448 * 1) walking the current tree, update tags that still exist and remove the
1449 * obsolescent ones;
1450 * 2) walking the remaining (non updated) tags, adds them in the list.
1452 * For better performances, we use 2 hash tables:
1453 * - one containing all the tags for lookup in the first pass (actually stores a
1454 * reference in the tags list for removing it efficiently), avoiding list search
1455 * on each tag;
1456 * - the other holding "tag-name":row references for tags having children, used to
1457 * lookup for a parent in both passes, avoiding tree traversal.
1459 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1461 GtkTreeStore *store = doc->priv->tag_store;
1462 GtkTreeModel *model = GTK_TREE_MODEL(store);
1463 GHashTable *parents_table;
1464 GHashTable *tags_table;
1465 GtkTreeIter iter;
1466 gboolean cont;
1467 GList *item;
1469 /* Build hash tables holding tags and parents */
1470 /* parent table holds "tag-name":GtkTreeIter */
1471 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1472 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1473 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1474 foreach_list(item, *tags)
1476 TMTag *tag = item->data;
1477 const gchar *name;
1479 tags_table_insert(tags_table, tag, item);
1481 name = get_parent_name(tag, doc->file_type->id);
1482 if (name)
1483 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1486 /* First pass, update existing rows or delete them.
1487 * It is OK to delete them since we walk top down so we would remove
1488 * parents before checking for their children, thus never implicitly
1489 * deleting an updated child */
1490 cont = gtk_tree_model_get_iter_first(model, &iter);
1491 while (cont)
1493 TMTag *tag;
1495 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1496 if (! tag) /* most probably a toplevel, skip it */
1497 cont = next_iter(model, &iter, TRUE);
1498 else
1500 GList *found_item;
1502 found_item = tags_table_lookup(tags_table, tag);
1503 if (! found_item) /* tag doesn't exist, remove it */
1504 cont = tree_store_remove_row(store, &iter);
1505 else /* tag still exist, update it */
1507 const gchar *name;
1508 const gchar *parent_name;
1509 TMTag *found = found_item->data;
1511 parent_name = get_parent_name(found, doc->file_type->id);
1512 /* if parent is unknown, ignore it */
1513 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1514 parent_name = NULL;
1516 /* only update fields that (can) have changed (name that holds line
1517 * number, and the tag itself) */
1518 name = get_symbol_name(doc, found, parent_name != NULL);
1519 gtk_tree_store_set(store, &iter,
1520 SYMBOLS_COLUMN_NAME, name,
1521 SYMBOLS_COLUMN_TAG, found,
1522 -1);
1524 update_parents_table(parents_table, found, parent_name, &iter);
1526 /* remove the updated tag from the table and list */
1527 tags_table_remove(tags_table, found);
1528 *tags = g_list_delete_link(*tags, found_item);
1530 cont = next_iter(model, &iter, TRUE);
1533 tm_tag_unref(tag);
1537 /* Second pass, now we have a tree cleaned up from invalid rows,
1538 * we simply add new ones */
1539 foreach_list (item, *tags)
1541 TMTag *tag = item->data;
1542 GtkTreeIter *parent;
1544 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1545 if (G_UNLIKELY(! parent))
1546 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1547 else
1549 gboolean expand;
1550 const gchar *name;
1551 const gchar *parent_name;
1552 gchar *tooltip;
1553 GdkPixbuf *icon = get_child_icon(store, parent);
1555 parent_name = get_parent_name(tag, doc->file_type->id);
1556 if (parent_name)
1558 GList **candidates;
1559 GtkTreeIter *parent_search = NULL;
1561 /* walk parent candidates to find the better one.
1562 * if there are more than one, take the one that has the closest line number
1563 * after the tag we're searching the parent for */
1564 candidates = g_hash_table_lookup(parents_table, parent_name);
1565 if (candidates)
1567 GList *node;
1568 glong delta = G_MAXLONG;
1569 foreach_list(node, *candidates)
1571 TMTag *parent_tag;
1572 glong d;
1574 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1575 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1577 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1578 if (! parent_search || (d >= 0 && d < delta))
1580 delta = d;
1581 parent_search = node->data;
1583 tm_tag_unref(parent_tag);
1587 if (parent_search)
1588 parent = parent_search;
1589 else
1590 parent_name = NULL;
1593 /* only expand to the iter if the parent was empty, otherwise we let the
1594 * folding as it was before (already expanded, or closed by the user) */
1595 expand = ! gtk_tree_model_iter_has_child(model, parent);
1597 /* insert the new element */
1598 gtk_tree_store_append(store, &iter, parent);
1599 name = get_symbol_name(doc, tag, parent_name != NULL);
1600 tooltip = get_symbol_tooltip(doc, tag);
1601 gtk_tree_store_set(store, &iter,
1602 SYMBOLS_COLUMN_NAME, name,
1603 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1604 SYMBOLS_COLUMN_ICON, icon,
1605 SYMBOLS_COLUMN_TAG, tag,
1606 -1);
1607 g_free(tooltip);
1608 if (G_LIKELY(icon))
1609 g_object_unref(icon);
1611 update_parents_table(parents_table, tag, parent_name, &iter);
1613 if (expand)
1614 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1618 g_hash_table_destroy(parents_table);
1619 tags_table_destroy(tags_table);
1623 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1624 * is not stable, so the order is already lost. */
1625 static gint compare_top_level_names(const gchar *a, const gchar *b)
1627 guint i;
1628 const gchar *name;
1630 /* This should never happen as it would mean that two or more top
1631 * level items have the same name but it can happen by typos in the translations. */
1632 if (utils_str_equal(a, b))
1633 return 1;
1635 foreach_ptr_array(name, i, top_level_iter_names)
1637 if (utils_str_equal(name, a))
1638 return -1;
1639 if (utils_str_equal(name, b))
1640 return 1;
1642 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1643 return 0;
1647 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1648 GtkTreeIter *iter)
1650 /* if the tag has a parent tag, it should be at depth >= 2 */
1651 return !EMPTY(tag->atts.entry.scope) &&
1652 gtk_tree_store_iter_depth(store, iter) == 1;
1656 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1657 gpointer user_data)
1659 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1660 TMTag *tag_a, *tag_b;
1661 gint cmp;
1663 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1664 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1666 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1667 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1668 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1669 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1671 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1672 compare_symbol_lines(tag_a, tag_b);
1674 else
1676 gchar *astr, *bstr;
1678 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1679 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1681 /* if a is toplevel, b must be also */
1682 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1684 cmp = compare_top_level_names(astr, bstr);
1686 else
1688 /* this is what g_strcmp0() does */
1689 if (! astr)
1690 cmp = -(astr != bstr);
1691 else if (! bstr)
1692 cmp = astr != bstr;
1693 else
1695 cmp = strcmp(astr, bstr);
1697 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1698 if (tag_a && tag_b)
1699 if (!sort_by_name ||
1700 (utils_str_equal(tag_a->name, tag_b->name) &&
1701 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1702 cmp = compare_symbol_lines(tag_a, tag_b);
1705 g_free(astr);
1706 g_free(bstr);
1708 tm_tag_unref(tag_a);
1709 tm_tag_unref(tag_b);
1711 return cmp;
1715 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1717 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1718 GINT_TO_POINTER(sort_by_name), NULL);
1720 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1724 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1726 GList *tags;
1728 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1730 tags = get_tag_list(doc, tm_tag_max_t);
1731 if (tags == NULL)
1732 return FALSE;
1734 /* FIXME: Not sure why we detached the model here? */
1736 /* disable sorting during update because the code doesn't support correctly
1737 * models that are currently being built */
1738 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1740 /* add grandparent type iters */
1741 add_top_level_items(doc);
1743 update_tree_tags(doc, &tags);
1744 g_list_free(tags);
1746 hide_empty_rows(doc->priv->tag_store);
1748 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1749 sort_mode = doc->priv->symbol_list_sort_mode;
1751 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1752 doc->priv->symbol_list_sort_mode = sort_mode;
1754 return TRUE;
1758 /* Detects a global tags filetype from the *.lang.* language extension.
1759 * Returns NULL if there was no matching TM language. */
1760 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1762 gchar *tags_ext;
1763 gchar *shortname = utils_strdupa(utf8_filename);
1764 GeanyFiletype *ft = NULL;
1766 tags_ext = g_strrstr(shortname, ".tags");
1767 if (tags_ext)
1769 *tags_ext = '\0'; /* remove .tags extension */
1770 ft = filetypes_detect_from_extension(shortname);
1771 if (ft->id != GEANY_FILETYPES_NONE)
1772 return ft;
1774 return NULL;
1778 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1779 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1780 * the relevant path.
1781 * Example:
1782 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1783 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1785 /* -E pre-process, -dD output user macros, -p prof info (?) */
1786 const char pre_process[] = "gcc -E -dD -p -I.";
1788 if (argc > 2)
1790 /* Create global taglist */
1791 int status;
1792 char *command;
1793 const char *tags_file = argv[1];
1794 char *utf8_fname;
1795 GeanyFiletype *ft;
1797 utf8_fname = utils_get_utf8_from_locale(tags_file);
1798 ft = detect_global_tags_filetype(utf8_fname);
1799 g_free(utf8_fname);
1801 if (ft == NULL)
1803 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1804 return 1;
1806 /* load config in case of custom filetypes */
1807 filetypes_load_config(ft->id, FALSE);
1809 /* load ignore list for C/C++ parser */
1810 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1811 load_c_ignore_tags();
1813 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1815 const gchar *cflags = getenv("CFLAGS");
1816 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1818 else
1819 command = NULL; /* don't preprocess */
1821 geany_debug("Generating %s tags file.", ft->name);
1822 tm_get_workspace();
1823 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1824 argc - 2, tags_file, ft->lang);
1825 g_free(command);
1826 symbols_finalize(); /* free c_tags_ignore data */
1827 if (! status)
1829 g_printerr(_("Failed to create tags file, perhaps because no tags "
1830 "were found.\n"));
1831 return 1;
1834 else
1836 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1837 g_printerr(_("Example:\n"
1838 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1839 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1840 return 1;
1842 return 0;
1846 void symbols_show_load_tags_dialog(void)
1848 GtkWidget *dialog;
1849 GtkFileFilter *filter;
1851 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1852 GTK_FILE_CHOOSER_ACTION_OPEN,
1853 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1854 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1855 NULL);
1856 gtk_widget_set_name(dialog, "GeanyDialog");
1857 filter = gtk_file_filter_new();
1858 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1859 gtk_file_filter_add_pattern(filter, "*.*.tags");
1860 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1862 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1864 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1865 GSList *item;
1867 for (item = flist; item != NULL; item = g_slist_next(item))
1869 gchar *fname = item->data;
1870 gchar *utf8_fname;
1871 GeanyFiletype *ft;
1873 utf8_fname = utils_get_utf8_from_locale(fname);
1874 ft = detect_global_tags_filetype(utf8_fname);
1876 if (ft != NULL && symbols_load_global_tags(fname, ft))
1877 /* For translators: the first wildcard is the filetype, the second the filename */
1878 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1879 filetypes_get_display_name(ft), utf8_fname);
1880 else
1881 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1883 g_free(utf8_fname);
1884 g_free(fname);
1886 g_slist_free(flist);
1888 gtk_widget_destroy(dialog);
1892 static void init_user_tags(void)
1894 GSList *file_list = NULL, *list = NULL;
1895 const GSList *node;
1896 gchar *dir;
1898 dir = g_build_filename(app->configdir, "tags", NULL);
1899 /* create the user tags dir for next time if it doesn't exist */
1900 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1901 utils_mkdir(dir, FALSE);
1902 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1904 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1905 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1906 g_free(dir);
1908 file_list = g_slist_concat(file_list, list);
1910 /* populate the filetype-specific tag files lists */
1911 for (node = file_list; node != NULL; node = node->next)
1913 gchar *fname = node->data;
1914 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1915 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1917 g_free(utf8_fname);
1919 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1920 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1921 else
1923 geany_debug("Unknown filetype for file '%s'.", fname);
1924 g_free(fname);
1928 /* don't need to delete list contents because they are now stored in
1929 * ft->priv->tag_files */
1930 g_slist_free(file_list);
1934 static void load_user_tags(filetype_id ft_id)
1936 static guchar *tags_loaded = NULL;
1937 static gboolean init_tags = FALSE;
1938 const GSList *node;
1939 GeanyFiletype *ft = filetypes[ft_id];
1941 g_return_if_fail(ft_id > 0);
1943 if (!tags_loaded)
1944 tags_loaded = g_new0(guchar, filetypes_array->len);
1945 if (tags_loaded[ft_id])
1946 return;
1947 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1949 if (!init_tags)
1951 init_user_tags();
1952 init_tags = TRUE;
1955 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1957 const gchar *fname = node->data;
1959 symbols_load_global_tags(fname, ft);
1964 static gboolean goto_tag(const gchar *name, gboolean definition)
1966 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1967 guint type;
1968 TMTag *tmtag = NULL;
1969 GeanyDocument *old_doc = document_get_current();
1971 /* goto tag definition: all except prototypes / forward declarations / externs */
1972 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1974 /* first look in the current document */
1975 if (old_doc != NULL && old_doc->tm_file)
1976 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1978 /* if not found, look in the workspace */
1979 if (tmtag == NULL)
1980 tmtag = find_workspace_tag(name, type);
1982 if (tmtag != NULL)
1984 GeanyDocument *new_doc = document_find_by_real_path(
1985 tmtag->atts.entry.file->work_object.file_name);
1987 if (new_doc)
1989 /* If we are already on the tag line, swap definition/declaration */
1990 if (new_doc == old_doc &&
1991 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1993 if (goto_tag(name, !definition))
1994 return TRUE;
1997 else
1999 /* not found in opened document, should open */
2000 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
2003 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
2004 return TRUE;
2006 return FALSE;
2010 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
2012 if (goto_tag(name, definition))
2013 return TRUE;
2015 /* if we are here, there was no match and we are beeping ;-) */
2016 utils_beep();
2018 if (!definition)
2019 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
2020 else
2021 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
2022 return FALSE;
2026 /* This could perhaps be improved to check for #if, class etc. */
2027 static gint get_function_fold_number(GeanyDocument *doc)
2029 /* for Java the functions are always one fold level above the class scope */
2030 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
2031 return SC_FOLDLEVELBASE + 1;
2032 else
2033 return SC_FOLDLEVELBASE;
2037 /* Should be used only with get_current_tag_cached.
2038 * tag_types caching might trigger recomputation too often but this isn't used differently often
2039 * enough to be an issue for now */
2040 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2042 static gint old_line = -2;
2043 static GeanyDocument *old_doc = NULL;
2044 static gint old_fold_num = -1;
2045 static guint old_tag_types = 0;
2046 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2047 gboolean ret;
2049 /* check if the cached line and file index have changed since last time: */
2050 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2051 ret = TRUE;
2052 else if (cur_line == old_line)
2053 ret = FALSE;
2054 else
2056 /* if the line has only changed by 1 */
2057 if (abs(cur_line - old_line) == 1)
2059 /* It's the same function if the fold number hasn't changed */
2060 ret = (fold_num != old_fold_num);
2062 else ret = TRUE;
2065 /* record current line and file index for next time */
2066 old_line = cur_line;
2067 old_doc = doc;
2068 old_fold_num = fold_num;
2069 old_tag_types = tag_types;
2070 return ret;
2074 /* Parse the function name up to 2 lines before tag_line.
2075 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2076 * type or argument names can be confused with the function name. */
2077 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2079 gint start, end, max_pos;
2080 gint fn_style;
2082 switch (sci_get_lexer(sci))
2084 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2085 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2086 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2088 start = sci_get_position_from_line(sci, tag_line - 2);
2089 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2090 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2091 start++;
2093 end = start;
2094 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2095 end++;
2097 if (start == end)
2098 return NULL;
2099 return sci_get_contents_range(sci, start, end);
2103 /* Parse the function name */
2104 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2106 gint start, end, first_pos, max_pos;
2107 gint tmp;
2108 gchar c;
2110 first_pos = end = sci_get_position_from_line(sci, tag_line);
2111 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2112 tmp = 0;
2113 /* goto the begin of function body */
2114 while (end < max_pos &&
2115 (tmp = sci_get_char_at(sci, end)) != '{' &&
2116 tmp != 0) end++;
2117 if (tmp == 0) end --;
2119 /* go back to the end of function identifier */
2120 while (end > 0 && end > first_pos - 500 &&
2121 (tmp = sci_get_char_at(sci, end)) != '(' &&
2122 tmp != 0) end--;
2123 end--;
2124 if (end < 0) end = 0;
2126 /* skip whitespaces between identifier and ( */
2127 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2129 start = end;
2130 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2131 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2132 || tmp == SCE_C_GLOBALCLASS
2133 || (c = sci_get_char_at(sci, start)) == '~'
2134 || c == ':'))
2135 start--;
2136 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2138 if (start == end) return NULL;
2139 return sci_get_contents_range(sci, start, end + 1);
2143 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2145 gint line_count = sci_get_line_count(sci);
2147 for (; line < line_count; line++)
2149 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2150 return line;
2153 return -1;
2157 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, guint tag_types)
2159 gint line;
2160 gint parent;
2162 line = sci_get_current_line(doc->editor->sci);
2163 parent = sci_get_fold_parent(doc->editor->sci, line);
2164 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2165 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2166 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2168 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2170 if (tag)
2172 gint tag_line = tag->atts.entry.line - 1;
2173 gint last_child = line + 1;
2175 /* if it may be a false positive because we're inside a fold level not inside anything
2176 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2177 * right after the tag we got from TM */
2178 if (abs(tag_line - parent) > 1)
2180 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2181 if (tag_fold >= 0)
2182 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2185 if (line <= last_child)
2187 if (tag->atts.entry.scope)
2188 *tagname = g_strconcat(tag->atts.entry.scope,
2189 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2190 else
2191 *tagname = g_strdup(tag->name);
2193 return tag_line;
2197 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2198 * to dirty and inaccurate hand-parsing */
2199 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2201 const gint fn_fold = get_function_fold_number(doc);
2202 gint tag_line = parent;
2203 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2205 /* find the top level fold point */
2206 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2208 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2209 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2212 if (tag_line >= 0)
2214 gchar *cur_tag;
2216 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2217 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2218 else
2219 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2221 if (cur_tag != NULL)
2223 *tagname = cur_tag;
2224 return tag_line;
2229 *tagname = g_strdup(_("unknown"));
2230 return -1;
2234 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, guint tag_types)
2236 static gint tag_line = -1;
2237 static gchar *cur_tag = NULL;
2239 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2241 if (doc == NULL) /* reset current function */
2243 current_tag_changed(NULL, -1, -1, 0);
2244 g_free(cur_tag);
2245 cur_tag = g_strdup(_("unknown"));
2246 if (tagname != NULL)
2247 *tagname = cur_tag;
2248 tag_line = -1;
2250 else
2252 gint line = sci_get_current_line(doc->editor->sci);
2253 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2255 if (current_tag_changed(doc, line, fold_level, tag_types))
2257 g_free(cur_tag);
2258 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2260 *tagname = cur_tag;
2263 return tag_line;
2267 /* Sets *tagname to point at the current function or tag name.
2268 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2269 * call to this function.
2270 * Returns: line number of the current tag, or -1 if unknown. */
2271 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2273 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2277 /* same as symbols_get_current_function() but finds class, namespaces and more */
2278 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2280 guint tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2281 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2283 /* Python parser reports imports as namespaces which confuses the scope detection */
2284 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2285 tag_types |= tm_tag_namespace_t;
2287 return get_current_tag_name_cached(doc, tagname, tag_types);
2291 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2293 gint sort_mode = GPOINTER_TO_INT(user_data);
2294 GeanyDocument *doc = document_get_current();
2296 if (ignore_callback)
2297 return;
2299 if (doc != NULL)
2300 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2304 static void on_symbol_tree_menu_show(GtkWidget *widget,
2305 gpointer user_data)
2307 GeanyDocument *doc = document_get_current();
2308 gboolean enable;
2310 enable = doc && doc->has_tags;
2311 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2312 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2313 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2314 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2315 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2316 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2318 if (! doc)
2319 return;
2321 ignore_callback = TRUE;
2323 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2324 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2325 else
2326 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2328 ignore_callback = FALSE;
2332 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2334 gboolean expand = GPOINTER_TO_INT(user_data);
2335 GeanyDocument *doc = document_get_current();
2337 if (! doc)
2338 return;
2340 g_return_if_fail(doc->priv->tag_tree);
2342 if (expand)
2343 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2344 else
2345 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2349 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2351 GtkTreeIter iter;
2352 GtkTreeSelection *selection;
2353 GtkTreeModel *model;
2354 GeanyDocument *doc;
2355 TMTag *tag = NULL;
2357 doc = document_get_current();
2358 if (!doc)
2359 return;
2361 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2362 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2363 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2364 if (tag)
2366 if (widget == symbol_menu.find_in_files)
2367 search_show_find_in_files_dialog_full(tag->name, NULL);
2368 else
2369 search_find_usage(tag->name, tag->name, SCFIND_WHOLEWORD | SCFIND_MATCHCASE,
2370 widget == symbol_menu.find_usage);
2372 tm_tag_unref(tag);
2377 static void create_taglist_popup_menu(void)
2379 GtkWidget *item, *menu;
2381 tv.popup_taglist = menu = gtk_menu_new();
2383 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2384 gtk_widget_show(item);
2385 gtk_container_add(GTK_CONTAINER(menu), item);
2386 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2388 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2389 gtk_widget_show(item);
2390 gtk_container_add(GTK_CONTAINER(menu), item);
2391 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2393 item = gtk_separator_menu_item_new();
2394 gtk_widget_show(item);
2395 gtk_container_add(GTK_CONTAINER(menu), item);
2397 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2398 _("Sort by _Name"));
2399 gtk_widget_show(item);
2400 gtk_container_add(GTK_CONTAINER(menu), item);
2401 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2402 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2404 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2405 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2406 gtk_widget_show(item);
2407 gtk_container_add(GTK_CONTAINER(menu), item);
2408 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2409 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2411 item = gtk_separator_menu_item_new();
2412 gtk_widget_show(item);
2413 gtk_container_add(GTK_CONTAINER(menu), item);
2415 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2416 gtk_widget_show(item);
2417 gtk_container_add(GTK_CONTAINER(menu), item);
2418 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2420 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2421 gtk_widget_show(item);
2422 gtk_container_add(GTK_CONTAINER(menu), item);
2423 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2425 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2426 gtk_widget_show(item);
2427 gtk_container_add(GTK_CONTAINER(menu), item);
2428 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2430 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2432 sidebar_add_common_menu_items(GTK_MENU(menu));
2436 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2438 gchar *f = g_build_filename(app->configdir, "ignore.tags", NULL);
2440 g_return_if_fail(!EMPTY(doc->real_path));
2442 if (utils_str_equal(doc->real_path, f))
2443 load_c_ignore_tags();
2445 g_free(f);
2449 void symbols_init(void)
2451 gchar *f;
2453 create_taglist_popup_menu();
2455 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2456 ui_add_config_file_menu_item(f, NULL, NULL);
2457 g_free(f);
2459 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2463 void symbols_finalize(void)
2465 g_strfreev(html_entities);
2466 g_strfreev(c_tags_ignore);