gd_editor.cpp 65 KB

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