@@ -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 ;
0 commit comments