Cleanup and bind remaing methods in TextEdit

This commit is contained in:
Paulb23 2021-07-10 11:41:38 +01:00
parent 0a32a6907b
commit ae4dcb8918
9 changed files with 2570 additions and 2477 deletions

View file

@ -142,26 +142,23 @@ void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) {
}
bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) {
int line, col;
String text = get_search_text();
Point2i pos = text_editor->search(text, p_flags, p_from_line, p_from_col);
bool found = text_editor->search(text, p_flags, p_from_line, p_from_col, line, col);
if (found) {
if (pos.x != -1) {
if (!preserve_cursor && !is_selection_only()) {
text_editor->unfold_line(line);
text_editor->set_caret_line(line, false);
text_editor->set_caret_column(col + text.length(), false);
text_editor->unfold_line(pos.y);
text_editor->set_caret_line(pos.y, false);
text_editor->set_caret_column(pos.x + text.length(), false);
text_editor->center_viewport_to_caret();
text_editor->select(line, col, line, col + text.length());
text_editor->select(pos.y, pos.x, pos.y, pos.x + text.length());
}
text_editor->set_search_text(text);
text_editor->set_search_flags(p_flags);
text_editor->set_current_search_result(line, col);
result_line = line;
result_col = col;
result_line = pos.y;
result_col = pos.x;
_update_results_count();
} else {
@ -170,12 +167,11 @@ bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col)
result_col = -1;
text_editor->set_search_text("");
text_editor->set_search_flags(p_flags);
text_editor->set_current_search_result(line, col);
}
_update_matches_label();
return found;
return pos.x != -1;
}
void FindReplaceBar::_replace() {

View file

@ -1048,6 +1048,7 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
theme->set_color("font_readonly_color", "LineEdit", font_readonly_color);
theme->set_color("caret_color", "TextEdit", font_color);
theme->set_color("selection_color", "TextEdit", selection_color);
theme->set_constant("line_spacing", "TextEdit", 4 * EDSCALE);
// CodeEdit
theme->set_stylebox("normal", "CodeEdit", style_widget);
@ -1062,6 +1063,7 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
theme->set_color("font_color", "CodeEdit", font_color);
theme->set_color("caret_color", "CodeEdit", font_color);
theme->set_color("selection_color", "CodeEdit", selection_color);
theme->set_constant("line_spacing", "CodeEdit", 4 * EDSCALE);
// H/VSplitContainer
theme->set_stylebox("bg", "VSplitContainer", make_stylebox(theme->get_icon("GuiVsplitBg", "EditorIcons"), 1, 1, 1, 1));

View file

@ -241,7 +241,7 @@ void ScriptTextEditor::_warning_clicked(Variant p_line) {
goto_line_centered(p_line.operator int64_t());
} else if (p_line.get_type() == Variant::DICTIONARY) {
Dictionary meta = p_line.operator Dictionary();
code_editor->get_text_editor()->insert_at("# warning-ignore:" + meta["code"].operator String(), meta["line"].operator int64_t() - 1);
code_editor->get_text_editor()->insert_line_at(meta["line"].operator int64_t() - 1, "# warning-ignore:" + meta["code"].operator String());
_validate_script();
}
}
@ -884,7 +884,7 @@ void ScriptTextEditor::update_toggle_scripts_button() {
void ScriptTextEditor::_update_connected_methods() {
CodeEdit *text_edit = code_editor->get_text_editor();
text_edit->set_gutter_width(connection_gutter, text_edit->get_row_height());
text_edit->set_gutter_width(connection_gutter, text_edit->get_line_height());
for (int i = 0; i < text_edit->get_line_count(); i++) {
if (text_edit->get_line_gutter_metadata(i, connection_gutter) == "") {
continue;
@ -1325,7 +1325,7 @@ void ScriptTextEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_row_height());
code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_line_height());
} break;
default:
break;
@ -1422,8 +1422,10 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
Dictionary d = p_data;
CodeEdit *te = code_editor->get_text_editor();
int row, col;
te->_get_mouse_pos(p_point, row, col);
Point2i pos = te->get_line_column_at_pos(p_point);
int row = pos.y;
int col = pos.x;
if (d.has("type") && String(d["type"]) == "resource") {
Ref<Resource> res = d["resource"];
@ -1512,8 +1514,9 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
}
if (create_menu) {
int col, row;
tx->_get_mouse_pos(local_pos, row, col);
Point2i pos = tx->get_line_column_at_pos(local_pos);
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
if (tx->is_move_caret_on_right_click_enabled()) {

View file

@ -550,9 +550,11 @@ void ShaderEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
if (mb.is_valid()) {
if (mb->get_button_index() == MOUSE_BUTTON_RIGHT && mb->is_pressed()) {
int col, row;
CodeEdit *tx = shader_editor->get_text_editor();
tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
if (tx->is_move_caret_on_right_click_enabled()) {

View file

@ -427,9 +427,11 @@ void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
if (mb.is_valid()) {
if (mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
int col, row;
CodeEdit *tx = code_editor->get_text_editor();
tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
bool can_fold = tx->can_fold_line(row);

View file

@ -46,9 +46,16 @@ void CodeEdit::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED:
case NOTIFICATION_ENTER_TREE: {
set_gutter_width(main_gutter, get_row_height());
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
set_gutter_width(fold_gutter, get_row_height() / 1.2);
style_normal = get_theme_stylebox(SNAME("normal"));
font = get_theme_font(SNAME("font"));
font_size = get_theme_font_size(SNAME("font_size"));
line_spacing = get_theme_constant(SNAME("line_spacing"));
set_gutter_width(main_gutter, get_line_height());
set_gutter_width(line_number_gutter, (line_number_digits + 1) * font->get_char_size('0', 0, font_size).width);
set_gutter_width(fold_gutter, get_line_height() / 1.2);
breakpoint_color = get_theme_color(SNAME("breakpoint_color"));
breakpoint_icon = get_theme_icon(SNAME("breakpoint"));
@ -65,7 +72,7 @@ void CodeEdit::_notification(int p_what) {
can_fold_icon = get_theme_icon(SNAME("can_fold"));
folded_icon = get_theme_icon(SNAME("folded"));
code_completion_max_width = get_theme_constant(SNAME("completion_max_width")) * cache.font->get_char_size('x').x;
code_completion_max_width = get_theme_constant(SNAME("completion_max_width")) * font->get_char_size('x').x;
code_completion_max_lines = get_theme_constant(SNAME("completion_lines"));
code_completion_scroll_width = get_theme_constant(SNAME("completion_scroll_width"));
code_completion_scroll_color = get_theme_color(SNAME("completion_scroll_color"));
@ -80,12 +87,12 @@ void CodeEdit::_notification(int p_what) {
const Size2 size = get_size();
const bool caret_visible = is_caret_visible();
const bool rtl = is_layout_rtl();
const int row_height = get_row_height();
const int row_height = get_line_height();
if (line_length_guideline_columns.size() > 0) {
const int xmargin_beg = cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
const int xmargin_end = size.width - cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
const int char_size = (int)cache.font->get_char_size('0', 0, cache.font_size).width;
const int xmargin_beg = style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
const int xmargin_end = size.width - style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
const int char_size = (int)font->get_char_size('0', 0, font_size).width;
for (int i = 0; i < line_length_guideline_columns.size(); i++) {
const int xoffset = xmargin_beg + char_size * (int)line_length_guideline_columns[i] - get_h_scroll();
@ -115,14 +122,14 @@ void CodeEdit::_notification(int p_what) {
const Point2 caret_pos = get_caret_draw_pos();
const int total_height = csb->get_minimum_size().y + code_completion_rect.size.height;
if (caret_pos.y + row_height + total_height > get_size().height) {
code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + cache.line_spacing;
code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + line_spacing;
} else {
code_completion_rect.position.y = caret_pos.y + (cache.line_spacing / 2.0f);
code_completion_rect.position.y = caret_pos.y + (line_spacing / 2.0f);
code_completion_below = true;
}
const int scroll_width = code_completion_options_count > code_completion_max_lines ? code_completion_scroll_width : 0;
const int code_completion_base_width = cache.font->get_string_size(code_completion_base).width;
const int code_completion_base_width = font->get_string_size(code_completion_base).width;
if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) {
code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width;
} else {
@ -144,7 +151,7 @@ void CodeEdit::_notification(int p_what) {
Ref<TextLine> tl;
tl.instantiate();
tl->add_string(code_completion_options[l].display, cache.font, cache.font_size);
tl->add_string(code_completion_options[l].display, font, font_size);
int yofs = (row_height - tl->get_size().y) / 2;
Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
@ -183,8 +190,7 @@ void CodeEdit::_notification(int p_what) {
/* Code hint */
if (caret_visible && code_hint != "" && (!code_completion_active || (code_completion_below != code_hint_draw_below))) {
const Ref<Font> font = cache.font;
const int font_height = font->get_height(cache.font_size);
const int font_height = font->get_height(font_size);
Ref<StyleBox> sb = get_theme_stylebox(SNAME("panel"), SNAME("TooltipPanel"));
Color font_color = get_theme_color(SNAME("font_color"), SNAME("TooltipLabel"));
@ -193,37 +199,37 @@ void CodeEdit::_notification(int p_what) {
int max_width = 0;
for (int i = 0; i < line_count; i++) {
max_width = MAX(max_width, font->get_string_size(code_hint_lines[i], cache.font_size).x);
max_width = MAX(max_width, font->get_string_size(code_hint_lines[i], font_size).x);
}
Size2 minsize = sb->get_minimum_size() + Size2(max_width, line_count * font_height + (cache.line_spacing * line_count - 1));
Size2 minsize = sb->get_minimum_size() + Size2(max_width, line_count * font_height + (line_spacing * line_count - 1));
int offset = font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), cache.font_size).x;
int offset = font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), font_size).x;
if (code_hint_xpos == -0xFFFF) {
code_hint_xpos = get_caret_draw_pos().x - offset;
}
Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y);
if (code_hint_draw_below) {
hint_ofs.y += cache.line_spacing / 2.0f;
hint_ofs.y += line_spacing / 2.0f;
} else {
hint_ofs.y -= (minsize.y + row_height) - cache.line_spacing;
hint_ofs.y -= (minsize.y + row_height) - line_spacing;
}
draw_style_box(sb, Rect2(hint_ofs, minsize));
int line_spacing = 0;
int yofs = 0;
for (int i = 0; i < line_count; i++) {
const String &line = code_hint_lines[i];
int begin = 0;
int end = 0;
if (line.find(String::chr(0xFFFF)) != -1) {
begin = font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), cache.font_size).x;
end = font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), cache.font_size).x;
begin = font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), font_size).x;
end = font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), font_size).x;
}
Point2 round_ofs = hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font_height * i + line_spacing);
Point2 round_ofs = hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font_height * i + yofs);
round_ofs = round_ofs.round();
draw_string(font, round_ofs, line.replace(String::chr(0xFFFF), ""), HALIGN_LEFT, -1, cache.font_size, font_color);
draw_string(font, round_ofs, line.replace(String::chr(0xFFFF), ""), HALIGN_LEFT, -1, font_size, font_color);
if (end > 0) {
// Draw an underline for the currently edited function parameter.
const Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font_height + font_height * i + line_spacing);
@ -235,7 +241,7 @@ void CodeEdit::_notification(int p_what) {
Vector2(end - begin, font_height));
draw_rect(highlight_rect, font_color * Color(1, 1, 1, 0.2));
}
line_spacing += cache.line_spacing;
yofs += line_spacing;
}
}
} break;
@ -270,7 +276,7 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
}
} break;
case MOUSE_BUTTON_LEFT: {
code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_row_height(), 0, code_completion_options.size() - 1);
code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_line_height(), 0, code_completion_options.size() - 1);
if (mb->is_double_click()) {
confirm_code_completion();
}
@ -290,8 +296,9 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
mpos.x = get_size().x - mpos.x;
}
int line, col;
_get_mouse_pos(Point2i(mpos.x, mpos.y), line, col);
Point2i pos = get_line_column_at_pos(Point2i(mpos.x, mpos.y));
int line = pos.y;
int col = pos.x;
if (mb->get_button_index() == MOUSE_BUTTON_LEFT) {
if (is_line_folded(line)) {
@ -313,8 +320,10 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
int line, col;
_get_mouse_pos(Point2i(mpos.x, mpos.y), line, col);
Point2i pos = get_line_column_at_pos(Point2i(mpos.x, mpos.y));
int line = pos.y;
int col = pos.x;
emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
return;
@ -357,7 +366,7 @@ void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
#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());
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);
}
@ -527,8 +536,9 @@ Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
return CURSOR_ARROW;
}
int line, col;
_get_mouse_pos(p_pos, line, col);
Point2i pos = get_line_column_at_pos(p_pos);
int line = pos.y;
int col = pos.x;
if (is_line_folded(line)) {
int wrap_index = get_line_wrap_index_at_column(line, col);
@ -560,7 +570,7 @@ void CodeEdit::_handle_unicode_input(const uint32_t p_unicode) {
/* Make sure we don't try and remove empty space. */
if (get_caret_column() < get_line(get_caret_line()).length()) {
_remove_text(get_caret_line(), get_caret_column(), get_caret_line(), get_caret_column() + 1);
remove_text(get_caret_line(), get_caret_column(), get_caret_line(), get_caret_column() + 1);
}
}
@ -631,9 +641,9 @@ void CodeEdit::_backspace() {
prev_column = cc - auto_brace_completion_pairs[idx].open_key.length();
if (_get_auto_brace_pair_close_at_pos(cl, cc) == idx) {
_remove_text(prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
remove_text(prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
} else {
_remove_text(prev_line, prev_column, cl, cc);
remove_text(prev_line, prev_column, cl, cc);
}
set_caret_line(prev_line, false, true);
set_caret_column(prev_column);
@ -650,7 +660,7 @@ void CodeEdit::_backspace() {
}
}
_remove_text(prev_line, prev_column, cl, cc);
remove_text(prev_line, prev_column, cl, cc);
set_caret_line(prev_line, false, true);
set_caret_column(prev_column);
@ -801,7 +811,7 @@ void CodeEdit::do_unindent() {
const String &line = get_line(cl);
if (line[cc - 1] == '\t') {
_remove_text(cl, cc - 1, cl, cc);
remove_text(cl, cc - 1, cl, cc);
set_caret_column(MAX(0, cc - 1));
return;
}
@ -818,7 +828,7 @@ void CodeEdit::do_unindent() {
break;
}
}
_remove_text(cl, cc - spaces_to_remove, cl, cc);
remove_text(cl, cc - spaces_to_remove, cl, cc);
set_caret_column(MAX(0, cc - spaces_to_remove));
}
}
@ -1281,8 +1291,8 @@ void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2
String fc = TS->format_number(String::num(p_line + 1).lpad(line_number_digits, line_number_padding));
Ref<TextLine> tl;
tl.instantiate();
tl->add_string(fc, cache.font, cache.font_size);
int yofs = p_region.position.y + (get_row_height() - tl->get_size().y) / 2;
tl->add_string(fc, font, font_size);
int yofs = p_region.position.y + (get_line_height() - tl->get_size().y) / 2;
Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
if (number_color == Color(1, 1, 1)) {
number_color = line_number_color;
@ -1881,7 +1891,7 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
}
/* Replace. */
_remove_text(caret_line, get_caret_column() - code_completion_base.length(), caret_remove_line, caret_col);
remove_text(caret_line, get_caret_column() - code_completion_base.length(), caret_remove_line, caret_col);
set_caret_column(get_caret_column() - code_completion_base.length(), false);
insert_text_at_caret(insert_text);
} else {
@ -1897,7 +1907,7 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
}
/* Remove base completion text. */
_remove_text(caret_line, get_caret_column() - code_completion_base.length(), caret_line, get_caret_column());
remove_text(caret_line, get_caret_column() - code_completion_base.length(), caret_line, get_caret_column());
set_caret_column(get_caret_column() - code_completion_base.length(), false);
/* Merge with text. */
@ -1916,11 +1926,11 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
int post_brace_pair = get_caret_column() < get_line(caret_line).length() ? _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column()) : -1;
if (post_brace_pair != -1 && (last_completion_char == next_char || last_completion_char_display == next_char)) {
_remove_text(caret_line, get_caret_column(), caret_line, get_caret_column() + 1);
remove_text(caret_line, get_caret_column(), caret_line, get_caret_column() + 1);
}
if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && (last_completion_char == next_char || last_completion_char_display == next_char)) {
_remove_text(caret_line, get_caret_column(), caret_line, get_caret_column() + 1);
remove_text(caret_line, get_caret_column(), caret_line, get_caret_column() + 1);
} else if (auto_brace_completion_enabled && pre_brace_pair != -1 && post_brace_pair == -1) {
insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key);
set_caret_column(get_caret_column() - auto_brace_completion_pairs[pre_brace_pair].close_key.length());
@ -1929,7 +1939,7 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column() > 0 && get_caret_column() < get_line(caret_line).length()) {
pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column() + 1);
if (pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column() - 1)) {
_remove_text(caret_line, get_caret_column() - 2, caret_line, get_caret_column());
remove_text(caret_line, get_caret_column() - 2, caret_line, get_caret_column());
if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column() - 1) != pre_brace_pair) {
set_caret_column(get_caret_column() - 1);
}
@ -1974,9 +1984,11 @@ bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
}
String CodeEdit::get_text_for_symbol_lookup() {
int line, col;
Point2i mp = _get_local_mouse_pos();
_get_mouse_pos(mp, line, col);
Point2i mp = get_local_mouse_pos();
Point2i pos = get_line_column_at_pos(mp);
int line = pos.y;
int col = pos.x;
StringBuilder lookup_text;
const int text_size = get_line_count();
@ -2677,7 +2689,7 @@ void CodeEdit::_filter_code_completion_candidates() {
option.icon = completion_options[i].get("icon");
option.default_value = completion_options[i].get("default_value");
max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
max_width = MAX(max_width, font->get_string_size(option.display).width);
code_completion_options.push_back(option);
}
@ -2781,7 +2793,7 @@ void CodeEdit::_filter_code_completion_candidates() {
if (string_to_complete.length() == 0) {
code_completion_options.push_back(option);
max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
max_width = MAX(max_width, font->get_string_size(option.display).width);
continue;
}
@ -2830,7 +2842,7 @@ void CodeEdit::_filter_code_completion_candidates() {
} else {
completion_options_subseq.push_back(option);
}
max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
max_width = MAX(max_width, font->get_string_size(option.display).width);
/* Matched the whole subsequence in s_lower. */
} else if (!*ssq_lower) {
/* Finished matching in the first s.length() characters. */
@ -2839,7 +2851,7 @@ void CodeEdit::_filter_code_completion_candidates() {
} else {
completion_options_subseq_casei.push_back(option);
}
max_width = MAX(max_width, cache.font->get_string_size(option.display).width);
max_width = MAX(max_width, font->get_string_size(option.display).width);
}
}
@ -2877,7 +2889,7 @@ void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
while (lc /= 10) {
line_number_digits++;
}
set_gutter_width(line_number_gutter, (line_number_digits + 1) * cache.font->get_char_size('0', 0, cache.font_size).width);
set_gutter_width(line_number_gutter, (line_number_digits + 1) * font->get_char_size('0', 0, font_size).width);
int from_line = MIN(p_from_line, p_to_line);
int line_count = (p_to_line - p_from_line);

