Browse Source

Move line length guidelines into CodeEdit

Paulb23 4 years ago
parent
commit
8f900ac178

+ 6 - 0
doc/classes/CodeEdit.xml

@@ -464,6 +464,9 @@
 		<member name="line_folding" type="bool" setter="set_line_folding_enabled" getter="is_line_folding_enabled" default="true">
 			Sets whether line folding is allowed.
 		</member>
+		<member name="line_length_guidelines" type="int[]" setter="set_line_length_guidelines" getter="get_line_length_guidelines" default="[]">
+		</member>
+			Draws vertical lines at the provided columns. The first entry is considered a main hard guideline and is draw more prominently.
 		<member name="structured_text_bidi_override_options" type="Array" setter="set_structured_text_bidi_override_options" getter="get_structured_text_bidi_override_options" override="true" default="[]" />
 		<member name="text_direction" type="int" setter="set_text_direction" getter="get_text_direction" override="true" enum="Control.TextDirection" default="1" />
 		<member name="zero_pad_line_numbers" type="bool" setter="set_line_numbers_zero_padded" getter="is_line_numbers_zero_padded" default="false">
@@ -568,6 +571,9 @@
 		<theme_item name="font_size" type="int">
 			Font size of the [CodeEdit]'s text.
 		</theme_item>
+		<theme_item name="line_length_guideline_color" type="Color" default="Color(0.3, 0.5, 0.8, 0.1)">
+			Color of the main line length guideline, secondary guidelines will have 50% alpha applied.
+		</theme_item>
 		<theme_item name="line_number_color" type="Color" default="Color(0.67, 0.67, 0.67, 0.4)">
 		</theme_item>
 		<theme_item name="line_spacing" type="int" default="4">

+ 6 - 0
doc/classes/TextEdit.xml

@@ -264,6 +264,12 @@
 				Returns the [TextEdit]'s' tab size.
 			</description>
 		</method>
+		<method name="get_total_gutter_width" qualifiers="const">
+			<return type="int">
+			</return>
+			<description>
+			</description>
+		</method>
 		<method name="get_visible_line_count" qualifiers="const">
 			<return type="int" />
 			<description>

+ 9 - 3
editor/code_editor.cpp

