2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Tag-related functions.
27 * Symbol Tree and TagManager-related convenience functions.
28 * TagManager parses tags for each document, and also adds them to the workspace (session).
29 * Global tags are lists of tags for each filetype, loaded when a document with a
30 * matching filetype is first loaded.
40 #include "callbacks.h" /* FIXME: for ignore_callback */
41 #include "documentprivate.h"
43 #include "encodings.h"
44 #include "filetypesprivate.h"
45 #include "geanyobject.h"
46 #include "highlighting.h"
49 #include "sciwrappers.h"
52 #include "tm_parser.h"
68 gint found_line
; /* return: the nearest line found */
69 gint line
; /* input: the line to look for */
70 gboolean lower
/* input: search only for lines with lower number than @line */;
74 static GPtrArray
*top_level_iter_names
= NULL
;
92 const gchar
*icon_name
;
95 symbols_icons
[N_ICONS
] = {
96 [ICON_CLASS
] = { "classviewer-class", NULL
},
97 [ICON_MACRO
] = { "classviewer-macro", NULL
},
98 [ICON_MEMBER
] = { "classviewer-member", NULL
},
99 [ICON_METHOD
] = { "classviewer-method", NULL
},
100 [ICON_NAMESPACE
] = { "classviewer-namespace", NULL
},
101 [ICON_OTHER
] = { "classviewer-other", NULL
},
102 [ICON_STRUCT
] = { "classviewer-struct", NULL
},
103 [ICON_VAR
] = { "classviewer-var", NULL
},
108 GtkWidget
*expand_all
;
109 GtkWidget
*collapse_all
;
110 GtkWidget
*sort_by_name
;
111 GtkWidget
*sort_by_appearance
;
112 GtkWidget
*find_usage
;
113 GtkWidget
*find_doc_usage
;
114 GtkWidget
*find_in_files
;
118 static void load_user_tags(GeanyFiletypeID ft_id
);
120 /* get the tags_ignore list, exported by geany_lcpp.c */
121 extern gchar
**c_tags_ignore
;
123 /* ignore certain tokens when parsing C-like syntax.
124 * Also works for reloading. */
125 static void load_c_ignore_tags(void)
127 gchar
*path
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
130 if (g_file_get_contents(path
, &content
, NULL
, NULL
))
134 /* historically we ignore the glib _DECLS for tag generation */
135 SETPTR(content
, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content
, NULL
));
137 g_strfreev(c_tags_ignore
);
138 tm_ctags_clear_ignore_symbols();
140 /* for old c.c parser */
141 c_tags_ignore
= g_strsplit_set(content
, " \n\r", -1);
142 /* for new cxx parser */
143 foreach_strv(line
, c_tags_ignore
)
145 tm_ctags_add_ignore_symbol(*line
);
154 void symbols_reload_config_files(void)
156 load_c_ignore_tags();
160 static gsize
get_tag_count(void)
162 GPtrArray
*tags
= tm_get_workspace()->global_tags
;
163 gsize count
= tags
? tags
->len
: 0;
169 /* wrapper for tm_workspace_load_global_tags().
170 * note that the tag count only counts new global tags added - if a tag has the same name,
171 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
172 static gboolean
symbols_load_global_tags(const gchar
*tags_file
, GeanyFiletype
*ft
)
175 gsize old_tag_count
= get_tag_count();
177 result
= tm_workspace_load_global_tags(tags_file
, ft
->lang
);
180 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file
, ft
->name
,
181 (guint
) (get_tag_count() - old_tag_count
));
187 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
188 * This provides autocompletion, calltips, etc. */
189 void symbols_global_tags_loaded(guint file_type_idx
)
191 /* load ignore list for C/C++ parser */
192 if ((file_type_idx
== GEANY_FILETYPES_C
|| file_type_idx
== GEANY_FILETYPES_CPP
) &&
193 c_tags_ignore
== NULL
)
195 load_c_ignore_tags();
198 if (cl_options
.ignore_global_tags
|| app
->tm_workspace
== NULL
)
201 /* load config in case of custom filetypes */
202 filetypes_load_config(file_type_idx
, FALSE
);
204 load_user_tags(file_type_idx
);
206 switch (file_type_idx
)
208 case GEANY_FILETYPES_CPP
:
209 symbols_global_tags_loaded(GEANY_FILETYPES_C
); /* load C global tags */
211 case GEANY_FILETYPES_PHP
:
212 symbols_global_tags_loaded(GEANY_FILETYPES_HTML
); /* load HTML global tags */
218 GString
*symbols_find_typenames_as_string(TMParserType lang
, gboolean global
)
224 TMParserType tag_lang
;
227 typedefs
= app
->tm_workspace
->global_typename_array
;
229 typedefs
= app
->tm_workspace
->typename_array
;
231 if ((typedefs
) && (typedefs
->len
> 0))
233 const gchar
*last_name
= "";
235 s
= g_string_sized_new(typedefs
->len
* 10);
236 for (j
= 0; j
< typedefs
->len
; ++j
)
238 tag
= TM_TAG(typedefs
->pdata
[j
]);
239 tag_lang
= tag
->lang
;
241 if (tag
->name
&& tm_parser_langs_compatible(lang
, tag_lang
) &&
242 strcmp(tag
->name
, last_name
) != 0)
245 g_string_append_c(s
, ' ');
246 g_string_append(s
, tag
->name
);
247 last_name
= tag
->name
;
255 /** Gets the context separator used by the tag manager for a particular file
257 * @param ft_id File type identifier.
258 * @return The context separator string.
260 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
261 * without a context separator.
266 const gchar
*symbols_get_context_separator(gint ft_id
)
268 return tm_parser_context_separator(filetypes
[ft_id
]->lang
);
272 /* sort by name, then line */
273 static gint
compare_symbol(const TMTag
*tag_a
, const TMTag
*tag_b
)
277 if (tag_a
== NULL
|| tag_b
== NULL
)
280 if (tag_a
->name
== NULL
)
281 return -(tag_a
->name
!= tag_b
->name
);
283 if (tag_b
->name
== NULL
)
284 return tag_a
->name
!= tag_b
->name
;
286 ret
= strcmp(tag_a
->name
, tag_b
->name
);
289 return tag_a
->line
- tag_b
->line
;
295 /* sort by line, then scope */
296 static gint
compare_symbol_lines(gconstpointer a
, gconstpointer b
)
298 const TMTag
*tag_a
= TM_TAG(a
);
299 const TMTag
*tag_b
= TM_TAG(b
);
302 if (a
== NULL
|| b
== NULL
)
305 ret
= tag_a
->line
- tag_b
->line
;
308 if (tag_a
->scope
== NULL
)
309 return -(tag_a
->scope
!= tag_b
->scope
);
310 if (tag_b
->scope
== NULL
)
311 return tag_a
->scope
!= tag_b
->scope
;
313 return strcmp(tag_a
->scope
, tag_b
->scope
);
319 static GList
*get_tag_list(GeanyDocument
*doc
, TMTagType tag_types
)
321 GList
*tag_names
= NULL
;
326 g_return_val_if_fail(doc
, NULL
);
328 if (! doc
->tm_file
|| ! doc
->tm_file
->tags_array
)
331 tf_entry
= GTK_ENTRY(ui_lookup_widget(main_widgets
.window
, "entry_tagfilter"));
332 tf_strv
= g_strsplit_set(gtk_entry_get_text(tf_entry
), " ", -1);
334 for (i
= 0; i
< doc
->tm_file
->tags_array
->len
; ++i
)
336 TMTag
*tag
= TM_TAG(doc
->tm_file
->tags_array
->pdata
[i
]);
338 if (tag
->type
& tag_types
)
340 gboolean filtered
= FALSE
;
342 gchar
*normalized_tagname
= g_utf8_normalize(tag
->name
, -1, G_NORMALIZE_ALL
);
344 foreach_strv(val
, tf_strv
)
346 gchar
*normalized_val
= g_utf8_normalize(*val
, -1, G_NORMALIZE_ALL
);
348 if (normalized_tagname
!= NULL
&& normalized_val
!= NULL
)
350 gchar
*case_normalized_tagname
= g_utf8_casefold(normalized_tagname
, -1);
351 gchar
*case_normalized_val
= g_utf8_casefold(normalized_val
, -1);
353 filtered
= strstr(case_normalized_tagname
, case_normalized_val
) == NULL
;
354 g_free(case_normalized_tagname
);
355 g_free(case_normalized_val
);
357 g_free(normalized_val
);
363 tag_names
= g_list_prepend(tag_names
, tag
);
365 g_free(normalized_tagname
);
368 tag_names
= g_list_sort(tag_names
, compare_symbol_lines
);
376 /* amount of types in the symbol list (currently max. 8 are used) */
377 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
379 struct TreeviewSymbols
381 GtkTreeIter tag_function
;
382 GtkTreeIter tag_class
;
383 GtkTreeIter tag_macro
;
384 GtkTreeIter tag_member
;
385 GtkTreeIter tag_variable
;
386 GtkTreeIter tag_externvar
;
387 GtkTreeIter tag_namespace
;
388 GtkTreeIter tag_struct
;
389 GtkTreeIter tag_interface
;
390 GtkTreeIter tag_type
;
391 GtkTreeIter tag_other
;
395 static void init_tag_iters(void)
397 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
398 * filetypes(e.g. config file to Python crashes Geany without this) */
399 tv_iters
.tag_function
.stamp
= -1;
400 tv_iters
.tag_class
.stamp
= -1;
401 tv_iters
.tag_member
.stamp
= -1;
402 tv_iters
.tag_macro
.stamp
= -1;
403 tv_iters
.tag_variable
.stamp
= -1;
404 tv_iters
.tag_externvar
.stamp
= -1;
405 tv_iters
.tag_namespace
.stamp
= -1;
406 tv_iters
.tag_struct
.stamp
= -1;
407 tv_iters
.tag_interface
.stamp
= -1;
408 tv_iters
.tag_type
.stamp
= -1;
409 tv_iters
.tag_other
.stamp
= -1;
413 static GdkPixbuf
*get_tag_icon(const gchar
*icon_name
)
415 static GtkIconTheme
*icon_theme
= NULL
;
418 if (G_UNLIKELY(x
< 0))
421 icon_theme
= gtk_icon_theme_get_default();
422 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &x
, &dummy
);
424 return gtk_icon_theme_load_icon(icon_theme
, icon_name
, x
, 0, NULL
);
428 static gboolean
find_toplevel_iter(GtkTreeStore
*store
, GtkTreeIter
*iter
, const gchar
*title
)
430 GtkTreeModel
*model
= GTK_TREE_MODEL(store
);
432 if (!gtk_tree_model_get_iter_first(model
, iter
))
438 gtk_tree_model_get(model
, iter
, SYMBOLS_COLUMN_NAME
, &candidate
, -1);
439 /* FIXME: what if 2 different items have the same name?
440 * this should never happen, but might be caused by a typo in a translation */
441 if (utils_str_equal(candidate
, title
))
449 while (gtk_tree_model_iter_next(model
, iter
));
455 /* Adds symbol list groups in (iter*, title) pairs.
456 * The list must be ended with NULL. */
457 static void G_GNUC_NULL_TERMINATED
458 tag_list_add_groups(GtkTreeStore
*tree_store
, ...)
463 g_return_if_fail(top_level_iter_names
);
465 va_start(args
, tree_store
);
466 for (; iter
= va_arg(args
, GtkTreeIter
*), iter
!= NULL
;)
468 gchar
*title
= va_arg(args
, gchar
*);
469 guint icon_id
= va_arg(args
, guint
);
470 GdkPixbuf
*icon
= NULL
;
472 if (icon_id
< N_ICONS
)
473 icon
= symbols_icons
[icon_id
].pixbuf
;
475 g_assert(title
!= NULL
);
476 g_ptr_array_add(top_level_iter_names
, title
);
478 if (!find_toplevel_iter(tree_store
, iter
, title
))
479 gtk_tree_store_append(tree_store
, iter
, NULL
);
482 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_ICON
, icon
, -1);
483 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_NAME
, title
, -1);
489 static void add_top_level_items(GeanyDocument
*doc
)
491 GeanyFiletypeID ft_id
= doc
->file_type
->id
;
492 GtkTreeStore
*tag_store
= doc
->priv
->tag_store
;
494 if (top_level_iter_names
== NULL
)
495 top_level_iter_names
= g_ptr_array_new();
497 g_ptr_array_set_size(top_level_iter_names
, 0);
503 case GEANY_FILETYPES_DIFF
:
505 tag_list_add_groups(tag_store
,
506 &(tv_iters
.tag_function
), _("Files"), ICON_NONE
, NULL
);
509 case GEANY_FILETYPES_DOCBOOK
:
511 tag_list_add_groups(tag_store
,
512 &(tv_iters
.tag_function
), _("Chapter"), ICON_NONE
,
513 &(tv_iters
.tag_class
), _("Section"), ICON_NONE
,
514 &(tv_iters
.tag_member
), _("Sect1"), ICON_NONE
,
515 &(tv_iters
.tag_macro
), _("Sect2"), ICON_NONE
,
516 &(tv_iters
.tag_variable
), _("Sect3"), ICON_NONE
,
517 &(tv_iters
.tag_struct
), _("Appendix"), ICON_NONE
,
518 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
522 case GEANY_FILETYPES_HASKELL
:
523 tag_list_add_groups(tag_store
,
524 &tv_iters
.tag_namespace
, _("Module"), ICON_NONE
,
525 &tv_iters
.tag_type
, _("Types"), ICON_NONE
,
526 &tv_iters
.tag_macro
, _("Type constructors"), ICON_NONE
,
527 &tv_iters
.tag_function
, _("Functions"), ICON_METHOD
,
530 case GEANY_FILETYPES_COBOL
:
531 tag_list_add_groups(tag_store
,
532 &tv_iters
.tag_class
, _("Program"), ICON_CLASS
,
533 &tv_iters
.tag_function
, _("File"), ICON_METHOD
,
534 &tv_iters
.tag_interface
, _("Divisions"), ICON_NAMESPACE
,
535 &tv_iters
.tag_namespace
, _("Sections"), ICON_NAMESPACE
,
536 &tv_iters
.tag_macro
, _("Paragraph"), ICON_OTHER
,
537 &tv_iters
.tag_struct
, _("Group"), ICON_STRUCT
,
538 &tv_iters
.tag_variable
, _("Data"), ICON_VAR
,
539 &tv_iters
.tag_externvar
, _("Copies"), ICON_NAMESPACE
,
542 case GEANY_FILETYPES_CONF
:
543 tag_list_add_groups(tag_store
,
544 &tv_iters
.tag_namespace
, _("Sections"), ICON_OTHER
,
545 &tv_iters
.tag_macro
, _("Keys"), ICON_VAR
,
548 case GEANY_FILETYPES_NSIS
:
549 tag_list_add_groups(tag_store
,
550 &tv_iters
.tag_namespace
, _("Sections"), ICON_OTHER
,
551 &tv_iters
.tag_function
, _("Functions"), ICON_METHOD
,
552 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
555 case GEANY_FILETYPES_LATEX
:
557 tag_list_add_groups(tag_store
,
558 &(tv_iters
.tag_function
), _("Command"), ICON_NONE
,
559 &(tv_iters
.tag_class
), _("Environment"), ICON_NONE
,
560 &(tv_iters
.tag_member
), _("Section"), ICON_NONE
,
561 &(tv_iters
.tag_macro
), _("Subsection"), ICON_NONE
,
562 &(tv_iters
.tag_variable
), _("Subsubsection"), ICON_NONE
,
563 &(tv_iters
.tag_struct
), _("Label"), ICON_NONE
,
564 &(tv_iters
.tag_namespace
), _("Chapter"), ICON_NONE
,
565 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
569 case GEANY_FILETYPES_BIBTEX
:
571 tag_list_add_groups(tag_store
,
572 &(tv_iters
.tag_function
), _("Articles"), ICON_NONE
,
573 &(tv_iters
.tag_macro
), _("Book Chapters"), ICON_NONE
,
574 &(tv_iters
.tag_class
), _("Books & Conference Proceedings"), ICON_NONE
,
575 &(tv_iters
.tag_member
), _("Conference Papers"), ICON_NONE
,
576 &(tv_iters
.tag_variable
), _("Theses"), ICON_NONE
,
577 &(tv_iters
.tag_namespace
), _("Strings"), ICON_NONE
,
578 &(tv_iters
.tag_externvar
), _("Unpublished"), ICON_NONE
,
579 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
583 case GEANY_FILETYPES_MATLAB
:
585 tag_list_add_groups(tag_store
,
586 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
587 &(tv_iters
.tag_struct
), _("Structures"), ICON_STRUCT
,
591 case GEANY_FILETYPES_ABAQUS
:
593 tag_list_add_groups(tag_store
,
594 &(tv_iters
.tag_class
), _("Parts"), ICON_NONE
,
595 &(tv_iters
.tag_member
), _("Assembly"), ICON_NONE
,
596 &(tv_iters
.tag_namespace
), _("Steps"), ICON_NONE
,
600 case GEANY_FILETYPES_R
:
602 tag_list_add_groups(tag_store
,
603 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
604 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
608 case GEANY_FILETYPES_RUST
:
610 tag_list_add_groups(tag_store
,
611 &(tv_iters
.tag_namespace
), _("Modules"), ICON_NAMESPACE
,
612 &(tv_iters
.tag_struct
), _("Structures"), ICON_STRUCT
,
613 &(tv_iters
.tag_interface
), _("Traits"), ICON_CLASS
,
614 &(tv_iters
.tag_class
), _("Implementations"), ICON_CLASS
,
615 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
616 &(tv_iters
.tag_type
), _("Typedefs / Enums"), ICON_STRUCT
,
617 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
618 &(tv_iters
.tag_macro
), _("Macros"), ICON_MACRO
,
619 &(tv_iters
.tag_member
), _("Methods"), ICON_MEMBER
,
620 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
624 case GEANY_FILETYPES_GO
:
626 tag_list_add_groups(tag_store
,
627 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
628 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
629 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
630 &(tv_iters
.tag_struct
), _("Structs"), ICON_STRUCT
,
631 &(tv_iters
.tag_type
), _("Types"), ICON_STRUCT
,
632 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
633 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
634 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
635 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
639 case GEANY_FILETYPES_PERL
:
641 tag_list_add_groups(tag_store
,
642 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
643 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
644 &(tv_iters
.tag_macro
), _("Labels"), ICON_NONE
,
645 &(tv_iters
.tag_type
), _("Constants"), ICON_NONE
,
646 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
650 case GEANY_FILETYPES_PHP
:
651 case GEANY_FILETYPES_ZEPHIR
:
653 tag_list_add_groups(tag_store
,
654 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
,
655 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
656 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
657 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
658 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
659 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
660 &(tv_iters
.tag_struct
), _("Traits"), ICON_STRUCT
,
664 case GEANY_FILETYPES_JULIA
:
666 tag_list_add_groups(tag_store
,
667 &(tv_iters
.tag_variable
), _("Constants"), ICON_VAR
,
668 &(tv_iters
.tag_namespace
), _("Modules"), ICON_NAMESPACE
,
669 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
670 &(tv_iters
.tag_member
), _("Fields"), ICON_MEMBER
,
671 &(tv_iters
.tag_macro
), _("Macros"), ICON_MACRO
,
672 &(tv_iters
.tag_struct
), _("Structures"), ICON_STRUCT
,
673 &(tv_iters
.tag_type
), _("Types"), ICON_CLASS
,
674 &(tv_iters
.tag_externvar
), _("Unknowns"), ICON_OTHER
,
678 case GEANY_FILETYPES_HTML
:
680 tag_list_add_groups(tag_store
,
681 &(tv_iters
.tag_function
), _("Functions"), ICON_NONE
,
682 &(tv_iters
.tag_member
), _("Anchors"), ICON_NONE
,
683 &(tv_iters
.tag_namespace
), _("H1 Headings"), ICON_NONE
,
684 &(tv_iters
.tag_class
), _("H2 Headings"), ICON_NONE
,
685 &(tv_iters
.tag_variable
), _("H3 Headings"), ICON_NONE
,
689 case GEANY_FILETYPES_CSS
:
691 tag_list_add_groups(tag_store
,
692 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
693 &(tv_iters
.tag_variable
), _("ID Selectors"), ICON_VAR
,
694 &(tv_iters
.tag_struct
), _("Type Selectors"), ICON_STRUCT
, NULL
);
697 case GEANY_FILETYPES_REST
:
698 case GEANY_FILETYPES_TXT2TAGS
:
699 case GEANY_FILETYPES_ABC
:
701 tag_list_add_groups(tag_store
,
702 &(tv_iters
.tag_namespace
), _("Chapter"), ICON_NONE
,
703 &(tv_iters
.tag_member
), _("Section"), ICON_NONE
,
704 &(tv_iters
.tag_macro
), _("Subsection"), ICON_NONE
,
705 &(tv_iters
.tag_variable
), _("Subsubsection"), ICON_NONE
,
709 case GEANY_FILETYPES_ASCIIDOC
:
711 tag_list_add_groups(tag_store
,
712 &(tv_iters
.tag_namespace
), _("Document"), ICON_NONE
,
713 &(tv_iters
.tag_member
), _("Section Level 1"), ICON_NONE
,
714 &(tv_iters
.tag_macro
), _("Section Level 2"), ICON_NONE
,
715 &(tv_iters
.tag_variable
), _("Section Level 3"), ICON_NONE
,
716 &(tv_iters
.tag_struct
), _("Section Level 4"), ICON_NONE
,
720 case GEANY_FILETYPES_RUBY
:
722 tag_list_add_groups(tag_store
,
723 &(tv_iters
.tag_namespace
), _("Modules"), ICON_NAMESPACE
,
724 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
725 &(tv_iters
.tag_member
), _("Singletons"), ICON_STRUCT
,
726 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
730 case GEANY_FILETYPES_TCL
:
732 tag_list_add_groups(tag_store
,
733 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
,
734 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
735 &(tv_iters
.tag_member
), _("Methods"), ICON_METHOD
,
736 &(tv_iters
.tag_function
), _("Procedures"), ICON_OTHER
,
740 case GEANY_FILETYPES_PYTHON
:
742 tag_list_add_groups(tag_store
,
743 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
744 &(tv_iters
.tag_member
), _("Methods"), ICON_MACRO
,
745 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
746 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
747 &(tv_iters
.tag_externvar
), _("Imports"), ICON_NAMESPACE
,
751 case GEANY_FILETYPES_VHDL
:
753 tag_list_add_groups(tag_store
,
754 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
755 &(tv_iters
.tag_class
), _("Entities"), ICON_CLASS
,
756 &(tv_iters
.tag_struct
), _("Architectures"), ICON_STRUCT
,
757 &(tv_iters
.tag_type
), _("Types"), ICON_OTHER
,
758 &(tv_iters
.tag_function
), _("Functions / Procedures"), ICON_METHOD
,
759 &(tv_iters
.tag_variable
), _("Variables / Signals"), ICON_VAR
,
760 &(tv_iters
.tag_member
), _("Processes / Blocks / Components"), ICON_MEMBER
,
761 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
765 case GEANY_FILETYPES_VERILOG
:
767 tag_list_add_groups(tag_store
,
768 &(tv_iters
.tag_type
), _("Events"), ICON_MACRO
,
769 &(tv_iters
.tag_class
), _("Modules"), ICON_CLASS
,
770 &(tv_iters
.tag_function
), _("Functions / Tasks"), ICON_METHOD
,
771 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
772 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
776 case GEANY_FILETYPES_JAVA
:
778 tag_list_add_groups(tag_store
,
779 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
780 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
781 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
782 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
783 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
784 &(tv_iters
.tag_type
), _("Enums"), ICON_STRUCT
,
785 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
789 case GEANY_FILETYPES_AS
:
791 tag_list_add_groups(tag_store
,
792 &(tv_iters
.tag_externvar
), _("Imports"), ICON_NAMESPACE
,
793 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
794 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
795 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
796 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
797 &(tv_iters
.tag_member
), _("Properties"), ICON_MEMBER
,
798 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
799 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
800 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
804 case GEANY_FILETYPES_HAXE
:
806 tag_list_add_groups(tag_store
,
807 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
808 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
809 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
810 &(tv_iters
.tag_type
), _("Types"), ICON_MACRO
,
811 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
812 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
816 case GEANY_FILETYPES_BASIC
:
818 tag_list_add_groups(tag_store
,
819 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
820 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
821 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
822 &(tv_iters
.tag_struct
), _("Types"), ICON_NAMESPACE
,
823 &(tv_iters
.tag_namespace
), _("Labels"), ICON_MEMBER
,
824 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
828 case GEANY_FILETYPES_F77
:
829 case GEANY_FILETYPES_FORTRAN
:
831 tag_list_add_groups(tag_store
,
832 &(tv_iters
.tag_namespace
), _("Module"), ICON_CLASS
,
833 &(tv_iters
.tag_struct
), _("Programs"), ICON_CLASS
,
834 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
835 &(tv_iters
.tag_function
), _("Functions / Subroutines"), ICON_METHOD
,
836 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
837 &(tv_iters
.tag_class
), _("Types"), ICON_CLASS
,
838 &(tv_iters
.tag_member
), _("Components"), ICON_MEMBER
,
839 &(tv_iters
.tag_macro
), _("Blocks"), ICON_MEMBER
,
840 &(tv_iters
.tag_type
), _("Enums"), ICON_STRUCT
,
841 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
845 case GEANY_FILETYPES_ASM
:
847 tag_list_add_groups(tag_store
,
848 &(tv_iters
.tag_namespace
), _("Labels"), ICON_NAMESPACE
,
849 &(tv_iters
.tag_function
), _("Macros"), ICON_METHOD
,
850 &(tv_iters
.tag_macro
), _("Defines"), ICON_MACRO
,
851 &(tv_iters
.tag_struct
), _("Types"), ICON_STRUCT
,
855 case GEANY_FILETYPES_MAKE
:
856 tag_list_add_groups(tag_store
,
857 &tv_iters
.tag_function
, _("Targets"), ICON_METHOD
,
858 &tv_iters
.tag_macro
, _("Macros"), ICON_MACRO
,
861 case GEANY_FILETYPES_SQL
:
863 tag_list_add_groups(tag_store
,
864 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
865 &(tv_iters
.tag_namespace
), _("Procedures"), ICON_NAMESPACE
,
866 &(tv_iters
.tag_struct
), _("Indexes"), ICON_STRUCT
,
867 &(tv_iters
.tag_class
), _("Tables"), ICON_CLASS
,
868 &(tv_iters
.tag_macro
), _("Triggers"), ICON_MACRO
,
869 &(tv_iters
.tag_member
), _("Views"), ICON_VAR
,
870 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
871 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
875 case GEANY_FILETYPES_D
:
878 if (ft_id
== GEANY_FILETYPES_D
)
879 tag_list_add_groups(tag_store
,
880 &(tv_iters
.tag_namespace
), _("Module"), ICON_NONE
, NULL
);
882 tag_list_add_groups(tag_store
,
883 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
, NULL
);
885 tag_list_add_groups(tag_store
,
886 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
887 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
888 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
889 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
890 &(tv_iters
.tag_struct
), _("Structs"), ICON_STRUCT
,
891 &(tv_iters
.tag_type
), _("Typedefs / Enums"), ICON_STRUCT
,
894 if (ft_id
!= GEANY_FILETYPES_D
)
896 tag_list_add_groups(tag_store
,
897 &(tv_iters
.tag_macro
), _("Macros"), ICON_MACRO
, NULL
);
899 tag_list_add_groups(tag_store
,
900 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
901 &(tv_iters
.tag_externvar
), _("Extern Variables"), ICON_VAR
,
902 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
, NULL
);
908 /* removes toplevel items that have no children */
909 static void hide_empty_rows(GtkTreeStore
*store
)
912 gboolean cont
= TRUE
;
914 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
))
915 return; /* stop when first iter is invalid, i.e. no elements */
919 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store
), &iter
))
920 cont
= gtk_tree_store_remove(store
, &iter
);
922 cont
= gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
);
927 static const gchar
*get_symbol_name(GeanyDocument
*doc
, const TMTag
*tag
, gboolean found_parent
)
930 const gchar
*scope
= tag
->scope
;
931 static GString
*buffer
= NULL
; /* buffer will be small so we can keep it for reuse */
932 gboolean doc_is_utf8
= FALSE
;
934 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
935 * for None at this point completely */
936 if (utils_str_equal(doc
->encoding
, "UTF-8") ||
937 utils_str_equal(doc
->encoding
, "None"))
939 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
940 * plugin might have called tm_source_file_update(), so check to be sure */
941 doc_is_utf8
= g_utf8_validate(tag
->name
, -1, NULL
);
944 utf8_name
= encodings_convert_to_utf8_from_charset(tag
->name
,
945 -1, doc
->encoding
, TRUE
);
947 utf8_name
= tag
->name
;
949 if (utf8_name
== NULL
)
953 buffer
= g_string_new(NULL
);
955 g_string_truncate(buffer
, 0);
957 /* check first char of scope is a wordchar */
958 if (!found_parent
&& scope
&&
959 strpbrk(scope
, GEANY_WORDCHARS
) == scope
)
961 const gchar
*sep
= symbols_get_context_separator(doc
->file_type
->id
);
963 g_string_append(buffer
, scope
);
964 g_string_append(buffer
, sep
);
966 g_string_append(buffer
, utf8_name
);
971 g_string_append_printf(buffer
, " [%lu]", tag
->line
);
977 static gchar
*get_symbol_tooltip(GeanyDocument
*doc
, const TMTag
*tag
)
979 gchar
*utf8_name
= tm_parser_format_function(tag
->lang
, tag
->name
,
980 tag
->arglist
, tag
->var_type
, tag
->scope
);
982 if (!utf8_name
&& tag
->var_type
&&
983 tag
->type
& (tm_tag_field_t
| tm_tag_member_t
| tm_tag_variable_t
| tm_tag_externvar_t
))
985 utf8_name
= tm_parser_format_variable(tag
->lang
, tag
->name
, tag
->var_type
);
988 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
989 * for None at this point completely */
990 if (utf8_name
!= NULL
&&
991 ! utils_str_equal(doc
->encoding
, "UTF-8") &&
992 ! utils_str_equal(doc
->encoding
, "None"))
995 encodings_convert_to_utf8_from_charset(utf8_name
, -1, doc
->encoding
, TRUE
));
1002 static const gchar
*get_parent_name(const TMTag
*tag
)
1004 return !EMPTY(tag
->scope
) ? tag
->scope
: NULL
;
1008 static GtkTreeIter
*get_tag_type_iter(TMTagType tag_type
)
1010 GtkTreeIter
*iter
= NULL
;
1014 case tm_tag_prototype_t
:
1015 case tm_tag_method_t
:
1016 case tm_tag_function_t
:
1018 iter
= &tv_iters
.tag_function
;
1021 case tm_tag_externvar_t
:
1023 iter
= &tv_iters
.tag_externvar
;
1026 case tm_tag_macro_t
:
1027 case tm_tag_macro_with_arg_t
:
1029 iter
= &tv_iters
.tag_macro
;
1032 case tm_tag_class_t
:
1034 iter
= &tv_iters
.tag_class
;
1037 case tm_tag_member_t
:
1038 case tm_tag_field_t
:
1040 iter
= &tv_iters
.tag_member
;
1043 case tm_tag_typedef_t
:
1046 iter
= &tv_iters
.tag_type
;
1049 case tm_tag_union_t
:
1050 case tm_tag_struct_t
:
1052 iter
= &tv_iters
.tag_struct
;
1055 case tm_tag_interface_t
:
1056 iter
= &tv_iters
.tag_interface
;
1058 case tm_tag_variable_t
:
1060 iter
= &tv_iters
.tag_variable
;
1063 case tm_tag_namespace_t
:
1064 case tm_tag_package_t
:
1066 iter
= &tv_iters
.tag_namespace
;
1071 iter
= &tv_iters
.tag_other
;
1074 if (G_LIKELY(iter
->stamp
!= -1))
1081 static GdkPixbuf
*get_child_icon(GtkTreeStore
*tree_store
, GtkTreeIter
*parent
)
1083 GdkPixbuf
*icon
= NULL
;
1085 if (parent
== &tv_iters
.tag_other
)
1087 return g_object_ref(symbols_icons
[ICON_VAR
].pixbuf
);
1089 /* copy parent icon */
1090 gtk_tree_model_get(GTK_TREE_MODEL(tree_store
), parent
,
1091 SYMBOLS_COLUMN_ICON
, &icon
, -1);
1096 static gboolean
tag_equal(gconstpointer v1
, gconstpointer v2
)
1098 const TMTag
*t1
= v1
;
1099 const TMTag
*t2
= v2
;
1101 return (t1
->type
== t2
->type
&& strcmp(t1
->name
, t2
->name
) == 0 &&
1102 utils_str_equal(t1
->scope
, t2
->scope
) &&
1103 /* include arglist in match to support e.g. C++ overloading */
1104 utils_str_equal(t1
->arglist
, t2
->arglist
));
1108 /* inspired from g_str_hash() */
1109 static guint
tag_hash(gconstpointer v
)
1111 const TMTag
*tag
= v
;
1115 h
= (h
<< 5) + h
+ tag
->type
;
1116 for (p
= tag
->name
; *p
!= '\0'; p
++)
1117 h
= (h
<< 5) + h
+ *p
;
1120 for (p
= tag
->scope
; *p
!= '\0'; p
++)
1121 h
= (h
<< 5) + h
+ *p
;
1123 /* for e.g. C++ overloading */
1126 for (p
= tag
->arglist
; *p
!= '\0'; p
++)
1127 h
= (h
<< 5) + h
+ *p
;
1134 /* like gtk_tree_view_expand_to_path() but with an iter */
1135 static void tree_view_expand_to_iter(GtkTreeView
*view
, GtkTreeIter
*iter
)
1137 GtkTreeModel
*model
= gtk_tree_view_get_model(view
);
1138 GtkTreePath
*path
= gtk_tree_model_get_path(model
, iter
);
1140 gtk_tree_view_expand_to_path(view
, path
);
1141 gtk_tree_path_free(path
);
1145 /* like gtk_tree_store_remove() but finds the next iter at any level */
1146 static gboolean
tree_store_remove_row(GtkTreeStore
*store
, GtkTreeIter
*iter
)
1149 gboolean has_parent
;
1152 has_parent
= gtk_tree_model_iter_parent(GTK_TREE_MODEL(store
), &parent
, iter
);
1153 cont
= gtk_tree_store_remove(store
, iter
);
1154 /* if there is no next at this level but there is a parent iter, continue from it */
1155 if (! cont
&& has_parent
)
1158 cont
= ui_tree_model_iter_any_next(GTK_TREE_MODEL(store
), iter
, FALSE
);
1165 static gint
tree_search_func(gconstpointer key
, gpointer user_data
)
1167 TreeSearchData
*data
= user_data
;
1168 gint parent_line
= GPOINTER_TO_INT(key
);
1169 gboolean new_nearest
;
1171 if (data
->found_line
== -1)
1172 data
->found_line
= parent_line
; /* initial value */
1174 new_nearest
= ABS(data
->line
- parent_line
) < ABS(data
->line
- data
->found_line
);
1176 if (parent_line
> data
->line
)
1178 if (new_nearest
&& !data
->lower
)
1179 data
->found_line
= parent_line
;
1184 data
->found_line
= parent_line
;
1186 if (parent_line
< data
->line
)
1193 static gint
tree_cmp(gconstpointer a
, gconstpointer b
, gpointer user_data
)
1195 return GPOINTER_TO_INT(a
) - GPOINTER_TO_INT(b
);
1199 static void parents_table_tree_value_free(gpointer data
)
1201 g_slice_free(GtkTreeIter
, data
);
1205 /* adds a new element in the parent table if its key is known. */
1206 static void update_parents_table(GHashTable
*table
, const TMTag
*tag
, const GtkTreeIter
*iter
)
1209 gchar
*name_free
= NULL
;
1212 if (EMPTY(tag
->scope
))
1214 /* simple case, just use the tag name */
1217 else if (! tm_parser_has_full_context(tag
->lang
))
1219 /* if the parser doesn't use fully qualified scope, use the name alone but
1220 * prevent Foo::Foo from making parent = child */
1221 if (utils_str_equal(tag
->scope
, tag
->name
))
1228 /* build the fully qualified scope as get_parent_name() would return it for a child tag */
1229 name_free
= g_strconcat(tag
->scope
, tm_parser_context_separator(tag
->lang
), tag
->name
, NULL
);
1233 if (name
&& g_hash_table_lookup_extended(table
, name
, NULL
, (gpointer
*) &tree
))
1237 tree
= g_tree_new_full(tree_cmp
, NULL
, NULL
, parents_table_tree_value_free
);
1238 g_hash_table_insert(table
, name_free
? name_free
: g_strdup(name
), tree
);
1242 g_tree_insert(tree
, GINT_TO_POINTER(tag
->line
), g_slice_dup(GtkTreeIter
, iter
));
1249 static GtkTreeIter
*parents_table_lookup(GHashTable
*table
, const gchar
*name
, guint line
)
1251 GtkTreeIter
*parent_search
= NULL
;
1254 tree
= g_hash_table_lookup(table
, name
);
1257 TreeSearchData user_data
= {-1, line
, TRUE
};
1259 /* search parent candidates for the one with the nearest
1260 * line number which is lower than the tag's line number */
1261 g_tree_search(tree
, (GCompareFunc
)tree_search_func
, &user_data
);
1262 parent_search
= g_tree_lookup(tree
, GINT_TO_POINTER(user_data
.found_line
));
1265 return parent_search
;
1269 static void parents_table_value_free(gpointer data
)
1273 g_tree_destroy(tree
);
1277 /* inserts a @data in @table on key @tag.
1278 * previous data is not overwritten if the key is duplicated, but rather the
1279 * two values are kept in a list
1281 * table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
1282 static void tags_table_insert(GHashTable
*table
, TMTag
*tag
, GList
*data
)
1284 GTree
*tree
= g_hash_table_lookup(table
, tag
);
1287 tree
= g_tree_new_full(tree_cmp
, NULL
, NULL
, NULL
);
1288 g_hash_table_insert(table
, tag
, tree
);
1290 GList
*list
= g_tree_lookup(tree
, GINT_TO_POINTER(tag
->line
));
1291 list
= g_list_prepend(list
, data
);
1292 g_tree_insert(tree
, GINT_TO_POINTER(tag
->line
), list
);
1296 /* looks up the entry in @table that best matches @tag.
1297 * if there is more than one candidate, the one that has closest line position to @tag is chosen */
1298 static GList
*tags_table_lookup(GHashTable
*table
, TMTag
*tag
)
1300 TreeSearchData user_data
= {-1, tag
->line
, FALSE
};
1301 GTree
*tree
= g_hash_table_lookup(table
, tag
);
1307 g_tree_search(tree
, (GCompareFunc
)tree_search_func
, &user_data
);
1308 list
= g_tree_lookup(tree
, GINT_TO_POINTER(user_data
.found_line
));
1309 /* return the first value in the list - we don't care which of the
1310 * tags with identical names defined on the same line we get */
1318 /* removes the element at @tag from @table.
1319 * @tag must be the exact pointer used at insertion time */
1320 static void tags_table_remove(GHashTable
*table
, TMTag
*tag
)
1322 GTree
*tree
= g_hash_table_lookup(table
, tag
);
1325 GList
*list
= g_tree_lookup(tree
, GINT_TO_POINTER(tag
->line
));
1329 /* should always be the first element as we returned the first one in
1330 * tags_table_lookup() */
1331 foreach_list(node
, list
)
1333 if (((GList
*) node
->data
)->data
== tag
)
1336 list
= g_list_delete_link(list
, node
);
1338 g_tree_remove(tree
, GINT_TO_POINTER(tag
->line
));
1340 g_tree_insert(tree
, GINT_TO_POINTER(tag
->line
), list
);
1346 static gboolean
tags_table_tree_value_free(gpointer key
, gpointer value
, gpointer data
)
1348 GList
*list
= value
;
1354 static void tags_table_value_free(gpointer data
)
1359 /* free any leftover elements. note that we can't register a value_free_func when
1360 * creating the tree because we only want to free it when destroying the tree,
1361 * not when inserting a duplicate (we handle this manually) */
1362 g_tree_foreach(tree
, tags_table_tree_value_free
, NULL
);
1363 g_tree_destroy(tree
);
1369 * Updates the tag tree for a document with the tags in *list.
1370 * @param doc a document
1371 * @param tags a pointer to a GList* holding the tags to add/update. This
1372 * list may be updated, removing updated elements.
1374 * The update is done in two passes:
1375 * 1) walking the current tree, update tags that still exist and remove the
1377 * 2) walking the remaining (non updated) tags, adds them in the list.
1379 * For better performances, we use 2 hash tables:
1380 * - one containing all the tags for lookup in the first pass (actually stores a
1381 * reference in the tags list for removing it efficiently), avoiding list search
1383 * - the other holding "tag-name":row references for tags having children, used to
1384 * lookup for a parent in both passes, avoiding tree traversal.
1386 static void update_tree_tags(GeanyDocument
*doc
, GList
**tags
)
1388 GtkTreeStore
*store
= doc
->priv
->tag_store
;
1389 GtkTreeModel
*model
= GTK_TREE_MODEL(store
);
1390 GHashTable
*parents_table
;
1391 GHashTable
*tags_table
;
1396 /* Build hash tables holding tags and parents */
1397 /* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>>
1398 * where tag_name might be a fully qualified name (with scope) if the language
1399 * parser reports scope properly (see tm_parser_has_full_context()). */
1400 parents_table
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, parents_table_value_free
);
1401 /* tags table is another representation of the @tags list,
1402 * GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
1403 tags_table
= g_hash_table_new_full(tag_hash
, tag_equal
, NULL
, tags_table_value_free
);
1404 foreach_list(item
, *tags
)
1406 TMTag
*tag
= item
->data
;
1407 const gchar
*parent_name
;
1409 tags_table_insert(tags_table
, tag
, item
);
1411 parent_name
= get_parent_name(tag
);
1413 g_hash_table_insert(parents_table
, g_strdup(parent_name
), NULL
);
1416 /* First pass, update existing rows or delete them.
1417 * It is OK to delete them since we walk top down so we would remove
1418 * parents before checking for their children, thus never implicitly
1419 * deleting an updated child */
1420 cont
= gtk_tree_model_get_iter_first(model
, &iter
);
1425 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
1426 if (! tag
) /* most probably a toplevel, skip it */
1427 cont
= ui_tree_model_iter_any_next(model
, &iter
, TRUE
);
1432 found_item
= tags_table_lookup(tags_table
, tag
);
1433 if (! found_item
) /* tag doesn't exist, remove it */
1434 cont
= tree_store_remove_row(store
, &iter
);
1435 else /* tag still exist, update it */
1437 const gchar
*parent_name
;
1438 TMTag
*found
= found_item
->data
;
1440 parent_name
= get_parent_name(found
);
1441 /* if parent is unknown, ignore it */
1442 if (parent_name
&& ! g_hash_table_lookup(parents_table
, parent_name
))
1445 if (!tm_tags_equal(tag
, found
))
1450 /* only update fields that (can) have changed (name that holds line
1451 * number, tooltip, and the tag itself) */
1452 name
= get_symbol_name(doc
, found
, parent_name
!= NULL
);
1453 tooltip
= get_symbol_tooltip(doc
, found
);
1454 gtk_tree_store_set(store
, &iter
,
1455 SYMBOLS_COLUMN_NAME
, name
,
1456 SYMBOLS_COLUMN_TOOLTIP
, tooltip
,
1457 SYMBOLS_COLUMN_TAG
, found
,
1462 update_parents_table(parents_table
, found
, &iter
);
1464 /* remove the updated tag from the table and list */
1465 tags_table_remove(tags_table
, found
);
1466 *tags
= g_list_delete_link(*tags
, found_item
);
1468 cont
= ui_tree_model_iter_any_next(model
, &iter
, TRUE
);
1475 /* Second pass, now we have a tree cleaned up from invalid rows,
1476 * we simply add new ones */
1477 foreach_list (item
, *tags
)
1479 TMTag
*tag
= item
->data
;
1480 GtkTreeIter
*parent
;
1482 parent
= get_tag_type_iter(tag
->type
);
1483 if (G_UNLIKELY(! parent
))
1484 geany_debug("Missing symbol-tree parent iter for type %d!", tag
->type
);
1489 const gchar
*parent_name
;
1491 GdkPixbuf
*icon
= get_child_icon(store
, parent
);
1493 parent_name
= get_parent_name(tag
);
1496 GtkTreeIter
*parent_search
= parents_table_lookup(parents_table
, parent_name
, tag
->line
);
1499 parent
= parent_search
;
1504 /* only expand to the iter if the parent was empty, otherwise we let the
1505 * folding as it was before (already expanded, or closed by the user) */
1506 expand
= ! gtk_tree_model_iter_has_child(model
, parent
);
1508 /* insert the new element */
1509 name
= get_symbol_name(doc
, tag
, parent_name
!= NULL
);
1510 tooltip
= get_symbol_tooltip(doc
, tag
);
1511 gtk_tree_store_insert_with_values(store
, &iter
, parent
, 0,
1512 SYMBOLS_COLUMN_NAME
, name
,
1513 SYMBOLS_COLUMN_TOOLTIP
, tooltip
,
1514 SYMBOLS_COLUMN_ICON
, icon
,
1515 SYMBOLS_COLUMN_TAG
, tag
,
1519 g_object_unref(icon
);
1521 update_parents_table(parents_table
, tag
, &iter
);
1524 tree_view_expand_to_iter(GTK_TREE_VIEW(doc
->priv
->tag_tree
), &iter
);
1528 g_hash_table_destroy(parents_table
);
1529 g_hash_table_destroy(tags_table
);
1533 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1534 * is not stable, so the order is already lost. */
1535 static gint
compare_top_level_names(const gchar
*a
, const gchar
*b
)
1540 /* This should never happen as it would mean that two or more top
1541 * level items have the same name but it can happen by typos in the translations. */
1542 if (utils_str_equal(a
, b
))
1545 foreach_ptr_array(name
, i
, top_level_iter_names
)
1547 if (utils_str_equal(name
, a
))
1549 if (utils_str_equal(name
, b
))
1552 g_warning("Couldn't find top level node '%s' or '%s'!", a
, b
);
1557 static gboolean
tag_has_missing_parent(const TMTag
*tag
, GtkTreeStore
*store
,
1560 /* if the tag has a parent tag, it should be at depth >= 2 */
1561 return !EMPTY(tag
->scope
) &&
1562 gtk_tree_store_iter_depth(store
, iter
) == 1;
1566 static gint
tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1569 gboolean sort_by_name
= GPOINTER_TO_INT(user_data
);
1570 TMTag
*tag_a
, *tag_b
;
1573 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_TAG
, &tag_a
, -1);
1574 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_TAG
, &tag_b
, -1);
1576 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1577 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1578 if (tag_a
&& !tag_has_missing_parent(tag_a
, GTK_TREE_STORE(model
), a
) &&
1579 tag_b
&& !tag_has_missing_parent(tag_b
, GTK_TREE_STORE(model
), b
))
1581 cmp
= sort_by_name
? compare_symbol(tag_a
, tag_b
) :
1582 compare_symbol_lines(tag_a
, tag_b
);
1588 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_NAME
, &astr
, -1);
1589 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_NAME
, &bstr
, -1);
1591 /* if a is toplevel, b must be also */
1592 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model
), a
) == 0)
1594 cmp
= compare_top_level_names(astr
, bstr
);
1598 /* this is what g_strcmp0() does */
1600 cmp
= -(astr
!= bstr
);
1605 cmp
= strcmp(astr
, bstr
);
1607 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1609 if (!sort_by_name
||
1610 (utils_str_equal(tag_a
->name
, tag_b
->name
) &&
1611 utils_str_equal(tag_a
->scope
, tag_b
->scope
)))
1612 cmp
= compare_symbol_lines(tag_a
, tag_b
);
1618 tm_tag_unref(tag_a
);
1619 tm_tag_unref(tag_b
);
1625 static void sort_tree(GtkTreeStore
*store
, gboolean sort_by_name
)
1627 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, tree_sort_func
,
1628 GINT_TO_POINTER(sort_by_name
), NULL
);
1630 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, GTK_SORT_ASCENDING
);
1634 gboolean
symbols_recreate_tag_list(GeanyDocument
*doc
, gint sort_mode
)
1638 g_return_val_if_fail(DOC_VALID(doc
), FALSE
);
1640 tags
= get_tag_list(doc
, tm_tag_max_t
);
1644 /* FIXME: Not sure why we detached the model here? */
1646 /* disable sorting during update because the code doesn't support correctly
1647 * models that are currently being built */
1648 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc
->priv
->tag_store
), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID
, 0);
1650 /* add grandparent type iters */
1651 add_top_level_items(doc
);
1653 update_tree_tags(doc
, &tags
);
1656 hide_empty_rows(doc
->priv
->tag_store
);
1658 if (sort_mode
== SYMBOLS_SORT_USE_PREVIOUS
)
1659 sort_mode
= doc
->priv
->symbol_list_sort_mode
;
1661 sort_tree(doc
->priv
->tag_store
, sort_mode
== SYMBOLS_SORT_BY_NAME
);
1662 doc
->priv
->symbol_list_sort_mode
= sort_mode
;
1668 /* Detects a global tags filetype from the *.lang.* language extension.
1669 * Returns NULL if there was no matching TM language. */
1670 static GeanyFiletype
*detect_global_tags_filetype(const gchar
*utf8_filename
)
1673 gchar
*shortname
= utils_strdupa(utf8_filename
);
1674 GeanyFiletype
*ft
= NULL
;
1676 tags_ext
= g_strrstr(shortname
, ".tags");
1679 *tags_ext
= '\0'; /* remove .tags extension */
1680 ft
= filetypes_detect_from_extension(shortname
);
1681 if (ft
->id
!= GEANY_FILETYPES_NONE
)
1688 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1689 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1690 * the relevant path.
1692 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1693 int symbols_generate_global_tags(int argc
, char **argv
, gboolean want_preprocess
)
1695 /* -E pre-process, -dD output user macros, -p prof info (?) */
1696 const char pre_process
[] = "gcc -E -dD -p -I.";
1700 /* Create global taglist */
1703 const char *tags_file
= argv
[1];
1707 utf8_fname
= utils_get_utf8_from_locale(tags_file
);
1708 ft
= detect_global_tags_filetype(utf8_fname
);
1713 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file
);
1716 /* load config in case of custom filetypes */
1717 filetypes_load_config(ft
->id
, FALSE
);
1719 /* load ignore list for C/C++ parser */
1720 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
1721 load_c_ignore_tags();
1723 if (want_preprocess
&& (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
))
1725 const gchar
*cflags
= getenv("CFLAGS");
1726 command
= g_strdup_printf("%s %s", pre_process
, FALLBACK(cflags
, ""));
1729 command
= NULL
; /* don't preprocess */
1731 geany_debug("Generating %s tags file.", ft
->name
);
1733 status
= tm_workspace_create_global_tags(command
, (const char **) (argv
+ 2),
1734 argc
- 2, tags_file
, ft
->lang
);
1736 symbols_finalize(); /* free c_tags_ignore data */
1739 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1746 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv
[0]);
1747 g_printerr(_("Example:\n"
1748 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1749 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv
[0]);
1756 void symbols_show_load_tags_dialog(void)
1759 GtkFileFilter
*filter
;
1761 dialog
= gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets
.window
),
1762 GTK_FILE_CHOOSER_ACTION_OPEN
,
1763 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1764 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
,
1766 gtk_widget_set_name(dialog
, "GeanyDialog");
1767 filter
= gtk_file_filter_new();
1768 gtk_file_filter_set_name(filter
, _("Geany tags file (*.*.tags)"));
1769 gtk_file_filter_add_pattern(filter
, "*.*.tags");
1770 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
1772 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1774 GSList
*flist
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog
));
1777 for (item
= flist
; item
!= NULL
; item
= g_slist_next(item
))
1779 gchar
*fname
= item
->data
;
1783 utf8_fname
= utils_get_utf8_from_locale(fname
);
1784 ft
= detect_global_tags_filetype(utf8_fname
);
1786 if (ft
!= NULL
&& symbols_load_global_tags(fname
, ft
))
1787 /* For translators: the first wildcard is the filetype, the second the filename */
1788 ui_set_statusbar(TRUE
, _("Loaded %s tags file '%s'."),
1789 filetypes_get_display_name(ft
), utf8_fname
);
1791 ui_set_statusbar(TRUE
, _("Could not load tags file '%s'."), utf8_fname
);
1796 g_slist_free(flist
);
1798 gtk_widget_destroy(dialog
);
1802 static void init_user_tags(void)
1804 GSList
*file_list
= NULL
, *list
= NULL
;
1808 dir
= g_build_filename(app
->configdir
, GEANY_TAGS_SUBDIR
, NULL
);
1809 /* create the user tags dir for next time if it doesn't exist */
1810 if (! g_file_test(dir
, G_FILE_TEST_IS_DIR
))
1811 utils_mkdir(dir
, FALSE
);
1812 file_list
= utils_get_file_list_full(dir
, TRUE
, FALSE
, NULL
);
1814 SETPTR(dir
, g_build_filename(app
->datadir
, GEANY_TAGS_SUBDIR
, NULL
));
1815 list
= utils_get_file_list_full(dir
, TRUE
, FALSE
, NULL
);
1818 file_list
= g_slist_concat(file_list
, list
);
1820 /* populate the filetype-specific tag files lists */
1821 for (node
= file_list
; node
!= NULL
; node
= node
->next
)
1823 gchar
*fname
= node
->data
;
1824 gchar
*utf8_fname
= utils_get_utf8_from_locale(fname
);
1825 GeanyFiletype
*ft
= detect_global_tags_filetype(utf8_fname
);
1829 if (FILETYPE_ID(ft
) != GEANY_FILETYPES_NONE
)
1830 ft
->priv
->tag_files
= g_slist_prepend(ft
->priv
->tag_files
, fname
);
1833 geany_debug("Unknown filetype for file '%s'.", fname
);
1838 /* don't need to delete list contents because they are now stored in
1839 * ft->priv->tag_files */
1840 g_slist_free(file_list
);
1844 static void load_user_tags(GeanyFiletypeID ft_id
)
1846 static guchar
*tags_loaded
= NULL
;
1847 static gboolean init_tags
= FALSE
;
1849 GeanyFiletype
*ft
= filetypes
[ft_id
];
1851 g_return_if_fail(ft_id
> 0);
1854 tags_loaded
= g_new0(guchar
, filetypes_array
->len
);
1855 if (tags_loaded
[ft_id
])
1857 tags_loaded
[ft_id
] = TRUE
; /* prevent reloading */
1865 for (node
= ft
->priv
->tag_files
; node
!= NULL
; node
= g_slist_next(node
))
1867 const gchar
*fname
= node
->data
;
1869 symbols_load_global_tags(fname
, ft
);
1874 static void on_goto_popup_item_activate(GtkMenuItem
*item
, TMTag
*tag
)
1876 GeanyDocument
*new_doc
, *old_doc
;
1878 g_return_if_fail(tag
);
1880 old_doc
= document_get_current();
1881 new_doc
= document_open_file(tag
->file
->file_name
, FALSE
, NULL
, NULL
);
1884 navqueue_goto_line(old_doc
, new_doc
, tag
->line
);
1888 /* FIXME: use the same icons as in the symbols tree defined in add_top_level_items() */
1889 static guint
get_tag_class(const TMTag
*tag
)
1893 case tm_tag_prototype_t
:
1894 case tm_tag_method_t
:
1895 case tm_tag_function_t
:
1897 case tm_tag_variable_t
:
1898 case tm_tag_externvar_t
:
1900 case tm_tag_macro_t
:
1901 case tm_tag_macro_with_arg_t
:
1903 case tm_tag_class_t
:
1905 case tm_tag_member_t
:
1906 case tm_tag_field_t
:
1908 case tm_tag_typedef_t
:
1910 case tm_tag_union_t
:
1911 case tm_tag_struct_t
:
1913 case tm_tag_namespace_t
:
1914 case tm_tag_package_t
:
1915 return ICON_NAMESPACE
;
1923 /* positions a popup at the caret from the ScintillaObject in @p data */
1924 static void goto_popup_position_func(GtkMenu
*menu
, gint
*x
, gint
*y
, gboolean
*push_in
, gpointer data
)
1927 GdkScreen
*screen
= gtk_widget_get_screen(GTK_WIDGET(menu
));
1929 GdkRectangle monitor
;
1931 GdkEventButton
*event_button
= g_object_get_data(G_OBJECT(menu
), "geany-button-event");
1935 /* if we got a mouse click, popup at that position */
1936 *x
= (gint
) event_button
->x_root
;
1937 *y
= (gint
) event_button
->y_root
;
1938 line_height
= 0; /* we don't want to offset below the line or anything */
1940 else /* keyboard positioning */
1942 ScintillaObject
*sci
= data
;
1943 GdkWindow
*window
= gtk_widget_get_window(GTK_WIDGET(sci
));
1944 gint pos
= sci_get_current_position(sci
);
1945 gint line
= sci_get_line_from_position(sci
, pos
);
1946 gint pos_x
= SSM(sci
, SCI_POINTXFROMPOSITION
, 0, pos
);
1947 gint pos_y
= SSM(sci
, SCI_POINTYFROMPOSITION
, 0, pos
);
1949 line_height
= SSM(sci
, SCI_TEXTHEIGHT
, line
, 0);
1951 gdk_window_get_origin(window
, x
, y
);
1956 monitor_num
= gdk_screen_get_monitor_at_point(screen
, *x
, *y
);
1958 gtk_widget_get_preferred_size(GTK_WIDGET(menu
), NULL
, &req
);
1960 #if GTK_CHECK_VERSION(3, 4, 0)
1961 gdk_screen_get_monitor_workarea(screen
, monitor_num
, &monitor
);
1963 gdk_screen_get_monitor_geometry(screen
, monitor_num
, &monitor
);
1966 /* put on one size of the X position, but within the monitor */
1967 if (gtk_widget_get_direction(GTK_WIDGET(menu
)) == GTK_TEXT_DIR_RTL
)
1969 if (*x
- req
.width
- 1 >= monitor
.x
)
1970 *x
-= req
.width
+ 1;
1971 else if (*x
+ req
.width
> monitor
.x
+ monitor
.width
)
1978 if (*x
+ req
.width
+ 1 <= monitor
.x
+ monitor
.width
)
1979 *x
= MAX(monitor
.x
, *x
+ 1);
1980 else if (*x
- req
.width
- 1 >= monitor
.x
)
1981 *x
-= req
.width
+ 1;
1983 *x
= monitor
.x
+ MAX(0, monitor
.width
- req
.width
);
1986 /* try to put, in order:
1987 * 1. below the Y position, under the line
1988 * 2. above the Y position
1989 * 3. within the monitor */
1990 if (*y
+ line_height
+ req
.height
<= monitor
.y
+ monitor
.height
)
1991 *y
= MAX(monitor
.y
, *y
+ line_height
);
1992 else if (*y
- req
.height
>= monitor
.y
)
1993 *y
= *y
- req
.height
;
1995 *y
= monitor
.y
+ MAX(0, monitor
.height
- req
.height
);
2001 static void show_goto_popup(GeanyDocument
*doc
, GPtrArray
*tags
, gboolean have_best
)
2003 GtkWidget
*first
= NULL
;
2006 GdkEventButton
*button_event
= NULL
;
2009 gchar
**short_names
, **file_names
;
2010 menu
= gtk_menu_new();
2012 /* If popup would show multiple files present a smart file list that allows
2013 * to easily distinguish the files while avoiding the file paths in their entirety */
2014 file_names
= g_new(gchar
*, tags
->len
);
2015 foreach_ptr_array(tmtag
, i
, tags
)
2016 file_names
[i
] = tmtag
->file
->file_name
;
2017 short_names
= utils_strv_shorten_file_list(file_names
, tags
->len
);
2020 foreach_ptr_array(tmtag
, i
, tags
)
2025 gchar
*fname
= short_names
[i
];
2028 if (! first
&& have_best
)
2029 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
2030 text
= g_markup_printf_escaped(_("<b>%s: %lu</b>"), fname
, tmtag
->line
);
2032 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
2033 text
= g_markup_printf_escaped(_("%s: %lu"), fname
, tmtag
->line
);
2035 image
= gtk_image_new_from_pixbuf(symbols_icons
[get_tag_class(tmtag
)].pixbuf
);
2036 label
= g_object_new(GTK_TYPE_LABEL
, "label", text
, "use-markup", TRUE
, "xalign", 0.0, NULL
);
2037 item
= g_object_new(GTK_TYPE_IMAGE_MENU_ITEM
, "image", image
, "child", label
, "always-show-image", TRUE
, NULL
);
2038 g_signal_connect_data(item
, "activate", G_CALLBACK(on_goto_popup_item_activate
),
2039 tm_tag_ref(tmtag
), (GClosureNotify
) tm_tag_unref
, 0);
2040 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
2048 g_free(short_names
);
2050 gtk_widget_show_all(menu
);
2052 if (first
) /* always select the first item for better keyboard navigation */
2053 g_signal_connect(menu
, "realize", G_CALLBACK(gtk_menu_shell_select_item
), first
);
2055 event
= gtk_get_current_event();
2056 if (event
&& event
->type
== GDK_BUTTON_PRESS
)
2057 button_event
= (GdkEventButton
*) event
;
2059 gdk_event_free(event
);
2061 g_object_set_data_full(G_OBJECT(menu
), "geany-button-event", button_event
,
2062 button_event
? (GDestroyNotify
) gdk_event_free
: NULL
);
2063 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, goto_popup_position_func
, doc
->editor
->sci
,
2064 button_event
? button_event
->button
: 0, gtk_get_current_event_time ());
2068 static gint
compare_tags_by_name_line(gconstpointer ptr1
, gconstpointer ptr2
)
2071 TMTag
*t1
= *((TMTag
**) ptr1
);
2072 TMTag
*t2
= *((TMTag
**) ptr2
);
2074 res
= g_strcmp0(t1
->file
->short_name
, t2
->file
->short_name
);
2077 return t1
->line
- t2
->line
;
2081 static TMTag
*find_best_goto_tag(GeanyDocument
*doc
, GPtrArray
*tags
)
2086 /* first check if we have a tag in the current file */
2087 foreach_ptr_array(tag
, i
, tags
)
2089 if (g_strcmp0(doc
->real_path
, tag
->file
->file_name
) == 0)
2093 /* next check if we have a tag for some of the open documents */
2094 foreach_ptr_array(tag
, i
, tags
)
2100 if (g_strcmp0(documents
[j
]->real_path
, tag
->file
->file_name
) == 0)
2105 /* next check if we have a tag for a file inside the current document's directory */
2106 foreach_ptr_array(tag
, i
, tags
)
2108 gchar
*dir
= g_path_get_dirname(doc
->real_path
);
2110 if (g_str_has_prefix(tag
->file
->file_name
, dir
))
2122 static GPtrArray
*filter_tags(GPtrArray
*tags
, TMTag
*current_tag
, gboolean definition
)
2124 const TMTagType forward_types
= tm_tag_prototype_t
| tm_tag_externvar_t
;
2125 TMTag
*tmtag
, *last_tag
= NULL
;
2126 GPtrArray
*filtered_tags
= g_ptr_array_new();
2129 foreach_ptr_array(tmtag
, i
, tags
)
2131 if ((definition
&& !(tmtag
->type
& forward_types
)) ||
2132 (!definition
&& (tmtag
->type
& forward_types
)))
2134 /* If there are typedefs of e.g. a struct such as
2135 * "typedef struct Foo {} Foo;", filter out the typedef unless
2136 * cursor is at the struct name. */
2137 if (last_tag
!= NULL
&& last_tag
->file
== tmtag
->file
&&
2138 last_tag
->type
!= tm_tag_typedef_t
&& tmtag
->type
== tm_tag_typedef_t
)
2140 if (last_tag
== current_tag
)
2141 g_ptr_array_add(filtered_tags
, tmtag
);
2143 else if (tmtag
!= current_tag
)
2144 g_ptr_array_add(filtered_tags
, tmtag
);
2150 return filtered_tags
;
2154 static gboolean
goto_tag(const gchar
*name
, gboolean definition
)
2156 const TMTagType forward_types
= tm_tag_prototype_t
| tm_tag_externvar_t
;
2157 TMTag
*tmtag
, *current_tag
= NULL
;
2158 GeanyDocument
*old_doc
= document_get_current();
2159 gboolean found
= FALSE
;
2160 const GPtrArray
*all_tags
;
2161 GPtrArray
*tags
, *filtered_tags
;
2163 guint current_line
= sci_get_current_line(old_doc
->editor
->sci
) + 1;
2165 all_tags
= tm_workspace_find(name
, NULL
, tm_tag_max_t
, NULL
, old_doc
->file_type
->lang
);
2167 /* get rid of global tags and find tag at current line */
2168 tags
= g_ptr_array_new();
2169 foreach_ptr_array(tmtag
, i
, all_tags
)
2173 g_ptr_array_add(tags
, tmtag
);
2174 if (tmtag
->file
== old_doc
->tm_file
&& tmtag
->line
== current_line
)
2175 current_tag
= tmtag
;
2180 /* swap definition/declaration search */
2181 definition
= current_tag
->type
& forward_types
;
2183 filtered_tags
= filter_tags(tags
, current_tag
, definition
);
2184 if (filtered_tags
->len
== 0)
2186 /* if we didn't find anything, try again with the opposite type */
2187 g_ptr_array_free(filtered_tags
, TRUE
);
2188 filtered_tags
= filter_tags(tags
, current_tag
, !definition
);
2190 g_ptr_array_free(tags
, TRUE
);
2191 tags
= filtered_tags
;
2195 GeanyDocument
*new_doc
;
2197 tmtag
= tags
->pdata
[0];
2198 new_doc
= document_find_by_real_path(tmtag
->file
->file_name
);
2201 /* not found in opened document, should open */
2202 new_doc
= document_open_file(tmtag
->file
->file_name
, FALSE
, NULL
, NULL
);
2204 navqueue_goto_line(old_doc
, new_doc
, tmtag
->line
);
2206 else if (tags
->len
> 1)
2208 GPtrArray
*tag_list
;
2209 TMTag
*tag
, *best_tag
;
2211 g_ptr_array_sort(tags
, compare_tags_by_name_line
);
2212 best_tag
= find_best_goto_tag(old_doc
, tags
);
2214 tag_list
= g_ptr_array_new();
2216 g_ptr_array_add(tag_list
, best_tag
);
2217 foreach_ptr_array(tag
, i
, tags
)
2219 if (tag
!= best_tag
)
2220 g_ptr_array_add(tag_list
, tag
);
2222 show_goto_popup(old_doc
, tag_list
, best_tag
!= NULL
);
2224 g_ptr_array_free(tag_list
, TRUE
);
2227 found
= tags
->len
> 0;
2228 g_ptr_array_free(tags
, TRUE
);
2234 gboolean
symbols_goto_tag(const gchar
*name
, gboolean definition
)
2236 if (goto_tag(name
, definition
))
2239 /* if we are here, there was no match and we are beeping ;-) */
2243 ui_set_statusbar(FALSE
, _("Forward declaration \"%s\" not found."), name
);
2245 ui_set_statusbar(FALSE
, _("Definition of \"%s\" not found."), name
);
2250 /* This could perhaps be improved to check for #if, class etc. */
2251 static gint
get_function_fold_number(GeanyDocument
*doc
)
2253 /* for Java the functions are always one fold level above the class scope */
2254 if (doc
->file_type
->id
== GEANY_FILETYPES_JAVA
)
2255 return SC_FOLDLEVELBASE
+ 1;
2257 return SC_FOLDLEVELBASE
;
2261 /* Should be used only with get_current_tag_cached.
2262 * tag_types caching might trigger recomputation too often but this isn't used differently often
2263 * enough to be an issue for now */
2264 static gboolean
current_tag_changed(GeanyDocument
*doc
, gint cur_line
, gint fold_level
, guint tag_types
)
2266 static gint old_line
= -2;
2267 static GeanyDocument
*old_doc
= NULL
;
2268 static gint old_fold_num
= -1;
2269 static guint old_tag_types
= 0;
2270 const gint fold_num
= fold_level
& SC_FOLDLEVELNUMBERMASK
;
2273 /* check if the cached line and file index have changed since last time: */
2274 if (doc
== NULL
|| doc
!= old_doc
|| old_tag_types
!= tag_types
)
2276 else if (cur_line
== old_line
)
2280 /* if the line has only changed by 1 */
2281 if (abs(cur_line
- old_line
) == 1)
2283 /* It's the same function if the fold number hasn't changed */
2284 ret
= (fold_num
!= old_fold_num
);
2289 /* record current line and file index for next time */
2290 old_line
= cur_line
;
2292 old_fold_num
= fold_num
;
2293 old_tag_types
= tag_types
;
2298 /* Parse the function name up to 2 lines before tag_line.
2299 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2300 * type or argument names can be confused with the function name. */
2301 static gchar
*parse_function_at_line(ScintillaObject
*sci
, gint tag_line
)
2303 gint start
, end
, max_pos
;
2306 switch (sci_get_lexer(sci
))
2308 case SCLEX_RUBY
: fn_style
= SCE_RB_DEFNAME
; break;
2309 case SCLEX_PYTHON
: fn_style
= SCE_P_DEFNAME
; break;
2310 default: fn_style
= SCE_C_IDENTIFIER
; /* several lexers use SCE_C_IDENTIFIER */
2312 start
= sci_get_position_from_line(sci
, tag_line
- 2);
2313 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
2314 while (start
< max_pos
&& sci_get_style_at(sci
, start
) != fn_style
)
2318 while (end
< max_pos
&& sci_get_style_at(sci
, end
) == fn_style
)
2323 return sci_get_contents_range(sci
, start
, end
);
2327 /* Parse the function name */
2328 static gchar
*parse_cpp_function_at_line(ScintillaObject
*sci
, gint tag_line
)
2330 gint start
, end
, first_pos
, max_pos
;
2334 first_pos
= end
= sci_get_position_from_line(sci
, tag_line
);
2335 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
2337 /* goto the begin of function body */
2338 while (end
< max_pos
&&
2339 (tmp
= sci_get_char_at(sci
, end
)) != '{' &&
2341 if (tmp
== 0) end
--;
2343 /* go back to the end of function identifier */
2344 while (end
> 0 && end
> first_pos
- 500 &&
2345 (tmp
= sci_get_char_at(sci
, end
)) != '(' &&
2348 if (end
< 0) end
= 0;
2350 /* skip whitespaces between identifier and ( */
2351 while (end
> 0 && isspace(sci_get_char_at(sci
, end
))) end
--;
2354 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2355 while (start
>= 0 && ((tmp
= sci_get_style_at(sci
, start
)) == SCE_C_IDENTIFIER
2356 || tmp
== SCE_C_GLOBALCLASS
2357 || (c
= sci_get_char_at(sci
, start
)) == '~'
2360 if (start
!= 0 && start
< end
) start
++; /* correct for last non-matching char */
2362 if (start
== end
) return NULL
;
2363 return sci_get_contents_range(sci
, start
, end
+ 1);
2367 /* gets the fold header after or on @line, but skipping folds created because of parentheses */
2368 static gint
get_fold_header_after(ScintillaObject
*sci
, gint line
)
2370 const gint line_count
= sci_get_line_count(sci
);
2372 for (; line
< line_count
; line
++)
2374 if (sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
)
2376 const gint last_child
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
2377 const gint line_end
= sci_get_line_end_position(sci
, line
);
2378 const gint lexer
= sci_get_lexer(sci
);
2379 gint parenthesis_match_line
= -1;
2381 /* now find any unbalanced open parenthesis on the line and see where the matching
2382 * brace would be, mimicking what folding on () does */
2383 for (gint pos
= sci_get_position_from_line(sci
, line
); pos
< line_end
; pos
++)
2385 if (highlighting_is_code_style(lexer
, sci_get_style_at(sci
, pos
)) &&
2386 sci_get_char_at(sci
, pos
) == '(')
2388 const gint matching
= sci_find_matching_brace(sci
, pos
);
2392 parenthesis_match_line
= sci_get_line_from_position(sci
, matching
);
2393 if (parenthesis_match_line
!= line
)
2394 break; /* match is on a different line, we found a possible fold */
2396 pos
= matching
; /* just skip the range and continue searching */
2401 /* if the matching parenthesis matches the fold level, skip it and continue.
2402 * it matches if it either spans the same lines, or spans one more but the next one is
2403 * a fold header (in which case the last child of the fold is one less to let the
2404 * header be at the parent level) */
2405 if ((parenthesis_match_line
== last_child
) ||
2406 (parenthesis_match_line
== last_child
+ 1 &&
2407 sci_get_fold_level(sci
, parenthesis_match_line
) & SC_FOLDLEVELHEADERFLAG
))
2418 static gint
get_current_tag_name(GeanyDocument
*doc
, gchar
**tagname
, TMTagType tag_types
)
2423 line
= sci_get_current_line(doc
->editor
->sci
);
2424 parent
= sci_get_fold_parent(doc
->editor
->sci
, line
);
2425 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2426 if (parent
>= 0 && doc
->tm_file
!= NULL
&& doc
->tm_file
->tags_array
!= NULL
&&
2427 (! doc
->changed
|| editor_prefs
.autocompletion_update_freq
> 0))
2429 const TMTag
*tag
= tm_get_current_tag(doc
->tm_file
->tags_array
, parent
+ 1, tag_types
);
2433 gint tag_line
= tag
->line
- 1;
2434 gint last_child
= line
+ 1;
2436 /* if it may be a false positive because we're inside a fold level not inside anything
2437 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2438 * right after the tag we got from TM.
2439 * Additionally, we perform parentheses matching on the initial line not to get confused
2440 * by folding on () in case the parameter list spans multiple lines */
2441 if (abs(tag_line
- parent
) > 1)
2443 const gint tag_fold
= get_fold_header_after(doc
->editor
->sci
, tag_line
);
2445 last_child
= SSM(doc
->editor
->sci
, SCI_GETLASTCHILD
, tag_fold
, -1);
2448 if (line
<= last_child
)
2451 *tagname
= g_strconcat(tag
->scope
,
2452 symbols_get_context_separator(doc
->file_type
->id
), tag
->name
, NULL
);
2454 *tagname
= g_strdup(tag
->name
);
2460 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2461 * to dirty and inaccurate hand-parsing */
2462 else if (parent
>= 0 && doc
->file_type
!= NULL
&& doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
2464 const gint fn_fold
= get_function_fold_number(doc
);
2465 gint tag_line
= parent
;
2466 gint fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
2468 /* find the top level fold point */
2469 while (tag_line
>= 0 && (fold_level
& SC_FOLDLEVELNUMBERMASK
) != fn_fold
)
2471 tag_line
= sci_get_fold_parent(doc
->editor
->sci
, tag_line
);
2472 fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
2479 if (sci_get_lexer(doc
->editor
->sci
) == SCLEX_CPP
)
2480 cur_tag
= parse_cpp_function_at_line(doc
->editor
->sci
, tag_line
);
2482 cur_tag
= parse_function_at_line(doc
->editor
->sci
, tag_line
);
2484 if (cur_tag
!= NULL
)
2492 *tagname
= g_strdup(_("unknown"));
2497 static gint
get_current_tag_name_cached(GeanyDocument
*doc
, const gchar
**tagname
, TMTagType tag_types
)
2499 static gint tag_line
= -1;
2500 static gchar
*cur_tag
= NULL
;
2502 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, -1);
2504 if (doc
== NULL
) /* reset current function */
2506 current_tag_changed(NULL
, -1, -1, 0);
2508 cur_tag
= g_strdup(_("unknown"));
2509 if (tagname
!= NULL
)
2515 gint line
= sci_get_current_line(doc
->editor
->sci
);
2516 gint fold_level
= sci_get_fold_level(doc
->editor
->sci
, line
);
2518 if (current_tag_changed(doc
, line
, fold_level
, tag_types
))
2521 tag_line
= get_current_tag_name(doc
, &cur_tag
, tag_types
);
2530 /* Sets *tagname to point at the current function or tag name.
2531 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2532 * call to this function.
2533 * Returns: line number of the current tag, or -1 if unknown. */
2534 gint
symbols_get_current_function(GeanyDocument
*doc
, const gchar
**tagname
)
2536 return get_current_tag_name_cached(doc
, tagname
, tm_tag_function_t
| tm_tag_method_t
);
2540 /* same as symbols_get_current_function() but finds class, namespaces and more */
2541 gint
symbols_get_current_scope(GeanyDocument
*doc
, const gchar
**tagname
)
2543 TMTagType tag_types
= (tm_tag_function_t
| tm_tag_method_t
| tm_tag_class_t
|
2544 tm_tag_struct_t
| tm_tag_enum_t
| tm_tag_union_t
| tm_tag_namespace_t
);
2546 return get_current_tag_name_cached(doc
, tagname
, tag_types
);
2550 static void on_symbol_tree_sort_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
2552 gint sort_mode
= GPOINTER_TO_INT(user_data
);
2553 GeanyDocument
*doc
= document_get_current();
2555 if (ignore_callback
)
2559 doc
->has_tags
= symbols_recreate_tag_list(doc
, sort_mode
);
2563 static void on_symbol_tree_menu_show(GtkWidget
*widget
,
2566 GeanyDocument
*doc
= document_get_current();
2569 enable
= doc
&& doc
->has_tags
;
2570 gtk_widget_set_sensitive(symbol_menu
.sort_by_name
, enable
);
2571 gtk_widget_set_sensitive(symbol_menu
.sort_by_appearance
, enable
);
2572 gtk_widget_set_sensitive(symbol_menu
.expand_all
, enable
);
2573 gtk_widget_set_sensitive(symbol_menu
.collapse_all
, enable
);
2574 gtk_widget_set_sensitive(symbol_menu
.find_usage
, enable
);
2575 gtk_widget_set_sensitive(symbol_menu
.find_doc_usage
, enable
);
2580 ignore_callback
= TRUE
;
2582 if (doc
->priv
->symbol_list_sort_mode
== SYMBOLS_SORT_BY_NAME
)
2583 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_name
), TRUE
);
2585 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_appearance
), TRUE
);
2587 ignore_callback
= FALSE
;
2591 static void on_expand_collapse(GtkWidget
*widget
, gpointer user_data
)
2593 gboolean expand
= GPOINTER_TO_INT(user_data
);
2594 GeanyDocument
*doc
= document_get_current();
2599 g_return_if_fail(doc
->priv
->tag_tree
);
2602 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2604 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2608 static void on_find_usage(GtkWidget
*widget
, G_GNUC_UNUSED gpointer unused
)
2611 GtkTreeSelection
*selection
;
2612 GtkTreeModel
*model
;
2616 doc
= document_get_current();
2620 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2621 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
2622 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
2625 if (widget
== symbol_menu
.find_in_files
)
2626 search_show_find_in_files_dialog_full(tag
->name
, NULL
);
2628 search_find_usage(tag
->name
, tag
->name
, GEANY_FIND_WHOLEWORD
| GEANY_FIND_MATCHCASE
,
2629 widget
== symbol_menu
.find_usage
);
2636 static void create_taglist_popup_menu(void)
2638 GtkWidget
*item
, *menu
;
2640 tv
.popup_taglist
= menu
= gtk_menu_new();
2642 symbol_menu
.expand_all
= item
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
2643 gtk_widget_show(item
);
2644 gtk_container_add(GTK_CONTAINER(menu
), item
);
2645 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GINT_TO_POINTER(TRUE
));
2647 symbol_menu
.collapse_all
= item
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
2648 gtk_widget_show(item
);
2649 gtk_container_add(GTK_CONTAINER(menu
), item
);
2650 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GINT_TO_POINTER(FALSE
));
2652 item
= gtk_separator_menu_item_new();
2653 gtk_widget_show(item
);
2654 gtk_container_add(GTK_CONTAINER(menu
), item
);
2656 symbol_menu
.sort_by_name
= item
= gtk_radio_menu_item_new_with_mnemonic(NULL
,
2657 _("Sort by _Name"));
2658 gtk_widget_show(item
);
2659 gtk_container_add(GTK_CONTAINER(menu
), item
);
2660 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
2661 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME
));
2663 symbol_menu
.sort_by_appearance
= item
= gtk_radio_menu_item_new_with_mnemonic_from_widget(
2664 GTK_RADIO_MENU_ITEM(item
), _("Sort by _Appearance"));
2665 gtk_widget_show(item
);
2666 gtk_container_add(GTK_CONTAINER(menu
), item
);
2667 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
2668 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE
));
2670 item
= gtk_separator_menu_item_new();
2671 gtk_widget_show(item
);
2672 gtk_container_add(GTK_CONTAINER(menu
), item
);
2674 symbol_menu
.find_usage
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find _Usage"));
2675 gtk_widget_show(item
);
2676 gtk_container_add(GTK_CONTAINER(menu
), item
);
2677 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), symbol_menu
.find_usage
);
2679 symbol_menu
.find_doc_usage
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find _Document Usage"));
2680 gtk_widget_show(item
);
2681 gtk_container_add(GTK_CONTAINER(menu
), item
);
2682 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), symbol_menu
.find_doc_usage
);
2684 symbol_menu
.find_in_files
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find in F_iles..."));
2685 gtk_widget_show(item
);
2686 gtk_container_add(GTK_CONTAINER(menu
), item
);
2687 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), NULL
);
2689 g_signal_connect(menu
, "show", G_CALLBACK(on_symbol_tree_menu_show
), NULL
);
2691 sidebar_add_common_menu_items(GTK_MENU(menu
));
2695 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
2699 g_return_if_fail(!EMPTY(doc
->real_path
));
2701 f
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
2702 if (utils_str_equal(doc
->real_path
, f
))
2703 load_c_ignore_tags();
2709 void symbols_init(void)
2714 create_taglist_popup_menu();
2716 f
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
2717 ui_add_config_file_menu_item(f
, NULL
, NULL
);
2720 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
2722 for (i
= 0; i
< G_N_ELEMENTS(symbols_icons
); i
++)
2723 symbols_icons
[i
].pixbuf
= get_tag_icon(symbols_icons
[i
].icon_name
);
2727 void symbols_finalize(void)
2731 g_strfreev(c_tags_ignore
);
2733 for (i
= 0; i
< G_N_ELEMENTS(symbols_icons
); i
++)
2735 if (symbols_icons
[i
].pixbuf
)
2736 g_object_unref(symbols_icons
[i
].pixbuf
);