gd_editor.cpp 86 KB

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