gd_editor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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);
  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. static void _parse_expression_node(const GDParser::Node *p_node,List<String>* r_options,List<String>::Element *p_indices) {
  299. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  300. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  301. _parse_completion_variant(cn->value,r_options,p_indices?p_indices->next():NULL);
  302. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  303. const GDParser::DictionaryNode *dn=static_cast<const GDParser::DictionaryNode*>(p_node);
  304. for(int i=0;i<dn->elements.size();i++) {
  305. if (dn->elements[i].key->type==GDParser::Node::TYPE_CONSTANT) {
  306. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(dn->elements[i].key);
  307. if (cn->value.get_type()==Variant::STRING) {
  308. String str=cn->value;
  309. if (p_indices) {
  310. if (str==p_indices->get()) {
  311. _parse_expression_node(dn->elements[i].value,r_options,p_indices->next());
  312. return;
  313. }
  314. } else {
  315. r_options->push_back(str);
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. static bool _parse_completion_block(const GDParser::BlockNode *p_block,int p_line,List<String>* r_options,List<String>::Element *p_indices) {
  323. for(int i=0;i<p_block->sub_blocks.size();i++) {
  324. //parse inner first
  325. 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)) {
  326. if (_parse_completion_block(p_block->sub_blocks[i],p_line,r_options,p_indices))
  327. return true;
  328. }
  329. }
  330. if (p_indices) {
  331. //parse indices in expressions :|
  332. for (int i=0;i<p_block->statements.size();i++) {
  333. if (p_block->statements[i]->line>p_line)
  334. break;
  335. if (p_block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  336. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(p_block->statements[i]);
  337. if (lv->assign && String(lv->name)==p_indices->get()) {
  338. _parse_expression_node(lv->assign,r_options,p_indices->next());
  339. return true;
  340. }
  341. }
  342. }
  343. } else {
  344. for(int i=0;i<p_block->variables.size();i++) {
  345. //parse variables second
  346. if (p_line>=p_block->variable_lines[i]) {
  347. r_options->push_back(p_block->variables[i]);
  348. }
  349. else break;
  350. }
  351. }
  352. return false;
  353. }
  354. static bool _parse_script_symbols(const Ref<GDScript>& p_script,bool p_static,List<String>* r_options,List<String>::Element *p_indices) {
  355. //for (Map<StringName,Ref<GDScript> >::Element ?
  356. if (!p_static && !p_indices) {
  357. for(const Set<StringName>::Element *E=p_script->get_members().front();E;E=E->next()) {
  358. r_options->push_back(E->get());
  359. }
  360. }
  361. for (const Map<StringName,Variant >::Element *E=p_script->get_constants().front();E;E=E->next()) {
  362. if( p_indices) {
  363. if (p_indices->get()==String(E->get())) {
  364. _parse_completion_variant(E->get(),r_options,p_indices->next());
  365. return true;
  366. }
  367. } else {
  368. r_options->push_back(E->key());
  369. }
  370. }
  371. if (!p_indices){
  372. for (const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().front();E;E=E->next()) {
  373. if (E->get().is_static() || !p_static)
  374. r_options->push_back(E->key());
  375. }
  376. }
  377. if (p_script->get_base().is_valid()){
  378. if (_parse_script_symbols(p_script->get_base(),p_static,r_options,p_indices))
  379. return true;
  380. } else if (p_script->get_native().is_valid() && !p_indices) {
  381. _parse_native_symbols(p_script->get_native()->get_name(),p_static,r_options);
  382. }
  383. return false;
  384. }
  385. 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) {
  386. static const char*_type_names[Variant::VARIANT_MAX]={
  387. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Trasnform",
  388. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  389. "Vector2Array","Vector3Array","ColorArray"};
  390. if (p_indices && !p_indices->next()) {
  391. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  392. if (p_indices->get()==_type_names[i]) {
  393. List<StringName> ic;
  394. Variant::get_numeric_constants_for_type(Variant::Type(i),&ic);
  395. for(List<StringName>::Element *E=ic.front();E;E=E->next()) {
  396. r_options->push_back(E->get());
  397. }
  398. return true;
  399. }
  400. }
  401. }
  402. for(int i=0;i<p_class->subclasses.size();i++) {
  403. if (p_line>=p_class->subclasses[i]->line && (p_line<=p_class->subclasses[i]->end_line || p_class->subclasses[i]->end_line==-1)) {
  404. if (_parse_completion_class(p_base_path,p_class->subclasses[i],p_line,r_options,p_indices))
  405. return true;
  406. }
  407. }
  408. bool in_static_func=false;
  409. for(int i=0;i<p_class->functions.size();i++) {
  410. const GDParser::FunctionNode *fu = p_class->functions[i];
  411. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  412. //if in function, first block stuff from outer to inner
  413. if (_parse_completion_block(fu->body,p_line,r_options,p_indices))
  414. return true;
  415. //then function arguments
  416. if (!p_indices) {
  417. for(int j=0;j<fu->arguments.size();j++) {
  418. r_options->push_back(fu->arguments[j]);
  419. }
  420. }
  421. }
  422. }
  423. for(int i=0;i<p_class->static_functions.size();i++) {
  424. const GDParser::FunctionNode *fu = p_class->static_functions[i];
  425. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  426. //if in function, first block stuff from outer to inne
  427. if (_parse_completion_block(fu->body,p_line,r_options,p_indices))
  428. return true;
  429. //then function arguments
  430. if (!p_indices) {
  431. for(int j=0;j<fu->arguments.size();j++) {
  432. r_options->push_back(fu->arguments[j]);
  433. }
  434. }
  435. in_static_func=true;
  436. }
  437. }
  438. //add all local names
  439. if (!p_indices) {
  440. if (!in_static_func) {
  441. for(int i=0;i<p_class->variables.size();i++) {
  442. r_options->push_back(p_class->variables[i].identifier);
  443. }
  444. }
  445. for(int i=0;i<p_class->constant_expressions.size();i++) {
  446. r_options->push_back(p_class->constant_expressions[i].identifier);
  447. }
  448. if (!in_static_func) {
  449. for(int i=0;i<p_class->functions.size();i++) {
  450. r_options->push_back(p_class->functions[i]->name);
  451. }
  452. }
  453. for(int i=0;i<p_class->static_functions.size();i++) {
  454. r_options->push_back(p_class->static_functions[i]->name);
  455. }
  456. }
  457. if (p_class->extends_used) {
  458. //do inheritance
  459. String path = p_class->extends_file;
  460. Ref<GDScript> script;
  461. Ref<GDNativeClass> native;
  462. if (path!="") {
  463. //path (and optionally subclasses)
  464. script = ResourceLoader::load(path);
  465. if (script.is_null()) {
  466. return false;
  467. }
  468. if (p_class->extends_class.size()) {
  469. for(int i=0;i<p_class->extends_class.size();i++) {
  470. String sub = p_class->extends_class[i];
  471. if (script->get_subclasses().has(sub)) {
  472. script=script->get_subclasses()[sub];
  473. } else {
  474. return false;
  475. }
  476. }
  477. }
  478. } else {
  479. ERR_FAIL_COND_V(p_class->extends_class.size()==0,false);
  480. //look around for the subclasses
  481. String base=p_class->extends_class[0];
  482. Ref<GDScript> base_class;
  483. #if 0
  484. while(p) {
  485. if (p->subclasses.has(base)) {
  486. base_class=p->subclasses[base];
  487. break;
  488. }
  489. p=p->_owner;
  490. }
  491. if (base_class.is_valid()) {
  492. for(int i=1;i<p_class->extends_class.size();i++) {
  493. String subclass=p_class->extends_class[i];
  494. if (base_class->subclasses.has(subclass)) {
  495. base_class=base_class->subclasses[subclass];
  496. } else {
  497. _set_error("Could not find subclass: "+subclass,p_class);
  498. return ERR_FILE_NOT_FOUND;
  499. }
  500. }
  501. } else {
  502. #endif
  503. if (p_class->extends_class.size()>1) {
  504. return false;
  505. }
  506. //if not found, try engine classes
  507. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  508. return false;
  509. }
  510. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  511. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  512. if (!native.is_valid()) {
  513. return false;
  514. }
  515. #if 0
  516. }
  517. #endif
  518. }
  519. if (script.is_valid()) {
  520. if (_parse_script_symbols(script,in_static_func,r_options,p_indices))
  521. return true;
  522. } else if (native.is_valid() && !p_indices) {
  523. _parse_native_symbols(native->get_name(),in_static_func,r_options);
  524. }
  525. }
  526. return false;
  527. }
  528. Error GDScriptLanguage::complete_keyword(const String& p_code, int p_line, const String& p_base_path, const String& p_base, List<String>* r_options) {
  529. GDParser p;
  530. Error err = p.parse(p_code,p_base_path);
  531. // don't care much about error I guess
  532. const GDParser::Node* root = p.get_parse_tree();
  533. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  534. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  535. List<String> indices;
  536. Vector<String> spl = p_base.split(".");
  537. for(int i=0;i<spl.size()-1;i++) {
  538. indices.push_back(spl[i]);
  539. }
  540. if (_parse_completion_class(p_base,cl,p_line,r_options,indices.front()))
  541. return OK;
  542. //and the globals x_x?
  543. for(Map<StringName,int>::Element *E=globals.front();E;E=E->next()) {
  544. if (!indices.empty()) {
  545. if (String(E->key())==indices.front()->get()) {
  546. _parse_completion_variant(global_array[E->get()],r_options,indices.front()->next());
  547. return OK;
  548. }
  549. } else {
  550. r_options->push_back(E->key());
  551. }
  552. }
  553. return OK;
  554. }
  555. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  556. Vector<String> lines = p_code.split("\n");
  557. List<int> indent_stack;
  558. for(int i=0;i<lines.size();i++) {
  559. String l = lines[i];
  560. int tc=0;
  561. for(int j=0;j<l.length();j++) {
  562. if (l[j]==' ' || l[j]=='\t') {
  563. tc++;
  564. } else {
  565. break;
  566. }
  567. }
  568. String st = l.substr(tc,l.length()).strip_edges();
  569. if (st=="" || st.begins_with("#"))
  570. continue; //ignore!
  571. int ilevel=0;
  572. if (indent_stack.size()) {
  573. ilevel=indent_stack.back()->get();
  574. }
  575. if (tc>ilevel) {
  576. indent_stack.push_back(tc);
  577. } else if (tc<ilevel) {
  578. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  579. indent_stack.pop_back();
  580. }
  581. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  582. indent_stack.push_back(tc); //this is not right but gets the job done
  583. }
  584. if (i>=p_from_line) {
  585. l="";
  586. for(int j=0;j<indent_stack.size();j++)
  587. l+="\t";
  588. l+=st;
  589. } else if (i>p_to_line) {
  590. break;
  591. }
  592. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  593. lines[i]=l;
  594. }
  595. p_code="";
  596. for(int i=0;i<lines.size();i++) {
  597. if (i>0)
  598. p_code+="\n";
  599. p_code+=lines[i];
  600. }
  601. }