View file

@ -233,6 +233,14 @@ private:
String symbol_lookup_new_word = "";
String symbol_lookup_word = "";
/* Visual */
Ref<StyleBox> style_normal;
Ref<Font> font;
int font_size = 16;
int line_spacing = 1;
protected:
void _gui_input(const Ref<InputEvent> &p_gui_input) override;
void _notification(int p_what);

File diff suppressed because it is too large Load diff

View file

@ -70,6 +70,47 @@ public:
GUTTER_TYPE_CUSTOM
};
/* Contex Menu. */
enum MenuItems {
MENU_CUT,
MENU_COPY,
MENU_PASTE,
MENU_CLEAR,
MENU_SELECT_ALL,
MENU_UNDO,
MENU_REDO,
MENU_DIR_INHERITED,
MENU_DIR_AUTO,
MENU_DIR_LTR,
MENU_DIR_RTL,
MENU_DISPLAY_UCC,
MENU_INSERT_LRM,
MENU_INSERT_RLM,
MENU_INSERT_LRE,
MENU_INSERT_RLE,
MENU_INSERT_LRO,
MENU_INSERT_RLO,
MENU_INSERT_PDF,
MENU_INSERT_ALM,
MENU_INSERT_LRI,
MENU_INSERT_RLI,
MENU_INSERT_FSI,
MENU_INSERT_PDI,
MENU_INSERT_ZWJ,
MENU_INSERT_ZWNJ,
MENU_INSERT_WJ,
MENU_INSERT_SHY,
MENU_MAX
};
/* Search. */
enum SearchFlags {
SEARCH_MATCH_CASE = 1,
SEARCH_WHOLE_WORDS = 2,
SEARCH_BACKWARDS = 4
};
private:
struct GutterInfo {
GutterType type = GutterType::GUTTER_TYPE_STRING;
@ -82,11 +123,6 @@ private:
ObjectID custom_draw_obj = ObjectID();
StringName custom_draw_callback;
};
Vector<GutterInfo> gutters;
int gutters_width = 0;
int gutter_padding = 0;
void _update_gutter_width();
class Text {
public:
@ -135,7 +171,7 @@ private:
void set_font(const Ref<Font> &p_font);
void set_font_size(int p_font_size);
void set_font_features(const Dictionary &p_features);
void set_direction_and_language(TextServer::Direction p_direction, String p_language);
void set_direction_and_language(TextServer::Direction p_direction, const String &p_language);
void set_draw_control_chars(bool p_draw_control_chars);
int get_line_height(int p_line, int p_wrap_index) const;
@ -173,7 +209,7 @@ private:
void set_line_gutter_text(int p_line, int p_gutter, const String &p_text) { text.write[p_line].gutters.write[p_gutter].text = p_text; }
const String &get_line_gutter_text(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].text; }
void set_line_gutter_icon(int p_line, int p_gutter, Ref<Texture2D> p_icon) { text.write[p_line].gutters.write[p_gutter].icon = p_icon; }
void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) { text.write[p_line].gutters.write[p_gutter].icon = p_icon; }
const Ref<Texture2D> &get_line_gutter_icon(int p_line, int p_gutter) const { return text[p_line].gutters[p_gutter].icon; }
void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) { text.write[p_line].gutters.write[p_gutter].color = p_color; }
@ -187,15 +223,102 @@ private:
const Color get_line_background_color(int p_line) const { return text[p_line].background_color; }
};
/* Text manipulation */
// User control.
/* Text */
Text text;
bool setting_text = false;
// Text properties.
String ime_text = "";
Point2 ime_selection;
/* Initialise to opposite first, so we get past the early-out in set_editable. */
bool editable = false;
bool overtype_mode = false;
TextDirection text_direction = TEXT_DIRECTION_AUTO;
TextDirection input_direction = TEXT_DIRECTION_LTR;
Dictionary opentype_features;
String language = "";
Control::StructuredTextParser st_parser = STRUCTURED_TEXT_DEFAULT;
Array st_args;
void _clear();
void _update_caches();
// User control.
bool overtype_mode = false;
bool context_menu_enabled = true;
bool shortcut_keys_enabled = true;
bool virtual_keyboard_enabled = true;
// Overridable actions
String cut_copy_line = "";
// Context menu.
PopupMenu *menu = nullptr;
PopupMenu *menu_dir = nullptr;
PopupMenu *menu_ctl = nullptr;
void _generate_context_menu();
int _get_menu_action_accelerator(const String &p_action);
/* Versioning */
struct TextOperation {
enum Type {
TYPE_NONE,
TYPE_INSERT,
TYPE_REMOVE
};
Type type = TYPE_NONE;
int from_line = 0;
int from_column = 0;
int to_line = 0;
int to_column = 0;
String text;
uint32_t prev_version = 0;
uint32_t version = 0;
bool chain_forward = false;
bool chain_backward = false;
};
bool undo_enabled = true;
int undo_stack_max_size = 50;
bool next_operation_is_complex = false;
TextOperation current_op;
List<TextOperation> undo_stack;
List<TextOperation>::Element *undo_stack_pos = nullptr;
Timer *idle_detect;
uint32_t version = 0;
uint32_t saved_version = 0;
void _push_current_op();
void _do_text_op(const TextOperation &p_op, bool p_reverse);
void _clear_redo();
/* Search */
Color search_result_color = Color(1, 1, 1);
Color search_result_border_color = Color(1, 1, 1);
String search_text = "";
uint32_t search_flags = 0;
int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const;
/* Tooltip. */
Object *tooltip_obj = nullptr;
StringName tooltip_func;
Variant tooltip_ud;
/* Mouse */
int _get_char_pos_for_line(int p_px, int p_line, int p_wrap_index = 0) const;
/* Caret. */
struct Caret {
Point2 draw_pos;
@ -230,6 +353,8 @@ private:
void _reset_caret_blink_timer();
void _toggle_draw_caret();
int _get_column_x_offset_for_line(int p_char, int p_line) const;
/* Selection. */
struct Selection {
SelectionMode selecting_mode = SelectionMode::SELECTION_MODE_NONE;
@ -296,6 +421,7 @@ private:
bool updating_scrolls = false;
void _update_scrollbars();
int _get_control_height() const;
void _v_scroll_input();
void _scroll_moved(double p_to_val);
@ -327,122 +453,62 @@ private:
void _update_minimap_click();
void _update_minimap_drag();
/* Gutters. */
Vector<GutterInfo> gutters;
int gutters_width = 0;
int gutter_padding = 0;
void _update_gutter_width();
/* Syntax highlighting. */
Map<int, Dictionary> syntax_highlighting_cache;
struct TextOperation {
enum Type {
TYPE_NONE,
TYPE_INSERT,
TYPE_REMOVE
};
Type type = TYPE_NONE;
int from_line = 0;
int from_column = 0;
int to_line = 0;
int to_column = 0;
String text;
uint32_t prev_version = 0;
uint32_t version = 0;
bool chain_forward = false;
bool chain_backward = false;
};
String ime_text;
Point2 ime_selection;
TextOperation current_op;
List<TextOperation> undo_stack;
List<TextOperation>::Element *undo_stack_pos = nullptr;
int undo_stack_max_size;
void _clear_redo();
void _do_text_op(const TextOperation &p_op, bool p_reverse);
//syntax coloring
Ref<SyntaxHighlighter> syntax_highlighter;
Set<String> keywords;
Map<int, Dictionary> syntax_highlighting_cache;
Dictionary _get_line_syntax_highlighting(int p_line);
bool setting_text = false;
/* Visual. */
Ref<StyleBox> style_normal;
Ref<StyleBox> style_focus;
Ref<StyleBox> style_readonly;
// data
Text text;
Ref<Texture2D> tab_icon;
Ref<Texture2D> space_icon;
Dictionary opentype_features;
String language;
TextDirection text_direction = TEXT_DIRECTION_AUTO;
TextDirection input_direction = TEXT_DIRECTION_LTR;
Control::StructuredTextParser st_parser = STRUCTURED_TEXT_DEFAULT;
Array st_args;
bool draw_control_chars = false;
Ref<Font> font;
int font_size = 16;
Color font_color = Color(1, 1, 1);
Color font_readonly_color = Color(1, 1, 1);
uint32_t version = 0;
uint32_t saved_version = 0;
int outline_size = 0;
Color outline_color = Color(1, 1, 1);
int line_spacing = 1;
Color background_color = Color(1, 1, 1);
Color current_line_color = Color(1, 1, 1);
Color word_highlighted_color = Color(1, 1, 1);
bool window_has_focus = true;
bool first_draw = true;
bool highlight_current_line = false;
bool highlight_all_occurrences = false;
bool draw_control_chars = false;
bool draw_tabs = false;
bool draw_spaces = false;
/*** Super internal Core API. Everything builds on it. ***/
bool text_changed_dirty = false;
bool undo_enabled = true;
bool highlight_all_occurrences = false;
bool highlight_current_line = false;
Timer *idle_detect;
Object *tooltip_obj = nullptr;
StringName tooltip_func;
Variant tooltip_ud;
bool next_operation_is_complex = false;
String search_text;
uint32_t search_flags = 0;
int search_result_line = 0;
int search_result_col = 0;
bool context_menu_enabled = true;
bool shortcut_keys_enabled = true;
bool virtual_keyboard_enabled = true;
int get_char_pos_for_line(int p_px, int p_line, int p_wrap_index = 0) const;
int get_column_x_offset_for_line(int p_char, int p_line) const;
Size2 get_minimum_size() const override;
int _get_control_height() const;
int _get_menu_action_accelerator(const String &p_action);
void _update_caches();
void _text_changed_emit();
void _push_current_op();
/* super internal api, undo/redo builds on it */
void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr);
void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
void _base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column);
String _base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const;
void _base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
int _get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column);
Dictionary _search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
PopupMenu *menu = nullptr;
PopupMenu *menu_dir = nullptr;
PopupMenu *menu_ctl = nullptr;
void _ensure_menu();
void _clear();
// Methods used in shortcuts
/* Input actions. */
void _swap_current_input_direction();
void _new_line(bool p_split_current = true, bool p_above = false);
void _move_caret_left(bool p_select, bool p_move_by_word = false);
@ -459,36 +525,8 @@ private:
void _move_caret_document_end(bool p_select);
protected:
bool highlight_matching_braces_enabled = false;
struct Cache {
Ref<Texture2D> tab_icon;
Ref<Texture2D> space_icon;
Ref<StyleBox> style_normal;
Ref<StyleBox> style_focus;
Ref<StyleBox> style_readonly;
Ref<Font> font;
int font_size = 16;
int outline_size = 0;
Color outline_color;
Color font_color;
Color font_readonly_color;
Color current_line_color;
Color brace_mismatch_color;
Color word_highlighted_color;
Color search_result_color;
Color search_result_border_color;
Color background_color;
int line_spacing = 1;
} cache;
virtual String get_tooltip(const Point2 &p_pos) const override;
void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr);
void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
virtual void _gui_input(const Ref<InputEvent> &p_gui_input);
void _notification(int p_what);
virtual void _gui_input(const Ref<InputEvent> &p_gui_input);
static void _bind_methods();
@ -497,6 +535,10 @@ protected:
void _get_property_list(List<PropertyInfo> *p_list) const;
/* Internal API for CodeEdit, pending public API. */
// brace matching
bool highlight_matching_braces_enabled = false;
Color brace_mismatch_color;
// Line hiding.
Color code_folding_color = Color(1, 1, 1);
Ref<Texture2D> folded_eol_icon;
@ -526,14 +568,78 @@ protected:
virtual void _paste();
public:
/* Text manipulation */
// User control
/* General overrides. */
virtual Size2 get_minimum_size() const override;
virtual bool is_text_field() const override;
virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
virtual String get_tooltip(const Point2 &p_pos) const override;
void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata);
/* Text */
// Text properties.
bool has_ime_text() const;
void set_editable(const bool p_editable);
bool is_editable() const;
void set_text_direction(TextDirection p_text_direction);
TextDirection get_text_direction() const;
void set_opentype_feature(const String &p_name, int p_value);
int get_opentype_feature(const String &p_name) const;
void clear_opentype_features();
void set_language(const String &p_language);
String get_language() const;
void set_structured_text_bidi_override(Control::StructuredTextParser p_parser);
Control::StructuredTextParser get_structured_text_bidi_override() const;
void set_structured_text_bidi_override_options(Array p_args);
Array get_structured_text_bidi_override_options() const;
void set_tab_size(const int p_size);
int get_tab_size() const;
// User controls
void set_overtype_mode_enabled(const bool p_enabled);
bool is_overtype_mode_enabled() const;
void set_context_menu_enabled(bool p_enable);
bool is_context_menu_enabled() const;
void set_shortcut_keys_enabled(bool p_enabled);
bool is_shortcut_keys_enabled() const;
void set_virtual_keyboard_enabled(bool p_enable);
bool is_virtual_keyboard_enabled() const;
// Text manipulation
void clear();
void set_text(const String &p_text);
String get_text() const;
int get_line_count() const;
void set_line(int p_line, const String &p_new_text);
String get_line(int p_line) const;
int get_line_width(int p_line, int p_wrap_index = -1) const;
int get_line_height() const;
int get_indent_level(int p_line) const;
int get_first_non_whitespace_column(int p_line) const;
void swap_lines(int p_from_line, int p_to_line);
void insert_line_at(int p_at, const String &p_text);
void insert_text_at_caret(const String &p_text);
void remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column);
int get_last_unhidden_line() const;
int get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const;
Point2i get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const;
// Overridable actions
void handle_unicode_input(const uint32_t p_unicode);
void backspace();
@ -542,6 +648,42 @@ public:
void copy();
void paste();
// Context menu.
PopupMenu *get_menu() const;
bool is_menu_visible() const;
void menu_option(int p_option);
/* Versioning */
void begin_complex_operation();
void end_complex_operation();
void undo();
void redo();
void clear_undo_history();
bool is_insert_text_operation() const;
void tag_saved_version();
uint32_t get_version() const;
uint32_t get_saved_version() const;
/* Search */
void set_search_text(const String &p_search_text);
void set_search_flags(uint32_t p_flags);
Point2i search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const;
/* Mouse */
Point2 get_local_mouse_pos() const;
String get_word_at_pos(const Vector2 &p_pos) const;
Point2i get_line_column_at_pos(const Point2i &p_pos) const;
int get_minimap_line_at_pos(const Point2i &p_pos) const;
bool is_dragging_cursor() const;
/* Caret */
void set_caret_type(CaretType p_type);
CaretType get_caret_type() const;
@ -569,6 +711,8 @@ public:
int get_caret_wrap_index() const;
String get_word_under_caret() const;
/* Selection. */
void set_selecting_enabled(const bool p_enabled);
bool is_selecting_enabled() const;
@ -609,7 +753,7 @@ public:
Vector<String> get_line_wrapped_text(int p_line) const;
/* Viewport. */
//Scrolling.
// Scrolling.
void set_smooth_scroll_enabled(const bool p_enable);
bool is_smooth_scroll_enabled() const;
@ -653,10 +797,6 @@ public:
int get_minimap_visible_lines() const;
/* Syntax Highlighting. */
Ref<SyntaxHighlighter> get_syntax_highlighter();
void set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter);
/* Gutters. */
void add_gutter(int p_at = -1);
void remove_gutter(int p_gutter);
@ -692,161 +832,40 @@ public:
void set_line_gutter_text(int p_line, int p_gutter, const String &p_text);
String get_line_gutter_text(int p_line, int p_gutter) const;
void set_line_gutter_icon(int p_line, int p_gutter, Ref<Texture2D> p_icon);
void set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon);
Ref<Texture2D> get_line_gutter_icon(int p_line, int p_gutter) const;
void set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color);
Color get_line_gutter_item_color(int p_line, int p_gutter);
Color get_line_gutter_item_color(int p_line, int p_gutter) const;
void set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable);
bool is_line_gutter_clickable(int p_line, int p_gutter) const;
// Line style
void set_line_background_color(int p_line, const Color &p_color);
Color get_line_background_color(int p_line);
Color get_line_background_color(int p_line) const;
enum MenuItems {
MENU_CUT,
MENU_COPY,
MENU_PASTE,
MENU_CLEAR,
MENU_SELECT_ALL,
MENU_UNDO,
MENU_REDO,
MENU_DIR_INHERITED,
MENU_DIR_AUTO,
MENU_DIR_LTR,
MENU_DIR_RTL,
MENU_DISPLAY_UCC,
MENU_INSERT_LRM,
MENU_INSERT_RLM,
MENU_INSERT_LRE,
MENU_INSERT_RLE,
MENU_INSERT_LRO,
MENU_INSERT_RLO,
MENU_INSERT_PDF,
MENU_INSERT_ALM,
MENU_INSERT_LRI,
MENU_INSERT_RLI,
MENU_INSERT_FSI,
MENU_INSERT_PDI,
MENU_INSERT_ZWJ,
MENU_INSERT_ZWNJ,
MENU_INSERT_WJ,
MENU_INSERT_SHY,
MENU_MAX
/* Syntax Highlighting. */
void set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter);
Ref<SyntaxHighlighter> get_syntax_highlighter() const;
};
enum SearchFlags {
SEARCH_MATCH_CASE = 1,
SEARCH_WHOLE_WORDS = 2,
SEARCH_BACKWARDS = 4
};
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 begin_complex_operation();
void end_complex_operation();
bool is_insert_text_operation();
void set_text_direction(TextDirection p_text_direction);
TextDirection get_text_direction() const;
void set_opentype_feature(const String &p_name, int p_value);
int get_opentype_feature(const String &p_name) const;
void clear_opentype_features();
void set_language(const String &p_language);
String get_language() const;
void set_draw_control_chars(bool p_draw_control_chars);
bool get_draw_control_chars() const;
void set_structured_text_bidi_override(Control::StructuredTextParser p_parser);
Control::StructuredTextParser get_structured_text_bidi_override() const;
void set_structured_text_bidi_override_options(Array p_args);
Array get_structured_text_bidi_override_options() const;
void set_text(String p_text);
void insert_text_at_caret(const String &p_text);
void insert_at(const String &p_text, int at);
int get_line_count() const;
int get_line_width(int p_line, int p_wrap_offset = -1) const;
int num_lines_from(int p_line_from, int visible_amount) const;
int num_lines_from_rows(int p_line_from, int p_wrap_index_from, int visible_amount, int &wrap_index) const;
int get_last_unhidden_line() const;
String get_text();
String get_line(int line) const;
bool has_ime_text() const;
void set_line(int line, String new_text);
int get_row_height() const;
int get_indent_level(int p_line) const;
int get_first_non_whitespace_column(int p_line) const;
void clear();
void swap_lines(int line1, int line2);
void set_search_text(const String &p_search_text);
void set_search_flags(uint32_t p_flags);
void set_current_search_result(int line, int col);
/* Visual. */
void set_highlight_current_line(bool p_enabled);
bool is_highlight_current_line_enabled() const;
void set_highlight_all_occurrences(const bool p_enabled);
bool is_highlight_all_occurrences_enabled() const;
String get_word_under_caret() const;
String get_word_at_pos(const Vector2 &p_pos) const;
void set_draw_control_chars(bool p_draw_control_chars);
bool get_draw_control_chars() const;
bool search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const;
void undo();
void redo();
void clear_undo_history();
void set_tab_size(const int p_size);
int get_tab_size() const;
void set_draw_tabs(bool p_draw);
bool is_drawing_tabs() const;
void set_draw_spaces(bool p_draw);
bool is_drawing_spaces() const;
uint32_t get_version() const;
uint32_t get_saved_version() const;
void tag_saved_version();
void menu_option(int p_option);
void set_highlight_current_line(bool p_enabled);
bool is_highlight_current_line_enabled() const;
void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata);
void set_context_menu_enabled(bool p_enable);
bool is_context_menu_enabled();
void set_shortcut_keys_enabled(bool p_enabled);
bool is_shortcut_keys_enabled() const;
void set_virtual_keyboard_enabled(bool p_enable);
bool is_virtual_keyboard_enabled() const;
bool is_menu_visible() const;
PopupMenu *get_menu() const;
virtual bool is_text_field() const override;
TextEdit();
~TextEdit();
};
VARIANT_ENUM_CAST(TextEdit::CaretType);