Move symbol lookup into CodeEdit

This commit is contained in:
Paulb23 2021-07-01 17:10:54 +01:00
parent 8f900ac178
commit dd5a37f556
8 changed files with 198 additions and 113 deletions

View file

@ -240,6 +240,13 @@
Returns the full text with char [code]0xFFFF[/code] at the caret location.
</description>
</method>
<method name="get_text_for_symbol_lookup">
<return type="String">
</return>
<description>
Returns the full text with char [code]0xFFFF[/code] at the cursor location.
</description>
</method>
<method name="has_auto_brace_completion_close_key" qualifiers="const">
<return type="bool">
</return>
@ -382,6 +389,15 @@
<description>
</description>
</method>
<method name="set_symbol_lookup_word_as_valid">
<return type="void">
</return>
<argument index="0" name="valid" type="bool">
</argument>
<description>
Sets the symbol emitted by [signal symbol_validate] as a valid lookup.
</description>
</method>
<method name="toggle_foldable_line">
<return type="void" />
<argument index="0" name="line" type="int" />
@ -466,8 +482,10 @@
</member>
<member name="line_length_guidelines" type="int[]" setter="set_line_length_guidelines" getter="get_line_length_guidelines" default="[]">
</member>
Draws vertical lines at the provided columns. The first entry is considered a main hard guideline and is draw more prominently.
<member name="structured_text_bidi_override_options" type="Array" setter="set_structured_text_bidi_override_options" getter="get_structured_text_bidi_override_options" override="true" default="[]" />
<member name="symbol_lookup_on_click" type="bool" setter="set_symbol_lookup_on_click_enabled" getter="is_symbol_lookup_on_click_enabled" default="false">
Set when a validated word from [signal symbol_validate] is clicked, the [signal symbol_lookup] should be emitted.
</member>
<member name="text_direction" type="int" setter="set_text_direction" getter="get_text_direction" override="true" enum="Control.TextDirection" default="1" />
<member name="zero_pad_line_numbers" type="bool" setter="set_line_numbers_zero_padded" getter="is_line_numbers_zero_padded" default="false">
</member>
@ -483,6 +501,24 @@
Emitted when the user requests code completion.
</description>
</signal>
<signal name="symbol_lookup">
<argument index="0" name="symbol" type="String">
</argument>
<argument index="1" name="line" type="int">
</argument>
<argument index="2" name="column" type="int">
</argument>
<description>
Emitted when the user has clicked on a valid symbol.
</description>
</signal>
<signal name="symbol_validate">
<argument index="0" name="symbol" type="String">
</argument>
<description>
Emitted when the user hovers over a symbol. The symbol should be validated and responded to, by calling [method set_symbol_lookup_word_as_valid].
</description>
</signal>
</signals>
<constants>
<constant name="KIND_CLASS" value="0" enum="CodeCompletionKind">

View file

@ -295,6 +295,12 @@
Returns [code]true[/code] if the caret is visible on the screen.
</description>
</method>
<method name="is_dragging_cursor" qualifiers="const">
<return type="bool">
</return>
<description>
</description>
</method>
<method name="is_gutter_clickable" qualifiers="const">
<return type="bool" />
<argument index="0" name="gutter" type="int" />
@ -668,18 +674,6 @@
<description>
</description>
</signal>
<signal name="symbol_lookup">
<argument index="0" name="symbol" type="String" />
<argument index="1" name="row" type="int" />
<argument index="2" name="column" type="int" />
<description>
</description>
</signal>
<signal name="symbol_validate">
<argument index="0" name="symbol" type="String" />
<description>
</description>
</signal>
<signal name="text_changed">
<description>
Emitted when the text changes.

View file

