Browse Source

-added brace matching to go with the new code completion

Juan Linietsky 10 years ago
parent
commit
c8b2a5f64a

+ 139 - 0
scene/gui/text_edit.cpp

@@ -462,6 +462,119 @@ void TextEdit::_notification(int p_what) {
 				}
 			}
 
+			int brace_open_match_line=-1;
+			int brace_open_match_column=-1;
+			bool brace_open_matching=false;
+			bool brace_open_mismatch=false;
+			int brace_close_match_line=-1;
+			int brace_close_match_column=-1;
+			bool brace_close_matching=false;
+			bool brace_close_mismatch=false;
+
+
+			if (brace_matching_enabled) {
+
+				if (cursor.column<text[cursor.line].length()) {
+					//check for open
+					CharType c = text[cursor.line][cursor.column];
+					CharType closec=0;
+
+					if (c=='[') {
+						closec=']';
+					} else if (c=='{') {
+						closec='}';
+					} else if (c=='(') {
+						closec=')';
+					}
+
+					if (closec!=0) {
+
+						int stack=1;
+
+
+						for(int i=cursor.line;i<text.size();i++) {
+
+							int from = i==cursor.line?cursor.column+1:0;
+							for(int j=from;j<text[i].length();j++) {
+
+								CharType cc = text[i][j];
+								if (cc==c)
+									stack++;
+								else if (cc==closec)
+									stack--;
+
+								if (stack==0) {
+									brace_open_match_line=i;
+									brace_open_match_column=j;
+									brace_open_matching=true;
+
+									break;
+								}
+							}
+							if (brace_open_match_line!=-1)
+								break;
+						}
+
+						if (!brace_open_matching)
+							brace_open_mismatch=true;
+
+
+					}
+				}
+
+				if (cursor.column>0) {
+					CharType c = text[cursor.line][cursor.column-1];
+					CharType closec=0;
+
+
+
+					if (c==']') {
+						closec='[';
+					} else if (c=='}') {
+						closec='{';
+					} else if (c==')') {
+						closec='(';
+					}
+
+					if (closec!=0) {
+
+						int stack=1;
+
+
+						for(int i=cursor.line;i>=0;i--) {
+
+							int from = i==cursor.line?cursor.column-2:text[i].length()-1;
+							for(int j=from;j>=0;j--) {
+
+								CharType cc = text[i][j];
+								if (cc==c)
+									stack++;
+								else if (cc==closec)
+									stack--;
+
+								if (stack==0) {
+									brace_close_match_line=i;
+									brace_close_match_column=j;
+									brace_close_matching=true;
+
+									break;
+								}
+							}
+							if (brace_close_match_line!=-1)
+								break;
+						}
+
+						if (!brace_close_matching)
+							brace_close_mismatch=true;
+
+
+					}
+
+
+				}
+			}
+
+
 			int deregion=0; //force it to clear inrgion
 			Point2 cursor_pos;
 
@@ -621,9 +734,32 @@ void TextEdit::_notification(int p_what) {
 					}
 
 
+					if (brace_matching_enabled) {
+						if ( (brace_open_match_line==line && brace_open_match_column==j) ||
+						    (cursor.column==j && cursor.line==line && (brace_open_matching||brace_open_mismatch))) {
+
+							if (brace_open_mismatch)
+								color=cache.brace_mismatch_color;
+							cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
+
+						}
+
+						if (
+						     (brace_close_match_line==line && brace_close_match_column==j) ||
+						     (cursor.column==j+1 && cursor.line==line && (brace_close_matching||brace_close_mismatch))) {
+
+
+							if (brace_close_mismatch)
+								color=cache.brace_mismatch_color;
+							cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),'_',str[j+1],in_selection?cache.font_selected_color:color);
+
+						}
+					}
+
 
 					if (str[j]>=32)
 						cache.font->draw_char(ci,Point2i( char_ofs+char_margin, ofs_y+ascent),str[j],str[j+1],in_selection?cache.font_selected_color:color);
