gd_editor.cpp 31 KB

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