gd_editor.cpp 84 KB

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