Skip to content

Commit ff39add

Browse files
committed
Prevent tooltip from showing when hovering past end of script line
1 parent 9630d4e commit ff39add

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

doc/classes/TextEdit.xml

+5-2
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@
349349
<method name="get_line_column_at_pos" qualifiers="const">
350350
<return type="Vector2i" />
351351
<param index="0" name="position" type="Vector2i" />
352-
<param index="1" name="allow_out_of_bounds" type="bool" default="true" />
352+
<param index="1" name="clamp_line" type="bool" default="true" />
353+
<param index="2" name="clamp_column" type="bool" default="true" />
353354
<description>
354-
Returns the line and column at the given position. In the returned vector, [code]x[/code] is the column, [code]y[/code] is the line. If [param allow_out_of_bounds] is [code]false[/code] and the position is not over the text, both vector values will be set to [code]-1[/code].
355+
Returns the line and column at the given position. In the returned vector, [code]x[/code] is the column and [code]y[/code] is the line.
356+
If [param clamp_line] is [code]false[/code] and [param position] is below the last line, [code]Vector2i(-1, -1)[/code] is returned.
357+
If [param clamp_column] is [code]false[/code] and [param position] is outside the column range of the line, [code]Vector2i(-1, -1)[/code] is returned.
355358
</description>
356359
</method>
357360
<method name="get_line_count" qualifiers="const">

misc/extension_api_validation/4.3-stable.expected

+7
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,10 @@ Validate extension JSON: API was removed: classes/EditorSceneFormatImporter/meth
309309

310310
This virtual method, and the internal public `get_import_flags`, were never used by the engine, since it was open sourced.
311311
So we're removing it despite the compat breakage as there's no way for users to rely on this affecting engine behavior.
312+
313+
314+
GH-100913
315+
---------
316+
Validate extension JSON: Error: Field 'classes/TextEdit/methods/get_line_column_at_pos/arguments': size changed value in new API, from 2 to 3.
317+
318+
Added optional argument to disallow positions that are outside the column range of the line. Compatibility method registered.

scene/gui/code_edit.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
421421
mpos.x = get_size().x - mpos.x;
422422
}
423423

424-
Point2i pos = get_line_column_at_pos(mpos, false);
424+
Point2i pos = get_line_column_at_pos(mpos, false, false);
425425
int line = pos.y;
426426
int col = pos.x;
427427

@@ -443,18 +443,18 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
443443

444444
if (symbol_lookup_on_click_enabled) {
445445
if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
446-
symbol_lookup_pos = get_line_column_at_pos(mpos);
446+
symbol_lookup_pos = get_line_column_at_pos(mpos, false, false);
447447
symbol_lookup_new_word = get_word_at_pos(mpos);
448448
if (symbol_lookup_new_word != symbol_lookup_word) {
449449
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
450450
}
451-
} else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos))) {
451+
} else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos, false, false))) {
452452
set_symbol_lookup_word_as_valid(false);
453453
}
454454
}
455455

456456
if (symbol_tooltip_on_hover_enabled) {
457-
symbol_tooltip_pos = get_line_column_at_pos(mpos, false);
457+
symbol_tooltip_pos = get_line_column_at_pos(mpos, false, false);
458458
symbol_tooltip_word = get_word_at_pos(mpos);
459459
symbol_tooltip_timer->start();
460460
}
@@ -2388,7 +2388,7 @@ bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
23882388

