gdscript_editor.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "gdscript.h"
  31. #include "core/engine.h"
  32. #include "editor/editor_settings.h"
  33. #include "gdscript_compiler.h"
  34. #include "global_constants.h"
  35. #include "os/file_access.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_file_system.h"
  38. #include "editor/editor_settings.h"
  39. #include "engine.h"
  40. #endif
  41. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  42. p_delimiters->push_back("#");
  43. p_delimiters->push_back("\"\"\" \"\"\"");
  44. }
  45. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  46. p_delimiters->push_back("\" \"");
  47. p_delimiters->push_back("' '");
  48. }
  49. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  50. String _template = "extends %BASE%\n"
  51. "\n"
  52. "# Declare member variables here. Examples:\n"
  53. "# var a = 2\n"
  54. "# var b = \"text\"\n"
  55. "\n"
  56. "# Called when the node enters the scene tree for the first time.\n"
  57. "func _ready():\n"
  58. "%TS%pass # Replace with function body.\n"
  59. "\n"
  60. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  61. "#func _process(delta):\n"
  62. "#%TS%pass\n";
  63. _template = _template.replace("%BASE%", p_base_class_name);
  64. _template = _template.replace("%TS%", _get_indentation());
  65. Ref<GDScript> script;
  66. script.instance();
  67. script->set_source_code(_template);
  68. return script;
  69. }
  70. bool GDScriptLanguage::is_using_templates() {
  71. return true;
  72. }
  73. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  74. String src = p_script->get_source_code();
  75. src = src.replace("%BASE%", p_base_class_name);
  76. src = src.replace("%TS%", _get_indentation());
  77. p_script->set_source_code(src);
  78. }
  79. bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const {
  80. GDScriptParser parser;
  81. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path);
  82. if (err) {
  83. r_line_error = parser.get_error_line();
  84. r_col_error = parser.get_error_column();
  85. r_test_error = parser.get_error();
  86. return false;
  87. } else {
  88. const GDScriptParser::Node *root = parser.get_parse_tree();
  89. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
  90. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  91. Map<int, String> funcs;
  92. for (int i = 0; i < cl->functions.size(); i++) {
  93. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  94. }
  95. for (int i = 0; i < cl->static_functions.size(); i++) {
  96. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  97. }
  98. for (int i = 0; i < cl->subclasses.size(); i++) {
  99. for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
  100. funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + String(cl->subclasses[i]->functions[j]->name);
  101. }
  102. }
  103. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  104. r_functions->push_back(E->get() + ":" + itos(E->key()));
  105. }
  106. }
  107. return true;
  108. }
  109. bool GDScriptLanguage::has_named_classes() const {
  110. return false;
  111. }
  112. bool GDScriptLanguage::supports_builtin_mode() const {
  113. return true;
  114. }
  115. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  116. GDScriptTokenizerText tokenizer;
  117. tokenizer.set_code(p_code);
  118. int indent = 0;
  119. while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
  120. if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
  121. indent = tokenizer.get_token_line_indent();
  122. }
  123. //print_line("TOKEN: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  124. if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
  125. String identifier = tokenizer.get_token_identifier(1);
  126. if (identifier == p_function) {
  127. return tokenizer.get_token_line();
  128. }
  129. }
  130. tokenizer.advance();
  131. //print_line("NEXT: "+String(GDScriptTokenizer::get_token_name(tokenizer.get_token())));
  132. }
  133. return -1;
  134. }
  135. Script *GDScriptLanguage::create_script() const {
  136. return memnew(GDScript);
  137. }
  138. /* DEBUGGER FUNCTIONS */
  139. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  140. //break because of parse error
  141. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  142. _debug_parse_err_line = p_line;
  143. _debug_parse_err_file = p_file;
  144. _debug_error = p_error;
  145. ScriptDebugger::get_singleton()->debug(this, false);
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  152. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  153. _debug_parse_err_line = -1;
  154. _debug_parse_err_file = "";
  155. _debug_error = p_error;
  156. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }
  162. String GDScriptLanguage::debug_get_error() const {
  163. return _debug_error;
  164. }
  165. int GDScriptLanguage::debug_get_stack_level_count() const {
  166. if (_debug_parse_err_line >= 0)
  167. return 1;
  168. return _debug_call_stack_pos;
  169. }
  170. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  171. if (_debug_parse_err_line >= 0)
  172. return _debug_parse_err_line;
  173. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  174. int l = _debug_call_stack_pos - p_level - 1;
  175. return *(_call_stack[l].line);
  176. }
  177. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  178. if (_debug_parse_err_line >= 0)
  179. return "";
  180. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  181. int l = _debug_call_stack_pos - p_level - 1;
  182. return _call_stack[l].function->get_name();
  183. }
  184. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  185. if (_debug_parse_err_line >= 0)
  186. return _debug_parse_err_file;
  187. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  188. int l = _debug_call_stack_pos - p_level - 1;
  189. return _call_stack[l].function->get_source();
  190. }
  191. void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  192. if (_debug_parse_err_line >= 0)
  193. return;
  194. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  195. int l = _debug_call_stack_pos - p_level - 1;
  196. GDScriptFunction *f = _call_stack[l].function;
  197. List<Pair<StringName, int> > locals;
  198. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  199. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  200. p_locals->push_back(E->get().first);
  201. p_values->push_back(_call_stack[l].stack[E->get().second]);
  202. }
  203. }
  204. void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  205. if (_debug_parse_err_line >= 0)
  206. return;
  207. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  208. int l = _debug_call_stack_pos - p_level - 1;
  209. GDScriptInstance *instance = _call_stack[l].instance;
  210. if (!instance)
  211. return;
  212. Ref<GDScript> script = instance->get_script();
  213. ERR_FAIL_COND(script.is_null());
  214. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  215. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  216. p_members->push_back(E->key());
  217. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  218. }
  219. }
  220. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  221. ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
  222. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
  223. int l = _debug_call_stack_pos - p_level - 1;
  224. ScriptInstance *instance = _call_stack[l].instance;
  225. return instance;
  226. }
  227. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  228. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  229. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  230. List<Pair<String, Variant> > cinfo;
  231. get_public_constants(&cinfo);
  232. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  233. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key()))
  234. continue;
  235. bool is_script_constant = false;
  236. for (List<Pair<String, Variant> >::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  237. if (CE->get().first == E->key()) {
  238. is_script_constant = true;
  239. break;
  240. }
  241. }
  242. if (is_script_constant)
  243. continue;
  244. const Variant &var = globals[E->value()];
  245. if (Object *obj = var) {
  246. if (Object::cast_to<GDScriptNativeClass>(obj))
  247. continue;
  248. }
  249. bool skip = false;
  250. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  251. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  252. skip = true;
  253. break;
  254. }
  255. }
  256. if (skip)
  257. continue;
  258. p_globals->push_back(E->key());
  259. p_values->push_back(var);
  260. }
  261. }
  262. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  263. if (_debug_parse_err_line >= 0)
  264. return "";
  265. return "";
  266. }
  267. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  268. p_extensions->push_back("gd");
  269. }
  270. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  271. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  272. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  273. }
  274. //not really "functions", but..
  275. {
  276. MethodInfo mi;
  277. mi.name = "preload";
  278. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  279. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  280. p_functions->push_back(mi);
  281. }
  282. {
  283. MethodInfo mi;
  284. mi.name = "yield";
  285. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  286. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  287. mi.default_arguments.push_back(Variant());
  288. mi.default_arguments.push_back(String());
  289. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
  290. p_functions->push_back(mi);
  291. }
  292. {
  293. MethodInfo mi;
  294. mi.name = "assert";
  295. mi.return_val.type = Variant::NIL;
  296. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  297. p_functions->push_back(mi);
  298. }
  299. }
  300. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  301. Pair<String, Variant> pi;
  302. pi.first = "PI";
  303. pi.second = Math_PI;
  304. p_constants->push_back(pi);
  305. Pair<String, Variant> tau;
  306. tau.first = "TAU";
  307. tau.second = Math_TAU;
  308. p_constants->push_back(tau);
  309. Pair<String, Variant> infinity;
  310. infinity.first = "INF";
  311. infinity.second = Math_INF;
  312. p_constants->push_back(infinity);
  313. Pair<String, Variant> nan;
  314. nan.first = "NAN";
  315. nan.second = Math_NAN;
  316. p_constants->push_back(nan);
  317. }
  318. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  319. String s = "func " + p_name + "(";
  320. if (p_args.size()) {
  321. for (int i = 0; i < p_args.size(); i++) {
  322. if (i > 0)
  323. s += ", ";
  324. s += p_args[i].get_slice(":", 0);
  325. }
  326. }
  327. s += "):\n" + _get_indentation() + "pass # Replace with function body.\n";
  328. return s;
  329. }
  330. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  331. struct GDScriptCompletionIdentifier {
  332. String enumeration;
  333. StringName obj_type;
  334. Ref<GDScript> script;
  335. Variant::Type type;
  336. Variant value; //im case there is a value, also return it
  337. GDScriptCompletionIdentifier() :
  338. type(Variant::NIL) {}
  339. };
  340. static GDScriptCompletionIdentifier _get_type_from_variant(const Variant &p_variant, bool p_allow_gdnative_class = false) {
  341. GDScriptCompletionIdentifier t;
  342. t.type = p_variant.get_type();
  343. t.value = p_variant;
  344. if (p_variant.get_type() == Variant::OBJECT) {
  345. Object *obj = p_variant;
  346. if (obj) {
  347. if (p_allow_gdnative_class && Object::cast_to<GDScriptNativeClass>(obj)) {
  348. t.obj_type = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  349. t.value = Variant();
  350. } else {
  351. t.obj_type = obj->get_class();
  352. }
  353. }
  354. }
  355. return t;
  356. }
  357. static GDScriptCompletionIdentifier _get_type_from_pinfo(const PropertyInfo &p_info) {
  358. GDScriptCompletionIdentifier t;
  359. t.type = p_info.type;
  360. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  361. t.obj_type = p_info.hint_string;
  362. }
  363. return t;
  364. }
  365. struct GDScriptCompletionContext {
  366. const GDScriptParser::ClassNode *_class;
  367. const GDScriptParser::FunctionNode *function;
  368. const GDScriptParser::BlockNode *block;
  369. Object *base;
  370. String base_path;
  371. };
  372. static Ref<Reference> _get_parent_class(GDScriptCompletionContext &context) {
  373. if (context._class->extends_used) {
  374. //do inheritance
  375. String path = context._class->extends_file;
  376. Ref<GDScript> script;
  377. Ref<GDScriptNativeClass> native;
  378. if (path != "") {
  379. //path (and optionally subclasses)
  380. if (path.is_rel_path()) {
  381. path = context.base_path.plus_file(path);
  382. }
  383. if (ScriptCodeCompletionCache::get_singleton())
  384. script = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(path);
  385. else
  386. script = ResourceLoader::load(path);
  387. if (script.is_null()) {
  388. return REF();
  389. }
  390. if (!script->is_valid()) {
  391. return REF();
  392. }
  393. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  394. if (context._class->extends_class.size()) {
  395. for (int i = 0; i < context._class->extends_class.size(); i++) {
  396. String sub = context._class->extends_class[i];
  397. if (script->get_subclasses().has(sub)) {
  398. script = script->get_subclasses()[sub];
  399. } else {
  400. return REF();
  401. }
  402. }
  403. }
  404. if (script.is_valid())
  405. return script;
  406. } else {
  407. if (context._class->extends_class.size() == 0) {
  408. ERR_PRINT("BUG");
  409. return REF();
  410. }
  411. String base = context._class->extends_class[0];
  412. if (context._class->extends_class.size() > 1) {
  413. return REF();
  414. }
  415. //if not found, try engine classes
  416. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  417. return REF();
  418. }
  419. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  420. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  421. return native;
  422. }
  423. }
  424. return Ref<Reference>();
  425. }
  426. static GDScriptCompletionIdentifier _get_native_class(GDScriptCompletionContext &context) {
  427. GDScriptCompletionIdentifier id;
  428. REF pc = _get_parent_class(context);
  429. if (!pc.is_valid()) {
  430. return id;
  431. }
  432. Ref<GDScriptNativeClass> nc = pc;
  433. Ref<GDScript> s = pc;
  434. if (s.is_null() && nc.is_null()) {
  435. return id;
  436. }
  437. while (!s.is_null()) {
  438. nc = s->get_native();
  439. s = s->get_base();
  440. }
  441. if (nc.is_null()) {
  442. return id;
  443. }
  444. id.type = Variant::OBJECT;
  445. if (context.base)
  446. id.value = context.base;
  447. id.obj_type = nc->get_name();
  448. return id;
  449. }
  450. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing);
  451. static bool _guess_expression_type(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, GDScriptCompletionIdentifier &r_type, bool p_for_indexing = false) {
  452. if (p_node->type == GDScriptParser::Node::TYPE_CONSTANT) {
  453. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_node);
  454. r_type = _get_type_from_variant(cn->value);
  455. return true;
  456. } else if (p_node->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  457. r_type.type = Variant::DICTIONARY;
  458. //what the heck, fill it anyway
  459. const GDScriptParser::DictionaryNode *an = static_cast<const GDScriptParser::DictionaryNode *>(p_node);
  460. Dictionary d;
  461. for (int i = 0; i < an->elements.size(); i++) {
  462. GDScriptCompletionIdentifier k;
  463. if (_guess_expression_type(context, an->elements[i].key, p_line, k) && k.value.get_type() != Variant::NIL) {
  464. GDScriptCompletionIdentifier v;
  465. if (_guess_expression_type(context, an->elements[i].value, p_line, v)) {
  466. d[k.value] = v.value;
  467. }
  468. }
  469. }
  470. r_type.value = d;
  471. return true;
  472. } else if (p_node->type == GDScriptParser::Node::TYPE_ARRAY) {
  473. r_type.type = Variant::ARRAY;
  474. //what the heck, fill it anyway
  475. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_node);
  476. Array arr;
  477. arr.resize(an->elements.size());
  478. for (int i = 0; i < an->elements.size(); i++) {
  479. GDScriptCompletionIdentifier ci;
  480. if (_guess_expression_type(context, an->elements[i], p_line, ci)) {
  481. arr[i] = ci.value;
  482. }
  483. }
  484. r_type.value = arr;
  485. return true;
  486. } else if (p_node->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  487. MethodInfo mi = GDScriptFunctions::get_info(static_cast<const GDScriptParser::BuiltInFunctionNode *>(p_node)->function);
  488. r_type = _get_type_from_pinfo(mi.return_val);
  489. return true;
  490. } else if (p_node->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  491. return _guess_identifier_type(context, p_line - 1, static_cast<const GDScriptParser::IdentifierNode *>(p_node)->name, r_type, p_for_indexing);
  492. } else if (p_node->type == GDScriptParser::Node::TYPE_SELF) {
  493. //eeh...
  494. r_type = _get_native_class(context);
  495. return r_type.type != Variant::NIL;
  496. } else if (p_node->type == GDScriptParser::Node::TYPE_OPERATOR) {
  497. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  498. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  499. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  500. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  501. r_type.type = tn->vtype;
  502. return true;
  503. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  504. const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  505. return _guess_expression_type(context, bin, p_line, r_type);
  506. } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  507. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  508. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && String(id) == "new") {
  509. //shortcut
  510. StringName identifier = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  511. if (ClassDB::class_exists(identifier)) {
  512. r_type.type = Variant::OBJECT;
  513. r_type.value = Variant();
  514. r_type.obj_type = identifier;
  515. return true;
  516. }
  517. }
  518. GDScriptCompletionIdentifier base;
  519. if (!_guess_expression_type(context, op->arguments[0], p_line, base))
  520. return false;
  521. if (base.type == Variant::OBJECT) {
  522. if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) {
  523. Object *obj = base.value;
  524. if (obj && Object::cast_to<GDScriptNativeClass>(obj)) {
  525. GDScriptNativeClass *gdnc = Object::cast_to<GDScriptNativeClass>(obj);
  526. r_type.type = Variant::OBJECT;
  527. r_type.value = Variant();
  528. r_type.obj_type = gdnc->get_name();
  529. return true;
  530. } else {
  531. if (base.obj_type != StringName()) {
  532. r_type.type = Variant::OBJECT;
  533. r_type.value = Variant();
  534. r_type.obj_type = base.obj_type;
  535. return true;
  536. }
  537. }
  538. }
  539. if (ClassDB::has_method(base.obj_type, id)) {
  540. #ifdef TOOLS_ENABLED
  541. MethodBind *mb = ClassDB::get_method(base.obj_type, id);
  542. PropertyInfo pi = mb->get_return_info();
  543. //try calling the function if constant and all args are constant, should not crash..
  544. Object *baseptr = base.value;
  545. if (mb->is_const() && pi.type == Variant::OBJECT) {
  546. bool all_valid = true;
  547. Vector<Variant> args;
  548. for (int i = 2; i < op->arguments.size(); i++) {
  549. GDScriptCompletionIdentifier arg;
  550. if (_guess_expression_type(context, op->arguments[i], p_line, arg)) {
  551. if (arg.value.get_type() != Variant::NIL && arg.value.get_type() != Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  552. args.push_back(arg.value);
  553. } else {
  554. all_valid = false;
  555. break;
  556. }
  557. } else {
  558. all_valid = false;
  559. }
  560. }
  561. if (all_valid && String(id) == "get_node" && ClassDB::is_parent_class(base.obj_type, "Node") && args.size()) {
  562. String arg1 = args[0];
  563. if (arg1.begins_with("/root/")) {
  564. String which = arg1.get_slice("/", 2);
  565. if (which != "") {
  566. List<PropertyInfo> props;
  567. ProjectSettings::get_singleton()->get_property_list(&props);
  568. //print_line("find singleton");
  569. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  570. String s = E->get().name;
  571. if (!s.begins_with("autoload/"))
  572. continue;
  573. //print_line("found "+s);
  574. String name = s.get_slice("/", 1);
  575. //print_line("name: "+name+", which: "+which);
  576. if (name == which) {
  577. String script = ProjectSettings::get_singleton()->get(s);
  578. if (!script.begins_with("res://")) {
  579. script = "res://" + script;
  580. }
  581. if (!script.ends_with(".gd")) {
  582. //not a script, try find the script anyway,
  583. //may have some success
  584. script = script.get_basename() + ".gd";
  585. }
  586. if (FileAccess::exists(script)) {
  587. //print_line("is a script");
  588. Ref<Script> scr;
  589. if (ScriptCodeCompletionCache::get_singleton())
  590. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  591. else
  592. scr = ResourceLoader::load(script);
  593. r_type.obj_type = "Node";
  594. r_type.type = Variant::OBJECT;
  595. r_type.script = scr;
  596. r_type.value = Variant();
  597. return true;
  598. }
  599. }
  600. }
  601. }
  602. }
  603. }
  604. if (baseptr) {
  605. if (all_valid) {
  606. Vector<const Variant *> argptr;
  607. for (int i = 0; i < args.size(); i++) {
  608. argptr.push_back(&args[i]);
  609. }
  610. Variant::CallError ce;
  611. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  612. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  613. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  614. r_type = _get_type_from_variant(ret);
  615. return true;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. r_type.type = pi.type;
  622. if (pi.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  623. r_type.obj_type = pi.hint_string;
  624. }
  625. return true;
  626. #else
  627. return false;
  628. #endif
  629. } else {
  630. return false;
  631. }
  632. } else {
  633. //method for some variant..
  634. Variant::CallError ce;
  635. Variant v = Variant::construct(base.type, NULL, 0, ce);
  636. List<MethodInfo> mi;
  637. v.get_method_list(&mi);
  638. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  639. if (!E->get().name.begins_with("_") && E->get().name == id.operator String()) {
  640. MethodInfo mi = E->get();
  641. r_type.type = mi.return_val.type;
  642. if (mi.return_val.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  643. r_type.obj_type = mi.return_val.hint_string;
  644. }
  645. return true;
  646. }
  647. }
  648. }
  649. }
  650. } else if (op->op == GDScriptParser::OperatorNode::OP_INDEX || op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  651. GDScriptCompletionIdentifier p1;
  652. GDScriptCompletionIdentifier p2;
  653. if (op->op == GDScriptParser::OperatorNode::OP_INDEX_NAMED) {
  654. if (op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  655. String id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  656. p2.type = Variant::STRING;
  657. p2.value = id;
  658. }
  659. } else {
  660. if (op->arguments[1]) {
  661. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  662. return false;
  663. }
  664. }
  665. }
  666. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
  667. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
  668. if (p2.value.is_num()) {
  669. int index = p2.value;
  670. if (index < 0 || index >= an->elements.size())
  671. return false;
  672. return _guess_expression_type(context, an->elements[index], p_line, r_type);
  673. }
  674. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  675. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  676. if (p2.value.get_type() == Variant::NIL)
  677. return false;
  678. for (int i = 0; i < dn->elements.size(); i++) {
  679. GDScriptCompletionIdentifier k;
  680. if (!_guess_expression_type(context, dn->elements[i].key, p_line, k)) {
  681. return false;
  682. }
  683. if (k.value.get_type() == Variant::NIL)
  684. return false;
  685. if (k.value == p2.value) {
  686. return _guess_expression_type(context, dn->elements[i].value, p_line, r_type);
  687. }
  688. }
  689. } else {
  690. if (op->arguments[0]) {
  691. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  692. return false;
  693. }
  694. }
  695. if (p1.value.get_type() == Variant::OBJECT) {
  696. //??
  697. if (p1.obj_type != StringName() && p2.type == Variant::STRING) {
  698. StringName base_type = p1.obj_type;
  699. if (p1.obj_type == "GDScriptNativeClass") {
  700. //native enum
  701. Ref<GDScriptNativeClass> gdn = p1.value;
  702. if (gdn.is_valid()) {
  703. base_type = gdn->get_name();
  704. }
  705. }
  706. StringName index = p2.value;
  707. bool valid;
  708. Variant::Type t = ClassDB::get_property_type(base_type, index, &valid);
  709. if (t != Variant::NIL && valid) {
  710. r_type.type = t;
  711. if (t == Variant::INT || t == Variant::OBJECT) {
  712. //check for enum!
  713. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  714. StringName getter = ClassDB::get_property_getter(base_type, index);
  715. if (getter != StringName()) {
  716. MethodBind *mb = ClassDB::get_method(base_type, getter);
  717. if (mb) {
  718. PropertyInfo rt = mb->get_return_info();
  719. if ((rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && t == Variant::INT) {
  720. r_type.enumeration = rt.class_name;
  721. } else if (t == Variant::OBJECT) {
  722. r_type.obj_type = rt.class_name;
  723. }
  724. }
  725. }
  726. #endif
  727. }
  728. return true;
  729. }
  730. }
  731. } else if (p1.value.get_type() != Variant::NIL) {
  732. bool valid;
  733. Variant ret = p1.value.get(p2.value, &valid);
  734. if (valid) {
  735. r_type = _get_type_from_variant(ret);
  736. return true;
  737. }
  738. } else {
  739. if (p1.type != Variant::NIL) {
  740. Variant::CallError ce;
  741. Variant base = Variant::construct(p1.type, NULL, 0, ce);
  742. bool valid;
  743. Variant ret = base.get(p2.value, &valid);
  744. if (valid) {
  745. r_type = _get_type_from_variant(ret);
  746. return true;
  747. }
  748. }
  749. }
  750. }
  751. } else {
  752. Variant::Operator vop = Variant::OP_MAX;
  753. switch (op->op) {
  754. case GDScriptParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  755. case GDScriptParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBTRACT; break;
  756. case GDScriptParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  757. case GDScriptParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  758. case GDScriptParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  759. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  760. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  761. case GDScriptParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  762. case GDScriptParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  763. case GDScriptParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  764. default: {}
  765. }
  766. if (vop == Variant::OP_MAX)
  767. return false;
  768. GDScriptCompletionIdentifier p1;
  769. GDScriptCompletionIdentifier p2;
  770. if (op->arguments[0]) {
  771. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  772. return false;
  773. }
  774. }
  775. if (op->arguments.size() > 1) {
  776. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  777. return false;
  778. }
  779. }
  780. Variant::CallError ce;
  781. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  782. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  783. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  784. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  785. // avoid potential invalid ops
  786. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  787. v2 = 1;
  788. v2_use_value = false;
  789. }
  790. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  791. v2 = 1.0;
  792. v2_use_value = false;
  793. }
  794. Variant r;
  795. bool valid;
  796. Variant::evaluate(vop, v1, v2, r, valid);
  797. if (!valid)
  798. return false;
  799. r_type.type = r.get_type();
  800. if (v1_use_value && v2_use_value)
  801. r_type.value = r;
  802. return true;
  803. }
  804. }
  805. return false;
  806. }
  807. static bool _guess_identifier_type_in_block(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  808. if (context.block->if_condition && context.block->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
  809. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  810. //super dirty hack, but very useful
  811. //credit: Zylann
  812. //TODO: this could be hacked to detect ANDed conditions too..
  813. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->if_condition);
  814. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  815. //bingo
  816. if (_guess_expression_type(context, op->arguments[1], op->line, r_type)) {
  817. return true;
  818. }
  819. }
  820. }
  821. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  822. if (gdi.obj_type != StringName()) {
  823. bool valid;
  824. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_identifier, &valid);
  825. if (t != Variant::NIL && valid) {
  826. r_type.type = t;
  827. if (t == Variant::INT) {
  828. //check for enum!
  829. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  830. StringName getter = ClassDB::get_property_getter(gdi.obj_type, p_identifier);
  831. if (getter != StringName()) {
  832. MethodBind *mb = ClassDB::get_method(gdi.obj_type, getter);
  833. if (mb) {
  834. PropertyInfo rt = mb->get_return_info();
  835. if (rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  836. r_type.enumeration = rt.class_name;
  837. }
  838. }
  839. }
  840. #endif
  841. }
  842. return true;
  843. }
  844. }
  845. const GDScriptParser::Node *last_assign = NULL;
  846. int last_assign_line = -1;
  847. for (int i = 0; i < context.block->statements.size(); i++) {
  848. if (context.block->statements[i]->line > p_line)
  849. continue;
  850. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  851. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(context.block->statements[i]);
  852. if (lv->assign && lv->name == p_identifier) {
  853. last_assign = lv->assign;
  854. last_assign_line = context.block->statements[i]->line;
  855. }
  856. }
  857. if (context.block->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  858. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(context.block->statements[i]);
  859. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  860. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  861. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  862. if (id->name == p_identifier) {
  863. last_assign = op->arguments[1];
  864. last_assign_line = context.block->statements[i]->line;
  865. }
  866. }
  867. }
  868. }
  869. }
  870. //use the last assignment, (then backwards?)
  871. if (last_assign && last_assign_line != p_line) {
  872. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  873. }
  874. return false;
  875. }
  876. static bool _guess_identifier_from_assignment_in_function(GDScriptCompletionContext &context, int p_src_line, const StringName &p_identifier, const StringName &p_function, GDScriptCompletionIdentifier &r_type) {
  877. const GDScriptParser::FunctionNode *func = NULL;
  878. for (int i = 0; i < context._class->functions.size(); i++) {
  879. if (context._class->functions[i]->name == p_function) {
  880. func = context._class->functions[i];
  881. break;
  882. }
  883. }
  884. if (!func)
  885. return false;
  886. for (int i = 0; i < func->body->statements.size(); i++) {
  887. if (func->body->statements[i]->line == p_src_line) {
  888. break;
  889. }
  890. if (func->body->statements[i]->type == GDScriptParser::BlockNode::TYPE_OPERATOR) {
  891. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(func->body->statements[i]);
  892. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  893. if (op->arguments.size() && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  894. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  895. if (id->name == p_identifier) {
  896. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  897. }
  898. }
  899. }
  900. }
  901. }
  902. return false;
  903. }
  904. static bool _guess_identifier_type(GDScriptCompletionContext &context, int p_line, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type, bool p_for_indexing) {
  905. //go to block first
  906. const GDScriptParser::BlockNode *block = context.block;
  907. while (block) {
  908. GDScriptCompletionContext c = context;
  909. c.block = block;
  910. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  911. return true;
  912. }
  913. block = block->parent_block;
  914. }
  915. //guess from argument if virtual
  916. if (context.function && context.function->name != StringName()) {
  917. int argindex = -1;
  918. for (int i = 0; i < context.function->arguments.size(); i++) {
  919. if (context.function->arguments[i] == p_identifier) {
  920. argindex = i;
  921. break;
  922. }
  923. }
  924. if (argindex != -1) {
  925. GDScriptCompletionIdentifier id = _get_native_class(context);
  926. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  927. //this kinda sucks but meh
  928. List<MethodInfo> vmethods;
  929. ClassDB::get_virtual_methods(id.obj_type, &vmethods);
  930. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  931. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  932. PropertyInfo arg = E->get().arguments[argindex];
  933. int scp = String(arg.name).find(":");
  934. if (scp != -1) {
  935. r_type.type = Variant::OBJECT;
  936. r_type.obj_type = String(arg.name).substr(scp + 1, String(arg.name).length());
  937. return true;
  938. } else {
  939. r_type.type = arg.type;
  940. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  941. r_type.obj_type = arg.hint_string;
  942. return true;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. }
  949. //guess type in constant
  950. if (context._class->constant_expressions.has(p_identifier)) {
  951. ERR_FAIL_COND_V(context._class->constant_expressions[p_identifier].expression->type != GDScriptParser::Node::TYPE_CONSTANT, false);
  952. r_type = _get_type_from_variant(static_cast<const GDScriptParser::ConstantNode *>(context._class->constant_expressions[p_identifier].expression)->value);
  953. return true;
  954. }
  955. if (!(context.function && context.function->_static)) {
  956. for (int i = 0; i < context._class->variables.size(); i++) {
  957. if (context._class->variables[i].identifier == p_identifier) {
  958. if (context._class->variables[i]._export.type != Variant::NIL) {
  959. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  960. return true;
  961. } else if (context._class->variables[i].expression) {
  962. if (p_line <= context._class->variables[i].line)
  963. return false;
  964. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  965. if (rtype && r_type.type != Variant::NIL)
  966. return true;
  967. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  968. }
  969. //try to guess from assignment in constructor or _ready
  970. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  971. return true;
  972. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  973. return true;
  974. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  975. return true;
  976. return false;
  977. }
  978. }
  979. }
  980. //autoloads as singletons
  981. List<PropertyInfo> props;
  982. ProjectSettings::get_singleton()->get_property_list(&props);
  983. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  984. String s = E->get().name;
  985. if (!s.begins_with("autoload/"))
  986. continue;
  987. String name = s.get_slice("/", 1);
  988. if (name == String(p_identifier)) {
  989. String path = ProjectSettings::get_singleton()->get(s);
  990. if (path.begins_with("*")) {
  991. String script = path.substr(1, path.length());
  992. if (!script.ends_with(".gd")) {
  993. //not a script, try find the script anyway,
  994. //may have some success
  995. script = script.get_basename() + ".gd";
  996. }
  997. if (FileAccess::exists(script)) {
  998. //print_line("is a script");
  999. Ref<Script> scr;
  1000. if (ScriptCodeCompletionCache::get_singleton())
  1001. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  1002. else
  1003. scr = ResourceLoader::load(script);
  1004. r_type.obj_type = "Node";
  1005. r_type.type = Variant::OBJECT;
  1006. r_type.script = scr;
  1007. r_type.value = Variant();
  1008. return true;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. //global
  1014. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1015. if (E->key() == p_identifier) {
  1016. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()], !p_for_indexing);
  1017. return true;
  1018. }
  1019. }
  1020. return false;
  1021. }
  1022. static void _find_identifiers_in_block(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1023. if (p_only_functions)
  1024. return;
  1025. for (int i = 0; i < context.block->statements.size(); i++) {
  1026. GDScriptParser::Node *statement = context.block->statements[i];
  1027. if (statement->line > p_line)
  1028. continue;
  1029. GDScriptParser::BlockNode::Type statementType = statement->type;
  1030. if (statementType == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  1031. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(statement);
  1032. result.insert(lv->name.operator String());
  1033. } else if (statementType == GDScriptParser::BlockNode::TYPE_CONTROL_FLOW) {
  1034. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(statement);
  1035. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_FOR) {
  1036. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(cf->arguments[0]);
  1037. result.insert(id->name.operator String());
  1038. }
  1039. }
  1040. }
  1041. }
  1042. static void _find_identifiers_in_class(GDScriptCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  1043. if (!p_static && !p_only_functions) {
  1044. for (int i = 0; i < context._class->variables.size(); i++) {
  1045. result.insert(context._class->variables[i].identifier);
  1046. }
  1047. }
  1048. if (!p_only_functions) {
  1049. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = context._class->constant_expressions.front(); E; E = E->next()) {
  1050. result.insert(E->key());
  1051. }
  1052. for (int i = 0; i < context._class->subclasses.size(); i++) {
  1053. result.insert(context._class->subclasses[i]->name);
  1054. }
  1055. }
  1056. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1057. if (context._class->static_functions[i]->arguments.size())
  1058. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  1059. else
  1060. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  1061. }
  1062. if (!p_static) {
  1063. for (int i = 0; i < context._class->functions.size(); i++) {
  1064. if (context._class->functions[i]->arguments.size())
  1065. result.insert(context._class->functions[i]->name.operator String() + "(");
  1066. else
  1067. result.insert(context._class->functions[i]->name.operator String() + "()");
  1068. }
  1069. }
  1070. //globals
  1071. Ref<Reference> base = _get_parent_class(context);
  1072. while (true) {
  1073. Ref<GDScript> script = base;
  1074. Ref<GDScriptNativeClass> nc = base;
  1075. if (script.is_valid()) {
  1076. if (!p_static && !p_only_functions) {
  1077. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1078. result.insert(E->get().operator String());
  1079. }
  1080. }
  1081. if (!p_only_functions) {
  1082. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1083. result.insert(E->key().operator String());
  1084. }
  1085. }
  1086. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1087. if (!p_static || E->get()->is_static()) {
  1088. if (E->get()->get_argument_count())
  1089. result.insert(E->key().operator String() + "(");
  1090. else
  1091. result.insert(E->key().operator String() + "()");
  1092. }
  1093. }
  1094. if (!p_only_functions) {
  1095. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1096. result.insert(E->key().operator String());
  1097. }
  1098. }
  1099. base = script->get_base();
  1100. if (base.is_null())
  1101. base = script->get_native();
  1102. } else if (nc.is_valid()) {
  1103. StringName type = nc->get_name();
  1104. if (!p_only_functions) {
  1105. List<String> constants;
  1106. ClassDB::get_integer_constant_list(type, &constants);
  1107. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1108. result.insert(E->get());
  1109. }
  1110. List<PropertyInfo> pinfo;
  1111. ClassDB::get_property_list(type, &pinfo);
  1112. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1113. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1114. continue;
  1115. if (String(E->get().name).find("/") != -1)
  1116. continue;
  1117. result.insert(E->get().name);
  1118. }
  1119. }
  1120. List<MethodInfo> methods;
  1121. ClassDB::get_method_list(type, &methods, false, true);
  1122. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1123. if (E->get().name.begins_with("_"))
  1124. continue;
  1125. if (E->get().arguments.size())
  1126. result.insert(E->get().name + "(");
  1127. else
  1128. result.insert(E->get().name + "()");
  1129. }
  1130. break;
  1131. } else
  1132. break;
  1133. }
  1134. }
  1135. static void _find_identifiers(GDScriptCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1136. const GDScriptParser::BlockNode *block = context.block;
  1137. if (context.function) {
  1138. const GDScriptParser::FunctionNode *f = context.function;
  1139. for (int i = 0; i < f->arguments.size(); i++) {
  1140. result.insert(f->arguments[i].operator String());
  1141. }
  1142. }
  1143. while (block) {
  1144. GDScriptCompletionContext c = context;
  1145. c.block = block;
  1146. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  1147. block = block->parent_block;
  1148. }
  1149. const GDScriptParser::ClassNode *clss = context._class;
  1150. bool _static = context.function && context.function->_static;
  1151. while (clss) {
  1152. GDScriptCompletionContext c = context;
  1153. c._class = clss;
  1154. c.block = NULL;
  1155. c.function = NULL;
  1156. _find_identifiers_in_class(c, _static, p_only_functions, result);
  1157. clss = clss->owner;
  1158. }
  1159. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1160. result.insert(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)));
  1161. }
  1162. static const char *_type_names[Variant::VARIANT_MAX] = {
  1163. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1164. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1165. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1166. };
  1167. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1168. result.insert(_type_names[i]);
  1169. }
  1170. List<String> reserved_words;
  1171. GDScriptLanguage::get_singleton()->get_reserved_words(&reserved_words);
  1172. for (List<String>::Element *E = reserved_words.front(); E; E = E->next()) {
  1173. result.insert(E->get());
  1174. }
  1175. //autoload singletons
  1176. List<PropertyInfo> props;
  1177. ProjectSettings::get_singleton()->get_property_list(&props);
  1178. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1179. String s = E->get().name;
  1180. if (!s.begins_with("autoload/"))
  1181. continue;
  1182. String name = s.get_slice("/", 1);
  1183. String path = ProjectSettings::get_singleton()->get(s);
  1184. if (path.begins_with("*")) {
  1185. result.insert(name);
  1186. }
  1187. }
  1188. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1189. result.insert(E->key().operator String());
  1190. }
  1191. }
  1192. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1193. String n = p_info.name;
  1194. int idx = n.find(":");
  1195. if (idx != -1) {
  1196. return n.substr(idx + 1, n.length());
  1197. }
  1198. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1199. return p_info.hint_string;
  1200. if (p_info.type == Variant::NIL) {
  1201. if (p_isarg)
  1202. return "var";
  1203. else
  1204. return "void";
  1205. }
  1206. return Variant::get_type_name(p_info.type);
  1207. }
  1208. static void _make_function_hint(const GDScriptParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1209. arghint = "func " + p_func->name + "(";
  1210. for (int i = 0; i < p_func->arguments.size(); i++) {
  1211. if (i > 0)
  1212. arghint += ", ";
  1213. else
  1214. arghint += " ";
  1215. if (i == p_argidx) {
  1216. arghint += String::chr(0xFFFF);
  1217. }
  1218. arghint += p_func->arguments[i].operator String();
  1219. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1220. if (i >= deffrom) {
  1221. int defidx = deffrom - i;
  1222. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1223. if (p_func->default_values[defidx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1224. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[defidx]);
  1225. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  1226. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(op->arguments[1]);
  1227. arghint += "=" + cn->value.get_construct_string();
  1228. }
  1229. } else {
  1230. }
  1231. }
  1232. }
  1233. if (i == p_argidx) {
  1234. arghint += String::chr(0xFFFF);
  1235. }
  1236. }
  1237. if (p_func->arguments.size() > 0)
  1238. arghint += " ";
  1239. arghint += ")";
  1240. }
  1241. void get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
  1242. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1243. get_directory_contents(p_dir->get_subdir(i), r_list);
  1244. }
  1245. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1246. r_list.insert("\"" + p_dir->get_file_path(i) + "\"");
  1247. }
  1248. }
  1249. static void _find_type_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, const StringName &p_method, const GDScriptCompletionIdentifier &id, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1250. //print_line("find type arguments?");
  1251. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1252. MethodBind *m = ClassDB::get_method(id.obj_type, p_method);
  1253. if (!m) {
  1254. //not in static method, see script
  1255. //print_line("not in static: "+String(p_method));
  1256. Ref<GDScript> on_script;
  1257. if (id.value.get_type()) {
  1258. Object *obj = id.value;
  1259. GDScript *scr = Object::cast_to<GDScript>(obj);
  1260. if (scr) {
  1261. while (scr) {
  1262. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1263. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1264. arghint = "static func " + String(p_method) + "(";
  1265. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1266. if (i > 0)
  1267. arghint += ", ";
  1268. else
  1269. arghint += " ";
  1270. if (i == p_argidx) {
  1271. arghint += String::chr(0xFFFF);
  1272. }
  1273. arghint += "var " + E->get()->get_argument_name(i);
  1274. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1275. if (i >= deffrom) {
  1276. int defidx = deffrom - i;
  1277. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1278. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1279. }
  1280. }
  1281. if (i == p_argidx) {
  1282. arghint += String::chr(0xFFFF);
  1283. }
  1284. }
  1285. arghint += ")";
  1286. return; //found
  1287. }
  1288. }
  1289. if (scr->get_base().is_valid())
  1290. scr = scr->get_base().ptr();
  1291. else
  1292. scr = NULL;
  1293. }
  1294. } else {
  1295. if (obj) {
  1296. on_script = obj->get_script();
  1297. }
  1298. }
  1299. }
  1300. //print_line("but it has a script?");
  1301. if (!on_script.is_valid() && id.script.is_valid()) {
  1302. //print_line("yes");
  1303. on_script = id.script;
  1304. }
  1305. if (on_script.is_valid()) {
  1306. GDScript *scr = on_script.ptr();
  1307. if (scr) {
  1308. while (scr) {
  1309. String code = scr->get_source_code();
  1310. //print_line("has source code!");
  1311. if (code != "") {
  1312. //if there is code, parse it. This way is slower but updates in real-time
  1313. GDScriptParser p;
  1314. //Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="",bool p_for_completion=false);
  1315. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1316. if (err == OK) {
  1317. //print_line("checking the functions...");
  1318. //only if ok, otherwise use what is cached on the script
  1319. //GDScriptParser::ClassNode *base = p.
  1320. const GDScriptParser::Node *root = p.get_parse_tree();
  1321. ERR_FAIL_COND(root->type != GDScriptParser::Node::TYPE_CLASS);
  1322. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1323. const GDScriptParser::FunctionNode *func = NULL;
  1324. bool st = false;
  1325. for (int i = 0; i < cl->functions.size(); i++) {
  1326. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1327. if (cl->functions[i]->name == p_method) {
  1328. func = cl->functions[i];
  1329. }
  1330. }
  1331. for (int i = 0; i < cl->static_functions.size(); i++) {
  1332. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1333. if (cl->static_functions[i]->name == p_method) {
  1334. func = cl->static_functions[i];
  1335. st = true;
  1336. }
  1337. }
  1338. if (func) {
  1339. arghint = "func " + String(p_method) + "(";
  1340. if (st)
  1341. arghint = "static " + arghint;
  1342. for (int i = 0; i < func->arguments.size(); i++) {
  1343. if (i > 0)
  1344. arghint += ", ";
  1345. else
  1346. arghint += " ";
  1347. if (i == p_argidx) {
  1348. arghint += String::chr(0xFFFF);
  1349. }
  1350. arghint += "var " + String(func->arguments[i]);
  1351. int deffrom = func->arguments.size() - func->default_values.size();
  1352. if (i >= deffrom) {
  1353. int defidx = deffrom - i;
  1354. if (defidx >= 0 && defidx < func->default_values.size() && func->default_values[defidx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1355. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(func->default_values[defidx]);
  1356. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN) {
  1357. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(op->arguments[1]);
  1358. arghint += "=" + cn->value.get_construct_string();
  1359. }
  1360. }
  1361. }
  1362. if (i == p_argidx) {
  1363. arghint += String::chr(0xFFFF);
  1364. }
  1365. }
  1366. arghint += " )";
  1367. return;
  1368. }
  1369. } else {
  1370. //print_line("failed parsing?");
  1371. code = "";
  1372. }
  1373. }
  1374. if (code == "") {
  1375. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1376. if (p_method == E->get()->get_name()) {
  1377. arghint = "func " + String(p_method) + "(";
  1378. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1379. if (i > 0)
  1380. arghint += ", ";
  1381. else
  1382. arghint += " ";
  1383. if (i == p_argidx) {
  1384. arghint += String::chr(0xFFFF);
  1385. }
  1386. arghint += "var " + E->get()->get_argument_name(i);
  1387. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1388. if (i >= deffrom) {
  1389. int defidx = deffrom - i;
  1390. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1391. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1392. }
  1393. }
  1394. if (i == p_argidx) {
  1395. arghint += String::chr(0xFFFF);
  1396. }
  1397. }
  1398. arghint += ")";
  1399. return; //found
  1400. }
  1401. }
  1402. }
  1403. if (scr->get_base().is_valid())
  1404. scr = scr->get_base().ptr();
  1405. else
  1406. scr = NULL;
  1407. }
  1408. }
  1409. }
  1410. } else {
  1411. //regular method
  1412. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1413. if (p_argidx < m->get_argument_count()) {
  1414. PropertyInfo pi = m->get_argument_info(p_argidx);
  1415. if (pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1416. String enumeration = pi.class_name;
  1417. if (enumeration.find(".") != -1) {
  1418. //class constant
  1419. List<StringName> constants;
  1420. String cls = enumeration.get_slice(".", 0);
  1421. String enm = enumeration.get_slice(".", 1);
  1422. ClassDB::get_enum_constants(cls, enm, &constants);
  1423. //constants.sort_custom<StringName::AlphCompare>();
  1424. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1425. String add = cls + "." + E->get();
  1426. result.insert(add);
  1427. r_forced = true;
  1428. }
  1429. } else {
  1430. //global constant
  1431. StringName current_enum = enumeration;
  1432. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1433. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1434. result.insert(GlobalConstants::get_global_constant_name(i));
  1435. r_forced = true;
  1436. }
  1437. }
  1438. //global
  1439. }
  1440. }
  1441. }
  1442. #endif
  1443. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1444. if (p_argidx == 0) {
  1445. List<MethodInfo> sigs;
  1446. ClassDB::get_signal_list(id.obj_type, &sigs);
  1447. if (id.script.is_valid()) {
  1448. id.script->get_script_signal_list(&sigs);
  1449. } else if (id.value.get_type() == Variant::OBJECT) {
  1450. Object *obj = id.value;
  1451. if (obj && !obj->get_script().is_null()) {
  1452. Ref<Script> scr = obj->get_script();
  1453. if (scr.is_valid()) {
  1454. scr->get_script_signal_list(&sigs);
  1455. }
  1456. }
  1457. }
  1458. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1459. result.insert("\"" + E->get().name + "\"");
  1460. r_forced = true;
  1461. }
  1462. } else if (p_argidx == 2) {
  1463. if (context._class) {
  1464. for (int i = 0; i < context._class->functions.size(); i++) {
  1465. result.insert("\"" + context._class->functions[i]->name + "\"");
  1466. r_forced = true;
  1467. }
  1468. }
  1469. }
  1470. /*if (p_argidx==2) {
  1471. ERR_FAIL_COND(p_node->type!=GDScriptParser::Node::TYPE_OPERATOR);
  1472. const GDScriptParser::OperatorNode *op=static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1473. if (op->arguments.size()>)
  1474. }*/
  1475. } else {
  1476. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ClassDB::is_parent_class(id.obj_type, "Node")) {
  1477. List<PropertyInfo> props;
  1478. ProjectSettings::get_singleton()->get_property_list(&props);
  1479. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1480. String s = E->get().name;
  1481. if (!s.begins_with("autoload/"))
  1482. continue;
  1483. //print_line("found "+s);
  1484. String name = s.get_slice("/", 1);
  1485. result.insert("\"/root/" + name + "\"");
  1486. r_forced = true;
  1487. }
  1488. }
  1489. Object *obj = id.value;
  1490. if (obj) {
  1491. List<String> options;
  1492. obj->get_argument_options(p_method, p_argidx, &options);
  1493. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1494. result.insert(E->get());
  1495. r_forced = true;
  1496. }
  1497. }
  1498. }
  1499. arghint = _get_visual_datatype(m->get_return_info(), false) + " " + p_method.operator String() + String("(");
  1500. for (int i = 0; i < m->get_argument_count(); i++) {
  1501. if (i > 0)
  1502. arghint += ", ";
  1503. else
  1504. arghint += " ";
  1505. if (i == p_argidx) {
  1506. arghint += String::chr(0xFFFF);
  1507. }
  1508. String n = m->get_argument_info(i).name;
  1509. int dp = n.find(":");
  1510. if (dp != -1)
  1511. n = n.substr(0, dp);
  1512. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1513. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1514. if (i >= deffrom) {
  1515. int defidx = i - deffrom;
  1516. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1517. Variant v = m->get_default_argument(i);
  1518. arghint += "=" + v.get_construct_string();
  1519. }
  1520. }
  1521. if (i == p_argidx) {
  1522. arghint += String::chr(0xFFFF);
  1523. }
  1524. }
  1525. if (m->get_argument_count() > 0)
  1526. arghint += " ";
  1527. arghint += ")";
  1528. }
  1529. }
  1530. }
  1531. static void _find_call_arguments(GDScriptCompletionContext &context, const GDScriptParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1532. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  1533. return;
  1534. }
  1535. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  1536. if (op->op != GDScriptParser::OperatorNode::OP_CALL) {
  1537. return;
  1538. }
  1539. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1540. //complete built-in function
  1541. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  1542. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  1543. if (mi.name == "load" && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  1544. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), result);
  1545. }
  1546. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDScriptFunctions::get_func_name(fn->function) + String("(");
  1547. for (int i = 0; i < mi.arguments.size(); i++) {
  1548. if (i > 0)
  1549. arghint += ", ";
  1550. else
  1551. arghint += " ";
  1552. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1553. arghint += String::chr(0xFFFF);
  1554. }
  1555. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1556. if (i == p_argidx || ((mi.flags & METHOD_FLAG_VARARG) && i > p_argidx)) {
  1557. arghint += String::chr(0xFFFF);
  1558. }
  1559. }
  1560. if (mi.arguments.size() > 0)
  1561. arghint += " ";
  1562. arghint += ")";
  1563. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  1564. //complete constructor
  1565. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  1566. List<MethodInfo> mil;
  1567. Variant::get_constructor_list(tn->vtype, &mil);
  1568. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1569. MethodInfo mi = E->get();
  1570. if (mi.arguments.size() == 0)
  1571. continue;
  1572. if (E->prev())
  1573. arghint += "\n";
  1574. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1575. for (int i = 0; i < mi.arguments.size(); i++) {
  1576. if (i > 0)
  1577. arghint += ", ";
  1578. else
  1579. arghint += " ";
  1580. if (i == p_argidx) {
  1581. arghint += String::chr(0xFFFF);
  1582. }
  1583. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1584. if (i == p_argidx) {
  1585. arghint += String::chr(0xFFFF);
  1586. }
  1587. }
  1588. if (mi.arguments.size() > 0)
  1589. arghint += " ";
  1590. arghint += ")";
  1591. }
  1592. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1593. //make sure identifier exists...
  1594. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  1595. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  1596. //self, look up
  1597. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1598. if (context._class->static_functions[i]->name == id->name) {
  1599. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1600. return;
  1601. }
  1602. }
  1603. if (context.function && !context.function->_static) {
  1604. for (int i = 0; i < context._class->functions.size(); i++) {
  1605. if (context._class->functions[i]->name == id->name) {
  1606. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1607. return;
  1608. }
  1609. }
  1610. }
  1611. Ref<Reference> base = _get_parent_class(context);
  1612. while (true) {
  1613. Ref<GDScript> script = base;
  1614. Ref<GDScriptNativeClass> nc = base;
  1615. if (script.is_valid()) {
  1616. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1617. if (E->key() == id->name) {
  1618. if (context.function && context.function->_static && !E->get()->is_static())
  1619. continue;
  1620. arghint = "func " + id->name.operator String() + String("(");
  1621. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1622. if (i > 0)
  1623. arghint += ", ";
  1624. else
  1625. arghint += " ";
  1626. if (i == p_argidx) {
  1627. arghint += String::chr(0xFFFF);
  1628. }
  1629. arghint += E->get()->get_argument_name(i);
  1630. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1631. if (i >= deffrom) {
  1632. int defidx = deffrom - i;
  1633. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1634. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1635. }
  1636. }
  1637. if (i == p_argidx) {
  1638. arghint += String::chr(0xFFFF);
  1639. }
  1640. }
  1641. if (E->get()->get_argument_count() > 0)
  1642. arghint += " ";
  1643. arghint += ")";
  1644. return;
  1645. }
  1646. }
  1647. base = script->get_base();
  1648. if (base.is_null())
  1649. base = script->get_native();
  1650. } else if (nc.is_valid()) {
  1651. if (!(context.function && context.function->_static)) {
  1652. GDScriptCompletionIdentifier ci;
  1653. ci.type = Variant::OBJECT;
  1654. ci.obj_type = nc->get_name();
  1655. if (!context._class->owner)
  1656. ci.value = context.base;
  1657. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1658. //guess type..
  1659. /*
  1660. List<MethodInfo> methods;
  1661. ClassDB::get_method_list(type,&methods);
  1662. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1663. if (E->get().arguments.size())
  1664. result.insert(E->get().name+"(");
  1665. else
  1666. result.insert(E->get().name+"()");
  1667. }*/
  1668. }
  1669. break;
  1670. } else
  1671. break;
  1672. }
  1673. } else {
  1674. //indexed lookup
  1675. GDScriptCompletionIdentifier ci;
  1676. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1677. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1678. return;
  1679. }
  1680. }
  1681. }
  1682. }
  1683. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1684. GDScriptParser p;
  1685. p.parse(p_code, p_base_path, false, "", true);
  1686. bool isfunction = false;
  1687. Set<String> options;
  1688. r_forced = false;
  1689. GDScriptCompletionContext context;
  1690. context._class = p.get_completion_class();
  1691. context.block = p.get_completion_block();
  1692. context.function = p.get_completion_function();
  1693. context.base = p_owner;
  1694. context.base_path = p_base_path;
  1695. switch (p.get_completion_type()) {
  1696. case GDScriptParser::COMPLETION_NONE: {
  1697. } break;
  1698. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1699. List<StringName> constants;
  1700. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1701. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1702. options.insert(E->get().operator String());
  1703. }
  1704. } break;
  1705. case GDScriptParser::COMPLETION_FUNCTION:
  1706. isfunction = true;
  1707. case GDScriptParser::COMPLETION_IDENTIFIER: {
  1708. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1709. } break;
  1710. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  1711. } break;
  1712. case GDScriptParser::COMPLETION_GET_NODE: {
  1713. if (p_owner) {
  1714. List<String> opts;
  1715. p_owner->get_argument_options("get_node", 0, &opts);
  1716. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  1717. String opt = E->get().strip_edges();
  1718. if (opt.is_quoted()) {
  1719. r_forced = true;
  1720. String idopt = opt.unquote();
  1721. if (idopt.replace("/", "_").is_valid_identifier()) {
  1722. options.insert(idopt);
  1723. } else {
  1724. options.insert(opt);
  1725. }
  1726. }
  1727. }
  1728. }
  1729. } break;
  1730. case GDScriptParser::COMPLETION_METHOD:
  1731. isfunction = true;
  1732. case GDScriptParser::COMPLETION_INDEX: {
  1733. const GDScriptParser::Node *node = p.get_completion_node();
  1734. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  1735. break;
  1736. GDScriptCompletionIdentifier t;
  1737. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t, true)) {
  1738. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  1739. //native enum
  1740. Ref<GDScriptNativeClass> gdn = t.value;
  1741. if (gdn.is_valid()) {
  1742. StringName cn = gdn->get_name();
  1743. List<String> cnames;
  1744. ClassDB::get_integer_constant_list(cn, &cnames);
  1745. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1746. options.insert(E->get());
  1747. }
  1748. List<PropertyInfo> pinfo;
  1749. ClassDB::get_property_list(cn, &pinfo);
  1750. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1751. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1752. continue;
  1753. if (String(E->get().name).find("/") != -1)
  1754. continue;
  1755. options.insert(E->get().name);
  1756. }
  1757. }
  1758. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1759. Ref<GDScript> on_script;
  1760. if (t.value.get_type()) {
  1761. Object *obj = t.value;
  1762. GDScript *scr = Object::cast_to<GDScript>(obj);
  1763. if (scr) {
  1764. while (scr) {
  1765. if (!isfunction) {
  1766. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1767. options.insert(E->key());
  1768. }
  1769. }
  1770. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1771. if (E->get()->is_static())
  1772. options.insert(E->key());
  1773. }
  1774. if (scr->get_base().is_valid())
  1775. scr = scr->get_base().ptr();
  1776. else
  1777. scr = NULL;
  1778. }
  1779. } else {
  1780. if (obj) {
  1781. on_script = obj->get_script();
  1782. }
  1783. }
  1784. }
  1785. if (!on_script.is_valid() && t.script.is_valid()) {
  1786. on_script = t.script;
  1787. }
  1788. if (on_script.is_valid()) {
  1789. GDScript *scr = on_script.ptr();
  1790. if (scr) {
  1791. while (scr) {
  1792. String code = scr->get_source_code();
  1793. if (code != "") {
  1794. //if there is code, parse it. This way is slower but updates in real-time
  1795. GDScriptParser p;
  1796. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1797. if (err == OK) {
  1798. //only if ok, otherwise use what is cached on the script
  1799. //GDScriptParser::ClassNode *base = p.
  1800. const GDScriptParser::Node *root = p.get_parse_tree();
  1801. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1802. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  1803. for (int i = 0; i < cl->functions.size(); i++) {
  1804. if (cl->functions[i]->arguments.size())
  1805. options.insert(String(cl->functions[i]->name) + "(");
  1806. else
  1807. options.insert(String(cl->functions[i]->name) + "()");
  1808. }
  1809. for (int i = 0; i < cl->static_functions.size(); i++) {
  1810. if (cl->static_functions[i]->arguments.size())
  1811. options.insert(String(cl->static_functions[i]->name) + "(");
  1812. else
  1813. options.insert(String(cl->static_functions[i]->name) + "()");
  1814. }
  1815. if (!isfunction) {
  1816. for (int i = 0; i < cl->variables.size(); i++) {
  1817. options.insert(String(cl->variables[i].identifier));
  1818. }
  1819. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = cl->constant_expressions.front(); E; E = E->next()) {
  1820. options.insert(String(E->key()));
  1821. }
  1822. }
  1823. } else {
  1824. code = ""; //well, then no code
  1825. }
  1826. }
  1827. if (code == "") {
  1828. //use class directly, no code was found
  1829. if (!isfunction) {
  1830. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1831. options.insert(E->key());
  1832. }
  1833. }
  1834. for (const Map<StringName, GDScriptFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1835. if (E->get()->get_argument_count())
  1836. options.insert(String(E->key()) + "()");
  1837. else
  1838. options.insert(String(E->key()) + "(");
  1839. }
  1840. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1841. options.insert(E->get());
  1842. }
  1843. }
  1844. if (scr->get_base().is_valid())
  1845. scr = scr->get_base().ptr();
  1846. else
  1847. scr = NULL;
  1848. }
  1849. }
  1850. }
  1851. if (!isfunction) {
  1852. ClassDB::get_integer_constant_list(t.obj_type, r_options);
  1853. List<PropertyInfo> pinfo;
  1854. ClassDB::get_property_list(t.obj_type, &pinfo);
  1855. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1856. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1857. continue;
  1858. if (String(E->get().name).find("/") != -1)
  1859. continue;
  1860. r_options->push_back(E->get().name);
  1861. }
  1862. }
  1863. List<MethodInfo> mi;
  1864. ClassDB::get_method_list(t.obj_type, &mi, false, true);
  1865. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1866. if (E->get().name.begins_with("_"))
  1867. continue;
  1868. if (E->get().arguments.size())
  1869. options.insert(E->get().name + "(");
  1870. else
  1871. options.insert(E->get().name + "()");
  1872. }
  1873. } else {
  1874. //check InputEvent hint
  1875. {
  1876. if (t.value.get_type() == Variant::NIL) {
  1877. Variant::CallError ce;
  1878. t.value = Variant::construct(t.type, NULL, 0, ce);
  1879. }
  1880. if (!isfunction) {
  1881. List<PropertyInfo> pl;
  1882. t.value.get_property_list(&pl);
  1883. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1884. if (String(E->get().name).find("/") == -1)
  1885. options.insert(E->get().name);
  1886. }
  1887. }
  1888. List<MethodInfo> mi;
  1889. t.value.get_method_list(&mi);
  1890. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1891. if (E->get().arguments.size())
  1892. options.insert(E->get().name + "(");
  1893. else
  1894. options.insert(E->get().name + "()");
  1895. }
  1896. }
  1897. }
  1898. }
  1899. } break;
  1900. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  1901. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_forced, r_call_hint);
  1902. } break;
  1903. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  1904. GDScriptCompletionIdentifier cid = _get_native_class(context);
  1905. if (cid.obj_type != StringName()) {
  1906. List<MethodInfo> vm;
  1907. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  1908. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1909. MethodInfo &mi = E->get();
  1910. String m = mi.name;
  1911. if (m.find(":") != -1)
  1912. m = m.substr(0, m.find(":"));
  1913. m += "(";
  1914. if (mi.arguments.size()) {
  1915. for (int i = 0; i < mi.arguments.size(); i++) {
  1916. if (i > 0)
  1917. m += ", ";
  1918. String n = mi.arguments[i].name;
  1919. if (n.find(":") != -1)
  1920. n = n.substr(0, n.find(":"));
  1921. m += n;
  1922. }
  1923. }
  1924. m += "):";
  1925. options.insert(m);
  1926. }
  1927. }
  1928. } break;
  1929. case GDScriptParser::COMPLETION_YIELD: {
  1930. const GDScriptParser::Node *node = p.get_completion_node();
  1931. GDScriptCompletionIdentifier t;
  1932. if (!_guess_expression_type(context, node, p.get_completion_line(), t))
  1933. break;
  1934. if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1935. List<MethodInfo> sigs;
  1936. ClassDB::get_signal_list(t.obj_type, &sigs);
  1937. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1938. options.insert("\"" + E->get().name + "\"");
  1939. r_forced = true;
  1940. }
  1941. }
  1942. } break;
  1943. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  1944. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  1945. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  1946. r_forced = true;
  1947. }
  1948. } break;
  1949. case GDScriptParser::COMPLETION_ASSIGN: {
  1950. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1951. GDScriptCompletionIdentifier ci;
  1952. if (_guess_expression_type(context, p.get_completion_node(), p.get_completion_line(), ci)) {
  1953. String enumeration = ci.enumeration;
  1954. if (enumeration.find(".") != -1) {
  1955. //class constant
  1956. List<StringName> constants;
  1957. String cls = enumeration.get_slice(".", 0);
  1958. String enm = enumeration.get_slice(".", 1);
  1959. ClassDB::get_enum_constants(cls, enm, &constants);
  1960. //constants.sort_custom<StringName::AlphCompare>();
  1961. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1962. String add = cls + "." + E->get();
  1963. r_options->push_back(add);
  1964. r_forced = true;
  1965. }
  1966. } else {
  1967. //global constant
  1968. StringName current_enum = enumeration;
  1969. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1970. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1971. r_options->push_back(GlobalConstants::get_global_constant_name(i));
  1972. r_forced = true;
  1973. }
  1974. }
  1975. //global
  1976. }
  1977. }
  1978. #endif
  1979. } break;
  1980. }
  1981. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  1982. r_options->push_back(E->get());
  1983. }
  1984. return OK;
  1985. }
  1986. #else
  1987. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1988. return OK;
  1989. }
  1990. #endif
  1991. String GDScriptLanguage::_get_indentation() const {
  1992. #ifdef TOOLS_ENABLED
  1993. if (Engine::get_singleton()->is_editor_hint()) {
  1994. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  1995. if (use_space_indentation) {
  1996. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  1997. String space_indent = "";
  1998. for (int i = 0; i < indent_size; i++) {
  1999. space_indent += " ";
  2000. }
  2001. return space_indent;
  2002. }
  2003. }
  2004. #endif
  2005. return "\t";
  2006. }
  2007. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2008. String indent = _get_indentation();
  2009. Vector<String> lines = p_code.split("\n");
  2010. List<int> indent_stack;
  2011. for (int i = 0; i < lines.size(); i++) {
  2012. String l = lines[i];
  2013. int tc = 0;
  2014. for (int j = 0; j < l.length(); j++) {
  2015. if (l[j] == ' ' || l[j] == '\t') {
  2016. tc++;
  2017. } else {
  2018. break;
  2019. }
  2020. }
  2021. String st = l.substr(tc, l.length()).strip_edges();
  2022. if (st == "" || st.begins_with("#"))
  2023. continue; //ignore!
  2024. int ilevel = 0;
  2025. if (indent_stack.size()) {
  2026. ilevel = indent_stack.back()->get();
  2027. }
  2028. if (tc > ilevel) {
  2029. indent_stack.push_back(tc);
  2030. } else if (tc < ilevel) {
  2031. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2032. indent_stack.pop_back();
  2033. }
  2034. if (indent_stack.size() && indent_stack.back()->get() != tc)
  2035. indent_stack.push_back(tc); //this is not right but gets the job done
  2036. }
  2037. if (i >= p_from_line) {
  2038. l = "";
  2039. for (int j = 0; j < indent_stack.size(); j++) {
  2040. l += indent;
  2041. }
  2042. l += st;
  2043. } else if (i > p_to_line) {
  2044. break;
  2045. }
  2046. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  2047. lines[i] = l;
  2048. }
  2049. p_code = "";
  2050. for (int i = 0; i < lines.size(); i++) {
  2051. if (i > 0)
  2052. p_code += "\n";
  2053. p_code += lines[i];
  2054. }
  2055. }
  2056. #ifdef TOOLS_ENABLED
  2057. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
  2058. //before parsing, try the usual stuff
  2059. if (ClassDB::class_exists(p_symbol)) {
  2060. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2061. r_result.class_name = p_symbol;
  2062. return OK;
  2063. }
  2064. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2065. Variant::Type t = Variant::Type(i);
  2066. if (Variant::get_type_name(t) == p_symbol) {
  2067. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2068. r_result.class_name = Variant::get_type_name(t);
  2069. return OK;
  2070. }
  2071. }
  2072. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  2073. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  2074. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2075. r_result.class_name = "@GDScript";
  2076. r_result.class_member = p_symbol;
  2077. return OK;
  2078. }
  2079. }
  2080. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2081. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2082. r_result.class_name = "@GDScript";
  2083. r_result.class_member = p_symbol;
  2084. return OK;
  2085. }
  2086. GDScriptParser p;
  2087. p.parse(p_code, p_base_path, false, "", true);
  2088. if (p.get_completion_type() == GDScriptParser::COMPLETION_NONE)
  2089. return ERR_CANT_RESOLVE;
  2090. GDScriptCompletionContext context;
  2091. context._class = p.get_completion_class();
  2092. context.block = p.get_completion_block();
  2093. context.function = p.get_completion_function();
  2094. context.base = p_owner;
  2095. context.base_path = p_base_path;
  2096. if (context._class && context._class->extends_class.size() > 0) {
  2097. bool success = false;
  2098. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  2099. if (success) {
  2100. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2101. r_result.class_name = context._class->extends_class[0];
  2102. r_result.class_member = p_symbol;
  2103. return OK;
  2104. }
  2105. }
  2106. bool isfunction = false;
  2107. switch (p.get_completion_type()) {
  2108. case GDScriptParser::COMPLETION_GET_NODE:
  2109. case GDScriptParser::COMPLETION_NONE: {
  2110. } break;
  2111. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2112. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2113. r_result.class_name = Variant::get_type_name(p.get_completion_built_in_constant());
  2114. r_result.class_member = p_symbol;
  2115. return OK;
  2116. } break;
  2117. case GDScriptParser::COMPLETION_FUNCTION: {
  2118. if (context._class && context._class->functions.size()) {
  2119. for (int i = 0; i < context._class->functions.size(); i++) {
  2120. if (context._class->functions[i]->name == p_symbol) {
  2121. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2122. r_result.location = context._class->functions[i]->line;
  2123. return OK;
  2124. }
  2125. }
  2126. }
  2127. Ref<GDScript> parent = _get_parent_class(context);
  2128. while (parent.is_valid()) {
  2129. int line = parent->get_member_line(p_symbol);
  2130. if (line >= 0) {
  2131. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2132. r_result.location = line;
  2133. r_result.script = parent;
  2134. return OK;
  2135. }
  2136. parent = parent->get_base();
  2137. }
  2138. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2139. print_line("identifier: " + String(identifier.obj_type));
  2140. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2141. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2142. r_result.class_name = identifier.obj_type;
  2143. r_result.class_member = p_symbol;
  2144. return OK;
  2145. }
  2146. } break;
  2147. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2148. //check if a function
  2149. if (p.get_completion_identifier_is_function()) {
  2150. if (context._class && context._class->functions.size()) {
  2151. for (int i = 0; i < context._class->functions.size(); i++) {
  2152. if (context._class->functions[i]->name == p_symbol) {
  2153. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2154. r_result.location = context._class->functions[i]->line;
  2155. return OK;
  2156. }
  2157. }
  2158. }
  2159. Ref<GDScript> parent = _get_parent_class(context);
  2160. while (parent.is_valid()) {
  2161. int line = parent->get_member_line(p_symbol);
  2162. if (line >= 0) {
  2163. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2164. r_result.location = line;
  2165. r_result.script = parent;
  2166. return OK;
  2167. }
  2168. parent = parent->get_base();
  2169. }
  2170. GDScriptCompletionIdentifier identifier = _get_native_class(context);
  2171. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2172. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2173. r_result.class_name = identifier.obj_type;
  2174. r_result.class_member = p_symbol;
  2175. return OK;
  2176. }
  2177. } else {
  2178. GDScriptCompletionIdentifier gdi = _get_native_class(context);
  2179. if (gdi.obj_type != StringName()) {
  2180. bool valid;
  2181. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_symbol, &valid);
  2182. if (t != Variant::NIL && valid) {
  2183. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2184. r_result.class_name = gdi.obj_type;
  2185. r_result.class_member = p_symbol;
  2186. return OK;
  2187. }
  2188. }
  2189. const GDScriptParser::BlockNode *block = context.block;
  2190. //search in blocks going up (local var?)
  2191. while (block) {
  2192. for (int i = 0; i < block->statements.size(); i++) {
  2193. if (block->statements[i]->line > p.get_completion_line())
  2194. continue;
  2195. if (block->statements[i]->type == GDScriptParser::BlockNode::TYPE_LOCAL_VAR) {
  2196. const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(block->statements[i]);
  2197. if (lv->assign && lv->name == p_symbol) {
  2198. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2199. r_result.location = block->statements[i]->line;
  2200. return OK;
  2201. }
  2202. }
  2203. }
  2204. block = block->parent_block;
  2205. }
  2206. //guess from function arguments
  2207. if (context.function && context.function->name != StringName()) {
  2208. for (int i = 0; i < context.function->arguments.size(); i++) {
  2209. if (context.function->arguments[i] == p_symbol) {
  2210. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2211. r_result.location = context.function->line;
  2212. return OK;
  2213. }
  2214. }
  2215. }
  2216. //guess in class constants
  2217. if (context._class->constant_expressions.has(p_symbol)) {
  2218. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2219. r_result.location = context._class->constant_expressions[p_symbol].expression->line;
  2220. return OK;
  2221. }
  2222. //guess in class variables
  2223. if (!(context.function && context.function->_static)) {
  2224. for (int i = 0; i < context._class->variables.size(); i++) {
  2225. if (context._class->variables[i].identifier == p_symbol) {
  2226. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2227. r_result.location = context._class->variables[i].line;
  2228. return OK;
  2229. }
  2230. }
  2231. }
  2232. //guess in autoloads as singletons
  2233. List<PropertyInfo> props;
  2234. ProjectSettings::get_singleton()->get_property_list(&props);
  2235. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2236. String s = E->get().name;
  2237. if (!s.begins_with("autoload/"))
  2238. continue;
  2239. String name = s.get_slice("/", 1);
  2240. if (name == String(p_symbol)) {
  2241. String path = ProjectSettings::get_singleton()->get(s);
  2242. if (path.begins_with("*")) {
  2243. String script = path.substr(1, path.length());
  2244. if (!script.ends_with(".gd")) {
  2245. //not a script, try find the script anyway,
  2246. //may have some success
  2247. script = script.get_basename() + ".gd";
  2248. }
  2249. if (FileAccess::exists(script)) {
  2250. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2251. r_result.location = 0;
  2252. r_result.script = ResourceLoader::load(script);
  2253. return OK;
  2254. }
  2255. }
  2256. }
  2257. }
  2258. //global
  2259. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2260. if (classes.has(p_symbol)) {
  2261. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2262. if (value.get_type() == Variant::OBJECT) {
  2263. Object *obj = value;
  2264. if (obj) {
  2265. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2266. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2267. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2268. } else {
  2269. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2270. r_result.class_name = obj->get_class();
  2271. }
  2272. // proxy class remove the underscore.
  2273. if (r_result.class_name.begins_with("_")) {
  2274. r_result.class_name = r_result.class_name.right(1);
  2275. }
  2276. return OK;
  2277. }
  2278. } else {
  2279. /*
  2280. // Because get_integer_constant_enum and get_integer_constant dont work on @GlobalScope
  2281. // We cannot determine the exact nature of the identifier here
  2282. // Otherwise these codes would work
  2283. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2284. if (enumName != NULL) {
  2285. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2286. r_result.class_name = "@GlobalScope";
  2287. r_result.class_member = enumName;
  2288. return OK;
  2289. }
  2290. else {
  2291. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2292. r_result.class_name = "@GlobalScope";
  2293. r_result.class_member = p_symbol;
  2294. return OK;
  2295. }*/
  2296. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2297. r_result.class_name = "@GlobalScope";
  2298. r_result.class_member = p_symbol;
  2299. return OK;
  2300. }
  2301. }
  2302. }
  2303. } break;
  2304. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2305. } break;
  2306. case GDScriptParser::COMPLETION_METHOD:
  2307. isfunction = true;
  2308. case GDScriptParser::COMPLETION_INDEX: {
  2309. const GDScriptParser::Node *node = p.get_completion_node();
  2310. if (node->type != GDScriptParser::Node::TYPE_OPERATOR)
  2311. break;
  2312. GDScriptCompletionIdentifier t;
  2313. if (_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  2314. if (t.type == Variant::OBJECT && t.obj_type == "GDScriptNativeClass") {
  2315. //native enum
  2316. Ref<GDScriptNativeClass> gdn = t.value;
  2317. if (gdn.is_valid()) {
  2318. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2319. r_result.class_name = gdn->get_name();
  2320. r_result.class_member = p_symbol;
  2321. return OK;
  2322. }
  2323. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  2324. Ref<GDScript> on_script;
  2325. if (t.value.get_type()) {
  2326. Object *obj = t.value;
  2327. if (obj) {
  2328. on_script = obj->get_script();
  2329. if (on_script.is_valid()) {
  2330. int loc = on_script->get_member_line(p_symbol);
  2331. if (loc >= 0) {
  2332. r_result.script = on_script;
  2333. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2334. r_result.location = loc;
  2335. return OK;
  2336. }
  2337. }
  2338. }
  2339. }
  2340. if (ClassDB::has_method(t.obj_type, p_symbol)) {
  2341. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2342. r_result.class_name = t.obj_type;
  2343. r_result.class_member = p_symbol;
  2344. return OK;
  2345. }
  2346. StringName enumName = ClassDB::get_integer_constant_enum(t.obj_type, p_symbol, true);
  2347. if (enumName != StringName()) {
  2348. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2349. r_result.class_name = t.obj_type;
  2350. r_result.class_member = enumName;
  2351. return OK;
  2352. }
  2353. bool success;
  2354. ClassDB::get_integer_constant(t.obj_type, p_symbol, &success);
  2355. if (success) {
  2356. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2357. r_result.class_name = t.obj_type;
  2358. r_result.class_member = p_symbol;
  2359. return OK;
  2360. }
  2361. ClassDB::get_property_type(t.obj_type, p_symbol, &success);
  2362. if (success) {
  2363. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2364. r_result.class_name = t.obj_type;
  2365. r_result.class_member = p_symbol;
  2366. return OK;
  2367. }
  2368. } else {
  2369. Variant::CallError ce;
  2370. Variant v = Variant::construct(t.type, NULL, 0, ce);
  2371. bool valid;
  2372. v.get_numeric_constant_value(t.type, p_symbol, &valid);
  2373. if (valid) {
  2374. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2375. r_result.class_name = Variant::get_type_name(t.type);
  2376. r_result.class_member = p_symbol;
  2377. return OK;
  2378. }
  2379. //todo check all inputevent types for property
  2380. v.get(p_symbol, &valid);
  2381. if (valid) {
  2382. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2383. r_result.class_name = Variant::get_type_name(t.type);
  2384. r_result.class_member = p_symbol;
  2385. return OK;
  2386. }
  2387. if (v.has_method(p_symbol)) {
  2388. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2389. r_result.class_name = Variant::get_type_name(t.type);
  2390. r_result.class_member = p_symbol;
  2391. return OK;
  2392. }
  2393. }
  2394. }
  2395. } break;
  2396. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2397. return ERR_CANT_RESOLVE;
  2398. } break;
  2399. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2400. GDScriptCompletionIdentifier cid = _get_native_class(context);
  2401. if (cid.obj_type != StringName()) {
  2402. List<MethodInfo> vm;
  2403. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  2404. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  2405. if (p_symbol == E->get().name) {
  2406. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2407. r_result.class_name = cid.obj_type;
  2408. r_result.class_member = p_symbol;
  2409. return OK;
  2410. }
  2411. }
  2412. }
  2413. } break;
  2414. case GDScriptParser::COMPLETION_YIELD: {
  2415. return ERR_CANT_RESOLVE;
  2416. } break;
  2417. }
  2418. return ERR_CANT_RESOLVE;
  2419. }
  2420. #endif