[cfe-dev] Autocompleting functions based on first parameter
Michael Kruse via cfe-dev
cfe-dev at lists.llvm.org
Tue Jan 10 08:00:21 PST 2017
Hi everyone,
I am interested in improving clang's autocompletion to also suggest
free functions that can take the expression at the cursor as first
parameter, in addition to the methods its class has. I am interested
in opinions and suggestions on how to implement it.
Currently, YouCompleteMe and clang-complete, after typing (| = cursor position)
std::string str;
str.|
will suggest std::string's methods (std::string::size(),
std::string::push_back(), etc.), but not free functions such as
std::stoi, std::stod, std::hash, etc. These are operations on the
string as well, but for various reason are not members of std::string.
Adding them to the autocompletion would make them more discoverable.
When selecting a free function, eg. stoi, completion would change the
"str." line to
std::stoi(str, |
Languages such as C# have extension methods that allow keeping the
class definition smaller and have additional functionalities in
separate compilation units while still being discoverable using
autocompletion. Unfortunately there is nothing equivalent in C or C++.
This could be even more useful for C-style char*. Typing "str" and
then triggering autocompletion allows to browse a list of
string-related functions, but neither it is complete (eg. atoi won't
appear) nor contains only string-related functions (eg.
std::streambuf). Typing the argument first and then get a list of
operations might be more useful than having to know the operation
first.
Implementation
I think it is not possible to implement this in YouCompleteMe or
clang-complete only, support by clang is needed as it needs to know
the expression's and parameter's types and their conversions. After
looking at clang's C API for autocompletion that is used by
YouCompleteMe and clang-complete (clang_codeCompleteAt), I think
placing something at locations other than the cursor position or
replacing previous characters doesn't seem to be possible. That is,
there are only values in CodeCompletionString::ChunkKind that add
text, but nothing to remove, overwrite or change the cursor position.
Eg. Visual Studio can automatically replace "." and "->", depending on
whether the expression under the cursor is a class or
pointer-to-class. This seems to be impossible to implement with the
current interface.
There is also a concern that enumerating a lot of free functions could
lead to a significant delay.
Background
We are discussing a C++ interface for the Integer Set Library [1]
which would make its reference counting automatically handled by RAII
in Polly [2]. One point of discussion was whether to make C functions
members of its classes or free functions in the isl:: namespace.
Essentially, the choice between
intersect(set1, set2);
and
set1.intersect(set2);
The latter introduces an asymmetry between the arguments and raises
the question whether the method modifies the object and/or returns a
new one. It also does not do implicit conversions of the "this"
argument. On the plus side, it does not pollute the isl:: namespace,
is more object-oriented and allows browsing the interface using
code-completion. The last point would not be an issue anymore if code
completion also suggested functions.
Regards,
Michael
[1] http://isl.gforge.inria.fr/
[2] https://docs.google.com/document/d/1h89T_wKRFSimY8bn9estBI4p0pJZkj7Zu5ptSE7nybI/edit#
More information about the cfe-dev
mailing list