gd_editor.cpp 62 KB

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