Beispiel #1
0
void TextEditor::reload_text() {

	ERR_FAIL_COND(text_file.is_null());

	TextEdit *te = code_editor->get_text_edit();
	int column = te->cursor_get_column();
	int row = te->cursor_get_line();
	int h = te->get_h_scroll();
	int v = te->get_v_scroll();

	te->set_text(text_file->get_text());
	te->clear_undo_history();
	te->cursor_set_line(row);
	te->cursor_set_column(column);
	te->set_h_scroll(h);
	te->set_v_scroll(v);

	te->tag_saved_version();

	code_editor->update_line_and_column();
}
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;
	}
}