| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-01-01 12:46:36 +01:00
										 |  |  | /*  text_editor.cpp                                                      */ | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "text_editor.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 20:22:52 +02:00
										 |  |  | #include "core/os/keyboard.h"
 | 
					
						
							| 
									
										
										
										
											2019-10-09 16:11:04 +02:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-31 21:14:15 +03:00
										 |  |  | #include "editor/editor_settings.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-02 16:40:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:08:15 +01:00
										 |  |  | void TextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 	ERR_FAIL_COND(p_highlighter.is_null()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 11:17:18 +00:00
										 |  |  | 	highlighters[p_highlighter->_get_name()] = p_highlighter; | 
					
						
							|  |  |  | 	highlighter_menu->add_radio_check_item(p_highlighter->_get_name()); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:08:15 +01:00
										 |  |  | void TextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 	ERR_FAIL_COND(p_highlighter.is_null()); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-13 15:04:37 +02:00
										 |  |  | 	HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin(); | 
					
						
							|  |  |  | 	while (el) { | 
					
						
							|  |  |  | 		int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key); | 
					
						
							|  |  |  | 		highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter); | 
					
						
							|  |  |  | 		++el; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	CodeEdit *te = code_editor->get_text_editor(); | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 	te->set_syntax_highlighter(p_highlighter); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_change_syntax_highlighter(int p_idx) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_load_theme_settings() { | 
					
						
							| 
									
										
										
										
											2020-09-10 21:25:00 +01:00
										 |  |  | 	code_editor->get_text_editor()->get_syntax_highlighter()->update_cache(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String TextEditor::get_name() { | 
					
						
							|  |  |  | 	String name; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 02:15:19 +01:00
										 |  |  | 	name = text_file->get_path().get_file(); | 
					
						
							|  |  |  | 	if (name.is_empty()) { | 
					
						
							|  |  |  | 		// This appears for newly created built-in text_files before saving the scene.
 | 
					
						
							|  |  |  | 		name = TTR("[unsaved]"); | 
					
						
							|  |  |  | 	} else if (text_file->is_built_in()) { | 
					
						
							|  |  |  | 		const String &text_file_name = text_file->get_name(); | 
					
						
							| 
									
										
										
										
											2021-12-09 03:42:46 -06:00
										 |  |  | 		if (!text_file_name.is_empty()) { | 
					
						
							| 
									
										
										
										
											2021-11-06 02:15:19 +01:00
										 |  |  | 			// If the built-in text_file has a custom resource name defined,
 | 
					
						
							|  |  |  | 			// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
 | 
					
						
							|  |  |  | 			name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-06 02:15:19 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (is_unsaved()) { | 
					
						
							|  |  |  | 		name += "(*)"; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 09:37:40 -03:00
										 |  |  | Ref<Texture2D> TextEditor::get_theme_icon() { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 	return EditorNode::get_singleton()->get_object_icon(text_file.ptr(), ""); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | Ref<Resource> TextEditor::get_edited_resource() const { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	return text_file; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | void TextEditor::set_edited_resource(const Ref<Resource> &p_res) { | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 	ERR_FAIL_COND(text_file.is_valid()); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(p_res.is_null()); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	text_file = p_res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	code_editor->get_text_editor()->set_text(text_file->get_text()); | 
					
						
							|  |  |  | 	code_editor->get_text_editor()->clear_undo_history(); | 
					
						
							|  |  |  | 	code_editor->get_text_editor()->tag_saved_version(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 	emit_signal(SNAME("name_changed")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	code_editor->update_line_and_column(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | void TextEditor::enable_editor() { | 
					
						
							|  |  |  | 	if (editor_enabled) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	editor_enabled = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_load_theme_settings(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | void TextEditor::add_callback(const String &p_function, PackedStringArray p_args) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::set_debugger_active(bool p_active) { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 02:50:25 +07:00
										 |  |  | Control *TextEditor::get_base_editor() const { | 
					
						
							|  |  |  | 	return code_editor->get_text_editor(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 03:41:48 +02:00
										 |  |  | PackedInt32Array TextEditor::get_breakpoints() { | 
					
						
							|  |  |  | 	return PackedInt32Array(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::reload_text() { | 
					
						
							|  |  |  | 	ERR_FAIL_COND(text_file.is_null()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	CodeEdit *te = code_editor->get_text_editor(); | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 	int column = te->get_caret_column(); | 
					
						
							|  |  |  | 	int row = te->get_caret_line(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	int h = te->get_h_scroll(); | 
					
						
							|  |  |  | 	int v = te->get_v_scroll(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	te->set_text(text_file->get_text()); | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 	te->set_caret_line(row); | 
					
						
							|  |  |  | 	te->set_caret_column(column); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	te->set_h_scroll(h); | 
					
						
							|  |  |  | 	te->set_v_scroll(v); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	te->tag_saved_version(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	code_editor->update_line_and_column(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_validate_script() { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 	emit_signal(SNAME("name_changed")); | 
					
						
							|  |  |  | 	emit_signal(SNAME("edited_script_changed")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | void TextEditor::_update_bookmark_list() { | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	bookmarks_menu->clear(); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); | 
					
						
							|  |  |  | 	bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL); | 
					
						
							|  |  |  | 	bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT); | 
					
						
							|  |  |  | 	bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 03:41:48 +02:00
										 |  |  | 	PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines(); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	if (bookmark_list.size() == 0) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	bookmarks_menu->add_separator(); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < bookmark_list.size(); i++) { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 		String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).strip_edges(); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 		// Limit the size of the line if too big.
 | 
					
						
							|  |  |  | 		if (line.length() > 50) { | 
					
						
							|  |  |  | 			line = line.substr(0, 50); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 		bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - \"" + line + "\""); | 
					
						
							| 
									
										
										
										
											2022-03-12 01:06:45 +01:00
										 |  |  | 		bookmarks_menu->set_item_metadata(-1, bookmark_list[i]); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_bookmark_item_pressed(int p_idx) { | 
					
						
							|  |  |  | 	if (p_idx < 4) { // Any item before the separator.
 | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 		_edit_option(bookmarks_menu->get_item_id(p_idx)); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 		code_editor->goto_line(bookmarks_menu->get_item_metadata(p_idx)); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | void TextEditor::apply_code() { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	text_file->set_text(code_editor->get_text_editor()->get_text()); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TextEditor::is_unsaved() { | 
					
						
							| 
									
										
										
										
											2020-07-23 15:21:28 +03:00
										 |  |  | 	const bool unsaved = | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 			code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || | 
					
						
							| 
									
										
										
										
											2020-12-15 12:04:21 +00:00
										 |  |  | 			text_file->get_path().is_empty(); // In memory.
 | 
					
						
							| 
									
										
										
										
											2020-07-23 15:21:28 +03:00
										 |  |  | 	return unsaved; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Variant TextEditor::get_edit_state() { | 
					
						
							|  |  |  | 	return code_editor->get_edit_state(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::set_edit_state(const Variant &p_state) { | 
					
						
							|  |  |  | 	code_editor->set_edit_state(p_state); | 
					
						
							| 
									
										
										
										
											2019-04-13 12:43:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Dictionary state = p_state; | 
					
						
							|  |  |  | 	if (state.has("syntax_highlighter")) { | 
					
						
							|  |  |  | 		int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]); | 
					
						
							|  |  |  | 		if (idx >= 0) { | 
					
						
							|  |  |  | 			_change_syntax_highlighter(idx); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-20 10:09:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ensure_focus(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::trim_trailing_whitespace() { | 
					
						
							|  |  |  | 	code_editor->trim_trailing_whitespace(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 18:27:32 -03:00
										 |  |  | void TextEditor::insert_final_newline() { | 
					
						
							|  |  |  | 	code_editor->insert_final_newline(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | void TextEditor::convert_indent_to_spaces() { | 
					
						
							|  |  |  | 	code_editor->convert_indent_to_spaces(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::convert_indent_to_tabs() { | 
					
						
							|  |  |  | 	code_editor->convert_indent_to_tabs(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::tag_saved_version() { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	code_editor->get_text_editor()->tag_saved_version(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::goto_line(int p_line, bool p_with_error) { | 
					
						
							|  |  |  | 	code_editor->goto_line(p_line); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 23:41:10 -07:00
										 |  |  | void TextEditor::goto_line_selection(int p_line, int p_begin, int p_end) { | 
					
						
							|  |  |  | 	code_editor->goto_line_selection(p_line, p_begin, p_end); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 17:20:27 +01:00
										 |  |  | void TextEditor::set_executing_line(int p_line) { | 
					
						
							|  |  |  | 	code_editor->set_executing_line(p_line); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::clear_executing_line() { | 
					
						
							|  |  |  | 	code_editor->clear_executing_line(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | void TextEditor::ensure_focus() { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	code_editor->get_text_editor()->grab_focus(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Vector<String> TextEditor::get_functions() { | 
					
						
							|  |  |  | 	return Vector<String>(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TextEditor::show_members_overview() { | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::update_settings() { | 
					
						
							|  |  |  | 	code_editor->update_editor_settings(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 17:26:15 +00:00
										 |  |  | void TextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) { | 
					
						
							|  |  |  | 	Variant args[1] = { this }; | 
					
						
							|  |  |  | 	const Variant *argp[] = { &args[0] }; | 
					
						
							| 
									
										
										
										
											2022-07-28 22:56:41 +02:00
										 |  |  | 	code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Control *TextEditor::get_edit_menu() { | 
					
						
							|  |  |  | 	return edit_hb; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::clear_edit_menu() { | 
					
						
							|  |  |  | 	memdelete(edit_hb); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 01:05:41 +02:00
										 |  |  | void TextEditor::set_find_replace_bar(FindReplaceBar *p_bar) { | 
					
						
							|  |  |  | 	code_editor->set_find_replace_bar(p_bar); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | void TextEditor::_edit_option(int p_op) { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	CodeEdit *tx = code_editor->get_text_editor(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch (p_op) { | 
					
						
							|  |  |  | 		case EDIT_UNDO: { | 
					
						
							|  |  |  | 			tx->undo(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_REDO: { | 
					
						
							|  |  |  | 			tx->redo(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_CUT: { | 
					
						
							|  |  |  | 			tx->cut(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_COPY: { | 
					
						
							|  |  |  | 			tx->copy(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_PASTE: { | 
					
						
							|  |  |  | 			tx->paste(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_SELECT_ALL: { | 
					
						
							|  |  |  | 			tx->select_all(); | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			tx->call_deferred(SNAME("grab_focus")); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_MOVE_LINE_UP: { | 
					
						
							|  |  |  | 			code_editor->move_lines_up(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_MOVE_LINE_DOWN: { | 
					
						
							|  |  |  | 			code_editor->move_lines_down(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2022-09-29 04:12:52 +02:00
										 |  |  | 		case EDIT_INDENT: { | 
					
						
							| 
									
										
										
										
											2021-06-15 15:05:01 +01:00
										 |  |  | 			tx->indent_lines(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2022-09-29 04:12:52 +02:00
										 |  |  | 		case EDIT_UNINDENT: { | 
					
						
							|  |  |  | 			tx->unindent_lines(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		case EDIT_DELETE_LINE: { | 
					
						
							|  |  |  | 			code_editor->delete_lines(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2021-06-29 12:09:07 +02:00
										 |  |  | 		case EDIT_DUPLICATE_SELECTION: { | 
					
						
							|  |  |  | 			code_editor->duplicate_selection(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_TOGGLE_FOLD_LINE: { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 			tx->toggle_foldable_line(tx->get_caret_line()); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 			tx->queue_redraw(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_FOLD_ALL_LINES: { | 
					
						
							|  |  |  | 			tx->fold_all_lines(); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 			tx->queue_redraw(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_UNFOLD_ALL_LINES: { | 
					
						
							| 
									
										
										
										
											2021-07-09 13:07:10 +01:00
										 |  |  | 			tx->unfold_all_lines(); | 
					
						
							| 
									
										
										
										
											2022-08-13 23:21:24 +02:00
										 |  |  | 			tx->queue_redraw(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_TRIM_TRAILING_WHITESAPCE: { | 
					
						
							|  |  |  | 			trim_trailing_whitespace(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_CONVERT_INDENT_TO_SPACES: { | 
					
						
							|  |  |  | 			convert_indent_to_spaces(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_CONVERT_INDENT_TO_TABS: { | 
					
						
							|  |  |  | 			convert_indent_to_tabs(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_TO_UPPERCASE: { | 
					
						
							|  |  |  | 			_convert_case(CodeTextEditor::UPPER); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_TO_LOWERCASE: { | 
					
						
							|  |  |  | 			_convert_case(CodeTextEditor::LOWER); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case EDIT_CAPITALIZE: { | 
					
						
							|  |  |  | 			_convert_case(CodeTextEditor::CAPITALIZE); | 
					
						
							|  |  |  | 		} 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; | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 		case SEARCH_IN_FILES: { | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 			String selected_text = code_editor->get_text_editor()->get_selected_text(); | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Yep, because it doesn't make sense to instance this dialog for every single script open...
 | 
					
						
							|  |  |  | 			// So this will be delegated to the ScriptEditor.
 | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			emit_signal(SNAME("search_in_files_requested"), selected_text); | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2020-02-09 10:10:58 +01:00
										 |  |  | 		case REPLACE_IN_FILES: { | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 			String selected_text = code_editor->get_text_editor()->get_selected_text(); | 
					
						
							| 
									
										
										
										
											2020-02-09 10:10:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 18:22:52 -03:00
										 |  |  | 			emit_signal(SNAME("replace_in_files_requested"), selected_text); | 
					
						
							| 
									
										
										
										
											2020-02-09 10:10:58 +01:00
										 |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		case SEARCH_GOTO_LINE: { | 
					
						
							|  |  |  | 			goto_line_dialog->popup_find_line(tx); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2019-04-20 01:51:25 +02:00
										 |  |  | 		case BOOKMARK_TOGGLE: { | 
					
						
							|  |  |  | 			code_editor->toggle_bookmark(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case BOOKMARK_GOTO_NEXT: { | 
					
						
							|  |  |  | 			code_editor->goto_next_bookmark(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case BOOKMARK_GOTO_PREV: { | 
					
						
							|  |  |  | 			code_editor->goto_prev_bookmark(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case BOOKMARK_REMOVE_ALL: { | 
					
						
							|  |  |  | 			code_editor->remove_all_bookmarks(); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) { | 
					
						
							|  |  |  | 	code_editor->convert_case(p_case); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 01:43:50 +02:00
										 |  |  | static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	if (Object::cast_to<TextFile>(*p_resource)) { | 
					
						
							|  |  |  | 		return memnew(TextEditor); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::register_editor() { | 
					
						
							|  |  |  | 	ScriptEditor::register_create_script_editor_function(create_editor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) { | 
					
						
							|  |  |  | 	Ref<InputEventMouseButton> mb = ev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mb.is_valid()) { | 
					
						
							| 
									
										
										
										
											2021-08-13 16:31:57 -05:00
										 |  |  | 		if (mb->get_button_index() == MouseButton::RIGHT) { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 			CodeEdit *tx = code_editor->get_text_editor(); | 
					
						
							| 
									
										
										
										
											2021-07-10 11:41:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position()); | 
					
						
							|  |  |  | 			int row = pos.y; | 
					
						
							|  |  |  | 			int col = pos.x; | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-15 18:14:46 +01:00
										 |  |  | 			tx->set_move_caret_on_right_click_enabled(EditorSettings::get_singleton()->get("text_editor/behavior/navigation/move_caret_on_right_click")); | 
					
						
							| 
									
										
										
										
											2021-03-13 14:09:49 +00:00
										 |  |  | 			bool can_fold = tx->can_fold_line(row); | 
					
						
							|  |  |  | 			bool is_folded = tx->is_line_folded(row); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 			if (tx->is_move_caret_on_right_click_enabled()) { | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 				if (tx->has_selection()) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 					int from_line = tx->get_selection_from_line(); | 
					
						
							|  |  |  | 					int to_line = tx->get_selection_to_line(); | 
					
						
							|  |  |  | 					int from_column = tx->get_selection_from_column(); | 
					
						
							|  |  |  | 					int to_column = tx->get_selection_to_column(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) { | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 						// Right click is outside the selected text.
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 						tx->deselect(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 				if (!tx->has_selection()) { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 					tx->set_caret_line(row, true, false); | 
					
						
							|  |  |  | 					tx->set_caret_column(col); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!mb->is_pressed()) { | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 				_make_context_menu(tx->has_selection(), can_fold, is_folded, get_local_mouse_position()); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-06-22 20:22:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Ref<InputEventKey> k = ev; | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 	if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) { | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 		CodeEdit *tx = code_editor->get_text_editor(); | 
					
						
							| 
									
										
										
										
											2021-07-08 18:35:56 +01:00
										 |  |  | 		int line = tx->get_caret_line(); | 
					
						
							|  |  |  | 		tx->adjust_viewport_to_caret(); | 
					
						
							| 
									
										
										
										
											2021-07-09 12:42:55 +01:00
										 |  |  | 		_make_context_menu(tx->has_selection(), tx->can_fold_line(line), tx->is_line_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos())); | 
					
						
							| 
									
										
										
										
											2019-06-22 20:22:52 +02:00
										 |  |  | 		context_menu->grab_focus(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 11:41:46 +08:00
										 |  |  | void TextEditor::_prepare_edit_menu() { | 
					
						
							|  |  |  | 	const CodeEdit *tx = code_editor->get_text_editor(); | 
					
						
							|  |  |  | 	PopupMenu *popup = edit_menu->get_popup(); | 
					
						
							|  |  |  | 	popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo()); | 
					
						
							|  |  |  | 	popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 20:22:52 +02:00
										 |  |  | void TextEditor::_make_context_menu(bool p_selection, bool p_can_fold, bool p_is_folded, Vector2 p_position) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	context_menu->clear(); | 
					
						
							|  |  |  | 	if (p_selection) { | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); | 
					
						
							|  |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	context_menu->add_separator(); | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	context_menu->add_separator(); | 
					
						
							| 
									
										
										
										
											2022-09-29 04:12:52 +02:00
										 |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); | 
					
						
							|  |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (p_selection) { | 
					
						
							|  |  |  | 		context_menu->add_separator(); | 
					
						
							|  |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE); | 
					
						
							|  |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_can_fold || p_is_folded) { | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 		context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-17 11:41:46 +08:00
										 |  |  | 	const CodeEdit *tx = code_editor->get_text_editor(); | 
					
						
							|  |  |  | 	context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo()); | 
					
						
							|  |  |  | 	context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 17:43:35 +02:00
										 |  |  | 	context_menu->set_position(get_screen_position() + p_position); | 
					
						
							| 
									
										
										
										
											2021-11-20 11:04:57 +03:00
										 |  |  | 	context_menu->reset_size(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	context_menu->popup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-14 12:17:47 +03:00
										 |  |  | void TextEditor::update_toggle_scripts_button() { | 
					
						
							|  |  |  | 	code_editor->update_toggle_scripts_button(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | TextEditor::TextEditor() { | 
					
						
							|  |  |  | 	code_editor = memnew(CodeTextEditor); | 
					
						
							|  |  |  | 	add_child(code_editor); | 
					
						
							| 
									
										
										
										
											2022-02-08 10:14:58 +01:00
										 |  |  | 	code_editor->add_theme_constant_override("separation", 0); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	code_editor->connect("load_theme_settings", callable_mp(this, &TextEditor::_load_theme_settings)); | 
					
						
							|  |  |  | 	code_editor->connect("validate_script", callable_mp(this, &TextEditor::_validate_script)); | 
					
						
							| 
									
										
										
										
											2022-03-18 19:02:57 -05:00
										 |  |  | 	code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); | 
					
						
							| 
									
										
										
										
											2021-09-14 12:17:47 +03:00
										 |  |  | 	code_editor->show_toggle_scripts_button(); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	update_settings(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	code_editor->get_text_editor()->set_context_menu_enabled(false); | 
					
						
							|  |  |  | 	code_editor->get_text_editor()->connect("gui_input", callable_mp(this, &TextEditor::_text_edit_gui_input)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	context_menu = memnew(PopupMenu); | 
					
						
							|  |  |  | 	add_child(context_menu); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	context_menu->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	edit_hb = memnew(HBoxContainer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	search_menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2020-09-17 11:40:00 +10:00
										 |  |  | 	search_menu->set_shortcut_context(this); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_hb->add_child(search_menu); | 
					
						
							|  |  |  | 	search_menu->set_text(TTR("Search")); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	search_menu->set_switch_on_hover(true); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	search_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE); | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 	search_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES); | 
					
						
							| 
									
										
										
										
											2020-02-09 10:10:58 +01:00
										 |  |  | 	search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	edit_menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2020-09-17 11:40:00 +10:00
										 |  |  | 	edit_menu->set_shortcut_context(this); | 
					
						
							| 
									
										
										
										
											2019-08-09 18:55:01 -03:00
										 |  |  | 	edit_hb->add_child(edit_menu); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->set_text(TTR("Edit")); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	edit_menu->set_switch_on_hover(true); | 
					
						
							| 
									
										
										
										
											2021-08-17 11:41:46 +08:00
										 |  |  | 	edit_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_prepare_edit_menu)); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO); | 
					
						
							| 
									
										
										
										
											2021-06-08 17:41:35 +01:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2020-12-07 21:32:00 +10:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN); | 
					
						
							| 
									
										
										
										
											2022-09-29 04:12:52 +02:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							| 
									
										
										
										
											2021-06-29 12:09:07 +02:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 	PopupMenu *convert_case = memnew(PopupMenu); | 
					
						
							|  |  |  | 	convert_case->set_name("convert_case"); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_child(convert_case); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case"); | 
					
						
							|  |  |  | 	convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE); | 
					
						
							|  |  |  | 	convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE); | 
					
						
							|  |  |  | 	convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	convert_case->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	highlighter_menu = memnew(PopupMenu); | 
					
						
							|  |  |  | 	highlighter_menu->set_name("highlighter_menu"); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_child(highlighter_menu); | 
					
						
							|  |  |  | 	edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu"); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	highlighter_menu->connect("id_pressed", callable_mp(this, &TextEditor::_change_syntax_highlighter)); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:08:15 +01:00
										 |  |  | 	Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	plain_highlighter.instantiate(); | 
					
						
							| 
									
										
										
										
											2020-05-03 17:08:15 +01:00
										 |  |  | 	add_syntax_highlighter(plain_highlighter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Ref<EditorStandardSyntaxHighlighter> highlighter; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	highlighter.instantiate(); | 
					
						
							| 
									
										
										
										
											2020-05-03 17:08:15 +01:00
										 |  |  | 	add_syntax_highlighter(highlighter); | 
					
						
							|  |  |  | 	set_syntax_highlighter(plain_highlighter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	MenuButton *goto_menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2020-09-17 11:40:00 +10:00
										 |  |  | 	goto_menu->set_shortcut_context(this); | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	edit_hb->add_child(goto_menu); | 
					
						
							|  |  |  | 	goto_menu->set_text(TTR("Go To")); | 
					
						
							|  |  |  | 	goto_menu->set_switch_on_hover(true); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	goto_menu->get_popup()->connect("id_pressed", callable_mp(this, &TextEditor::_edit_option)); | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE); | 
					
						
							|  |  |  | 	goto_menu->get_popup()->add_separator(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bookmarks_menu = memnew(PopupMenu); | 
					
						
							| 
									
										
										
										
											2019-11-25 13:23:09 +01:00
										 |  |  | 	bookmarks_menu->set_name("Bookmarks"); | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 	goto_menu->get_popup()->add_child(bookmarks_menu); | 
					
						
							|  |  |  | 	goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks"); | 
					
						
							| 
									
										
										
										
											2019-05-21 05:07:48 -03:00
										 |  |  | 	_update_bookmark_list(); | 
					
						
							| 
									
										
										
										
											2020-03-12 09:37:40 -03:00
										 |  |  | 	bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextEditor::_update_bookmark_list)); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:28:45 +01:00
										 |  |  | 	bookmarks_menu->connect("index_pressed", callable_mp(this, &TextEditor::_bookmark_item_pressed)); | 
					
						
							| 
									
										
										
										
											2019-08-09 17:23:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	goto_line_dialog = memnew(GotoLineDialog); | 
					
						
							|  |  |  | 	add_child(goto_line_dialog); | 
					
						
							| 
									
										
										
										
											2019-04-20 01:51:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 15:50:35 +01:00
										 |  |  | 	code_editor->get_text_editor()->set_drag_forwarding(this); | 
					
						
							| 
									
										
										
										
											2018-05-28 16:52:28 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-08 18:49:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 17:30:48 +01:00
										 |  |  | TextEditor::~TextEditor() { | 
					
						
							|  |  |  | 	highlighters.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-08 18:49:49 +02:00
										 |  |  | void TextEditor::validate() { | 
					
						
							|  |  |  | } |