gd_editor.cpp 86 KB

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