script_text_editor.cpp 51 KB

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