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.
25 * Tag-related functions.
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.
42 #include "callbacks.h" /* FIXME: for ignore_callback */
43 #include "documentprivate.h"
45 #include "encodings.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
50 #include "sciwrappers.h"
53 #include "tm_parser.h"
60 #include "gtkcompat.h"
70 const gchar
*tag_file
;
73 /* Check before adding any more tags files, usually they should be downloaded separately. */
74 enum /* Geany tag files */
85 static TagFileInfo tag_file_info
[GTF_MAX
] =
88 {FALSE
, "pascal.tags"},
90 {FALSE
, "html_entities.tags"},
91 {FALSE
, "latex.tags"},
92 {FALSE
, "python.tags"}
95 static GPtrArray
*top_level_iter_names
= NULL
;
113 const gchar
*icon_name
;
116 symbols_icons
[N_ICONS
] = {
117 [ICON_CLASS
] = { "classviewer-class", NULL
},
118 [ICON_MACRO
] = { "classviewer-macro", NULL
},
119 [ICON_MEMBER
] = { "classviewer-member", NULL
},
120 [ICON_METHOD
] = { "classviewer-method", NULL
},
121 [ICON_NAMESPACE
] = { "classviewer-namespace", NULL
},
122 [ICON_OTHER
] = { "classviewer-other", NULL
},
123 [ICON_STRUCT
] = { "classviewer-struct", NULL
},
124 [ICON_VAR
] = { "classviewer-var", NULL
},
129 GtkWidget
*expand_all
;
130 GtkWidget
*collapse_all
;
131 GtkWidget
*sort_by_name
;
132 GtkWidget
*sort_by_appearance
;
133 GtkWidget
*find_usage
;
134 GtkWidget
*find_doc_usage
;
135 GtkWidget
*find_in_files
;
139 static void load_user_tags(GeanyFiletypeID ft_id
);
141 /* get the tags_ignore list, exported by tagmanager's options.c */
142 extern gchar
**c_tags_ignore
;
144 /* ignore certain tokens when parsing C-like syntax.
145 * Also works for reloading. */
146 static void load_c_ignore_tags(void)
148 gchar
*path
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
151 if (g_file_get_contents(path
, &content
, NULL
, NULL
))
153 /* historically we ignore the glib _DECLS for tag generation */
154 SETPTR(content
, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content
, NULL
));
156 g_strfreev(c_tags_ignore
);
157 c_tags_ignore
= g_strsplit_set(content
, " \n\r", -1);
164 void symbols_reload_config_files(void)
166 load_c_ignore_tags();
170 static gsize
get_tag_count(void)
172 GPtrArray
*tags
= tm_get_workspace()->global_tags
;
173 gsize count
= tags
? tags
->len
: 0;
179 /* wrapper for tm_workspace_load_global_tags().
180 * note that the tag count only counts new global tags added - if a tag has the same name,
181 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
182 static gboolean
symbols_load_global_tags(const gchar
*tags_file
, GeanyFiletype
*ft
)
185 gsize old_tag_count
= get_tag_count();
187 result
= tm_workspace_load_global_tags(tags_file
, ft
->lang
);
190 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file
, ft
->name
,
191 (guint
) (get_tag_count() - old_tag_count
));
197 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
198 * This provides autocompletion, calltips, etc. */
199 void symbols_global_tags_loaded(guint file_type_idx
)
204 /* load ignore list for C/C++ parser */
205 if ((file_type_idx
== GEANY_FILETYPES_C
|| file_type_idx
== GEANY_FILETYPES_CPP
) &&
206 c_tags_ignore
== NULL
)
208 load_c_ignore_tags();
211 if (cl_options
.ignore_global_tags
|| app
->tm_workspace
== NULL
)
214 /* load config in case of custom filetypes */
215 filetypes_load_config(file_type_idx
, FALSE
);
217 load_user_tags(file_type_idx
);
219 switch (file_type_idx
)
221 case GEANY_FILETYPES_CPP
:
222 symbols_global_tags_loaded(GEANY_FILETYPES_C
); /* load C global tags */
223 /* no C++ tagfile yet */
225 case GEANY_FILETYPES_C
: tag_type
= GTF_C
; break;
226 case GEANY_FILETYPES_PASCAL
:tag_type
= GTF_PASCAL
; break;
227 case GEANY_FILETYPES_LATEX
: tag_type
= GTF_LATEX
; break;
228 case GEANY_FILETYPES_PYTHON
:tag_type
= GTF_PYTHON
; break;
229 case GEANY_FILETYPES_HTML
: tag_type
= GTF_HTML_ENTITIES
; break;
230 case GEANY_FILETYPES_PHP
:
231 symbols_global_tags_loaded(GEANY_FILETYPES_HTML
); /* load HTML global tags */
237 tfi
= &tag_file_info
[tag_type
];
239 if (! tfi
->tags_loaded
)
241 gchar
*fname
= g_build_filename(app
->datadir
, GEANY_TAGS_SUBDIR
, tfi
->tag_file
, NULL
);
243 symbols_load_global_tags(fname
, filetypes
[file_type_idx
]);
244 tfi
->tags_loaded
= TRUE
;
250 GString
*symbols_find_typenames_as_string(TMParserType lang
, gboolean global
)
256 TMParserType tag_lang
;
259 typedefs
= app
->tm_workspace
->global_typename_array
;
261 typedefs
= app
->tm_workspace
->typename_array
;
263 if ((typedefs
) && (typedefs
->len
> 0))
265 const gchar
*last_name
= "";
267 s
= g_string_sized_new(typedefs
->len
* 10);
268 for (j
= 0; j
< typedefs
->len
; ++j
)
270 tag
= TM_TAG(typedefs
->pdata
[j
]);
271 tag_lang
= tag
->lang
;
273 if (tag
->name
&& tm_tag_langs_compatible(lang
, tag_lang
) &&
274 strcmp(tag
->name
, last_name
) != 0)
277 g_string_append_c(s
, ' ');
278 g_string_append(s
, tag
->name
);
279 last_name
= tag
->name
;
287 /** Gets the context separator used by the tag manager for a particular file
289 * @param ft_id File type identifier.
290 * @return The context separator string.
292 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
293 * without a context separator.
298 const gchar
*symbols_get_context_separator(gint ft_id
)
300 return tm_tag_context_separator(filetypes
[ft_id
]->lang
);
304 /* sort by name, then line */
305 static gint
compare_symbol(const TMTag
*tag_a
, const TMTag
*tag_b
)
309 if (tag_a
== NULL
|| tag_b
== NULL
)
312 if (tag_a
->name
== NULL
)
313 return -(tag_a
->name
!= tag_b
->name
);
315 if (tag_b
->name
== NULL
)
316 return tag_a
->name
!= tag_b
->name
;
318 ret
= strcmp(tag_a
->name
, tag_b
->name
);
321 return tag_a
->line
- tag_b
->line
;
327 /* sort by line, then scope */
328 static gint
compare_symbol_lines(gconstpointer a
, gconstpointer b
)
330 const TMTag
*tag_a
= TM_TAG(a
);
331 const TMTag
*tag_b
= TM_TAG(b
);
334 if (a
== NULL
|| b
== NULL
)
337 ret
= tag_a
->line
- tag_b
->line
;
340 if (tag_a
->scope
== NULL
)
341 return -(tag_a
->scope
!= tag_b
->scope
);
342 if (tag_b
->scope
== NULL
)
343 return tag_a
->scope
!= tag_b
->scope
;
345 return strcmp(tag_a
->scope
, tag_b
->scope
);
351 static GList
*get_tag_list(GeanyDocument
*doc
, TMTagType tag_types
)
353 GList
*tag_names
= NULL
;
357 g_return_val_if_fail(doc
, NULL
);
359 if (! doc
->tm_file
|| ! doc
->tm_file
->tags_array
)
362 for (i
= 0; i
< doc
->tm_file
->tags_array
->len
; ++i
)
364 tag
= TM_TAG(doc
->tm_file
->tags_array
->pdata
[i
]);
365 if (G_UNLIKELY(tag
== NULL
))
368 if (tag
->type
& tag_types
)
370 tag_names
= g_list_prepend(tag_names
, tag
);
373 tag_names
= g_list_sort(tag_names
, compare_symbol_lines
);
378 /* amount of types in the symbol list (currently max. 8 are used) */
379 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
381 struct TreeviewSymbols
383 GtkTreeIter tag_function
;
384 GtkTreeIter tag_class
;
385 GtkTreeIter tag_macro
;
386 GtkTreeIter tag_member
;
387 GtkTreeIter tag_variable
;
388 GtkTreeIter tag_externvar
;
389 GtkTreeIter tag_namespace
;
390 GtkTreeIter tag_struct
;
391 GtkTreeIter tag_interface
;
392 GtkTreeIter tag_type
;
393 GtkTreeIter tag_other
;
397 static void init_tag_iters(void)
399 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
400 * filetypes(e.g. config file to Python crashes Geany without this) */
401 tv_iters
.tag_function
.stamp
= -1;
402 tv_iters
.tag_class
.stamp
= -1;
403 tv_iters
.tag_member
.stamp
= -1;
404 tv_iters
.tag_macro
.stamp
= -1;
405 tv_iters
.tag_variable
.stamp
= -1;
406 tv_iters
.tag_externvar
.stamp
= -1;
407 tv_iters
.tag_namespace
.stamp
= -1;
408 tv_iters
.tag_struct
.stamp
= -1;
409 tv_iters
.tag_interface
.stamp
= -1;
410 tv_iters
.tag_type
.stamp
= -1;
411 tv_iters
.tag_other
.stamp
= -1;
415 static GdkPixbuf
*get_tag_icon(const gchar
*icon_name
)
417 static GtkIconTheme
*icon_theme
= NULL
;
420 if (G_UNLIKELY(x
< 0))
423 icon_theme
= gtk_icon_theme_get_default();
424 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &x
, &dummy
);
426 return gtk_icon_theme_load_icon(icon_theme
, icon_name
, x
, 0, NULL
);
430 static gboolean
find_toplevel_iter(GtkTreeStore
*store
, GtkTreeIter
*iter
, const gchar
*title
)
432 GtkTreeModel
*model
= GTK_TREE_MODEL(store
);
434 if (!gtk_tree_model_get_iter_first(model
, iter
))
440 gtk_tree_model_get(model
, iter
, SYMBOLS_COLUMN_NAME
, &candidate
, -1);
441 /* FIXME: what if 2 different items have the same name?
442 * this should never happen, but might be caused by a typo in a translation */
443 if (utils_str_equal(candidate
, title
))
451 while (gtk_tree_model_iter_next(model
, iter
));
457 /* Adds symbol list groups in (iter*, title) pairs.
458 * The list must be ended with NULL. */
459 static void G_GNUC_NULL_TERMINATED
460 tag_list_add_groups(GtkTreeStore
*tree_store
, ...)
465 g_return_if_fail(top_level_iter_names
);
467 va_start(args
, tree_store
);
468 for (; iter
= va_arg(args
, GtkTreeIter
*), iter
!= NULL
;)
470 gchar
*title
= va_arg(args
, gchar
*);
471 guint icon_id
= va_arg(args
, guint
);
472 GdkPixbuf
*icon
= NULL
;
474 if (icon_id
< N_ICONS
)
475 icon
= symbols_icons
[icon_id
].pixbuf
;
477 g_assert(title
!= NULL
);
478 g_ptr_array_add(top_level_iter_names
, title
);
480 if (!find_toplevel_iter(tree_store
, iter
, title
))
481 gtk_tree_store_append(tree_store
, iter
, NULL
);
484 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_ICON
, icon
, -1);
485 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_NAME
, title
, -1);
491 static void add_top_level_items(GeanyDocument
*doc
)
493 GeanyFiletypeID ft_id
= doc
->file_type
->id
;
494 GtkTreeStore
*tag_store
= doc
->priv
->tag_store
;
496 if (top_level_iter_names
== NULL
)
497 top_level_iter_names
= g_ptr_array_new();
499 g_ptr_array_set_size(top_level_iter_names
, 0);
505 case GEANY_FILETYPES_DIFF
:
507 tag_list_add_groups(tag_store
,
508 &(tv_iters
.tag_function
), _("Files"), ICON_NONE
, NULL
);
511 case GEANY_FILETYPES_DOCBOOK
:
513 tag_list_add_groups(tag_store
,
514 &(tv_iters
.tag_function
), _("Chapter"), ICON_NONE
,
515 &(tv_iters
.tag_class
), _("Section"), ICON_NONE
,
516 &(tv_iters
.tag_member
), _("Sect1"), ICON_NONE
,
517 &(tv_iters
.tag_macro
), _("Sect2"), ICON_NONE
,
518 &(tv_iters
.tag_variable
), _("Sect3"), ICON_NONE
,
519 &(tv_iters
.tag_struct
), _("Appendix"), ICON_NONE
,
520 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
524 case GEANY_FILETYPES_HASKELL
:
525 tag_list_add_groups(tag_store
,
526 &tv_iters
.tag_namespace
, _("Module"), ICON_NONE
,
527 &tv_iters
.tag_type
, _("Types"), ICON_NONE
,
528 &tv_iters
.tag_macro
, _("Type constructors"), ICON_NONE
,
529 &tv_iters
.tag_function
, _("Functions"), ICON_METHOD
,
532 case GEANY_FILETYPES_COBOL
:
533 tag_list_add_groups(tag_store
,
534 &tv_iters
.tag_class
, _("Program"), ICON_CLASS
,
535 &tv_iters
.tag_function
, _("File"), ICON_METHOD
,
536 &tv_iters
.tag_namespace
, _("Sections"), ICON_NAMESPACE
,
537 &tv_iters
.tag_macro
, _("Paragraph"), ICON_OTHER
,
538 &tv_iters
.tag_struct
, _("Group"), ICON_STRUCT
,
539 &tv_iters
.tag_variable
, _("Data"), ICON_VAR
,
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_MATLAB
:
571 tag_list_add_groups(tag_store
,
572 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
573 &(tv_iters
.tag_struct
), _("Structures"), ICON_STRUCT
,
577 case GEANY_FILETYPES_ABAQUS
:
579 tag_list_add_groups(tag_store
,
580 &(tv_iters
.tag_class
), _("Parts"), ICON_NONE
,
581 &(tv_iters
.tag_member
), _("Assembly"), ICON_NONE
,
582 &(tv_iters
.tag_namespace
), _("Steps"), ICON_NONE
,
586 case GEANY_FILETYPES_R
:
588 tag_list_add_groups(tag_store
,
589 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
590 &(tv_iters
.tag_other
), _("Other"), ICON_NONE
,
594 case GEANY_FILETYPES_RUST
:
596 tag_list_add_groups(tag_store
,
597 &(tv_iters
.tag_namespace
), _("Modules"), ICON_NAMESPACE
,
598 &(tv_iters
.tag_struct
), _("Structures"), ICON_STRUCT
,
599 &(tv_iters
.tag_interface
), _("Traits"), ICON_CLASS
,
600 &(tv_iters
.tag_class
), _("Implementations"), ICON_CLASS
,
601 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
602 &(tv_iters
.tag_type
), _("Typedefs / Enums"), ICON_STRUCT
,
603 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
604 &(tv_iters
.tag_macro
), _("Macros"), ICON_MACRO
,
605 &(tv_iters
.tag_member
), _("Methods"), ICON_MEMBER
,
606 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
610 case GEANY_FILETYPES_GO
:
612 tag_list_add_groups(tag_store
,
613 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
614 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
615 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
616 &(tv_iters
.tag_struct
), _("Structs"), ICON_STRUCT
,
617 &(tv_iters
.tag_type
), _("Types"), ICON_STRUCT
,
618 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
619 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
620 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
621 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
625 case GEANY_FILETYPES_PERL
:
627 tag_list_add_groups(tag_store
,
628 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
629 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
630 &(tv_iters
.tag_macro
), _("Labels"), ICON_NONE
,
631 &(tv_iters
.tag_type
), _("Constants"), ICON_NONE
,
632 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
636 case GEANY_FILETYPES_PHP
:
637 case GEANY_FILETYPES_ZEPHIR
:
639 tag_list_add_groups(tag_store
,
640 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
,
641 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
642 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
643 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
644 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
645 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
646 &(tv_iters
.tag_struct
), _("Traits"), ICON_STRUCT
,
650 case GEANY_FILETYPES_HTML
:
652 tag_list_add_groups(tag_store
,
653 &(tv_iters
.tag_function
), _("Functions"), ICON_NONE
,
654 &(tv_iters
.tag_member
), _("Anchors"), ICON_NONE
,
655 &(tv_iters
.tag_namespace
), _("H1 Headings"), ICON_NONE
,
656 &(tv_iters
.tag_class
), _("H2 Headings"), ICON_NONE
,
657 &(tv_iters
.tag_variable
), _("H3 Headings"), ICON_NONE
,
661 case GEANY_FILETYPES_CSS
:
663 tag_list_add_groups(tag_store
,
664 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
665 &(tv_iters
.tag_variable
), _("ID Selectors"), ICON_VAR
,
666 &(tv_iters
.tag_struct
), _("Type Selectors"), ICON_STRUCT
, NULL
);
669 case GEANY_FILETYPES_REST
:
670 case GEANY_FILETYPES_TXT2TAGS
:
671 case GEANY_FILETYPES_ABC
:
673 tag_list_add_groups(tag_store
,
674 &(tv_iters
.tag_namespace
), _("Chapter"), ICON_NONE
,
675 &(tv_iters
.tag_member
), _("Section"), ICON_NONE
,
676 &(tv_iters
.tag_macro
), _("Subsection"), ICON_NONE
,
677 &(tv_iters
.tag_variable
), _("Subsubsection"), ICON_NONE
,
681 case GEANY_FILETYPES_ASCIIDOC
:
683 tag_list_add_groups(tag_store
,
684 &(tv_iters
.tag_namespace
), _("Document"), ICON_NONE
,
685 &(tv_iters
.tag_member
), _("Section Level 1"), ICON_NONE
,
686 &(tv_iters
.tag_macro
), _("Section Level 2"), ICON_NONE
,
687 &(tv_iters
.tag_variable
), _("Section Level 3"), ICON_NONE
,
688 &(tv_iters
.tag_struct
), _("Section Level 4"), ICON_NONE
,
692 case GEANY_FILETYPES_RUBY
:
694 tag_list_add_groups(tag_store
,
695 &(tv_iters
.tag_namespace
), _("Modules"), ICON_NAMESPACE
,
696 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
697 &(tv_iters
.tag_member
), _("Singletons"), ICON_STRUCT
,
698 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
702 case GEANY_FILETYPES_TCL
:
704 tag_list_add_groups(tag_store
,
705 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
,
706 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
707 &(tv_iters
.tag_member
), _("Methods"), ICON_METHOD
,
708 &(tv_iters
.tag_function
), _("Procedures"), ICON_OTHER
,
712 case GEANY_FILETYPES_PYTHON
:
714 tag_list_add_groups(tag_store
,
715 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
716 &(tv_iters
.tag_member
), _("Methods"), ICON_MACRO
,
717 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
718 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
719 &(tv_iters
.tag_externvar
), _("Imports"), ICON_NAMESPACE
,
723 case GEANY_FILETYPES_VHDL
:
725 tag_list_add_groups(tag_store
,
726 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
727 &(tv_iters
.tag_class
), _("Entities"), ICON_CLASS
,
728 &(tv_iters
.tag_struct
), _("Architectures"), ICON_STRUCT
,
729 &(tv_iters
.tag_type
), _("Types"), ICON_OTHER
,
730 &(tv_iters
.tag_function
), _("Functions / Procedures"), ICON_METHOD
,
731 &(tv_iters
.tag_variable
), _("Variables / Signals"), ICON_VAR
,
732 &(tv_iters
.tag_member
), _("Processes / Blocks / Components"), ICON_MEMBER
,
733 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
737 case GEANY_FILETYPES_VERILOG
:
739 tag_list_add_groups(tag_store
,
740 &(tv_iters
.tag_type
), _("Events"), ICON_MACRO
,
741 &(tv_iters
.tag_class
), _("Modules"), ICON_CLASS
,
742 &(tv_iters
.tag_function
), _("Functions / Tasks"), ICON_METHOD
,
743 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
744 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
748 case GEANY_FILETYPES_JAVA
:
750 tag_list_add_groups(tag_store
,
751 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
752 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
753 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
754 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
755 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
756 &(tv_iters
.tag_type
), _("Enums"), ICON_STRUCT
,
757 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
761 case GEANY_FILETYPES_AS
:
763 tag_list_add_groups(tag_store
,
764 &(tv_iters
.tag_namespace
), _("Package"), ICON_NAMESPACE
,
765 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
766 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
767 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
768 &(tv_iters
.tag_member
), _("Properties"), ICON_MEMBER
,
769 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
770 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
771 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
775 case GEANY_FILETYPES_HAXE
:
777 tag_list_add_groups(tag_store
,
778 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
779 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
780 &(tv_iters
.tag_function
), _("Methods"), ICON_METHOD
,
781 &(tv_iters
.tag_type
), _("Types"), ICON_MACRO
,
782 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
783 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
787 case GEANY_FILETYPES_BASIC
:
789 tag_list_add_groups(tag_store
,
790 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
791 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
792 &(tv_iters
.tag_macro
), _("Constants"), ICON_MACRO
,
793 &(tv_iters
.tag_struct
), _("Types"), ICON_NAMESPACE
,
794 &(tv_iters
.tag_namespace
), _("Labels"), ICON_MEMBER
,
795 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
799 case GEANY_FILETYPES_F77
:
800 case GEANY_FILETYPES_FORTRAN
:
802 tag_list_add_groups(tag_store
,
803 &(tv_iters
.tag_namespace
), _("Module"), ICON_CLASS
,
804 &(tv_iters
.tag_struct
), _("Programs"), ICON_CLASS
,
805 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
806 &(tv_iters
.tag_function
), _("Functions / Subroutines"), ICON_METHOD
,
807 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
808 &(tv_iters
.tag_class
), _("Types"), ICON_CLASS
,
809 &(tv_iters
.tag_member
), _("Components"), ICON_MEMBER
,
810 &(tv_iters
.tag_macro
), _("Blocks"), ICON_MEMBER
,
811 &(tv_iters
.tag_type
), _("Enums"), ICON_STRUCT
,
812 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
816 case GEANY_FILETYPES_ASM
:
818 tag_list_add_groups(tag_store
,
819 &(tv_iters
.tag_namespace
), _("Labels"), ICON_NAMESPACE
,
820 &(tv_iters
.tag_function
), _("Macros"), ICON_METHOD
,
821 &(tv_iters
.tag_macro
), _("Defines"), ICON_MACRO
,
822 &(tv_iters
.tag_struct
), _("Types"), ICON_STRUCT
,
826 case GEANY_FILETYPES_MAKE
:
827 tag_list_add_groups(tag_store
,
828 &tv_iters
.tag_function
, _("Targets"), ICON_METHOD
,
829 &tv_iters
.tag_macro
, _("Macros"), ICON_MACRO
,
832 case GEANY_FILETYPES_SQL
:
834 tag_list_add_groups(tag_store
,
835 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
836 &(tv_iters
.tag_namespace
), _("Procedures"), ICON_NAMESPACE
,
837 &(tv_iters
.tag_struct
), _("Indexes"), ICON_STRUCT
,
838 &(tv_iters
.tag_class
), _("Tables"), ICON_CLASS
,
839 &(tv_iters
.tag_macro
), _("Triggers"), ICON_MACRO
,
840 &(tv_iters
.tag_member
), _("Views"), ICON_VAR
,
841 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
,
842 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
846 case GEANY_FILETYPES_D
:
849 if (ft_id
== GEANY_FILETYPES_D
)
850 tag_list_add_groups(tag_store
,
851 &(tv_iters
.tag_namespace
), _("Module"), ICON_NONE
, NULL
);
853 tag_list_add_groups(tag_store
,
854 &(tv_iters
.tag_namespace
), _("Namespaces"), ICON_NAMESPACE
, NULL
);
856 tag_list_add_groups(tag_store
,
857 &(tv_iters
.tag_class
), _("Classes"), ICON_CLASS
,
858 &(tv_iters
.tag_interface
), _("Interfaces"), ICON_STRUCT
,
859 &(tv_iters
.tag_function
), _("Functions"), ICON_METHOD
,
860 &(tv_iters
.tag_member
), _("Members"), ICON_MEMBER
,
861 &(tv_iters
.tag_struct
), _("Structs"), ICON_STRUCT
,
862 &(tv_iters
.tag_type
), _("Typedefs / Enums"), ICON_STRUCT
,
865 if (ft_id
!= GEANY_FILETYPES_D
)
867 tag_list_add_groups(tag_store
,
868 &(tv_iters
.tag_macro
), _("Macros"), ICON_MACRO
, NULL
);
870 tag_list_add_groups(tag_store
,
871 &(tv_iters
.tag_variable
), _("Variables"), ICON_VAR
,
872 &(tv_iters
.tag_externvar
), _("Extern Variables"), ICON_VAR
,
873 &(tv_iters
.tag_other
), _("Other"), ICON_OTHER
, NULL
);
879 /* removes toplevel items that have no children */
880 static void hide_empty_rows(GtkTreeStore
*store
)
883 gboolean cont
= TRUE
;
885 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
))
886 return; /* stop when first iter is invalid, i.e. no elements */
890 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store
), &iter
))
891 cont
= gtk_tree_store_remove(store
, &iter
);
893 cont
= gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
);
898 static const gchar
*get_symbol_name(GeanyDocument
*doc
, const TMTag
*tag
, gboolean found_parent
)
901 const gchar
*scope
= tag
->scope
;
902 static GString
*buffer
= NULL
; /* buffer will be small so we can keep it for reuse */
903 gboolean doc_is_utf8
= FALSE
;
905 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
906 * for None at this point completely */
907 if (utils_str_equal(doc
->encoding
, "UTF-8") ||
908 utils_str_equal(doc
->encoding
, "None"))
910 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
911 * plugin might have called tm_source_file_update(), so check to be sure */
912 doc_is_utf8
= g_utf8_validate(tag
->name
, -1, NULL
);
915 utf8_name
= encodings_convert_to_utf8_from_charset(tag
->name
,
916 -1, doc
->encoding
, TRUE
);
918 utf8_name
= tag
->name
;
920 if (utf8_name
== NULL
)
924 buffer
= g_string_new(NULL
);
926 g_string_truncate(buffer
, 0);
928 /* check first char of scope is a wordchar */
929 if (!found_parent
&& scope
&&
930 strpbrk(scope
, GEANY_WORDCHARS
) == scope
)
932 const gchar
*sep
= symbols_get_context_separator(doc
->file_type
->id
);
934 g_string_append(buffer
, scope
);
935 g_string_append(buffer
, sep
);
937 g_string_append(buffer
, utf8_name
);
942 g_string_append_printf(buffer
, " [%lu]", tag
->line
);
948 static gchar
*get_symbol_tooltip(GeanyDocument
*doc
, const TMTag
*tag
)
950 gchar
*utf8_name
= editor_get_calltip_text(doc
->editor
, tag
);
952 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
953 * for None at this point completely */
954 if (utf8_name
!= NULL
&&
955 ! utils_str_equal(doc
->encoding
, "UTF-8") &&
956 ! utils_str_equal(doc
->encoding
, "None"))
959 encodings_convert_to_utf8_from_charset(utf8_name
, -1, doc
->encoding
, TRUE
));
966 /* find the last word in "foo::bar::blah", e.g. "blah" */
967 static const gchar
*get_parent_name(const TMTag
*tag
, GeanyFiletypeID ft_id
)
969 const gchar
*scope
= tag
->scope
;
970 const gchar
*separator
= symbols_get_context_separator(ft_id
);
971 const gchar
*str
, *ptr
;
980 ptr
= strstr(str
, separator
);
983 str
= ptr
+ strlen(separator
);
989 return !EMPTY(str
) ? str
: NULL
;
993 static GtkTreeIter
*get_tag_type_iter(TMTagType tag_type
)
995 GtkTreeIter
*iter
= NULL
;
999 case tm_tag_prototype_t
:
1000 case tm_tag_method_t
:
1001 case tm_tag_function_t
:
1003 iter
= &tv_iters
.tag_function
;
1006 case tm_tag_externvar_t
:
1008 iter
= &tv_iters
.tag_externvar
;
1011 case tm_tag_macro_t
:
1012 case tm_tag_macro_with_arg_t
:
1014 iter
= &tv_iters
.tag_macro
;
1017 case tm_tag_class_t
:
1019 iter
= &tv_iters
.tag_class
;
1022 case tm_tag_member_t
:
1023 case tm_tag_field_t
:
1025 iter
= &tv_iters
.tag_member
;
1028 case tm_tag_typedef_t
:
1031 iter
= &tv_iters
.tag_type
;
1034 case tm_tag_union_t
:
1035 case tm_tag_struct_t
:
1037 iter
= &tv_iters
.tag_struct
;
1040 case tm_tag_interface_t
:
1041 iter
= &tv_iters
.tag_interface
;
1043 case tm_tag_variable_t
:
1045 iter
= &tv_iters
.tag_variable
;
1048 case tm_tag_namespace_t
:
1049 case tm_tag_package_t
:
1051 iter
= &tv_iters
.tag_namespace
;
1056 iter
= &tv_iters
.tag_other
;
1059 if (G_LIKELY(iter
->stamp
!= -1))
1066 static GdkPixbuf
*get_child_icon(GtkTreeStore
*tree_store
, GtkTreeIter
*parent
)
1068 GdkPixbuf
*icon
= NULL
;
1070 if (parent
== &tv_iters
.tag_other
)
1072 return g_object_ref(symbols_icons
[ICON_VAR
].pixbuf
);
1074 /* copy parent icon */
1075 gtk_tree_model_get(GTK_TREE_MODEL(tree_store
), parent
,
1076 SYMBOLS_COLUMN_ICON
, &icon
, -1);
1081 static gboolean
tag_equal(gconstpointer v1
, gconstpointer v2
)
1083 const TMTag
*t1
= v1
;
1084 const TMTag
*t2
= v2
;
1086 return (t1
->type
== t2
->type
&& strcmp(t1
->name
, t2
->name
) == 0 &&
1087 utils_str_equal(t1
->scope
, t2
->scope
) &&
1088 /* include arglist in match to support e.g. C++ overloading */
1089 utils_str_equal(t1
->arglist
, t2
->arglist
));
1093 /* inspired from g_str_hash() */
1094 static guint
tag_hash(gconstpointer v
)
1096 const TMTag
*tag
= v
;
1100 h
= (h
<< 5) + h
+ tag
->type
;
1101 for (p
= tag
->name
; *p
!= '\0'; p
++)
1102 h
= (h
<< 5) + h
+ *p
;
1105 for (p
= tag
->scope
; *p
!= '\0'; p
++)
1106 h
= (h
<< 5) + h
+ *p
;
1108 /* for e.g. C++ overloading */
1111 for (p
= tag
->arglist
; *p
!= '\0'; p
++)
1112 h
= (h
<< 5) + h
+ *p
;
1119 /* like gtk_tree_view_expand_to_path() but with an iter */
1120 static void tree_view_expand_to_iter(GtkTreeView
*view
, GtkTreeIter
*iter
)
1122 GtkTreeModel
*model
= gtk_tree_view_get_model(view
);
1123 GtkTreePath
*path
= gtk_tree_model_get_path(model
, iter
);
1125 gtk_tree_view_expand_to_path(view
, path
);
1126 gtk_tree_path_free(path
);
1130 /* like gtk_tree_store_remove() but finds the next iter at any level */
1131 static gboolean
tree_store_remove_row(GtkTreeStore
*store
, GtkTreeIter
*iter
)
1134 gboolean has_parent
;
1137 has_parent
= gtk_tree_model_iter_parent(GTK_TREE_MODEL(store
), &parent
, iter
);
1138 cont
= gtk_tree_store_remove(store
, iter
);
1139 /* if there is no next at this level but there is a parent iter, continue from it */
1140 if (! cont
&& has_parent
)
1143 cont
= ui_tree_model_iter_any_next(GTK_TREE_MODEL(store
), iter
, FALSE
);
1150 /* adds a new element in the parent table if it's key is known.
1151 * duplicates are kept */
1152 static void update_parents_table(GHashTable
*table
, const TMTag
*tag
, const gchar
*parent_name
,
1153 const GtkTreeIter
*iter
)
1156 if (g_hash_table_lookup_extended(table
, tag
->name
, NULL
, (gpointer
*) &list
) &&
1157 ! utils_str_equal(parent_name
, tag
->name
) /* prevent Foo::Foo from making parent = child */)
1161 list
= g_slice_alloc(sizeof *list
);
1163 g_hash_table_insert(table
, tag
->name
, list
);
1165 *list
= g_list_prepend(*list
, g_slice_dup(GtkTreeIter
, iter
));
1170 static void free_iter_slice_list(gpointer data
)
1172 GList
**list
= data
;
1177 foreach_list(node
, *list
)
1178 g_slice_free(GtkTreeIter
, node
->data
);
1180 g_slice_free1(sizeof *list
, list
);
1185 /* inserts a @data in @table on key @tag.
1186 * previous data is not overwritten if the key is duplicated, but rather the
1187 * two values are kept in a list
1189 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1190 static void tags_table_insert(GHashTable
*table
, TMTag
*tag
, GList
*data
)
1192 GList
*list
= g_hash_table_lookup(table
, tag
);
1193 list
= g_list_prepend(list
, data
);
1194 g_hash_table_insert(table
, tag
, list
);
1198 /* looks up the entry in @table that better matches @tag.
1199 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1200 static GList
*tags_table_lookup(GHashTable
*table
, TMTag
*tag
)
1203 GList
*node
= g_hash_table_lookup(table
, tag
);
1209 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->line - (glong) TM_TAG(b)->line)
1211 delta
= TAG_DELTA(((GList
*) node
->data
)->data
, tag
);
1212 for (node
= node
->next
; node
; node
= node
->next
)
1214 glong d
= TAG_DELTA(((GList
*) node
->data
)->data
, tag
);
1230 /* removes the element at @tag from @table.
1231 * @tag must be the exact pointer used at insertion time */
1232 static void tags_table_remove(GHashTable
*table
, TMTag
*tag
)
1234 GList
*list
= g_hash_table_lookup(table
, tag
);
1238 foreach_list(node
, list
)
1240 if (((GList
*) node
->data
)->data
== tag
)
1243 list
= g_list_delete_link(list
, node
);
1245 g_hash_table_insert(table
, tag
, list
);
1247 g_hash_table_remove(table
, tag
);
1252 static void tags_table_destroy(GHashTable
*table
)
1254 /* free any leftover elements. note that we can't register a value_free_func when
1255 * creating the hash table because we only want to free it when destroying the table,
1256 * not when inserting a duplicate (we handle this manually) */
1257 GHashTableIter iter
;
1260 g_hash_table_iter_init(&iter
, table
);
1261 while (g_hash_table_iter_next(&iter
, NULL
, &value
))
1263 g_hash_table_destroy(table
);
1268 * Updates the tag tree for a document with the tags in *list.
1269 * @param doc a document
1270 * @param tags a pointer to a GList* holding the tags to add/update. This
1271 * list may be updated, removing updated elements.
1273 * The update is done in two passes:
1274 * 1) walking the current tree, update tags that still exist and remove the
1276 * 2) walking the remaining (non updated) tags, adds them in the list.
1278 * For better performances, we use 2 hash tables:
1279 * - one containing all the tags for lookup in the first pass (actually stores a
1280 * reference in the tags list for removing it efficiently), avoiding list search
1282 * - the other holding "tag-name":row references for tags having children, used to
1283 * lookup for a parent in both passes, avoiding tree traversal.
1285 static void update_tree_tags(GeanyDocument
*doc
, GList
**tags
)
1287 GtkTreeStore
*store
= doc
->priv
->tag_store
;
1288 GtkTreeModel
*model
= GTK_TREE_MODEL(store
);
1289 GHashTable
*parents_table
;
1290 GHashTable
*tags_table
;
1295 /* Build hash tables holding tags and parents */
1296 /* parent table holds "tag-name":GtkTreeIter */
1297 parents_table
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
, free_iter_slice_list
);
1298 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1299 tags_table
= g_hash_table_new_full(tag_hash
, tag_equal
, NULL
, NULL
);
1300 foreach_list(item
, *tags
)
1302 TMTag
*tag
= item
->data
;
1305 tags_table_insert(tags_table
, tag
, item
);
1307 name
= get_parent_name(tag
, doc
->file_type
->id
);
1309 g_hash_table_insert(parents_table
, (gpointer
) name
, NULL
);
1312 /* First pass, update existing rows or delete them.
1313 * It is OK to delete them since we walk top down so we would remove
1314 * parents before checking for their children, thus never implicitly
1315 * deleting an updated child */
1316 cont
= gtk_tree_model_get_iter_first(model
, &iter
);
1321 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
1322 if (! tag
) /* most probably a toplevel, skip it */
1323 cont
= ui_tree_model_iter_any_next(model
, &iter
, TRUE
);
1328 found_item
= tags_table_lookup(tags_table
, tag
);
1329 if (! found_item
) /* tag doesn't exist, remove it */
1330 cont
= tree_store_remove_row(store
, &iter
);
1331 else /* tag still exist, update it */
1333 const gchar
*parent_name
;
1334 TMTag
*found
= found_item
->data
;
1336 parent_name
= get_parent_name(found
, doc
->file_type
->id
);
1337 /* if parent is unknown, ignore it */
1338 if (parent_name
&& ! g_hash_table_lookup(parents_table
, parent_name
))
1341 if (!tm_tags_equal(tag
, found
))
1346 /* only update fields that (can) have changed (name that holds line
1347 * number, tooltip, and the tag itself) */
1348 name
= get_symbol_name(doc
, found
, parent_name
!= NULL
);
1349 tooltip
= get_symbol_tooltip(doc
, found
);
1350 gtk_tree_store_set(store
, &iter
,
1351 SYMBOLS_COLUMN_NAME
, name
,
1352 SYMBOLS_COLUMN_TOOLTIP
, tooltip
,
1353 SYMBOLS_COLUMN_TAG
, found
,
1358 update_parents_table(parents_table
, found
, parent_name
, &iter
);
1360 /* remove the updated tag from the table and list */
1361 tags_table_remove(tags_table
, found
);
1362 *tags
= g_list_delete_link(*tags
, found_item
);
1364 cont
= ui_tree_model_iter_any_next(model
, &iter
, TRUE
);
1371 /* Second pass, now we have a tree cleaned up from invalid rows,
1372 * we simply add new ones */
1373 foreach_list (item
, *tags
)
1375 TMTag
*tag
= item
->data
;
1376 GtkTreeIter
*parent
;
1378 parent
= get_tag_type_iter(tag
->type
);
1379 if (G_UNLIKELY(! parent
))
1380 geany_debug("Missing symbol-tree parent iter for type %d!", tag
->type
);
1385 const gchar
*parent_name
;
1387 GdkPixbuf
*icon
= get_child_icon(store
, parent
);
1389 parent_name
= get_parent_name(tag
, doc
->file_type
->id
);
1393 GtkTreeIter
*parent_search
= NULL
;
1395 /* walk parent candidates to find the better one.
1396 * if there are more than one, take the one that has the closest line number
1397 * after the tag we're searching the parent for */
1398 candidates
= g_hash_table_lookup(parents_table
, parent_name
);
1402 glong delta
= G_MAXLONG
;
1403 foreach_list(node
, *candidates
)
1408 gtk_tree_model_get(GTK_TREE_MODEL(store
), node
->data
,
1409 SYMBOLS_COLUMN_TAG
, &parent_tag
, -1);
1411 d
= tag
->line
- parent_tag
->line
;
1412 if (! parent_search
|| (d
>= 0 && d
< delta
))
1415 parent_search
= node
->data
;
1417 tm_tag_unref(parent_tag
);
1422 parent
= parent_search
;
1427 /* only expand to the iter if the parent was empty, otherwise we let the
1428 * folding as it was before (already expanded, or closed by the user) */
1429 expand
= ! gtk_tree_model_iter_has_child(model
, parent
);
1431 /* insert the new element */
1432 name
= get_symbol_name(doc
, tag
, parent_name
!= NULL
);
1433 tooltip
= get_symbol_tooltip(doc
, tag
);
1434 gtk_tree_store_insert_with_values(store
, &iter
, parent
, 0,
1435 SYMBOLS_COLUMN_NAME
, name
,
1436 SYMBOLS_COLUMN_TOOLTIP
, tooltip
,
1437 SYMBOLS_COLUMN_ICON
, icon
,
1438 SYMBOLS_COLUMN_TAG
, tag
,
1442 g_object_unref(icon
);
1444 update_parents_table(parents_table
, tag
, parent_name
, &iter
);
1447 tree_view_expand_to_iter(GTK_TREE_VIEW(doc
->priv
->tag_tree
), &iter
);
1451 g_hash_table_destroy(parents_table
);
1452 tags_table_destroy(tags_table
);
1456 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1457 * is not stable, so the order is already lost. */
1458 static gint
compare_top_level_names(const gchar
*a
, const gchar
*b
)
1463 /* This should never happen as it would mean that two or more top
1464 * level items have the same name but it can happen by typos in the translations. */
1465 if (utils_str_equal(a
, b
))
1468 foreach_ptr_array(name
, i
, top_level_iter_names
)
1470 if (utils_str_equal(name
, a
))
1472 if (utils_str_equal(name
, b
))
1475 g_warning("Couldn't find top level node '%s' or '%s'!", a
, b
);
1480 static gboolean
tag_has_missing_parent(const TMTag
*tag
, GtkTreeStore
*store
,
1483 /* if the tag has a parent tag, it should be at depth >= 2 */
1484 return !EMPTY(tag
->scope
) &&
1485 gtk_tree_store_iter_depth(store
, iter
) == 1;
1489 static gint
tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1492 gboolean sort_by_name
= GPOINTER_TO_INT(user_data
);
1493 TMTag
*tag_a
, *tag_b
;
1496 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_TAG
, &tag_a
, -1);
1497 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_TAG
, &tag_b
, -1);
1499 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1500 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1501 if (tag_a
&& !tag_has_missing_parent(tag_a
, GTK_TREE_STORE(model
), a
) &&
1502 tag_b
&& !tag_has_missing_parent(tag_b
, GTK_TREE_STORE(model
), b
))
1504 cmp
= sort_by_name
? compare_symbol(tag_a
, tag_b
) :
1505 compare_symbol_lines(tag_a
, tag_b
);
1511 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_NAME
, &astr
, -1);
1512 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_NAME
, &bstr
, -1);
1514 /* if a is toplevel, b must be also */
1515 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model
), a
) == 0)
1517 cmp
= compare_top_level_names(astr
, bstr
);
1521 /* this is what g_strcmp0() does */
1523 cmp
= -(astr
!= bstr
);
1528 cmp
= strcmp(astr
, bstr
);
1530 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1532 if (!sort_by_name
||
1533 (utils_str_equal(tag_a
->name
, tag_b
->name
) &&
1534 utils_str_equal(tag_a
->scope
, tag_b
->scope
)))
1535 cmp
= compare_symbol_lines(tag_a
, tag_b
);
1541 tm_tag_unref(tag_a
);
1542 tm_tag_unref(tag_b
);
1548 static void sort_tree(GtkTreeStore
*store
, gboolean sort_by_name
)
1550 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, tree_sort_func
,
1551 GINT_TO_POINTER(sort_by_name
), NULL
);
1553 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, GTK_SORT_ASCENDING
);
1557 gboolean
symbols_recreate_tag_list(GeanyDocument
*doc
, gint sort_mode
)
1561 g_return_val_if_fail(DOC_VALID(doc
), FALSE
);
1563 tags
= get_tag_list(doc
, tm_tag_max_t
);
1567 /* FIXME: Not sure why we detached the model here? */
1569 /* disable sorting during update because the code doesn't support correctly
1570 * models that are currently being built */
1571 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc
->priv
->tag_store
), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID
, 0);
1573 /* add grandparent type iters */
1574 add_top_level_items(doc
);
1576 update_tree_tags(doc
, &tags
);
1579 hide_empty_rows(doc
->priv
->tag_store
);
1581 if (sort_mode
== SYMBOLS_SORT_USE_PREVIOUS
)
1582 sort_mode
= doc
->priv
->symbol_list_sort_mode
;
1584 sort_tree(doc
->priv
->tag_store
, sort_mode
== SYMBOLS_SORT_BY_NAME
);
1585 doc
->priv
->symbol_list_sort_mode
= sort_mode
;
1591 /* Detects a global tags filetype from the *.lang.* language extension.
1592 * Returns NULL if there was no matching TM language. */
1593 static GeanyFiletype
*detect_global_tags_filetype(const gchar
*utf8_filename
)
1596 gchar
*shortname
= utils_strdupa(utf8_filename
);
1597 GeanyFiletype
*ft
= NULL
;
1599 tags_ext
= g_strrstr(shortname
, ".tags");
1602 *tags_ext
= '\0'; /* remove .tags extension */
1603 ft
= filetypes_detect_from_extension(shortname
);
1604 if (ft
->id
!= GEANY_FILETYPES_NONE
)
1611 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1612 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1613 * the relevant path.
1615 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1616 int symbols_generate_global_tags(int argc
, char **argv
, gboolean want_preprocess
)
1618 /* -E pre-process, -dD output user macros, -p prof info (?) */
1619 const char pre_process
[] = "gcc -E -dD -p -I.";
1623 /* Create global taglist */
1626 const char *tags_file
= argv
[1];
1630 utf8_fname
= utils_get_utf8_from_locale(tags_file
);
1631 ft
= detect_global_tags_filetype(utf8_fname
);
1636 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file
);
1639 /* load config in case of custom filetypes */
1640 filetypes_load_config(ft
->id
, FALSE
);
1642 /* load ignore list for C/C++ parser */
1643 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
1644 load_c_ignore_tags();
1646 if (want_preprocess
&& (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
))
1648 const gchar
*cflags
= getenv("CFLAGS");
1649 command
= g_strdup_printf("%s %s", pre_process
, FALLBACK(cflags
, ""));
1652 command
= NULL
; /* don't preprocess */
1654 geany_debug("Generating %s tags file.", ft
->name
);
1656 status
= tm_workspace_create_global_tags(command
, (const char **) (argv
+ 2),
1657 argc
- 2, tags_file
, ft
->lang
);
1659 symbols_finalize(); /* free c_tags_ignore data */
1662 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1669 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv
[0]);
1670 g_printerr(_("Example:\n"
1671 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1672 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv
[0]);
1679 void symbols_show_load_tags_dialog(void)
1682 GtkFileFilter
*filter
;
1684 dialog
= gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets
.window
),
1685 GTK_FILE_CHOOSER_ACTION_OPEN
,
1686 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1687 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
,
1689 gtk_widget_set_name(dialog
, "GeanyDialog");
1690 filter
= gtk_file_filter_new();
1691 gtk_file_filter_set_name(filter
, _("Geany tags file (*.*.tags)"));
1692 gtk_file_filter_add_pattern(filter
, "*.*.tags");
1693 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
1695 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1697 GSList
*flist
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog
));
1700 for (item
= flist
; item
!= NULL
; item
= g_slist_next(item
))
1702 gchar
*fname
= item
->data
;
1706 utf8_fname
= utils_get_utf8_from_locale(fname
);
1707 ft
= detect_global_tags_filetype(utf8_fname
);
1709 if (ft
!= NULL
&& symbols_load_global_tags(fname
, ft
))
1710 /* For translators: the first wildcard is the filetype, the second the filename */
1711 ui_set_statusbar(TRUE
, _("Loaded %s tags file '%s'."),
1712 filetypes_get_display_name(ft
), utf8_fname
);
1714 ui_set_statusbar(TRUE
, _("Could not load tags file '%s'."), utf8_fname
);
1719 g_slist_free(flist
);
1721 gtk_widget_destroy(dialog
);
1725 static void init_user_tags(void)
1727 GSList
*file_list
= NULL
, *list
= NULL
;
1731 dir
= g_build_filename(app
->configdir
, GEANY_TAGS_SUBDIR
, NULL
);
1732 /* create the user tags dir for next time if it doesn't exist */
1733 if (! g_file_test(dir
, G_FILE_TEST_IS_DIR
))
1734 utils_mkdir(dir
, FALSE
);
1735 file_list
= utils_get_file_list_full(dir
, TRUE
, FALSE
, NULL
);
1737 SETPTR(dir
, g_build_filename(app
->datadir
, GEANY_TAGS_SUBDIR
, NULL
));
1738 list
= utils_get_file_list_full(dir
, TRUE
, FALSE
, NULL
);
1741 file_list
= g_slist_concat(file_list
, list
);
1743 /* populate the filetype-specific tag files lists */
1744 for (node
= file_list
; node
!= NULL
; node
= node
->next
)
1746 gchar
*fname
= node
->data
;
1747 gchar
*utf8_fname
= utils_get_utf8_from_locale(fname
);
1748 GeanyFiletype
*ft
= detect_global_tags_filetype(utf8_fname
);
1752 if (FILETYPE_ID(ft
) != GEANY_FILETYPES_NONE
)
1753 ft
->priv
->tag_files
= g_slist_prepend(ft
->priv
->tag_files
, fname
);
1756 geany_debug("Unknown filetype for file '%s'.", fname
);
1761 /* don't need to delete list contents because they are now stored in
1762 * ft->priv->tag_files */
1763 g_slist_free(file_list
);
1767 static void load_user_tags(GeanyFiletypeID ft_id
)
1769 static guchar
*tags_loaded
= NULL
;
1770 static gboolean init_tags
= FALSE
;
1772 GeanyFiletype
*ft
= filetypes
[ft_id
];
1774 g_return_if_fail(ft_id
> 0);
1777 tags_loaded
= g_new0(guchar
, filetypes_array
->len
);
1778 if (tags_loaded
[ft_id
])
1780 tags_loaded
[ft_id
] = TRUE
; /* prevent reloading */
1788 for (node
= ft
->priv
->tag_files
; node
!= NULL
; node
= g_slist_next(node
))
1790 const gchar
*fname
= node
->data
;
1792 symbols_load_global_tags(fname
, ft
);
1797 static void on_goto_popup_item_activate(GtkMenuItem
*item
, TMTag
*tag
)
1799 GeanyDocument
*new_doc
, *old_doc
;
1801 g_return_if_fail(tag
);
1803 old_doc
= document_get_current();
1804 new_doc
= document_open_file(tag
->file
->file_name
, FALSE
, NULL
, NULL
);
1807 navqueue_goto_line(old_doc
, new_doc
, tag
->line
);
1811 /* FIXME: use the same icons as in the symbols tree defined in add_top_level_items() */
1812 static guint
get_tag_class(const TMTag
*tag
)
1816 case tm_tag_prototype_t
:
1817 case tm_tag_method_t
:
1818 case tm_tag_function_t
:
1820 case tm_tag_variable_t
:
1821 case tm_tag_externvar_t
:
1823 case tm_tag_macro_t
:
1824 case tm_tag_macro_with_arg_t
:
1826 case tm_tag_class_t
:
1828 case tm_tag_member_t
:
1829 case tm_tag_field_t
:
1831 case tm_tag_typedef_t
:
1833 case tm_tag_union_t
:
1834 case tm_tag_struct_t
:
1836 case tm_tag_namespace_t
:
1837 case tm_tag_package_t
:
1838 return ICON_NAMESPACE
;
1846 /* positions a popup at the caret from the ScintillaObject in @p data */
1847 static void goto_popup_position_func(GtkMenu
*menu
, gint
*x
, gint
*y
, gboolean
*push_in
, gpointer data
)
1850 GdkScreen
*screen
= gtk_widget_get_screen(GTK_WIDGET(menu
));
1852 GdkRectangle monitor
;
1854 GdkEventButton
*event_button
= g_object_get_data(G_OBJECT(menu
), "geany-button-event");
1858 /* if we got a mouse click, popup at that position */
1859 *x
= (gint
) event_button
->x_root
;
1860 *y
= (gint
) event_button
->y_root
;
1861 line_height
= 0; /* we don't want to offset below the line or anything */
1863 else /* keyboard positioning */
1865 ScintillaObject
*sci
= data
;
1866 GdkWindow
*window
= gtk_widget_get_window(GTK_WIDGET(sci
));
1867 gint pos
= sci_get_current_position(sci
);
1868 gint line
= sci_get_line_from_position(sci
, pos
);
1869 gint pos_x
= scintilla_send_message(sci
, SCI_POINTXFROMPOSITION
, 0, pos
);
1870 gint pos_y
= scintilla_send_message(sci
, SCI_POINTYFROMPOSITION
, 0, pos
);
1872 line_height
= scintilla_send_message(sci
, SCI_TEXTHEIGHT
, line
, 0);
1874 gdk_window_get_origin(window
, x
, y
);
1879 monitor_num
= gdk_screen_get_monitor_at_point(screen
, *x
, *y
);
1881 #if GTK_CHECK_VERSION(3, 0, 0)
1882 gtk_widget_get_preferred_size(GTK_WIDGET(menu
), NULL
, &req
);
1884 gtk_widget_size_request(GTK_WIDGET(menu
), &req
);
1887 #if GTK_CHECK_VERSION(3, 4, 0)
1888 gdk_screen_get_monitor_workarea(screen
, monitor_num
, &monitor
);
1890 gdk_screen_get_monitor_geometry(screen
, monitor_num
, &monitor
);
1893 /* put on one size of the X position, but within the monitor */
1894 if (gtk_widget_get_direction(GTK_WIDGET(menu
)) == GTK_TEXT_DIR_RTL
)
1896 if (*x
- req
.width
- 1 >= monitor
.x
)
1897 *x
-= req
.width
+ 1;
1898 else if (*x
+ req
.width
> monitor
.x
+ monitor
.width
)
1905 if (*x
+ req
.width
+ 1 <= monitor
.x
+ monitor
.width
)
1906 *x
= MAX(monitor
.x
, *x
+ 1);
1907 else if (*x
- req
.width
- 1 >= monitor
.x
)
1908 *x
-= req
.width
+ 1;
1910 *x
= monitor
.x
+ MAX(0, monitor
.width
- req
.width
);
1913 /* try to put, in order:
1914 * 1. below the Y position, under the line
1915 * 2. above the Y position
1916 * 3. within the monitor */
1917 if (*y
+ line_height
+ req
.height
<= monitor
.y
+ monitor
.height
)
1918 *y
= MAX(monitor
.y
, *y
+ line_height
);
1919 else if (*y
- req
.height
>= monitor
.y
)
1920 *y
= *y
- req
.height
;
1922 *y
= monitor
.y
+ MAX(0, monitor
.height
- req
.height
);
1928 static void show_goto_popup(GeanyDocument
*doc
, GPtrArray
*tags
, gboolean have_best
)
1930 GtkWidget
*first
= NULL
;
1933 GdkEventButton
*button_event
= NULL
;
1937 menu
= gtk_menu_new();
1939 foreach_ptr_array(tmtag
, i
, tags
)
1944 gchar
*fname
= g_path_get_basename(tmtag
->file
->file_name
);
1947 if (! first
&& have_best
)
1948 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1949 text
= g_markup_printf_escaped(_("<b>%s: %lu</b>"), fname
, tmtag
->line
);
1951 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1952 text
= g_markup_printf_escaped(_("%s: %lu"), fname
, tmtag
->line
);
1954 image
= gtk_image_new_from_pixbuf(symbols_icons
[get_tag_class(tmtag
)].pixbuf
);
1955 label
= g_object_new(GTK_TYPE_LABEL
, "label", text
, "use-markup", TRUE
, "xalign", 0.0, NULL
);
1956 item
= g_object_new(GTK_TYPE_IMAGE_MENU_ITEM
, "image", image
, "child", label
, NULL
);
1957 g_signal_connect_data(item
, "activate", G_CALLBACK(on_goto_popup_item_activate
),
1958 tm_tag_ref(tmtag
), (GClosureNotify
) tm_tag_unref
, 0);
1959 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
1968 gtk_widget_show_all(menu
);
1970 if (first
) /* always select the first item for better keyboard navigation */
1971 g_signal_connect(menu
, "realize", G_CALLBACK(gtk_menu_shell_select_item
), first
);
1973 event
= gtk_get_current_event();
1974 if (event
&& event
->type
== GDK_BUTTON_PRESS
)
1975 button_event
= (GdkEventButton
*) event
;
1977 gdk_event_free(event
);
1979 g_object_set_data_full(G_OBJECT(menu
), "geany-button-event", button_event
,
1980 button_event
? (GDestroyNotify
) gdk_event_free
: NULL
);
1981 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, goto_popup_position_func
, doc
->editor
->sci
,
1982 button_event
? button_event
->button
: 0, gtk_get_current_event_time ());
1986 static gint
compare_tags_by_name_line(gconstpointer ptr1
, gconstpointer ptr2
)
1989 TMTag
*t1
= *((TMTag
**) ptr1
);
1990 TMTag
*t2
= *((TMTag
**) ptr2
);
1992 res
= g_strcmp0(t1
->file
->short_name
, t2
->file
->short_name
);
1995 return t1
->line
- t2
->line
;
1999 static TMTag
*find_best_goto_tag(GeanyDocument
*doc
, GPtrArray
*tags
)
2004 /* first check if we have a tag in the current file */
2005 foreach_ptr_array(tag
, i
, tags
)
2007 if (g_strcmp0(doc
->real_path
, tag
->file
->file_name
) == 0)
2011 /* next check if we have a tag for some of the open documents */
2012 foreach_ptr_array(tag
, i
, tags
)
2018 if (g_strcmp0(documents
[j
]->real_path
, tag
->file
->file_name
) == 0)
2023 /* next check if we have a tag for a file inside the current document's directory */
2024 foreach_ptr_array(tag
, i
, tags
)
2026 gchar
*dir
= g_path_get_dirname(doc
->real_path
);
2028 if (g_str_has_prefix(tag
->file
->file_name
, dir
))
2040 static gboolean
goto_tag(const gchar
*name
, gboolean definition
)
2042 const TMTagType forward_types
= tm_tag_prototype_t
| tm_tag_externvar_t
;
2044 TMTag
*tmtag
, *last_tag
;
2045 GeanyDocument
*old_doc
= document_get_current();
2046 gboolean found
= FALSE
;
2047 const GPtrArray
*all_tags
;
2048 GPtrArray
*workspace_tags
, *filtered_tags
;
2050 guint current_line
= sci_get_current_line(old_doc
->editor
->sci
) + 1;
2052 /* goto tag definition: all except prototypes / forward declarations / externs */
2053 type
= (definition
) ? tm_tag_max_t
- forward_types
: forward_types
;
2054 all_tags
= tm_workspace_find(name
, NULL
, type
, NULL
, old_doc
->file_type
->lang
);
2056 /* get rid of global tags */
2057 workspace_tags
= g_ptr_array_new();
2058 foreach_ptr_array(tmtag
, i
, all_tags
)
2061 g_ptr_array_add(workspace_tags
, tmtag
);
2064 /* If there are typedefs of e.g. a struct such as "typedef struct Foo {} Foo;",
2065 * keep just one of the names in the list - when the cursor is on the struct
2066 * name, keep the typename, otherwise keep the struct name. */
2068 filtered_tags
= g_ptr_array_new();
2069 foreach_ptr_array(tmtag
, i
, workspace_tags
)
2071 if (last_tag
!= NULL
&& last_tag
->file
== tmtag
->file
&&
2072 last_tag
->type
!= tm_tag_typedef_t
&& tmtag
->type
== tm_tag_typedef_t
)
2074 if (last_tag
->line
== current_line
&& filtered_tags
->len
> 0)
2075 /* if cursor on struct, replace struct with the typedef */
2076 filtered_tags
->pdata
[filtered_tags
->len
-1] = tmtag
;
2077 /* if cursor anywhere else, use struct (already added) and discard typedef */
2080 g_ptr_array_add(filtered_tags
, tmtag
);
2084 g_ptr_array_free(workspace_tags
, TRUE
);
2085 workspace_tags
= filtered_tags
;
2087 if (workspace_tags
->len
== 1)
2089 GeanyDocument
*new_doc
;
2091 tmtag
= workspace_tags
->pdata
[0];
2092 new_doc
= document_find_by_real_path(
2093 tmtag
->file
->file_name
);
2097 /* If we are already on the tag line, swap definition/declaration */
2098 if (new_doc
== old_doc
&& tmtag
->line
== current_line
)
2100 if (goto_tag(name
, !definition
))
2106 /* not found in opened document, should open */
2107 new_doc
= document_open_file(tmtag
->file
->file_name
, FALSE
, NULL
, NULL
);
2110 if (!found
&& navqueue_goto_line(old_doc
, new_doc
, tmtag
->line
))
2113 else if (workspace_tags
->len
> 1)
2116 TMTag
*tag
, *best_tag
;
2118 g_ptr_array_sort(workspace_tags
, compare_tags_by_name_line
);
2119 best_tag
= find_best_goto_tag(old_doc
, workspace_tags
);
2121 tags
= g_ptr_array_new();
2123 g_ptr_array_add(tags
, best_tag
);
2124 foreach_ptr_array(tag
, i
, workspace_tags
)
2126 if (tag
!= best_tag
)
2127 g_ptr_array_add(tags
, tag
);
2129 show_goto_popup(old_doc
, tags
, best_tag
!= NULL
);
2131 g_ptr_array_free(tags
, TRUE
);
2135 g_ptr_array_free(workspace_tags
, TRUE
);
2141 gboolean
symbols_goto_tag(const gchar
*name
, gboolean definition
)
2143 if (goto_tag(name
, definition
))
2146 /* if we are here, there was no match and we are beeping ;-) */
2150 ui_set_statusbar(FALSE
, _("Forward declaration \"%s\" not found."), name
);
2152 ui_set_statusbar(FALSE
, _("Definition of \"%s\" not found."), name
);
2157 /* This could perhaps be improved to check for #if, class etc. */
2158 static gint
get_function_fold_number(GeanyDocument
*doc
)
2160 /* for Java the functions are always one fold level above the class scope */
2161 if (doc
->file_type
->id
== GEANY_FILETYPES_JAVA
)
2162 return SC_FOLDLEVELBASE
+ 1;
2164 return SC_FOLDLEVELBASE
;
2168 /* Should be used only with get_current_tag_cached.
2169 * tag_types caching might trigger recomputation too often but this isn't used differently often
2170 * enough to be an issue for now */
2171 static gboolean
current_tag_changed(GeanyDocument
*doc
, gint cur_line
, gint fold_level
, guint tag_types
)
2173 static gint old_line
= -2;
2174 static GeanyDocument
*old_doc
= NULL
;
2175 static gint old_fold_num
= -1;
2176 static guint old_tag_types
= 0;
2177 const gint fold_num
= fold_level
& SC_FOLDLEVELNUMBERMASK
;
2180 /* check if the cached line and file index have changed since last time: */
2181 if (doc
== NULL
|| doc
!= old_doc
|| old_tag_types
!= tag_types
)
2183 else if (cur_line
== old_line
)
2187 /* if the line has only changed by 1 */
2188 if (abs(cur_line
- old_line
) == 1)
2190 /* It's the same function if the fold number hasn't changed */
2191 ret
= (fold_num
!= old_fold_num
);
2196 /* record current line and file index for next time */
2197 old_line
= cur_line
;
2199 old_fold_num
= fold_num
;
2200 old_tag_types
= tag_types
;
2205 /* Parse the function name up to 2 lines before tag_line.
2206 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2207 * type or argument names can be confused with the function name. */
2208 static gchar
*parse_function_at_line(ScintillaObject
*sci
, gint tag_line
)
2210 gint start
, end
, max_pos
;
2213 switch (sci_get_lexer(sci
))
2215 case SCLEX_RUBY
: fn_style
= SCE_RB_DEFNAME
; break;
2216 case SCLEX_PYTHON
: fn_style
= SCE_P_DEFNAME
; break;
2217 default: fn_style
= SCE_C_IDENTIFIER
; /* several lexers use SCE_C_IDENTIFIER */
2219 start
= sci_get_position_from_line(sci
, tag_line
- 2);
2220 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
2221 while (start
< max_pos
&& sci_get_style_at(sci
, start
) != fn_style
)
2225 while (end
< max_pos
&& sci_get_style_at(sci
, end
) == fn_style
)
2230 return sci_get_contents_range(sci
, start
, end
);
2234 /* Parse the function name */
2235 static gchar
*parse_cpp_function_at_line(ScintillaObject
*sci
, gint tag_line
)
2237 gint start
, end
, first_pos
, max_pos
;
2241 first_pos
= end
= sci_get_position_from_line(sci
, tag_line
);
2242 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
2244 /* goto the begin of function body */
2245 while (end
< max_pos
&&
2246 (tmp
= sci_get_char_at(sci
, end
)) != '{' &&
2248 if (tmp
== 0) end
--;
2250 /* go back to the end of function identifier */
2251 while (end
> 0 && end
> first_pos
- 500 &&
2252 (tmp
= sci_get_char_at(sci
, end
)) != '(' &&
2255 if (end
< 0) end
= 0;
2257 /* skip whitespaces between identifier and ( */
2258 while (end
> 0 && isspace(sci_get_char_at(sci
, end
))) end
--;
2261 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2262 while (start
>= 0 && ((tmp
= sci_get_style_at(sci
, start
)) == SCE_C_IDENTIFIER
2263 || tmp
== SCE_C_GLOBALCLASS
2264 || (c
= sci_get_char_at(sci
, start
)) == '~'
2267 if (start
!= 0 && start
< end
) start
++; /* correct for last non-matching char */
2269 if (start
== end
) return NULL
;
2270 return sci_get_contents_range(sci
, start
, end
+ 1);
2274 static gint
get_fold_header_after(ScintillaObject
*sci
, gint line
)
2276 gint line_count
= sci_get_line_count(sci
);
2278 for (; line
< line_count
; line
++)
2280 if (sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
)
2288 static gint
get_current_tag_name(GeanyDocument
*doc
, gchar
**tagname
, TMTagType tag_types
)
2293 line
= sci_get_current_line(doc
->editor
->sci
);
2294 parent
= sci_get_fold_parent(doc
->editor
->sci
, line
);
2295 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
2296 if (parent
>= 0 && doc
->tm_file
!= NULL
&& doc
->tm_file
->tags_array
!= NULL
&&
2297 (! doc
->changed
|| editor_prefs
.autocompletion_update_freq
> 0))
2299 const TMTag
*tag
= tm_get_current_tag(doc
->tm_file
->tags_array
, parent
+ 1, tag_types
);
2303 gint tag_line
= tag
->line
- 1;
2304 gint last_child
= line
+ 1;
2306 /* if it may be a false positive because we're inside a fold level not inside anything
2307 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
2308 * right after the tag we got from TM */
2309 if (abs(tag_line
- parent
) > 1)
2311 gint tag_fold
= get_fold_header_after(doc
->editor
->sci
, tag_line
);
2313 last_child
= scintilla_send_message(doc
->editor
->sci
, SCI_GETLASTCHILD
, tag_fold
, -1);
2316 if (line
<= last_child
)
2319 *tagname
= g_strconcat(tag
->scope
,
2320 symbols_get_context_separator(doc
->file_type
->id
), tag
->name
, NULL
);
2322 *tagname
= g_strdup(tag
->name
);
2328 /* for the poor guy with a modified document and without real time tag parsing, we fallback
2329 * to dirty and inaccurate hand-parsing */
2330 else if (parent
>= 0 && doc
->file_type
!= NULL
&& doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
2332 const gint fn_fold
= get_function_fold_number(doc
);
2333 gint tag_line
= parent
;
2334 gint fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
2336 /* find the top level fold point */
2337 while (tag_line
>= 0 && (fold_level
& SC_FOLDLEVELNUMBERMASK
) != fn_fold
)
2339 tag_line
= sci_get_fold_parent(doc
->editor
->sci
, tag_line
);
2340 fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
2347 if (sci_get_lexer(doc
->editor
->sci
) == SCLEX_CPP
)
2348 cur_tag
= parse_cpp_function_at_line(doc
->editor
->sci
, tag_line
);
2350 cur_tag
= parse_function_at_line(doc
->editor
->sci
, tag_line
);
2352 if (cur_tag
!= NULL
)
2360 *tagname
= g_strdup(_("unknown"));
2365 static gint
get_current_tag_name_cached(GeanyDocument
*doc
, const gchar
**tagname
, TMTagType tag_types
)
2367 static gint tag_line
= -1;
2368 static gchar
*cur_tag
= NULL
;
2370 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, -1);
2372 if (doc
== NULL
) /* reset current function */
2374 current_tag_changed(NULL
, -1, -1, 0);
2376 cur_tag
= g_strdup(_("unknown"));
2377 if (tagname
!= NULL
)
2383 gint line
= sci_get_current_line(doc
->editor
->sci
);
2384 gint fold_level
= sci_get_fold_level(doc
->editor
->sci
, line
);
2386 if (current_tag_changed(doc
, line
, fold_level
, tag_types
))
2389 tag_line
= get_current_tag_name(doc
, &cur_tag
, tag_types
);
2398 /* Sets *tagname to point at the current function or tag name.
2399 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2400 * call to this function.
2401 * Returns: line number of the current tag, or -1 if unknown. */
2402 gint
symbols_get_current_function(GeanyDocument
*doc
, const gchar
**tagname
)
2404 return get_current_tag_name_cached(doc
, tagname
, tm_tag_function_t
| tm_tag_method_t
);
2408 /* same as symbols_get_current_function() but finds class, namespaces and more */
2409 gint
symbols_get_current_scope(GeanyDocument
*doc
, const gchar
**tagname
)
2411 TMTagType tag_types
= (tm_tag_function_t
| tm_tag_method_t
| tm_tag_class_t
|
2412 tm_tag_struct_t
| tm_tag_enum_t
| tm_tag_union_t
);
2414 /* Python parser reports imports as namespaces which confuses the scope detection */
2415 if (doc
&& doc
->file_type
->lang
!= filetypes
[GEANY_FILETYPES_PYTHON
]->lang
)
2416 tag_types
|= tm_tag_namespace_t
;
2418 return get_current_tag_name_cached(doc
, tagname
, tag_types
);
2422 static void on_symbol_tree_sort_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
2424 gint sort_mode
= GPOINTER_TO_INT(user_data
);
2425 GeanyDocument
*doc
= document_get_current();
2427 if (ignore_callback
)
2431 doc
->has_tags
= symbols_recreate_tag_list(doc
, sort_mode
);
2435 static void on_symbol_tree_menu_show(GtkWidget
*widget
,
2438 GeanyDocument
*doc
= document_get_current();
2441 enable
= doc
&& doc
->has_tags
;
2442 gtk_widget_set_sensitive(symbol_menu
.sort_by_name
, enable
);
2443 gtk_widget_set_sensitive(symbol_menu
.sort_by_appearance
, enable
);
2444 gtk_widget_set_sensitive(symbol_menu
.expand_all
, enable
);
2445 gtk_widget_set_sensitive(symbol_menu
.collapse_all
, enable
);
2446 gtk_widget_set_sensitive(symbol_menu
.find_usage
, enable
);
2447 gtk_widget_set_sensitive(symbol_menu
.find_doc_usage
, enable
);
2452 ignore_callback
= TRUE
;
2454 if (doc
->priv
->symbol_list_sort_mode
== SYMBOLS_SORT_BY_NAME
)
2455 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_name
), TRUE
);
2457 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_appearance
), TRUE
);
2459 ignore_callback
= FALSE
;
2463 static void on_expand_collapse(GtkWidget
*widget
, gpointer user_data
)
2465 gboolean expand
= GPOINTER_TO_INT(user_data
);
2466 GeanyDocument
*doc
= document_get_current();
2471 g_return_if_fail(doc
->priv
->tag_tree
);
2474 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2476 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2480 static void on_find_usage(GtkWidget
*widget
, G_GNUC_UNUSED gpointer unused
)
2483 GtkTreeSelection
*selection
;
2484 GtkTreeModel
*model
;
2488 doc
= document_get_current();
2492 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
2493 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
2494 gtk_tree_model_get(model
, &iter
, SYMBOLS_COLUMN_TAG
, &tag
, -1);
2497 if (widget
== symbol_menu
.find_in_files
)
2498 search_show_find_in_files_dialog_full(tag
->name
, NULL
);
2500 search_find_usage(tag
->name
, tag
->name
, GEANY_FIND_WHOLEWORD
| GEANY_FIND_MATCHCASE
,
2501 widget
== symbol_menu
.find_usage
);
2508 static void create_taglist_popup_menu(void)
2510 GtkWidget
*item
, *menu
;
2512 tv
.popup_taglist
= menu
= gtk_menu_new();
2514 symbol_menu
.expand_all
= item
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
2515 gtk_widget_show(item
);
2516 gtk_container_add(GTK_CONTAINER(menu
), item
);
2517 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GINT_TO_POINTER(TRUE
));
2519 symbol_menu
.collapse_all
= item
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
2520 gtk_widget_show(item
);
2521 gtk_container_add(GTK_CONTAINER(menu
), item
);
2522 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GINT_TO_POINTER(FALSE
));
2524 item
= gtk_separator_menu_item_new();
2525 gtk_widget_show(item
);
2526 gtk_container_add(GTK_CONTAINER(menu
), item
);
2528 symbol_menu
.sort_by_name
= item
= gtk_radio_menu_item_new_with_mnemonic(NULL
,
2529 _("Sort by _Name"));
2530 gtk_widget_show(item
);
2531 gtk_container_add(GTK_CONTAINER(menu
), item
);
2532 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
2533 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME
));
2535 symbol_menu
.sort_by_appearance
= item
= gtk_radio_menu_item_new_with_mnemonic_from_widget(
2536 GTK_RADIO_MENU_ITEM(item
), _("Sort by _Appearance"));
2537 gtk_widget_show(item
);
2538 gtk_container_add(GTK_CONTAINER(menu
), item
);
2539 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
2540 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE
));
2542 item
= gtk_separator_menu_item_new();
2543 gtk_widget_show(item
);
2544 gtk_container_add(GTK_CONTAINER(menu
), item
);
2546 symbol_menu
.find_usage
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find _Usage"));
2547 gtk_widget_show(item
);
2548 gtk_container_add(GTK_CONTAINER(menu
), item
);
2549 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), symbol_menu
.find_usage
);
2551 symbol_menu
.find_doc_usage
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find _Document Usage"));
2552 gtk_widget_show(item
);
2553 gtk_container_add(GTK_CONTAINER(menu
), item
);
2554 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), symbol_menu
.find_doc_usage
);
2556 symbol_menu
.find_in_files
= item
= ui_image_menu_item_new(GTK_STOCK_FIND
, _("Find in F_iles..."));
2557 gtk_widget_show(item
);
2558 gtk_container_add(GTK_CONTAINER(menu
), item
);
2559 g_signal_connect(item
, "activate", G_CALLBACK(on_find_usage
), NULL
);
2561 g_signal_connect(menu
, "show", G_CALLBACK(on_symbol_tree_menu_show
), NULL
);
2563 sidebar_add_common_menu_items(GTK_MENU(menu
));
2567 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
2571 g_return_if_fail(!EMPTY(doc
->real_path
));
2573 f
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
2574 if (utils_str_equal(doc
->real_path
, f
))
2575 load_c_ignore_tags();
2581 void symbols_init(void)
2586 create_taglist_popup_menu();
2588 f
= g_build_filename(app
->configdir
, "ignore.tags", NULL
);
2589 ui_add_config_file_menu_item(f
, NULL
, NULL
);
2592 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
2594 for (i
= 0; i
< G_N_ELEMENTS(symbols_icons
); i
++)
2595 symbols_icons
[i
].pixbuf
= get_tag_icon(symbols_icons
[i
].icon_name
);
2599 void symbols_finalize(void)
2603 g_strfreev(c_tags_ignore
);
2605 for (i
= 0; i
< G_N_ELEMENTS(symbols_icons
); i
++)
2607 if (symbols_icons
[i
].pixbuf
)
2608 g_object_unref(symbols_icons
[i
].pixbuf
);