godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Match words if selection was empty and Ctrl+D initially selected a word #10576

Open janosharsanyi opened 2 months ago

janosharsanyi commented 2 months ago

Describe the project you are working on

Any project

Describe the problem or limitation you are having in your project

When variables/functions in the code start with the same prefix, it can be cumbersome to select the next whole word matches.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

If the selection was empty and the cursor is near a word, it is selected automatically. However currently when Ctrl+D is pressed again, it can also select parts of words. With this enhancement, it would only select matched words in this case.

If parts of the code was selected when Ctrl+D was first pressed, next presses of Ctrl+D would select substrings (as the current behavior, this part would be unchanged).

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Ctrl+D pressed without initial selection:

  1. First, it selects the word under the cursor.
  2. Second, the next matching word is selected.

mspaint_JCTZ0n9pCH

Ctrl+D pressed when a part of the text was already selected (keep current behavior):

  1. Always select the next matching substring.

image image


We would need to know when the user pressed Ctrl+D (in TextEdit::add_selection_for_next_occurrence / ui_text_add_selection_for_next_occurrence) if the selection was initially empty (and the first word selection was started by the function itself) or the user pressed Ctrl+D after selecting part of the code.

diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index 422783b01b..52696396b0 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -5221,6 +5221,7 @@ void TextEdit::add_selection_for_next_occurrence() {
        int caret = get_caret_count() - 1;

        if (!has_selection(caret)) {
+               // TODO: somehow add SEARCH_WHOLE_WORDS to flags for next call in search method for next_occurrence
                select_word_under_caret(caret);
                return;
        }

(This would reset on manual selection change.)

If this enhancement will not be used often, can it be worked around with a few lines of script?

False-positive matches can be skipped with Ctrl+Alt+D, but takes longer and error-prone.

Is there a reason why this should be core and not an add-on in the asset library?

Other editors work like this, it's inconvenient to re-adjust using this text editor

peterhoglund commented 2 months ago

I like this idea. I use Ctrl+D a lot to change variable names in bulk and it is really annoying that the "position" in global_position is selected when you have selected the position variable name, as in your example. It is especially annoying since if you select "position" (the variable) all other position variables will be highlighted, but the Ctrl+D action will deviate from that highlighting and select global_position as well.