@@ -951,14 +951,20 @@ void CodeTextEditor::update_editor_settings() {
 	text_editor->set_line_folding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
 	text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
 	text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/word_wrap"));
-	text_editor->set_show_line_length_guidelines(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guidelines"));
-	text_editor->set_line_length_guideline_soft_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_soft_column"));
-	text_editor->set_line_length_guideline_hard_column(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_hard_column"));
 	text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/cursor/scroll_past_end_of_file"));
 	text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/cursor/block_caret"));
 	text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink"));
 	text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/cursor/caret_blink_speed"));
 	text_editor->set_auto_brace_completion_enabled(EditorSettings::get_singleton()->get("text_editor/completion/auto_brace_complete"));
+
+	if (EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guidelines")) {
+		TypedArray<int> guideline_cols;
+		guideline_cols.append(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_hard_column"));
+		if (EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_soft_column") != guideline_cols[0]) {
+			guideline_cols.append(EditorSettings::get_singleton()->get("text_editor/appearance/line_length_guideline_soft_column"));
+		}
+		text_editor->set_line_length_guidelines(guideline_cols);
+	}
 }
 
 void CodeTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {

+ 37 - 0
scene/gui/code_edit.cpp

@@ -72,13 +72,34 @@ void CodeEdit::_notification(int p_what) {
 			code_completion_background_color = get_theme_color(SNAME("completion_background_color"));
 			code_completion_selected_color = get_theme_color(SNAME("completion_selected_color"));
 			code_completion_existing_color = get_theme_color(SNAME("completion_existing_color"));
+
+			line_length_guideline_color = get_theme_color(SNAME("line_length_guideline_color"));
 		} break;
 		case NOTIFICATION_DRAW: {
 			RID ci = get_canvas_item();
+			const Size2 size = get_size();
 			const bool caret_visible = is_caret_visible();
 			const bool rtl = is_layout_rtl();
 			const int row_height = get_row_height();
 
+			if (line_length_guideline_columns.size() > 0) {
+				const int xmargin_beg = cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
+				const int xmargin_end = size.width - cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
+				const int char_size = (int)cache.font->get_char_size('0', 0, cache.font_size).width;
+
+				for (int i = 0; i < line_length_guideline_columns.size(); i++) {
+					const int xoffset = xmargin_beg + char_size * (int)line_length_guideline_columns[i] - get_h_scroll();
+					if (xoffset > xmargin_beg && xoffset < xmargin_end) {
+						Color guideline_color = (i == 0) ? line_length_guideline_color : line_length_guideline_color * Color(1, 1, 1, 0.5);
+						if (rtl) {
+							RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
+							continue;
+						}
+						RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
+					}
+				}
+			}
+
 			bool code_completion_below = false;
 			if (caret_visible && code_completion_active && code_completion_options.size() > 0) {
 				Ref<StyleBox> csb = get_theme_stylebox(SNAME("completion"));
@@ -1866,6 +1887,16 @@ void CodeEdit::cancel_code_completion() {
 	update();
 }
 
+/* Line length guidelines */
+void CodeEdit::set_line_length_guidelines(TypedArray<int> p_guideline_columns) {
+	line_length_guideline_columns = p_guideline_columns;
+	update();
+}
+
+TypedArray<int> CodeEdit::get_line_length_guidelines() const {
+	return line_length_guideline_columns;
+}
+
 void CodeEdit::_bind_methods() {
 	/* Indent management */
 	ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
@@ -2030,7 +2061,13 @@ void CodeEdit::_bind_methods() {
 	BIND_VMETHOD(MethodInfo("_request_code_completion", PropertyInfo(Variant::BOOL, "force")));
 	BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_filter_code_completion_candidates", PropertyInfo(Variant::ARRAY, "candidates")));
 
+	/* Line length guidelines */
+	ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
+	ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
+
 	/* Inspector */
+	ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
+
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
 
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");

+ 8 - 0
scene/gui/code_edit.h

@@ -223,6 +223,10 @@ private:
 
 	void _lines_edited_from(int p_from_line, int p_to_line);
 
+	/* Line length guidelines */
+	TypedArray<int> line_length_guideline_columns;
+	Color line_length_guideline_color;
+
 protected:
 	void _gui_input(const Ref<InputEvent> &p_gui_input) override;
 	void _notification(int p_what);
@@ -378,6 +382,10 @@ public:
 	void confirm_code_completion(bool p_replace = false);
 	void cancel_code_completion();
 
+	/* Line length guidelines */
+	void set_line_length_guidelines(TypedArray<int> p_guideline_columns);
+	TypedArray<int> get_line_length_guidelines() const;
+
 	CodeEdit();
 	~CodeEdit();
 };

+ 1 - 39
scene/gui/text_edit.cpp

@@ -594,29 +594,6 @@ void TextEdit::_notification(int p_what) {
 				RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(), get_size()), cache.background_color);
 			}
 
-			if (line_length_guidelines) {
-				const int hard_x = xmargin_beg + (int)cache.font->get_char_size('0', 0, cache.font_size).width * line_length_guideline_hard_col - cursor.x_ofs;
-				if (hard_x > xmargin_beg && hard_x < xmargin_end) {
-					if (rtl) {
-						RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - hard_x, 0), Point2(size.width - hard_x, size.height), cache.line_length_guideline_color);
-					} else {
-						RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(hard_x, 0), Point2(hard_x, size.height), cache.line_length_guideline_color);
-					}
-				}
-
-				// Draw a "Soft" line length guideline, less visible than the hard line length guideline.
-				// It's usually set to a lower column compared to the hard line length guideline.
-				// Only drawn if its column differs from the hard line length guideline.
-				const int soft_x = xmargin_beg + (int)cache.font->get_char_size('0', 0, cache.font_size).width * line_length_guideline_soft_col - cursor.x_ofs;
-				if (hard_x != soft_x && soft_x > xmargin_beg && soft_x < xmargin_end) {
-					if (rtl) {
-						RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - soft_x, 0), Point2(size.width - soft_x, size.height), cache.line_length_guideline_color * Color(1, 1, 1, 0.5));
-					} else {
-						RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(soft_x, 0), Point2(soft_x, size.height), cache.line_length_guideline_color * Color(1, 1, 1, 0.5));
-					}
-				}
-			}
-
 			int brace_open_match_line = -1;
 			int brace_open_match_column = -1;
 			bool brace_open_matching = false;