@ -750,7 +750,7 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c
EditorNode::get_singleton()->load_resource(p_symbol);
}
} else if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_lookup_completion(), p_symbol, script->get_path(), base, result) == OK) {
} else if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK) {
_goto_line(p_row);
result.class_name = result.class_name.trim_prefix("_");
@ -853,18 +853,17 @@ void ScriptTextEditor::_validate_symbol(const String &p_symbol) {
}
ScriptLanguage::LookupResult result;
if (ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_lookup_completion(), p_symbol, script->get_path(), base, result) == OK || (ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton)) {
text_edit->set_highlighted_word(p_symbol);
if (ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK || (ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton)) {
text_edit->set_symbol_lookup_word_as_valid(true);
} else if (p_symbol.is_rel_path()) {
String path = _get_absolute_path(p_symbol);
if (FileAccess::exists(path)) {
text_edit->set_highlighted_word(p_symbol);
text_edit->set_symbol_lookup_word_as_valid(true);
} else {
text_edit->set_highlighted_word(String());
text_edit->set_symbol_lookup_word_as_valid(false);
}
} else {
text_edit->set_highlighted_word(String());
text_edit->set_symbol_lookup_word_as_valid(false);
}
}
@ -1552,7 +1551,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
base = _find_node_for_script(base, base, script);
}
ScriptLanguage::LookupResult result;
if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_lookup_completion(), word_at_pos, script->get_path(), base, result) == OK) {
if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) {
open_docs = true;
}
}
@ -1837,7 +1836,7 @@ ScriptTextEditor::ScriptTextEditor() {
code_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"));
code_editor->get_text_editor()->set_select_identifiers_on_hover(true);
code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
code_editor->get_text_editor()->set_context_menu_enabled(false);
context_menu = memnew(PopupMenu);

View file

@ -663,7 +663,7 @@ ShaderEditor::ShaderEditor(EditorNode *p_node) {
shader_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"));
shader_editor->get_text_editor()->set_select_identifiers_on_hover(true);
shader_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
shader_editor->get_text_editor()->set_context_menu_enabled(false);
shader_editor->get_text_editor()->connect("gui_input", callable_mp(this, &ShaderEditor::_text_edit_gui_input));

View file

@ -299,6 +299,39 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
}
}
}
} else {
if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
if (mb->is_command_pressed() && symbol_lookup_word != String()) {
Vector2i mpos = mb->get_position();
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
int line, col;
_get_mouse_pos(Point2i(mpos.x, mpos.y), line, col);
emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
return;
}
}
}
}
Ref<InputEventMouseMotion> mm = p_gui_input;
if (mm.is_valid()) {
Vector2i mpos = mm->get_position();
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
if (symbol_lookup_on_click_enabled) {
if (mm->is_command_pressed() && mm->get_button_mask() == 0 && !is_dragging_cursor()) {
symbol_lookup_new_word = get_word_at_pos(mpos);
if (symbol_lookup_new_word != symbol_lookup_word) {
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
}
} else {
set_symbol_lookup_word_as_valid(false);
}
}
}
@ -309,6 +342,25 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
return;
}
/* Ctrl + Hover symbols */
#ifdef OSX_ENABLED
if (k->get_keycode() == KEY_META) {
#else
if (k->get_keycode() == KEY_CTRL) {
#endif
if (symbol_lookup_on_click_enabled) {
if (k->is_pressed() && !is_dragging_cursor()) {
symbol_lookup_new_word = get_word_at_pos(_get_local_mouse_pos());
if (symbol_lookup_new_word != symbol_lookup_word) {
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
}
} else {
set_symbol_lookup_word_as_valid(false);
}
}
return;
}
/* If a modifier has been pressed, and nothing else, return. */
if (!k->is_pressed() || k->get_keycode() == KEY_CTRL || k->get_keycode() == KEY_ALT || k->get_keycode() == KEY_SHIFT || k->get_keycode() == KEY_META) {
return;
@ -460,6 +512,10 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
/* General overrides */
Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
if (symbol_lookup_word != String()) {
return CURSOR_POINTING_HAND;
}
if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (is_readonly() && (!is_selecting_enabled() || get_line_count() == 0))) {
return CURSOR_ARROW;
}
@ -1897,6 +1953,48 @@ TypedArray<int> CodeEdit::get_line_length_guidelines() const {
return line_length_guideline_columns;
}
/* Symbol lookup */
void CodeEdit::set_symbol_lookup_on_click_enabled(bool p_enabled) {
symbol_lookup_on_click_enabled = p_enabled;
set_symbol_lookup_word_as_valid(false);
}
bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
return symbol_lookup_on_click_enabled;
}
String CodeEdit::get_text_for_symbol_lookup() {
int line, col;
Point2i mp = _get_local_mouse_pos();
_get_mouse_pos(mp, line, col);
StringBuilder lookup_text;
const int text_size = get_line_count();
for (int i = 0; i < text_size; i++) {
String text = get_line(i);
if (i == line) {
lookup_text += text.substr(0, col);
/* Not unicode, represents the cursor. */
lookup_text += String::chr(0xFFFF);
lookup_text += text.substr(col, text.size());
} else {
lookup_text += text;
}
if (i != text_size - 1) {
lookup_text += "\n";
}
}
return lookup_text.as_string();
}
void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) {
symbol_lookup_word = p_valid ? symbol_lookup_new_word : "";
symbol_lookup_new_word = "";
_set_symbol_lookup_word(symbol_lookup_word);
}
void CodeEdit::_bind_methods() {
/* Indent management */
ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
@ -2065,7 +2163,17 @@ void CodeEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
/* Symbol lookup */
ClassDB::bind_method(D_METHOD("set_symbol_lookup_on_click_enabled", "enable"), &CodeEdit::set_symbol_lookup_on_click_enabled);
ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled);
ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup);
ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid);
/* Inspector */
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_lookup_on_click"), "set_symbol_lookup_on_click_enabled", "is_symbol_lookup_on_click_enabled");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
@ -2101,8 +2209,15 @@ void CodeEdit::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
/* Signals */
/* Gutters */
ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
/* Code Completion */
ADD_SIGNAL(MethodInfo("request_code_completion"));
/* Symbol lookup */
ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
}
/* Auto brace completion */

