In previous post, Code Search for GNOME Builder: Indexing, I wrote about how indexing of source code is implemented to support searching symbols in GNOME Builder. After discussing with Christian Hergert, we changed the design of indexing to make it easier to support indexing of source code in languages other than C/C++.
This is the new design,
An interface IdeCodeIndexer is created. This interface will take a source file of a particular language and returns a list of info of symbols present in that file. Returned list of symbols are in form of GListModel. Any class that implements this interface can provide indexing support for a language.
This is how indexing is done now. CodeIndex plugin will browse working directory recursively and creates index for every directory for which index is not there or index is older than files in that directory. For indexing each directory, each file in that directory will be taken and given to IdeCodeIndexer corresponding to language of that file. CodeIndex plugin will then use list of info of symbols returned by IdeCodeIndexer to create index for that directory. For finding IdeCodeIndexer corresponding to a language CodeIndex plugin will browse through all plugins and check if that plugin implements IdeCodeIndexer interface and supports that language. For checking whether a plugin supports a language or not it will check for value of “X-Ide-Code-Indexer-Languages” key in plugin info.
Adding support for indexing a new language:
If indexing a new language has to be supported then a plugin has to implement IdeCodeIndexer for that corresponding language and add a key-value pair in .plugin file like X-Ide-Code-Indexer-Languages=lang.
Support for indexing C, C++ files:
I implemented IdeCodeIndexer for C, C++ languages in clang plugin. For indexing, first this will create AST of that source file and returns a GListModel backed by this AST. On each g_list_model_get_item request this GListModel will do breadth first search till it finds declaration of a symbol and returns that symbol info. On subsequent g_list_model_get_item requests it will continue breadth first search from where it has stopped previously till it finds declaration of a symbol returns that symbol info. When all symbols are returned g_list_model_get_item will return NULL.
This repo contains code which implements CodeIndex plugin and IdeCodeIndexer in clang plugin.