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

Fix find result current match count issues #93674

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,18 @@ void FindReplaceBar::_replace_all() {
needs_to_count_results = true;
}

void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searching_next) {
void FindReplaceBar::_get_search_from(int &r_line, int &r_col, SearchMode p_search_mode) {
if (!text_editor->has_selection(0) || is_selection_only()) {
r_line = text_editor->get_caret_line(0);
r_col = text_editor->get_caret_column(0);

if (!p_is_searching_next && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
if (p_search_mode == SEARCH_PREV && r_line == result_line && r_col >= result_col && r_col <= result_col + get_search_text().length()) {
r_col = result_col;
}
return;
}

if (p_is_searching_next) {
if (p_search_mode == SEARCH_NEXT) {
r_line = text_editor->get_selection_to_line();
r_col = text_editor->get_selection_to_column();
} else {
Expand All @@ -368,7 +368,11 @@ void FindReplaceBar::_get_search_from(int &r_line, int &r_col, bool p_is_searchi
}

void FindReplaceBar::_update_results_count() {
if (!needs_to_count_results && (result_line != -1) && results_count_to_current > 0) {
int caret_line, caret_column;
_get_search_from(caret_line, caret_column, SEARCH_CURRENT);
bool match_selected = caret_line == result_line && caret_column == result_col && !is_selection_only() && text_editor->has_selection(0);

if (match_selected && !needs_to_count_results && result_line != -1 && results_count_to_current > 0) {
results_count_to_current += (flags & TextEdit::SEARCH_BACKWARDS) ? -1 : 1;

if (results_count_to_current > results_count) {
Expand All @@ -385,9 +389,10 @@ void FindReplaceBar::_update_results_count() {
return;
}

needs_to_count_results = false;
needs_to_count_results = !match_selected;

results_count = 0;
results_count_to_current = 0;

for (int i = 0; i < text_editor->get_line_count(); i++) {
String line_text = text_editor->get_line(i);
Expand Down Expand Up @@ -417,18 +422,26 @@ void FindReplaceBar::_update_results_count() {

results_count++;

if (i == result_line) {
if (col_pos == result_col) {
results_count_to_current = results_count;
} else if (col_pos < result_col && col_pos + searched.length() > result_col) {
col_pos = result_col;
results_count_to_current = results_count;
}
if (i <= result_line && col_pos <= result_col) {
results_count_to_current = results_count;
}
if (i == result_line && col_pos < result_col && col_pos + searched.length() > result_col) {
// Searching forwards and backwards with repeating text can lead to different matches.
col_pos = result_col;
}

col_pos += searched.length();
}
}
if (!match_selected) {
// Current result should refer to the match before the caret, if the caret is not on a match.
if (caret_line != result_line || caret_column != result_col) {
results_count_to_current -= 1;
}
if (results_count_to_current == 0 && (caret_line > result_line || (caret_line == result_line && caret_column > result_col))) {
// Caret is after all matches.
results_count_to_current = results_count;
}
}
}

void FindReplaceBar::_update_matches_display() {
Expand Down Expand Up @@ -457,7 +470,7 @@ bool FindReplaceBar::search_current() {
_update_flags(false);

int line, col;
_get_search_from(line, col);
_get_search_from(line, col, SEARCH_CURRENT);

return _search(flags, line, col);
}
Expand All @@ -473,10 +486,14 @@ bool FindReplaceBar::search_prev() {

String text = get_search_text();

if ((flags & TextEdit::SEARCH_BACKWARDS) == 0) {
needs_to_count_results = true;
}

_update_flags(true);

int line, col;
_get_search_from(line, col);
_get_search_from(line, col, SEARCH_PREV);

col -= text.length();
if (col < 0) {
Expand All @@ -499,10 +516,14 @@ bool FindReplaceBar::search_next() {
popup_search(true);
}

if (flags & TextEdit::SEARCH_BACKWARDS) {
needs_to_count_results = true;
}

_update_flags(false);

int line, col;
_get_search_from(line, col, true);
_get_search_from(line, col, SEARCH_NEXT);

return _search(flags, line, col);
}
Expand Down
8 changes: 7 additions & 1 deletion editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class CodeTextEditor;
class FindReplaceBar : public HBoxContainer {
GDCLASS(FindReplaceBar, HBoxContainer);

enum SearchMode {
SEARCH_CURRENT,
SEARCH_NEXT,
SEARCH_PREV,
};

LineEdit *search_text = nullptr;
Label *matches_label = nullptr;
Button *find_prev = nullptr;
Expand Down Expand Up @@ -94,7 +100,7 @@ class FindReplaceBar : public HBoxContainer {
bool replace_all_mode = false;
bool preserve_cursor = false;

void _get_search_from(int &r_line, int &r_col, bool p_is_searching_next = false);
void _get_search_from(int &r_line, int &r_col, SearchMode p_search_mode);
void _update_results_count();
void _update_matches_display();

Expand Down
Loading