diff options
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r-- | scene/gui/text_edit.cpp | 1180 |
1 files changed, 851 insertions, 329 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index ff0c723141..5f9b913e8c 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -129,7 +129,7 @@ void TextEdit::Text::_update_line_cache(int p_line) const { int len = text[p_line].data.length(); const CharType *str = text[p_line].data.c_str(); - //update width + // Update width. for (int i = 0; i < len; i++) { w += get_char_width(str[i], str[i + 1], w); @@ -139,7 +139,7 @@ void TextEdit::Text::_update_line_cache(int p_line) const { text.write[p_line].wrap_amount_cache = -1; - //update regions + // Update regions. text.write[p_line].region_info.clear(); @@ -148,7 +148,7 @@ void TextEdit::Text::_update_line_cache(int p_line) const { if (!_is_symbol(str[i])) continue; if (str[i] == '\\') { - i++; //skip quoted anything + i++; // Skip quoted anything. continue; } @@ -275,7 +275,7 @@ void TextEdit::Text::clear() { } int TextEdit::Text::get_max_width(bool p_exclude_hidden) const { - //quite some work.. but should be fast enough. + // Quite some work, but should be fast enough. int max = 0; for (int i = 0; i < text.size(); i++) { @@ -323,7 +323,7 @@ int TextEdit::Text::get_char_width(CharType c, CharType next_c, int px) const { if (left == 0) w = tab_w; else - w = tab_w - px % tab_w; // is right... + w = tab_w - px % tab_w; // Is right. } else { w = font->get_char_size(c, next_c).width; @@ -367,23 +367,25 @@ void TextEdit::_update_scrollbars() { total_width += cache.fold_gutter_width; } + if (draw_minimap) { + total_width += cache.minimap_width; + } + bool use_hscroll = true; bool use_vscroll = true; + // Thanks yessopie for this clever bit of logic. if (total_rows <= visible_rows && total_width <= visible_width) { - //thanks yessopie for this clever bit of logic + use_hscroll = false; use_vscroll = false; - } else { if (total_rows > visible_rows && total_width <= visible_width) { - //thanks yessopie for this clever bit of logic use_hscroll = false; } if (total_rows <= visible_rows && total_width > visible_width) { - //thanks yessopie for this clever bit of logic use_vscroll = false; } } @@ -459,6 +461,7 @@ void TextEdit::_click_selection_held() { } void TextEdit::_update_selection_mode_pointer() { + dragging_selection = true; Point2 mp = get_local_mouse_position(); int row, col; @@ -474,6 +477,7 @@ void TextEdit::_update_selection_mode_pointer() { } void TextEdit::_update_selection_mode_word() { + dragging_selection = true; Point2 mp = get_local_mouse_position(); int row, col; @@ -481,7 +485,7 @@ void TextEdit::_update_selection_mode_word() { String line = text[row]; int beg = CLAMP(col, 0, line.length()); - // if its the first selection and on whitespace make sure we grab the word instead.. + // If its the first selection and on whitespace make sure we grab the word instead. if (!selection.active) { while (beg > 0 && line[beg] <= 32) { beg--; @@ -490,7 +494,7 @@ void TextEdit::_update_selection_mode_word() { int end = beg; bool symbol = beg < line.length() && _is_symbol(line[beg]); - // get the word end and begin points + // Get the word end and begin points. while (beg > 0 && line[beg - 1] > 32 && (symbol == _is_symbol(line[beg - 1]))) { beg--; } @@ -501,7 +505,7 @@ void TextEdit::_update_selection_mode_word() { end += 1; } - // initial selection + // Initial selection. if (!selection.active) { select(row, beg, row, end); selection.selecting_column = beg; @@ -530,6 +534,7 @@ void TextEdit::_update_selection_mode_word() { } void TextEdit::_update_selection_mode_line() { + dragging_selection = true; Point2 mp = get_local_mouse_position(); int row, col; @@ -537,11 +542,11 @@ void TextEdit::_update_selection_mode_line() { col = 0; if (row < selection.selecting_line) { - // cursor is above us + // Cursor is above us. cursor_set_line(row - 1, false); selection.selecting_column = text[selection.selecting_line].length(); } else { - // cursor is below us + // Cursor is below us. cursor_set_line(row + 1, false); selection.selecting_column = 0; col = text[row].length(); @@ -554,11 +559,58 @@ void TextEdit::_update_selection_mode_line() { click_select_held->start(); } +void TextEdit::_update_minimap_click() { + Point2 mp = get_local_mouse_position(); + + int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT); + if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.y > xmargin_end)) { + minimap_clicked = false; + return; + } + minimap_clicked = true; + dragging_minimap = true; + + int row; + _get_minimap_mouse_row(Point2i(mp.x, mp.y), row); + + if (row >= get_first_visible_line() && (row < get_last_visible_line() || row >= (text.size() - 1))) { + minimap_scroll_ratio = v_scroll->get_as_ratio(); + minimap_scroll_click_pos = mp.y; + can_drag_minimap = true; + return; + } + + int wi; + int first_line = row - num_lines_from_rows(row, 0, -get_visible_rows() / 2, wi) + 1; + double delta = get_scroll_pos_for_line(first_line, wi) - get_v_scroll(); + if (delta < 0) { + _scroll_up(-delta); + } else { + _scroll_down(delta); + } +} + +void TextEdit::_update_minimap_drag() { + + if (!can_drag_minimap) { + return; + } + + int control_height = _get_control_height(); + int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing); + if (control_height > scroll_height) { + control_height = scroll_height; + } + + Point2 mp = get_local_mouse_position(); + double diff = (mp.y - minimap_scroll_click_pos) / control_height; + v_scroll->set_as_ratio(minimap_scroll_ratio + diff); +} + void TextEdit::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { - _update_caches(); if (cursor_changed_dirty) MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit"); @@ -567,14 +619,19 @@ void TextEdit::_notification(int p_what) { _update_wrap_at(); } break; case NOTIFICATION_RESIZED: { - _update_scrollbars(); - call_deferred("_update_wrap_at"); + _update_wrap_at(); + } break; + case NOTIFICATION_VISIBILITY_CHANGED: { + if (is_visible()) { + call_deferred("_update_scrollbars"); + call_deferred("_update_wrap_at"); + } } break; case NOTIFICATION_THEME_CHANGED: { - _update_caches(); _update_wrap_at(); + syntax_highlighting_cache.clear(); } break; case MainLoop::NOTIFICATION_WM_FOCUS_IN: { window_has_focus = true; @@ -590,24 +647,26 @@ void TextEdit::_notification(int p_what) { if (scrolling && get_v_scroll() != target_v_scroll) { double target_y = target_v_scroll - get_v_scroll(); double dist = sqrt(target_y * target_y); - double vel = ((target_y / dist) * v_scroll_speed) * get_physics_process_delta_time(); + // To ensure minimap is responsive override the speed setting. + double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_physics_process_delta_time(); if (Math::abs(vel) >= dist) { set_v_scroll(target_v_scroll); scrolling = false; + minimap_clicked = false; set_physics_process_internal(false); } else { set_v_scroll(get_v_scroll() + vel); } } else { scrolling = false; + minimap_clicked = false; set_physics_process_internal(false); } } break; case NOTIFICATION_DRAW: { - if (first_draw) { - //size may not be the final one, so attempts to ensure cursor was visible may have failed + // Size may not be the final one, so attempts to ensure cursor was visible may have failed. adjust_viewport_to_cursor(); first_draw = false; } @@ -637,6 +696,11 @@ void TextEdit::_notification(int p_what) { cache.fold_gutter_width = 0; } + cache.minimap_width = 0; + if (draw_minimap) { + cache.minimap_width = minimap_width; + } + int line_number_char_count = 0; { @@ -660,8 +724,9 @@ void TextEdit::_notification(int p_what) { RID ci = get_canvas_item(); VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true); int xmargin_beg = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width; - int xmargin_end = size.width - cache.style_normal->get_margin(MARGIN_RIGHT); - //let's do it easy for now: + + int xmargin_end = size.width - cache.style_normal->get_margin(MARGIN_RIGHT) - cache.minimap_width; + // Let's do it easy for now. cache.style_normal->draw(ci, Rect2(Point2(), size)); if (readonly) { cache.style_readonly->draw(ci, Rect2(Point2(), size)); @@ -701,7 +766,7 @@ void TextEdit::_notification(int p_what) { if (brace_matching_enabled && cursor.line >= 0 && cursor.line < text.size() && cursor.column >= 0) { if (cursor.column < text[cursor.line].length()) { - //check for open + // Check for open. CharType c = text[cursor.line][cursor.column]; CharType closec = 0; @@ -723,7 +788,7 @@ void TextEdit::_notification(int p_what) { for (int j = from; j < text[i].length(); j++) { CharType cc = text[i][j]; - //ignore any brackets inside a string + // Ignore any brackets inside a string. if (cc == '"' || cc == '\'') { CharType quotation = cc; do { @@ -732,7 +797,7 @@ void TextEdit::_notification(int p_what) { break; } cc = text[i][j]; - //skip over escaped quotation marks inside strings + // Skip over escaped quotation marks inside strings. if (cc == '\\') { bool escaped = true; while (j + 1 < text[i].length() && text[i][j + 1] == '\\') { @@ -789,7 +854,7 @@ void TextEdit::_notification(int p_what) { for (int j = from; j >= 0; j--) { CharType cc = text[i][j]; - //ignore any brackets inside a string + // Ignore any brackets inside a string. if (cc == '"' || cc == '\'') { CharType quotation = cc; do { @@ -798,7 +863,7 @@ void TextEdit::_notification(int p_what) { break; } cc = text[i][j]; - //skip over escaped quotation marks inside strings + // Skip over escaped quotation marks inside strings. if (cc == quotation) { bool escaped = false; while (j - 1 >= 0 && text[i][j - 1] == '\\') { @@ -837,10 +902,10 @@ void TextEdit::_notification(int p_what) { Point2 cursor_pos; int cursor_insert_offset_y = 0; - // get the highlighted words + // Get the highlighted words. String highlighted_text = get_selection_text(); - // check if highlighted words contains only whitespaces (tabs or spaces) + // Check if highlighted words contains only whitespaces (tabs or spaces). bool only_whitespaces_highlighted = highlighted_text.strip_edges() == String(); String line_num_padding = line_numbers_zero_padded ? "0" : " "; @@ -849,9 +914,156 @@ void TextEdit::_notification(int p_what) { FontDrawer drawer(cache.font, Color(1, 1, 1)); - int line = get_first_visible_line() - 1; + int first_visible_line = get_first_visible_line() - 1; int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0); - draw_amount += times_line_wraps(line + 1); + draw_amount += times_line_wraps(first_visible_line + 1); + + // minimap + if (draw_minimap) { + int minimap_visible_lines = _get_minimap_visible_rows(); + int minimap_line_height = (minimap_char_size.y + minimap_line_spacing); + int minimap_tab_size = minimap_char_size.x * indent_size; + + // calculate viewport size and y offset + int viewport_height = (draw_amount - 1) * minimap_line_height; + int control_height = _get_control_height() - viewport_height; + int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount)); + + // calculate the first line. + int num_lines_before = round((viewport_offset_y) / minimap_line_height); + int wi; + int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line; + if (minimap_line >= 0) { + minimap_line -= num_lines_from_rows(first_visible_line, 0, -num_lines_before, wi); + minimap_line -= (smooth_scroll_enabled ? 1 : 0); + } + int minimap_draw_amount = minimap_visible_lines + times_line_wraps(minimap_line + 1); + + // draw the minimap + Color viewport_color = (cache.background_color.get_v() < 0.5) ? Color(1, 1, 1, 0.1) : Color(0, 0, 0, 0.1); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, cache.minimap_width, viewport_height), viewport_color); + for (int i = 0; i < minimap_draw_amount; i++) { + + minimap_line++; + + if (minimap_line < 0 || minimap_line >= (int)text.size()) { + break; + } + + while (is_line_hidden(minimap_line)) { + minimap_line++; + if (minimap_line < 0 || minimap_line >= (int)text.size()) { + break; + } + } + + Map<int, HighlighterInfo> color_map; + if (syntax_coloring) { + color_map = _get_line_syntax_highlighting(minimap_line); + } + + Color current_color = cache.font_color; + if (readonly) { + current_color = cache.font_color_readonly; + } + + Vector<String> wrap_rows = get_wrap_rows_text(minimap_line); + int line_wrap_amount = times_line_wraps(minimap_line); + int last_wrap_column = 0; + + for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) { + if (line_wrap_index != 0) { + i++; + if (i >= minimap_draw_amount) + break; + } + + const String &str = wrap_rows[line_wrap_index]; + int indent_px = line_wrap_index != 0 ? get_indent_level(minimap_line) : 0; + if (indent_px >= wrap_at) { + indent_px = 0; + } + indent_px = minimap_char_size.x * indent_px; + + if (line_wrap_index > 0) { + last_wrap_column += wrap_rows[line_wrap_index - 1].length(); + } + + if (minimap_line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) { + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), i * 3, cache.minimap_width, 2), cache.current_line_color); + } + + Color previous_color; + int characters = 0; + int tabs = 0; + for (int j = 0; j < str.length(); j++) { + if (syntax_coloring) { + if (color_map.has(last_wrap_column + j)) { + current_color = color_map[last_wrap_column + j].color; + if (readonly) { + current_color.a = cache.font_color_readonly.a; + } + } + color = current_color; + } + + if (j == 0) { + previous_color = color; + } + + int xpos = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * j)) + tabs; + bool out_of_bounds = (xpos >= xmargin_end + cache.minimap_width); + + bool is_whitespace = _is_whitespace(str[j]); + if (!is_whitespace) { + characters++; + + if (j < str.length() - 1 && color == previous_color && !out_of_bounds) { + continue; + } + + // If we've changed colour we are at the start of a new section, therefore we need to go back to the end + // of the previous section to draw it, we'll also add the character back on. + if (color != previous_color) { + characters--; + j--; + + if (str[j] == '\t') { + tabs -= minimap_tab_size; + } + } + } + + if (characters > 0) { + previous_color.a *= 0.6; + // take one for zero indexing, and if we hit whitespace / the end of a word. + int chars = MAX(0, (j - (characters - 1)) - (is_whitespace ? 1 : 0)) + 1; + int char_x_ofs = indent_px + ((xmargin_end + minimap_char_size.x) + (minimap_char_size.x * chars)) + tabs; + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(char_x_ofs, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), previous_color); + } + + if (out_of_bounds) { + break; + } + + // re-adjust if we went backwards. + if (color != previous_color && !is_whitespace) { + characters++; + } + + if (str[j] == '\t') { + tabs += minimap_tab_size; + } + + previous_color = color; + characters = 0; + } + } + } + } + + // draw main text + int line = first_visible_line; for (int i = 0; i < draw_amount; i++) { line++; @@ -875,7 +1087,7 @@ void TextEdit::_notification(int p_what) { if (syntax_coloring) { color_map = _get_line_syntax_highlighting(line); } - // ensure we at least use the font color + // Ensure we at least use the font color. Color current_color = readonly ? cache.font_color_readonly : cache.font_color; bool underlined = false; @@ -916,7 +1128,7 @@ void TextEdit::_notification(int p_what) { if (smooth_scroll_enabled) ofs_y += (-get_v_scroll_offset()) * get_row_height(); - // check if line contains highlighted word + // Check if line contains highlighted word. int highlighted_text_col = -1; int search_text_col = -1; int highlighted_word_col = -1; @@ -938,25 +1150,25 @@ void TextEdit::_notification(int p_what) { } if (str.length() == 0) { - // draw line background if empty as we won't loop at at all + // Draw line background if empty as we won't loop at at all. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs_x, ofs_y, xmargin_end, get_row_height()), cache.current_line_color); } - // give visual indication of empty selected line + // Give visual indication of empty selected line. if (selection.active && line >= selection.from_line && line <= selection.to_line && char_margin >= xmargin_beg) { int char_w = cache.font->get_char_size(' ').width; VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, char_w, get_row_height()), cache.selection_color); } } else { - // if it has text, then draw current line marker in the margin, as line number etc will draw over it, draw the rest of line marker later. + // If it has text, then draw current line marker in the margin, as line number etc will draw over it, draw the rest of line marker later. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_beg, get_row_height()), cache.current_line_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_beg + ofs_x, get_row_height()), cache.current_line_color); } } if (line_wrap_index == 0) { - // only do these if we are on the first wrapped part of a line + // Only do these if we are on the first wrapped part of a line. if (text.is_breakpoint(line) && !draw_breakpoint_gutter) { #ifdef TOOLS_ENABLED @@ -966,7 +1178,7 @@ void TextEdit::_notification(int p_what) { #endif } - // draw bookmark marker + // Draw bookmark marker. if (text.is_bookmark(line)) { if (draw_bookmark_gutter) { int vertical_gap = (get_row_height() * 40) / 100; @@ -976,26 +1188,26 @@ void TextEdit::_notification(int p_what) { } } - // draw breakpoint marker + // Draw breakpoint marker. if (text.is_breakpoint(line)) { if (draw_breakpoint_gutter) { int vertical_gap = (get_row_height() * 40) / 100; int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100; int marker_height = get_row_height() - (vertical_gap * 2); int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2); - // no transparency on marker + // No transparency on marker. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap, marker_width, marker_height), Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b)); } } - // draw info icons + // Draw info icons. if (draw_info_gutter && text.has_info_icon(line)) { int vertical_gap = (get_row_height() * 40) / 100; int horizontal_gap = (cache.info_gutter_width * 30) / 100; int gutter_left = cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width; Ref<Texture> info_icon = text.get_info_icon(line); - // ensure the icon fits the gutter size + // Ensure the icon fits the gutter size. Size2i icon_size = info_icon->get_size(); if (icon_size.width > cache.info_gutter_width - horizontal_gap) { icon_size.width = cache.info_gutter_width - horizontal_gap; @@ -1013,7 +1225,7 @@ void TextEdit::_notification(int p_what) { draw_texture_rect(info_icon, Rect2(icon_pos, icon_size)); } - // draw execution marker + // Draw execution marker. if (executing_line == line) { if (draw_breakpoint_gutter) { int icon_extra_size = 4; @@ -1031,7 +1243,7 @@ void TextEdit::_notification(int p_what) { } } - // draw fold markers + // Draw fold markers. if (draw_fold_gutter) { int horizontal_gap = (cache.fold_gutter_width * 30) / 100; int gutter_left = cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width + cache.line_number_w + cache.info_gutter_width; @@ -1046,7 +1258,7 @@ void TextEdit::_notification(int p_what) { } } - // draw line numbers + // Draw line numbers. if (cache.line_number_w) { int yofs = ofs_y + (get_row_height() - cache.font->get_height()) / 2; String fc = String::num(line + 1); @@ -1058,32 +1270,36 @@ void TextEdit::_notification(int p_what) { } } - //loop through characters in one line - for (int j = 0; j < str.length(); j++) { + // Loop through characters in one line. + int j = 0; + for (; j < str.length(); j++) { if (syntax_coloring) { if (color_map.has(last_wrap_column + j)) { - current_color = readonly ? cache.font_color_readonly : color_map[last_wrap_column + j].color; + current_color = color_map[last_wrap_column + j].color; + if (readonly && current_color.a > cache.font_color_readonly.a) { + current_color.a = cache.font_color_readonly.a; + } } color = current_color; } int char_w; - //handle tabulator + // Handle tabulator. char_w = text.get_char_width(str[j], str[j + 1], char_ofs); if ((char_ofs + char_margin) < xmargin_beg) { char_ofs += char_w; - // line highlighting handle horizontal clipping + // Line highlighting handle horizontal clipping. if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) { if (j == str.length() - 1) { - // end of line when last char is skipped + // End of line when last char is skipped. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color); } else if ((char_ofs + char_margin) > xmargin_beg) { - // char next to margin is skipped + // Char next to margin is skipped. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg + ofs_x, ofs_y, (char_ofs + char_margin) - (xmargin_beg + ofs_x), get_row_height()), cache.current_line_color); } } @@ -1097,7 +1313,7 @@ void TextEdit::_notification(int p_what) { bool in_search_result = false; if (search_text_col != -1) { - // if we are at the end check for new search result on same line + // If we are at the end check for new search result on same line. if (j >= search_text_col + search_text.length()) search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j); @@ -1108,19 +1324,19 @@ void TextEdit::_notification(int p_what) { } } - //current line highlighting + // Current line highlighting. bool in_selection = (selection.active && line >= selection.from_line && line <= selection.to_line && (line > selection.from_line || last_wrap_column + j >= selection.from_column) && (line < selection.to_line || last_wrap_column + j < selection.to_column)); if (line == cursor.line && cursor_wrap_index == line_wrap_index && highlight_current_line) { - // draw the wrap indent offset highlight + // Draw the wrap indent offset highlight. if (line_wrap_index != 0 && j == 0) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin - indent_px, ofs_y, indent_px, get_row_height()), cache.current_line_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + ofs_x - indent_px, ofs_y, indent_px, get_row_height()), cache.current_line_color); } - // if its the last char draw to end of the line + // If its the last char draw to end of the line. if (j == str.length() - 1) { - VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + char_w, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color); + VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(char_ofs + char_margin + char_w + ofs_x, ofs_y, xmargin_end - (char_ofs + char_margin + char_w), get_row_height()), cache.current_line_color); } - // actual text + // Actual text. if (!in_selection) { VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + ofs_x, ofs_y), Size2i(char_w, get_row_height())), cache.current_line_color); } @@ -1145,14 +1361,14 @@ void TextEdit::_notification(int p_what) { if (highlight_all_occurrences && !only_whitespaces_highlighted) { if (highlighted_text_col != -1) { - // if we are at the end check for new word on same line + // If we are at the end check for new word on same line. if (j > highlighted_text_col + highlighted_text.length()) { highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j); } bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col + highlighted_text.length()); - // if this is the original highlighted text we don't want to highlight it again + // If this is the original highlighted text we don't want to highlight it again. if (cursor.line == line && cursor_wrap_index == line_wrap_index && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col + highlighted_text.length())) { in_highlighted_word = false; } @@ -1286,7 +1502,7 @@ void TextEdit::_notification(int p_what) { } } - if (cursor.column == last_wrap_column + str.length() && cursor.line == line && cursor_wrap_index == line_wrap_index && (char_ofs + char_margin) >= xmargin_beg) { + if (cursor.column == (last_wrap_column + j) && cursor.line == line && cursor_wrap_index == line_wrap_index && (char_ofs + char_margin) >= xmargin_beg) { cursor_pos = Point2i(char_ofs + char_margin + ofs_x, ofs_y); cursor_pos.y += (get_row_height() - cache.font->get_height()) / 2; @@ -1349,7 +1565,7 @@ void TextEdit::_notification(int p_what) { bool completion_below = false; if (completion_active) { - // code completion box + // Code completion box. Ref<StyleBox> csb = get_stylebox("completion"); int maxlines = get_constant("completion_lines"); int cmax_width = get_constant("completion_max_width") * cache.font->get_char_size('x').x; @@ -1371,7 +1587,7 @@ void TextEdit::_notification(int p_what) { w = cmax_width; } - // Add space for completion icons + // Add space for completion icons. const int icon_hsep = get_constant("hseparation", "ItemList"); Size2 icon_area_size(get_row_height(), get_row_height()); w += icon_area_size.width + icon_hsep; @@ -1403,7 +1619,7 @@ void TextEdit::_notification(int p_what) { } int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines); VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.position.x, completion_rect.position.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color); - draw_rect(Rect2(completion_rect.position + Vector2(icon_area_size.x + icon_hsep, 0), Size2(nofs, completion_rect.size.height)), cache.completion_existing_color); + draw_rect(Rect2(completion_rect.position + Vector2(icon_area_size.x + icon_hsep, 0), Size2(MIN(nofs, completion_rect.size.width - (icon_area_size.x + icon_hsep)), completion_rect.size.height)), cache.completion_existing_color); for (int i = 0; i < lines; i++) { @@ -1418,7 +1634,7 @@ void TextEdit::_notification(int p_what) { int yofs = (get_row_height() - cache.font->get_height()) / 2; Point2 title_pos(completion_rect.position.x, completion_rect.position.y + i * get_row_height() + cache.font->get_ascent() + yofs); - //draw completion icon if it is valid + // Draw completion icon if it is valid. Ref<Texture> icon = completion_options[l].icon; Rect2 icon_area(completion_rect.position.x, completion_rect.position.y + i * get_row_height(), icon_area_size.width, icon_area_size.height); if (icon.is_valid()) { @@ -1430,11 +1646,11 @@ void TextEdit::_notification(int p_what) { } title_pos.x = icon_area.position.x + icon_area.size.width + icon_hsep; - draw_string(cache.font, title_pos, completion_options[l].display, text_color, completion_rect.size.width); + draw_string(cache.font, title_pos, completion_options[l].display, text_color, completion_rect.size.width - (icon_area_size.x + icon_hsep)); } if (scrollw) { - //draw a small scroll rectangle to show a position in the options + // Draw a small scroll rectangle to show a position in the options. float r = maxlines / (float)completion_options.size(); float o = line_from / (float)completion_options.size(); draw_rect(Rect2(completion_rect.position.x + completion_rect.size.width, completion_rect.position.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc); @@ -1443,7 +1659,7 @@ void TextEdit::_notification(int p_what) { completion_line_ofs = line_from; } - // check to see if the hint should be drawn + // Check to see if the hint should be drawn. bool show_hint = false; if (completion_hint != "") { if (completion_active) { @@ -1520,11 +1736,12 @@ void TextEdit::_notification(int p_what) { OS::get_singleton()->set_ime_active(true); OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos + Point2(0, get_row_height())); } - } break; case NOTIFICATION_FOCUS_ENTER: { - if (!caret_blink_enabled) { + if (caret_blink_enabled) { + caret_blink_timer->start(); + } else { draw_caret = true; } @@ -1537,6 +1754,10 @@ void TextEdit::_notification(int p_what) { } break; case NOTIFICATION_FOCUS_EXIT: { + if (caret_blink_enabled) { + caret_blink_timer->stop(); + } + OS::get_singleton()->set_ime_position(Point2()); OS::get_singleton()->set_ime_active(false); ime_text = ""; @@ -1662,9 +1883,9 @@ void TextEdit::backspace_at_cursor() { _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) { _consume_backspace_for_pair_symbol(prev_line, prev_column); } else { - // handle space indentation + // Handle space indentation. if (cursor.column != 0 && indent_using_spaces) { - // check if there are no other chars before cursor, just indentation + // Check if there are no other chars before cursor, just indentation. bool unindent = true; int i = 0; while (i < cursor.column && i < text[cursor.line].length()) { @@ -1675,10 +1896,9 @@ void TextEdit::backspace_at_cursor() { i++; } - // then we can remove all spaces as a single character. + // Then we can remove all spaces as a single character. if (unindent) { - // we want to remove spaces up to closest indent - // or whole indent if cursor is pointing at it + // We want to remove spaces up to closest indent, or whole indent if cursor is pointing at it. int spaces_to_delete = _calculate_spaces_till_next_left_indent(cursor.column); prev_column = cursor.column - spaces_to_delete; _remove_text(cursor.line, prev_column, cursor.line, cursor.column); @@ -1699,8 +1919,8 @@ void TextEdit::indent_right() { int start_line; int end_line; - // this value informs us by how much we changed selection position by indenting right - // default is 1 for tab indentation + // This value informs us by how much we changed selection position by indenting right. + // Default is 1 for tab indentation. int selection_offset = 1; begin_complex_operation(); @@ -1712,7 +1932,7 @@ void TextEdit::indent_right() { end_line = start_line; } - // ignore if the cursor is not past the first column + // Ignore if the cursor is not past the first column. if (is_selection_active() && get_selection_to_column() == 0) { end_line--; } @@ -1720,10 +1940,10 @@ void TextEdit::indent_right() { for (int i = start_line; i <= end_line; i++) { String line_text = get_line(i); if (indent_using_spaces) { - // we don't really care where selection is - we just need to know indentation level at the beginning of the line + // We don't really care where selection is - we just need to know indentation level at the beginning of the line. int left = _find_first_non_whitespace_column_of_line(line_text); int spaces_to_add = _calculate_spaces_till_next_right_indent(left); - // since we will add this much spaces we want move whole selection and cursor by this much + // Since we will add this much spaces we want move whole selection and cursor by this much. selection_offset = spaces_to_add; for (int j = 0; j < spaces_to_add; j++) line_text = ' ' + line_text; @@ -1733,7 +1953,7 @@ void TextEdit::indent_right() { set_line(i, line_text); } - // fix selection and cursor being off after shifting selection right + // Fix selection and cursor being off after shifting selection right. if (is_selection_active()) { select(selection.from_line, selection.from_column + selection_offset, selection.to_line, selection.to_column + selection_offset); } @@ -1747,10 +1967,9 @@ void TextEdit::indent_left() { int start_line; int end_line; - // moving cursor and selection after unindenting can get tricky - // because changing content of line can move cursor and selection on it's own (if new line ends before previous position of either) - // therefore we just remember initial values - // and at the end of the operation offset them by number of removed characters + // Moving cursor and selection after unindenting can get tricky because + // changing content of line can move cursor and selection on it's own (if new line ends before previous position of either), + // therefore we just remember initial values and at the end of the operation offset them by number of removed characters. int removed_characters = 0; int initial_selection_end_column = selection.to_column; int initial_cursor_column = cursor.column; @@ -1765,7 +1984,7 @@ void TextEdit::indent_left() { end_line = start_line; } - // ignore if the cursor is not past the first column + // Ignore if the cursor is not past the first column. if (is_selection_active() && get_selection_to_column() == 0) { end_line--; } @@ -1779,12 +1998,12 @@ void TextEdit::indent_left() { set_line(i, line_text); removed_characters = 1; } else if (line_text.begins_with(" ")) { - // when unindenting we aim to remove spaces before line that has selection no matter what is selected + // When unindenting we aim to remove spaces before line that has selection no matter what is selected, // so we start of by finding first non whitespace character of line int left = _find_first_non_whitespace_column_of_line(line_text); - // here we remove only enough spaces to align text to nearest full multiple of indentation_size - // in case where selection begins at the start of indentation_size multiple we remove whole indentation level + // Here we remove only enough spaces to align text to nearest full multiple of indentation_size. + // In case where selection begins at the start of indentation_size multiple we remove whole indentation level. int spaces_to_remove = _calculate_spaces_till_next_left_indent(left); line_text = line_text.substr(spaces_to_remove, line_text.length()); @@ -1793,7 +2012,7 @@ void TextEdit::indent_left() { } } - // fix selection and cursor being off by one on the last line + // Fix selection and cursor being off by one on the last line. if (is_selection_active() && last_line_text != get_line(end_line)) { select(selection.from_line, selection.from_column - removed_characters, selection.to_line, initial_selection_end_column - removed_characters); @@ -1834,7 +2053,7 @@ void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) co } if (row < 0) - row = 0; //todo + row = 0; // TODO. int col = 0; @@ -1848,7 +2067,7 @@ void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) co colx += cursor.x_ofs; col = get_char_pos_for_line(colx, row, wrap_index); if (is_wrap_enabled() && wrap_index < times_line_wraps(row)) { - // move back one if we are at the end of the row + // Move back one if we are at the end of the row. Vector<String> rows2 = get_wrap_rows_text(row); int row_end_col = 0; for (int i = 0; i < wrap_index + 1; i++) { @@ -1863,6 +2082,99 @@ void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) co r_col = col; } +Vector2i TextEdit::_get_cursor_pixel_pos() { + adjust_viewport_to_cursor(); + int row = (cursor.line - get_first_visible_line() - cursor.wrap_ofs); + // Correct for hidden and wrapped lines + for (int i = get_first_visible_line(); i < cursor.line; i++) { + if (is_line_hidden(i)) { + row -= 1; + continue; + } + row += times_line_wraps(i); + } + // Row might be wrapped. Adjust row and r_column + Vector<String> rows2 = get_wrap_rows_text(cursor.line); + while (rows2.size() > 1) { + if (cursor.column >= rows2[0].length()) { + cursor.column -= rows2[0].length(); + rows2.remove(0); + row++; + } else { + break; + } + } + + // Calculate final pixel position + int y = (row - get_v_scroll_offset() + 1 /*Bottom of line*/) * get_row_height(); + int x = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width - cursor.x_ofs; + int ix = 0; + while (ix < rows2[0].size() && ix < cursor.column) { + if (cache.font != NULL) { + x += cache.font->get_char_size(rows2[0].get(ix)).width; + } + ix++; + } + x += get_indent_level(cursor.line) * cache.font->get_char_size(' ').width; + + return Vector2i(x, y); +} + +void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const { + + float rows = p_mouse.y; + rows -= cache.style_normal->get_margin(MARGIN_TOP); + rows /= (minimap_char_size.y + minimap_line_spacing); + rows += get_v_scroll_offset(); + + // calculate visible lines + int minimap_visible_lines = _get_minimap_visible_rows(); + int visible_rows = get_visible_rows() + 1; + int first_visible_line = get_first_visible_line() - 1; + int draw_amount = visible_rows + (smooth_scroll_enabled ? 1 : 0); + draw_amount += times_line_wraps(first_visible_line + 1); + int minimap_line_height = (minimap_char_size.y + minimap_line_spacing); + + // calculate viewport size and y offset + int viewport_height = (draw_amount - 1) * minimap_line_height; + int control_height = _get_control_height() - viewport_height; + int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount)); + + // calculate the first line. + int num_lines_before = round((viewport_offset_y) / minimap_line_height); + int wi; + int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_visible_line; + if (first_visible_line > 0 && minimap_line >= 0) { + minimap_line -= num_lines_from_rows(first_visible_line, 0, -num_lines_before, wi); + minimap_line -= (smooth_scroll_enabled ? 1 : 0); + } else { + minimap_line = 0; + } + + int row = minimap_line + Math::floor(rows); + int wrap_index = 0; + + if (is_wrap_enabled() || is_hiding_enabled()) { + + int f_ofs = num_lines_from_rows(minimap_line, cursor.wrap_ofs, rows + (1 * SGN(rows)), wrap_index) - 1; + if (rows < 0) { + row = minimap_line - f_ofs; + } else { + row = minimap_line + f_ofs; + } + } + + if (row < 0) { + row = 0; + } + + if (row >= text.size()) { + row = text.size() - 1; + } + + r_row = row; +} + void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { double prev_v_scroll = v_scroll->get_value(); @@ -1936,13 +2248,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { int row, col; _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col); - if (mb->get_command() && highlighted_word != String()) { - - emit_signal("symbol_lookup", highlighted_word, row, col); - return; - } - - // toggle breakpoint on gutter click + // Toggle breakpoint on gutter click. if (draw_breakpoint_gutter) { int gutter = cache.style_normal->get_margin(MARGIN_LEFT); if (mb->get_position().x > gutter - 6 && mb->get_position().x <= gutter + cache.breakpoint_gutter_width - 3) { @@ -1952,7 +2258,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } - // emit info clicked + // Emit info clicked. if (draw_info_gutter && text.has_info_icon(row)) { int left_margin = cache.style_normal->get_margin(MARGIN_LEFT); int gutter_left = left_margin + cache.breakpoint_gutter_width; @@ -1962,7 +2268,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } - // toggle fold on gutter click if can + // Toggle fold on gutter click if can. if (draw_fold_gutter) { int left_margin = cache.style_normal->get_margin(MARGIN_LEFT); @@ -1977,7 +2283,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } - // unfold on folded icon click + // Unfold on folded icon click. if (is_folded(row)) { int line_width = text.get_line_width(row); line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.info_gutter_width + cache.fold_gutter_width - cursor.x_ofs; @@ -1987,6 +2293,14 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } + // minimap + if (draw_minimap) { + _update_minimap_click(); + if (dragging_minimap) { + return; + } + } + int prev_col = cursor.column; int prev_line = cursor.line; @@ -2044,9 +2358,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } else { - //if sel active and dblick last time < something - - //else selection.active = false; selection.selecting_mode = Selection::MODE_POINTER; selection.selecting_line = row; @@ -2054,14 +2365,14 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } if (!mb->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) { - //tripleclick select line + + // Triple-click select line. selection.selecting_mode = Selection::MODE_LINE; _update_selection_mode_line(); last_dblclk = 0; - } else if (mb->is_doubleclick() && text[cursor.line].length()) { - //doubleclick select word + // Double-click select word. selection.selecting_mode = Selection::MODE_WORD; _update_selection_mode_word(); last_dblclk = OS::get_singleton()->get_ticks_msec(); @@ -2086,7 +2397,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { int to_column = get_selection_to_column(); if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) { - // Right click is outside the selected text + // Right click is outside the selected text. deselect(); } } @@ -2104,10 +2415,22 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } else { - if (mb->get_button_index() == BUTTON_LEFT) + if (mb->get_button_index() == BUTTON_LEFT) { + if (mb->get_command() && highlighted_word != String()) { + int row, col; + _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col); + + emit_signal("symbol_lookup", highlighted_word, row, col); + return; + } + + dragging_minimap = false; + dragging_selection = false; + can_drag_minimap = false; click_select_held->stop(); + } - // notify to show soft keyboard + // Notify to show soft keyboard. notification(NOTIFICATION_FOCUS_ENTER); } } @@ -2123,7 +2446,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100); if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) - accept_event(); //accept event if scroll changed + accept_event(); // Accept event if scroll changed. return; } @@ -2133,7 +2456,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (mm.is_valid()) { if (select_identifiers_enabled) { - if (mm->get_command() && mm->get_button_mask() == 0) { + if (!dragging_minimap && !dragging_selection && mm->get_command() && mm->get_button_mask() == 0) { String new_word = get_word_at_pos(mm->get_position()); if (new_word != highlighted_word) { @@ -2148,34 +2471,40 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } - if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { //ignore if dragging + if (mm->get_button_mask() & BUTTON_MASK_LEFT && get_viewport()->gui_get_drag_data() == Variant()) { // Ignore if dragging. _reset_caret_blink_timer(); - switch (selection.selecting_mode) { - case Selection::MODE_POINTER: { - _update_selection_mode_pointer(); - } break; - case Selection::MODE_WORD: { - _update_selection_mode_word(); - } break; - case Selection::MODE_LINE: { - _update_selection_mode_line(); - } break; - default: { - break; + if (draw_minimap && !dragging_selection) { + _update_minimap_drag(); + } + + if (!dragging_minimap) { + switch (selection.selecting_mode) { + case Selection::MODE_POINTER: { + _update_selection_mode_pointer(); + } break; + case Selection::MODE_WORD: { + _update_selection_mode_word(); + } break; + case Selection::MODE_LINE: { + _update_selection_mode_line(); + } break; + default: { + break; + } } } } } if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) - accept_event(); //accept event if scroll changed + accept_event(); // Accept event if scroll changed. Ref<InputEventKey> k = p_gui_input; if (k.is_valid()) { - k = k->duplicate(); //it will be modified later on + k = k->duplicate(); // It will be modified later on. #ifdef OSX_ENABLED if (k->get_scancode() == KEY_META) { @@ -2185,7 +2514,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { #endif if (select_identifiers_enabled) { - if (k->is_pressed()) { + if (k->is_pressed() && !dragging_minimap && !dragging_selection) { highlighted_word = get_word_at_pos(get_local_mouse_position()); update(); @@ -2311,11 +2640,11 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _consume_pair_symbol(chr[0]); } else { - // remove the old character if in insert mode + // Remove the old character if in insert mode. if (insert_mode) { begin_complex_operation(); - // make sure we don't try and remove empty space + // Make sure we don't try and remove empty space. if (cursor.column < get_line(cursor.line).length()) { _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); } @@ -2337,9 +2666,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _cancel_completion(); } - /* TEST CONTROL FIRST!! */ + /* TEST CONTROL FIRST! */ - // some remaps for duplicate functions.. + // Some remaps for duplicate functions. if (k->get_command() && !k->get_shift() && !k->get_alt() && !k->get_metakey() && k->get_scancode() == KEY_INSERT) { k->set_scancode(KEY_C); @@ -2383,10 +2712,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _reset_caret_blink_timer(); - // save here for insert mode, just in case it is cleared in the following section + // Save here for insert mode, just in case it is cleared in the following section. bool had_selection = selection.active; - // stuff to do when selection is active.. + // Stuff to do when selection is active. if (!readonly && selection.active) { bool clear = false; @@ -2406,7 +2735,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } break; case KEY_X: case KEY_C: - //special keys often used with control, wait... + // Special keys often used with control, wait. clear = (!k->get_command() || k->get_shift() || k->get_alt()); break; case KEY_DELETE: @@ -2431,7 +2760,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { case KEY_PAGEDOWN: case KEY_HOME: case KEY_END: - // ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys) + // Ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys). if (k->get_command() || k->get_shift() || k->get_alt()) break; unselect = true; @@ -2469,7 +2798,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { bool scancode_handled = true; - // special scancode test... + // Special scancode test. switch (k->get_scancode()) { @@ -2481,7 +2810,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { String ins = "\n"; - //keep indentation + // Keep indentation. int space_count = 0; for (int i = 0; i < cursor.column; i++) { if (text[cursor.line][i] == '\t') { @@ -2512,10 +2841,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { bool brace_indent = false; - // no need to indent if we are going upwards. + // No need to indent if we are going upwards. if (auto_indent && !(k->get_command() && k->get_shift())) { - // indent once again if previous line will end with ':' or '{' and the line is not a comment - // (i.e. colon/brace precedes current cursor position) + // Indent once again if previous line will end with ':' or '{' and the line is not a comment + // (i.e. colon/brace precedes current cursor position). if (cursor.column > 0 && (text[cursor.line][cursor.column - 1] == ':' || text[cursor.line][cursor.column - 1] == '{') && !is_line_comment(cursor.line)) { if (indent_using_spaces) { ins += space_indent; @@ -2523,7 +2852,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { ins += "\t"; } - // no need to move the brace below if we are not taking the text with us. + // No need to move the brace below if we are not taking the text with us. if (text[cursor.line][cursor.column] == '}' && !k->get_command()) { brace_indent = true; ins += "\n" + ins.substr(1, ins.length() - 2); @@ -2565,7 +2894,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } } break; case KEY_TAB: { - if (k->get_command()) break; // avoid tab when command + if (k->get_command()) break; // Avoid tab when command. if (readonly) break; @@ -2579,7 +2908,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } else { if (k->get_shift()) { - // simple unindent + // Simple unindent. int cc = cursor.column; const String &line = text[cursor.line]; @@ -2591,17 +2920,17 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (cc > 0 && cc <= text[cursor.line].length()) { if (text[cursor.line][cc - 1] == '\t') { - // tabs unindentation + // Tabs unindentation. _remove_text(cursor.line, cc - 1, cursor.line, cc); if (cursor.column >= left) cursor_set_column(MAX(0, cursor.column - 1)); update(); } else { - // spaces unindentation + // Spaces unindentation. int spaces_to_remove = _calculate_spaces_till_next_left_indent(cc); if (spaces_to_remove > 0) { _remove_text(cursor.line, cc - spaces_to_remove, cursor.line, cc); - if (cursor.column > left - spaces_to_remove) // inside text? + if (cursor.column > left - spaces_to_remove) // Inside text? cursor_set_column(MAX(0, cursor.column - spaces_to_remove)); update(); } @@ -2611,9 +2940,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { update(); } } else { - // simple indent + // Simple indent. if (indent_using_spaces) { - // insert only as much spaces as needed till next indentation level + // Insert only as much spaces as needed till next indentation level. int spaces_to_add = _calculate_spaces_till_next_right_indent(cursor.column); String indent_to_insert = String(); for (int i = 0; i < spaces_to_add; i++) @@ -2641,20 +2970,20 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { int line = cursor.line; int column = cursor.column; - // check if we are removing a single whitespace, if so remove it and the next char type - // else we just remove the whitespace + // Check if we are removing a single whitespace, if so remove it and the next char type, + // else we just remove the whitespace. bool only_whitespace = false; if (_is_whitespace(text[line][column - 1]) && _is_whitespace(text[line][column - 2])) { only_whitespace = true; } else if (_is_whitespace(text[line][column - 1])) { - // remove the single whitespace + // Remove the single whitespace. column--; } - // check if its a text char + // Check if its a text char. bool only_char = (_is_text_char(text[line][column - 1]) && !only_whitespace); - // if its not whitespace or char then symbol. + // If its not whitespace or char then symbol. bool only_symbols = !(only_whitespace || only_char); while (column > 0) { @@ -2932,7 +3261,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (readonly) break; - if (k->get_shift() && !k->get_command() && !k->get_alt()) { + if (k->get_shift() && !k->get_command() && !k->get_alt() && is_shortcut_keys_enabled()) { cut(); break; } @@ -2940,7 +3269,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { int curline_len = text[cursor.line].length(); if (cursor.line == text.size() - 1 && cursor.column == curline_len) - break; //nothing to do + break; // Nothing to do. int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1; int next_column; @@ -2957,20 +3286,20 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { int line = cursor.line; int column = cursor.column; - // check if we are removing a single whitespace, if so remove it and the next char type - // else we just remove the whitespace + // Check if we are removing a single whitespace, if so remove it and the next char type, + // else we just remove the whitespace. bool only_whitespace = false; if (_is_whitespace(text[line][column]) && _is_whitespace(text[line][column + 1])) { only_whitespace = true; } else if (_is_whitespace(text[line][column])) { - // remove the single whitespace + // Remove the single whitespace. column++; } - // check if its a text char + // Check if its a text char. bool only_char = (_is_text_char(text[line][column]) && !only_whitespace); - // if its not whitespace or char then symbol. + // If its not whitespace or char then symbol. bool only_symbols = !(only_whitespace || only_char); while (column < curline_len) { @@ -3029,7 +3358,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { cursor_set_column(0); } else { - // move cursor column to start of wrapped row and then to start of text + // Move cursor column to start of wrapped row and then to start of text. Vector<String> rows = get_wrap_rows_text(cursor.line); int wi = get_cursor_wrap_index(); int row_start_col = 0; @@ -3037,7 +3366,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { row_start_col += rows[i].length(); } if (cursor.column == row_start_col || wi == 0) { - // compute whitespace symbols seq length + // Compute whitespace symbols seq length. int current_line_whitespace_len = 0; while (current_line_whitespace_len < text[cursor.line].length()) { CharType c = text[cursor.line][current_line_whitespace_len]; @@ -3088,7 +3417,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (k->get_command()) cursor_set_line(get_last_unhidden_line(), true, false, 9999); - // move cursor column to end of wrapped row and then to end of text + // Move cursor column to end of wrapped row and then to end of text. Vector<String> rows = get_wrap_rows_text(cursor.line); int wi = get_cursor_wrap_index(); int row_end_col = -1; @@ -3163,13 +3492,15 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { scancode_handled = false; break; } - select_all(); + if (is_shortcut_keys_enabled()) { + select_all(); + } #else if ((!k->get_command() && !k->get_control())) { scancode_handled = false; break; } - if (!k->get_shift() && k->get_command()) + if (!k->get_shift() && k->get_command() && is_shortcut_keys_enabled()) select_all(); else if (k->get_control()) { if (k->get_shift()) @@ -3225,8 +3556,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { scancode_handled = false; break; } - - cut(); + if (is_shortcut_keys_enabled()) { + cut(); + } } break; case KEY_C: { @@ -3236,29 +3568,43 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - copy(); + if (is_shortcut_keys_enabled()) { + copy(); + } } break; case KEY_Z: { + if (readonly) { + break; + } + if (!k->get_command()) { scancode_handled = false; break; } - if (k->get_shift()) - redo(); - else - undo(); + if (is_shortcut_keys_enabled()) { + if (k->get_shift()) + redo(); + else + undo(); + } } break; case KEY_Y: { + if (readonly) { + break; + } + if (!k->get_command()) { scancode_handled = false; break; } - redo(); + if (is_shortcut_keys_enabled()) { + redo(); + } } break; case KEY_V: { if (readonly) { @@ -3269,12 +3615,14 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - paste(); + if (is_shortcut_keys_enabled()) { + paste(); + } } break; case KEY_SPACE: { #ifdef OSX_ENABLED - if (completion_enabled && k->get_metakey()) { //cmd-space is spotlight shortcut in OSX + if (completion_enabled && k->get_metakey()) { // cmd-space is spotlight shortcut in OSX #else if (completion_enabled && k->get_command()) { #endif @@ -3287,6 +3635,16 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } break; + case KEY_MENU: { + if (context_menu_enabled) { + menu->set_position(get_global_transform().xform(_get_cursor_pixel_pos())); + menu->set_size(Vector2(1, 1)); + menu->set_scale(get_global_transform().get_scale()); + menu->popup(); + menu->grab_focus(); + } + } break; + default: { scancode_handled = false; @@ -3302,18 +3660,18 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { return; } - if (!scancode_handled && !k->get_command()) { //for German kbds + if (!scancode_handled && !k->get_command()) { // For German keyboards. if (k->get_unicode() >= 32) { if (readonly) return; - // remove the old character if in insert mode and no selection + // Remove the old character if in insert mode and no selection. if (insert_mode && !had_selection) { begin_complex_operation(); - // make sure we don't try and remove empty space + // Make sure we don't try and remove empty space. if (cursor.column < get_line(cursor.line).length()) { _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); } @@ -3347,8 +3705,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { void TextEdit::_scroll_up(real_t p_delta) { - if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(-p_delta)) + if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(-p_delta)) { scrolling = false; + minimap_clicked = false; + } if (scrolling) { target_v_scroll = (target_v_scroll - p_delta); @@ -3373,8 +3733,10 @@ void TextEdit::_scroll_up(real_t p_delta) { void TextEdit::_scroll_down(real_t p_delta) { - if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(p_delta)) + if (scrolling && smooth_scroll_enabled && SGN(target_v_scroll - v_scroll->get_value()) != SGN(p_delta)) { scrolling = false; + minimap_clicked = false; + } if (scrolling) { target_v_scroll = (target_v_scroll + p_delta); @@ -3423,11 +3785,12 @@ void TextEdit::_post_shift_selection() { void TextEdit::_scroll_lines_up() { scrolling = false; + minimap_clicked = false; - // adjust the vertical scroll + // Adjust the vertical scroll. set_v_scroll(get_v_scroll() - 1); - // adjust the cursor to viewport + // Adjust the cursor to viewport. if (!selection.active) { int cur_line = cursor.line; int cur_wrap = get_cursor_wrap_index(); @@ -3442,11 +3805,12 @@ void TextEdit::_scroll_lines_up() { void TextEdit::_scroll_lines_down() { scrolling = false; + minimap_clicked = false; - // adjust the vertical scroll + // Adjust the vertical scroll. set_v_scroll(get_v_scroll() + 1); - // adjust the cursor to viewport + // Adjust the cursor to viewport. if (!selection.active) { int cur_line = cursor.line; int cur_wrap = get_cursor_wrap_index(); @@ -3463,15 +3827,15 @@ void TextEdit::_scroll_lines_down() { void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) { - //save for undo... + // Save for undo. ERR_FAIL_INDEX(p_line, text.size()); ERR_FAIL_COND(p_char < 0); - /* STEP 1 remove \r from source text and separate in substrings */ + /* STEP 1: Remove \r from source text and separate in substrings. */ Vector<String> substrings = p_text.replace("\r", "").split("\n"); - /* STEP 2 fire breakpoint_toggled signals */ + /* STEP 2: Fire breakpoint_toggled signals. */ // Is this just a new empty line? bool shift_first_line = p_char == 0 && p_text.replace("\r", "") == "\n"; @@ -3487,19 +3851,19 @@ void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, i } } - /* STEP 3 add spaces if the char is greater than the end of the line */ + /* STEP 3: Add spaces if the char is greater than the end of the line. */ while (p_char > text[p_line].length()) { text.set(p_line, text[p_line] + String::chr(' ')); } - /* STEP 4 separate dest string in pre and post text */ + /* STEP 4: Separate dest string in pre and post text. */ String preinsert_text = text[p_line].substr(0, p_char); String postinsert_text = text[p_line].substr(p_char, text[p_line].size()); for (int j = 0; j < substrings.size(); j++) { - //insert the substrings + // Insert the substrings. if (j == 0) { @@ -3544,8 +3908,8 @@ String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_lin ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String()); ERR_FAIL_INDEX_V(p_to_line, text.size(), String()); ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String()); - ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // from > to - ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // from > to + ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'. + ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'. String ret; @@ -3568,8 +3932,8 @@ void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_li ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1); ERR_FAIL_INDEX(p_to_line, text.size()); ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1); - ERR_FAIL_COND(p_to_line < p_from_line); // from > to - ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // from > to + ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'. + ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'. String pre_text = text[p_from_line].substr(0, p_from_column); String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length()); @@ -3602,7 +3966,7 @@ void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_li void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) { - if (!setting_text) + if (!setting_text && idle_detect->is_inside_tree()) idle_detect->start(); if (undo_enabled) { @@ -3631,22 +3995,22 @@ void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r op.chain_forward = false; op.chain_backward = false; - //see if it should just be set as current op + // See if it should just be set as current op. if (current_op.type != op.type) { op.prev_version = get_version(); _push_current_op(); current_op = op; - return; //set as current op, return + return; // Set as current op, return. } - //see if it can be merged + // See if it can be merged. if (current_op.to_line != p_line || current_op.to_column != p_char) { op.prev_version = get_version(); _push_current_op(); current_op = op; - return; //set as current op, return + return; // Set as current op, return. } - //merge current op + // Merge current op. current_op.text += p_text; current_op.to_column = retchar; @@ -3656,7 +4020,7 @@ void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { - if (!setting_text) + if (!setting_text && idle_detect->is_inside_tree()) idle_detect->start(); String text; @@ -3670,7 +4034,7 @@ void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, i if (!undo_enabled) return; - /* UNDO!! */ + /* UNDO! */ TextOperation op; op.type = TextOperation::TYPE_REMOVE; op.from_line = p_from_line; @@ -3682,27 +4046,20 @@ void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, i op.chain_forward = false; op.chain_backward = false; - //see if it should just be set as current op + // See if it should just be set as current op. if (current_op.type != op.type) { op.prev_version = get_version(); _push_current_op(); current_op = op; - return; //set as current op, return + return; // Set as current op, return. } - //see if it can be merged + // See if it can be merged. if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) { - //basckace or similar + // Backspace or similar. current_op.text = text + current_op.text; current_op.from_line = p_from_line; current_op.from_column = p_from_column; - return; //update current op - } - if (current_op.from_line == p_from_line && current_op.from_column == p_from_column) { - - //current_op.text=text+current_op.text; - //current_op.from_line=p_from_line; - //current_op.from_column=p_from_column; - //return; //update current op + return; // Update current op. } op.prev_version = get_version(); @@ -3714,6 +4071,7 @@ void TextEdit::_insert_text_at_cursor(const String &p_text) { int new_column, new_line; _insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column); + _update_scrollbars(); cursor_set_line(new_line); cursor_set_column(new_column); @@ -3725,6 +4083,15 @@ void TextEdit::_line_edited_from(int p_line) { for (int i = p_line; i < cache_size; i++) { color_region_cache.erase(i); } + + if (syntax_highlighting_cache.size() > 0) { + cache_size = syntax_highlighting_cache.back()->key(); + for (int i = p_line - 1; i <= cache_size; i++) { + if (syntax_highlighting_cache.has(i)) { + syntax_highlighting_cache.erase(i); + } + } + } } int TextEdit::get_char_count() { @@ -3734,11 +4101,11 @@ int TextEdit::get_char_count() { for (int i = 0; i < text.size(); i++) { if (i > 0) - totalsize++; // incliude \n + totalsize++; // Include \n. totalsize += text[i].length(); } - return totalsize; // omit last \n + return totalsize; // Omit last \n. } Size2 TextEdit::get_minimum_size() const { @@ -3746,19 +4113,45 @@ Size2 TextEdit::get_minimum_size() const { return cache.style_normal->get_minimum_size(); } +int TextEdit::_get_control_height() const { + int control_height = get_size().height; + control_height -= cache.style_normal->get_minimum_size().height; + if (h_scroll->is_visible_in_tree()) { + control_height -= h_scroll->get_size().height; + } + return control_height; +} + +void TextEdit::_generate_context_menu() { + // Reorganize context menu. + menu->clear(); + if (!readonly) + menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0); + menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0); + if (!readonly) + menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0); + menu->add_separator(); + if (is_selecting_enabled()) + menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0); + if (!readonly) { + menu->add_item(RTR("Clear"), MENU_CLEAR); + menu->add_separator(); + menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0); + menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0); + } +} + int TextEdit::get_visible_rows() const { + return _get_control_height() / get_row_height(); +} - int total = get_size().height; - total -= cache.style_normal->get_minimum_size().height; - if (h_scroll->is_visible_in_tree()) - total -= h_scroll->get_size().height; - total /= get_row_height(); - return total; +int TextEdit::_get_minimap_visible_rows() const { + return _get_control_height() / (minimap_char_size.y + minimap_line_spacing); } int TextEdit::get_total_visible_rows() const { - // returns the total amount of rows we need in the editor. + // Returns the total amount of rows we need in the editor. // This skips hidden lines and counts each wrapping of a line. if (!is_hiding_enabled() && !is_wrap_enabled()) return text.size(); @@ -3775,12 +4168,12 @@ int TextEdit::get_total_visible_rows() const { void TextEdit::_update_wrap_at() { - wrap_at = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - wrap_right_offset; + wrap_at = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width - wrap_right_offset; update_cursor_wrap_offset(); text.clear_wrap_cache(); for (int i = 0; i < text.size(); i++) { - // update all values that wrap + // Update all values that wrap. if (!line_wraps(i)) continue; Vector<String> rows = get_wrap_rows_text(i); @@ -3790,8 +4183,9 @@ void TextEdit::_update_wrap_at() { void TextEdit::adjust_viewport_to_cursor() { - // make sure cursor is visible on the screen + // Make sure cursor is visible on the screen. scrolling = false; + minimap_clicked = false; int cur_line = cursor.line; int cur_wrap = get_cursor_wrap_index(); @@ -3802,20 +4196,20 @@ void TextEdit::adjust_viewport_to_cursor() { int last_vis_wrap = get_last_visible_line_wrap_index(); if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) { - // cursor is above screen + // Cursor is above screen. set_line_as_first_visible(cur_line, cur_wrap); } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) { - // cursor is below screen + // Cursor is below screen. set_line_as_last_visible(cur_line, cur_wrap); } - int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width; + int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width; if (v_scroll->is_visible_in_tree()) visible_width -= v_scroll->get_combined_minimum_size().width; - visible_width -= 20; // give it a little more space + visible_width -= 20; // Give it a little more space. if (!is_wrap_enabled()) { - // adjust x offset + // Adjust x offset. int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]); if (cursor_x > (cursor.x_ofs + visible_width)) @@ -3833,20 +4227,21 @@ void TextEdit::adjust_viewport_to_cursor() { void TextEdit::center_viewport_to_cursor() { - // move viewport so the cursor is in the center of the screen + // Move viewport so the cursor is in the center of the screen. scrolling = false; + minimap_clicked = false; if (is_line_hidden(cursor.line)) unfold_line(cursor.line); set_line_as_center_visible(cursor.line, get_cursor_wrap_index()); - int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width; + int visible_width = get_size().width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width - cache.fold_gutter_width - cache.info_gutter_width - cache.minimap_width; if (v_scroll->is_visible_in_tree()) visible_width -= v_scroll->get_combined_minimum_size().width; - visible_width -= 20; // give it a little more space + visible_width -= 20; // Give it a little more space. if (is_wrap_enabled()) { - // center x offset + // Center x offset. int cursor_x = get_column_x_offset_for_line(cursor.column, cursor.line); if (cursor_x > (cursor.x_ofs + visible_width)) @@ -3888,7 +4283,7 @@ int TextEdit::times_line_wraps(int line) const { int wrap_amount = text.get_line_wrap_amount(line); if (wrap_amount == -1) { - // update the value + // Update the value. Vector<String> rows = get_wrap_rows_text(line); wrap_amount = rows.size() - 1; text.set_line_wrap_amount(line, wrap_amount); @@ -3928,7 +4323,7 @@ Vector<String> TextEdit::get_wrap_rows_text(int p_line) const { int indent_ofs = (cur_wrap_index != 0 ? tab_offset_px : 0); if (indent_ofs + word_px + w > wrap_at) { - // not enough space to add this char; start next line + // Not enough space to add this char; start next line. wrap_substring += word_str; lines.push_back(wrap_substring); cur_wrap_index++; @@ -3942,7 +4337,7 @@ Vector<String> TextEdit::get_wrap_rows_text(int p_line) const { word_str += c; word_px += w; if (c == ' ') { - // end of a word; add this word to the substring + // End of a word; add this word to the substring. wrap_substring += word_str; px += word_px; word_str = ""; @@ -3950,9 +4345,9 @@ Vector<String> TextEdit::get_wrap_rows_text(int p_line) const { } if (indent_ofs + px + word_px > wrap_at) { - // this word will be moved to the next line + // This word will be moved to the next line. lines.push_back(wrap_substring); - // reset for next wrap + // Reset for next wrap. cur_wrap_index++; wrap_substring = ""; px = 0; @@ -3960,11 +4355,11 @@ Vector<String> TextEdit::get_wrap_rows_text(int p_line) const { } col++; } - // line ends before hit wrap_at; add this word to the substring + // Line ends before hit wrap_at; add this word to the substring. wrap_substring += word_str; lines.push_back(wrap_substring); - // update cache + // Update cache. text.set_line_wrap_amount(p_line, lines.size() - 1); return lines; @@ -3982,7 +4377,7 @@ int TextEdit::get_line_wrap_index_at_col(int p_line, int p_column) const { if (!line_wraps(p_line)) return 0; - // loop through wraps in the line text until we get to the column + // Loop through wraps in the line text until we get to the column. int wrap_index = 0; int col = 0; Vector<String> rows = get_wrap_rows_text(p_line); @@ -4087,11 +4482,14 @@ bool TextEdit::cursor_get_blink_enabled() const { void TextEdit::cursor_set_blink_enabled(const bool p_enabled) { caret_blink_enabled = p_enabled; - if (p_enabled) { - caret_blink_timer->start(); - } else { - caret_blink_timer->stop(); + if (has_focus()) { + if (p_enabled) { + caret_blink_timer->start(); + } else { + caret_blink_timer->stop(); + } } + draw_caret = true; } @@ -4123,6 +4521,7 @@ bool TextEdit::is_right_click_moving_caret() const { void TextEdit::_v_scroll_input() { scrolling = false; + minimap_clicked = false; } void TextEdit::_scroll_moved(double p_to_val) { @@ -4134,7 +4533,7 @@ void TextEdit::_scroll_moved(double p_to_val) { cursor.x_ofs = h_scroll->get_value(); if (v_scroll->is_visible_in_tree()) { - // set line ofs and wrap ofs + // Set line ofs and wrap ofs. int v_scroll_i = floor(get_v_scroll()); int sc = 0; int n_line; @@ -4289,12 +4688,12 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { _get_mouse_pos(p_pos, row, col); int left_margin = cache.style_normal->get_margin(MARGIN_LEFT); - // breakpoint icon + // Breakpoint icon. if (draw_breakpoint_gutter && p_pos.x > left_margin - 6 && p_pos.x <= left_margin + cache.breakpoint_gutter_width - 3) { return CURSOR_POINTING_HAND; } - // info icons + // Info icons. int gutter_left = left_margin + cache.breakpoint_gutter_width + cache.info_gutter_width; if (draw_info_gutter && p_pos.x > left_margin + cache.breakpoint_gutter_width - 6 && p_pos.x <= gutter_left - 3) { if (text.has_info_icon(row)) { @@ -4303,7 +4702,7 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { return CURSOR_ARROW; } - // fold icon + // Fold icon. if (draw_fold_gutter && p_pos.x > gutter_left + cache.line_number_w - 6 && p_pos.x <= gutter_left + cache.line_number_w + cache.fold_gutter_width - 3) { if (is_folded(row) || can_fold(row)) return CURSOR_POINTING_HAND; @@ -4313,9 +4712,14 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { return CURSOR_ARROW; } else { + int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT); + if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) { + return CURSOR_ARROW; + } + int row, col; _get_mouse_pos(p_pos, row, col); - // eol fold icon + // EOL fold icon. if (is_folded(row)) { int line_width = text.get_line_width(row); line_width += cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width - cursor.x_ofs; @@ -4349,8 +4753,6 @@ void TextEdit::set_text(String p_text) { update(); setting_text = false; - - //get_range()->set(0); }; String TextEdit::get_text() { @@ -4377,7 +4779,7 @@ String TextEdit::get_text_for_lookup_completion() { if (i == row) { longthing += text[i].substr(0, col); - longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += String::chr(0xFFFF); // Not unicode, represents the cursor. longthing += text[i].substr(col, text[i].size()); } else { @@ -4399,7 +4801,7 @@ String TextEdit::get_text_for_completion() { if (i == cursor.line) { longthing += text[i].substr(0, cursor.column); - longthing += String::chr(0xFFFF); //not unicode, represents the cursor + longthing += String::chr(0xFFFF); // Not unicode, represents the cursor. longthing += text[i].substr(cursor.column, text[i].size()); } else { @@ -4447,21 +4849,32 @@ void TextEdit::set_readonly(bool p_readonly) { return; readonly = p_readonly; + _generate_context_menu(); // Reorganize context menu. menu->clear(); - if (!readonly) + + if (!readonly) { + menu->add_item(RTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z); + menu->add_item(RTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z); + } + + if (!readonly) { + menu->add_separator(); menu->add_item(RTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X); + } + menu->add_item(RTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C); - if (!readonly) + + if (!readonly) { menu->add_item(RTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V); + } + menu->add_separator(); menu->add_item(RTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A); + if (!readonly) { menu->add_item(RTR("Clear"), MENU_CLEAR); - menu->add_separator(); - menu->add_item(RTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z); - menu->add_item(RTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z); } update(); @@ -4494,10 +4907,12 @@ int TextEdit::get_max_chars() const { void TextEdit::_reset_caret_blink_timer() { if (caret_blink_enabled) { - caret_blink_timer->stop(); - caret_blink_timer->start(); draw_caret = true; - update(); + if (has_focus()) { + caret_blink_timer->stop(); + caret_blink_timer->start(); + update(); + } } } @@ -4572,17 +4987,18 @@ void TextEdit::_set_syntax_highlighting(SyntaxHighlighter *p_syntax_highlighter) syntax_highlighter->set_text_editor(this); syntax_highlighter->_update_cache(); } + syntax_highlighting_cache.clear(); update(); } int TextEdit::_is_line_in_region(int p_line) { - // do we have in cache? + // Do we have in cache? if (color_region_cache.has(p_line)) { return color_region_cache[p_line]; } - // if not find the closest line we have + // If not find the closest line we have. int previous_line = p_line - 1; for (; previous_line > -1; previous_line--) { if (color_region_cache.has(p_line)) { @@ -4590,7 +5006,7 @@ int TextEdit::_is_line_in_region(int p_line) { } } - // calculate up to line we need and update the cache along the way. + // Calculate up to line we need and update the cache along the way. int in_region = color_region_cache[previous_line]; if (previous_line == -1) { in_region = -1; @@ -4638,6 +5054,7 @@ void TextEdit::clear_colors() { keywords.clear(); color_regions.clear(); color_region_cache.clear(); + syntax_highlighting_cache.clear(); text.clear_width_cache(); } @@ -4718,7 +5135,7 @@ void TextEdit::cut() { OS::get_singleton()->set_clipboard(clipboard); _remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column); - cursor_set_line(selection.from_line); // set afterwards else it causes the view to be offset + cursor_set_line(selection.from_line); // Set afterwards else it causes the view to be offset. cursor_set_column(selection.from_column); selection.active = false; @@ -4772,6 +5189,8 @@ void TextEdit::paste() { } void TextEdit::select_all() { + if (!selecting_enabled) + return; if (text.size() == 1 && text[0].length() == 0) return; @@ -4796,6 +5215,8 @@ void TextEdit::deselect() { } void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { + if (!selecting_enabled) + return; if (p_from_line < 0) p_from_line = 0; @@ -4947,7 +5368,7 @@ int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_searc col = p_search.findn(p_key, p_from_column); } - // whole words only + // Whole words only. if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) { p_from_column = col; @@ -4987,14 +5408,12 @@ bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_l ERR_FAIL_INDEX_V(p_from_line, text.size(), false); ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false); - //search through the whole document, but start by current line + // Search through the whole document, but start by current line. int line = p_from_line; int pos = -1; for (int i = 0; i < text.size() + 1; i++) { - //backwards is broken... - //int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch if (line < 0) { line = text.size() - 1; @@ -5008,7 +5427,7 @@ bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_l if (line == p_from_line) { if (i == text.size()) { - //wrapped + // Wrapped. if (p_search_flags & SEARCH_BACKWARDS) { from_column = text_line.length(); @@ -5042,6 +5461,9 @@ bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_l break; } pos_from = last_pos - p_key.length(); + if (pos_from < 0) { + break; + } } } else { while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) { @@ -5056,7 +5478,7 @@ bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_l bool is_match = true; if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) { - //validate for whole words + // Validate for whole words. if (pos > 0 && _is_text_char(text_line[pos - 1])) is_match = false; else if (pos + p_key.length() < text_line.length() && _is_text_char(text_line[pos + p_key.length()])) @@ -5254,7 +5676,7 @@ void TextEdit::unhide_all_lines() { int TextEdit::num_lines_from(int p_line_from, int visible_amount) const { - // returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines) + // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines). ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount)); if (!is_hiding_enabled()) @@ -5287,8 +5709,8 @@ int TextEdit::num_lines_from(int p_line_from, int visible_amount) const { int TextEdit::num_lines_from_rows(int p_line_from, int p_wrap_index_from, int visible_amount, int &wrap_index) const { - // returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows) - // wrap index is set to the wrap index of the last line + // Returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows). + // Wrap index is set to the wrap index of the last line. wrap_index = 0; ERR_FAIL_INDEX_V(p_line_from, text.size(), ABS(visible_amount)); @@ -5334,7 +5756,7 @@ int TextEdit::num_lines_from_rows(int p_line_from, int p_wrap_index_from, int vi int TextEdit::get_last_unhidden_line() const { - // returns the last line in the text that is not hidden + // Returns the last line in the text that is not hidden. if (!is_hiding_enabled()) return text.size() - 1; @@ -5351,7 +5773,7 @@ int TextEdit::get_indent_level(int p_line) const { ERR_FAIL_INDEX_V(p_line, text.size(), 0); - // counts number of tabs and spaces before line starts + // Counts number of tabs and spaces before line starts. int tab_count = 0; int whitespace_count = 0; int line_length = text[p_line].size(); @@ -5369,7 +5791,7 @@ int TextEdit::get_indent_level(int p_line) const { bool TextEdit::is_line_comment(int p_line) const { - // checks to see if this line is the start of a comment + // Checks to see if this line is the start of a comment. ERR_FAIL_INDEX_V(p_line, text.size(), false); const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(p_line); @@ -5449,7 +5871,7 @@ void TextEdit::fold_line(int p_line) { if (!can_fold(p_line)) return; - // hide lines below this one + // Hide lines below this one. int start_indent = get_indent_level(p_line); int last_line = start_indent; for (int i = p_line + 1; i < text.size(); i++) { @@ -5467,7 +5889,7 @@ void TextEdit::fold_line(int p_line) { set_line_as_hidden(i, true); } - // fix selection + // Fix selection. if (is_selection_active()) { if (is_line_hidden(selection.from_line) && is_line_hidden(selection.to_line)) { deselect(); @@ -5478,7 +5900,7 @@ void TextEdit::fold_line(int p_line) { } } - // reset cursor + // Reset cursor. if (is_line_hidden(cursor.line)) { cursor_set_line(p_line, false, false); cursor_set_column(get_line(p_line).length(), false); @@ -5493,7 +5915,7 @@ void TextEdit::unfold_line(int p_line) { if (!is_folded(p_line) && !is_line_hidden(p_line)) return; - int fold_start = p_line; + int fold_start; for (fold_start = p_line; fold_start > 0; fold_start--) { if (is_folded(fold_start)) break; @@ -5539,8 +5961,8 @@ void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) { int check_line; int check_column; _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column); - ERR_FAIL_COND(check_line != p_op.to_line); // BUG - ERR_FAIL_COND(check_column != p_op.to_column); // BUG + ERR_FAIL_COND(check_line != p_op.to_line); // BUG. + ERR_FAIL_COND(check_column != p_op.to_column); // BUG. } else { _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column); @@ -5550,7 +5972,7 @@ void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) { void TextEdit::_clear_redo() { if (undo_stack_pos == NULL) - return; //nothing to clear + return; // Nothing to clear. _push_current_op(); @@ -5568,12 +5990,12 @@ void TextEdit::undo() { if (undo_stack_pos == NULL) { if (!undo_stack.size()) - return; //nothing to undo + return; // Nothing to undo. undo_stack_pos = undo_stack.back(); } else if (undo_stack_pos == undo_stack.front()) - return; // at the bottom of the undo stack + return; // At the bottom of the undo stack. else undo_stack_pos = undo_stack_pos->prev(); @@ -5614,7 +6036,7 @@ void TextEdit::redo() { _push_current_op(); if (undo_stack_pos == NULL) - return; //nothing to do. + return; // Nothing to do. deselect(); @@ -5668,7 +6090,7 @@ void TextEdit::end_complex_operation() { void TextEdit::_push_current_op() { if (current_op.type == TextOperation::TYPE_NONE) - return; // do nothing + return; // Nothing to do. if (next_operation_is_complex) { current_op.chain_forward = true; @@ -5690,7 +6112,7 @@ bool TextEdit::is_indent_using_spaces() const { } void TextEdit::set_indent_size(const int p_size) { - ERR_FAIL_COND(p_size <= 0); + ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0."); indent_size = p_size; text.set_indent_size(p_size); @@ -5768,7 +6190,7 @@ double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const { if (!is_wrap_enabled() && !is_hiding_enabled()) return p_line; - // count the number of visible lines up to this line + // Count the number of visible lines up to this line. double new_line_scroll_pos = 0; int to = CLAMP(p_line, 0, text.size() - 1); for (int i = 0; i < to; i++) { @@ -5828,10 +6250,7 @@ int TextEdit::get_last_visible_line_wrap_index() const { double TextEdit::get_visible_rows_offset() const { - double total = get_size().height; - total -= cache.style_normal->get_minimum_size().height; - if (h_scroll->is_visible_in_tree()) - total -= h_scroll->get_size().height; + double total = _get_control_height(); total /= (double)get_row_height(); total = total - floor(total); total = -CLAMP(total, 0.001, 1) + 1; @@ -5913,7 +6332,7 @@ void TextEdit::_confirm_completion() { CharType last_completion_char = completion_current.insert_text[completion_current.insert_text.length() - 1]; if ((last_completion_char == '"' || last_completion_char == '\'') && last_completion_char == next_char) { - _base_remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); + _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); } if (last_completion_char == '(') { @@ -5969,7 +6388,7 @@ void TextEdit::_update_completion_candidates() { String s; - //look for keywords first + // Look for keywords first. bool inquote = false; int first_quote = -1; @@ -5994,7 +6413,7 @@ void TextEdit::_update_completion_candidates() { bool cancel = false; if (!inquote && first_quote == cofs - 1) { - //no completion here + // No completion here. cancel = true; } else if (inquote && first_quote != -1) { @@ -6032,11 +6451,12 @@ void TextEdit::_update_completion_candidates() { bool prev_is_prefix = false; if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1]))) prev_is_prefix = true; - if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2]))) //check with one space before prefix, to allow indent + // Check with one space before prefix, to allow indent. + if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2]))) prev_is_prefix = true; if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) { - //none to complete, cancel + // None to complete, cancel. _cancel_completion(); return; } @@ -6086,18 +6506,18 @@ void TextEdit::_update_completion_candidates() { } if (completion_options.size() == 0) { - //no options to complete, cancel + // No options to complete, cancel. _cancel_completion(); return; } if (completion_options.size() == 1 && s == completion_options[0].display) { - // A perfect match, stop completion + // A perfect match, stop completion. _cancel_completion(); return; } - // The top of the list is the best match + // The top of the list is the best match. completion_current = completion_options[0]; completion_enabled = true; } @@ -6116,10 +6536,30 @@ void TextEdit::query_code_comple() { c--; } - if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1])))) - emit_signal("request_completion"); - else if (ofs > 1 && l[ofs - 1] == ' ' && completion_prefixes.has(String::chr(l[ofs - 2]))) //make it work with a space too, it's good enough - emit_signal("request_completion"); + bool ignored = completion_active && !completion_options.empty(); + if (ignored) { + ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT; + const ScriptCodeCompletionOption *previous_option = NULL; + for (int i = 0; i < completion_options.size(); i++) { + const ScriptCodeCompletionOption ¤t_option = completion_options[i]; + if (!previous_option) { + previous_option = ¤t_option; + kind = current_option.kind; + } + if (previous_option->kind != current_option.kind) { + ignored = false; + break; + } + } + ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL); + } + + if (!ignored) { + if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1])))) + emit_signal("request_completion"); + else if (ofs > 1 && l[ofs - 1] == ' ' && completion_prefixes.has(String::chr(l[ofs - 2]))) // Make it work with a space too, it's good enough. + emit_signal("request_completion"); + } } void TextEdit::set_code_hint(const String &p_hint) { @@ -6220,9 +6660,21 @@ void TextEdit::set_line(int line, String new_text) { } void TextEdit::insert_at(const String &p_text, int at) { - cursor_set_column(0); - cursor_set_line(at, false, true); _insert_text(at, 0, p_text + "\n"); + if (cursor.line >= at) { + // offset cursor when located after inserted line + ++cursor.line; + } + if (is_selection_active()) { + if (selection.from_line >= at) { + // offset selection when located after inserted line + ++selection.from_line; + ++selection.to_line; + } else if (selection.to_line >= at) { + // extend selection that includes inserted line + ++selection.to_line; + } + } } void TextEdit::set_show_line_numbers(bool p_show) { @@ -6314,6 +6766,24 @@ int TextEdit::get_info_gutter_width() const { return info_gutter_width; } +void TextEdit::set_draw_minimap(bool p_draw) { + draw_minimap = p_draw; + update(); +} + +bool TextEdit::is_drawing_minimap() const { + return draw_minimap; +} + +void TextEdit::set_minimap_width(int p_minimap_width) { + minimap_width = p_minimap_width; + update(); +} + +int TextEdit::get_minimap_width() const { + return minimap_width; +} + void TextEdit::set_hiding_enabled(bool p_enabled) { if (!p_enabled) unhide_all_lines(); @@ -6390,6 +6860,29 @@ bool TextEdit::is_context_menu_enabled() { return context_menu_enabled; } +void TextEdit::set_shortcut_keys_enabled(bool p_enabled) { + shortcut_keys_enabled = p_enabled; + + _generate_context_menu(); +} + +void TextEdit::set_selecting_enabled(bool p_enabled) { + selecting_enabled = p_enabled; + + if (!selecting_enabled) + deselect(); + + _generate_context_menu(); +} + +bool TextEdit::is_selecting_enabled() const { + return selecting_enabled; +} + +bool TextEdit::is_shortcut_keys_enabled() const { + return shortcut_keys_enabled; +} + PopupMenu *TextEdit::get_menu() const { return menu; } @@ -6443,10 +6936,12 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_wrap_enabled", "enable"), &TextEdit::set_wrap_enabled); ClassDB::bind_method(D_METHOD("is_wrap_enabled"), &TextEdit::is_wrap_enabled); - // ClassDB::bind_method(D_METHOD("set_max_chars", "amount"), &TextEdit::set_max_chars); - // ClassDB::bind_method(D_METHOD("get_max_char"), &TextEdit::get_max_chars); ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled); ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled); + ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &TextEdit::set_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled); + ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled); ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut); ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy); @@ -6520,6 +7015,11 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("get_breakpoints"), &TextEdit::get_breakpoints_array); ClassDB::bind_method(D_METHOD("remove_breakpoints"), &TextEdit::remove_breakpoints); + ClassDB::bind_method(D_METHOD("draw_minimap", "draw"), &TextEdit::set_draw_minimap); + ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap); + ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width); + ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "readonly"), "set_readonly", "is_readonly"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled"); @@ -6532,11 +7032,16 @@ void TextEdit::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_enabled"), "set_wrap_enabled", "is_wrap_enabled"); - // ADD_PROPERTY(PropertyInfo(Variant::BOOL, "max_chars"), "set_max_chars", "get_max_chars"); + + ADD_GROUP("Minimap", "minimap_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw"), "draw_minimap", "is_drawing_minimap"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width"), "set_minimap_width", "get_minimap_width"); ADD_GROUP("Caret", "caret_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_block_mode"), "cursor_set_block_mode", "cursor_is_block_mode"); @@ -6561,7 +7066,7 @@ void TextEdit::_bind_methods() { BIND_ENUM_CONSTANT(MENU_MAX); GLOBAL_DEF("gui/timers/text_edit_idle_detect_sec", 3); - ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::REAL, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers + ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/text_edit_idle_detect_sec", PropertyInfo(Variant::REAL, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater")); // No negative numbers. } TextEdit::TextEdit() { @@ -6592,8 +7097,6 @@ TextEdit::TextEdit() { indent_size = 4; text.set_indent_size(indent_size); text.clear(); - //text.insert(1,"Mongolia..."); - //text.insert(2,"PAIS GENEROSO!!"); text.set_color_regions(&color_regions); h_scroll = memnew(HScrollBar); @@ -6676,13 +7179,25 @@ TextEdit::TextEdit() { select_identifiers_enabled = false; smooth_scroll_enabled = false; scrolling = false; + minimap_clicked = false; + dragging_minimap = false; + can_drag_minimap = false; + minimap_scroll_ratio = 0; + minimap_scroll_click_pos = 0; + dragging_selection = false; target_v_scroll = 0; v_scroll_speed = 80; + draw_minimap = false; + minimap_width = 80; + minimap_char_size = Point2(1, 2); + minimap_line_spacing = 1; + selecting_enabled = true; context_menu_enabled = true; + shortcut_keys_enabled = true; menu = memnew(PopupMenu); add_child(menu); - readonly = true; // initialise to opposite first, so we get past the early-out in set_readonly + readonly = true; // Initialise to opposite first, so we get past the early-out in set_readonly. set_readonly(false); menu->connect("id_pressed", this, "menu_option"); first_draw = true; @@ -6696,8 +7211,14 @@ TextEdit::~TextEdit() { /////////////////////////////////////////////////////////////////////////////// Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int p_line) { + if (syntax_highlighting_cache.has(p_line)) { + return syntax_highlighting_cache[p_line]; + } + if (syntax_highlighter != NULL) { - return syntax_highlighter->_get_line_syntax_highlighting(p_line); + Map<int, HighlighterInfo> color_map = syntax_highlighter->_get_line_syntax_highlighting(p_line); + syntax_highlighting_cache[p_line] = color_map; + return color_map; } Map<int, HighlighterInfo> color_map; @@ -6743,14 +7264,14 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int bool is_symbol = _is_symbol(str[j]); bool is_number = _is_number(str[j]); - // allow ABCDEF in hex notation + // Allow ABCDEF in hex notation. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) { is_number = true; } else { is_hex_notation = false; } - // check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation + // Check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e') && !in_word && prev_is_number && !is_number) { is_number = true; is_symbol = false; @@ -6780,7 +7301,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int if (!cri.end) { in_region = cri.region; } - } else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise + } else if (in_region == cri.region && !color_regions[cri.region].line_only) { // Ignore otherwise. if (cri.end || color_regions[cri.region].eq) { deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length(); } @@ -6808,7 +7329,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int if (col) { for (int k = j - 1; k >= 0; k--) { if (str[k] == '.') { - col = NULL; //member indexing not allowed + col = NULL; // Member indexing not allowed. break; } else if (str[k] > 32) { break; @@ -6830,7 +7351,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int k++; } - // check for space between name and bracket + // Check for space between name and bracket. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) { k++; } @@ -6879,6 +7400,7 @@ Map<int, TextEdit::HighlighterInfo> TextEdit::_get_line_syntax_highlighting(int } } + syntax_highlighting_cache[p_line] = color_map; return color_map; } |