@@ -3836,7 +3813,6 @@ void TextEdit::_update_caches() {
 	cache.font_readonly_color = get_theme_color(SNAME("font_readonly_color"));
 	cache.selection_color = get_theme_color(SNAME("selection_color"));
 	cache.current_line_color = get_theme_color(SNAME("current_line_color"));
-	cache.line_length_guideline_color = get_theme_color(SNAME("line_length_guideline_color"));
 	cache.code_folding_color = get_theme_color(SNAME("code_folding_color"), SNAME("CodeEdit"));
 	cache.brace_mismatch_color = get_theme_color(SNAME("brace_mismatch_color"), SNAME("CodeEdit"));
 	cache.word_highlighted_color = get_theme_color(SNAME("word_highlighted_color"));
@@ -5145,21 +5121,6 @@ void TextEdit::insert_at(const String &p_text, int at) {
 	}
 }
 
-void TextEdit::set_show_line_length_guidelines(bool p_show) {
-	line_length_guidelines = p_show;
-	update();
-}
-
-void TextEdit::set_line_length_guideline_soft_column(int p_column) {
-	line_length_guideline_soft_col = p_column;
-	update();
-}
-
-void TextEdit::set_line_length_guideline_hard_column(int p_column) {
-	line_length_guideline_hard_col = p_column;
-	update();
-}
-
 void TextEdit::set_draw_minimap(bool p_draw) {
 	if (draw_minimap != p_draw) {
 		draw_minimap = p_draw;
@@ -5599,6 +5560,7 @@ void TextEdit::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable);
 	ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters);
 	ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "object", "callback"), &TextEdit::set_gutter_custom_draw);
+	ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width);
 
 	// Line gutters.
 	ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata);

+ 0 - 8
scene/gui/text_edit.h

@@ -280,9 +280,6 @@ private:
 	bool cursor_changed_dirty = false;
 	bool text_changed_dirty = false;
 	bool undo_enabled = true;
-	bool line_length_guidelines = false;
-	int line_length_guideline_soft_col = 80;
-	int line_length_guideline_hard_col = 100;
 	bool hiding_enabled = false;
 	bool draw_minimap = false;
 	int minimap_width = 80;
@@ -453,7 +450,6 @@ protected:
 		Color selection_color;
 		Color code_folding_color;
 		Color current_line_color;
-		Color line_length_guideline_color;
 		Color brace_mismatch_color;
 		Color word_highlighted_color;
 		Color search_result_color;
@@ -739,10 +735,6 @@ public:
 	void set_highlight_current_line(bool p_enabled);
 	bool is_highlight_current_line_enabled() const;
 
-	void set_show_line_length_guidelines(bool p_show);
-	void set_line_length_guideline_soft_column(int p_column);
-	void set_line_length_guideline_hard_column(int p_column);
-
 	void set_draw_minimap(bool p_draw);
 	bool is_drawing_minimap() const;
 

+ 1 - 0
scene/resources/default_theme/default_theme.cpp

@@ -503,6 +503,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
 	theme->set_color("line_number_color", "CodeEdit", Color(0.67, 0.67, 0.67, 0.4));
 	theme->set_color("safe_line_number_color", "CodeEdit", Color(0.67, 0.78, 0.67, 0.6));
 	theme->set_color("word_highlighted_color", "CodeEdit", Color(0.8, 0.9, 0.9, 0.15));
+	theme->set_color("line_length_guideline_color", "CodeEdit", Color(0.3, 0.5, 0.8, 0.1));
 
 	theme->set_constant("completion_lines", "CodeEdit", 7);
 	theme->set_constant("completion_max_width", "CodeEdit", 50);