2
0

script_text_editor.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*************************************************************************/
  2. /* script_text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_text_editor.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/script_editor_debugger.h"
  34. #include "os/keyboard.h"
  35. Vector<String> ScriptTextEditor::get_functions() {
  36. String errortxt;
  37. int line = -1, col;
  38. TextEdit *te = code_editor->get_text_edit();
  39. String text = te->get_text();
  40. List<String> fnc;
  41. if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  42. //if valid rewrite functions to latest
  43. functions.clear();
  44. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  45. functions.push_back(E->get());
  46. }
  47. }
  48. return functions;
  49. }
  50. void ScriptTextEditor::apply_code() {
  51. if (script.is_null())
  52. return;
  53. //print_line("applying code");
  54. script->set_source_code(code_editor->get_text_edit()->get_text());
  55. script->update_exports();
  56. }
  57. Ref<Script> ScriptTextEditor::get_edited_script() const {
  58. return script;
  59. }
  60. void ScriptTextEditor::_load_theme_settings() {
  61. TextEdit *text_edit = code_editor->get_text_edit();
  62. text_edit->clear_colors();
  63. /* keyword color */
  64. text_edit->add_color_override("background_color", EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0)));
  65. text_edit->add_color_override("completion_background_color", EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0)));
  66. text_edit->add_color_override("completion_selected_color", EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244")));
  67. text_edit->add_color_override("completion_existing_color", EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf")));
  68. text_edit->add_color_override("completion_scroll_color", EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff")));
  69. text_edit->add_color_override("completion_font_color", EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa")));
  70. text_edit->add_color_override("font_color", EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0)));
  71. text_edit->add_color_override("line_number_color", EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0)));
  72. text_edit->add_color_override("caret_color", EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0)));
  73. text_edit->add_color_override("caret_background_color", EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0)));
  74. text_edit->add_color_override("font_selected_color", EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1)));
  75. text_edit->add_color_override("selection_color", EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1)));
  76. text_edit->add_color_override("brace_mismatch_color", EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2)));
  77. text_edit->add_color_override("current_line_color", EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15)));
  78. text_edit->add_color_override("line_length_guideline_color", EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0)));
  79. text_edit->add_color_override("word_highlighted_color", EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15)));
  80. text_edit->add_color_override("number_color", EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2)));
  81. text_edit->add_color_override("function_color", EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8)));
  82. text_edit->add_color_override("member_variable_color", EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3)));
  83. text_edit->add_color_override("mark_color", EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4)));
  84. text_edit->add_color_override("breakpoint_color", EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2)));
  85. text_edit->add_color_override("search_result_color", EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1)));
  86. text_edit->add_color_override("search_result_border_color", EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)));
  87. text_edit->add_color_override("symbol_color", EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff)));
  88. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 4));
  89. Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2));
  90. List<String> keywords;
  91. script->get_language()->get_reserved_words(&keywords);
  92. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  93. text_edit->add_keyword_color(E->get(), keyword_color);
  94. }
  95. //colorize core types
  96. Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0));
  97. text_edit->add_keyword_color("String", basetype_color);
  98. text_edit->add_keyword_color("Vector2", basetype_color);
  99. text_edit->add_keyword_color("Rect2", basetype_color);
  100. text_edit->add_keyword_color("Transform2D", basetype_color);
  101. text_edit->add_keyword_color("Vector3", basetype_color);
  102. text_edit->add_keyword_color("Rect3", basetype_color);
  103. text_edit->add_keyword_color("Basis", basetype_color);
  104. text_edit->add_keyword_color("Plane", basetype_color);
  105. text_edit->add_keyword_color("Transform", basetype_color);
  106. text_edit->add_keyword_color("Quat", basetype_color);
  107. text_edit->add_keyword_color("Color", basetype_color);
  108. text_edit->add_keyword_color("Object", basetype_color);
  109. text_edit->add_keyword_color("NodePath", basetype_color);
  110. text_edit->add_keyword_color("RID", basetype_color);
  111. text_edit->add_keyword_color("Dictionary", basetype_color);
  112. text_edit->add_keyword_color("Array", basetype_color);
  113. text_edit->add_keyword_color("PoolByteArray", basetype_color);
  114. text_edit->add_keyword_color("PoolIntArray", basetype_color);
  115. text_edit->add_keyword_color("PoolRealArray", basetype_color);
  116. text_edit->add_keyword_color("PoolStringArray", basetype_color);
  117. text_edit->add_keyword_color("PoolVector2Array", basetype_color);
  118. text_edit->add_keyword_color("PoolVector3Array", basetype_color);
  119. text_edit->add_keyword_color("PoolColorArray", basetype_color);
  120. //colorize engine types
  121. Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4));
  122. List<StringName> types;
  123. ClassDB::get_class_list(&types);
  124. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  125. String n = E->get();
  126. if (n.begins_with("_"))
  127. n = n.substr(1, n.length());
  128. text_edit->add_keyword_color(n, type_color);
  129. }
  130. //colorize comments
  131. Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff));
  132. List<String> comments;
  133. script->get_language()->get_comment_delimiters(&comments);
  134. for (List<String>::Element *E = comments.front(); E; E = E->next()) {
  135. String comment = E->get();
  136. String beg = comment.get_slice(" ", 0);
  137. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  138. text_edit->add_color_region(beg, end, comment_color, end == "");
  139. }
  140. //colorize strings
  141. Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff));
  142. List<String> strings;
  143. script->get_language()->get_string_delimiters(&strings);
  144. for (List<String>::Element *E = strings.front(); E; E = E->next()) {
  145. String string = E->get();
  146. String beg = string.get_slice(" ", 0);
  147. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  148. text_edit->add_color_region(beg, end, string_color, end == "");
  149. }
  150. }
  151. void ScriptTextEditor::reload_text() {
  152. ERR_FAIL_COND(script.is_null());
  153. TextEdit *te = code_editor->get_text_edit();
  154. int column = te->cursor_get_column();
  155. int row = te->cursor_get_line();
  156. int h = te->get_h_scroll();
  157. int v = te->get_v_scroll();
  158. te->set_text(script->get_source_code());
  159. te->clear_undo_history();
  160. te->cursor_set_line(row);
  161. te->cursor_set_column(column);
  162. te->set_h_scroll(h);
  163. te->set_v_scroll(v);
  164. te->tag_saved_version();
  165. code_editor->update_line_and_column();
  166. }
  167. void ScriptTextEditor::_notification(int p_what) {
  168. if (p_what == NOTIFICATION_READY) {
  169. //emit_signal("name_changed");
  170. }
  171. }
  172. void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  173. String code = code_editor->get_text_edit()->get_text();
  174. int pos = script->get_language()->find_function(p_function, code);
  175. if (pos == -1) {
  176. //does not exist
  177. code_editor->get_text_edit()->deselect();
  178. pos = code_editor->get_text_edit()->get_line_count() + 2;
  179. String func = script->get_language()->make_function("", p_function, p_args);
  180. //code=code+func;
  181. code_editor->get_text_edit()->cursor_set_line(pos + 1);
  182. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  183. code_editor->get_text_edit()->insert_text_at_cursor("\n\n" + func);
  184. }
  185. code_editor->get_text_edit()->cursor_set_line(pos);
  186. code_editor->get_text_edit()->cursor_set_column(1);
  187. }
  188. bool ScriptTextEditor::show_members_overview() {
  189. return true;
  190. }
  191. void ScriptTextEditor::update_settings() {
  192. code_editor->update_editor_settings();
  193. }
  194. bool ScriptTextEditor::is_unsaved() {
  195. return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
  196. }
  197. Variant ScriptTextEditor::get_edit_state() {
  198. Dictionary state;
  199. state["scroll_pos"] = code_editor->get_text_edit()->get_v_scroll();
  200. state["column"] = code_editor->get_text_edit()->cursor_get_column();
  201. state["row"] = code_editor->get_text_edit()->cursor_get_line();
  202. return state;
  203. }
  204. void ScriptTextEditor::_convert_case(CaseStyle p_case) {
  205. TextEdit *te = code_editor->get_text_edit();
  206. Ref<Script> scr = get_edited_script();
  207. if (scr.is_null()) {
  208. return;
  209. }
  210. if (te->is_selection_active()) {
  211. te->begin_complex_operation();
  212. int begin = te->get_selection_from_line();
  213. int end = te->get_selection_to_line();
  214. int begin_col = te->get_selection_from_column();
  215. int end_col = te->get_selection_to_column();
  216. for (int i = begin; i <= end; i++) {
  217. String new_line = te->get_line(i);
  218. switch (p_case) {
  219. case UPPER: {
  220. new_line = new_line.to_upper();
  221. } break;
  222. case LOWER: {
  223. new_line = new_line.to_lower();
  224. } break;
  225. case CAPITALIZE: {
  226. new_line = new_line.capitalize();
  227. } break;
  228. }
  229. if (i == begin) {
  230. new_line = te->get_line(i).left(begin_col) + new_line.right(begin_col);
  231. }
  232. if (i == end) {
  233. new_line = new_line.left(end_col) + te->get_line(i).right(end_col);
  234. }
  235. te->set_line(i, new_line);
  236. }
  237. te->end_complex_operation();
  238. }
  239. }
  240. void ScriptTextEditor::trim_trailing_whitespace() {
  241. TextEdit *tx = code_editor->get_text_edit();
  242. bool trimed_whitespace = false;
  243. for (int i = 0; i < tx->get_line_count(); i++) {
  244. String line = tx->get_line(i);
  245. if (line.ends_with(" ") || line.ends_with("\t")) {
  246. if (!trimed_whitespace) {
  247. tx->begin_complex_operation();
  248. trimed_whitespace = true;
  249. }
  250. int end = 0;
  251. for (int j = line.length() - 1; j > -1; j--) {
  252. if (line[j] != ' ' && line[j] != '\t') {
  253. end = j + 1;
  254. break;
  255. }
  256. }
  257. tx->set_line(i, line.substr(0, end));
  258. }
  259. }
  260. if (trimed_whitespace) {
  261. tx->end_complex_operation();
  262. tx->update();
  263. }
  264. }
  265. void ScriptTextEditor::convert_indent_to_spaces() {
  266. TextEdit *tx = code_editor->get_text_edit();
  267. Ref<Script> scr = get_edited_script();
  268. if (scr.is_null()) {
  269. return;
  270. }
  271. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  272. String indent = "";
  273. for (int i = 0; i < indent_size; i++) {
  274. indent += " ";
  275. }
  276. int cursor_line = tx->cursor_get_line();
  277. int cursor_column = tx->cursor_get_column();
  278. bool changed_indentation = false;
  279. for (int i = 0; i < tx->get_line_count(); i++) {
  280. String line = tx->get_line(i);
  281. if (line.length() <= 0) {
  282. continue;
  283. }
  284. int j = 0;
  285. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  286. if (line[j] == '\t') {
  287. if (!changed_indentation) {
  288. tx->begin_complex_operation();
  289. changed_indentation = true;
  290. }
  291. if (cursor_line == i && cursor_column > j) {
  292. cursor_column += indent_size - 1;
  293. }
  294. line = line.left(j) + indent + line.right(j + 1);
  295. }
  296. j++;
  297. }
  298. if (changed_indentation) {
  299. tx->set_line(i, line);
  300. }
  301. }
  302. if (changed_indentation) {
  303. tx->cursor_set_column(cursor_column);
  304. tx->end_complex_operation();
  305. tx->update();
  306. }
  307. }
  308. void ScriptTextEditor::convert_indent_to_tabs() {
  309. TextEdit *tx = code_editor->get_text_edit();
  310. Ref<Script> scr = get_edited_script();
  311. if (scr.is_null()) {
  312. return;
  313. }
  314. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  315. indent_size -= 1;
  316. int cursor_line = tx->cursor_get_line();
  317. int cursor_column = tx->cursor_get_column();
  318. bool changed_indentation = false;
  319. for (int i = 0; i < tx->get_line_count(); i++) {
  320. String line = tx->get_line(i);
  321. if (line.length() <= 0) {
  322. continue;
  323. }
  324. int j = 0;
  325. int space_count = -1;
  326. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  327. if (line[j] != '\t') {
  328. space_count++;
  329. if (space_count == indent_size) {
  330. if (!changed_indentation) {
  331. tx->begin_complex_operation();
  332. changed_indentation = true;
  333. }
  334. if (cursor_line == i && cursor_column > j) {
  335. cursor_column -= indent_size;
  336. }
  337. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  338. j = 0;
  339. space_count = -1;
  340. }
  341. } else {
  342. space_count = -1;
  343. }
  344. j++;
  345. }
  346. if (changed_indentation) {
  347. tx->set_line(i, line);
  348. }
  349. }
  350. if (changed_indentation) {
  351. tx->cursor_set_column(cursor_column);
  352. tx->end_complex_operation();
  353. tx->update();
  354. }
  355. }
  356. void ScriptTextEditor::tag_saved_version() {
  357. code_editor->get_text_edit()->tag_saved_version();
  358. }
  359. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  360. code_editor->get_text_edit()->call_deferred("cursor_set_line", p_line);
  361. }
  362. void ScriptTextEditor::ensure_focus() {
  363. code_editor->get_text_edit()->grab_focus();
  364. }
  365. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  366. Dictionary state = p_state;
  367. code_editor->get_text_edit()->set_v_scroll(state["scroll_pos"]);
  368. code_editor->get_text_edit()->cursor_set_column(state["column"]);
  369. code_editor->get_text_edit()->cursor_set_line(state["row"]);
  370. code_editor->get_text_edit()->grab_focus();
  371. //int scroll_pos;
  372. //int cursor_column;
  373. //int cursor_row;
  374. }
  375. String ScriptTextEditor::get_name() {
  376. String name;
  377. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  378. name = script->get_path().get_file();
  379. if (is_unsaved()) {
  380. name += "(*)";
  381. }
  382. } else if (script->get_name() != "")
  383. name = script->get_name();
  384. else
  385. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  386. return name;
  387. }
  388. Ref<Texture> ScriptTextEditor::get_icon() {
  389. if (get_parent_control() && get_parent_control()->has_icon(script->get_class(), "EditorIcons")) {
  390. return get_parent_control()->get_icon(script->get_class(), "EditorIcons");
  391. }
  392. return Ref<Texture>();
  393. }
  394. void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
  395. ERR_FAIL_COND(!script.is_null());
  396. script = p_script;
  397. _load_theme_settings();
  398. code_editor->get_text_edit()->set_text(script->get_source_code());
  399. code_editor->get_text_edit()->clear_undo_history();
  400. code_editor->get_text_edit()->tag_saved_version();
  401. emit_signal("name_changed");
  402. code_editor->update_line_and_column();
  403. }
  404. void ScriptTextEditor::_validate_script() {
  405. String errortxt;
  406. int line = -1, col;
  407. TextEdit *te = code_editor->get_text_edit();
  408. String text = te->get_text();
  409. List<String> fnc;
  410. if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  411. String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
  412. code_editor->set_error(error_text);
  413. } else {
  414. code_editor->set_error("");
  415. line = -1;
  416. if (!script->is_tool()) {
  417. script->set_source_code(text);
  418. script->update_exports();
  419. //script->reload(); //will update all the variables in property editors
  420. }
  421. functions.clear();
  422. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  423. functions.push_back(E->get());
  424. }
  425. }
  426. line--;
  427. for (int i = 0; i < te->get_line_count(); i++) {
  428. te->set_line_as_marked(i, line == i);
  429. }
  430. emit_signal("name_changed");
  431. emit_signal("edited_script_changed");
  432. }
  433. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  434. if (p_current->get_owner() != p_base && p_base != p_current)
  435. return NULL;
  436. Ref<Script> c = p_current->get_script();
  437. if (c == p_script)
  438. return p_current;
  439. for (int i = 0; i < p_current->get_child_count(); i++) {
  440. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  441. if (found)
  442. return found;
  443. }
  444. return NULL;
  445. }
  446. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
  447. if (p_current->get_owner() != p_base && p_base != p_current)
  448. return;
  449. Ref<Script> c = p_current->get_script();
  450. if (c.is_valid())
  451. r_scripts.insert(c);
  452. for (int i = 0; i < p_current->get_child_count(); i++) {
  453. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  454. }
  455. }
  456. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  457. if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  458. return;
  459. ERR_FAIL_COND(!get_tree());
  460. Set<Ref<Script> > scripts;
  461. Node *base = get_tree()->get_edited_scene_root();
  462. if (base) {
  463. _find_changed_scripts_for_external_editor(base, base, scripts);
  464. }
  465. for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
  466. Ref<Script> script = E->get();
  467. if (p_for_script.is_valid() && p_for_script != script)
  468. continue;
  469. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  470. continue; //internal script, who cares, though weird
  471. }
  472. uint64_t last_date = script->get_last_modified_time();
  473. uint64_t date = FileAccess::get_modified_time(script->get_path());
  474. if (last_date != date) {
  475. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  476. ERR_CONTINUE(!rel_script.is_valid());
  477. script->set_source_code(rel_script->get_source_code());
  478. script->set_last_modified_time(rel_script->get_last_modified_time());
  479. script->update_exports();
  480. }
  481. }
  482. }
  483. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<String> *r_options, bool &r_force) {
  484. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  485. ste->_code_complete_script(p_code, r_options, r_force);
  486. }
  487. void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> *r_options, bool &r_force) {
  488. if (color_panel->is_visible_in_tree()) return;
  489. Node *base = get_tree()->get_edited_scene_root();
  490. if (base) {
  491. base = _find_node_for_script(base, base, script);
  492. }
  493. String hint;
  494. Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, r_force, hint);
  495. if (err == OK && hint != "") {
  496. code_editor->get_text_edit()->set_code_hint(hint);
  497. }
  498. }
  499. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  500. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_edit()->is_line_set_as_breakpoint(p_row));
  501. }
  502. static void swap_lines(TextEdit *tx, int line1, int line2) {
  503. String tmp = tx->get_line(line1);
  504. String tmp2 = tx->get_line(line2);
  505. tx->set_line(line2, tmp);
  506. tx->set_line(line1, tmp2);
  507. tx->cursor_set_line(line2);
  508. }
  509. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  510. Node *base = get_tree()->get_edited_scene_root();
  511. if (base) {
  512. base = _find_node_for_script(base, base, script);
  513. }
  514. ScriptLanguage::LookupResult result;
  515. if (p_symbol.is_resource_file()) {
  516. List<String> scene_extensions;
  517. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  518. if (scene_extensions.find(p_symbol.get_extension())) {
  519. EditorNode::get_singleton()->load_scene(p_symbol);
  520. } else {
  521. EditorNode::get_singleton()->load_resource(p_symbol);
  522. }
  523. } else if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path().get_base_dir(), base, result) == OK) {
  524. _goto_line(p_row);
  525. switch (result.type) {
  526. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  527. if (result.script.is_valid()) {
  528. emit_signal("request_open_script_at_line", result.script, result.location - 1);
  529. } else {
  530. emit_signal("request_save_history");
  531. _goto_line(result.location - 1);
  532. }
  533. } break;
  534. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  535. emit_signal("go_to_help", "class_name:" + result.class_name);
  536. } break;
  537. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  538. StringName cname = result.class_name;
  539. bool success;
  540. while (true) {
  541. ClassDB::get_integer_constant(cname, result.class_member, &success);
  542. if (success) {
  543. result.class_name = cname;
  544. cname = ClassDB::get_parent_class(cname);
  545. } else {
  546. break;
  547. }
  548. }
  549. emit_signal("go_to_help", "class_constant:" + result.class_name + ":" + result.class_member);
  550. } break;
  551. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  552. emit_signal("go_to_help", "class_property:" + result.class_name + ":" + result.class_member);
  553. } break;
  554. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  555. StringName cname = result.class_name;
  556. while (true) {
  557. if (ClassDB::has_method(cname, result.class_member)) {
  558. result.class_name = cname;
  559. cname = ClassDB::get_parent_class(cname);
  560. } else {
  561. break;
  562. }
  563. }
  564. emit_signal("go_to_help", "class_method:" + result.class_name + ":" + result.class_member);
  565. } break;
  566. }
  567. }
  568. }
  569. void ScriptTextEditor::_edit_option(int p_op) {
  570. switch (p_op) {
  571. case EDIT_UNDO: {
  572. code_editor->get_text_edit()->undo();
  573. code_editor->get_text_edit()->call_deferred("grab_focus");
  574. } break;
  575. case EDIT_REDO: {
  576. code_editor->get_text_edit()->redo();
  577. code_editor->get_text_edit()->call_deferred("grab_focus");
  578. } break;
  579. case EDIT_CUT: {
  580. code_editor->get_text_edit()->cut();
  581. code_editor->get_text_edit()->call_deferred("grab_focus");
  582. } break;
  583. case EDIT_COPY: {
  584. code_editor->get_text_edit()->copy();
  585. code_editor->get_text_edit()->call_deferred("grab_focus");
  586. } break;
  587. case EDIT_PASTE: {
  588. code_editor->get_text_edit()->paste();
  589. code_editor->get_text_edit()->call_deferred("grab_focus");
  590. } break;
  591. case EDIT_SELECT_ALL: {
  592. code_editor->get_text_edit()->select_all();
  593. code_editor->get_text_edit()->call_deferred("grab_focus");
  594. } break;
  595. case EDIT_MOVE_LINE_UP: {
  596. TextEdit *tx = code_editor->get_text_edit();
  597. Ref<Script> scr = script;
  598. if (scr.is_null())
  599. return;
  600. tx->begin_complex_operation();
  601. if (tx->is_selection_active()) {
  602. int from_line = tx->get_selection_from_line();
  603. int from_col = tx->get_selection_from_column();
  604. int to_line = tx->get_selection_to_line();
  605. int to_column = tx->get_selection_to_column();
  606. for (int i = from_line; i <= to_line; i++) {
  607. int line_id = i;
  608. int next_id = i - 1;
  609. if (line_id == 0 || next_id < 0)
  610. return;
  611. swap_lines(tx, line_id, next_id);
  612. }
  613. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  614. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  615. tx->select(from_line_up, from_col, to_line_up, to_column);
  616. } else {
  617. int line_id = tx->cursor_get_line();
  618. int next_id = line_id - 1;
  619. if (line_id == 0 || next_id < 0)
  620. return;
  621. swap_lines(tx, line_id, next_id);
  622. }
  623. tx->end_complex_operation();
  624. tx->update();
  625. } break;
  626. case EDIT_MOVE_LINE_DOWN: {
  627. TextEdit *tx = code_editor->get_text_edit();
  628. Ref<Script> scr = get_edited_script();
  629. if (scr.is_null())
  630. return;
  631. tx->begin_complex_operation();
  632. if (tx->is_selection_active()) {
  633. int from_line = tx->get_selection_from_line();
  634. int from_col = tx->get_selection_from_column();
  635. int to_line = tx->get_selection_to_line();
  636. int to_column = tx->get_selection_to_column();
  637. for (int i = to_line; i >= from_line; i--) {
  638. int line_id = i;
  639. int next_id = i + 1;
  640. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  641. return;
  642. swap_lines(tx, line_id, next_id);
  643. }
  644. int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
  645. int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
  646. tx->select(from_line_down, from_col, to_line_down, to_column);
  647. } else {
  648. int line_id = tx->cursor_get_line();
  649. int next_id = line_id + 1;
  650. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  651. return;
  652. swap_lines(tx, line_id, next_id);
  653. }
  654. tx->end_complex_operation();
  655. tx->update();
  656. } break;
  657. case EDIT_INDENT_LEFT: {
  658. TextEdit *tx = code_editor->get_text_edit();
  659. Ref<Script> scr = get_edited_script();
  660. if (scr.is_null())
  661. return;
  662. tx->begin_complex_operation();
  663. if (tx->is_selection_active()) {
  664. tx->indent_selection_left();
  665. } else {
  666. int begin = tx->cursor_get_line();
  667. String line_text = tx->get_line(begin);
  668. // begins with tab
  669. if (line_text.begins_with("\t")) {
  670. line_text = line_text.substr(1, line_text.length());
  671. tx->set_line(begin, line_text);
  672. }
  673. // begins with 4 spaces
  674. else if (line_text.begins_with(" ")) {
  675. line_text = line_text.substr(4, line_text.length());
  676. tx->set_line(begin, line_text);
  677. }
  678. }
  679. tx->end_complex_operation();
  680. tx->update();
  681. //tx->deselect();
  682. } break;
  683. case EDIT_INDENT_RIGHT: {
  684. TextEdit *tx = code_editor->get_text_edit();
  685. Ref<Script> scr = get_edited_script();
  686. if (scr.is_null())
  687. return;
  688. tx->begin_complex_operation();
  689. if (tx->is_selection_active()) {
  690. tx->indent_selection_right();
  691. } else {
  692. int begin = tx->cursor_get_line();
  693. String line_text = tx->get_line(begin);
  694. line_text = '\t' + line_text;
  695. tx->set_line(begin, line_text);
  696. }
  697. tx->end_complex_operation();
  698. tx->update();
  699. //tx->deselect();
  700. } break;
  701. case EDIT_DELETE_LINE: {
  702. TextEdit *tx = code_editor->get_text_edit();
  703. Ref<Script> scr = get_edited_script();
  704. if (scr.is_null())
  705. return;
  706. tx->begin_complex_operation();
  707. int line = tx->cursor_get_line();
  708. tx->set_line(tx->cursor_get_line(), "");
  709. tx->backspace_at_cursor();
  710. tx->cursor_set_line(line);
  711. tx->end_complex_operation();
  712. } break;
  713. case EDIT_CLONE_DOWN: {
  714. TextEdit *tx = code_editor->get_text_edit();
  715. Ref<Script> scr = get_edited_script();
  716. if (scr.is_null())
  717. return;
  718. int from_line = tx->cursor_get_line();
  719. int to_line = tx->cursor_get_line();
  720. int column = tx->cursor_get_column();
  721. if (tx->is_selection_active()) {
  722. from_line = tx->get_selection_from_line();
  723. to_line = tx->get_selection_to_line();
  724. column = tx->cursor_get_column();
  725. }
  726. int next_line = to_line + 1;
  727. tx->begin_complex_operation();
  728. for (int i = from_line; i <= to_line; i++) {
  729. if (i >= tx->get_line_count() - 1) {
  730. tx->set_line(i, tx->get_line(i) + "\n");
  731. }
  732. String line_clone = tx->get_line(i);
  733. tx->insert_at(line_clone, next_line);
  734. next_line++;
  735. }
  736. tx->cursor_set_column(column);
  737. if (tx->is_selection_active()) {
  738. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  739. }
  740. tx->end_complex_operation();
  741. tx->update();
  742. } break;
  743. case EDIT_TOGGLE_COMMENT: {
  744. TextEdit *tx = code_editor->get_text_edit();
  745. Ref<Script> scr = get_edited_script();
  746. if (scr.is_null())
  747. return;
  748. tx->begin_complex_operation();
  749. if (tx->is_selection_active()) {
  750. int begin = tx->get_selection_from_line();
  751. int end = tx->get_selection_to_line();
  752. // End of selection ends on the first column of the last line, ignore it.
  753. if (tx->get_selection_to_column() == 0)
  754. end -= 1;
  755. for (int i = begin; i <= end; i++) {
  756. String line_text = tx->get_line(i);
  757. if (line_text.begins_with("#"))
  758. line_text = line_text.substr(1, line_text.length());
  759. else
  760. line_text = "#" + line_text;
  761. tx->set_line(i, line_text);
  762. }
  763. } else {
  764. int begin = tx->cursor_get_line();
  765. String line_text = tx->get_line(begin);
  766. if (line_text.begins_with("#"))
  767. line_text = line_text.substr(1, line_text.length());
  768. else
  769. line_text = "#" + line_text;
  770. tx->set_line(begin, line_text);
  771. }
  772. tx->end_complex_operation();
  773. tx->update();
  774. //tx->deselect();
  775. } break;
  776. case EDIT_COMPLETE: {
  777. code_editor->get_text_edit()->query_code_comple();
  778. } break;
  779. case EDIT_AUTO_INDENT: {
  780. TextEdit *te = code_editor->get_text_edit();
  781. String text = te->get_text();
  782. Ref<Script> scr = get_edited_script();
  783. if (scr.is_null())
  784. return;
  785. te->begin_complex_operation();
  786. int begin, end;
  787. if (te->is_selection_active()) {
  788. begin = te->get_selection_from_line();
  789. end = te->get_selection_to_line();
  790. // ignore if the cursor is not past the first column
  791. if (te->get_selection_to_column() == 0) {
  792. end--;
  793. }
  794. } else {
  795. begin = 0;
  796. end = te->get_line_count() - 1;
  797. }
  798. scr->get_language()->auto_indent_code(text, begin, end);
  799. Vector<String> lines = text.split("\n");
  800. for (int i = begin; i <= end; ++i) {
  801. te->set_line(i, lines[i]);
  802. }
  803. te->end_complex_operation();
  804. } break;
  805. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  806. trim_trailing_whitespace();
  807. } break;
  808. case EDIT_CONVERT_INDENT_TO_SPACES: {
  809. convert_indent_to_spaces();
  810. } break;
  811. case EDIT_CONVERT_INDENT_TO_TABS: {
  812. convert_indent_to_tabs();
  813. } break;
  814. case EDIT_PICK_COLOR: {
  815. color_panel->popup();
  816. } break;
  817. case EDIT_TO_UPPERCASE: {
  818. _convert_case(UPPER);
  819. } break;
  820. case EDIT_TO_LOWERCASE: {
  821. _convert_case(LOWER);
  822. } break;
  823. case EDIT_CAPITALIZE: {
  824. _convert_case(CAPITALIZE);
  825. } break;
  826. case SEARCH_FIND: {
  827. code_editor->get_find_replace_bar()->popup_search();
  828. } break;
  829. case SEARCH_FIND_NEXT: {
  830. code_editor->get_find_replace_bar()->search_next();
  831. } break;
  832. case SEARCH_FIND_PREV: {
  833. code_editor->get_find_replace_bar()->search_prev();
  834. } break;
  835. case SEARCH_REPLACE: {
  836. code_editor->get_find_replace_bar()->popup_replace();
  837. } break;
  838. case SEARCH_LOCATE_FUNCTION: {
  839. quick_open->popup(get_functions());
  840. } break;
  841. case SEARCH_GOTO_LINE: {
  842. goto_line_dialog->popup_find_line(code_editor->get_text_edit());
  843. } break;
  844. case DEBUG_TOGGLE_BREAKPOINT: {
  845. int line = code_editor->get_text_edit()->cursor_get_line();
  846. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  847. code_editor->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  848. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  849. } break;
  850. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  851. List<int> bpoints;
  852. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  853. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  854. int line = E->get();
  855. bool dobreak = !code_editor->get_text_edit()->is_line_set_as_breakpoint(line);
  856. code_editor->get_text_edit()->set_line_as_breakpoint(line, dobreak);
  857. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  858. }
  859. }
  860. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  861. List<int> bpoints;
  862. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  863. if (bpoints.size() <= 0) {
  864. return;
  865. }
  866. int line = code_editor->get_text_edit()->cursor_get_line();
  867. // wrap around
  868. if (line >= bpoints[bpoints.size() - 1]) {
  869. code_editor->get_text_edit()->cursor_set_line(bpoints[0]);
  870. } else {
  871. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  872. int bline = E->get();
  873. if (bline > line) {
  874. code_editor->get_text_edit()->cursor_set_line(bline);
  875. return;
  876. }
  877. }
  878. }
  879. } break;
  880. case DEBUG_GOTO_PREV_BREAKPOINT: {
  881. List<int> bpoints;
  882. code_editor->get_text_edit()->get_breakpoints(&bpoints);
  883. if (bpoints.size() <= 0) {
  884. return;
  885. }
  886. int line = code_editor->get_text_edit()->cursor_get_line();
  887. // wrap around
  888. if (line <= bpoints[0]) {
  889. code_editor->get_text_edit()->cursor_set_line(bpoints[bpoints.size() - 1]);
  890. } else {
  891. for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
  892. int bline = E->get();
  893. if (bline < line) {
  894. code_editor->get_text_edit()->cursor_set_line(bline);
  895. return;
  896. }
  897. }
  898. }
  899. } break;
  900. case HELP_CONTEXTUAL: {
  901. String text = code_editor->get_text_edit()->get_selection_text();
  902. if (text == "")
  903. text = code_editor->get_text_edit()->get_word_under_cursor();
  904. if (text != "") {
  905. emit_signal("request_help_search", text);
  906. }
  907. } break;
  908. }
  909. }
  910. void ScriptTextEditor::_bind_methods() {
  911. ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
  912. ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
  913. ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
  914. ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
  915. ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
  916. ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
  917. ClassDB::bind_method("_text_edit_gui_input", &ScriptTextEditor::_text_edit_gui_input);
  918. ClassDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  919. ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
  920. ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
  921. ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
  922. }
  923. Control *ScriptTextEditor::get_edit_menu() {
  924. return edit_hb;
  925. }
  926. void ScriptTextEditor::clear_edit_menu() {
  927. memdelete(edit_hb);
  928. }
  929. void ScriptTextEditor::reload(bool p_soft) {
  930. TextEdit *te = code_editor->get_text_edit();
  931. Ref<Script> scr = get_edited_script();
  932. if (scr.is_null())
  933. return;
  934. scr->set_source_code(te->get_text());
  935. bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
  936. scr->get_language()->reload_tool_script(scr, soft);
  937. }
  938. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  939. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  940. }
  941. void ScriptTextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  942. code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
  943. }
  944. void ScriptTextEditor::set_debugger_active(bool p_active) {
  945. }
  946. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  947. return Variant();
  948. }
  949. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  950. Dictionary d = p_data;
  951. if (d.has("type") &&
  952. (
  953. String(d["type"]) == "resource" ||
  954. String(d["type"]) == "files" ||
  955. String(d["type"]) == "nodes")) {
  956. return true;
  957. }
  958. return false;
  959. }
  960. #ifdef TOOLS_ENABLED
  961. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  962. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  963. return NULL;
  964. Ref<Script> scr = p_current_node->get_script();
  965. if (scr.is_valid() && scr == script)
  966. return p_current_node;
  967. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  968. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  969. if (n)
  970. return n;
  971. }
  972. return NULL;
  973. }
  974. #else
  975. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  976. return NULL;
  977. }
  978. #endif
  979. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  980. Dictionary d = p_data;
  981. if (d.has("type") && String(d["type"]) == "resource") {
  982. Ref<Resource> res = d["resource"];
  983. if (!res.is_valid()) {
  984. return;
  985. }
  986. if (res->get_path().is_resource_file()) {
  987. EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
  988. return;
  989. }
  990. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  991. }
  992. if (d.has("type") && String(d["type"]) == "files") {
  993. Array files = d["files"];
  994. String text_to_drop;
  995. for (int i = 0; i < files.size(); i++) {
  996. if (i > 0)
  997. text_to_drop += ",";
  998. text_to_drop += "\"" + String(files[i]).c_escape() + "\"";
  999. }
  1000. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  1001. }
  1002. if (d.has("type") && String(d["type"]) == "nodes") {
  1003. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1004. if (!sn) {
  1005. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1006. return;
  1007. }
  1008. Array nodes = d["nodes"];
  1009. String text_to_drop;
  1010. for (int i = 0; i < nodes.size(); i++) {
  1011. if (i > 0)
  1012. text_to_drop += ",";
  1013. NodePath np = nodes[i];
  1014. Node *node = get_node(np);
  1015. if (!node) {
  1016. continue;
  1017. }
  1018. String path = sn->get_path_to(node);
  1019. text_to_drop += "\"" + path.c_escape() + "\"";
  1020. }
  1021. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  1022. }
  1023. }
  1024. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  1025. Ref<InputEventMouseButton> mb = ev;
  1026. if (mb.is_valid()) {
  1027. if (mb->get_button_index() == BUTTON_RIGHT && !mb->is_pressed()) {
  1028. int col, row;
  1029. TextEdit *tx = code_editor->get_text_edit();
  1030. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  1031. Vector2 mpos = mb->get_global_position() - tx->get_global_position();
  1032. bool have_selection = (tx->get_selection_text().length() > 0);
  1033. bool have_color = (tx->get_word_at_pos(mpos) == "Color");
  1034. if (have_color) {
  1035. String line = tx->get_line(row);
  1036. color_line = row;
  1037. int begin = 0;
  1038. int end = 0;
  1039. bool valid = false;
  1040. for (int i = col; i < line.length(); i++) {
  1041. if (line[i] == '(') {
  1042. begin = i;
  1043. continue;
  1044. } else if (line[i] == ')') {
  1045. end = i + 1;
  1046. valid = true;
  1047. break;
  1048. }
  1049. }
  1050. if (valid) {
  1051. color_args = line.substr(begin, end - begin);
  1052. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  1053. Vector<float> color = stripped.split_floats(",");
  1054. if (color.size() > 2) {
  1055. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1056. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1057. }
  1058. color_panel->set_position(get_global_transform().xform(get_local_mouse_pos()));
  1059. } else {
  1060. have_color = false;
  1061. }
  1062. }
  1063. _make_context_menu(have_selection, have_color);
  1064. }
  1065. }
  1066. }
  1067. void ScriptTextEditor::_color_changed(const Color &p_color) {
  1068. String new_args;
  1069. if (p_color.a == 1.0f) {
  1070. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
  1071. } else {
  1072. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
  1073. }
  1074. String line = code_editor->get_text_edit()->get_line(color_line);
  1075. String new_line = line.replace(color_args, new_args);
  1076. color_args = new_args;
  1077. code_editor->get_text_edit()->set_line(color_line, new_line);
  1078. }
  1079. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) {
  1080. context_menu->clear();
  1081. if (p_selection) {
  1082. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1083. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1084. }
  1085. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1086. context_menu->add_separator();
  1087. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1088. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1089. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1090. if (p_selection) {
  1091. context_menu->add_separator();
  1092. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1093. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1094. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1095. }
  1096. if (p_color) {
  1097. context_menu->add_separator();
  1098. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  1099. }
  1100. context_menu->set_position(get_global_transform().xform(get_local_mouse_pos()));
  1101. context_menu->set_size(Vector2(1, 1));
  1102. context_menu->popup();
  1103. }
  1104. ScriptTextEditor::ScriptTextEditor() {
  1105. code_editor = memnew(CodeTextEditor);
  1106. add_child(code_editor);
  1107. code_editor->add_constant_override("separation", 0);
  1108. code_editor->set_area_as_parent_rect();
  1109. code_editor->connect("validate_script", this, "_validate_script");
  1110. code_editor->connect("load_theme_settings", this, "_load_theme_settings");
  1111. code_editor->set_code_complete_func(_code_complete_scripts, this);
  1112. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  1113. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  1114. update_settings();
  1115. code_editor->get_text_edit()->set_callhint_settings(
  1116. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1117. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  1118. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  1119. code_editor->get_text_edit()->set_context_menu_enabled(false);
  1120. code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  1121. context_menu = memnew(PopupMenu);
  1122. add_child(context_menu);
  1123. context_menu->connect("id_pressed", this, "_edit_option");
  1124. color_panel = memnew(PopupPanel);
  1125. add_child(color_panel);
  1126. color_picker = memnew(ColorPicker);
  1127. color_panel->add_child(color_picker);
  1128. color_picker->connect("color_changed", this, "_color_changed");
  1129. edit_hb = memnew(HBoxContainer);
  1130. edit_menu = memnew(MenuButton);
  1131. edit_menu->set_text(TTR("Edit"));
  1132. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1133. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1134. edit_menu->get_popup()->add_separator();
  1135. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1136. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1137. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1138. edit_menu->get_popup()->add_separator();
  1139. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1140. edit_menu->get_popup()->add_separator();
  1141. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1142. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1143. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1144. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1145. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  1146. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1147. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  1148. edit_menu->get_popup()->add_separator();
  1149. #ifdef OSX_ENABLED
  1150. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1151. #else
  1152. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1153. #endif
  1154. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1155. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  1156. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  1157. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1158. edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1159. edit_menu->get_popup()->add_separator();
  1160. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  1161. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1162. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  1163. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  1164. edit_menu->get_popup()->add_separator();
  1165. PopupMenu *convert_case = memnew(PopupMenu);
  1166. convert_case->set_name("convert_case");
  1167. edit_menu->get_popup()->add_child(convert_case);
  1168. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  1169. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  1170. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  1171. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  1172. convert_case->connect("id_pressed", this, "_edit_option");
  1173. search_menu = memnew(MenuButton);
  1174. edit_hb->add_child(search_menu);
  1175. search_menu->set_text(TTR("Search"));
  1176. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1177. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1178. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1179. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1180. search_menu->get_popup()->add_separator();
  1181. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1182. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1183. search_menu->get_popup()->add_separator();
  1184. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1185. search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1186. edit_hb->add_child(edit_menu);
  1187. quick_open = memnew(ScriptEditorQuickOpen);
  1188. add_child(quick_open);
  1189. quick_open->connect("goto_line", this, "_goto_line");
  1190. goto_line_dialog = memnew(GotoLineDialog);
  1191. add_child(goto_line_dialog);
  1192. code_editor->get_text_edit()->set_drag_forwarding(this);
  1193. }
  1194. static ScriptEditorBase *create_editor(const Ref<Script> &p_script) {
  1195. if (p_script->has_source_code()) {
  1196. return memnew(ScriptTextEditor);
  1197. }
  1198. return NULL;
  1199. }
  1200. void ScriptTextEditor::register_editor() {
  1201. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z);
  1202. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y);
  1203. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  1204. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  1205. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  1206. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
  1207. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
  1208. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
  1209. ED_SHORTCUT("script_text_editor/delete_line", TTR("Delete Line"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_K);
  1210. //leave these at zero, same can be accomplished with tab/shift-tab, including selection
  1211. //the next/previous in history shortcut in this case makes a lot more sene.
  1212. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
  1213. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
  1214. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K);
  1215. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_B);
  1216. #ifdef OSX_ENABLED
  1217. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE);
  1218. #else
  1219. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
  1220. #endif
  1221. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T);
  1222. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent To Spaces"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_Y);
  1223. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent To Tabs"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_X);
  1224. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
  1225. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1226. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_F9);
  1227. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD);
  1228. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA);
  1229. ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Convert To Uppercase"), KEY_MASK_SHIFT | KEY_F4);
  1230. ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Convert To Lowercase"), KEY_MASK_SHIFT | KEY_F3);
  1231. ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F2);
  1232. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F);
  1233. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1234. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);
  1235. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R);
  1236. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_F);
  1237. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L);
  1238. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT | KEY_F1);
  1239. ScriptEditor::register_create_script_editor_function(create_editor);
  1240. }