gd_editor.cpp 86 KB

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