gd_editor.cpp 65 KB

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