gd_editor.cpp 50 KB

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