Merge pull request #1133 from techee/readme_rst
[geany-mirror.git] / src / symbols.c
blob971b3f72c5113d65c85dbf7ab1f83a7bdf5fb886
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 typedef struct
69 gint found_line; /* return: the nearest line found */
70 gint line; /* input: the line to look for */
71 gboolean lower /* input: search only for lines with lower number than @line */;
72 } TreeSearchData;
75 static GPtrArray *top_level_iter_names = NULL;
77 enum
79 ICON_CLASS,
80 ICON_MACRO,
81 ICON_MEMBER,
82 ICON_METHOD,
83 ICON_NAMESPACE,
84 ICON_OTHER,
85 ICON_STRUCT,
86 ICON_VAR,
87 ICON_NONE,
88 N_ICONS = ICON_NONE
91 static struct
93 const gchar *icon_name;
94 GdkPixbuf *pixbuf;
96 symbols_icons[N_ICONS] = {
97 [ICON_CLASS] = { "classviewer-class", NULL },
98 [ICON_MACRO] = { "classviewer-macro", NULL },
99 [ICON_MEMBER] = { "classviewer-member", NULL },
100 [ICON_METHOD] = { "classviewer-method", NULL },
101 [ICON_NAMESPACE] = { "classviewer-namespace", NULL },
102 [ICON_OTHER] = { "classviewer-other", NULL },
103 [ICON_STRUCT] = { "classviewer-struct", NULL },
104 [ICON_VAR] = { "classviewer-var", NULL },
107 static struct
109 GtkWidget *expand_all;
110 GtkWidget *collapse_all;
111 GtkWidget *sort_by_name;
112 GtkWidget *sort_by_appearance;
113 GtkWidget *find_usage;
114 GtkWidget *find_doc_usage;
115 GtkWidget *find_in_files;
117 symbol_menu;
119 static void load_user_tags(GeanyFiletypeID ft_id);
121 /* get the tags_ignore list, exported by tagmanager's options.c */
122 extern gchar **c_tags_ignore;
124 /* ignore certain tokens when parsing C-like syntax.
125 * Also works for reloading. */
126 static void load_c_ignore_tags(void)
128 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
129 gchar *content;
131 if (g_file_get_contents(path, &content, NULL, NULL))
133 /* historically we ignore the glib _DECLS for tag generation */
134 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
136 g_strfreev(c_tags_ignore);
137 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
138 g_free(content);
140 g_free(path);
144 void symbols_reload_config_files(void)
146 load_c_ignore_tags();
150 static gsize get_tag_count(void)
152 GPtrArray *tags = tm_get_workspace()->global_tags;
153 gsize count = tags ? tags->len : 0;
155 return count;
159 /* wrapper for tm_workspace_load_global_tags().
160 * note that the tag count only counts new global tags added - if a tag has the same name,
161 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
162 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
164 gboolean result;
165 gsize old_tag_count = get_tag_count();
167 result = tm_workspace_load_global_tags(tags_file, ft->lang);
168 if (result)
170 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
171 (guint) (get_tag_count() - old_tag_count));
173 return result;
177 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
178 * This provides autocompletion, calltips, etc. */
179 void symbols_global_tags_loaded(guint file_type_idx)
181 /* load ignore list for C/C++ parser */
182 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
183 c_tags_ignore == NULL)
185 load_c_ignore_tags();
188 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
189 return;
191 /* load config in case of custom filetypes */
192 filetypes_load_config(file_type_idx, FALSE);
194 load_user_tags(file_type_idx);
196 switch (file_type_idx)
198 case GEANY_FILETYPES_CPP:
199 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
200 break;
201 case GEANY_FILETYPES_PHP:
202 symbols_global_tags_loaded(GEANY_FILETYPES_HTML); /* load HTML global tags */
203 break;
208 GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
210 guint j;
211 TMTag *tag;
212 GString *s = NULL;
213 GPtrArray *typedefs;
214 TMParserType tag_lang;
216 if (global)
217 typedefs = app->tm_workspace->global_typename_array;
218 else
219 typedefs = app->tm_workspace->typename_array;
221 if ((typedefs) && (typedefs->len > 0))
223 const gchar *last_name = "";
225 s = g_string_sized_new(typedefs->len * 10);
226 for (j = 0; j < typedefs->len; ++j)
228 tag = TM_TAG(typedefs->pdata[j]);
229 tag_lang = tag->lang;
231 if (tag->name && tm_tag_langs_compatible(lang, tag_lang) &&
232 strcmp(tag->name, last_name) != 0)
234 if (j != 0)
235 g_string_append_c(s, ' ');
236 g_string_append(s, tag->name);
237 last_name = tag->name;
241 return s;
245 /** Gets the context separator used by the tag manager for a particular file
246 * type.
247 * @param ft_id File type identifier.
248 * @return The context separator string.
250 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
251 * without a context separator.
253 * @since 0.19
255 GEANY_API_SYMBOL
256 const gchar *symbols_get_context_separator(gint ft_id)
258 return tm_tag_context_separator(filetypes[ft_id]->lang);
262 /* sort by name, then line */
263 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
265 gint ret;
267 if (tag_a == NULL || tag_b == NULL)
268 return 0;
270 if (tag_a->name == NULL)
271 return -(tag_a->name != tag_b->name);
273 if (tag_b->name == NULL)
274 return tag_a->name != tag_b->name;
276 ret = strcmp(tag_a->name, tag_b->name);
277 if (ret == 0)
279 return tag_a->line - tag_b->line;
281 return ret;
285 /* sort by line, then scope */
286 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
288 const TMTag *tag_a = TM_TAG(a);
289 const TMTag *tag_b = TM_TAG(b);
290 gint ret;
292 if (a == NULL || b == NULL)
293 return 0;
295 ret = tag_a->line - tag_b->line;
296 if (ret == 0)
298 if (tag_a->scope == NULL)
299 return -(tag_a->scope != tag_b->scope);
300 if (tag_b->scope == NULL)
301 return tag_a->scope != tag_b->scope;
302 else
303 return strcmp(tag_a->scope, tag_b->scope);
305 return ret;
309 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
311 GList *tag_names = NULL;
312 guint i;
314 g_return_val_if_fail(doc, NULL);
316 if (! doc->tm_file || ! doc->tm_file->tags_array)
317 return NULL;
319 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
321 TMTag *tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
323 if (G_UNLIKELY(tag == NULL))
324 return NULL;
326 if (tag->type & tag_types)
328 tag_names = g_list_prepend(tag_names, tag);
331 tag_names = g_list_sort(tag_names, compare_symbol_lines);
332 return tag_names;
336 /* amount of types in the symbol list (currently max. 8 are used) */
337 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
339 struct TreeviewSymbols
341 GtkTreeIter tag_function;
342 GtkTreeIter tag_class;
343 GtkTreeIter tag_macro;
344 GtkTreeIter tag_member;
345 GtkTreeIter tag_variable;
346 GtkTreeIter tag_externvar;
347 GtkTreeIter tag_namespace;
348 GtkTreeIter tag_struct;
349 GtkTreeIter tag_interface;
350 GtkTreeIter tag_type;
351 GtkTreeIter tag_other;
352 } tv_iters;
355 static void init_tag_iters(void)
357 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
358 * filetypes(e.g. config file to Python crashes Geany without this) */
359 tv_iters.tag_function.stamp = -1;
360 tv_iters.tag_class.stamp = -1;
361 tv_iters.tag_member.stamp = -1;
362 tv_iters.tag_macro.stamp = -1;
363 tv_iters.tag_variable.stamp = -1;
364 tv_iters.tag_externvar.stamp = -1;
365 tv_iters.tag_namespace.stamp = -1;
366 tv_iters.tag_struct.stamp = -1;
367 tv_iters.tag_interface.stamp = -1;
368 tv_iters.tag_type.stamp = -1;
369 tv_iters.tag_other.stamp = -1;
373 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
375 static GtkIconTheme *icon_theme = NULL;
376 static gint x = -1;
378 if (G_UNLIKELY(x < 0))
380 gint dummy;
381 icon_theme = gtk_icon_theme_get_default();
382 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
384 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
388 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
390 GtkTreeModel *model = GTK_TREE_MODEL(store);
392 if (!gtk_tree_model_get_iter_first(model, iter))
393 return FALSE;
396 gchar *candidate;
398 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
399 /* FIXME: what if 2 different items have the same name?
400 * this should never happen, but might be caused by a typo in a translation */
401 if (utils_str_equal(candidate, title))
403 g_free(candidate);
404 return TRUE;
406 else
407 g_free(candidate);
409 while (gtk_tree_model_iter_next(model, iter));
411 return FALSE;
415 /* Adds symbol list groups in (iter*, title) pairs.
416 * The list must be ended with NULL. */
417 static void G_GNUC_NULL_TERMINATED
418 tag_list_add_groups(GtkTreeStore *tree_store, ...)
420 va_list args;
421 GtkTreeIter *iter;
423 g_return_if_fail(top_level_iter_names);
425 va_start(args, tree_store);
426 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
428 gchar *title = va_arg(args, gchar*);
429 guint icon_id = va_arg(args, guint);
430 GdkPixbuf *icon = NULL;
432 if (icon_id < N_ICONS)
433 icon = symbols_icons[icon_id].pixbuf;
435 g_assert(title != NULL);
436 g_ptr_array_add(top_level_iter_names, title);
438 if (!find_toplevel_iter(tree_store, iter, title))
439 gtk_tree_store_append(tree_store, iter, NULL);
441 if (icon)
442 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
443 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
445 va_end(args);
449 static void add_top_level_items(GeanyDocument *doc)
451 GeanyFiletypeID ft_id = doc->file_type->id;
452 GtkTreeStore *tag_store = doc->priv->tag_store;
454 if (top_level_iter_names == NULL)
455 top_level_iter_names = g_ptr_array_new();
456 else
457 g_ptr_array_set_size(top_level_iter_names, 0);
459 init_tag_iters();
461 switch (ft_id)
463 case GEANY_FILETYPES_DIFF:
465 tag_list_add_groups(tag_store,
466 &(tv_iters.tag_function), _("Files"), ICON_NONE, NULL);
467 break;
469 case GEANY_FILETYPES_DOCBOOK:
471 tag_list_add_groups(tag_store,
472 &(tv_iters.tag_function), _("Chapter"), ICON_NONE,
473 &(tv_iters.tag_class), _("Section"), ICON_NONE,
474 &(tv_iters.tag_member), _("Sect1"), ICON_NONE,
475 &(tv_iters.tag_macro), _("Sect2"), ICON_NONE,
476 &(tv_iters.tag_variable), _("Sect3"), ICON_NONE,
477 &(tv_iters.tag_struct), _("Appendix"), ICON_NONE,
478 &(tv_iters.tag_other), _("Other"), ICON_NONE,
479 NULL);
480 break;
482 case GEANY_FILETYPES_HASKELL:
483 tag_list_add_groups(tag_store,
484 &tv_iters.tag_namespace, _("Module"), ICON_NONE,
485 &tv_iters.tag_type, _("Types"), ICON_NONE,
486 &tv_iters.tag_macro, _("Type constructors"), ICON_NONE,
487 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
488 NULL);
489 break;
490 case GEANY_FILETYPES_COBOL:
491 tag_list_add_groups(tag_store,
492 &tv_iters.tag_class, _("Program"), ICON_CLASS,
493 &tv_iters.tag_function, _("File"), ICON_METHOD,
494 &tv_iters.tag_namespace, _("Sections"), ICON_NAMESPACE,
495 &tv_iters.tag_macro, _("Paragraph"), ICON_OTHER,
496 &tv_iters.tag_struct, _("Group"), ICON_STRUCT,
497 &tv_iters.tag_variable, _("Data"), ICON_VAR,
498 NULL);
499 break;
500 case GEANY_FILETYPES_CONF:
501 tag_list_add_groups(tag_store,
502 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
503 &tv_iters.tag_macro, _("Keys"), ICON_VAR,
504 NULL);
505 break;
506 case GEANY_FILETYPES_NSIS:
507 tag_list_add_groups(tag_store,
508 &tv_iters.tag_namespace, _("Sections"), ICON_OTHER,
509 &tv_iters.tag_function, _("Functions"), ICON_METHOD,
510 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
511 NULL);
512 break;
513 case GEANY_FILETYPES_LATEX:
515 tag_list_add_groups(tag_store,
516 &(tv_iters.tag_function), _("Command"), ICON_NONE,
517 &(tv_iters.tag_class), _("Environment"), ICON_NONE,
518 &(tv_iters.tag_member), _("Section"), ICON_NONE,
519 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
520 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
521 &(tv_iters.tag_struct), _("Label"), ICON_NONE,
522 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
523 &(tv_iters.tag_other), _("Other"), ICON_NONE,
524 NULL);
525 break;
527 case GEANY_FILETYPES_MATLAB:
529 tag_list_add_groups(tag_store,
530 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
531 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
532 NULL);
533 break;
535 case GEANY_FILETYPES_ABAQUS:
537 tag_list_add_groups(tag_store,
538 &(tv_iters.tag_class), _("Parts"), ICON_NONE,
539 &(tv_iters.tag_member), _("Assembly"), ICON_NONE,
540 &(tv_iters.tag_namespace), _("Steps"), ICON_NONE,
541 NULL);
542 break;
544 case GEANY_FILETYPES_R:
546 tag_list_add_groups(tag_store,
547 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
548 &(tv_iters.tag_other), _("Other"), ICON_NONE,
549 NULL);
550 break;
552 case GEANY_FILETYPES_RUST:
554 tag_list_add_groups(tag_store,
555 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
556 &(tv_iters.tag_struct), _("Structures"), ICON_STRUCT,
557 &(tv_iters.tag_interface), _("Traits"), ICON_CLASS,
558 &(tv_iters.tag_class), _("Implementations"), ICON_CLASS,
559 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
560 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
561 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
562 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO,
563 &(tv_iters.tag_member), _("Methods"), ICON_MEMBER,
564 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
565 NULL);
566 break;
568 case GEANY_FILETYPES_GO:
570 tag_list_add_groups(tag_store,
571 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
572 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
573 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
574 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
575 &(tv_iters.tag_type), _("Types"), ICON_STRUCT,
576 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
577 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
578 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
579 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
580 NULL);
581 break;
583 case GEANY_FILETYPES_PERL:
585 tag_list_add_groups(tag_store,
586 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
587 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
588 &(tv_iters.tag_macro), _("Labels"), ICON_NONE,
589 &(tv_iters.tag_type), _("Constants"), ICON_NONE,
590 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
591 NULL);
592 break;
594 case GEANY_FILETYPES_PHP:
595 case GEANY_FILETYPES_ZEPHIR:
597 tag_list_add_groups(tag_store,
598 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
599 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
600 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
601 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
602 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
603 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
604 &(tv_iters.tag_struct), _("Traits"), ICON_STRUCT,
605 NULL);
606 break;
608 case GEANY_FILETYPES_HTML:
610 tag_list_add_groups(tag_store,
611 &(tv_iters.tag_function), _("Functions"), ICON_NONE,
612 &(tv_iters.tag_member), _("Anchors"), ICON_NONE,
613 &(tv_iters.tag_namespace), _("H1 Headings"), ICON_NONE,
614 &(tv_iters.tag_class), _("H2 Headings"), ICON_NONE,
615 &(tv_iters.tag_variable), _("H3 Headings"), ICON_NONE,
616 NULL);
617 break;
619 case GEANY_FILETYPES_CSS:
621 tag_list_add_groups(tag_store,
622 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
623 &(tv_iters.tag_variable), _("ID Selectors"), ICON_VAR,
624 &(tv_iters.tag_struct), _("Type Selectors"), ICON_STRUCT, NULL);
625 break;
627 case GEANY_FILETYPES_REST:
628 case GEANY_FILETYPES_TXT2TAGS:
629 case GEANY_FILETYPES_ABC:
631 tag_list_add_groups(tag_store,
632 &(tv_iters.tag_namespace), _("Chapter"), ICON_NONE,
633 &(tv_iters.tag_member), _("Section"), ICON_NONE,
634 &(tv_iters.tag_macro), _("Subsection"), ICON_NONE,
635 &(tv_iters.tag_variable), _("Subsubsection"), ICON_NONE,
636 NULL);
637 break;
639 case GEANY_FILETYPES_ASCIIDOC:
641 tag_list_add_groups(tag_store,
642 &(tv_iters.tag_namespace), _("Document"), ICON_NONE,
643 &(tv_iters.tag_member), _("Section Level 1"), ICON_NONE,
644 &(tv_iters.tag_macro), _("Section Level 2"), ICON_NONE,
645 &(tv_iters.tag_variable), _("Section Level 3"), ICON_NONE,
646 &(tv_iters.tag_struct), _("Section Level 4"), ICON_NONE,
647 NULL);
648 break;
650 case GEANY_FILETYPES_RUBY:
652 tag_list_add_groups(tag_store,
653 &(tv_iters.tag_namespace), _("Modules"), ICON_NAMESPACE,
654 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
655 &(tv_iters.tag_member), _("Singletons"), ICON_STRUCT,
656 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
657 NULL);
658 break;
660 case GEANY_FILETYPES_TCL:
662 tag_list_add_groups(tag_store,
663 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE,
664 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
665 &(tv_iters.tag_member), _("Methods"), ICON_METHOD,
666 &(tv_iters.tag_function), _("Procedures"), ICON_OTHER,
667 NULL);
668 break;
670 case GEANY_FILETYPES_PYTHON:
672 tag_list_add_groups(tag_store,
673 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
674 &(tv_iters.tag_member), _("Methods"), ICON_MACRO,
675 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
676 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
677 &(tv_iters.tag_externvar), _("Imports"), ICON_NAMESPACE,
678 NULL);
679 break;
681 case GEANY_FILETYPES_VHDL:
683 tag_list_add_groups(tag_store,
684 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
685 &(tv_iters.tag_class), _("Entities"), ICON_CLASS,
686 &(tv_iters.tag_struct), _("Architectures"), ICON_STRUCT,
687 &(tv_iters.tag_type), _("Types"), ICON_OTHER,
688 &(tv_iters.tag_function), _("Functions / Procedures"), ICON_METHOD,
689 &(tv_iters.tag_variable), _("Variables / Signals"), ICON_VAR,
690 &(tv_iters.tag_member), _("Processes / Blocks / Components"), ICON_MEMBER,
691 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
692 NULL);
693 break;
695 case GEANY_FILETYPES_VERILOG:
697 tag_list_add_groups(tag_store,
698 &(tv_iters.tag_type), _("Events"), ICON_MACRO,
699 &(tv_iters.tag_class), _("Modules"), ICON_CLASS,
700 &(tv_iters.tag_function), _("Functions / Tasks"), ICON_METHOD,
701 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
702 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
703 NULL);
704 break;
706 case GEANY_FILETYPES_JAVA:
708 tag_list_add_groups(tag_store,
709 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
710 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
711 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
712 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
713 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
714 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
715 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
716 NULL);
717 break;
719 case GEANY_FILETYPES_AS:
721 tag_list_add_groups(tag_store,
722 &(tv_iters.tag_namespace), _("Package"), ICON_NAMESPACE,
723 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
724 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
725 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
726 &(tv_iters.tag_member), _("Properties"), ICON_MEMBER,
727 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
728 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
729 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
730 NULL);
731 break;
733 case GEANY_FILETYPES_HAXE:
735 tag_list_add_groups(tag_store,
736 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
737 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
738 &(tv_iters.tag_function), _("Methods"), ICON_METHOD,
739 &(tv_iters.tag_type), _("Types"), ICON_MACRO,
740 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
741 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
742 NULL);
743 break;
745 case GEANY_FILETYPES_BASIC:
747 tag_list_add_groups(tag_store,
748 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
749 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
750 &(tv_iters.tag_macro), _("Constants"), ICON_MACRO,
751 &(tv_iters.tag_struct), _("Types"), ICON_NAMESPACE,
752 &(tv_iters.tag_namespace), _("Labels"), ICON_MEMBER,
753 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
754 NULL);
755 break;
757 case GEANY_FILETYPES_F77:
758 case GEANY_FILETYPES_FORTRAN:
760 tag_list_add_groups(tag_store,
761 &(tv_iters.tag_namespace), _("Module"), ICON_CLASS,
762 &(tv_iters.tag_struct), _("Programs"), ICON_CLASS,
763 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
764 &(tv_iters.tag_function), _("Functions / Subroutines"), ICON_METHOD,
765 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
766 &(tv_iters.tag_class), _("Types"), ICON_CLASS,
767 &(tv_iters.tag_member), _("Components"), ICON_MEMBER,
768 &(tv_iters.tag_macro), _("Blocks"), ICON_MEMBER,
769 &(tv_iters.tag_type), _("Enums"), ICON_STRUCT,
770 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
771 NULL);
772 break;
774 case GEANY_FILETYPES_ASM:
776 tag_list_add_groups(tag_store,
777 &(tv_iters.tag_namespace), _("Labels"), ICON_NAMESPACE,
778 &(tv_iters.tag_function), _("Macros"), ICON_METHOD,
779 &(tv_iters.tag_macro), _("Defines"), ICON_MACRO,
780 &(tv_iters.tag_struct), _("Types"), ICON_STRUCT,
781 NULL);
782 break;
784 case GEANY_FILETYPES_MAKE:
785 tag_list_add_groups(tag_store,
786 &tv_iters.tag_function, _("Targets"), ICON_METHOD,
787 &tv_iters.tag_macro, _("Macros"), ICON_MACRO,
788 NULL);
789 break;
790 case GEANY_FILETYPES_SQL:
792 tag_list_add_groups(tag_store,
793 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
794 &(tv_iters.tag_namespace), _("Procedures"), ICON_NAMESPACE,
795 &(tv_iters.tag_struct), _("Indexes"), ICON_STRUCT,
796 &(tv_iters.tag_class), _("Tables"), ICON_CLASS,
797 &(tv_iters.tag_macro), _("Triggers"), ICON_MACRO,
798 &(tv_iters.tag_member), _("Views"), ICON_VAR,
799 &(tv_iters.tag_other), _("Other"), ICON_OTHER,
800 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
801 NULL);
802 break;
804 case GEANY_FILETYPES_D:
805 default:
807 if (ft_id == GEANY_FILETYPES_D)
808 tag_list_add_groups(tag_store,
809 &(tv_iters.tag_namespace), _("Module"), ICON_NONE, NULL);
810 else
811 tag_list_add_groups(tag_store,
812 &(tv_iters.tag_namespace), _("Namespaces"), ICON_NAMESPACE, NULL);
814 tag_list_add_groups(tag_store,
815 &(tv_iters.tag_class), _("Classes"), ICON_CLASS,
816 &(tv_iters.tag_interface), _("Interfaces"), ICON_STRUCT,
817 &(tv_iters.tag_function), _("Functions"), ICON_METHOD,
818 &(tv_iters.tag_member), _("Members"), ICON_MEMBER,
819 &(tv_iters.tag_struct), _("Structs"), ICON_STRUCT,
820 &(tv_iters.tag_type), _("Typedefs / Enums"), ICON_STRUCT,
821 NULL);
823 if (ft_id != GEANY_FILETYPES_D)
825 tag_list_add_groups(tag_store,
826 &(tv_iters.tag_macro), _("Macros"), ICON_MACRO, NULL);
828 tag_list_add_groups(tag_store,
829 &(tv_iters.tag_variable), _("Variables"), ICON_VAR,
830 &(tv_iters.tag_externvar), _("Extern Variables"), ICON_VAR,
831 &(tv_iters.tag_other), _("Other"), ICON_OTHER, NULL);
837 /* removes toplevel items that have no children */
838 static void hide_empty_rows(GtkTreeStore *store)
840 GtkTreeIter iter;
841 gboolean cont = TRUE;
843 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
844 return; /* stop when first iter is invalid, i.e. no elements */
846 while (cont)
848 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
849 cont = gtk_tree_store_remove(store, &iter);
850 else
851 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
856 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
858 gchar *utf8_name;
859 const gchar *scope = tag->scope;
860 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
861 gboolean doc_is_utf8 = FALSE;
863 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
864 * for None at this point completely */
865 if (utils_str_equal(doc->encoding, "UTF-8") ||
866 utils_str_equal(doc->encoding, "None"))
867 doc_is_utf8 = TRUE;
868 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
869 * plugin might have called tm_source_file_update(), so check to be sure */
870 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
872 if (! doc_is_utf8)
873 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
874 -1, doc->encoding, TRUE);
875 else
876 utf8_name = tag->name;
878 if (utf8_name == NULL)
879 return NULL;
881 if (! buffer)
882 buffer = g_string_new(NULL);
883 else
884 g_string_truncate(buffer, 0);
886 /* check first char of scope is a wordchar */
887 if (!found_parent && scope &&
888 strpbrk(scope, GEANY_WORDCHARS) == scope)
890 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
892 g_string_append(buffer, scope);
893 g_string_append(buffer, sep);
895 g_string_append(buffer, utf8_name);
897 if (! doc_is_utf8)
898 g_free(utf8_name);
900 g_string_append_printf(buffer, " [%lu]", tag->line);
902 return buffer->str;
906 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
908 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
910 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
911 * for None at this point completely */
912 if (utf8_name != NULL &&
913 ! utils_str_equal(doc->encoding, "UTF-8") &&
914 ! utils_str_equal(doc->encoding, "None"))
916 SETPTR(utf8_name,
917 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
920 return utf8_name;
924 /* find the last word in "foo::bar::blah", e.g. "blah" */
925 static const gchar *get_parent_name(const TMTag *tag, GeanyFiletypeID ft_id)
927 const gchar *scope = tag->scope;
928 const gchar *separator = symbols_get_context_separator(ft_id);
929 const gchar *str, *ptr;
931 if (!scope)
932 return NULL;
934 str = scope;
936 while (1)
938 ptr = strstr(str, separator);
939 if (ptr)
941 str = ptr + strlen(separator);
943 else
944 break;
947 return !EMPTY(str) ? str : NULL;
951 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type)
953 GtkTreeIter *iter = NULL;
955 switch (tag_type)
957 case tm_tag_prototype_t:
958 case tm_tag_method_t:
959 case tm_tag_function_t:
961 iter = &tv_iters.tag_function;
962 break;
964 case tm_tag_externvar_t:
966 iter = &tv_iters.tag_externvar;
967 break;
969 case tm_tag_macro_t:
970 case tm_tag_macro_with_arg_t:
972 iter = &tv_iters.tag_macro;
973 break;
975 case tm_tag_class_t:
977 iter = &tv_iters.tag_class;
978 break;
980 case tm_tag_member_t:
981 case tm_tag_field_t:
983 iter = &tv_iters.tag_member;
984 break;
986 case tm_tag_typedef_t:
987 case tm_tag_enum_t:
989 iter = &tv_iters.tag_type;
990 break;
992 case tm_tag_union_t:
993 case tm_tag_struct_t:
995 iter = &tv_iters.tag_struct;
996 break;
998 case tm_tag_interface_t:
999 iter = &tv_iters.tag_interface;
1000 break;
1001 case tm_tag_variable_t:
1003 iter = &tv_iters.tag_variable;
1004 break;
1006 case tm_tag_namespace_t:
1007 case tm_tag_package_t:
1009 iter = &tv_iters.tag_namespace;
1010 break;
1012 default:
1014 iter = &tv_iters.tag_other;
1017 if (G_LIKELY(iter->stamp != -1))
1018 return iter;
1019 else
1020 return NULL;
1024 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1026 GdkPixbuf *icon = NULL;
1028 if (parent == &tv_iters.tag_other)
1030 return g_object_ref(symbols_icons[ICON_VAR].pixbuf);
1032 /* copy parent icon */
1033 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1034 SYMBOLS_COLUMN_ICON, &icon, -1);
1035 return icon;
1039 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1041 const TMTag *t1 = v1;
1042 const TMTag *t2 = v2;
1044 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1045 utils_str_equal(t1->scope, t2->scope) &&
1046 /* include arglist in match to support e.g. C++ overloading */
1047 utils_str_equal(t1->arglist, t2->arglist));
1051 /* inspired from g_str_hash() */
1052 static guint tag_hash(gconstpointer v)
1054 const TMTag *tag = v;
1055 const gchar *p;
1056 guint32 h = 5381;
1058 h = (h << 5) + h + tag->type;
1059 for (p = tag->name; *p != '\0'; p++)
1060 h = (h << 5) + h + *p;
1061 if (tag->scope)
1063 for (p = tag->scope; *p != '\0'; p++)
1064 h = (h << 5) + h + *p;
1066 /* for e.g. C++ overloading */
1067 if (tag->arglist)
1069 for (p = tag->arglist; *p != '\0'; p++)
1070 h = (h << 5) + h + *p;
1073 return h;
1077 /* like gtk_tree_view_expand_to_path() but with an iter */
1078 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1080 GtkTreeModel *model = gtk_tree_view_get_model(view);
1081 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1083 gtk_tree_view_expand_to_path(view, path);
1084 gtk_tree_path_free(path);
1088 /* like gtk_tree_store_remove() but finds the next iter at any level */
1089 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1091 GtkTreeIter parent;
1092 gboolean has_parent;
1093 gboolean cont;
1095 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1096 cont = gtk_tree_store_remove(store, iter);
1097 /* if there is no next at this level but there is a parent iter, continue from it */
1098 if (! cont && has_parent)
1100 *iter = parent;
1101 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
1104 return cont;
1108 static gint tree_search_func(gconstpointer key, gpointer user_data)
1110 TreeSearchData *data = user_data;
1111 gint parent_line = GPOINTER_TO_INT(key);
1112 gboolean new_nearest;
1114 if (data->found_line == -1)
1115 data->found_line = parent_line; /* initial value */
1117 new_nearest = ABS(data->line - parent_line) < ABS(data->line - data->found_line);
1119 if (parent_line > data->line)
1121 if (new_nearest && !data->lower)
1122 data->found_line = parent_line;
1123 return -1;
1126 if (new_nearest)
1127 data->found_line = parent_line;
1129 if (parent_line < data->line)
1130 return 1;
1132 return 0;
1136 static gint tree_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
1138 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
1142 static void parents_table_tree_value_free(gpointer data)
1144 g_slice_free(GtkTreeIter, data);
1148 /* adds a new element in the parent table if its key is known. */
1149 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1150 const GtkTreeIter *iter)
1152 GTree *tree;
1153 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &tree) &&
1154 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1156 if (!tree)
1158 tree = g_tree_new_full(tree_cmp, NULL, NULL, parents_table_tree_value_free);
1159 g_hash_table_insert(table, tag->name, tree);
1162 g_tree_insert(tree, GINT_TO_POINTER(tag->line), g_slice_dup(GtkTreeIter, iter));
1167 static GtkTreeIter *parents_table_lookup(GHashTable *table, const gchar *name, guint line)
1169 GtkTreeIter *parent_search = NULL;
1170 GTree *tree;
1172 tree = g_hash_table_lookup(table, name);
1173 if (tree)
1175 TreeSearchData user_data = {-1, line, TRUE};
1177 /* search parent candidates for the one with the nearest
1178 * line number which is lower than the tag's line number */
1179 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
1180 parent_search = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
1183 return parent_search;
1187 static void parents_table_value_free(gpointer data)
1189 GTree *tree = data;
1190 if (tree)
1191 g_tree_destroy(tree);
1195 /* inserts a @data in @table on key @tag.
1196 * previous data is not overwritten if the key is duplicated, but rather the
1197 * two values are kept in a list
1199 * table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
1200 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1202 GTree *tree = g_hash_table_lookup(table, tag);
1203 if (!tree)
1205 tree = g_tree_new_full(tree_cmp, NULL, NULL, NULL);
1206 g_hash_table_insert(table, tag, tree);
1208 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
1209 list = g_list_prepend(list, data);
1210 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
1214 /* looks up the entry in @table that best matches @tag.
1215 * if there is more than one candidate, the one that has closest line position to @tag is chosen */
1216 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1218 TreeSearchData user_data = {-1, tag->line, FALSE};
1219 GTree *tree = g_hash_table_lookup(table, tag);
1221 if (tree)
1223 GList *list;
1225 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
1226 list = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
1227 /* return the first value in the list - we don't care which of the
1228 * tags with identical names defined on the same line we get */
1229 if (list)
1230 return list->data;
1232 return NULL;
1236 /* removes the element at @tag from @table.
1237 * @tag must be the exact pointer used at insertion time */
1238 static void tags_table_remove(GHashTable *table, TMTag *tag)
1240 GTree *tree = g_hash_table_lookup(table, tag);
1241 if (tree)
1243 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
1244 if (list)
1246 GList *node;
1247 /* should always be the first element as we returned the first one in
1248 * tags_table_lookup() */
1249 foreach_list(node, list)
1251 if (((GList *) node->data)->data == tag)
1252 break;
1254 list = g_list_delete_link(list, node);
1255 if (!list)
1256 g_tree_remove(tree, GINT_TO_POINTER(tag->line));
1257 else
1258 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
1264 static gboolean tags_table_tree_value_free(gpointer key, gpointer value, gpointer data)
1266 GList *list = value;
1267 g_list_free(list);
1268 return FALSE;
1272 static void tags_table_value_free(gpointer data)
1274 GTree *tree = data;
1275 if (tree)
1277 /* free any leftover elements. note that we can't register a value_free_func when
1278 * creating the tree because we only want to free it when destroying the tree,
1279 * not when inserting a duplicate (we handle this manually) */
1280 g_tree_foreach(tree, tags_table_tree_value_free, NULL);
1281 g_tree_destroy(tree);
1287 * Updates the tag tree for a document with the tags in *list.
1288 * @param doc a document
1289 * @param tags a pointer to a GList* holding the tags to add/update. This
1290 * list may be updated, removing updated elements.
1292 * The update is done in two passes:
1293 * 1) walking the current tree, update tags that still exist and remove the
1294 * obsolescent ones;
1295 * 2) walking the remaining (non updated) tags, adds them in the list.
1297 * For better performances, we use 2 hash tables:
1298 * - one containing all the tags for lookup in the first pass (actually stores a
1299 * reference in the tags list for removing it efficiently), avoiding list search
1300 * on each tag;
1301 * - the other holding "tag-name":row references for tags having children, used to
1302 * lookup for a parent in both passes, avoiding tree traversal.
1304 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1306 GtkTreeStore *store = doc->priv->tag_store;
1307 GtkTreeModel *model = GTK_TREE_MODEL(store);
1308 GHashTable *parents_table;
1309 GHashTable *tags_table;
1310 GtkTreeIter iter;
1311 gboolean cont;
1312 GList *item;
1314 /* Build hash tables holding tags and parents */
1315 /* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>> */
1316 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, parents_table_value_free);
1317 /* tags table is another representation of the @tags list,
1318 * GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
1319 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, tags_table_value_free);
1320 foreach_list(item, *tags)
1322 TMTag *tag = item->data;
1323 const gchar *name;
1325 tags_table_insert(tags_table, tag, item);
1327 name = get_parent_name(tag, doc->file_type->id);
1328 if (name)
1329 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1332 /* First pass, update existing rows or delete them.
1333 * It is OK to delete them since we walk top down so we would remove
1334 * parents before checking for their children, thus never implicitly
1335 * deleting an updated child */
1336 cont = gtk_tree_model_get_iter_first(model, &iter);
1337 while (cont)
1339 TMTag *tag;
1341 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1342 if (! tag) /* most probably a toplevel, skip it */
1343 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1344 else
1346 GList *found_item;
1348 found_item = tags_table_lookup(tags_table, tag);
1349 if (! found_item) /* tag doesn't exist, remove it */
1350 cont = tree_store_remove_row(store, &iter);
1351 else /* tag still exist, update it */
1353 const gchar *parent_name;
1354 TMTag *found = found_item->data;
1356 parent_name = get_parent_name(found, doc->file_type->id);
1357 /* if parent is unknown, ignore it */
1358 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1359 parent_name = NULL;
1361 if (!tm_tags_equal(tag, found))
1363 const gchar *name;
1364 gchar *tooltip;
1366 /* only update fields that (can) have changed (name that holds line
1367 * number, tooltip, and the tag itself) */
1368 name = get_symbol_name(doc, found, parent_name != NULL);
1369 tooltip = get_symbol_tooltip(doc, found);
1370 gtk_tree_store_set(store, &iter,
1371 SYMBOLS_COLUMN_NAME, name,
1372 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1373 SYMBOLS_COLUMN_TAG, found,
1374 -1);
1375 g_free(tooltip);
1378 update_parents_table(parents_table, found, parent_name, &iter);
1380 /* remove the updated tag from the table and list */
1381 tags_table_remove(tags_table, found);
1382 *tags = g_list_delete_link(*tags, found_item);
1384 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
1387 tm_tag_unref(tag);
1391 /* Second pass, now we have a tree cleaned up from invalid rows,
1392 * we simply add new ones */
1393 foreach_list (item, *tags)
1395 TMTag *tag = item->data;
1396 GtkTreeIter *parent;
1398 parent = get_tag_type_iter(tag->type);
1399 if (G_UNLIKELY(! parent))
1400 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1401 else
1403 gboolean expand;
1404 const gchar *name;
1405 const gchar *parent_name;
1406 gchar *tooltip;
1407 GdkPixbuf *icon = get_child_icon(store, parent);
1409 parent_name = get_parent_name(tag, doc->file_type->id);
1410 if (parent_name)
1412 GtkTreeIter *parent_search = parents_table_lookup(parents_table, parent_name, tag->line);
1414 if (parent_search)
1415 parent = parent_search;
1416 else
1417 parent_name = NULL;
1420 /* only expand to the iter if the parent was empty, otherwise we let the
1421 * folding as it was before (already expanded, or closed by the user) */
1422 expand = ! gtk_tree_model_iter_has_child(model, parent);
1424 /* insert the new element */
1425 name = get_symbol_name(doc, tag, parent_name != NULL);
1426 tooltip = get_symbol_tooltip(doc, tag);
1427 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1428 SYMBOLS_COLUMN_NAME, name,
1429 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1430 SYMBOLS_COLUMN_ICON, icon,
1431 SYMBOLS_COLUMN_TAG, tag,
1432 -1);
1433 g_free(tooltip);
1434 if (G_LIKELY(icon))
1435 g_object_unref(icon);
1437 update_parents_table(parents_table, tag, parent_name, &iter);
1439 if (expand)
1440 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1444 g_hash_table_destroy(parents_table);
1445 g_hash_table_destroy(tags_table);
1449 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1450 * is not stable, so the order is already lost. */
1451 static gint compare_top_level_names(const gchar *a, const gchar *b)
1453 guint i;
1454 const gchar *name;
1456 /* This should never happen as it would mean that two or more top
1457 * level items have the same name but it can happen by typos in the translations. */
1458 if (utils_str_equal(a, b))
1459 return 1;
1461 foreach_ptr_array(name, i, top_level_iter_names)
1463 if (utils_str_equal(name, a))
1464 return -1;
1465 if (utils_str_equal(name, b))
1466 return 1;
1468 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1469 return 0;
1473 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1474 GtkTreeIter *iter)
1476 /* if the tag has a parent tag, it should be at depth >= 2 */
1477 return !EMPTY(tag->scope) &&
1478 gtk_tree_store_iter_depth(store, iter) == 1;
1482 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1483 gpointer user_data)
1485 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1486 TMTag *tag_a, *tag_b;
1487 gint cmp;
1489 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1490 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1492 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1493 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1494 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1495 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1497 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1498 compare_symbol_lines(tag_a, tag_b);
1500 else
1502 gchar *astr, *bstr;
1504 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1505 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1507 /* if a is toplevel, b must be also */
1508 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1510 cmp = compare_top_level_names(astr, bstr);
1512 else
1514 /* this is what g_strcmp0() does */
1515 if (! astr)
1516 cmp = -(astr != bstr);
1517 else if (! bstr)
1518 cmp = astr != bstr;
1519 else
1521 cmp = strcmp(astr, bstr);
1523 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1524 if (tag_a && tag_b)
1525 if (!sort_by_name ||
1526 (utils_str_equal(tag_a->name, tag_b->name) &&
1527 utils_str_equal(tag_a->scope, tag_b->scope)))
1528 cmp = compare_symbol_lines(tag_a, tag_b);
1531 g_free(astr);
1532 g_free(bstr);
1534 tm_tag_unref(tag_a);
1535 tm_tag_unref(tag_b);
1537 return cmp;
1541 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1543 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1544 GINT_TO_POINTER(sort_by_name), NULL);
1546 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1550 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1552 GList *tags;
1554 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1556 tags = get_tag_list(doc, tm_tag_max_t);
1557 if (tags == NULL)
1558 return FALSE;
1560 /* FIXME: Not sure why we detached the model here? */
1562 /* disable sorting during update because the code doesn't support correctly
1563 * models that are currently being built */
1564 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1566 /* add grandparent type iters */
1567 add_top_level_items(doc);
1569 update_tree_tags(doc, &tags);
1570 g_list_free(tags);
1572 hide_empty_rows(doc->priv->tag_store);
1574 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1575 sort_mode = doc->priv->symbol_list_sort_mode;
1577 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1578 doc->priv->symbol_list_sort_mode = sort_mode;
1580 return TRUE;
1584 /* Detects a global tags filetype from the *.lang.* language extension.
1585 * Returns NULL if there was no matching TM language. */
1586 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1588 gchar *tags_ext;
1589 gchar *shortname = utils_strdupa(utf8_filename);
1590 GeanyFiletype *ft = NULL;
1592 tags_ext = g_strrstr(shortname, ".tags");
1593 if (tags_ext)
1595 *tags_ext = '\0'; /* remove .tags extension */
1596 ft = filetypes_detect_from_extension(shortname);
1597 if (ft->id != GEANY_FILETYPES_NONE)
1598 return ft;
1600 return NULL;
1604 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1605 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1606 * the relevant path.
1607 * Example:
1608 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1609 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1611 /* -E pre-process, -dD output user macros, -p prof info (?) */
1612 const char pre_process[] = "gcc -E -dD -p -I.";
1614 if (argc > 2)
1616 /* Create global taglist */
1617 int status;
1618 char *command;
1619 const char *tags_file = argv[1];
1620 char *utf8_fname;
1621 GeanyFiletype *ft;
1623 utf8_fname = utils_get_utf8_from_locale(tags_file);
1624 ft = detect_global_tags_filetype(utf8_fname);
1625 g_free(utf8_fname);
1627 if (ft == NULL)
1629 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1630 return 1;
1632 /* load config in case of custom filetypes */
1633 filetypes_load_config(ft->id, FALSE);
1635 /* load ignore list for C/C++ parser */
1636 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1637 load_c_ignore_tags();
1639 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1641 const gchar *cflags = getenv("CFLAGS");
1642 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1644 else
1645 command = NULL; /* don't preprocess */
1647 geany_debug("Generating %s tags file.", ft->name);
1648 tm_get_workspace();
1649 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1650 argc - 2, tags_file, ft->lang);
1651 g_free(command);
1652 symbols_finalize(); /* free c_tags_ignore data */
1653 if (! status)
1655 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1656 "were found.\n"));
1657 return 1;
1660 else
1662 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv[0]);
1663 g_printerr(_("Example:\n"
1664 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1665 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1666 return 1;
1668 return 0;
1672 void symbols_show_load_tags_dialog(void)
1674 GtkWidget *dialog;
1675 GtkFileFilter *filter;
1677 dialog = gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets.window),
1678 GTK_FILE_CHOOSER_ACTION_OPEN,
1679 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1680 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1681 NULL);
1682 gtk_widget_set_name(dialog, "GeanyDialog");
1683 filter = gtk_file_filter_new();
1684 gtk_file_filter_set_name(filter, _("Geany tags file (*.*.tags)"));
1685 gtk_file_filter_add_pattern(filter, "*.*.tags");
1686 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1688 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1690 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1691 GSList *item;
1693 for (item = flist; item != NULL; item = g_slist_next(item))
1695 gchar *fname = item->data;
1696 gchar *utf8_fname;
1697 GeanyFiletype *ft;
1699 utf8_fname = utils_get_utf8_from_locale(fname);
1700 ft = detect_global_tags_filetype(utf8_fname);
1702 if (ft != NULL && symbols_load_global_tags(fname, ft))
1703 /* For translators: the first wildcard is the filetype, the second the filename */
1704 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1705 filetypes_get_display_name(ft), utf8_fname);
1706 else
1707 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1709 g_free(utf8_fname);
1710 g_free(fname);
1712 g_slist_free(flist);
1714 gtk_widget_destroy(dialog);
1718 static void init_user_tags(void)
1720 GSList *file_list = NULL, *list = NULL;
1721 const GSList *node;
1722 gchar *dir;
1724 dir = g_build_filename(app->configdir, GEANY_TAGS_SUBDIR, NULL);
1725 /* create the user tags dir for next time if it doesn't exist */
1726 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1727 utils_mkdir(dir, FALSE);
1728 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1730 SETPTR(dir, g_build_filename(app->datadir, GEANY_TAGS_SUBDIR, NULL));
1731 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1732 g_free(dir);
1734 file_list = g_slist_concat(file_list, list);
1736 /* populate the filetype-specific tag files lists */
1737 for (node = file_list; node != NULL; node = node->next)
1739 gchar *fname = node->data;
1740 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1741 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1743 g_free(utf8_fname);
1745 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1746 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1747 else
1749 geany_debug("Unknown filetype for file '%s'.", fname);
1750 g_free(fname);
1754 /* don't need to delete list contents because they are now stored in
1755 * ft->priv->tag_files */
1756 g_slist_free(file_list);
1760 static void load_user_tags(GeanyFiletypeID ft_id)
1762 static guchar *tags_loaded = NULL;
1763 static gboolean init_tags = FALSE;
1764 const GSList *node;
1765 GeanyFiletype *ft = filetypes[ft_id];
1767 g_return_if_fail(ft_id > 0);
1769 if (!tags_loaded)
1770 tags_loaded = g_new0(guchar, filetypes_array->len);
1771 if (tags_loaded[ft_id])
1772 return;
1773 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1775 if (!init_tags)
1777 init_user_tags();
1778 init_tags = TRUE;
1781 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1783 const gchar *fname = node->data;
1785 symbols_load_global_tags(fname, ft);
1790 static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
1792 GeanyDocument *new_doc, *old_doc;
1794 g_return_if_fail(tag);
1796 old_doc = document_get_current();
1797 new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);
1799 if (new_doc)
1800 navqueue_goto_line(old_doc, new_doc, tag->line);
1804 /* FIXME: use the same icons as in the symbols tree defined in add_top_level_items() */
1805 static guint get_tag_class(const TMTag *tag)
1807 switch (tag->type)
1809 case tm_tag_prototype_t:
1810 case tm_tag_method_t:
1811 case tm_tag_function_t:
1812 return ICON_METHOD;
1813 case tm_tag_variable_t:
1814 case tm_tag_externvar_t:
1815 return ICON_VAR;
1816 case tm_tag_macro_t:
1817 case tm_tag_macro_with_arg_t:
1818 return ICON_MACRO;
1819 case tm_tag_class_t:
1820 return ICON_CLASS;
1821 case tm_tag_member_t:
1822 case tm_tag_field_t:
1823 return ICON_MEMBER;
1824 case tm_tag_typedef_t:
1825 case tm_tag_enum_t:
1826 case tm_tag_union_t:
1827 case tm_tag_struct_t:
1828 return ICON_STRUCT;
1829 case tm_tag_namespace_t:
1830 case tm_tag_package_t:
1831 return ICON_NAMESPACE;
1832 default:
1833 break;
1835 return ICON_STRUCT;
1839 /* positions a popup at the caret from the ScintillaObject in @p data */
1840 static void goto_popup_position_func(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1842 gint line_height;
1843 GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(menu));
1844 gint monitor_num;
1845 GdkRectangle monitor;
1846 GtkRequisition req;
1847 GdkEventButton *event_button = g_object_get_data(G_OBJECT(menu), "geany-button-event");
1849 if (event_button)
1851 /* if we got a mouse click, popup at that position */
1852 *x = (gint) event_button->x_root;
1853 *y = (gint) event_button->y_root;
1854 line_height = 0; /* we don't want to offset below the line or anything */
1856 else /* keyboard positioning */
1858 ScintillaObject *sci = data;
1859 GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(sci));
1860 gint pos = sci_get_current_position(sci);
1861 gint line = sci_get_line_from_position(sci, pos);
1862 gint pos_x = scintilla_send_message(sci, SCI_POINTXFROMPOSITION, 0, pos);
1863 gint pos_y = scintilla_send_message(sci, SCI_POINTYFROMPOSITION, 0, pos);
1865 line_height = scintilla_send_message(sci, SCI_TEXTHEIGHT, line, 0);
1867 gdk_window_get_origin(window, x, y);
1868 *x += pos_x;
1869 *y += pos_y;
1872 monitor_num = gdk_screen_get_monitor_at_point(screen, *x, *y);
1874 #if GTK_CHECK_VERSION(3, 0, 0)
1875 gtk_widget_get_preferred_size(GTK_WIDGET(menu), NULL, &req);
1876 #else
1877 gtk_widget_size_request(GTK_WIDGET(menu), &req);
1878 #endif
1880 #if GTK_CHECK_VERSION(3, 4, 0)
1881 gdk_screen_get_monitor_workarea(screen, monitor_num, &monitor);
1882 #else
1883 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
1884 #endif
1886 /* put on one size of the X position, but within the monitor */
1887 if (gtk_widget_get_direction(GTK_WIDGET(menu)) == GTK_TEXT_DIR_RTL)
1889 if (*x - req.width - 1 >= monitor.x)
1890 *x -= req.width + 1;
1891 else if (*x + req.width > monitor.x + monitor.width)
1892 *x = monitor.x;
1893 else
1894 *x += 1;
1896 else
1898 if (*x + req.width + 1 <= monitor.x + monitor.width)
1899 *x = MAX(monitor.x, *x + 1);
1900 else if (*x - req.width - 1 >= monitor.x)
1901 *x -= req.width + 1;
1902 else
1903 *x = monitor.x + MAX(0, monitor.width - req.width);
1906 /* try to put, in order:
1907 * 1. below the Y position, under the line
1908 * 2. above the Y position
1909 * 3. within the monitor */
1910 if (*y + line_height + req.height <= monitor.y + monitor.height)
1911 *y = MAX(monitor.y, *y + line_height);
1912 else if (*y - req.height >= monitor.y)
1913 *y = *y - req.height;
1914 else
1915 *y = monitor.y + MAX(0, monitor.height - req.height);
1917 *push_in = FALSE;
1921 static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_best)
1923 GtkWidget *first = NULL;
1924 GtkWidget *menu;
1925 GdkEvent *event;
1926 GdkEventButton *button_event = NULL;
1927 TMTag *tmtag;
1928 guint i;
1930 menu = gtk_menu_new();
1932 foreach_ptr_array(tmtag, i, tags)
1934 GtkWidget *item;
1935 GtkWidget *label;
1936 GtkWidget *image;
1937 gchar *fname = g_path_get_basename(tmtag->file->file_name);
1938 gchar *text;
1940 if (! first && have_best)
1941 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1942 text = g_markup_printf_escaped(_("<b>%s: %lu</b>"), fname, tmtag->line);
1943 else
1944 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1945 text = g_markup_printf_escaped(_("%s: %lu"), fname, tmtag->line);
1947 image = gtk_image_new_from_pixbuf(symbols_icons[get_tag_class(tmtag)].pixbuf);
1948 label = g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0, NULL);
1949 item = g_object_new(GTK_TYPE_IMAGE_MENU_ITEM, "image", image, "child", label, NULL);
1950 g_signal_connect_data(item, "activate", G_CALLBACK(on_goto_popup_item_activate),
1951 tm_tag_ref(tmtag), (GClosureNotify) tm_tag_unref, 0);
1952 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1954 if (! first)
1955 first = item;
1957 g_free(text);
1958 g_free(fname);
1961 gtk_widget_show_all(menu);
1963 if (first) /* always select the first item for better keyboard navigation */
1964 g_signal_connect(menu, "realize", G_CALLBACK(gtk_menu_shell_select_item), first);
1966 event = gtk_get_current_event();
1967 if (event && event->type == GDK_BUTTON_PRESS)
1968 button_event = (GdkEventButton *) event;
1969 else
1970 gdk_event_free(event);
1972 g_object_set_data_full(G_OBJECT(menu), "geany-button-event", button_event,
1973 button_event ? (GDestroyNotify) gdk_event_free : NULL);
1974 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, goto_popup_position_func, doc->editor->sci,
1975 button_event ? button_event->button : 0, gtk_get_current_event_time ());
1979 static gint compare_tags_by_name_line(gconstpointer ptr1, gconstpointer ptr2)
1981 gint res;
1982 TMTag *t1 = *((TMTag **) ptr1);
1983 TMTag *t2 = *((TMTag **) ptr2);
1985 res = g_strcmp0(t1->file->short_name, t2->file->short_name);
1986 if (res != 0)
1987 return res;
1988 return t1->line - t2->line;
1992 static TMTag *find_best_goto_tag(GeanyDocument *doc, GPtrArray *tags)
1994 TMTag *tag;
1995 guint i;
1997 /* first check if we have a tag in the current file */
1998 foreach_ptr_array(tag, i, tags)
2000 if (g_strcmp0(doc->real_path, tag->file->file_name) == 0)
2001 return tag;
2004 /* next check if we have a tag for some of the open documents */
2005 foreach_ptr_array(tag, i, tags)
2007 guint j;
2009 foreach_document(j)
2011 if (g_strcmp0(documents[j]->real_path, tag->file->file_name) == 0)
2012 return tag;
2016 /* next check if we have a tag for a file inside the current document's directory */
2017 foreach_ptr_array(tag, i, tags)
2019 gchar *dir = g_path_get_dirname(doc->real_path);
2021 if (g_str_has_prefix(tag->file->file_name, dir))
2023 g_free(dir);
2024 return tag;
2026 g_free(dir);
2029 return NULL;
2033 static GPtrArray *filter_tags(GPtrArray *tags, TMTag *current_tag, gboolean definition)
2035 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
2036 TMTag *tmtag, *last_tag = NULL;
2037 GPtrArray *filtered_tags = g_ptr_array_new();
2038 guint i;
2040 foreach_ptr_array(tmtag, i, tags)
2042 if ((definition && !(tmtag->type & forward_types)) ||
2043 (!definition && (tmtag->type & forward_types)))
2045 /* If there are typedefs of e.g. a struct such as
2046 * "typedef struct Foo {} Foo;", filter out the typedef unless
2047 * cursor is at the struct name. */
2048 if (last_tag != NULL && last_tag->file == tmtag->file &&
2049 last_tag->type != tm_tag_typedef_t && tmtag->type == tm_tag_typedef_t)
2051 if (last_tag == current_tag)
2052 g_ptr_array_add(filtered_tags, tmtag);
2054 else if (tmtag != current_tag)
2055 g_ptr_array_add(filtered_tags, tmtag);
2057 last_tag = tmtag;
2061 return filtered_tags;
2065 static gboolean goto_tag(const gchar *name, gboolean definition)
2067 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
2068 TMTag *tmtag, *current_tag = NULL;
2069 GeanyDocument *old_doc = document_get_current();
2070 gboolean found = FALSE;
2071 const GPtrArray *all_tags;
2072 GPtrArray *tags, *filtered_tags;
2073 guint i;
2074 guint current_line = sci_get_current_line(old_doc->editor->sci) + 1;
2076 all_tags = tm_workspace_find(name, NULL, tm_tag_max_t, NULL, old_doc->file_type->lang);
2078 /* get rid of global tags and find tag at current line */
2079 tags = g_ptr_array_new();
2080 foreach_ptr_array(tmtag, i, all_tags)
2082 if (tmtag->file)
2084 g_ptr_array_add(tags, tmtag);
2085 if (tmtag->file == old_doc->tm_file && tmtag->line == current_line)
2086 current_tag = tmtag;
2090 if (current_tag)
2091 /* swap definition/declaration search */
2092 definition = current_tag->type & forward_types;
2094 filtered_tags = filter_tags(tags, current_tag, definition);
2095 if (filtered_tags->len == 0)
2097 /* if we didn't find anything, try again with the opposite type */
2098 g_ptr_array_free(filtered_tags, TRUE);
2099 filtered_tags = filter_tags(tags, current_tag, !definition);
2101 g_ptr_array_free(tags, TRUE);
2102 tags = filtered_tags;
2104 if (tags->len == 1)
2106 GeanyDocument *new_doc;
2108 tmtag = tags->pdata[0];
2109 new_doc = document_find_by_real_path(tmtag->file->file_name);
2111 if (!new_doc)
2112 /* not found in opened document, should open */
2113 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
2115 navqueue_goto_line(old_doc, new_doc, tmtag->line);
2117 else if (tags->len > 1)
2119 GPtrArray *tag_list;
2120 TMTag *tag, *best_tag;
2122 g_ptr_array_sort(tags, compare_tags_by_name_line);
2123 best_tag = find_best_goto_tag(old_doc, tags);
2125 tag_list = g_ptr_array_new();
2126 if (best_tag)
2127 g_ptr_array_add(tag_list, best_tag);
2128 foreach_ptr_array(tag, i, tags)
2130 if (tag != best_tag)
2131 g_ptr_array_add(tag_list, tag);
2133 show_goto_popup(old_doc, tag_list, best_tag != NULL);
2135 g_ptr_array_free(tag_list, TRUE);
2138 found = tags->len > 0;
2139 g_ptr_array_free(tags, TRUE);
2141 return found;
2145 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
2147 if (goto_tag(name, definition))
2148 return TRUE;
2150 /* if we are here, there was no match and we are beeping ;-) */
2151 utils_beep();
2153 if (!definition)
2154 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
2155 else
2156 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
2157 return FALSE;
2161 /* This could perhaps be improved to check for #if, class etc. */
2162 static gint get_function_fold_number(GeanyDocument *doc)
2164 /* for Java the functions are always one fold level above the class scope */
2165 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
2166 return SC_FOLDLEVELBASE + 1;
2167 else
2168 return SC_FOLDLEVELBASE;
2172 /* Should be used only with get_current_tag_cached.
2173 * tag_types caching might trigger recomputation too often but this isn't used differently often
2174 * enough to be an issue for now */
2175 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
2177 static gint old_line = -2;
2178 static GeanyDocument *old_doc = NULL;
2179 static gint old_fold_num = -1;
2180 static guint old_tag_types = 0;
2181 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
2182 gboolean ret;
2184 /* check if the cached line and file index have changed since last time: */
2185 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
2186 ret = TRUE;
2187 else if (cur_line == old_line)
2188 ret = FALSE;
2189 else
2191 /* if the line has only changed by 1 */
2192 if (abs(cur_line - old_line) == 1)
2194 /* It's the same function if the fold number hasn't changed */
2195 ret = (fold_num != old_fold_num);
2197 else ret = TRUE;
2200 /* record current line and file index for next time */
2201 old_line = cur_line;
2202 old_doc = doc;
2203 old_fold_num = fold_num;
2204 old_tag_types = tag_types;
2205 return ret;
2209 /* Parse the function name up to 2 lines before tag_line.
2210 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2211 * type or argument names can be confused with the function name. */
2212 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2214 gint start, end, max_pos;
2215 gint fn_style;
2217 switch (sci_get_lexer(sci))
2219 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2220 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2221 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2223 start = sci_get_position_from_line(sci, tag_line - 2);
2224 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2225 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
2226 start++;
2228 end = start;
2229 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
2230 end++;
2232 if (start == end)
2233 return NULL;
2234 return sci_get_contents_range(sci, start, end);
2238 /* Parse the function name */
2239 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2241 gint start, end, first_pos, max_pos;
2242 gint tmp;
2243 gchar c;
2245 first_pos = end = sci_get_position_from_line(sci, tag_line);
2246 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2247 tmp = 0;
2248 /* goto the begin of function body */
2249 while (end < max_pos &&
2250 (tmp = sci_get_char_at(sci, end)) != '{' &&
2251 tmp != 0) end++;
2252 if (tmp == 0) end --;
2254 /* go back to the end of function identifier */
2255 while (end > 0 && end > first_pos - 500 &&
2256 (tmp = sci_get_char_at(sci, end)) != '(' &&
2257 tmp != 0) end--;
2258 end--;
2259 if (end < 0) end = 0;
2261 /* skip whitespaces between identifier and ( */
2262 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2264 start = end;
2265 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2266 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2267 || tmp == SCE_C_GLOBALCLASS
2268 || (c = sci_get_char_at(sci, start)) == '~'
2269 || c == ':'))
2270 start--;
2271 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2273 if (start == end) return NULL;
2274 return sci_get_contents_range(sci, start, end + 1);
2278 static gint get_fold_header_after(ScintillaObject *sci, gint line)
2280 gint line_count = sci_get_line_count(sci);
2282 for (; line < line_count; line++)
2284 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
2285 return line;
2288 return -1;
2292 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
2294 gint line;
2295 gint parent;
2297 line = sci_get_current_line(doc->editor->sci);
2298 parent = sci_get_fold_parent(doc->editor->sci, line);
2299 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2300 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
2301 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
2303 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
2305 if (tag)
2307 gint tag_line = tag->line - 1;
2308 gint last_child = line + 1;
2310 /* if it may be a false positive because we're inside a fold level not inside anything
2311 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2312 * right after the tag we got from TM */
2313 if (abs(tag_line - parent) > 1)
2315 gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
2316 if (tag_fold >= 0)
2317 last_child = scintilla_send_message(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
2320 if (line <= last_child)
2322 if (tag->scope)
2323 *tagname = g_strconcat(tag->scope,
2324 symbols_get_context_separator(doc->file_type->id), tag->name, NULL);
2325 else
2326 *tagname = g_strdup(tag->name);
2328 return tag_line;
2332 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2333 * to dirty and inaccurate hand-parsing */
2334 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2336 const gint fn_fold = get_function_fold_number(doc);
2337 gint tag_line = parent;
2338 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2340 /* find the top level fold point */
2341 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
2343 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2344 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2347 if (tag_line >= 0)
2349 gchar *cur_tag;
2351 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2352 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2353 else
2354 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2356 if (cur_tag != NULL)
2358 *tagname = cur_tag;
2359 return tag_line;
2364 *tagname = g_strdup(_("unknown"));
2365 return -1;
2369 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
2371 static gint tag_line = -1;
2372 static gchar *cur_tag = NULL;
2374 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2376 if (doc == NULL) /* reset current function */
2378 current_tag_changed(NULL, -1, -1, 0);
2379 g_free(cur_tag);
2380 cur_tag = g_strdup(_("unknown"));
2381 if (tagname != NULL)
2382 *tagname = cur_tag;
2383 tag_line = -1;
2385 else
2387 gint line = sci_get_current_line(doc->editor->sci);
2388 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2390 if (current_tag_changed(doc, line, fold_level, tag_types))
2392 g_free(cur_tag);
2393 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2395 *tagname = cur_tag;
2398 return tag_line;
2402 /* Sets *tagname to point at the current function or tag name.
2403 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2404 * call to this function.
2405 * Returns: line number of the current tag, or -1 if unknown. */
2406 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2408 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2412 /* same as symbols_get_current_function() but finds class, namespaces and more */
2413 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2415 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2416 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t);
2418 /* Python parser reports imports as namespaces which confuses the scope detection */
2419 if (doc && doc->file_type->lang != filetypes[GEANY_FILETYPES_PYTHON]->lang)
2420 tag_types |= tm_tag_namespace_t;
2422 return get_current_tag_name_cached(doc, tagname, tag_types);
2426 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2428 gint sort_mode = GPOINTER_TO_INT(user_data);
2429 GeanyDocument *doc = document_get_current();
2431 if (ignore_callback)
2432 return;
2434 if (doc != NULL)
2435 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2439 static void on_symbol_tree_menu_show(GtkWidget *widget,
2440 gpointer user_data)
2442 GeanyDocument *doc = document_get_current();
2443 gboolean enable;
2445 enable = doc && doc->has_tags;
2446 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2447 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2448 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2449 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2450 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2451 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2453 if (! doc)
2454 return;
2456 ignore_callback = TRUE;
2458 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2459 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2460 else
2461 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2463 ignore_callback = FALSE;
2467 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2469 gboolean expand = GPOINTER_TO_INT(user_data);
2470 GeanyDocument *doc = document_get_current();
2472 if (! doc)
2473 return;
2475 g_return_if_fail(doc->priv->tag_tree);
2477 if (expand)
2478 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2479 else
2480 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2484 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2486 GtkTreeIter iter;
2487 GtkTreeSelection *selection;
2488 GtkTreeModel *model;
2489 GeanyDocument *doc;
2490 TMTag *tag = NULL;
2492 doc = document_get_current();
2493 if (!doc)
2494 return;
2496 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2497 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2498 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2499 if (tag)
2501 if (widget == symbol_menu.find_in_files)
2502 search_show_find_in_files_dialog_full(tag->name, NULL);
2503 else
2504 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2505 widget == symbol_menu.find_usage);
2507 tm_tag_unref(tag);
2512 static void create_taglist_popup_menu(void)
2514 GtkWidget *item, *menu;
2516 tv.popup_taglist = menu = gtk_menu_new();
2518 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2519 gtk_widget_show(item);
2520 gtk_container_add(GTK_CONTAINER(menu), item);
2521 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2523 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2524 gtk_widget_show(item);
2525 gtk_container_add(GTK_CONTAINER(menu), item);
2526 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2528 item = gtk_separator_menu_item_new();
2529 gtk_widget_show(item);
2530 gtk_container_add(GTK_CONTAINER(menu), item);
2532 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2533 _("Sort by _Name"));
2534 gtk_widget_show(item);
2535 gtk_container_add(GTK_CONTAINER(menu), item);
2536 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2537 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2539 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2540 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2541 gtk_widget_show(item);
2542 gtk_container_add(GTK_CONTAINER(menu), item);
2543 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2544 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2546 item = gtk_separator_menu_item_new();
2547 gtk_widget_show(item);
2548 gtk_container_add(GTK_CONTAINER(menu), item);
2550 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2551 gtk_widget_show(item);
2552 gtk_container_add(GTK_CONTAINER(menu), item);
2553 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2555 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2556 gtk_widget_show(item);
2557 gtk_container_add(GTK_CONTAINER(menu), item);
2558 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2560 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2561 gtk_widget_show(item);
2562 gtk_container_add(GTK_CONTAINER(menu), item);
2563 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2565 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2567 sidebar_add_common_menu_items(GTK_MENU(menu));
2571 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2573 gchar *f;
2575 g_return_if_fail(!EMPTY(doc->real_path));
2577 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2578 if (utils_str_equal(doc->real_path, f))
2579 load_c_ignore_tags();
2581 g_free(f);
2585 void symbols_init(void)
2587 gchar *f;
2588 guint i;
2590 create_taglist_popup_menu();
2592 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2593 ui_add_config_file_menu_item(f, NULL, NULL);
2594 g_free(f);
2596 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2598 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2599 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2603 void symbols_finalize(void)
2605 guint i;
2607 g_strfreev(c_tags_ignore);
2609 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2611 if (symbols_icons[i].pixbuf)
2612 g_object_unref(symbols_icons[i].pixbuf);