gd_editor.cpp 65 KB

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