gd_editor.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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. static void _parse_native_symbols(const StringName& p_native,bool p_static,List<String>* r_options) {
  222. if (!p_static) {
  223. List<MethodInfo> methods;
  224. ObjectTypeDB::get_method_list(p_native,&methods);
  225. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  226. if (!E->get().name.begins_with("_")) {
  227. r_options->push_back(E->get().name);
  228. }
  229. }
  230. }
  231. List<String> constants;
  232. ObjectTypeDB::get_integer_constant_list(p_native,&constants);
  233. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  234. r_options->push_back(E->get());
  235. }
  236. }
  237. static bool _parse_script_symbols(const Ref<GDScript>& p_script,bool p_static,List<String>* r_options,List<String>::Element *p_indices);
  238. static bool _parse_completion_variant(const Variant& p_var,List<String>* r_options,List<String>::Element *p_indices) {
  239. if (p_indices) {
  240. bool ok;
  241. Variant si = p_var.get(p_indices->get(),&ok);
  242. if (!ok)
  243. return false;
  244. return _parse_completion_variant(si,r_options,p_indices->next());
  245. } else {
  246. switch(p_var.get_type()) {
  247. case Variant::DICTIONARY: {
  248. Dictionary d=p_var;
  249. List<Variant> vl;
  250. d.get_key_list(&vl);
  251. for (List<Variant>::Element *E=vl.front();E;E=E->next()) {
  252. if (E->get().get_type()==Variant::STRING)
  253. r_options->push_back(E->get());
  254. }
  255. List<MethodInfo> ml;
  256. p_var.get_method_list(&ml);
  257. for(List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  258. r_options->push_back(E->get().name);
  259. }
  260. } break;
  261. case Variant::OBJECT: {
  262. Object *o=p_var;
  263. if (o) {
  264. print_line("OBJECT: "+o->get_type());
  265. if (p_var.is_ref() && o->cast_to<GDScript>()) {
  266. Ref<GDScript> gds = p_var;
  267. _parse_script_symbols(gds,true,r_options,NULL);
  268. } else if (o->is_type("GDNativeClass")){
  269. GDNativeClass *gnc = o->cast_to<GDNativeClass>();
  270. _parse_native_symbols(gnc->get_name(),false,r_options);
  271. } else {
  272. print_line("REGULAR BLEND");
  273. _parse_native_symbols(o->get_type(),false,r_options);
  274. }
  275. }
  276. } break;
  277. default: {
  278. List<PropertyInfo> pi;
  279. p_var.get_property_list(&pi);
  280. for(List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  281. r_options->push_back(E->get().name);
  282. }
  283. List<StringName> cl;
  284. p_var.get_numeric_constants_for_type(p_var.get_type(),&cl);
  285. for(List<StringName>::Element *E=cl.front();E;E=E->next()) {
  286. r_options->push_back(E->get());
  287. }
  288. List<MethodInfo> ml;
  289. p_var.get_method_list(&ml);
  290. for(List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  291. r_options->push_back(E->get().name);
  292. }
  293. } break;
  294. }
  295. return true;
  296. }
  297. }
  298. struct GDCompletionIdentifier {
  299. StringName obj_type;
  300. Variant::Type type;
  301. };
  302. static GDCompletionIdentifier _guess_identifier_type(const GDParser::ClassNode *p_class,int p_line,const StringName& p_identifier);
  303. static bool _guess_identifier_type_in_expression(const GDParser::ClassNode *p_class,const GDParser::Node *p_node,int p_line,GDCompletionIdentifier &r_type) {
  304. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  305. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  306. r_type.type=cn->value.get_type();
  307. if (r_type.type==Variant::OBJECT) {
  308. Object *obj = cn->value;
  309. if (obj) {
  310. r_type.obj_type=obj->get_type();
  311. }
  312. }
  313. return true;
  314. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  315. r_type.type=Variant::DICTIONARY;
  316. return true;
  317. } else if (p_node->type==GDParser::Node::TYPE_ARRAY) {
  318. r_type.type=Variant::ARRAY;
  319. return true;
  320. } else if (p_node->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  321. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode*>(p_node)->function);
  322. r_type.type=mi.return_val.type;
  323. if (mi.return_val.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  324. r_type.obj_type=mi.return_val.hint_string;
  325. }
  326. return true;
  327. } else if (p_node->type==GDParser::Node::TYPE_IDENTIFIER) {
  328. r_type=_guess_identifier_type(p_class,p_line,static_cast<const GDParser::IdentifierNode *>(p_node)->name);
  329. return true;
  330. } else if (p_node->type==GDParser::Node::TYPE_SELF) {
  331. //eeh...
  332. return false;
  333. } else if (p_node->type==GDParser::Node::TYPE_OPERATOR) {
  334. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  335. if (op->op==GDParser::OperatorNode::OP_CALL) {
  336. if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  337. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  338. r_type.type=tn->vtype;
  339. return true;
  340. } else if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  341. const GDParser::BuiltInFunctionNode *bin = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  342. return _guess_identifier_type_in_expression(p_class,bin,p_line,r_type);
  343. } else if (op->arguments.size()>1 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  344. GDCompletionIdentifier base;
  345. if (!_guess_identifier_type_in_expression(p_class,op->arguments[0],p_line,base))
  346. return false;
  347. StringName id = static_cast<const GDParser::IdentifierNode *>(p_node)->name;
  348. if (base.type==Variant::OBJECT) {
  349. if (ObjectTypeDB::has_method(base.obj_type,id)) {
  350. #ifdef TOOLS_ENABLED
  351. MethodBind *mb = ObjectTypeDB::get_method(base.obj_type,id);
  352. PropertyInfo pi = mb->get_argument_info(-1);
  353. r_type.type=pi.type;
  354. if (pi.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  355. r_type.obj_type=pi.hint_string;
  356. }
  357. #else
  358. return false;
  359. #endif
  360. } else {
  361. return false;
  362. }
  363. } else {
  364. //method for some variant..
  365. Variant::CallError ce;
  366. Variant v = Variant::construct(base.type,NULL,0,ce);
  367. List<MethodInfo> mi;
  368. v.get_method_list(&mi);
  369. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  370. if (E->get().name==id.operator String()) {
  371. MethodInfo mi = E->get();
  372. r_type.type=mi.return_val.type;
  373. if (mi.return_val.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  374. r_type.obj_type=mi.return_val.hint_string;
  375. }
  376. return true;
  377. }
  378. }
  379. }
  380. }
  381. } else {
  382. Variant::Operator vop = Variant::OP_MAX;
  383. switch(op->op) {
  384. case GDParser::OperatorNode::OP_ASSIGN_ADD: vop=Variant::OP_ADD; break;
  385. case GDParser::OperatorNode::OP_ASSIGN_SUB: vop=Variant::OP_SUBSTRACT; break;
  386. case GDParser::OperatorNode::OP_ASSIGN_MUL: vop=Variant::OP_MULTIPLY; break;
  387. case GDParser::OperatorNode::OP_ASSIGN_DIV: vop=Variant::OP_DIVIDE; break;
  388. case GDParser::OperatorNode::OP_ASSIGN_MOD: vop=Variant::OP_MODULE; break;
  389. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: vop=Variant::OP_SHIFT_LEFT; break;
  390. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: vop=Variant::OP_SHIFT_RIGHT; break;
  391. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND: vop=Variant::OP_BIT_AND; break;
  392. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR: vop=Variant::OP_BIT_OR; break;
  393. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR: vop=Variant::OP_BIT_XOR; break;
  394. default:{}
  395. }
  396. if (vop==Variant::OP_MAX)
  397. return false;
  398. GDCompletionIdentifier p1;
  399. GDCompletionIdentifier p2;
  400. if (op->arguments[0]) {
  401. if (!_guess_identifier_type_in_expression(p_class,op->arguments[0],p_line,p1))
  402. return false;
  403. }
  404. if (op->arguments.size()>1) {
  405. if (!_guess_identifier_type_in_expression(p_class,op->arguments[1],p_line,p2))
  406. return false;
  407. }
  408. Variant::CallError ce;
  409. Variant v1 = Variant::construct(p1.type,NULL,0,ce);
  410. Variant v2 = Variant::construct(p2.type,NULL,0,ce);
  411. // avoid potential invalid ops
  412. if ((vop==Variant::OP_DIVIDE || vop==Variant::OP_MODULE) && v2.get_type()==Variant::INT) {
  413. v2=1;
  414. }
  415. if (vop==Variant::OP_DIVIDE && v2.get_type()==Variant::REAL) {
  416. v2=1.0;
  417. }
  418. Variant r;
  419. bool valid;
  420. Variant::evaluate(vop,v1,v2,r,valid);
  421. if (!valid)
  422. return false;
  423. r_type.type=r.get_type();
  424. return true;
  425. }
  426. }
  427. return false;
  428. }
  429. static bool _guess_identifier_type_in_block(const GDParser::ClassNode *p_class,const GDParser::BlockNode *p_block,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  430. for(int i=0;i<p_block->sub_blocks.size();i++) {
  431. //parse inner first
  432. if (p_line>=p_block->sub_blocks[i]->line && (p_line<=p_block->sub_blocks[i]->end_line || p_block->sub_blocks[i]->end_line==-1)) {
  433. if (_guess_identifier_type_in_block(p_class,p_block->sub_blocks[i],p_line,p_identifier,r_type))
  434. return true;
  435. }
  436. }
  437. const GDParser::Node *last_assign=NULL;
  438. int last_assign_line=-1;
  439. for (int i=0;i<p_block->statements.size();i++) {
  440. if (p_block->statements[i]->line>p_line)
  441. break;
  442. if (p_block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  443. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(p_block->statements[i]);
  444. if (lv->assign && lv->name==p_identifier) {
  445. last_assign=lv->assign;
  446. last_assign_line=p_block->statements[i]->line;
  447. }
  448. }
  449. if (p_block->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
  450. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_block->statements[i]);
  451. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  452. if (op->arguments.size() && op->arguments[0]->type==GDParser::Node::TYPE_IDENTIFIER) {
  453. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  454. if (id->name==p_identifier) {
  455. last_assign=op->arguments[1];
  456. last_assign_line=p_block->statements[i]->line;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. //use the last assignment, (then backwards?)
  463. if (last_assign) {
  464. return _guess_identifier_type_in_expression(p_class,last_assign,last_assign_line-1,r_type);
  465. }
  466. return false;
  467. }
  468. static GDCompletionIdentifier _guess_identifier_type(const GDParser::ClassNode *p_class,int p_line,const StringName& p_identifier) {
  469. return GDCompletionIdentifier();
  470. }
  471. static void _parse_expression_node(const GDParser::ClassNode *p_class,const GDParser::Node *p_node,int p_line,List<String>* r_options,List<String>::Element *p_indices) {
  472. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  473. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  474. _parse_completion_variant(cn->value,r_options,p_indices?p_indices->next():NULL);
  475. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  476. const GDParser::DictionaryNode *dn=static_cast<const GDParser::DictionaryNode*>(p_node);
  477. for (int i=0;i<dn->elements.size();i++) {
  478. if (dn->elements[i].key->type==GDParser::Node::TYPE_CONSTANT) {
  479. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(dn->elements[i].key);
  480. if (cn->value.get_type()==Variant::STRING) {
  481. String str=cn->value;
  482. if (p_indices) {
  483. if (str==p_indices->get()) {
  484. _parse_expression_node(p_class,dn->elements[i].value,p_line,r_options,p_indices->next());
  485. return;
  486. }
  487. } else {
  488. r_options->push_back(str);
  489. }
  490. }
  491. }
  492. }
  493. } else if (p_node->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  494. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode*>(p_node)->function);
  495. Variant::CallError ce;
  496. _parse_completion_variant(Variant::construct(mi.return_val.type,NULL,0,ce),r_options,p_indices?p_indices->next():NULL);
  497. } else if (p_node->type==GDParser::Node::TYPE_IDENTIFIER) {
  498. //GDCompletionIdentifier idt = _guess_identifier_type(p_class,p_line-1,static_cast<const GDParser::IdentifierNode *>(p_node)->name);
  499. //Variant::CallError ce;
  500. //_parse_completion_variant(Variant::construct(mi.return_val.type,NULL,0,ce),r_options,p_indices?p_indices->next():NULL);
  501. }
  502. }
  503. static bool _parse_completion_block(const GDParser::ClassNode *p_class,const GDParser::BlockNode *p_block,int p_line,List<String>* r_options,List<String>::Element *p_indices) {
  504. print_line("COMPLETION BLOCK "+itos(p_block->line)+" -> "+itos(p_block->end_line));
  505. for(int i=0;i<p_block->sub_blocks.size();i++) {
  506. //parse inner first
  507. if (p_line>=p_block->sub_blocks[i]->line && (p_line<=p_block->sub_blocks[i]->end_line || p_block->sub_blocks[i]->end_line==-1)) {
  508. if (_parse_completion_block(p_class,p_block->sub_blocks[i],p_line,r_options,p_indices))
  509. return true;
  510. }
  511. }
  512. if (p_indices) {
  513. //parse indices in expressions :|
  514. const GDParser::Node *last_assign=NULL;
  515. int last_assign_line=-1;
  516. for (int i=0;i<p_block->statements.size();i++) {
  517. if (p_block->statements[i]->line>p_line)
  518. break;
  519. if (p_block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  520. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(p_block->statements[i]);
  521. if (lv->assign && String(lv->name)==p_indices->get()) {
  522. last_assign=lv->assign;
  523. last_assign_line=p_block->statements[i]->line;
  524. }
  525. }
  526. }
  527. //use the last assignment, (then backwards?)
  528. if (last_assign) {
  529. _parse_expression_node(p_class,last_assign,last_assign_line,r_options,p_indices->next());
  530. return true;
  531. }
  532. } else {
  533. //no indices, just add all variables and continue
  534. for(int i=0;i<p_block->variables.size();i++) {
  535. //parse variables second
  536. if (p_line>=p_block->variable_lines[i]) {
  537. r_options->push_back(p_block->variables[i]);
  538. } else break;
  539. }
  540. }
  541. return false;
  542. }
  543. static bool _parse_script_symbols(const Ref<GDScript>& p_script,bool p_static,List<String>* r_options,List<String>::Element *p_indices) {
  544. //for (Map<StringName,Ref<GDScript> >::Element ?
  545. if (!p_static && !p_indices) {
  546. for(const Set<StringName>::Element *E=p_script->get_members().front();E;E=E->next()) {
  547. r_options->push_back(E->get());
  548. }
  549. }
  550. for (const Map<StringName,Variant >::Element *E=p_script->get_constants().front();E;E=E->next()) {
  551. if( p_indices) {
  552. if (p_indices->get()==String(E->get())) {
  553. _parse_completion_variant(E->get(),r_options,p_indices->next());
  554. return true;
  555. }
  556. } else {
  557. r_options->push_back(E->key());
  558. }
  559. }
  560. if (!p_indices){
  561. for (const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().front();E;E=E->next()) {
  562. if (E->get().is_static() || !p_static)
  563. r_options->push_back(E->key());
  564. }
  565. }
  566. if (p_script->get_base().is_valid()){
  567. if (_parse_script_symbols(p_script->get_base(),p_static,r_options,p_indices))
  568. return true;
  569. } else if (p_script->get_native().is_valid() && !p_indices) {
  570. _parse_native_symbols(p_script->get_native()->get_name(),p_static,r_options);
  571. }
  572. return false;
  573. }
  574. static bool _parse_completion_class(const String& p_base_path,const GDParser::ClassNode *p_class,int p_line,List<String>* r_options,List<String>::Element *p_indices) {
  575. //checks known classes or built-in types for completion
  576. if (p_indices && !p_indices->next()) {
  577. //built-in types do not have sub-classes, try these first if no sub-indices exist.
  578. static const char*_type_names[Variant::VARIANT_MAX]={
  579. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Trasnform",
  580. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  581. "Vector2Array","Vector3Array","ColorArray"};
  582. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  583. if (p_indices->get()==_type_names[i]) {
  584. List<StringName> ic;
  585. Variant::get_numeric_constants_for_type(Variant::Type(i),&ic);
  586. for(List<StringName>::Element *E=ic.front();E;E=E->next()) {
  587. r_options->push_back(E->get());
  588. }
  589. return true;
  590. }
  591. }
  592. }
  593. // check the sub-classes of current class
  594. for(int i=0;i<p_class->subclasses.size();i++) {
  595. if (p_line>=p_class->subclasses[i]->line && (p_line<=p_class->subclasses[i]->end_line || p_class->subclasses[i]->end_line==-1)) {
  596. // if OK in sub-classes, try completing the sub-class
  597. if (_parse_completion_class(p_base_path,p_class->subclasses[i],p_line,r_options,p_indices))
  598. return true;
  599. }
  600. }
  601. bool in_static_func=false;
  602. for(int i=0;i<p_class->functions.size();i++) {
  603. const GDParser::FunctionNode *fu = p_class->functions[i];
  604. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  605. //if in function, first block stuff from outer to inner
  606. if (_parse_completion_block(p_class,fu->body,p_line,r_options,p_indices))
  607. return true;
  608. //then function arguments
  609. if (!p_indices) {
  610. for(int j=0;j<fu->arguments.size();j++) {
  611. r_options->push_back(fu->arguments[j]);
  612. }
  613. }
  614. }
  615. }
  616. for(int i=0;i<p_class->static_functions.size();i++) {
  617. const GDParser::FunctionNode *fu = p_class->static_functions[i];
  618. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  619. //if in function, first block stuff from outer to inne
  620. if (_parse_completion_block(p_class,fu->body,p_line,r_options,p_indices))
  621. return true;
  622. //then function arguments
  623. if (!p_indices) {
  624. for(int j=0;j<fu->arguments.size();j++) {
  625. r_options->push_back(fu->arguments[j]);
  626. }
  627. }
  628. in_static_func=true;
  629. }
  630. }
  631. //add all local names
  632. if (!p_indices) {
  633. if (!in_static_func) {
  634. for(int i=0;i<p_class->variables.size();i++) {
  635. r_options->push_back(p_class->variables[i].identifier);
  636. }
  637. }
  638. for(int i=0;i<p_class->constant_expressions.size();i++) {
  639. r_options->push_back(p_class->constant_expressions[i].identifier);
  640. }
  641. if (!in_static_func) {
  642. for(int i=0;i<p_class->functions.size();i++) {
  643. r_options->push_back(p_class->functions[i]->name);
  644. }
  645. }
  646. for(int i=0;i<p_class->static_functions.size();i++) {
  647. r_options->push_back(p_class->static_functions[i]->name);
  648. }
  649. }
  650. if (p_class->extends_used) {
  651. //do inheritance
  652. String path = p_class->extends_file;
  653. Ref<GDScript> script;
  654. Ref<GDNativeClass> native;
  655. if (path!="") {
  656. //path (and optionally subclasses)
  657. script = ResourceLoader::load(path);
  658. if (script.is_null()) {
  659. return false;
  660. }
  661. if (p_class->extends_class.size()) {
  662. for(int i=0;i<p_class->extends_class.size();i++) {
  663. String sub = p_class->extends_class[i];
  664. if (script->get_subclasses().has(sub)) {
  665. script=script->get_subclasses()[sub];
  666. } else {
  667. return false;
  668. }
  669. }
  670. }
  671. } else {
  672. ERR_FAIL_COND_V(p_class->extends_class.size()==0,false);
  673. //look around for the subclasses
  674. String base=p_class->extends_class[0];
  675. Ref<GDScript> base_class;
  676. #if 0
  677. while(p) {
  678. if (p->subclasses.has(base)) {
  679. base_class=p->subclasses[base];
  680. break;
  681. }
  682. p=p->_owner;
  683. }
  684. if (base_class.is_valid()) {
  685. for(int i=1;i<p_class->extends_class.size();i++) {
  686. String subclass=p_class->extends_class[i];
  687. if (base_class->subclasses.has(subclass)) {
  688. base_class=base_class->subclasses[subclass];
  689. } else {
  690. _set_error("Could not find subclass: "+subclass,p_class);
  691. return ERR_FILE_NOT_FOUND;
  692. }
  693. }
  694. } else {
  695. #endif
  696. if (p_class->extends_class.size()>1) {
  697. return false;
  698. }
  699. //if not found, try engine classes
  700. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  701. return false;
  702. }
  703. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  704. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  705. if (!native.is_valid()) {
  706. return false;
  707. }
  708. #if 0
  709. }
  710. #endif
  711. }
  712. if (script.is_valid()) {
  713. if (_parse_script_symbols(script,in_static_func,r_options,p_indices))
  714. return true;
  715. } else if (native.is_valid() && !p_indices) {
  716. _parse_native_symbols(native->get_name(),in_static_func,r_options);
  717. }
  718. }
  719. return false;
  720. }
  721. Error GDScriptLanguage::complete_keyword(const String& p_code, int p_line, const String& p_base_path, const String& p_base, List<String>* r_options) {
  722. GDParser p;
  723. Error err = p.parse(p_code,p_base_path);
  724. // don't care much about error I guess
  725. const GDParser::Node* root = p.get_parse_tree();
  726. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  727. print_line("BASE: "+p_base);
  728. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  729. List<String> indices;
  730. Vector<String> spl = p_base.split(".");
  731. for(int i=0;i<spl.size()-1;i++) {
  732. print_line("INDEX "+itos(i)+": "+spl[i]);
  733. indices.push_back(spl[i]);
  734. }
  735. //parse completion inside of the class first
  736. if (_parse_completion_class(p_base,cl,p_line,r_options,indices.front()))
  737. return OK;
  738. //if parsing completion inside of the class fails (none found), try using globals for completion
  739. for(Map<StringName,int>::Element *E=globals.front();E;E=E->next()) {
  740. if (!indices.empty()) {
  741. if (String(E->key())==indices.front()->get()) {
  742. _parse_completion_variant(global_array[E->get()],r_options,indices.front()->next());
  743. return OK;
  744. }
  745. } else {
  746. r_options->push_back(E->key());
  747. }
  748. }
  749. return OK;
  750. }
  751. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  752. Vector<String> lines = p_code.split("\n");
  753. List<int> indent_stack;
  754. for(int i=0;i<lines.size();i++) {
  755. String l = lines[i];
  756. int tc=0;
  757. for(int j=0;j<l.length();j++) {
  758. if (l[j]==' ' || l[j]=='\t') {
  759. tc++;
  760. } else {
  761. break;
  762. }
  763. }
  764. String st = l.substr(tc,l.length()).strip_edges();
  765. if (st=="" || st.begins_with("#"))
  766. continue; //ignore!
  767. int ilevel=0;
  768. if (indent_stack.size()) {
  769. ilevel=indent_stack.back()->get();
  770. }
  771. if (tc>ilevel) {
  772. indent_stack.push_back(tc);
  773. } else if (tc<ilevel) {
  774. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  775. indent_stack.pop_back();
  776. }
  777. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  778. indent_stack.push_back(tc); //this is not right but gets the job done
  779. }
  780. if (i>=p_from_line) {
  781. l="";
  782. for(int j=0;j<indent_stack.size();j++)
  783. l+="\t";
  784. l+=st;
  785. } else if (i>p_to_line) {
  786. break;
  787. }
  788. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  789. lines[i]=l;
  790. }
  791. p_code="";
  792. for(int i=0;i<lines.size();i++) {
  793. if (i>0)
  794. p_code+="\n";
  795. p_code+=lines[i];
  796. }
  797. }