23892389
String CodeEdit::get_text_for_symbol_lookup() const {
23902390
Point2i mp = get_local_mouse_pos();
2391-
Point2i pos = get_line_column_at_pos(mp, false);
2391+
Point2i pos = get_line_column_at_pos(mp, false, false);
23922392
int line = pos.y;
23932393
int col = pos.x;
23942394

scene/gui/text_edit.compat.inc

+5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ void TextEdit::_set_selection_mode_compat_86978(SelectionMode p_mode, int p_line
3434
set_selection_mode(p_mode);
3535
}
3636

37+
Point2i TextEdit::_get_line_column_at_pos_bind_compat_100913(const Point2i &p_pos, bool p_allow_out_of_bounds) const {
38+
return get_line_column_at_pos(p_pos, p_allow_out_of_bounds, true);
39+
}
40+
3741
void TextEdit::_bind_compatibility_methods() {
3842
ClassDB::bind_compatibility_method(D_METHOD("set_selection_mode", "mode", "line", "column", "caret_index"), &TextEdit::_set_selection_mode_compat_86978, DEFVAL(-1), DEFVAL(-1), DEFVAL(0));
43+
ClassDB::bind_compatibility_method(D_METHOD("get_line_column_at_pos", "position", "allow_out_of_bounds"), &TextEdit::_get_line_column_at_pos_bind_compat_100913, DEFVAL(true));
3944
}
4045

4146
#endif

scene/gui/text_edit.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -4397,9 +4397,12 @@ Point2 TextEdit::get_local_mouse_pos() const {
43974397
}
43984398

43994399
String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
4400-
Point2i pos = get_line_column_at_pos(p_pos);
4400+
Point2i pos = get_line_column_at_pos(p_pos, false, false);
44014401
int row = pos.y;
44024402
int col = pos.x;
4403+
if (row < 0 || col < 0) {
4404+
return "";
4405+
}
44034406

44044407
String s = text[row];
44054408
if (s.length() == 0) {
@@ -4435,7 +4438,7 @@ String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
44354438
return String();
44364439
}
44374440

4438-
Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_of_bounds) const {
4441+
Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const {
44394442
float rows = p_pos.y - theme_cache.style_normal->get_margin(SIDE_TOP);
44404443
if (!editable) {
44414444
rows -= theme_cache.style_readonly->get_offset().y / 2;
@@ -4462,10 +4465,10 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_
44624465

44634466
int visible_lines = get_visible_line_count_in_range(first_vis_line, row);
44644467
if (rows > visible_lines) {
4465-
if (!p_allow_out_of_bounds) {
4466-
return Point2i(-1, -1);
4468+
if (p_clamp_line) {
4469+
return Point2i(text[row].length(), row);
44674470
}
4468-
return Point2i(text[row].length(), row);
4471+
return Point2i(-1, -1);
44694472
}
44704473

44714474
int colx = p_pos.x - (theme_cache.style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding);
@@ -4482,6 +4485,11 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_
44824485
} else {
44834486
colx -= wrap_indent;
44844487
}
4488+
4489+
if (!p_clamp_column && (colx < 0 || colx > TS->shaped_text_get_size(text_rid).x)) {
4490+
return Point2i(-1, -1);
4491+
}
4492+
44854493
int col = TS->shaped_text_hit_test_position(text_rid, colx);
44864494
if (!caret_mid_grapheme_enabled) {
44874495
col = TS->shaped_text_closest_character_pos(text_rid, col);
@@ -6740,7 +6748,7 @@ void TextEdit::_bind_methods() {
67406748

67416749
ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
67426750

6743-
ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "allow_out_of_bounds"), &TextEdit::get_line_column_at_pos, DEFVAL(true));
6751+
ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "clamp_line", "clamp_column"), &TextEdit::get_line_column_at_pos, DEFVAL(true), DEFVAL(true));
67446752
ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column);
67456753
ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column);
67466754

scene/gui/text_edit.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ class TextEdit : public Control {
658658

659659
#ifndef DISABLE_DEPRECATED
660660
void _set_selection_mode_compat_86978(SelectionMode p_mode, int p_line = -1, int p_column = -1, int p_caret = 0);
661+
Point2i _get_line_column_at_pos_bind_compat_100913(const Point2i &p_pos, bool p_allow_out_of_bounds = true) const;
661662
static void _bind_compatibility_methods();
662663
#endif // DISABLE_DEPRECATED
663664

@@ -860,7 +861,7 @@ class TextEdit : public Control {
860861

861862
String get_word_at_pos(const Vector2 &p_pos) const;
862863

863-
Point2i get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_of_bounds = true) const;
864+
Point2i get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line = true, bool p_clamp_column = true) const;
864865
Point2i get_pos_at_line_column(int p_line, int p_column) const;
865866
Rect2i get_rect_at_line_column(int p_line, int p_column) const;
866867

0 commit comments

Comments
 (0)