View file

@ -227,6 +227,12 @@ private:
TypedArray<int> line_length_guideline_columns;
Color line_length_guideline_color;
/* Symbol lookup */
bool symbol_lookup_on_click_enabled = false;
String symbol_lookup_new_word = "";
String symbol_lookup_word = "";
protected:
void _gui_input(const Ref<InputEvent> &p_gui_input) override;
void _notification(int p_what);
@ -386,6 +392,14 @@ public:
void set_line_length_guidelines(TypedArray<int> p_guideline_columns);
TypedArray<int> get_line_length_guidelines() const;
/* Symbol lookup */
void set_symbol_lookup_on_click_enabled(bool p_enabled);
bool is_symbol_lookup_on_click_enabled() const;
String get_text_for_symbol_lookup();
void set_symbol_lookup_word_as_valid(bool p_valid);
CodeEdit();
~CodeEdit();
};

View file

@ -1177,11 +1177,11 @@ void TextEdit::_notification(int p_what) {
}
}
if (!clipped && select_identifiers_enabled && highlighted_word.length() != 0) { // Highlight word
if (_is_char(highlighted_word[0]) || highlighted_word[0] == '.') {
int highlighted_word_col = _get_column_pos_of_word(highlighted_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word
if (_is_char(lookup_symbol_word[0]) || lookup_symbol_word[0] == '.') {
int highlighted_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
while (highlighted_word_col != -1) {
Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_word_col + start, highlighted_word_col + highlighted_word.length() + start);
Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_word_col + start, highlighted_word_col + lookup_symbol_word.length() + start);
for (int j = 0; j < sel.size(); j++) {
Rect2 rect = Rect2(sel[j].x + char_margin + ofs_x, ofs_y, sel[j].y - sel[j].x, row_height);
if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) {
@ -1198,7 +1198,7 @@ void TextEdit::_notification(int p_what) {
draw_rect(rect, cache.font_selected_color);
}
highlighted_word_col = _get_column_pos_of_word(highlighted_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_word_col + 1);
highlighted_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_word_col + 1);
}
}
}
@ -2112,6 +2112,10 @@ void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const
r_row = row;
}
bool TextEdit::is_dragging_cursor() const {
return dragging_selection || dragging_minimap;
}
void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
ERR_FAIL_COND(p_gui_input.is_null());
@ -2289,14 +2293,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
}
} else {
if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
if (mb->is_command_pressed() && highlighted_word != String()) {
int row, col;
_get_mouse_pos(Point2i(mpos.x, mpos.y), row, col);
emit_signal(SNAME("symbol_lookup"), highlighted_word, row, col);
return;
}
dragging_minimap = false;
dragging_selection = false;
can_drag_minimap = false;
@ -2331,18 +2327,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
if (select_identifiers_enabled) {
if (!dragging_minimap && !dragging_selection && mm->is_command_pressed() && mm->get_button_mask() == 0) {
String new_word = get_word_at_pos(mpos);
if (new_word != highlighted_word) {
emit_signal(SNAME("symbol_validate"), new_word);
}
} else {
if (highlighted_word != String()) {
set_highlighted_word(String());
}
}
}
if (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging.
_reset_caret_blink_timer();
@ -2377,23 +2361,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
Ref<InputEventKey> k = p_gui_input;
if (k.is_valid()) {
// Ctrl + Hover symbols
#ifdef OSX_ENABLED
if (k->get_keycode() == KEY_META) {
#else
if (k->get_keycode() == KEY_CTRL) {
#endif
if (select_identifiers_enabled) {
if (k->is_pressed() && !dragging_minimap && !dragging_selection) {
Point2 mp = _get_local_mouse_pos();
emit_signal(SNAME("symbol_validate"), get_word_at_pos(mp));
} else {
set_highlighted_word(String());
}
}
return;
}
if (!k->is_pressed()) {
return;
}
@ -3515,10 +3482,6 @@ void TextEdit::insert_text_at_cursor(const String &p_text) {
}
Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
if (highlighted_word != String()) {
return CURSOR_POINTING_HAND;
}
int row, col;
_get_mouse_pos(p_pos, row, col);
@ -3701,30 +3664,6 @@ bool TextEdit::get_draw_control_chars() const {
return draw_control_chars;
}
String TextEdit::get_text_for_lookup_completion() {
int row, col;
Point2i mp = _get_local_mouse_pos();
_get_mouse_pos(mp, row, col);
String longthing;
int len = text.size();
for (int i = 0; i < len; i++) {
if (i == row) {
longthing += text[i].substr(0, col);
longthing += String::chr(0xFFFF); // Not unicode, represents the cursor.
longthing += text[i].substr(col, text[i].size());
} else {
longthing += text[i];
}
if (i != len - 1) {
longthing += "\n";
}
}
return longthing;
}
String TextEdit::get_line(int line) const {
if (line < 0 || line >= text.size()) {
return "";
@ -5297,19 +5236,11 @@ void TextEdit::menu_option(int p_option) {
}
}
void TextEdit::set_highlighted_word(const String &new_word) {
highlighted_word = new_word;
void TextEdit::_set_symbol_lookup_word(const String &p_symbol) {
lookup_symbol_word = p_symbol;
update();
}
void TextEdit::set_select_identifiers_on_hover(bool p_enable) {
select_identifiers_enabled = p_enable;
}
bool TextEdit::is_selecting_identifiers_on_hover_enabled() const {
return select_identifiers_enabled;
}
void TextEdit::set_context_menu_enabled(bool p_enable) {
context_menu_enabled = p_enable;
}
@ -5510,6 +5441,7 @@ void TextEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all);
ClassDB::bind_method(D_METHOD("deselect"), &TextEdit::deselect);
ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor);
ClassDB::bind_method(D_METHOD("is_selection_active"), &TextEdit::is_selection_active);
ClassDB::bind_method(D_METHOD("get_selection_from_line"), &TextEdit::get_selection_from_line);
@ -5642,8 +5574,6 @@ void TextEdit::_bind_methods() {
ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter")));
ADD_SIGNAL(MethodInfo("gutter_added"));
ADD_SIGNAL(MethodInfo("gutter_removed"));
ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "row"), PropertyInfo(Variant::INT, "column")));
ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
BIND_ENUM_CONSTANT(MENU_CUT);
BIND_ENUM_CONSTANT(MENU_COPY);

View file

@ -305,7 +305,7 @@ private:
float target_v_scroll = 0.0;
float v_scroll_speed = 80.0;
String highlighted_word;
String lookup_symbol_word;
uint64_t last_dblclk = 0;
@ -382,7 +382,6 @@ private:
Size2 get_minimum_size() const override;
int _get_control_height() const;
Point2 _get_local_mouse_pos() const;
int _get_menu_action_accelerator(const String &p_action);
void _reset_caret_blink_timer();
@ -473,6 +472,8 @@ protected:
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _set_symbol_lookup_word(const String &p_symbol);
public:
/* Syntax Highlighting. */
Ref<SyntaxHighlighter> get_syntax_highlighter();
@ -567,8 +568,10 @@ public:
virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
Point2 _get_local_mouse_pos() const;
void _get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const;
void _get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const;
bool is_dragging_cursor() const;
//void delete_char();
//void delete_line();
@ -597,7 +600,6 @@ public:
void set_structured_text_bidi_override_options(Array p_args);
Array get_structured_text_bidi_override_options() const;
void set_highlighted_word(const String &new_word);
void set_text(String p_text);
void insert_text_at_cursor(const String &p_text);
void insert_at(const String &p_text, int at);
@ -746,9 +748,6 @@ public:
void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata);
void set_select_identifiers_on_hover(bool p_enable);
bool is_selecting_identifiers_on_hover_enabled() const;
void set_context_menu_enabled(bool p_enable);
bool is_context_menu_enabled();
@ -764,8 +763,6 @@ public:
bool is_menu_visible() const;
PopupMenu *get_menu() const;
String get_text_for_lookup_completion();
virtual bool is_text_field() const override;
TextEdit();
~TextEdit();