+
 					else if (draw_tabs && str[j]=='\t') {
 						int yofs= (get_row_height() - cache.tab_icon->get_height())/2;
 						cache.tab_icon->draw(ci, Point2(char_ofs+char_margin,ofs_y+yofs),in_selection?cache.font_selected_color:color);
@@ -2488,6 +2624,7 @@ void TextEdit::_update_caches() {
     cache.mark_color=get_color("mark_color");
     cache.current_line_color=get_color("current_line_color");
     cache.breakpoint_color=get_color("breakpoint_color");
+    cache.brace_mismatch_color=get_color("brace_mismatch_color");
     cache.line_spacing=get_constant("line_spacing");
     cache.row_height = cache.font->get_height() + cache.line_spacing;
     cache.tab_icon=get_icon("tab");
@@ -3428,6 +3565,8 @@ TextEdit::TextEdit()  {
     line_numbers=false;
     next_operation_is_complex=false;
     auto_brace_completion_enabled=false;
+    brace_matching_enabled=false;
+
 }
 
 TextEdit::~TextEdit()

+ 7 - 1
scene/gui/text_edit.h

@@ -79,6 +79,7 @@ class TextEdit : public Control  {
 		Color mark_color;
 		Color breakpoint_color;
 		Color current_line_color;
+		Color brace_mismatch_color;
 
 		int row_height;
 		int line_spacing;
@@ -210,6 +211,7 @@ class TextEdit : public Control  {
 	bool line_numbers;
 	
 	bool auto_brace_completion_enabled;
+	bool brace_matching_enabled;
 	bool cut_copy_line;
 
 	uint64_t last_dblclk;
@@ -316,7 +318,11 @@ public:
 	inline void set_auto_brace_completion(bool p_enabled) {
 		auto_brace_completion_enabled = p_enabled;
 	}
-	
+	inline void set_brace_matching(bool p_enabled) {
+		brace_matching_enabled=p_enabled;
+		update();
+	}
+
 	void cursor_set_column(int p_col);
 	void cursor_set_line(int p_row);
 

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

@@ -331,6 +331,7 @@ void make_default_theme() {
 	t->set_color("current_line_color","TextEdit", Color(0.3,0.5,0.8,0.15) );
 	t->set_color("cursor_color","TextEdit", control_font_color );
 	t->set_color("symbol_color","TextEdit", control_font_color_hover );
+	t->set_color("brace_mismatch_color","TextEdit", Color(1,0.2,0.2) );
 	t->set_constant("line_spacing","TextEdit",1 );
 
 	t->set_stylebox("scroll","HScrollBar", make_stylebox( hscroll_bg_png,3,3,3,3,0,0,0,0) );

+ 1 - 0
tools/editor/code_editor.cpp

@@ -601,6 +601,7 @@ CodeTextEditor::CodeTextEditor() {
 	text_editor->set_margin(MARGIN_BOTTOM,20);
 	text_editor->add_font_override("font",get_font("source","Fonts"));
 	text_editor->set_show_line_numbers(true);
+	text_editor->set_brace_matching(true);
 
 	line_col = memnew( Label );
 	add_child(line_col);

+ 1 - 0
tools/editor/editor_settings.cpp

@@ -403,6 +403,7 @@ void EditorSettings::_load_defaults() {
 	set("text_editor/string_color",Color::html("ef6ebe"));
 	set("text_editor/symbol_color",Color::html("badfff"));
 	set("text_editor/selection_color",Color::html("7b5dbe"));
+	set("text_editor/brace_mismatch_color",Color(1,0.2,0.2));
 
 	set("text_editor/idle_parse_delay",2);
 	set("text_editor/create_signal_callbacks",true);

+ 1 - 0
tools/editor/plugins/script_editor_plugin.cpp

@@ -209,6 +209,7 @@ void ScriptTextEditor::_load_theme_settings() {
 	get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
 	get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
+	get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
 
 	Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));
 

+ 1 - 0
tools/editor/plugins/shader_editor_plugin.cpp

@@ -80,6 +80,7 @@ void ShaderTextEditor::_load_theme_settings() {
 	get_text_edit()->add_color_override("font_color",EDITOR_DEF("text_editor/text_color",Color(0,0,0)));
 	get_text_edit()->add_color_override("font_selected_color",EDITOR_DEF("text_editor/text_selected_color",Color(1,1,1)));
 	get_text_edit()->add_color_override("selection_color",EDITOR_DEF("text_editor/selection_color",Color(0.2,0.2,1)));
+	get_text_edit()->add_color_override("brace_mismatch_color",EDITOR_DEF("text_editor/brace_mismatch_color",Color(1,0.2,0.2)));
 
 	Color keyword_color= EDITOR_DEF("text_editor/keyword_color",Color(0.5,0.0,0.2));