Skip to content

Commit 834dfcb

Browse files
Fix completion popup placement and adjust lines to available space
1 parent b79fe2e commit 834dfcb

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

scene/gui/code_edit.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ void CodeEdit::_notification(int p_what) {
138138
/* Code completion */
139139
if (draw_code_completion) {
140140
const int code_completion_options_count = code_completion_options.size();
141-
const int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
141+
int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
142142
const Size2 icon_area_size(row_height, row_height);
143143

144144
code_completion_rect.size.width = code_completion_longest_line + theme_cache.code_completion_icon_separation + icon_area_size.width + 2;
145145
code_completion_rect.size.height = lines * row_height;
146146

147147
const Point2 caret_pos = get_caret_draw_pos();
148-
const int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
148+
int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
149149
int min_y = caret_pos.y - row_height;
150150
int max_y = caret_pos.y + row_height + total_height;
151151
if (draw_code_hint) {
@@ -156,9 +156,31 @@ void CodeEdit::_notification(int p_what) {
156156
}
157157
}
158158

159-
const bool can_fit_completion_above = (min_y > total_height);
160-
const bool can_fit_completion_below = (max_y <= get_size().height);
161-
if (!can_fit_completion_below && can_fit_completion_above) {
159+
const bool can_fit_completion_above = min_y > total_height;
160+
const bool can_fit_completion_below = max_y <= get_size().height;
161+
162+
bool should_place_above = !can_fit_completion_below && can_fit_completion_above;
163+
164+
if (!can_fit_completion_below && !can_fit_completion_above) {
165+
const int space_above = caret_pos.y - row_height;
166+
const int space_below = get_size().height - caret_pos.y;
167+
should_place_above = space_above > space_below;
168+
169+
// Reduce the line count and recalculate heights to better fit the completion popup.
170+
int space_avail;
171+
if (should_place_above) {
172+
space_avail = space_above - theme_cache.code_completion_style->get_minimum_size().y;
173+
} else {
174+
space_avail = space_below - theme_cache.code_completion_style->get_minimum_size().y;
175+
}
176+
177+
int max_lines_fit = MAX(1, space_avail / row_height);
178+
lines = MIN(lines, max_lines_fit);
179+
code_completion_rect.size.height = lines * row_height;
180+
total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
181+
}
182+
183+
if (should_place_above) {
162184
code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + theme_cache.line_spacing;
163185
if (draw_code_hint && !code_hint_draw_below) {
164186
code_completion_rect.position.y -= code_hint_minsize.y;

tests/scene/test_code_edit.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,15 +4158,15 @@ TEST_CASE("[SceneTree][CodeEdit] completion") {
41584158
CHECK(code_edit->get_code_completion_selected_index() == 0);
41594159

41604160
/* Single click selects. */
4161-
caret_pos.y += code_edit->get_line_height() * 2;
4161+
caret_pos.y += code_edit->get_line_height();
41624162
SEND_GUI_MOUSE_BUTTON_EVENT(caret_pos, MouseButton::LEFT, MouseButtonMask::LEFT, Key::NONE);
41634163
SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(caret_pos, MouseButton::LEFT, MouseButtonMask::NONE, Key::NONE);
4164-
CHECK(code_edit->get_code_completion_selected_index() == 2);
4164+
CHECK(code_edit->get_code_completion_selected_index() == 1);
41654165

41664166
/* Double click inserts. */
41674167
SEND_GUI_DOUBLE_CLICK(caret_pos, Key::NONE);
41684168
CHECK(code_edit->get_code_completion_selected_index() == -1);
4169-
CHECK(code_edit->get_line(0) == "item_2");
4169+
CHECK(code_edit->get_line(0) == "item_1");
41704170

41714171
code_edit->set_auto_brace_completion_enabled(false);
41724172

0 commit comments

Comments
 (0)