Ejemplo n.º 1
0
void ScriptTextEditor::trim_trailing_whitespace() {

	TextEdit *tx = code_editor->get_text_edit();

	bool trimed_whitespace = false;
	for (int i = 0; i < tx->get_line_count(); i++) {
		String line = tx->get_line(i);
		if (line.ends_with(" ") || line.ends_with("\t")) {

			if (!trimed_whitespace) {
				tx->begin_complex_operation();
				trimed_whitespace = true;
			}

			int end = 0;
			for (int j = line.length() - 1; j > -1; j--) {
				if (line[j] != ' ' && line[j] != '\t') {
					end = j+1;
					break;
				}
			}
			tx->set_line(i, line.substr(0, end));
		}
	}
	if (trimed_whitespace) {
		tx->end_complex_operation();
		tx->update();
	}
}
Ejemplo n.º 2
0
void ScriptTextEditor::_edit_option(int p_op) {

	switch(p_op) {
		case EDIT_UNDO: {
			code_editor->get_text_edit()->undo();
			code_editor->get_text_edit()->call_deferred("grab_focus");
		} break;
		case EDIT_REDO: {
			code_editor->get_text_edit()->redo();
			code_editor->get_text_edit()->call_deferred("grab_focus");
		} break;
		case EDIT_CUT: {

			code_editor->get_text_edit()->cut();
			code_editor->get_text_edit()->call_deferred("grab_focus");
		} break;
		case EDIT_COPY: {
			code_editor->get_text_edit()->copy();
			code_editor->get_text_edit()->call_deferred("grab_focus");

		} break;
		case EDIT_PASTE: {
			code_editor->get_text_edit()->paste();
			code_editor->get_text_edit()->call_deferred("grab_focus");

		} break;
		case EDIT_SELECT_ALL: {

			code_editor->get_text_edit()->select_all();
			code_editor->get_text_edit()->call_deferred("grab_focus");

		} break;
		case EDIT_MOVE_LINE_UP: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = script;
			if (scr.is_null())
				return;

			tx->begin_complex_operation();
			if (tx->is_selection_active())
			{
				int from_line = tx->get_selection_from_line();
				int from_col  = tx->get_selection_from_column();
				int to_line   = tx->get_selection_to_line();
				int to_column = tx->get_selection_to_column();

				for (int i = from_line; i <= to_line; i++)
				{
					int line_id = i;
					int next_id = i - 1;

					if (line_id == 0 || next_id < 0)
						return;

					swap_lines(tx, line_id, next_id);
				}
				int from_line_up = from_line > 0 ? from_line-1 : from_line;
				int to_line_up   = to_line   > 0 ? to_line-1   : to_line;
				tx->select(from_line_up, from_col, to_line_up, to_column);
			}
			else
			{
				int line_id = tx->cursor_get_line();
				int next_id = line_id - 1;

				if (line_id == 0 || next_id < 0)
					return;

				swap_lines(tx, line_id, next_id);
			}
			tx->end_complex_operation();
			tx->update();

		} break;
		case EDIT_MOVE_LINE_DOWN: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;

			tx->begin_complex_operation();
			if (tx->is_selection_active())
			{
				int from_line = tx->get_selection_from_line();
				int from_col  = tx->get_selection_from_column();
				int to_line   = tx->get_selection_to_line();
				int to_column = tx->get_selection_to_column();

				for (int i = to_line; i >= from_line; i--)
				{
					int line_id = i;
					int next_id = i + 1;

					if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
						return;

					swap_lines(tx, line_id, next_id);
				}
				int from_line_down = from_line < tx->get_line_count() ? from_line+1 : from_line;
				int to_line_down   = to_line   < tx->get_line_count() ? to_line+1   : to_line;
				tx->select(from_line_down, from_col, to_line_down, to_column);
			}
			else
			{
				int line_id = tx->cursor_get_line();
				int next_id = line_id + 1;

				if (line_id == tx->get_line_count()-1 || next_id > tx->get_line_count())
					return;

				swap_lines(tx, line_id, next_id);
			}
			tx->end_complex_operation();
			tx->update();

		} break;
		case EDIT_INDENT_LEFT: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;

			tx->begin_complex_operation();
			if (tx->is_selection_active())
			{
				tx->indent_selection_left();
			}
			else
			{
				int begin = tx->cursor_get_line();
				String line_text = tx->get_line(begin);
				// begins with tab
				if (line_text.begins_with("\t"))
				{
					line_text = line_text.substr(1, line_text.length());
					tx->set_line(begin, line_text);
				}
				// begins with 4 spaces
				else if (line_text.begins_with("    "))
				{
					line_text = line_text.substr(4, line_text.length());
					tx->set_line(begin, line_text);
				}
			}
			tx->end_complex_operation();
			tx->update();
			//tx->deselect();

		} break;
		case EDIT_INDENT_RIGHT: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;

			tx->begin_complex_operation();
			if (tx->is_selection_active())
			{
				tx->indent_selection_right();
			}
			else
			{
				int begin = tx->cursor_get_line();
				String line_text = tx->get_line(begin);
				line_text = '\t' + line_text;
				tx->set_line(begin, line_text);
			}
			tx->end_complex_operation();
			tx->update();
			//tx->deselect();

		} break;
		case EDIT_CLONE_DOWN: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;

			int from_line = tx->cursor_get_line();
			int to_line = tx->cursor_get_line();
			int column = tx->cursor_get_column();

			if (tx->is_selection_active()) {
				from_line = tx->get_selection_from_line();
				to_line = tx->get_selection_to_line();
				column = tx->cursor_get_column();
			}
			int next_line = to_line + 1;

			tx->begin_complex_operation();
			for (int i = from_line; i <= to_line; i++) {

				if (i >= tx->get_line_count() - 1) {
						tx->set_line(i, tx->get_line(i) + "\n");
				}
				String line_clone = tx->get_line(i);
				tx->insert_at(line_clone, next_line);
				next_line++;
			}

			tx->cursor_set_column(column);
			if (tx->is_selection_active()) {
				tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
			}

			tx->end_complex_operation();
			tx->update();

		} break;
		case EDIT_TOGGLE_COMMENT: {

			TextEdit *tx = code_editor->get_text_edit();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;


			tx->begin_complex_operation();
			if (tx->is_selection_active())
			{
				int begin = tx->get_selection_from_line();
				int end = tx->get_selection_to_line();

				// End of selection ends on the first column of the last line, ignore it.
				if(tx->get_selection_to_column() == 0)
					end -= 1;

				for (int i = begin; i <= end; i++)
				{
					String line_text = tx->get_line(i);

					if (line_text.begins_with("#"))
						line_text = line_text.substr(1, line_text.length());
					else
						line_text = "#" + line_text;
					tx->set_line(i, line_text);
				}
			}
			else
			{
				int begin = tx->cursor_get_line();
				String line_text = tx->get_line(begin);

				if (line_text.begins_with("#"))
					line_text = line_text.substr(1, line_text.length());
				else
					line_text = "#" + line_text;
				tx->set_line(begin, line_text);
			}
			tx->end_complex_operation();
			tx->update();
			//tx->deselect();

		} break;
		case EDIT_COMPLETE: {

			code_editor->get_text_edit()->query_code_comple();

		} break;
		case EDIT_AUTO_INDENT: {

			TextEdit *te = code_editor->get_text_edit();
			String text = te->get_text();
			Ref<Script> scr = get_edited_script();
			if (scr.is_null())
				return;
			int begin,end;
			if (te->is_selection_active()) {
				begin=te->get_selection_from_line();
				end=te->get_selection_to_line();
			} else {
				begin=0;
				end=te->get_line_count()-1;
			}
			scr->get_language()->auto_indent_code(text,begin,end);
			te->set_text(text);


		} break;
		case EDIT_TRIM_TRAILING_WHITESAPCE: {
			trim_trailing_whitespace();
		} break;


		case SEARCH_FIND: {

			code_editor->get_find_replace_bar()->popup_search();
		} break;
		case SEARCH_FIND_NEXT: {

			code_editor->get_find_replace_bar()->search_next();
		} break;
		case SEARCH_FIND_PREV: {

			code_editor->get_find_replace_bar()->search_prev();
		} break;
		case SEARCH_REPLACE: {

			code_editor->get_find_replace_bar()->popup_replace();
		} break;
		case SEARCH_LOCATE_FUNCTION: {

			quick_open->popup(get_functions());
		} break;
		case SEARCH_GOTO_LINE: {

			goto_line_dialog->popup_find_line(code_editor->get_text_edit());
		} break;
		case DEBUG_TOGGLE_BREAKPOINT: {
			int line=code_editor->get_text_edit()->cursor_get_line();
			bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
			code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
			ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
		} break;
		case DEBUG_REMOVE_ALL_BREAKPOINTS: {
			List<int> bpoints;
			code_editor->get_text_edit()->get_breakpoints(&bpoints);

			for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
				int line = E->get();
				bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
				code_editor->get_text_edit()->set_line_as_breakpoint(line,dobreak);
				ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(),line+1,dobreak);
			}
		}
		case DEBUG_GOTO_NEXT_BREAKPOINT: {
			List<int> bpoints;
			code_editor->get_text_edit()->get_breakpoints(&bpoints);
			if (bpoints.size() <= 0) {
				return;
			}

			int line=code_editor->get_text_edit()->cursor_get_line();
			// wrap around
			if (line >= bpoints[bpoints.size() - 1]) {
				code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
			} else {
				for(List<int>::Element *E=bpoints.front();E;E=E->next()) {
					int bline = E->get();
					if (bline > line) {
						code_editor->get_text_edit()->cursor_set_line(bline);
						return;
					}
				}
			}

		} break;
		case DEBUG_GOTO_PREV_BREAKPOINT: {
			List<int> bpoints;
			code_editor->get_text_edit()->get_breakpoints(&bpoints);
			if (bpoints.size() <= 0) {
				return;
			}

			int line=code_editor->get_text_edit()->cursor_get_line();
			// wrap around
			if (line <= bpoints[0]) {
				code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
			} else {
				for(List<int>::Element *E=bpoints.back();E;E=E->prev()) {
					int bline = E->get();
					if (bline < line) {
						code_editor->get_text_edit()->cursor_set_line(bline);
						return;
					}
				}
			}

		} break;

		case HELP_CONTEXTUAL: {
			String text = code_editor->get_text_edit()->get_selection_text();
			if (text == "")
				text = code_editor->get_text_edit()->get_word_under_cursor();
			if (text != "") {
				emit_signal("request_help_search",text);
			}
		} break;
	}
}
Ejemplo n.º 3
0
void ShaderEditor::_menu_option(int p_option) {

	switch (p_option) {
		case EDIT_UNDO: {
			shader_editor->get_text_edit()->undo();
		} break;
		case EDIT_REDO: {
			shader_editor->get_text_edit()->redo();
		} break;
		case EDIT_CUT: {
			shader_editor->get_text_edit()->cut();
		} break;
		case EDIT_COPY: {
			shader_editor->get_text_edit()->copy();
		} break;
		case EDIT_PASTE: {
			shader_editor->get_text_edit()->paste();
		} break;
		case EDIT_SELECT_ALL: {
			shader_editor->get_text_edit()->select_all();
		} break;
		case EDIT_MOVE_LINE_UP: {
			shader_editor->move_lines_up();
		} break;
		case EDIT_MOVE_LINE_DOWN: {
			shader_editor->move_lines_down();
		} break;
		case EDIT_INDENT_LEFT: {

			TextEdit *tx = shader_editor->get_text_edit();
			if (shader.is_null())
				return;

			tx->indent_left();

		} break;
		case EDIT_INDENT_RIGHT: {

			TextEdit *tx = shader_editor->get_text_edit();
			if (shader.is_null())
				return;

			tx->indent_right();

		} break;
		case EDIT_DELETE_LINE: {
			shader_editor->delete_lines();
		} break;
		case EDIT_CLONE_DOWN: {
			shader_editor->clone_lines_down();
		} break;
		case EDIT_TOGGLE_COMMENT: {

			TextEdit *tx = shader_editor->get_text_edit();
			if (shader.is_null())
				return;

			tx->begin_complex_operation();
			if (tx->is_selection_active()) {
				int begin = tx->get_selection_from_line();
				int end = tx->get_selection_to_line();

				// End of selection ends on the first column of the last line, ignore it.
				if (tx->get_selection_to_column() == 0)
					end -= 1;

				// Check if all lines in the selected block are commented
				bool is_commented = true;
				for (int i = begin; i <= end; i++) {
					if (!tx->get_line(i).begins_with("//")) {
						is_commented = false;
						break;
					}
				}
				for (int i = begin; i <= end; i++) {
					String line_text = tx->get_line(i);

					if (line_text.strip_edges().empty()) {
						line_text = "//";
					} else {
						if (is_commented) {
							line_text = line_text.substr(2, line_text.length());
						} else {
							line_text = "//" + line_text;
						}
					}
					tx->set_line(i, line_text);
				}
			} else {
				int begin = tx->cursor_get_line();
				String line_text = tx->get_line(begin);

				if (line_text.begins_with("//"))
					line_text = line_text.substr(2, line_text.length());
				else
					line_text = "//" + line_text;
				tx->set_line(begin, line_text);
			}
			tx->end_complex_operation();
			tx->update();
			//tx->deselect();

		} break;
		case EDIT_COMPLETE: {

			shader_editor->get_text_edit()->query_code_comple();
		} break;
		case SEARCH_FIND: {

			shader_editor->get_find_replace_bar()->popup_search();
		} break;
		case SEARCH_FIND_NEXT: {

			shader_editor->get_find_replace_bar()->search_next();
		} break;
		case SEARCH_FIND_PREV: {

			shader_editor->get_find_replace_bar()->search_prev();
		} break;
		case SEARCH_REPLACE: {

			shader_editor->get_find_replace_bar()->popup_replace();
		} break;
		case SEARCH_GOTO_LINE: {

			goto_line_dialog->popup_find_line(shader_editor->get_text_edit());
		} break;
	}
	if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
		shader_editor->get_text_edit()->call_deferred("grab_focus");
	}
}