gd_editor.cpp 66 KB

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