gd_editor.cpp 51 KB

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