gd_editor.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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-2014 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. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  32. p_delimiters->push_back("#");
  33. p_delimiters->push_back("\"\"\"");
  34. }
  35. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  36. p_delimiters->push_back("\" \"");
  37. p_delimiters->push_back("' '");
  38. }
  39. String GDScriptLanguage::get_template(const String& p_class_name, const String& p_base_class_name) const {
  40. String _template = String()+
  41. "\nextends %BASE%\n\n"+
  42. "# member variables here, example:\n"+
  43. "# var a=2\n"+
  44. "# var b=\"textvar\"\n\n"+
  45. "func _ready():\n"+
  46. "\t# Initalization here\n"+
  47. "\tpass\n"+
  48. "\n"+
  49. "\n";
  50. return _template.replace("%BASE%",p_base_class_name);
  51. }
  52. 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 {
  53. GDParser parser;
  54. Error err = parser.parse(p_script,p_path.get_base_dir(),true,p_path);
  55. if (err) {
  56. r_line_error=parser.get_error_line();
  57. r_col_error=parser.get_error_column();
  58. r_test_error=parser.get_error();
  59. return false;
  60. } else {
  61. const GDParser::Node *root = parser.get_parse_tree();
  62. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,false);
  63. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  64. Map<int,String> funcs;
  65. for(int i=0;i<cl->functions.size();i++) {
  66. funcs[cl->functions[i]->line]=cl->functions[i]->name;
  67. }
  68. for(int i=0;i<cl->static_functions.size();i++) {
  69. funcs[cl->static_functions[i]->line]=cl->static_functions[i]->name;
  70. }
  71. for (Map<int,String>::Element *E=funcs.front();E;E=E->next()) {
  72. r_functions->push_back(E->get()+":"+itos(E->key()));
  73. }
  74. }
  75. return true;
  76. }
  77. bool GDScriptLanguage::has_named_classes() const {
  78. return false;
  79. }
  80. int GDScriptLanguage::find_function(const String& p_function,const String& p_code) const {
  81. GDTokenizerText tokenizer;
  82. tokenizer.set_code(p_code);
  83. int indent=0;
  84. while(tokenizer.get_token()!=GDTokenizer::TK_EOF && tokenizer.get_token()!=GDTokenizer::TK_ERROR) {
  85. if (tokenizer.get_token()==GDTokenizer::TK_NEWLINE) {
  86. indent=tokenizer.get_token_line_indent();
  87. }
  88. //print_line("TOKEN: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  89. if (indent==0 && tokenizer.get_token()==GDTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1)==GDTokenizer::TK_IDENTIFIER) {
  90. String identifier = tokenizer.get_token_identifier(1);
  91. if (identifier==p_function) {
  92. return tokenizer.get_token_line();
  93. }
  94. }
  95. tokenizer.advance();
  96. //print_line("NEXT: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  97. }
  98. return -1;
  99. }
  100. Script *GDScriptLanguage::create_script() const {
  101. return memnew( GDScript );
  102. }
  103. /* DEBUGGER FUNCTIONS */
  104. bool GDScriptLanguage::debug_break_parse(const String& p_file, int p_line,const String& p_error) {
  105. //break because of parse error
  106. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  107. _debug_parse_err_line=p_line;
  108. _debug_parse_err_file=p_file;
  109. _debug_error=p_error;
  110. ScriptDebugger::get_singleton()->debug(this,false);
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. bool GDScriptLanguage::debug_break(const String& p_error,bool p_allow_continue) {
  117. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  118. _debug_parse_err_line=-1;
  119. _debug_parse_err_file="";
  120. _debug_error=p_error;
  121. ScriptDebugger::get_singleton()->debug(this,p_allow_continue);
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }
  127. String GDScriptLanguage::debug_get_error() const {
  128. return _debug_error;
  129. }
  130. int GDScriptLanguage::debug_get_stack_level_count() const {
  131. if (_debug_parse_err_line>=0)
  132. return 1;
  133. return _debug_call_stack_pos;
  134. }
  135. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  136. if (_debug_parse_err_line>=0)
  137. return _debug_parse_err_line;
  138. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,-1);
  139. int l = _debug_call_stack_pos - p_level -1;
  140. return *(_call_stack[l].line);
  141. }
  142. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  143. if (_debug_parse_err_line>=0)
  144. return "";
  145. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  146. int l = _debug_call_stack_pos - p_level -1;
  147. return _call_stack[l].function->get_name();
  148. }
  149. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  150. if (_debug_parse_err_line>=0)
  151. return _debug_parse_err_file;
  152. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  153. int l = _debug_call_stack_pos - p_level -1;
  154. return _call_stack[l].function->get_script()->get_path();
  155. }
  156. 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) {
  157. if (_debug_parse_err_line>=0)
  158. return;
  159. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  160. int l = _debug_call_stack_pos - p_level -1;
  161. GDFunction *f = _call_stack[l].function;
  162. List<Pair<StringName,int> > locals;
  163. f->debug_get_stack_member_state(*_call_stack[l].line,&locals);
  164. for( List<Pair<StringName,int> >::Element *E = locals.front();E;E=E->next() ) {
  165. p_locals->push_back(E->get().first);
  166. p_values->push_back(_call_stack[l].stack[E->get().second]);
  167. }
  168. }
  169. 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) {
  170. if (_debug_parse_err_line>=0)
  171. return;
  172. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  173. int l = _debug_call_stack_pos - p_level -1;
  174. GDInstance *instance = _call_stack[l].instance;
  175. if (!instance)
  176. return;
  177. Ref<GDScript> script = instance->get_script();
  178. ERR_FAIL_COND( script.is_null() );
  179. const Map<StringName,GDScript::MemberInfo>& mi = script->debug_get_member_indices();
  180. for(const Map<StringName,GDScript::MemberInfo>::Element *E=mi.front();E;E=E->next()) {
  181. p_members->push_back(E->key());
  182. p_values->push_back( instance->debug_get_member_by_index(E->get().index));
  183. }
  184. }
  185. void GDScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  186. //no globals are really reachable in gdscript
  187. }
  188. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems,int p_max_depth) {
  189. if (_debug_parse_err_line>=0)
  190. return "";
  191. return "";
  192. }
  193. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  194. p_extensions->push_back("gd");
  195. }
  196. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  197. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  198. p_functions->push_back(GDFunctions::get_info(GDFunctions::Function(i)));
  199. }
  200. }
  201. void GDScriptLanguage::get_public_constants(List<Pair<String,Variant> > *p_constants) const {
  202. Pair<String,Variant> pi;
  203. pi.first="PI";
  204. pi.second=Math_PI;
  205. p_constants->push_back(pi);
  206. }
  207. String GDScriptLanguage::make_function(const String& p_class,const String& p_name,const StringArray& p_args) const {
  208. String s="func "+p_name+"(";
  209. if (p_args.size()) {
  210. s+=" ";
  211. for(int i=0;i<p_args.size();i++) {
  212. if (i>0)
  213. s+=", ";
  214. s+=p_args[i];
  215. }
  216. s+=" ";
  217. }
  218. s+="):\n\tpass # replace with function body\n";
  219. return s;
  220. }
  221. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  222. struct GDCompletionIdentifier {
  223. StringName obj_type;
  224. Variant::Type type;
  225. Variant value; //im case there is a value, also return it
  226. };
  227. static GDCompletionIdentifier _get_type_from_variant(const Variant& p_variant) {
  228. GDCompletionIdentifier t;
  229. t.type=p_variant.get_type();
  230. t.value=p_variant;
  231. if (p_variant.get_type()==Variant::OBJECT) {
  232. Object *obj = p_variant;
  233. if (obj) {
  234. //if (obj->cast_to<GDNativeClass>()) {
  235. // t.obj_type=obj->cast_to<GDNativeClass>()->get_name();
  236. // t.value=Variant();
  237. //} else {
  238. t.obj_type=obj->get_type();
  239. //}
  240. }
  241. }
  242. return t;
  243. }
  244. static GDCompletionIdentifier _get_type_from_pinfo(const PropertyInfo& p_info) {
  245. GDCompletionIdentifier t;
  246. t.type=p_info.type;
  247. if (p_info.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  248. t.obj_type=p_info.hint_string;
  249. }
  250. return t;
  251. }
  252. struct GDCompletionContext {
  253. const GDParser::ClassNode *_class;
  254. const GDParser::FunctionNode *function;
  255. const GDParser::BlockNode *block;
  256. Object* base;
  257. String base_path;
  258. };
  259. static Ref<Reference> _get_parent_class(GDCompletionContext& context) {
  260. if (context._class->extends_used) {
  261. //do inheritance
  262. String path = context._class->extends_file;
  263. Ref<GDScript> script;
  264. Ref<GDNativeClass> native;
  265. if (path!="") {
  266. //path (and optionally subclasses)
  267. if (path.is_rel_path()) {
  268. path=context.base_path.plus_file(path);
  269. }
  270. script = ResourceLoader::load(path);
  271. if (script.is_null()) {
  272. return REF();
  273. }
  274. if (script->is_valid()) {
  275. return REF();
  276. }
  277. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  278. if (context._class->extends_class.size()) {
  279. for(int i=0;i<context._class->extends_class.size();i++) {
  280. String sub = context._class->extends_class[i];
  281. if (script->get_subclasses().has(sub)) {
  282. script=script->get_subclasses()[sub];
  283. } else {
  284. return REF();
  285. }
  286. }
  287. }
  288. if (script.is_valid())
  289. return script;
  290. } else {
  291. if (context._class->extends_class.size()==0) {
  292. ERR_PRINT("BUG");
  293. return REF();
  294. }
  295. String base=context._class->extends_class[0];
  296. const GDParser::ClassNode *p = context._class->owner;
  297. Ref<GDScript> base_class;
  298. #if 0
  299. while(p) {
  300. if (p->subclasses.has(base)) {
  301. base_class=p->subclasses[base];
  302. break;
  303. }
  304. p=p->_owner;
  305. }
  306. #endif
  307. if (base_class.is_valid()) {
  308. #if 0
  309. for(int i=1;i<context._class->extends_class.size();i++) {
  310. String subclass=context._class->extends_class[i];
  311. if (base_class->subclasses.has(subclass)) {
  312. base_class=base_class->subclasses[subclass];
  313. } else {
  314. print_line("Could not find subclass: "+subclass);
  315. return _get_type_from_class(context); //fail please
  316. }
  317. }
  318. script=base_class;
  319. #endif
  320. } else {
  321. if (context._class->extends_class.size()>1) {
  322. return REF();
  323. }
  324. //if not found, try engine classes
  325. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  326. return REF();
  327. }
  328. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  329. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  330. if (!native.is_valid()) {
  331. print_line("Global not a class: '"+base+"'");
  332. }
  333. return native;
  334. }
  335. }
  336. }
  337. return Ref<Reference>();
  338. }
  339. static GDCompletionIdentifier _get_native_class(GDCompletionContext& context) {
  340. //eeh...
  341. GDCompletionIdentifier id;
  342. id.type=Variant::NIL;
  343. REF pc = _get_parent_class(context);
  344. if (!pc.is_valid()) {
  345. return id;
  346. }
  347. Ref<GDNativeClass> nc = pc;
  348. Ref<GDScript> s = pc;
  349. if (s.is_null() && nc.is_null()) {
  350. return id;
  351. }
  352. while(!s.is_null()) {
  353. nc=s->get_native();
  354. s=s->get_base();
  355. }
  356. if (nc.is_null()) {
  357. return id;
  358. }
  359. id.type=Variant::OBJECT;
  360. if (context.base)
  361. id.value=context.base;
  362. id.obj_type=nc->get_name();
  363. return id;
  364. }
  365. static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type);
  366. static bool _guess_expression_type(GDCompletionContext& context,const GDParser::Node* p_node,int p_line,GDCompletionIdentifier &r_type) {
  367. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  368. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  369. r_type=_get_type_from_variant(cn->value);
  370. return true;
  371. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  372. r_type.type=Variant::DICTIONARY;
  373. //what the heck, fill it anyway
  374. const GDParser::DictionaryNode *an = static_cast<const GDParser::DictionaryNode *>(p_node);
  375. Dictionary d;
  376. for(int i=0;i<an->elements.size();i++) {
  377. GDCompletionIdentifier k;
  378. if (_guess_expression_type(context,an->elements[i].key,p_line,k) && k.value.get_type()!=Variant::NIL) {
  379. GDCompletionIdentifier v;
  380. if (_guess_expression_type(context,an->elements[i].value,p_line,v)) {
  381. d[k.value]=v.value;
  382. }
  383. }
  384. }
  385. r_type.value=d;
  386. return true;
  387. } else if (p_node->type==GDParser::Node::TYPE_ARRAY) {
  388. r_type.type=Variant::ARRAY;
  389. //what the heck, fill it anyway
  390. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(p_node);
  391. Array arr;
  392. arr.resize(an->elements.size());
  393. for(int i=0;i<an->elements.size();i++) {
  394. GDCompletionIdentifier ci;
  395. if (_guess_expression_type(context,an->elements[i],p_line,ci)) {
  396. arr[i]=ci.value;
  397. }
  398. }
  399. r_type.value=arr;
  400. return true;
  401. } else if (p_node->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  402. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode*>(p_node)->function);
  403. r_type=_get_type_from_pinfo(mi.return_val);
  404. return true;
  405. } else if (p_node->type==GDParser::Node::TYPE_IDENTIFIER) {
  406. return _guess_identifier_type(context,p_line-1,static_cast<const GDParser::IdentifierNode *>(p_node)->name,r_type);
  407. } else if (p_node->type==GDParser::Node::TYPE_SELF) {
  408. //eeh...
  409. r_type=_get_native_class(context);
  410. return r_type.type!=Variant::NIL;
  411. } else if (p_node->type==GDParser::Node::TYPE_OPERATOR) {
  412. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  413. if (op->op==GDParser::OperatorNode::OP_CALL) {
  414. if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  415. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  416. r_type.type=tn->vtype;
  417. return true;
  418. } else if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  419. const GDParser::BuiltInFunctionNode *bin = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  420. return _guess_expression_type(context,bin,p_line,r_type);
  421. } else if (op->arguments.size()>1 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  422. GDCompletionIdentifier base;
  423. if (!_guess_expression_type(context,op->arguments[0],p_line,base))
  424. return false;
  425. StringName id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  426. if (base.type==Variant::OBJECT) {
  427. if (ObjectTypeDB::has_method(base.obj_type,id)) {
  428. #ifdef TOOLS_ENABLED
  429. MethodBind *mb = ObjectTypeDB::get_method(base.obj_type,id);
  430. PropertyInfo pi = mb->get_argument_info(-1);
  431. //try calling the function if constant and all args are constant, should not crash..
  432. Object *baseptr = base.value;
  433. if (baseptr && mb->is_const() && pi.type==Variant::OBJECT) {
  434. bool all_valid=true;
  435. Vector<Variant> args;
  436. for(int i=2;i<op->arguments.size();i++) {
  437. GDCompletionIdentifier arg;
  438. if (_guess_expression_type(context,op->arguments[i],p_line,arg)) {
  439. if (arg.value.get_type()!=Variant::NIL && arg.value.get_type()!=Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  440. args.push_back(arg.value);
  441. } else {
  442. all_valid=false;
  443. break;
  444. }
  445. } else {
  446. all_valid=false;
  447. }
  448. }
  449. if (all_valid) {
  450. Vector<const Variant*> argptr;
  451. for(int i=0;i<args.size();i++) {
  452. argptr.push_back(&args[i]);
  453. }
  454. Variant::CallError ce;
  455. Variant ret=mb->call(baseptr,argptr.ptr(),argptr.size(),ce);
  456. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  457. if (ret.get_type()!=Variant::OBJECT || ret.operator Object*()!=NULL) {
  458. r_type=_get_type_from_variant(ret);
  459. return true;
  460. }
  461. }
  462. }
  463. }
  464. r_type.type=pi.type;
  465. if (pi.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  466. r_type.obj_type=pi.hint_string;
  467. }
  468. return true;
  469. #else
  470. return false;
  471. #endif
  472. } else {
  473. return false;
  474. }
  475. } else {
  476. //method for some variant..
  477. Variant::CallError ce;
  478. Variant v = Variant::construct(base.type,NULL,0,ce);
  479. List<MethodInfo> mi;
  480. v.get_method_list(&mi);
  481. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  482. if (E->get().name==id.operator String()) {
  483. MethodInfo mi = E->get();
  484. r_type.type=mi.return_val.type;
  485. if (mi.return_val.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  486. r_type.obj_type=mi.return_val.hint_string;
  487. }
  488. return true;
  489. }
  490. }
  491. }
  492. }
  493. } else if (op->op==GDParser::OperatorNode::OP_INDEX || op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  494. GDCompletionIdentifier p1;
  495. GDCompletionIdentifier p2;
  496. if (op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  497. if (op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  498. String id = static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name;
  499. p2.type=Variant::STRING;
  500. p2.value=id;
  501. }
  502. } else {
  503. if (op->arguments[1]) {
  504. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  505. return false;
  506. }
  507. }
  508. }
  509. if (op->arguments[0]->type==GDParser::Node::TYPE_ARRAY) {
  510. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(op->arguments[0]);
  511. if (p2.value.is_num()) {
  512. int index = p2.value;
  513. if (index<0 || index>=an->elements.size())
  514. return false;
  515. return _guess_expression_type(context,an->elements[index],p_line,r_type);
  516. }
  517. } else if (op->arguments[0]->type==GDParser::Node::TYPE_DICTIONARY) {
  518. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode *>(op->arguments[0]);
  519. if (p2.value.get_type()==Variant::NIL)
  520. return false;
  521. for(int i=0;i<dn->elements.size();i++) {
  522. GDCompletionIdentifier k;
  523. if (!_guess_expression_type(context,dn->elements[i].key,p_line,k)) {
  524. return false;
  525. }
  526. if (k.value.get_type()==Variant::NIL)
  527. return false;
  528. if (k.value==p2.value) {
  529. return _guess_expression_type(context,dn->elements[i].value,p_line,r_type);
  530. }
  531. }
  532. } else {
  533. if (op->arguments[0]) {
  534. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  535. return false;
  536. }
  537. }
  538. if (p1.value.get_type()==Variant::OBJECT) {
  539. //??
  540. } else if (p1.value.get_type()!=Variant::NIL) {
  541. bool valid;
  542. Variant ret = p1.value.get(p2.value,&valid);
  543. if (valid) {
  544. r_type=_get_type_from_variant(ret);
  545. return true;
  546. }
  547. } else {
  548. if (p1.type!=Variant::NIL) {
  549. Variant::CallError ce;
  550. Variant base = Variant::construct(p1.type,NULL,0,ce);
  551. bool valid;
  552. Variant ret = base.get(p2.value,&valid);
  553. if (valid) {
  554. r_type=_get_type_from_variant(ret);
  555. return true;
  556. }
  557. }
  558. }
  559. }
  560. } else {
  561. Variant::Operator vop = Variant::OP_MAX;
  562. switch(op->op) {
  563. case GDParser::OperatorNode::OP_ADD: vop=Variant::OP_ADD; break;
  564. case GDParser::OperatorNode::OP_SUB: vop=Variant::OP_SUBSTRACT; break;
  565. case GDParser::OperatorNode::OP_MUL: vop=Variant::OP_MULTIPLY; break;
  566. case GDParser::OperatorNode::OP_DIV: vop=Variant::OP_DIVIDE; break;
  567. case GDParser::OperatorNode::OP_MOD: vop=Variant::OP_MODULE; break;
  568. case GDParser::OperatorNode::OP_SHIFT_LEFT: vop=Variant::OP_SHIFT_LEFT; break;
  569. case GDParser::OperatorNode::OP_SHIFT_RIGHT: vop=Variant::OP_SHIFT_RIGHT; break;
  570. case GDParser::OperatorNode::OP_BIT_AND: vop=Variant::OP_BIT_AND; break;
  571. case GDParser::OperatorNode::OP_BIT_OR: vop=Variant::OP_BIT_OR; break;
  572. case GDParser::OperatorNode::OP_BIT_XOR: vop=Variant::OP_BIT_XOR; break;
  573. default:{}
  574. }
  575. if (vop==Variant::OP_MAX)
  576. return false;
  577. GDCompletionIdentifier p1;
  578. GDCompletionIdentifier p2;
  579. if (op->arguments[0]) {
  580. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  581. return false;
  582. }
  583. }
  584. if (op->arguments.size()>1) {
  585. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  586. return false;
  587. }
  588. }
  589. Variant::CallError ce;
  590. bool v1_use_value = p1.value.get_type()!=Variant::NIL && p1.value.get_type()!=Variant::OBJECT;
  591. Variant v1 = (v1_use_value)?p1.value:Variant::construct(p1.type,NULL,0,ce);
  592. bool v2_use_value = p2.value.get_type()!=Variant::NIL && p2.value.get_type()!=Variant::OBJECT;
  593. Variant v2 = (v2_use_value)?p2.value:Variant::construct(p2.type,NULL,0,ce);
  594. // avoid potential invalid ops
  595. if ((vop==Variant::OP_DIVIDE || vop==Variant::OP_MODULE) && v2.get_type()==Variant::INT) {
  596. v2=1;
  597. v2_use_value=false;
  598. }
  599. if (vop==Variant::OP_DIVIDE && v2.get_type()==Variant::REAL) {
  600. v2=1.0;
  601. v2_use_value=false;
  602. }
  603. Variant r;
  604. bool valid;
  605. Variant::evaluate(vop,v1,v2,r,valid);
  606. if (!valid)
  607. return false;
  608. r_type.type=r.get_type();
  609. if (v1_use_value && v2_use_value)
  610. r_type.value=r;
  611. return true;
  612. }
  613. }
  614. return false;
  615. }
  616. static bool _guess_identifier_type_in_block(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  617. const GDParser::Node *last_assign=NULL;
  618. int last_assign_line=-1;
  619. for (int i=0;i<context.block->statements.size();i++) {
  620. if (context.block->statements[i]->line>p_line)
  621. continue;
  622. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  623. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  624. if (lv->assign && lv->name==p_identifier) {
  625. last_assign=lv->assign;
  626. last_assign_line=context.block->statements[i]->line;
  627. }
  628. }
  629. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
  630. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  631. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  632. if (op->arguments.size() && op->arguments[0]->type==GDParser::Node::TYPE_IDENTIFIER) {
  633. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  634. if (id->name==p_identifier) {
  635. last_assign=op->arguments[1];
  636. last_assign_line=context.block->statements[i]->line;
  637. }
  638. }
  639. }
  640. }
  641. }
  642. //use the last assignment, (then backwards?)
  643. if (last_assign) {
  644. return _guess_expression_type(context,last_assign,last_assign_line,r_type);
  645. }
  646. return false;
  647. }
  648. static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  649. //go to block first
  650. const GDParser::BlockNode *block=context.block;
  651. while(block) {
  652. GDCompletionContext c = context;
  653. c.block=block;
  654. if (_guess_identifier_type_in_block(c,p_line,p_identifier,r_type)) {
  655. return true;
  656. }
  657. block=block->parent_block;
  658. }
  659. //TODO guess identifier type of arguments (ONLY if this is a virtual function)
  660. if (context.function && context.function->name!=StringName()) {
  661. int argindex = -1;
  662. for(int i=0;i<context.function->arguments.size();i++) {
  663. if (context.function->arguments[i]==p_identifier) {
  664. argindex=i;
  665. break;
  666. }
  667. }
  668. if (argindex!=-1) {
  669. GDCompletionIdentifier id =_get_native_class(context);
  670. if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  671. //this kinda sucks but meh
  672. List<MethodInfo> vmethods;
  673. ObjectTypeDB::get_virtual_methods(id.obj_type,&vmethods);
  674. for (List<MethodInfo>::Element *E=vmethods.front();E;E=E->next()) {
  675. if (E->get().name==context.function->name && argindex<E->get().arguments.size()) {
  676. PropertyInfo arg=E->get().arguments[argindex];
  677. int scp = arg.name.find(":");
  678. if (scp!=-1) {
  679. r_type.type=Variant::OBJECT;
  680. r_type.obj_type=arg.name.substr(scp+1,arg.name.length());
  681. return true;
  682. } else {
  683. r_type.type=arg.type;
  684. if (arg.hint==PROPERTY_HINT_RESOURCE_TYPE)
  685. r_type.obj_type=arg.hint_string;
  686. return true;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. }
  693. //guess type in constant
  694. for(int i=0;i<context._class->constant_expressions.size();i++) {
  695. if (context._class->constant_expressions[i].identifier==p_identifier) {
  696. ERR_FAIL_COND_V( context._class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT, false );
  697. r_type=_get_type_from_variant(static_cast<const GDParser::ConstantNode*>(context._class->constant_expressions[i].expression)->value );
  698. return true;
  699. }
  700. }
  701. if (context.function && !context.function->_static) {
  702. for(int i=0;i<context._class->variables.size();i++) {
  703. if (context._class->variables[i].identifier==p_identifier) {
  704. if (context._class->variables[i]._export.type!=Variant::NIL) {
  705. r_type=_get_type_from_pinfo(context._class->variables[i]._export);
  706. return true;
  707. } else if (context._class->variables[i].expression) {
  708. return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  709. }
  710. }
  711. }
  712. }
  713. for(Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  714. if (E->key()==p_identifier) {
  715. r_type=_get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  716. return true;
  717. }
  718. }
  719. return false;
  720. }
  721. static void _find_identifiers_in_block(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  722. if (p_only_functions)
  723. return;
  724. for (int i=0;i<context.block->statements.size();i++) {
  725. if (context.block->statements[i]->line>p_line)
  726. continue;
  727. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  728. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  729. result.insert(lv->name.operator String());
  730. }
  731. }
  732. }
  733. static void _find_identifiers_in_class(GDCompletionContext& context,bool p_static,bool p_only_functions,Set<String>& result) {
  734. if (!p_static && !p_only_functions) {
  735. for(int i=0;i<context._class->variables.size();i++) {
  736. result.insert(context._class->variables[i].identifier);
  737. }
  738. }
  739. if (!p_only_functions) {
  740. for(int i=0;i<context._class->constant_expressions.size();i++) {
  741. result.insert(context._class->constant_expressions[i].identifier);
  742. }
  743. for(int i=0;i<context._class->subclasses.size();i++) {
  744. result.insert(context._class->subclasses[i]->name);
  745. }
  746. }
  747. for(int i=0;i<context._class->static_functions.size();i++) {
  748. if (context._class->static_functions[i]->arguments.size())
  749. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  750. else
  751. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  752. }
  753. if (!p_static) {
  754. for(int i=0;i<context._class->functions.size();i++) {
  755. if (context._class->functions[i]->arguments.size())
  756. result.insert(context._class->functions[i]->name.operator String()+"(");
  757. else
  758. result.insert(context._class->functions[i]->name.operator String()+"()");
  759. }
  760. }
  761. //globals
  762. Ref<Reference> base = _get_parent_class(context);
  763. while(true) {
  764. Ref<GDScript> script = base;
  765. Ref<GDNativeClass> nc = base;
  766. if (script.is_valid()) {
  767. if (!p_static && !p_only_functions) {
  768. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  769. result.insert(E->get().operator String());
  770. }
  771. }
  772. if (!p_only_functions) {
  773. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  774. result.insert(E->key().operator String());
  775. }
  776. }
  777. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  778. if (!p_static || E->get().is_static()) {
  779. if (E->get().get_argument_count())
  780. result.insert(E->key().operator String()+"(");
  781. else
  782. result.insert(E->key().operator String()+"()");
  783. }
  784. }
  785. if (!p_only_functions) {
  786. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  787. result.insert(E->key().operator String());
  788. }
  789. }
  790. base=script->get_base();
  791. if (base.is_null())
  792. base=script->get_native();
  793. } else if (nc.is_valid()) {
  794. if (!p_only_functions) {
  795. StringName type = nc->get_name();
  796. List<String> constants;
  797. ObjectTypeDB::get_integer_constant_list(type,&constants);
  798. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  799. result.insert(E->get());
  800. }
  801. List<MethodInfo> methods;
  802. ObjectTypeDB::get_method_list(type,&methods);
  803. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  804. if (E->get().arguments.size())
  805. result.insert(E->get().name+"(");
  806. else
  807. result.insert(E->get().name+"()");
  808. }
  809. }
  810. break;
  811. } else
  812. break;
  813. }
  814. }
  815. static void _find_identifiers(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  816. const GDParser::BlockNode *block=context.block;
  817. while(block) {
  818. GDCompletionContext c = context;
  819. c.block=block;
  820. _find_identifiers_in_block(c,p_line,p_only_functions,result);
  821. block=block->parent_block;
  822. }
  823. const GDParser::ClassNode *clss=context._class;
  824. bool _static=context.function && context.function->_static;
  825. while(clss) {
  826. GDCompletionContext c = context;
  827. c._class=clss;
  828. c.block=NULL;
  829. c.function=NULL;
  830. _find_identifiers_in_class(c,_static,p_only_functions,result);
  831. clss=clss->owner;
  832. }
  833. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  834. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  835. }
  836. static const char*_type_names[Variant::VARIANT_MAX]={
  837. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Trasnform",
  838. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  839. "Vector2Array","Vector3Array","ColorArray"};
  840. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  841. result.insert(_type_names[i]);
  842. }
  843. for(const Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  844. result.insert(E->key().operator String());
  845. }
  846. }
  847. static String _get_visual_datatype(const PropertyInfo& p_info) {
  848. String n = p_info.name;
  849. int idx = n.find(":");
  850. if (idx!=-1) {
  851. return n.substr(idx+1,n.length());
  852. }
  853. if (p_info.type==Variant::OBJECT && p_info.hint==PROPERTY_HINT_RESOURCE_TYPE)
  854. return p_info.hint_string;
  855. if (p_info.type==Variant::NIL)
  856. return "void";
  857. return Variant::get_type_name(p_info.type);
  858. }
  859. static void _make_function_hint(const GDParser::FunctionNode* p_func,int p_argidx,String& arghint) {
  860. arghint="func "+p_func->name+"(";
  861. for (int i=0;i<p_func->arguments.size();i++) {
  862. if (i>0)
  863. arghint+=", ";
  864. else
  865. arghint+=" ";
  866. if (i==p_argidx) {
  867. arghint+=String::chr(0xFFFF);
  868. }
  869. arghint+=p_func->arguments[i].operator String();
  870. int deffrom = p_func->arguments.size()-p_func->default_values.size();
  871. if (i>=deffrom) {
  872. int defidx = deffrom-i;
  873. if (defidx>=0 && defidx<p_func->default_values.size()) {
  874. if (p_func->default_values[defidx]->type==GDParser::Node::TYPE_OPERATOR) {
  875. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  876. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  877. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  878. arghint+="="+cn->value.get_construct_string();
  879. }
  880. } else {
  881. }
  882. }
  883. }
  884. if (i==p_argidx) {
  885. arghint+=String::chr(0xFFFF);
  886. }
  887. }
  888. if (p_func->arguments.size()>0)
  889. arghint+=" ";
  890. arghint+=")";
  891. }
  892. 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) {
  893. if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  894. MethodBind *m = ObjectTypeDB::get_method(id.obj_type,p_method);
  895. if (!m)
  896. return;
  897. if (p_method.operator String()=="connect") {
  898. if (p_argidx==0) {
  899. List<MethodInfo> sigs;
  900. ObjectTypeDB::get_signal_list(id.obj_type,&sigs);
  901. for (List<MethodInfo>::Element *E=sigs.front();E;E=E->next()) {
  902. result.insert("\""+E->get().name+"\"");
  903. }
  904. }
  905. /*if (p_argidx==2) {
  906. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  907. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  908. if (op->arguments.size()>)
  909. }*/
  910. } else {
  911. Object *obj=id.value;
  912. if (obj) {
  913. List<String> options;
  914. obj->get_argument_options(p_method,p_argidx,&options);
  915. for(List<String>::Element *E=options.front();E;E=E->next()) {
  916. result.insert(E->get());
  917. }
  918. }
  919. }
  920. arghint = _get_visual_datatype(m->get_argument_info(-1))+" "+p_method.operator String()+String("(");
  921. for(int i=0;i<m->get_argument_count();i++) {
  922. if (i>0)
  923. arghint+=", ";
  924. else
  925. arghint+=" ";
  926. if (i==p_argidx) {
  927. arghint+=String::chr(0xFFFF);
  928. }
  929. String n = m->get_argument_info(i).name;
  930. int dp = n.find(":");
  931. if (dp!=-1)
  932. n=n.substr(0,dp);
  933. arghint+=_get_visual_datatype(m->get_argument_info(i))+" "+n;
  934. int deffrom = m->get_argument_count()-m->get_default_argument_count();
  935. if (i>=deffrom) {
  936. int defidx = i-deffrom;
  937. if (defidx>=0 && defidx<m->get_default_argument_count()) {
  938. Variant v= m->get_default_argument(i);
  939. arghint+="="+v.get_construct_string();
  940. }
  941. }
  942. if (i==p_argidx) {
  943. arghint+=String::chr(0xFFFF);
  944. }
  945. }
  946. if (m->get_argument_count()>0)
  947. arghint+=" ";
  948. arghint+=")";
  949. }
  950. }
  951. static void _find_call_arguments(GDCompletionContext& context,const GDParser::Node* p_node, int p_line,int p_argidx, Set<String>& result, String& arghint) {
  952. if (!p_node || p_node->type!=GDParser::Node::TYPE_OPERATOR) {
  953. return;
  954. }
  955. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  956. if (op->op!=GDParser::OperatorNode::OP_CALL) {
  957. return;
  958. }
  959. if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  960. //complete built-in function
  961. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode*>(op->arguments[0]);
  962. MethodInfo mi = GDFunctions::get_info(fn->function);
  963. arghint = _get_visual_datatype(mi.return_val)+" "+GDFunctions::get_func_name(fn->function)+String("(");
  964. for(int i=0;i<mi.arguments.size();i++) {
  965. if (i>0)
  966. arghint+=", ";
  967. else
  968. arghint+=" ";
  969. if (i==p_argidx) {
  970. arghint+=String::chr(0xFFFF);
  971. }
  972. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  973. if (i==p_argidx) {
  974. arghint+=String::chr(0xFFFF);
  975. }
  976. }
  977. if (mi.arguments.size()>0)
  978. arghint+=" ";
  979. arghint+=")";
  980. } else if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  981. //complete built-in function
  982. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode*>(op->arguments[0]);
  983. List<MethodInfo> mil;
  984. Variant::get_constructor_list(tn->vtype,&mil);
  985. for(List<MethodInfo>::Element *E=mil.front();E;E=E->next()) {
  986. MethodInfo mi = E->get();
  987. if (mi.arguments.size()==0)
  988. continue;
  989. if (E->prev())
  990. arghint+="\n";
  991. arghint += Variant::get_type_name(tn->vtype)+" "+Variant::get_type_name(tn->vtype)+String("(");
  992. for(int i=0;i<mi.arguments.size();i++) {
  993. if (i>0)
  994. arghint+=", ";
  995. else
  996. arghint+=" ";
  997. if (i==p_argidx) {
  998. arghint+=String::chr(0xFFFF);
  999. }
  1000. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  1001. if (i==p_argidx) {
  1002. arghint+=String::chr(0xFFFF);
  1003. }
  1004. }
  1005. if (mi.arguments.size()>0)
  1006. arghint+=" ";
  1007. arghint+=")";
  1008. }
  1009. } else if (op->arguments.size()>=2 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  1010. //make sure identifier exists...
  1011. const GDParser::IdentifierNode *id=static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1012. if (op->arguments[0]->type==GDParser::Node::TYPE_SELF) {
  1013. //self, look up
  1014. for(int i=0;i<context._class->static_functions.size();i++) {
  1015. if (context._class->static_functions[i]->name==id->name) {
  1016. _make_function_hint(context._class->static_functions[i],p_argidx,arghint);
  1017. return;
  1018. }
  1019. }
  1020. if (context.function && !context.function->_static) {
  1021. for(int i=0;i<context._class->functions.size();i++) {
  1022. if (context._class->functions[i]->name==id->name) {
  1023. _make_function_hint(context._class->functions[i],p_argidx,arghint);
  1024. return;
  1025. }
  1026. }
  1027. }
  1028. Ref<Reference> base = _get_parent_class(context);
  1029. while(true) {
  1030. Ref<GDScript> script = base;
  1031. Ref<GDNativeClass> nc = base;
  1032. if (script.is_valid()) {
  1033. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1034. if (E->key()==id->name) {
  1035. if (context.function && context.function->_static && !E->get().is_static())
  1036. continue;
  1037. arghint = "func "+id->name.operator String()+String("(");
  1038. for(int i=0;i<E->get().get_argument_count();i++) {
  1039. if (i>0)
  1040. arghint+=", ";
  1041. else
  1042. arghint+=" ";
  1043. if (i==p_argidx) {
  1044. arghint+=String::chr(0xFFFF);
  1045. }
  1046. arghint+=E->get().get_argument_name(i);
  1047. int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
  1048. if (i>=deffrom) {
  1049. int defidx = deffrom-i;
  1050. if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
  1051. arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
  1052. }
  1053. }
  1054. if (i==p_argidx) {
  1055. arghint+=String::chr(0xFFFF);
  1056. }
  1057. }
  1058. if (E->get().get_argument_count()>0)
  1059. arghint+=" ";
  1060. arghint+=")";
  1061. return;
  1062. }
  1063. }
  1064. base=script->get_base();
  1065. if (base.is_null())
  1066. base=script->get_native();
  1067. } else if (nc.is_valid()) {
  1068. if (context.function && !context.function->_static) {
  1069. GDCompletionIdentifier ci;
  1070. ci.type=Variant::OBJECT;
  1071. ci.obj_type=nc->get_name();
  1072. if (!context._class->owner)
  1073. ci.value=context.base;
  1074. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1075. //guess type..
  1076. /*
  1077. List<MethodInfo> methods;
  1078. ObjectTypeDB::get_method_list(type,&methods);
  1079. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1080. //if (E->get().arguments.size())
  1081. // result.insert(E->get().name+"(");
  1082. //else
  1083. // result.insert(E->get().name+"()");
  1084. }*/
  1085. }
  1086. break;
  1087. } else
  1088. break;
  1089. }
  1090. } else {
  1091. GDCompletionIdentifier ci;
  1092. if (_guess_expression_type(context,op->arguments[0],p_line,ci)) {
  1093. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1094. return;
  1095. }
  1096. }
  1097. }
  1098. #if 0
  1099. bool _static=context.function->_static;
  1100. for(int i=0;i<context._class->static_functions.size();i++) {
  1101. if (context._class->static_functions[i]->arguments.size())
  1102. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1103. else
  1104. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1105. }
  1106. if (!p_static) {
  1107. for(int i=0;i<context._class->functions.size();i++) {
  1108. if (context._class->functions[i]->arguments.size())
  1109. result.insert(context._class->functions[i]->name.operator String()+"(");
  1110. else
  1111. result.insert(context._class->functions[i]->name.operator String()+"()");
  1112. }
  1113. }
  1114. Ref<Reference> base = _get_parent_class(context);
  1115. while(true) {
  1116. Ref<GDScript> script = base;
  1117. Ref<GDNativeClass> nc = base;
  1118. if (script.is_valid()) {
  1119. if (!p_static && !p_only_functions) {
  1120. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1121. result.insert(E->get().operator String());
  1122. }
  1123. }
  1124. if (!p_only_functions) {
  1125. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1126. result.insert(E->key().operator String());
  1127. }
  1128. }
  1129. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1130. if (!p_static || E->get().is_static()) {
  1131. if (E->get().get_argument_count())
  1132. result.insert(E->key().operator String()+"(");
  1133. else
  1134. result.insert(E->key().operator String()+"()");
  1135. }
  1136. }
  1137. if (!p_only_functions) {
  1138. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1139. result.insert(E->key().operator String());
  1140. }
  1141. }
  1142. base=script->get_base();
  1143. if (base.is_null())
  1144. base=script->get_native();
  1145. } else if (nc.is_valid()) {
  1146. if (!p_only_functions) {
  1147. StringName type = nc->get_name();
  1148. List<String> constants;
  1149. ObjectTypeDB::get_integer_constant_list(type,&constants);
  1150. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1151. result.insert(E->get());
  1152. }
  1153. List<MethodInfo> methods;
  1154. ObjectTypeDB::get_method_list(type,&methods);
  1155. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1156. if (E->get().arguments.size())
  1157. result.insert(E->get().name+"(");
  1158. else
  1159. result.insert(E->get().name+"()");
  1160. }
  1161. }
  1162. break;
  1163. } else
  1164. break;
  1165. }
  1166. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1167. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1168. }
  1169. #endif
  1170. }
  1171. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1172. /* bugs:
  1173. a[0].<complete> does not work
  1174. functions should end in (
  1175. when completing virtuals, ask for full back
  1176. */
  1177. //print_line( p_code.replace(String::chr(0xFFFF),"<cursor>"));
  1178. GDParser p;
  1179. Error err = p.parse(p_code,p_base_path);
  1180. bool isfunction=false;
  1181. Set<String> options;
  1182. GDCompletionContext context;
  1183. context._class=p.get_completion_class();
  1184. context.block=p.get_completion_block();
  1185. context.function=p.get_completion_function();
  1186. context.base=p_owner;
  1187. context.base_path=p_base_path;
  1188. switch(p.get_completion_type()) {
  1189. case GDParser::COMPLETION_NONE: {
  1190. print_line("No completion");
  1191. } break;
  1192. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1193. print_line("Built in type constant");
  1194. List<StringName> constants;
  1195. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(),&constants);
  1196. for(List<StringName>::Element *E=constants.front();E;E=E->next()) {
  1197. options.insert(E->get().operator String());
  1198. }
  1199. } break;
  1200. case GDParser::COMPLETION_FUNCTION:
  1201. isfunction=true;
  1202. case GDParser::COMPLETION_IDENTIFIER: {
  1203. _find_identifiers(context,p.get_completion_line(),isfunction,options);
  1204. } break;
  1205. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1206. print_line("parent function");
  1207. } break;
  1208. case GDParser::COMPLETION_METHOD:
  1209. isfunction=true;
  1210. case GDParser::COMPLETION_INDEX: {
  1211. print_line("index");
  1212. const GDParser::Node *node = p.get_completion_node();
  1213. if (node->type!=GDParser::Node::TYPE_OPERATOR)
  1214. break;
  1215. GDCompletionIdentifier t;
  1216. if (_guess_expression_type(context,static_cast<const GDParser::OperatorNode *>(node)->arguments[0],p.get_completion_line(),t)) {
  1217. if (t.type==Variant::OBJECT && t.obj_type!=StringName()) {
  1218. if (!isfunction) {
  1219. ObjectTypeDB::get_integer_constant_list(t.obj_type,r_options);
  1220. }
  1221. List<MethodInfo> mi;
  1222. ObjectTypeDB::get_method_list(t.obj_type,&mi);
  1223. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1224. if (E->get().arguments.size())
  1225. options.insert(E->get().name+"(");
  1226. else
  1227. options.insert(E->get().name+"()");
  1228. }
  1229. } else {
  1230. if (t.value.get_type()==Variant::NIL) {
  1231. Variant::CallError ce;
  1232. t.value=Variant::construct(t.type,NULL,0,ce);
  1233. }
  1234. if (!isfunction) {
  1235. List<PropertyInfo> pl;
  1236. t.value.get_property_list(&pl);
  1237. for (List<PropertyInfo>::Element *E=pl.front();E;E=E->next()) {
  1238. if (E->get().name.find("/")==-1)
  1239. options.insert(E->get().name);
  1240. }
  1241. }
  1242. List<MethodInfo> mi;
  1243. t.value.get_method_list(&mi);
  1244. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1245. if (E->get().arguments.size())
  1246. options.insert(E->get().name+"(");
  1247. else
  1248. options.insert(E->get().name+"()");
  1249. }
  1250. }
  1251. }
  1252. } break;
  1253. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1254. _find_call_arguments(context,p.get_completion_node(),p.get_completion_line(),p.get_completion_argument_index(),options,r_call_hint);
  1255. } break;
  1256. }
  1257. for(Set<String>::Element *E=options.front();E;E=E->next()) {
  1258. r_options->push_back(E->get());
  1259. }
  1260. return OK;
  1261. }
  1262. #else
  1263. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1264. return OK;
  1265. }
  1266. #endif
  1267. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  1268. Vector<String> lines = p_code.split("\n");
  1269. List<int> indent_stack;
  1270. for(int i=0;i<lines.size();i++) {
  1271. String l = lines[i];
  1272. int tc=0;
  1273. for(int j=0;j<l.length();j++) {
  1274. if (l[j]==' ' || l[j]=='\t') {
  1275. tc++;
  1276. } else {
  1277. break;
  1278. }
  1279. }
  1280. String st = l.substr(tc,l.length()).strip_edges();
  1281. if (st=="" || st.begins_with("#"))
  1282. continue; //ignore!
  1283. int ilevel=0;
  1284. if (indent_stack.size()) {
  1285. ilevel=indent_stack.back()->get();
  1286. }
  1287. if (tc>ilevel) {
  1288. indent_stack.push_back(tc);
  1289. } else if (tc<ilevel) {
  1290. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  1291. indent_stack.pop_back();
  1292. }
  1293. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  1294. indent_stack.push_back(tc); //this is not right but gets the job done
  1295. }
  1296. if (i>=p_from_line) {
  1297. l="";
  1298. for(int j=0;j<indent_stack.size();j++)
  1299. l+="\t";
  1300. l+=st;
  1301. } else if (i>p_to_line) {
  1302. break;
  1303. }
  1304. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  1305. lines[i]=l;
  1306. }
  1307. p_code="";
  1308. for(int i=0;i<lines.size();i++) {
  1309. if (i>0)
  1310. p_code+="\n";
  1311. p_code+=lines[i];
  1312. }
  1313. }