gd_editor.cpp 83 KB

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