Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting completion candidates based on clang priority #519

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

434189275
Copy link

Sorting completion candidates based on clang priority

Sorting completion candidates based on clang priority
@Sarcasm
Copy link
Owner

Sarcasm commented Jan 23, 2019

Hello and thank you for proposing this change!

Candidates are currently sorted according to clang_sortCodeCompletionResults(), to benefit from the performances of letting libclang doing it.
And personally, I prefer the predictability of alphabetical ordering.

Some frontends may like to sort them that way, e.g. when merging candidates from multiple backends.

I do expose the priority to elisp, so that the frontend (e.g. company-irony) can sort accordingly.
So, if you use the company frontend, I would suggest you to make a patch there, and enable it only under a defcustom.

@Sarcasm Sarcasm closed this Jan 23, 2019
@434189275
Copy link
Author

Hello and very nice mode by the way! Keep up the good work!

I personally use it mainly for C++ with company-irony, and the reason why I submitted this pull request is that sorting according to libclang priority (clang_getCompletionPriority()) allows completions to be context-aware, and thus, more relevant. Take for example this situation:

capture_1

In that case, I think most users would expect to have the sprite variable in first position, sprintf being less relevant here.

Now, sorting according to libclang priority:

capture_2

sprite variable now in first position, results still being sorted alphabetically.

And personally, I prefer the predictability of alphabetical ordering.

I totally agree with you on that point, but I think you'll agree that alphabetical sorting should not prevail on the relevance of the completion.

As you see, I could have just removed the call to clang_sortCodeCompletionResults() to have results sorted according to priority only, but as I, too, want the results to be sorted alphabetically, I manually sort them after calling clang_sortCodeCompletionResults() so we have both the benefits of the clang priority and of the alphabetical sorting.

What do you think ?

@Sarcasm
Copy link
Owner

Sarcasm commented Jan 24, 2019

I do think it's a legitimate use case, but I think it can be done in company-irony, in a defcustom variable, so that it's easier to enable/disable than something in the C++ part.

I believe you can wrap this around company-irony--candidates:

https://github.com/Sarcasm/company-irony/blob/52aca45bcd0f2cb0648fcafa2bbb4f8ad4b2fee7/company-irony.el#L78

Do you see what I mean?

@434189275
Copy link
Author

I get what you mean, but I definitely think that sorting should not be done in the front end for obvious performance reasons. More over, it can very easily be disabled by the mean of a defcustom variable, passed to the candidates command along with the prefix and style arguments.

Now, I do not see the need for disabling it, as totally ignoring clang priority actually leads up to incorrect completions in my opinion. And as I said above, alphabetical sorting is not put apart, it is just improved by taking into account the clang priority.

@Sarcasm
Copy link
Owner

Sarcasm commented Jan 25, 2019

Sorting in C++ could be faster, but sorting in company, assuming the default prefix length of size 3, does not sort all the candidates, but only a subset.
So in the general case, I'm not sure of how slower it would be, could really depend on the prefix.

Ok for prefix and style-like method, I would accept such PR.
I reopen if you prefer this solution.

@Sarcasm Sarcasm reopened this Jan 25, 2019
@@ -102,7 +102,7 @@ class Irony {
/// Get all the completion candidates.
///
/// \pre complete() was called.
void candidates(const std::string &prefix, PrefixMatchStyle style) const;
void candidates(const std::string &prefix, PrefixMatchStyle style, const std::string &sorting) const;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do like PrefixMatchStyle, and make a dedicated enum to represent the possibles values instead of storing it as a string.

@@ -181,7 +181,7 @@ int main(int ac, const char *av[]) {
break;

case Command::Candidates:
irony.candidates(c->prefix, c->style);
irony.candidates(c->prefix, c->style, c->sorting);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could make more sense to do this in the complete command, instead of candidates.
complete is called once but candidates many time.
Since the sorting does not depend on the candidates input, it may be better to do the sorting once, next to the sorting done by clang.

@@ -221,6 +221,7 @@ Command *CommandParser::parse(const std::vector<std::string> &argv) {
case Command::Candidates:
positionalArgs.push_back(StringConverter(&command_.prefix));
positionalArgs.push_back(PrefixMatchStyleConverter(&command_.style));
positionalArgs.push_back(StringConverter(&command_.sorting));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to modify Command.def as well, to change the description of the command (which I recommend below to apply to the complete command, instead of candidates).

@@ -180,6 +180,17 @@ Larger values can improve performances on large buffers.
If non-nil, `w32-pipe-buffer-size' will be let-bound to this value
during the creation of the irony-server process.")

(defcustom irony-candidates-clang-sorting nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of vocabulary, I think I would do something like:

sorting-method: priority, alphabetical.

I would not allow nil, but instead produce an error.
nil seems to imply this is the soundest default value, which I am not yet sure.
A more neutral approach is to have enumeration value, and if one make more sense than the other, the defcustom will be updated accordingly.
I'm mostly thinking about a future 3rd option coming up, that would be the best default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants