gd_editor.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. }
  34. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  35. p_delimiters->push_back("\" \"");
  36. p_delimiters->push_back("' '");
  37. }
  38. String GDScriptLanguage::get_template(const String& p_class_name, const String& p_base_class_name) const {
  39. String _template = String()+
  40. "\nextends %BASE%\n\n"+
  41. "# member variables here, example:\n"+
  42. "# var a=2\n"+
  43. "# var b=\"textvar\"\n\n"+
  44. "func _ready():\n"+
  45. "\t# Initalization here\n"+
  46. "\tpass\n"+
  47. "\n"+
  48. "\n";
  49. return _template.replace("%BASE%",p_base_class_name);
  50. }
  51. 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 {
  52. GDParser parser;
  53. Error err = parser.parse(p_script,p_path.get_base_dir(),true);
  54. if (err) {
  55. r_line_error=parser.get_error_line();
  56. r_col_error=parser.get_error_column();
  57. r_test_error=parser.get_error();
  58. return false;
  59. } else {
  60. const GDParser::Node *root = parser.get_parse_tree();
  61. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,false);
  62. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  63. Map<int,String> funcs;
  64. for(int i=0;i<cl->functions.size();i++) {
  65. funcs[cl->functions[i]->line]=cl->functions[i]->name;
  66. }
  67. for(int i=0;i<cl->static_functions.size();i++) {
  68. funcs[cl->static_functions[i]->line]=cl->static_functions[i]->name;
  69. }
  70. for (Map<int,String>::Element *E=funcs.front();E;E=E->next()) {
  71. r_functions->push_back(E->get()+":"+itos(E->key()));
  72. }
  73. }
  74. return true;
  75. }
  76. bool GDScriptLanguage::has_named_classes() const {
  77. return false;
  78. }
  79. int GDScriptLanguage::find_function(const String& p_function,const String& p_code) const {
  80. GDTokenizerText tokenizer;
  81. tokenizer.set_code(p_code);
  82. int indent=0;
  83. while(tokenizer.get_token()!=GDTokenizer::TK_EOF && tokenizer.get_token()!=GDTokenizer::TK_ERROR) {
  84. if (tokenizer.get_token()==GDTokenizer::TK_NEWLINE) {
  85. indent=tokenizer.get_token_line_indent();
  86. }
  87. if (indent==0 && tokenizer.get_token()==GDTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1)==GDTokenizer::TK_IDENTIFIER) {
  88. String identifier = tokenizer.get_token_identifier(1);
  89. if (identifier==p_function) {
  90. return tokenizer.get_token_line();
  91. }
  92. }
  93. tokenizer.advance();
  94. }
  95. return -1;
  96. }
  97. Script *GDScriptLanguage::create_script() const {
  98. return memnew( GDScript );
  99. }
  100. /* DEBUGGER FUNCTIONS */
  101. bool GDScriptLanguage::debug_break_parse(const String& p_file, int p_line,const String& p_error) {
  102. //break because of parse error
  103. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  104. _debug_parse_err_line=p_line;
  105. _debug_parse_err_file=p_file;
  106. _debug_error=p_error;
  107. ScriptDebugger::get_singleton()->debug(this,false);
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. bool GDScriptLanguage::debug_break(const String& p_error,bool p_allow_continue) {
  114. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  115. _debug_parse_err_line=-1;
  116. _debug_parse_err_file="";
  117. _debug_error=p_error;
  118. ScriptDebugger::get_singleton()->debug(this,p_allow_continue);
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. String GDScriptLanguage::debug_get_error() const {
  125. return _debug_error;
  126. }
  127. int GDScriptLanguage::debug_get_stack_level_count() const {
  128. if (_debug_parse_err_line>=0)
  129. return 1;
  130. return _debug_call_stack_pos;
  131. }
  132. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  133. if (_debug_parse_err_line>=0)
  134. return _debug_parse_err_line;
  135. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,-1);
  136. int l = _debug_call_stack_pos - p_level -1;
  137. return *(_call_stack[l].line);
  138. }
  139. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  140. if (_debug_parse_err_line>=0)
  141. return "";
  142. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  143. int l = _debug_call_stack_pos - p_level -1;
  144. return _call_stack[l].function->get_name();
  145. }
  146. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  147. if (_debug_parse_err_line>=0)
  148. return _debug_parse_err_file;
  149. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  150. int l = _debug_call_stack_pos - p_level -1;
  151. return _call_stack[l].function->get_script()->get_path();
  152. }
  153. 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) {
  154. if (_debug_parse_err_line>=0)
  155. return;
  156. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  157. int l = _debug_call_stack_pos - p_level -1;
  158. GDFunction *f = _call_stack[l].function;
  159. List<Pair<StringName,int> > locals;
  160. f->debug_get_stack_member_state(*_call_stack[l].line,&locals);
  161. for( List<Pair<StringName,int> >::Element *E = locals.front();E;E=E->next() ) {
  162. p_locals->push_back(E->get().first);
  163. p_values->push_back(_call_stack[l].stack[E->get().second]);
  164. }
  165. }
  166. 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) {
  167. if (_debug_parse_err_line>=0)
  168. return;
  169. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  170. int l = _debug_call_stack_pos - p_level -1;
  171. GDInstance *instance = _call_stack[l].instance;
  172. if (!instance)
  173. return;
  174. Ref<GDScript> script = instance->get_script();
  175. ERR_FAIL_COND( script.is_null() );
  176. const Map<StringName,int>& mi = script->debug_get_member_indices();
  177. for(const Map<StringName,int>::Element *E=mi.front();E;E=E->next()) {
  178. p_members->push_back(E->key());
  179. p_values->push_back( instance->debug_get_member_by_index(E->get()));
  180. }
  181. }
  182. void GDScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  183. //no globals are really reachable in gdscript
  184. }
  185. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems,int p_max_depth) {
  186. if (_debug_parse_err_line>=0)
  187. return "";
  188. return "";
  189. }
  190. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  191. p_extensions->push_back("gd");
  192. }
  193. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  194. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  195. p_functions->push_back(GDFunctions::get_info(GDFunctions::Function(i)));
  196. }
  197. }
  198. void GDScriptLanguage::get_public_constants(List<Pair<String,Variant> > *p_constants) const {
  199. Pair<String,Variant> pi;
  200. pi.first="PI";
  201. pi.second=Math_PI;
  202. p_constants->push_back(pi);
  203. }
  204. String GDScriptLanguage::make_function(const String& p_class,const String& p_name,const StringArray& p_args) const {
  205. String s="func "+p_name+"(";
  206. if (p_args.size()) {
  207. s+=" ";
  208. for(int i=0;i<p_args.size();i++) {
  209. if (i>0)
  210. s+=", ";
  211. s+=p_args[i];
  212. }
  213. s+=" ";
  214. }
  215. s+="):\n\tpass # replace with function body\n";
  216. return s;
  217. }
  218. static void _parse_native_symbols(const StringName& p_native,bool p_static,List<String>* r_options) {
  219. if (!p_static) {
  220. List<MethodInfo> methods;
  221. ObjectTypeDB::get_method_list(p_native,&methods);
  222. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  223. if (!E->get().name.begins_with("_")) {
  224. r_options->push_back(E->get().name);
  225. }
  226. }
  227. }
  228. List<String> constants;
  229. ObjectTypeDB::get_integer_constant_list(p_native,&constants);
  230. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  231. r_options->push_back(E->get());
  232. }
  233. }
  234. static bool _parse_script_symbols(const Ref<GDScript>& p_script,bool p_static,List<String>* r_options,List<String>::Element *p_indices);
  235. static bool _parse_completion_variant(const Variant& p_var,List<String>* r_options,List<String>::Element *p_indices) {
  236. if (p_indices) {
  237. bool ok;
  238. Variant si = p_var.get(p_indices->get(),&ok);
  239. if (!ok)
  240. return false;
  241. return _parse_completion_variant(si,r_options,p_indices->next());
  242. } else {
  243. switch(p_var.get_type()) {
  244. case Variant::DICTIONARY: {
  245. Dictionary d=p_var;
  246. List<Variant> vl;
  247. d.get_key_list(&vl);
  248. for (List<Variant>::Element *E=vl.front();E;E=E->next()) {
  249. if (E->get().get_type()==Variant::STRING)
  250. r_options->push_back(E->get());
  251. }
  252. List<MethodInfo> ml;
  253. p_var.get_method_list(&ml);
  254. for(List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  255. r_options->push_back(E->get().name);
  256. }
  257. } break;
  258. case Variant::OBJECT: {
  259. Object *o=p_var;
  260. if (o) {
  261. print_line("OBJECT: "+o->get_type());
  262. if (p_var.is_ref() && o->cast_to<GDScript>()) {
  263. Ref<GDScript> gds = p_var;
  264. _parse_script_symbols(gds,true,r_options,NULL);
  265. } else if (o->is_type("GDNativeClass")){
  266. GDNativeClass *gnc = o->cast_to<GDNativeClass>();
  267. _parse_native_symbols(gnc->get_name(),false,r_options);
  268. } else {
  269. print_line("REGULAR BLEND");
  270. _parse_native_symbols(o->get_type(),false,r_options);
  271. }
  272. }
  273. } break;
  274. default: {
  275. List<PropertyInfo> pi;
  276. p_var.get_property_list(&pi);
  277. for(List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  278. r_options->push_back(E->get().name);
  279. }
  280. List<StringName> cl;
  281. p_var.get_numeric_constants_for_type(p_var.get_type(),&cl);
  282. for(List<StringName>::Element *E=cl.front();E;E=E->next()) {
  283. r_options->push_back(E->get());
  284. }
  285. List<MethodInfo> ml;
  286. p_var.get_method_list(&ml);
  287. for(List<MethodInfo>::Element *E=ml.front();E;E=E->next()) {
  288. r_options->push_back(E->get().name);
  289. }
  290. } break;
  291. }
  292. return true;
  293. }
  294. }
  295. static void _parse_expression_node(const GDParser::Node *p_node,List<String>* r_options,List<String>::Element *p_indices) {
  296. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  297. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  298. _parse_completion_variant(cn->value,r_options,p_indices?p_indices->next():NULL);
  299. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  300. const GDParser::DictionaryNode *dn=static_cast<const GDParser::DictionaryNode*>(p_node);
  301. for(int i=0;i<dn->elements.size();i++) {
  302. if (dn->elements[i].key->type==GDParser::Node::TYPE_CONSTANT) {
  303. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(dn->elements[i].key);
  304. if (cn->value.get_type()==Variant::STRING) {
  305. String str=cn->value;
  306. if (p_indices) {
  307. if (str==p_indices->get()) {
  308. _parse_expression_node(dn->elements[i].value,r_options,p_indices->next());
  309. return;
  310. }
  311. } else {
  312. r_options->push_back(str);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. static bool _parse_completion_block(const GDParser::BlockNode *p_block,int p_line,List<String>* r_options,List<String>::Element *p_indices) {
  320. for(int i=0;i<p_block->sub_blocks.size();i++) {
  321. //parse inner first
  322. 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)) {
  323. if (_parse_completion_block(p_block->sub_blocks[i],p_line,r_options,p_indices))
  324. return true;
  325. }
  326. }
  327. if (p_indices) {
  328. //parse indices in expressions :|
  329. for (int i=0;i<p_block->statements.size();i++) {
  330. if (p_block->statements[i]->line>p_line)
  331. break;
  332. if (p_block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  333. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(p_block->statements[i]);
  334. if (lv->assign && String(lv->name)==p_indices->get()) {
  335. _parse_expression_node(lv->assign,r_options,p_indices->next());
  336. return true;
  337. }
  338. }
  339. }
  340. } else {
  341. for(int i=0;i<p_block->variables.size();i++) {
  342. //parse variables second
  343. if (p_line>=p_block->variable_lines[i]) {
  344. r_options->push_back(p_block->variables[i]);
  345. }
  346. else break;
  347. }
  348. }
  349. return false;
  350. }
  351. static bool _parse_script_symbols(const Ref<GDScript>& p_script,bool p_static,List<String>* r_options,List<String>::Element *p_indices) {
  352. //for (Map<StringName,Ref<GDScript> >::Element ?
  353. if (!p_static && !p_indices) {
  354. for(const Set<StringName>::Element *E=p_script->get_members().front();E;E=E->next()) {
  355. r_options->push_back(E->get());
  356. }
  357. }
  358. for (const Map<StringName,Variant >::Element *E=p_script->get_constants().front();E;E=E->next()) {
  359. if( p_indices) {
  360. if (p_indices->get()==String(E->get())) {
  361. _parse_completion_variant(E->get(),r_options,p_indices->next());
  362. return true;
  363. }
  364. } else {
  365. r_options->push_back(E->key());
  366. }
  367. }
  368. if (!p_indices){
  369. for (const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().front();E;E=E->next()) {
  370. if (E->get().is_static() || !p_static)
  371. r_options->push_back(E->key());
  372. }
  373. }
  374. if (p_script->get_base().is_valid()){
  375. if (_parse_script_symbols(p_script->get_base(),p_static,r_options,p_indices))
  376. return true;
  377. } else if (p_script->get_native().is_valid() && !p_indices) {
  378. _parse_native_symbols(p_script->get_native()->get_name(),p_static,r_options);
  379. }
  380. return false;
  381. }
  382. 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) {
  383. static const char*_type_names[Variant::VARIANT_MAX]={
  384. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Trasnform",
  385. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  386. "Vector2Array","Vector3Array","ColorArray"};
  387. if (p_indices && !p_indices->next()) {
  388. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  389. if (p_indices->get()==_type_names[i]) {
  390. List<StringName> ic;
  391. Variant::get_numeric_constants_for_type(Variant::Type(i),&ic);
  392. for(List<StringName>::Element *E=ic.front();E;E=E->next()) {
  393. r_options->push_back(E->get());
  394. }
  395. return true;
  396. }
  397. }
  398. }
  399. for(int i=0;i<p_class->subclasses.size();i++) {
  400. if (p_line>=p_class->subclasses[i]->line && (p_line<=p_class->subclasses[i]->end_line || p_class->subclasses[i]->end_line==-1)) {
  401. if (_parse_completion_class(p_base_path,p_class->subclasses[i],p_line,r_options,p_indices))
  402. return true;
  403. }
  404. }
  405. bool in_static_func=false;
  406. for(int i=0;i<p_class->functions.size();i++) {
  407. const GDParser::FunctionNode *fu = p_class->functions[i];
  408. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  409. //if in function, first block stuff from outer to inner
  410. if (_parse_completion_block(fu->body,p_line,r_options,p_indices))
  411. return true;
  412. //then function arguments
  413. if (!p_indices) {
  414. for(int j=0;j<fu->arguments.size();j++) {
  415. r_options->push_back(fu->arguments[j]);
  416. }
  417. }
  418. }
  419. }
  420. for(int i=0;i<p_class->static_functions.size();i++) {
  421. const GDParser::FunctionNode *fu = p_class->static_functions[i];
  422. if (p_line>=fu->body->line && (p_line<=fu->body->end_line || fu->body->end_line==-1)) {
  423. //if in function, first block stuff from outer to inne
  424. if (_parse_completion_block(fu->body,p_line,r_options,p_indices))
  425. return true;
  426. //then function arguments
  427. if (!p_indices) {
  428. for(int j=0;j<fu->arguments.size();j++) {
  429. r_options->push_back(fu->arguments[j]);
  430. }
  431. }
  432. in_static_func=true;
  433. }
  434. }
  435. //add all local names
  436. if (!p_indices) {
  437. if (!in_static_func) {
  438. for(int i=0;i<p_class->variables.size();i++) {
  439. r_options->push_back(p_class->variables[i].identifier);
  440. }
  441. }
  442. for(int i=0;i<p_class->constant_expressions.size();i++) {
  443. r_options->push_back(p_class->constant_expressions[i].identifier);
  444. }
  445. if (!in_static_func) {
  446. for(int i=0;i<p_class->functions.size();i++) {
  447. r_options->push_back(p_class->functions[i]->name);
  448. }
  449. }
  450. for(int i=0;i<p_class->static_functions.size();i++) {
  451. r_options->push_back(p_class->static_functions[i]->name);
  452. }
  453. }
  454. if (p_class->extends_used) {
  455. //do inheritance
  456. String path = p_class->extends_file;
  457. Ref<GDScript> script;
  458. Ref<GDNativeClass> native;
  459. if (path!="") {
  460. //path (and optionally subclasses)
  461. script = ResourceLoader::load(path);
  462. if (script.is_null()) {
  463. return false;
  464. }
  465. if (p_class->extends_class.size()) {
  466. for(int i=0;i<p_class->extends_class.size();i++) {
  467. String sub = p_class->extends_class[i];
  468. if (script->get_subclasses().has(sub)) {
  469. script=script->get_subclasses()[sub];
  470. } else {
  471. return false;
  472. }
  473. }
  474. }
  475. } else {
  476. ERR_FAIL_COND_V(p_class->extends_class.size()==0,false);
  477. //look around for the subclasses
  478. String base=p_class->extends_class[0];
  479. Ref<GDScript> base_class;
  480. #if 0
  481. while(p) {
  482. if (p->subclasses.has(base)) {
  483. base_class=p->subclasses[base];
  484. break;
  485. }
  486. p=p->_owner;
  487. }
  488. if (base_class.is_valid()) {
  489. for(int i=1;i<p_class->extends_class.size();i++) {
  490. String subclass=p_class->extends_class[i];
  491. if (base_class->subclasses.has(subclass)) {
  492. base_class=base_class->subclasses[subclass];
  493. } else {
  494. _set_error("Could not find subclass: "+subclass,p_class);
  495. return ERR_FILE_NOT_FOUND;
  496. }
  497. }
  498. } else {
  499. #endif
  500. if (p_class->extends_class.size()>1) {
  501. return false;
  502. }
  503. //if not found, try engine classes
  504. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  505. return false;
  506. }
  507. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  508. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  509. if (!native.is_valid()) {
  510. return false;
  511. }
  512. #if 0
  513. }
  514. #endif
  515. }
  516. if (script.is_valid()) {
  517. if (_parse_script_symbols(script,in_static_func,r_options,p_indices))
  518. return true;
  519. } else if (native.is_valid() && !p_indices) {
  520. _parse_native_symbols(native->get_name(),in_static_func,r_options);
  521. }
  522. }
  523. return false;
  524. }
  525. Error GDScriptLanguage::complete_keyword(const String& p_code, int p_line, const String& p_base_path, const String& p_base, List<String>* r_options) {
  526. GDParser p;
  527. Error err = p.parse(p_code,p_base_path);
  528. // don't care much about error I guess
  529. const GDParser::Node* root = p.get_parse_tree();
  530. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  531. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  532. List<String> indices;
  533. Vector<String> spl = p_base.split(".");
  534. for(int i=0;i<spl.size()-1;i++) {
  535. indices.push_back(spl[i]);
  536. }
  537. if (_parse_completion_class(p_base,cl,p_line,r_options,indices.front()))
  538. return OK;
  539. //and the globals x_x?
  540. for(Map<StringName,int>::Element *E=globals.front();E;E=E->next()) {
  541. if (!indices.empty()) {
  542. if (String(E->key())==indices.front()->get()) {
  543. _parse_completion_variant(global_array[E->get()],r_options,indices.front()->next());
  544. return OK;
  545. }
  546. } else {
  547. r_options->push_back(E->key());
  548. }
  549. }
  550. return OK;
  551. }
  552. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  553. Vector<String> lines = p_code.split("\n");
  554. List<int> indent_stack;
  555. for(int i=0;i<lines.size();i++) {
  556. String l = lines[i];
  557. int tc=0;
  558. for(int j=0;j<l.length();j++) {
  559. if (l[j]==' ' || l[j]=='\t') {
  560. tc++;
  561. } else {
  562. break;
  563. }
  564. }
  565. String st = l.substr(tc,l.length()).strip_edges();
  566. if (st=="" || st.begins_with("#"))
  567. continue; //ignore!
  568. int ilevel=0;
  569. if (indent_stack.size()) {
  570. ilevel=indent_stack.back()->get();
  571. }
  572. if (tc>ilevel) {
  573. indent_stack.push_back(tc);
  574. } else if (tc<ilevel) {
  575. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  576. indent_stack.pop_back();
  577. }
  578. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  579. indent_stack.push_back(tc); //this is not right but gets the job done
  580. }
  581. if (i>=p_from_line) {
  582. l="";
  583. for(int j=0;j<indent_stack.size();j++)
  584. l+="\t";
  585. l+=st;
  586. } else if (i>p_to_line) {
  587. break;
  588. }
  589. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  590. lines[i]=l;
  591. }
  592. p_code="";
  593. for(int i=0;i<lines.size();i++) {
  594. if (i>0)
  595. p_code+="\n";
  596. p_code+=lines[i];
  597. }
  598. }