gdscript_editor.cpp 91 KB

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