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

Sort autocomplete/code completion options in a better way #58931

Merged

Conversation

EricEzaM
Copy link
Contributor

@EricEzaM EricEzaM commented Mar 9, 2022

Closes godotengine/godot-proposals#4189
Closes godotengine/godot-proposals#99

Please read above proposal for more info and some example of what code completion currently looks like.
After changes in this PR:
image

Code Overview:

  • Add location property to ScriptCodeCompletionOption. It is an int, but has a corresponding enum, and more importantly one of the enum values is a mask, LOCATION_PARENT_MASK = (1 << 8),. This allows you, for example, to sort closer ancestors differently from more distant ancestors. The class itself will be value 256, while the class it inherits will have its location at 257, and then its parent at 258, etc.
  • When creating options, pass a location. This can be easily calculated if the options are created in a loop, where the loop is looping over parent classes and getting the class info for them. const int location = classes_processed | ScriptCodeCompletionOption::LOCATION_PARENT_MASK;
  • When you have all the options, simply sort on location. The lowest values will be highest priority. If two locations are the same, sort alphanumerically. The order in which different ScriptCodeCompletionOption::Kind's are sorted has been added too.

@EricEzaM EricEzaM requested review from a team as code owners March 9, 2022 14:22
@EricEzaM EricEzaM force-pushed the proposals/4189-better-code-completion branch from 18ecfc9 to 82d4930 Compare March 9, 2022 14:23
@EricEzaM EricEzaM requested review from a team and removed request for a team March 9, 2022 14:23
@YeldhamDev YeldhamDev added this to the 4.0 milestone Mar 9, 2022
@EricEzaM EricEzaM force-pushed the proposals/4189-better-code-completion branch from 82d4930 to 0d21ce9 Compare March 9, 2022 14:28
@EricEzaM EricEzaM requested a review from Paulb23 March 22, 2022 12:47
@EricEzaM EricEzaM requested a review from vnen March 22, 2022 12:47
@EricEzaM
Copy link
Contributor Author

@Paulb23 @vnen Let me know what you think about this implementation. Without changing too much code, it is the best solution I could come up with, although as a result it is quite rigid. However if it is good for 99% of cases its lack of flexibility isnt so much of a big deal.

@vnen
Copy link
Member

vnen commented Mar 23, 2022

The implementation looks good to me. Just need to fix the issues pointed by Akien.

Done by ordering options by their location in the code - e.g. local, parent class, global, etc.
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

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

LGTM

@akien-mga akien-mga merged commit 7fe5bec into godotengine:master Mar 28, 2022
@akien-mga
Copy link
Member

Thanks!

@reduz
Copy link
Member

reduz commented Mar 28, 2022

I did many changes to the script API in #59553 and this PR has broken them. I suggest reverting this PR first and make it redone on top of my changes. It also adds things to script_language.h that should not be there, like the sorting order for completion. This should go in editor, not in script_language.h

}
};

const int KIND_COUNT = 10;
Copy link
Member

Choose a reason for hiding this comment

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

If this is only used by the editor, it should not be here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, can be easily moved.


ScriptCodeCompletionOption() {}

ScriptCodeCompletionOption(const String &p_text, Kind p_kind) {
ScriptCodeCompletionOption(const String &p_text, Kind p_kind, int p_location = LOCATION_OTHER) {
Copy link
Member

Choose a reason for hiding this comment

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

Make sure to check that script_language_extension bindings to this are correct.

Kind kind = KIND_PLAIN_TEXT;
String display;
String insert_text;
Color font_color;
RES icon;
Variant default_value;
Vector<Pair<int, int>> matches;
int location = LOCATION_OTHER;
Copy link
Member

Choose a reason for hiding this comment

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

Should be an enum.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use the PARENT_MASK enum value as a flag to indicate it comes from a parent - but then the other bits in the integer are used to store the depth of the parent, so it is not strictly an enum value.

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