gd_editor.cpp 78 KB

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