gd_editor.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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 (id.operator String()=="new" && base.value.get_type()==Variant::OBJECT) {
  428. Object *obj = base.value;
  429. if (obj && obj->cast_to<GDNativeClass>()) {
  430. GDNativeClass *gdnc = obj->cast_to<GDNativeClass>();
  431. r_type.type=Variant::OBJECT;
  432. r_type.value=Variant();
  433. r_type.obj_type=gdnc->get_name();
  434. return true;
  435. }
  436. }
  437. if (ObjectTypeDB::has_method(base.obj_type,id)) {
  438. #ifdef TOOLS_ENABLED
  439. MethodBind *mb = ObjectTypeDB::get_method(base.obj_type,id);
  440. PropertyInfo pi = mb->get_argument_info(-1);
  441. //try calling the function if constant and all args are constant, should not crash..
  442. Object *baseptr = base.value;
  443. if (baseptr && mb->is_const() && pi.type==Variant::OBJECT) {
  444. bool all_valid=true;
  445. Vector<Variant> args;
  446. for(int i=2;i<op->arguments.size();i++) {
  447. GDCompletionIdentifier arg;
  448. if (_guess_expression_type(context,op->arguments[i],p_line,arg)) {
  449. if (arg.value.get_type()!=Variant::NIL && arg.value.get_type()!=Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  450. args.push_back(arg.value);
  451. } else {
  452. all_valid=false;
  453. break;
  454. }
  455. } else {
  456. all_valid=false;
  457. }
  458. }
  459. if (all_valid) {
  460. Vector<const Variant*> argptr;
  461. for(int i=0;i<args.size();i++) {
  462. argptr.push_back(&args[i]);
  463. }
  464. Variant::CallError ce;
  465. Variant ret=mb->call(baseptr,argptr.ptr(),argptr.size(),ce);
  466. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  467. if (ret.get_type()!=Variant::OBJECT || ret.operator Object*()!=NULL) {
  468. r_type=_get_type_from_variant(ret);
  469. return true;
  470. }
  471. }
  472. }
  473. }
  474. r_type.type=pi.type;
  475. if (pi.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  476. r_type.obj_type=pi.hint_string;
  477. }
  478. return true;
  479. #else
  480. return false;
  481. #endif
  482. } else {
  483. return false;
  484. }
  485. } else {
  486. //method for some variant..
  487. Variant::CallError ce;
  488. Variant v = Variant::construct(base.type,NULL,0,ce);
  489. List<MethodInfo> mi;
  490. v.get_method_list(&mi);
  491. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  492. if (!E->get().name.begins_with("_") && E->get().name==id.operator String()) {
  493. MethodInfo mi = E->get();
  494. r_type.type=mi.return_val.type;
  495. if (mi.return_val.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  496. r_type.obj_type=mi.return_val.hint_string;
  497. }
  498. return true;
  499. }
  500. }
  501. }
  502. }
  503. } else if (op->op==GDParser::OperatorNode::OP_INDEX || op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  504. GDCompletionIdentifier p1;
  505. GDCompletionIdentifier p2;
  506. if (op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  507. if (op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  508. String id = static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name;
  509. p2.type=Variant::STRING;
  510. p2.value=id;
  511. }
  512. } else {
  513. if (op->arguments[1]) {
  514. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  515. return false;
  516. }
  517. }
  518. }
  519. if (op->arguments[0]->type==GDParser::Node::TYPE_ARRAY) {
  520. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(op->arguments[0]);
  521. if (p2.value.is_num()) {
  522. int index = p2.value;
  523. if (index<0 || index>=an->elements.size())
  524. return false;
  525. return _guess_expression_type(context,an->elements[index],p_line,r_type);
  526. }
  527. } else if (op->arguments[0]->type==GDParser::Node::TYPE_DICTIONARY) {
  528. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode *>(op->arguments[0]);
  529. if (p2.value.get_type()==Variant::NIL)
  530. return false;
  531. for(int i=0;i<dn->elements.size();i++) {
  532. GDCompletionIdentifier k;
  533. if (!_guess_expression_type(context,dn->elements[i].key,p_line,k)) {
  534. return false;
  535. }
  536. if (k.value.get_type()==Variant::NIL)
  537. return false;
  538. if (k.value==p2.value) {
  539. return _guess_expression_type(context,dn->elements[i].value,p_line,r_type);
  540. }
  541. }
  542. } else {
  543. if (op->arguments[0]) {
  544. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  545. return false;
  546. }
  547. }
  548. if (p1.value.get_type()==Variant::OBJECT) {
  549. //??
  550. } else if (p1.value.get_type()!=Variant::NIL) {
  551. bool valid;
  552. Variant ret = p1.value.get(p2.value,&valid);
  553. if (valid) {
  554. r_type=_get_type_from_variant(ret);
  555. return true;
  556. }
  557. } else {
  558. if (p1.type!=Variant::NIL) {
  559. Variant::CallError ce;
  560. Variant base = Variant::construct(p1.type,NULL,0,ce);
  561. bool valid;
  562. Variant ret = base.get(p2.value,&valid);
  563. if (valid) {
  564. r_type=_get_type_from_variant(ret);
  565. return true;
  566. }
  567. }
  568. }
  569. }
  570. } else {
  571. Variant::Operator vop = Variant::OP_MAX;
  572. switch(op->op) {
  573. case GDParser::OperatorNode::OP_ADD: vop=Variant::OP_ADD; break;
  574. case GDParser::OperatorNode::OP_SUB: vop=Variant::OP_SUBSTRACT; break;
  575. case GDParser::OperatorNode::OP_MUL: vop=Variant::OP_MULTIPLY; break;
  576. case GDParser::OperatorNode::OP_DIV: vop=Variant::OP_DIVIDE; break;
  577. case GDParser::OperatorNode::OP_MOD: vop=Variant::OP_MODULE; break;
  578. case GDParser::OperatorNode::OP_SHIFT_LEFT: vop=Variant::OP_SHIFT_LEFT; break;
  579. case GDParser::OperatorNode::OP_SHIFT_RIGHT: vop=Variant::OP_SHIFT_RIGHT; break;
  580. case GDParser::OperatorNode::OP_BIT_AND: vop=Variant::OP_BIT_AND; break;
  581. case GDParser::OperatorNode::OP_BIT_OR: vop=Variant::OP_BIT_OR; break;
  582. case GDParser::OperatorNode::OP_BIT_XOR: vop=Variant::OP_BIT_XOR; break;
  583. default:{}
  584. }
  585. if (vop==Variant::OP_MAX)
  586. return false;
  587. GDCompletionIdentifier p1;
  588. GDCompletionIdentifier p2;
  589. if (op->arguments[0]) {
  590. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  591. return false;
  592. }
  593. }
  594. if (op->arguments.size()>1) {
  595. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  596. return false;
  597. }
  598. }
  599. Variant::CallError ce;
  600. bool v1_use_value = p1.value.get_type()!=Variant::NIL && p1.value.get_type()!=Variant::OBJECT;
  601. Variant v1 = (v1_use_value)?p1.value:Variant::construct(p1.type,NULL,0,ce);
  602. bool v2_use_value = p2.value.get_type()!=Variant::NIL && p2.value.get_type()!=Variant::OBJECT;
  603. Variant v2 = (v2_use_value)?p2.value:Variant::construct(p2.type,NULL,0,ce);
  604. // avoid potential invalid ops
  605. if ((vop==Variant::OP_DIVIDE || vop==Variant::OP_MODULE) && v2.get_type()==Variant::INT) {
  606. v2=1;
  607. v2_use_value=false;
  608. }
  609. if (vop==Variant::OP_DIVIDE && v2.get_type()==Variant::REAL) {
  610. v2=1.0;
  611. v2_use_value=false;
  612. }
  613. Variant r;
  614. bool valid;
  615. Variant::evaluate(vop,v1,v2,r,valid);
  616. if (!valid)
  617. return false;
  618. r_type.type=r.get_type();
  619. if (v1_use_value && v2_use_value)
  620. r_type.value=r;
  621. return true;
  622. }
  623. }
  624. return false;
  625. }
  626. static bool _guess_identifier_type_in_block(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  627. const GDParser::Node *last_assign=NULL;
  628. int last_assign_line=-1;
  629. for (int i=0;i<context.block->statements.size();i++) {
  630. if (context.block->statements[i]->line>p_line)
  631. continue;
  632. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  633. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  634. if (lv->assign && lv->name==p_identifier) {
  635. last_assign=lv->assign;
  636. last_assign_line=context.block->statements[i]->line;
  637. }
  638. }
  639. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
  640. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  641. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  642. if (op->arguments.size() && op->arguments[0]->type==GDParser::Node::TYPE_IDENTIFIER) {
  643. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  644. if (id->name==p_identifier) {
  645. last_assign=op->arguments[1];
  646. last_assign_line=context.block->statements[i]->line;
  647. }
  648. }
  649. }
  650. }
  651. }
  652. //use the last assignment, (then backwards?)
  653. if (last_assign) {
  654. return _guess_expression_type(context,last_assign,last_assign_line,r_type);
  655. }
  656. return false;
  657. }
  658. static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  659. //go to block first
  660. const GDParser::BlockNode *block=context.block;
  661. while(block) {
  662. GDCompletionContext c = context;
  663. c.block=block;
  664. if (_guess_identifier_type_in_block(c,p_line,p_identifier,r_type)) {
  665. return true;
  666. }
  667. block=block->parent_block;
  668. }
  669. //guess from argument if virtual
  670. if (context.function && context.function->name!=StringName()) {
  671. int argindex = -1;
  672. for(int i=0;i<context.function->arguments.size();i++) {
  673. if (context.function->arguments[i]==p_identifier) {
  674. argindex=i;
  675. break;
  676. }
  677. }
  678. if (argindex!=-1) {
  679. GDCompletionIdentifier id =_get_native_class(context);
  680. if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  681. //this kinda sucks but meh
  682. List<MethodInfo> vmethods;
  683. ObjectTypeDB::get_virtual_methods(id.obj_type,&vmethods);
  684. for (List<MethodInfo>::Element *E=vmethods.front();E;E=E->next()) {
  685. if (E->get().name==context.function->name && argindex<E->get().arguments.size()) {
  686. PropertyInfo arg=E->get().arguments[argindex];
  687. int scp = arg.name.find(":");
  688. if (scp!=-1) {
  689. r_type.type=Variant::OBJECT;
  690. r_type.obj_type=arg.name.substr(scp+1,arg.name.length());
  691. return true;
  692. } else {
  693. r_type.type=arg.type;
  694. if (arg.hint==PROPERTY_HINT_RESOURCE_TYPE)
  695. r_type.obj_type=arg.hint_string;
  696. return true;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. //guess type in constant
  704. for(int i=0;i<context._class->constant_expressions.size();i++) {
  705. if (context._class->constant_expressions[i].identifier==p_identifier) {
  706. ERR_FAIL_COND_V( context._class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT, false );
  707. r_type=_get_type_from_variant(static_cast<const GDParser::ConstantNode*>(context._class->constant_expressions[i].expression)->value );
  708. return true;
  709. }
  710. }
  711. if (!(context.function && context.function->_static)) {
  712. for(int i=0;i<context._class->variables.size();i++) {
  713. if (context._class->variables[i].identifier==p_identifier) {
  714. if (context._class->variables[i]._export.type!=Variant::NIL) {
  715. r_type=_get_type_from_pinfo(context._class->variables[i]._export);
  716. return true;
  717. } else if (context._class->variables[i].expression) {
  718. return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  719. }
  720. }
  721. }
  722. }
  723. for(Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  724. if (E->key()==p_identifier) {
  725. r_type=_get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  726. return true;
  727. }
  728. }
  729. return false;
  730. }
  731. static void _find_identifiers_in_block(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  732. if (p_only_functions)
  733. return;
  734. for (int i=0;i<context.block->statements.size();i++) {
  735. if (context.block->statements[i]->line>p_line)
  736. continue;
  737. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  738. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  739. result.insert(lv->name.operator String());
  740. }
  741. }
  742. }
  743. static void _find_identifiers_in_class(GDCompletionContext& context,bool p_static,bool p_only_functions,Set<String>& result) {
  744. if (!p_static && !p_only_functions) {
  745. for(int i=0;i<context._class->variables.size();i++) {
  746. result.insert(context._class->variables[i].identifier);
  747. }
  748. }
  749. if (!p_only_functions) {
  750. for(int i=0;i<context._class->constant_expressions.size();i++) {
  751. result.insert(context._class->constant_expressions[i].identifier);
  752. }
  753. for(int i=0;i<context._class->subclasses.size();i++) {
  754. result.insert(context._class->subclasses[i]->name);
  755. }
  756. }
  757. for(int i=0;i<context._class->static_functions.size();i++) {
  758. if (context._class->static_functions[i]->arguments.size())
  759. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  760. else
  761. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  762. }
  763. if (!p_static) {
  764. for(int i=0;i<context._class->functions.size();i++) {
  765. if (context._class->functions[i]->arguments.size())
  766. result.insert(context._class->functions[i]->name.operator String()+"(");
  767. else
  768. result.insert(context._class->functions[i]->name.operator String()+"()");
  769. }
  770. }
  771. //globals
  772. Ref<Reference> base = _get_parent_class(context);
  773. while(true) {
  774. Ref<GDScript> script = base;
  775. Ref<GDNativeClass> nc = base;
  776. if (script.is_valid()) {
  777. if (!p_static && !p_only_functions) {
  778. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  779. result.insert(E->get().operator String());
  780. }
  781. }
  782. if (!p_only_functions) {
  783. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  784. result.insert(E->key().operator String());
  785. }
  786. }
  787. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  788. if (!p_static || E->get().is_static()) {
  789. if (E->get().get_argument_count())
  790. result.insert(E->key().operator String()+"(");
  791. else
  792. result.insert(E->key().operator String()+"()");
  793. }
  794. }
  795. if (!p_only_functions) {
  796. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  797. result.insert(E->key().operator String());
  798. }
  799. }
  800. base=script->get_base();
  801. if (base.is_null())
  802. base=script->get_native();
  803. } else if (nc.is_valid()) {
  804. if (!p_only_functions) {
  805. StringName type = nc->get_name();
  806. List<String> constants;
  807. ObjectTypeDB::get_integer_constant_list(type,&constants);
  808. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  809. result.insert(E->get());
  810. }
  811. List<MethodInfo> methods;
  812. ObjectTypeDB::get_method_list(type,&methods);
  813. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  814. if (E->get().name.begins_with("_"))
  815. continue;
  816. if (E->get().arguments.size())
  817. result.insert(E->get().name+"(");
  818. else
  819. result.insert(E->get().name+"()");
  820. }
  821. }
  822. break;
  823. } else
  824. break;
  825. }
  826. }
  827. static void _find_identifiers(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  828. const GDParser::BlockNode *block=context.block;
  829. while(block) {
  830. GDCompletionContext c = context;
  831. c.block=block;
  832. _find_identifiers_in_block(c,p_line,p_only_functions,result);
  833. block=block->parent_block;
  834. }
  835. const GDParser::ClassNode *clss=context._class;
  836. bool _static=context.function && context.function->_static;
  837. while(clss) {
  838. GDCompletionContext c = context;
  839. c._class=clss;
  840. c.block=NULL;
  841. c.function=NULL;
  842. _find_identifiers_in_class(c,_static,p_only_functions,result);
  843. clss=clss->owner;
  844. }
  845. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  846. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  847. }
  848. static const char*_type_names[Variant::VARIANT_MAX]={
  849. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Trasnform",
  850. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  851. "Vector2Array","Vector3Array","ColorArray"};
  852. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  853. result.insert(_type_names[i]);
  854. }
  855. for(const Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  856. result.insert(E->key().operator String());
  857. }
  858. }
  859. static String _get_visual_datatype(const PropertyInfo& p_info,bool p_isarg=true) {
  860. String n = p_info.name;
  861. int idx = n.find(":");
  862. if (idx!=-1) {
  863. return n.substr(idx+1,n.length());
  864. }
  865. if (p_info.type==Variant::OBJECT && p_info.hint==PROPERTY_HINT_RESOURCE_TYPE)
  866. return p_info.hint_string;
  867. if (p_info.type==Variant::NIL) {
  868. if (p_isarg)
  869. return "var";
  870. else
  871. return "void";
  872. }
  873. return Variant::get_type_name(p_info.type);
  874. }
  875. static void _make_function_hint(const GDParser::FunctionNode* p_func,int p_argidx,String& arghint) {
  876. arghint="func "+p_func->name+"(";
  877. for (int i=0;i<p_func->arguments.size();i++) {
  878. if (i>0)
  879. arghint+=", ";
  880. else
  881. arghint+=" ";
  882. if (i==p_argidx) {
  883. arghint+=String::chr(0xFFFF);
  884. }
  885. arghint+=p_func->arguments[i].operator String();
  886. int deffrom = p_func->arguments.size()-p_func->default_values.size();
  887. if (i>=deffrom) {
  888. int defidx = deffrom-i;
  889. if (defidx>=0 && defidx<p_func->default_values.size()) {
  890. if (p_func->default_values[defidx]->type==GDParser::Node::TYPE_OPERATOR) {
  891. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  892. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  893. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  894. arghint+="="+cn->value.get_construct_string();
  895. }
  896. } else {
  897. }
  898. }
  899. }
  900. if (i==p_argidx) {
  901. arghint+=String::chr(0xFFFF);
  902. }
  903. }
  904. if (p_func->arguments.size()>0)
  905. arghint+=" ";
  906. arghint+=")";
  907. }
  908. 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) {
  909. if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  910. MethodBind *m = ObjectTypeDB::get_method(id.obj_type,p_method);
  911. if (!m)
  912. return;
  913. if (p_method.operator String()=="connect") {
  914. if (p_argidx==0) {
  915. List<MethodInfo> sigs;
  916. ObjectTypeDB::get_signal_list(id.obj_type,&sigs);
  917. for (List<MethodInfo>::Element *E=sigs.front();E;E=E->next()) {
  918. result.insert("\""+E->get().name+"\"");
  919. }
  920. }
  921. /*if (p_argidx==2) {
  922. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  923. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  924. if (op->arguments.size()>)
  925. }*/
  926. } else {
  927. Object *obj=id.value;
  928. if (obj) {
  929. List<String> options;
  930. obj->get_argument_options(p_method,p_argidx,&options);
  931. for(List<String>::Element *E=options.front();E;E=E->next()) {
  932. result.insert(E->get());
  933. }
  934. }
  935. }
  936. arghint = _get_visual_datatype(m->get_argument_info(-1),false)+" "+p_method.operator String()+String("(");
  937. for(int i=0;i<m->get_argument_count();i++) {
  938. if (i>0)
  939. arghint+=", ";
  940. else
  941. arghint+=" ";
  942. if (i==p_argidx) {
  943. arghint+=String::chr(0xFFFF);
  944. }
  945. String n = m->get_argument_info(i).name;
  946. int dp = n.find(":");
  947. if (dp!=-1)
  948. n=n.substr(0,dp);
  949. arghint+=_get_visual_datatype(m->get_argument_info(i))+" "+n;
  950. int deffrom = m->get_argument_count()-m->get_default_argument_count();
  951. if (i>=deffrom) {
  952. int defidx = i-deffrom;
  953. if (defidx>=0 && defidx<m->get_default_argument_count()) {
  954. Variant v= m->get_default_argument(i);
  955. arghint+="="+v.get_construct_string();
  956. }
  957. }
  958. if (i==p_argidx) {
  959. arghint+=String::chr(0xFFFF);
  960. }
  961. }
  962. if (m->get_argument_count()>0)
  963. arghint+=" ";
  964. arghint+=")";
  965. }
  966. }
  967. static void _find_call_arguments(GDCompletionContext& context,const GDParser::Node* p_node, int p_line,int p_argidx, Set<String>& result, String& arghint) {
  968. if (!p_node || p_node->type!=GDParser::Node::TYPE_OPERATOR) {
  969. return;
  970. }
  971. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  972. if (op->op!=GDParser::OperatorNode::OP_CALL) {
  973. return;
  974. }
  975. if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  976. //complete built-in function
  977. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode*>(op->arguments[0]);
  978. MethodInfo mi = GDFunctions::get_info(fn->function);
  979. arghint = _get_visual_datatype(mi.return_val,false)+" "+GDFunctions::get_func_name(fn->function)+String("(");
  980. for(int i=0;i<mi.arguments.size();i++) {
  981. if (i>0)
  982. arghint+=", ";
  983. else
  984. arghint+=" ";
  985. if (i==p_argidx) {
  986. arghint+=String::chr(0xFFFF);
  987. }
  988. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  989. if (i==p_argidx) {
  990. arghint+=String::chr(0xFFFF);
  991. }
  992. }
  993. if (mi.arguments.size()>0)
  994. arghint+=" ";
  995. arghint+=")";
  996. } else if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  997. //complete built-in function
  998. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode*>(op->arguments[0]);
  999. List<MethodInfo> mil;
  1000. Variant::get_constructor_list(tn->vtype,&mil);
  1001. for(List<MethodInfo>::Element *E=mil.front();E;E=E->next()) {
  1002. MethodInfo mi = E->get();
  1003. if (mi.arguments.size()==0)
  1004. continue;
  1005. if (E->prev())
  1006. arghint+="\n";
  1007. arghint += Variant::get_type_name(tn->vtype)+" "+Variant::get_type_name(tn->vtype)+String("(");
  1008. for(int i=0;i<mi.arguments.size();i++) {
  1009. if (i>0)
  1010. arghint+=", ";
  1011. else
  1012. arghint+=" ";
  1013. if (i==p_argidx) {
  1014. arghint+=String::chr(0xFFFF);
  1015. }
  1016. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  1017. if (i==p_argidx) {
  1018. arghint+=String::chr(0xFFFF);
  1019. }
  1020. }
  1021. if (mi.arguments.size()>0)
  1022. arghint+=" ";
  1023. arghint+=")";
  1024. }
  1025. } else if (op->arguments.size()>=2 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  1026. //make sure identifier exists...
  1027. const GDParser::IdentifierNode *id=static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1028. if (op->arguments[0]->type==GDParser::Node::TYPE_SELF) {
  1029. //self, look up
  1030. for(int i=0;i<context._class->static_functions.size();i++) {
  1031. if (context._class->static_functions[i]->name==id->name) {
  1032. _make_function_hint(context._class->static_functions[i],p_argidx,arghint);
  1033. return;
  1034. }
  1035. }
  1036. if (context.function && !context.function->_static) {
  1037. for(int i=0;i<context._class->functions.size();i++) {
  1038. if (context._class->functions[i]->name==id->name) {
  1039. _make_function_hint(context._class->functions[i],p_argidx,arghint);
  1040. return;
  1041. }
  1042. }
  1043. }
  1044. Ref<Reference> base = _get_parent_class(context);
  1045. while(true) {
  1046. Ref<GDScript> script = base;
  1047. Ref<GDNativeClass> nc = base;
  1048. if (script.is_valid()) {
  1049. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1050. if (E->key()==id->name) {
  1051. if (context.function && context.function->_static && !E->get().is_static())
  1052. continue;
  1053. arghint = "func "+id->name.operator String()+String("(");
  1054. for(int i=0;i<E->get().get_argument_count();i++) {
  1055. if (i>0)
  1056. arghint+=", ";
  1057. else
  1058. arghint+=" ";
  1059. if (i==p_argidx) {
  1060. arghint+=String::chr(0xFFFF);
  1061. }
  1062. arghint+=E->get().get_argument_name(i);
  1063. int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
  1064. if (i>=deffrom) {
  1065. int defidx = deffrom-i;
  1066. if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
  1067. arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
  1068. }
  1069. }
  1070. if (i==p_argidx) {
  1071. arghint+=String::chr(0xFFFF);
  1072. }
  1073. }
  1074. if (E->get().get_argument_count()>0)
  1075. arghint+=" ";
  1076. arghint+=")";
  1077. return;
  1078. }
  1079. }
  1080. base=script->get_base();
  1081. if (base.is_null())
  1082. base=script->get_native();
  1083. } else if (nc.is_valid()) {
  1084. if (context.function && !context.function->_static) {
  1085. GDCompletionIdentifier ci;
  1086. ci.type=Variant::OBJECT;
  1087. ci.obj_type=nc->get_name();
  1088. if (!context._class->owner)
  1089. ci.value=context.base;
  1090. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1091. //guess type..
  1092. /*
  1093. List<MethodInfo> methods;
  1094. ObjectTypeDB::get_method_list(type,&methods);
  1095. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1096. //if (E->get().arguments.size())
  1097. // result.insert(E->get().name+"(");
  1098. //else
  1099. // result.insert(E->get().name+"()");
  1100. }*/
  1101. }
  1102. break;
  1103. } else
  1104. break;
  1105. }
  1106. } else {
  1107. GDCompletionIdentifier ci;
  1108. if (_guess_expression_type(context,op->arguments[0],p_line,ci)) {
  1109. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1110. return;
  1111. }
  1112. }
  1113. }
  1114. #if 0
  1115. bool _static=context.function->_static;
  1116. for(int i=0;i<context._class->static_functions.size();i++) {
  1117. if (context._class->static_functions[i]->arguments.size())
  1118. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1119. else
  1120. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1121. }
  1122. if (!p_static) {
  1123. for(int i=0;i<context._class->functions.size();i++) {
  1124. if (context._class->functions[i]->arguments.size())
  1125. result.insert(context._class->functions[i]->name.operator String()+"(");
  1126. else
  1127. result.insert(context._class->functions[i]->name.operator String()+"()");
  1128. }
  1129. }
  1130. Ref<Reference> base = _get_parent_class(context);
  1131. while(true) {
  1132. Ref<GDScript> script = base;
  1133. Ref<GDNativeClass> nc = base;
  1134. if (script.is_valid()) {
  1135. if (!p_static && !p_only_functions) {
  1136. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1137. result.insert(E->get().operator String());
  1138. }
  1139. }
  1140. if (!p_only_functions) {
  1141. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1142. result.insert(E->key().operator String());
  1143. }
  1144. }
  1145. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1146. if (!p_static || E->get().is_static()) {
  1147. if (E->get().get_argument_count())
  1148. result.insert(E->key().operator String()+"(");
  1149. else
  1150. result.insert(E->key().operator String()+"()");
  1151. }
  1152. }
  1153. if (!p_only_functions) {
  1154. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1155. result.insert(E->key().operator String());
  1156. }
  1157. }
  1158. base=script->get_base();
  1159. if (base.is_null())
  1160. base=script->get_native();
  1161. } else if (nc.is_valid()) {
  1162. if (!p_only_functions) {
  1163. StringName type = nc->get_name();
  1164. List<String> constants;
  1165. ObjectTypeDB::get_integer_constant_list(type,&constants);
  1166. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1167. result.insert(E->get());
  1168. }
  1169. List<MethodInfo> methods;
  1170. ObjectTypeDB::get_method_list(type,&methods);
  1171. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1172. if (E->get().arguments.size())
  1173. result.insert(E->get().name+"(");
  1174. else
  1175. result.insert(E->get().name+"()");
  1176. }
  1177. }
  1178. break;
  1179. } else
  1180. break;
  1181. }
  1182. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1183. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1184. }
  1185. #endif
  1186. }
  1187. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1188. /* bugs:
  1189. a[0].<complete> does not work
  1190. functions should end in (
  1191. when completing virtuals, ask for full back
  1192. */
  1193. //print_line( p_code.replace(String::chr(0xFFFF),"<cursor>"));
  1194. GDParser p;
  1195. Error err = p.parse(p_code,p_base_path);
  1196. bool isfunction=false;
  1197. Set<String> options;
  1198. GDCompletionContext context;
  1199. context._class=p.get_completion_class();
  1200. context.block=p.get_completion_block();
  1201. context.function=p.get_completion_function();
  1202. context.base=p_owner;
  1203. context.base_path=p_base_path;
  1204. switch(p.get_completion_type()) {
  1205. case GDParser::COMPLETION_NONE: {
  1206. print_line("No completion");
  1207. } break;
  1208. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1209. print_line("Built in type constant");
  1210. List<StringName> constants;
  1211. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(),&constants);
  1212. for(List<StringName>::Element *E=constants.front();E;E=E->next()) {
  1213. options.insert(E->get().operator String());
  1214. }
  1215. } break;
  1216. case GDParser::COMPLETION_FUNCTION:
  1217. isfunction=true;
  1218. case GDParser::COMPLETION_IDENTIFIER: {
  1219. _find_identifiers(context,p.get_completion_line(),isfunction,options);
  1220. } break;
  1221. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1222. print_line("parent function");
  1223. } break;
  1224. case GDParser::COMPLETION_METHOD:
  1225. isfunction=true;
  1226. case GDParser::COMPLETION_INDEX: {
  1227. const GDParser::Node *node = p.get_completion_node();
  1228. if (node->type!=GDParser::Node::TYPE_OPERATOR)
  1229. break;
  1230. GDCompletionIdentifier t;
  1231. if (_guess_expression_type(context,static_cast<const GDParser::OperatorNode *>(node)->arguments[0],p.get_completion_line(),t)) {
  1232. if (t.type==Variant::OBJECT && t.obj_type!=StringName()) {
  1233. if (t.value.get_type()) {
  1234. Object *obj=t.value;
  1235. if (obj) {
  1236. GDScript *scr = obj->cast_to<GDScript>();
  1237. while (scr) {
  1238. if (!isfunction) {
  1239. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1240. options.insert(E->key());
  1241. }
  1242. }
  1243. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1244. options.insert(E->key());
  1245. }
  1246. if (scr->get_base().is_valid())
  1247. scr=scr->get_base().ptr();
  1248. else
  1249. scr=NULL;
  1250. }
  1251. }
  1252. }
  1253. if (!isfunction) {
  1254. ObjectTypeDB::get_integer_constant_list(t.obj_type,r_options);
  1255. }
  1256. List<MethodInfo> mi;
  1257. ObjectTypeDB::get_method_list(t.obj_type,&mi);
  1258. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1259. if (E->get().name.begins_with("_"))
  1260. continue;
  1261. if (E->get().arguments.size())
  1262. options.insert(E->get().name+"(");
  1263. else
  1264. options.insert(E->get().name+"()");
  1265. }
  1266. } else {
  1267. if (t.value.get_type()==Variant::NIL) {
  1268. Variant::CallError ce;
  1269. t.value=Variant::construct(t.type,NULL,0,ce);
  1270. }
  1271. if (!isfunction) {
  1272. List<PropertyInfo> pl;
  1273. t.value.get_property_list(&pl);
  1274. for (List<PropertyInfo>::Element *E=pl.front();E;E=E->next()) {
  1275. if (E->get().name.find("/")==-1)
  1276. options.insert(E->get().name);
  1277. }
  1278. }
  1279. List<MethodInfo> mi;
  1280. t.value.get_method_list(&mi);
  1281. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1282. if (E->get().arguments.size())
  1283. options.insert(E->get().name+"(");
  1284. else
  1285. options.insert(E->get().name+"()");
  1286. }
  1287. }
  1288. }
  1289. } break;
  1290. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1291. _find_call_arguments(context,p.get_completion_node(),p.get_completion_line(),p.get_completion_argument_index(),options,r_call_hint);
  1292. } break;
  1293. }
  1294. for(Set<String>::Element *E=options.front();E;E=E->next()) {
  1295. r_options->push_back(E->get());
  1296. }
  1297. return OK;
  1298. }
  1299. #else
  1300. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1301. return OK;
  1302. }
  1303. #endif
  1304. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  1305. Vector<String> lines = p_code.split("\n");
  1306. List<int> indent_stack;
  1307. for(int i=0;i<lines.size();i++) {
  1308. String l = lines[i];
  1309. int tc=0;
  1310. for(int j=0;j<l.length();j++) {
  1311. if (l[j]==' ' || l[j]=='\t') {
  1312. tc++;
  1313. } else {
  1314. break;
  1315. }
  1316. }
  1317. String st = l.substr(tc,l.length()).strip_edges();
  1318. if (st=="" || st.begins_with("#"))
  1319. continue; //ignore!
  1320. int ilevel=0;
  1321. if (indent_stack.size()) {
  1322. ilevel=indent_stack.back()->get();
  1323. }
  1324. if (tc>ilevel) {
  1325. indent_stack.push_back(tc);
  1326. } else if (tc<ilevel) {
  1327. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  1328. indent_stack.pop_back();
  1329. }
  1330. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  1331. indent_stack.push_back(tc); //this is not right but gets the job done
  1332. }
  1333. if (i>=p_from_line) {
  1334. l="";
  1335. for(int j=0;j<indent_stack.size();j++)
  1336. l+="\t";
  1337. l+=st;
  1338. } else if (i>p_to_line) {
  1339. break;
  1340. }
  1341. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  1342. lines[i]=l;
  1343. }
  1344. p_code="";
  1345. for(int i=0;i<lines.size();i++) {
  1346. if (i>0)
  1347. p_code+="\n";
  1348. p_code+=lines[i];
  1349. }
  1350. }