GNOME Bugzilla – Bug 643139
Symbol-db does not know when an scope ends.
Last modified: 2020-11-06 20:22:46 UTC
I was trying to implement Johannes Schmid asked me in https://bugzilla.gnome.org/show_bug.cgi?id=642991#c10 , but I got stuck in something I think is a bug in symbol-db. While implementing it, I added the following code to on_glade_drop_possible: gint line; printf ("line number: %d\n", (line = ianjuta_editor_get_line_from_position (editor, iterator, NULL))); GFile* file = ianjuta_file_get_file (IANJUTA_FILE (editor), NULL); char *path; printf ("file path: %s\n", (path = g_file_get_path (file))); IAnjutaSymbolManager *isymbol_manager = anjuta_shell_get_interface ( ANJUTA_PLUGIN (lang_plugin)->shell, IAnjutaSymbolManager, NULL); IAnjutaSymbolQuery *symbol_query = ianjuta_symbol_manager_create_query ( isymbol_manager, IANJUTA_SYMBOL_QUERY_SEARCH_SCOPE, IANJUTA_SYMBOL_QUERY_DB_PROJECT, NULL); IAnjutaSymbolField field[3]; field[0] = IANJUTA_SYMBOL_FIELD_NAME; field[1] = IANJUTA_SYMBOL_FIELD_KIND; field[2] = IANJUTA_SYMBOL_FIELD_TYPE; ianjuta_symbol_query_set_fields (symbol_query, 3, field, NULL); IAnjutaIterable *iter =ianjuta_symbol_query_search_scope (symbol_query, path, line, NULL); g_free(path); if (!iter) { printf ("NULL scope. Is it global scope?\n"); } else { printf ("non-NULL scope. Is it global scope?\n"); printf ("Name field: %s\n", ianjuta_symbol_get_string (IANJUTA_SYMBOL (iter), IANJUTA_SYMBOL_FIELD_NAME, NULL)); printf ("Kind field: %s\n", ianjuta_symbol_get_string (IANJUTA_SYMBOL (iter), IANJUTA_SYMBOL_FIELD_KIND, NULL)); printf ("Type field: %s\n", ianjuta_symbol_get_string (IANJUTA_SYMBOL (iter), IANJUTA_SYMBOL_FIELD_TYPE, NULL)); g_object_unref (iter); } It turns out that ianjuta_symbol_query_search_scope only returns NULL if it is check on the beginning of a file, just before any macro, function or any other thing has been declared. After a declaration, it always returns the scope of the definition immediately before the position that is given; even when we are in a "global" scope. Is that the expected behavior?
yes, that's the expected behaviour, not a bug. It's the behaviour that ctags http://ctags.sourceforge.net/ has.
Is there a reliable way to know if a position in a source file is in a "global" scope?
Well, in C you're always in global scope, except by when you're inside structs, functions etc. A different thing is C++ where you have classes, namespaces etc. Unfortunately ctags parser gives only the initial position of the scope: it'll last until a new scope is encountered, e.g. there's no "line 123 is the end of function scope FooFunc". You may try to detect the "global" scope (maybe better calling it the NULL scope) using brackets matching and the informations retrieved by symbol-db about the scopes in a file.
I've been looking at anjuta-tags source code. It looks like it is not that hard to implement a way in anjuta-tags to detect where a scope ends. Just changing the case case ';': setToken (st, TOKEN_SEMICOLON); if (st->parent == NULL) printf ("End of scope in line %lu\n", getSourceLineNumber ()); break; in nextToken in plugins/symbol-db/anjuta-tags/c.c and adding if (braceMatching) { printf ("End of scope at line: %ld\n", getSourceLineNumber()); } before the last "if" in skipToMatch (same file) allowed to detect where scopes ends. The method has some problems. Sometimes it detects the end of a scope multiple times, which is an easy to fix problem if we limit it to only mark the end of each scope once. I tested it with various files and it worked pretty well. I could not find a case where it found the end of a scope before the real end of it. Not that this is an important addition and will allow many improvements in anjuta/glade integration. I think we should come up with an implementation plan for this. I'm very interested in doing it, but, since I'm not used to the anjuta-tags source code, I would need guidance. What do you think about that?
I think that would be a very nice addition but I might need some changes in the symbol-database to make use of that information and it should be done for any C-style language (C, C++, C#, Java, Vala) where it is easy to detect the bounds of a block. That would help in quite a lot of part of anjuta including code completion.
While this would be great, I'm pretty dubious it could be done so easily. 1. Why ctags devs have never thought about this before? 2. Please try your patch against some complex cases in C++, and see if it still works good. 3. Codelite, despite many patches against ctags, uses an extra parser to detect (at completion time) the scope of the current line. 4. Have you tested yout patch against macros, structs, function declarations etc.? As Johannes correctly said, the patch should be implemented to work with C++, C#, Java etc. The changes at 'tags' output should include the end-of-scope field. This could mean to modify a lot of code in symbol-db core, and query system (new fields mapping, new APIs etc.). Definitely not an easy task. As I'm not a ctags expert I would suggest you to ask on ctags-dev mailing list for this end-of-scope thing. Maybe they could come up with some 'evident' problems that we cannot see right now: this could save us a lot of time in case this is a really difficult task. Ctags is a near-dead project, IMHO. It's missing a lot of interesting features even if it provides many, but unfortunately it seems to be the only reliable choice in OSS world.
Created attachment 182220 [details] [review] Patch to detect end of scopes If anyone is interested in testing it, the attached patch reproduces my experiments. Please test it. I really would like to have this feature.
bugzilla.gnome.org is being replaced by gitlab.gnome.org. We are closing all old bug reports in Bugzilla which have not seen updates for many years. If you can still reproduce this issue in a currently supported version of GNOME (currently that would be 3.38), then please feel free to report it at https://gitlab.gnome.org/GNOME/anjuta/-/issues/ Thank you for reporting this issue and we are sorry it could not be fixed.