Skip to content

Commit 42e07df

Browse files
committed
Merge pull request godotengine#100913 from larspet/tooltip-hover-oob
Prevent tooltip from showing when hovering past the end of script line
2 parents f0f5319 + ff39add commit 42e07df

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
@@ -420,7 +420,7 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
420420
mpos.x = get_size().x - mpos.x;
421421
}
422422

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

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

443443
if (symbol_lookup_on_click_enabled) {
444444
if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
445-
symbol_lookup_pos = get_line_column_at_pos(mpos);
445+
symbol_lookup_pos = get_line_column_at_pos(mpos, false, false);
446446
symbol_lookup_new_word = get_word_at_pos(mpos);
447447
if (symbol_lookup_new_word != symbol_lookup_word) {
448448
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
449449
}
450-
} else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos))) {
450+
} 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))) {
451451
set_symbol_lookup_word_as_valid(false);
452452
}
453453
}
454454

455455
if (symbol_tooltip_on_hover_enabled) {
456-
symbol_tooltip_pos = get_line_column_at_pos(mpos, false);
456+
symbol_tooltip_pos = get_line_column_at_pos(mpos, false, false);
457457
symbol_tooltip_word = get_word_at_pos(mpos);
458458
symbol_tooltip_timer->start();
459459
}
@@ -2387,7 +2387,7 @@ bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
23872387

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

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
@@ -4370,9 +4370,12 @@ Point2 TextEdit::get_local_mouse_pos() const {
43704370
}
43714371

43724372
String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
4373-
Point2i pos = get_line_column_at_pos(p_pos);
4373+
Point2i pos = get_line_column_at_pos(p_pos, false, false);
43744374
int row = pos.y;
43754375
int col = pos.x;
4376+
if (row < 0 || col < 0) {
4377+
return "";
4378+
}
43764379

43774380
String s = text[row];
43784381
if (s.length() == 0) {
@@ -4408,7 +4411,7 @@ String TextEdit::get_word_at_pos(const Vector2 &p_pos) const {
44084411
return String();
44094412
}
44104413

4411-
Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_of_bounds) const {
4414+
Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const {
44124415
float rows = p_pos.y - theme_cache.style_normal->get_margin(SIDE_TOP);
44134416
if (!editable) {
44144417
rows -= theme_cache.style_readonly->get_offset().y / 2;
@@ -4435,10 +4438,10 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_
44354438

44364439
int visible_lines = get_visible_line_count_in_range(first_vis_line, row);
44374440
if (rows > visible_lines) {
4438-
if (!p_allow_out_of_bounds) {
4439-
return Point2i(-1, -1);
4441+
if (p_clamp_line) {
4442+
return Point2i(text[row].length(), row);
44404443
}
4441-
return Point2i(text[row].length(), row);
4444+
return Point2i(-1, -1);
44424445
}
44434446

44444447
int colx = p_pos.x - (theme_cache.style_normal->get_margin(SIDE_LEFT) + gutters_width + gutter_padding);
@@ -4455,6 +4458,11 @@ Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_allow_out_
44554458
} else {
44564459
colx -= wrap_indent;
44574460
}
4461+
4462+
if (!p_clamp_column && (colx < 0 || colx > TS->shaped_text_get_size(text_rid).x)) {
4463+
return Point2i(-1, -1);
4464+
}
4465+
44584466
int col = TS->shaped_text_hit_test_position(text_rid, colx);
44594467
if (!caret_mid_grapheme_enabled) {
44604468
col = TS->shaped_text_closest_character_pos(text_rid, col);
@@ -6642,7 +6650,7 @@ void TextEdit::_bind_methods() {
66426650

66436651
ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos);
66446652

6645-
ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "allow_out_of_bounds"), &TextEdit::get_line_column_at_pos, DEFVAL(true));
6653+
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));
66466654
ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column);
66476655
ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column);
66486656

scene/gui/text_edit.h

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

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

@@ -859,7 +860,7 @@ class TextEdit : public Control {
859860

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

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

0 commit comments

Comments
 (0)