gd_editor.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  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. #include "os/file_access.h"
  33. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  34. p_delimiters->push_back("#");
  35. p_delimiters->push_back("\"\"\" \"\"\"");
  36. }
  37. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  38. p_delimiters->push_back("\" \"");
  39. p_delimiters->push_back("' '");
  40. }
  41. String GDScriptLanguage::get_template(const String& p_class_name, const String& p_base_class_name) const {
  42. String _template = String()+
  43. "\nextends %BASE%\n\n"+
  44. "# member variables here, example:\n"+
  45. "# var a=2\n"+
  46. "# var b=\"textvar\"\n\n"+
  47. "func _ready():\n"+
  48. "\t# Initialization here\n"+
  49. "\tpass\n"+
  50. "\n"+
  51. "\n";
  52. return _template.replace("%BASE%",p_base_class_name);
  53. }
  54. 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 {
  55. GDParser parser;
  56. Error err = parser.parse(p_script,p_path.get_base_dir(),true,p_path);
  57. if (err) {
  58. r_line_error=parser.get_error_line();
  59. r_col_error=parser.get_error_column();
  60. r_test_error=parser.get_error();
  61. return false;
  62. } else {
  63. const GDParser::Node *root = parser.get_parse_tree();
  64. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,false);
  65. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  66. Map<int,String> funcs;
  67. for(int i=0;i<cl->functions.size();i++) {
  68. funcs[cl->functions[i]->line]=cl->functions[i]->name;
  69. }
  70. for(int i=0;i<cl->static_functions.size();i++) {
  71. funcs[cl->static_functions[i]->line]=cl->static_functions[i]->name;
  72. }
  73. for (Map<int,String>::Element *E=funcs.front();E;E=E->next()) {
  74. r_functions->push_back(E->get()+":"+itos(E->key()));
  75. }
  76. }
  77. return true;
  78. }
  79. bool GDScriptLanguage::has_named_classes() const {
  80. return false;
  81. }
  82. int GDScriptLanguage::find_function(const String& p_function,const String& p_code) const {
  83. GDTokenizerText tokenizer;
  84. tokenizer.set_code(p_code);
  85. int indent=0;
  86. while(tokenizer.get_token()!=GDTokenizer::TK_EOF && tokenizer.get_token()!=GDTokenizer::TK_ERROR) {
  87. if (tokenizer.get_token()==GDTokenizer::TK_NEWLINE) {
  88. indent=tokenizer.get_token_line_indent();
  89. }
  90. //print_line("TOKEN: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  91. if (indent==0 && tokenizer.get_token()==GDTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1)==GDTokenizer::TK_IDENTIFIER) {
  92. String identifier = tokenizer.get_token_identifier(1);
  93. if (identifier==p_function) {
  94. return tokenizer.get_token_line();
  95. }
  96. }
  97. tokenizer.advance();
  98. //print_line("NEXT: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  99. }
  100. return -1;
  101. }
  102. Script *GDScriptLanguage::create_script() const {
  103. return memnew( GDScript );
  104. }
  105. /* DEBUGGER FUNCTIONS */
  106. bool GDScriptLanguage::debug_break_parse(const String& p_file, int p_line,const String& p_error) {
  107. //break because of parse error
  108. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  109. _debug_parse_err_line=p_line;
  110. _debug_parse_err_file=p_file;
  111. _debug_error=p_error;
  112. ScriptDebugger::get_singleton()->debug(this,false);
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. bool GDScriptLanguage::debug_break(const String& p_error,bool p_allow_continue) {
  119. if (ScriptDebugger::get_singleton() && Thread::get_caller_ID()==Thread::get_main_ID()) {
  120. _debug_parse_err_line=-1;
  121. _debug_parse_err_file="";
  122. _debug_error=p_error;
  123. ScriptDebugger::get_singleton()->debug(this,p_allow_continue);
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. String GDScriptLanguage::debug_get_error() const {
  130. return _debug_error;
  131. }
  132. int GDScriptLanguage::debug_get_stack_level_count() const {
  133. if (_debug_parse_err_line>=0)
  134. return 1;
  135. return _debug_call_stack_pos;
  136. }
  137. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  138. if (_debug_parse_err_line>=0)
  139. return _debug_parse_err_line;
  140. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,-1);
  141. int l = _debug_call_stack_pos - p_level -1;
  142. return *(_call_stack[l].line);
  143. }
  144. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  145. if (_debug_parse_err_line>=0)
  146. return "";
  147. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  148. int l = _debug_call_stack_pos - p_level -1;
  149. return _call_stack[l].function->get_name();
  150. }
  151. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  152. if (_debug_parse_err_line>=0)
  153. return _debug_parse_err_file;
  154. ERR_FAIL_INDEX_V(p_level,_debug_call_stack_pos,"");
  155. int l = _debug_call_stack_pos - p_level -1;
  156. return _call_stack[l].function->get_script()->get_path();
  157. }
  158. 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) {
  159. if (_debug_parse_err_line>=0)
  160. return;
  161. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  162. int l = _debug_call_stack_pos - p_level -1;
  163. GDFunction *f = _call_stack[l].function;
  164. List<Pair<StringName,int> > locals;
  165. f->debug_get_stack_member_state(*_call_stack[l].line,&locals);
  166. for( List<Pair<StringName,int> >::Element *E = locals.front();E;E=E->next() ) {
  167. p_locals->push_back(E->get().first);
  168. p_values->push_back(_call_stack[l].stack[E->get().second]);
  169. }
  170. }
  171. 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) {
  172. if (_debug_parse_err_line>=0)
  173. return;
  174. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  175. int l = _debug_call_stack_pos - p_level -1;
  176. GDInstance *instance = _call_stack[l].instance;
  177. if (!instance)
  178. return;
  179. Ref<GDScript> script = instance->get_script();
  180. ERR_FAIL_COND( script.is_null() );
  181. const Map<StringName,GDScript::MemberInfo>& mi = script->debug_get_member_indices();
  182. for(const Map<StringName,GDScript::MemberInfo>::Element *E=mi.front();E;E=E->next()) {
  183. p_members->push_back(E->key());
  184. p_values->push_back( instance->debug_get_member_by_index(E->get().index));
  185. }
  186. }
  187. void GDScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems,int p_max_depth) {
  188. //no globals are really reachable in gdscript
  189. }
  190. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems,int p_max_depth) {
  191. if (_debug_parse_err_line>=0)
  192. return "";
  193. return "";
  194. }
  195. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  196. p_extensions->push_back("gd");
  197. }
  198. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  199. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  200. p_functions->push_back(GDFunctions::get_info(GDFunctions::Function(i)));
  201. }
  202. }
  203. void GDScriptLanguage::get_public_constants(List<Pair<String,Variant> > *p_constants) const {
  204. Pair<String,Variant> pi;
  205. pi.first="PI";
  206. pi.second=Math_PI;
  207. p_constants->push_back(pi);
  208. }
  209. String GDScriptLanguage::make_function(const String& p_class,const String& p_name,const StringArray& p_args) const {
  210. String s="func "+p_name+"(";
  211. if (p_args.size()) {
  212. s+=" ";
  213. for(int i=0;i<p_args.size();i++) {
  214. if (i>0)
  215. s+=", ";
  216. s+=p_args[i];
  217. }
  218. s+=" ";
  219. }
  220. s+="):\n\tpass # replace with function body\n";
  221. return s;
  222. }
  223. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  224. struct GDCompletionIdentifier {
  225. StringName obj_type;
  226. Ref<GDScript> script;
  227. Variant::Type type;
  228. Variant value; //im case there is a value, also return it
  229. };
  230. static GDCompletionIdentifier _get_type_from_variant(const Variant& p_variant) {
  231. GDCompletionIdentifier t;
  232. t.type=p_variant.get_type();
  233. t.value=p_variant;
  234. if (p_variant.get_type()==Variant::OBJECT) {
  235. Object *obj = p_variant;
  236. if (obj) {
  237. //if (obj->cast_to<GDNativeClass>()) {
  238. // t.obj_type=obj->cast_to<GDNativeClass>()->get_name();
  239. // t.value=Variant();
  240. //} else {
  241. t.obj_type=obj->get_type();
  242. //}
  243. }
  244. }
  245. return t;
  246. }
  247. static GDCompletionIdentifier _get_type_from_pinfo(const PropertyInfo& p_info) {
  248. GDCompletionIdentifier t;
  249. t.type=p_info.type;
  250. if (p_info.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  251. t.obj_type=p_info.hint_string;
  252. }
  253. return t;
  254. }
  255. struct GDCompletionContext {
  256. const GDParser::ClassNode *_class;
  257. const GDParser::FunctionNode *function;
  258. const GDParser::BlockNode *block;
  259. Object* base;
  260. String base_path;
  261. };
  262. static Ref<Reference> _get_parent_class(GDCompletionContext& context) {
  263. if (context._class->extends_used) {
  264. //do inheritance
  265. String path = context._class->extends_file;
  266. Ref<GDScript> script;
  267. Ref<GDNativeClass> native;
  268. if (path!="") {
  269. //path (and optionally subclasses)
  270. if (path.is_rel_path()) {
  271. path=context.base_path.plus_file(path);
  272. }
  273. if (ScriptCodeCompletionCache::get_sigleton())
  274. script = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  275. else
  276. script = ResourceLoader::load(path);
  277. if (script.is_null()) {
  278. return REF();
  279. }
  280. if (script->is_valid()) {
  281. return REF();
  282. }
  283. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  284. if (context._class->extends_class.size()) {
  285. for(int i=0;i<context._class->extends_class.size();i++) {
  286. String sub = context._class->extends_class[i];
  287. if (script->get_subclasses().has(sub)) {
  288. script=script->get_subclasses()[sub];
  289. } else {
  290. return REF();
  291. }
  292. }
  293. }
  294. if (script.is_valid())
  295. return script;
  296. } else {
  297. if (context._class->extends_class.size()==0) {
  298. ERR_PRINT("BUG");
  299. return REF();
  300. }
  301. String base=context._class->extends_class[0];
  302. const GDParser::ClassNode *p = context._class->owner;
  303. Ref<GDScript> base_class;
  304. #if 0
  305. while(p) {
  306. if (p->subclasses.has(base)) {
  307. base_class=p->subclasses[base];
  308. break;
  309. }
  310. p=p->_owner;
  311. }
  312. #endif
  313. if (base_class.is_valid()) {
  314. #if 0
  315. for(int i=1;i<context._class->extends_class.size();i++) {
  316. String subclass=context._class->extends_class[i];
  317. if (base_class->subclasses.has(subclass)) {
  318. base_class=base_class->subclasses[subclass];
  319. } else {
  320. //print_line("Could not find subclass: "+subclass);
  321. return _get_type_from_class(context); //fail please
  322. }
  323. }
  324. script=base_class;
  325. #endif
  326. } else {
  327. if (context._class->extends_class.size()>1) {
  328. return REF();
  329. }
  330. //if not found, try engine classes
  331. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  332. return REF();
  333. }
  334. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  335. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  336. if (!native.is_valid()) {
  337. print_line("Global not a class: '"+base+"'");
  338. }
  339. return native;
  340. }
  341. }
  342. }
  343. return Ref<Reference>();
  344. }
  345. static GDCompletionIdentifier _get_native_class(GDCompletionContext& context) {
  346. //eeh...
  347. GDCompletionIdentifier id;
  348. id.type=Variant::NIL;
  349. REF pc = _get_parent_class(context);
  350. if (!pc.is_valid()) {
  351. return id;
  352. }
  353. Ref<GDNativeClass> nc = pc;
  354. Ref<GDScript> s = pc;
  355. if (s.is_null() && nc.is_null()) {
  356. return id;
  357. }
  358. while(!s.is_null()) {
  359. nc=s->get_native();
  360. s=s->get_base();
  361. }
  362. if (nc.is_null()) {
  363. return id;
  364. }
  365. id.type=Variant::OBJECT;
  366. if (context.base)
  367. id.value=context.base;
  368. id.obj_type=nc->get_name();
  369. return id;
  370. }
  371. static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type);
  372. static bool _guess_expression_type(GDCompletionContext& context,const GDParser::Node* p_node,int p_line,GDCompletionIdentifier &r_type) {
  373. if (p_node->type==GDParser::Node::TYPE_CONSTANT) {
  374. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(p_node);
  375. r_type=_get_type_from_variant(cn->value);
  376. return true;
  377. } else if (p_node->type==GDParser::Node::TYPE_DICTIONARY) {
  378. r_type.type=Variant::DICTIONARY;
  379. //what the heck, fill it anyway
  380. const GDParser::DictionaryNode *an = static_cast<const GDParser::DictionaryNode *>(p_node);
  381. Dictionary d;
  382. for(int i=0;i<an->elements.size();i++) {
  383. GDCompletionIdentifier k;
  384. if (_guess_expression_type(context,an->elements[i].key,p_line,k) && k.value.get_type()!=Variant::NIL) {
  385. GDCompletionIdentifier v;
  386. if (_guess_expression_type(context,an->elements[i].value,p_line,v)) {
  387. d[k.value]=v.value;
  388. }
  389. }
  390. }
  391. r_type.value=d;
  392. return true;
  393. } else if (p_node->type==GDParser::Node::TYPE_ARRAY) {
  394. r_type.type=Variant::ARRAY;
  395. //what the heck, fill it anyway
  396. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(p_node);
  397. Array arr;
  398. arr.resize(an->elements.size());
  399. for(int i=0;i<an->elements.size();i++) {
  400. GDCompletionIdentifier ci;
  401. if (_guess_expression_type(context,an->elements[i],p_line,ci)) {
  402. arr[i]=ci.value;
  403. }
  404. }
  405. r_type.value=arr;
  406. return true;
  407. } else if (p_node->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  408. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode*>(p_node)->function);
  409. r_type=_get_type_from_pinfo(mi.return_val);
  410. return true;
  411. } else if (p_node->type==GDParser::Node::TYPE_IDENTIFIER) {
  412. return _guess_identifier_type(context,p_line-1,static_cast<const GDParser::IdentifierNode *>(p_node)->name,r_type);
  413. } else if (p_node->type==GDParser::Node::TYPE_SELF) {
  414. //eeh...
  415. r_type=_get_native_class(context);
  416. return r_type.type!=Variant::NIL;
  417. } else if (p_node->type==GDParser::Node::TYPE_OPERATOR) {
  418. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  419. if (op->op==GDParser::OperatorNode::OP_CALL) {
  420. if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  421. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  422. r_type.type=tn->vtype;
  423. return true;
  424. } else if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  425. const GDParser::BuiltInFunctionNode *bin = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  426. return _guess_expression_type(context,bin,p_line,r_type);
  427. } else if (op->arguments.size()>1 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  428. GDCompletionIdentifier base;
  429. if (!_guess_expression_type(context,op->arguments[0],p_line,base))
  430. return false;
  431. StringName id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  432. if (base.type==Variant::OBJECT) {
  433. if (id.operator String()=="new" && base.value.get_type()==Variant::OBJECT) {
  434. Object *obj = base.value;
  435. if (obj && obj->cast_to<GDNativeClass>()) {
  436. GDNativeClass *gdnc = obj->cast_to<GDNativeClass>();
  437. r_type.type=Variant::OBJECT;
  438. r_type.value=Variant();
  439. r_type.obj_type=gdnc->get_name();
  440. return true;
  441. }
  442. }
  443. if (ObjectTypeDB::has_method(base.obj_type,id)) {
  444. #ifdef TOOLS_ENABLED
  445. MethodBind *mb = ObjectTypeDB::get_method(base.obj_type,id);
  446. PropertyInfo pi = mb->get_argument_info(-1);
  447. //try calling the function if constant and all args are constant, should not crash..
  448. Object *baseptr = base.value;
  449. if (mb->is_const() && pi.type==Variant::OBJECT) {
  450. bool all_valid=true;
  451. Vector<Variant> args;
  452. for(int i=2;i<op->arguments.size();i++) {
  453. GDCompletionIdentifier arg;
  454. if (_guess_expression_type(context,op->arguments[i],p_line,arg)) {
  455. if (arg.value.get_type()!=Variant::NIL && arg.value.get_type()!=Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  456. args.push_back(arg.value);
  457. } else {
  458. all_valid=false;
  459. break;
  460. }
  461. } else {
  462. all_valid=false;
  463. }
  464. }
  465. if (all_valid && String(id)=="get_node" && ObjectTypeDB::is_type(base.obj_type,"Node") && args.size()) {
  466. String arg1=args[0];
  467. if (arg1.begins_with("/root/")) {
  468. String which = arg1.get_slice("/",2);
  469. if (which!="") {
  470. List<PropertyInfo> props;
  471. Globals::get_singleton()->get_property_list(&props);
  472. //print_line("find singleton");
  473. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  474. String s = E->get().name;
  475. if (!s.begins_with("autoload/"))
  476. continue;
  477. //print_line("found "+s);
  478. String name = s.get_slice("/",1);
  479. //print_line("name: "+name+", which: "+which);
  480. if (name==which) {
  481. String script = Globals::get_singleton()->get(s);
  482. if (!script.begins_with("res://")) {
  483. script="res://"+script;
  484. }
  485. if (!script.ends_with(".gd")) {
  486. //not a script, try find the script anyway,
  487. //may have some success
  488. script=script.basename()+".gd";
  489. }
  490. if (FileAccess::exists(script)) {
  491. //print_line("is a script");
  492. Ref<Script> scr;
  493. if (ScriptCodeCompletionCache::get_sigleton())
  494. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  495. else
  496. scr = ResourceLoader::load(script);
  497. r_type.obj_type="Node";
  498. r_type.type=Variant::OBJECT;
  499. r_type.script=scr;
  500. r_type.value=Variant();
  501. return true;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. if (baseptr) {
  509. if (all_valid) {
  510. Vector<const Variant*> argptr;
  511. for(int i=0;i<args.size();i++) {
  512. argptr.push_back(&args[i]);
  513. }
  514. Variant::CallError ce;
  515. Variant ret=mb->call(baseptr,argptr.ptr(),argptr.size(),ce);
  516. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()!=Variant::NIL) {
  517. if (ret.get_type()!=Variant::OBJECT || ret.operator Object*()!=NULL) {
  518. r_type=_get_type_from_variant(ret);
  519. return true;
  520. }
  521. }
  522. }
  523. }
  524. }
  525. r_type.type=pi.type;
  526. if (pi.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  527. r_type.obj_type=pi.hint_string;
  528. }
  529. return true;
  530. #else
  531. return false;
  532. #endif
  533. } else {
  534. return false;
  535. }
  536. } else {
  537. //method for some variant..
  538. Variant::CallError ce;
  539. Variant v = Variant::construct(base.type,NULL,0,ce);
  540. List<MethodInfo> mi;
  541. v.get_method_list(&mi);
  542. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  543. if (!E->get().name.begins_with("_") && E->get().name==id.operator String()) {
  544. MethodInfo mi = E->get();
  545. r_type.type=mi.return_val.type;
  546. if (mi.return_val.hint==PROPERTY_HINT_RESOURCE_TYPE) {
  547. r_type.obj_type=mi.return_val.hint_string;
  548. }
  549. return true;
  550. }
  551. }
  552. }
  553. }
  554. } else if (op->op==GDParser::OperatorNode::OP_INDEX || op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  555. GDCompletionIdentifier p1;
  556. GDCompletionIdentifier p2;
  557. if (op->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  558. if (op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  559. String id = static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name;
  560. p2.type=Variant::STRING;
  561. p2.value=id;
  562. }
  563. } else {
  564. if (op->arguments[1]) {
  565. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  566. return false;
  567. }
  568. }
  569. }
  570. if (op->arguments[0]->type==GDParser::Node::TYPE_ARRAY) {
  571. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(op->arguments[0]);
  572. if (p2.value.is_num()) {
  573. int index = p2.value;
  574. if (index<0 || index>=an->elements.size())
  575. return false;
  576. return _guess_expression_type(context,an->elements[index],p_line,r_type);
  577. }
  578. } else if (op->arguments[0]->type==GDParser::Node::TYPE_DICTIONARY) {
  579. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode *>(op->arguments[0]);
  580. if (p2.value.get_type()==Variant::NIL)
  581. return false;
  582. for(int i=0;i<dn->elements.size();i++) {
  583. GDCompletionIdentifier k;
  584. if (!_guess_expression_type(context,dn->elements[i].key,p_line,k)) {
  585. return false;
  586. }
  587. if (k.value.get_type()==Variant::NIL)
  588. return false;
  589. if (k.value==p2.value) {
  590. return _guess_expression_type(context,dn->elements[i].value,p_line,r_type);
  591. }
  592. }
  593. } else {
  594. if (op->arguments[0]) {
  595. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  596. return false;
  597. }
  598. }
  599. if (p1.value.get_type()==Variant::OBJECT) {
  600. //??
  601. } else if (p1.value.get_type()!=Variant::NIL) {
  602. bool valid;
  603. Variant ret = p1.value.get(p2.value,&valid);
  604. if (valid) {
  605. r_type=_get_type_from_variant(ret);
  606. return true;
  607. }
  608. } else {
  609. if (p1.type!=Variant::NIL) {
  610. Variant::CallError ce;
  611. Variant base = Variant::construct(p1.type,NULL,0,ce);
  612. bool valid;
  613. Variant ret = base.get(p2.value,&valid);
  614. if (valid) {
  615. r_type=_get_type_from_variant(ret);
  616. return true;
  617. }
  618. }
  619. }
  620. }
  621. } else {
  622. Variant::Operator vop = Variant::OP_MAX;
  623. switch(op->op) {
  624. case GDParser::OperatorNode::OP_ADD: vop=Variant::OP_ADD; break;
  625. case GDParser::OperatorNode::OP_SUB: vop=Variant::OP_SUBSTRACT; break;
  626. case GDParser::OperatorNode::OP_MUL: vop=Variant::OP_MULTIPLY; break;
  627. case GDParser::OperatorNode::OP_DIV: vop=Variant::OP_DIVIDE; break;
  628. case GDParser::OperatorNode::OP_MOD: vop=Variant::OP_MODULE; break;
  629. case GDParser::OperatorNode::OP_SHIFT_LEFT: vop=Variant::OP_SHIFT_LEFT; break;
  630. case GDParser::OperatorNode::OP_SHIFT_RIGHT: vop=Variant::OP_SHIFT_RIGHT; break;
  631. case GDParser::OperatorNode::OP_BIT_AND: vop=Variant::OP_BIT_AND; break;
  632. case GDParser::OperatorNode::OP_BIT_OR: vop=Variant::OP_BIT_OR; break;
  633. case GDParser::OperatorNode::OP_BIT_XOR: vop=Variant::OP_BIT_XOR; break;
  634. default:{}
  635. }
  636. if (vop==Variant::OP_MAX)
  637. return false;
  638. GDCompletionIdentifier p1;
  639. GDCompletionIdentifier p2;
  640. if (op->arguments[0]) {
  641. if (!_guess_expression_type(context,op->arguments[0],p_line,p1)) {
  642. return false;
  643. }
  644. }
  645. if (op->arguments.size()>1) {
  646. if (!_guess_expression_type(context,op->arguments[1],p_line,p2)) {
  647. return false;
  648. }
  649. }
  650. Variant::CallError ce;
  651. bool v1_use_value = p1.value.get_type()!=Variant::NIL && p1.value.get_type()!=Variant::OBJECT;
  652. Variant v1 = (v1_use_value)?p1.value:Variant::construct(p1.type,NULL,0,ce);
  653. bool v2_use_value = p2.value.get_type()!=Variant::NIL && p2.value.get_type()!=Variant::OBJECT;
  654. Variant v2 = (v2_use_value)?p2.value:Variant::construct(p2.type,NULL,0,ce);
  655. // avoid potential invalid ops
  656. if ((vop==Variant::OP_DIVIDE || vop==Variant::OP_MODULE) && v2.get_type()==Variant::INT) {
  657. v2=1;
  658. v2_use_value=false;
  659. }
  660. if (vop==Variant::OP_DIVIDE && v2.get_type()==Variant::REAL) {
  661. v2=1.0;
  662. v2_use_value=false;
  663. }
  664. Variant r;
  665. bool valid;
  666. Variant::evaluate(vop,v1,v2,r,valid);
  667. if (!valid)
  668. return false;
  669. r_type.type=r.get_type();
  670. if (v1_use_value && v2_use_value)
  671. r_type.value=r;
  672. return true;
  673. }
  674. }
  675. return false;
  676. }
  677. static bool _guess_identifier_type_in_block(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  678. const GDParser::Node *last_assign=NULL;
  679. int last_assign_line=-1;
  680. for (int i=0;i<context.block->statements.size();i++) {
  681. if (context.block->statements[i]->line>p_line)
  682. continue;
  683. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  684. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  685. if (lv->assign && lv->name==p_identifier) {
  686. last_assign=lv->assign;
  687. last_assign_line=context.block->statements[i]->line;
  688. }
  689. }
  690. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
  691. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  692. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  693. if (op->arguments.size() && op->arguments[0]->type==GDParser::Node::TYPE_IDENTIFIER) {
  694. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  695. if (id->name==p_identifier) {
  696. last_assign=op->arguments[1];
  697. last_assign_line=context.block->statements[i]->line;
  698. }
  699. }
  700. }
  701. }
  702. }
  703. //use the last assignment, (then backwards?)
  704. if (last_assign) {
  705. return _guess_expression_type(context,last_assign,last_assign_line,r_type);
  706. }
  707. return false;
  708. }
  709. static bool _guess_identifier_from_assignment_in_function(GDCompletionContext& context,const StringName& p_identifier, const StringName& p_function,GDCompletionIdentifier &r_type) {
  710. const GDParser::FunctionNode* func=NULL;
  711. for(int i=0;i<context._class->functions.size();i++) {
  712. if (context._class->functions[i]->name==p_function) {
  713. func=context._class->functions[i];
  714. break;
  715. }
  716. }
  717. if (!func)
  718. return false;
  719. for(int i=0;i<func->body->statements.size();i++) {
  720. if (func->body->statements[i]->type==GDParser::BlockNode::TYPE_OPERATOR) {
  721. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->body->statements[i]);
  722. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  723. if (op->arguments.size() && op->arguments[0]->type==GDParser::Node::TYPE_IDENTIFIER) {
  724. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  725. if (id->name==p_identifier) {
  726. return _guess_expression_type(context,op->arguments[1],func->body->statements[i]->line,r_type);
  727. }
  728. }
  729. }
  730. }
  731. }
  732. return false;
  733. }
  734. static bool _guess_identifier_type(GDCompletionContext& context,int p_line,const StringName& p_identifier,GDCompletionIdentifier &r_type) {
  735. //go to block first
  736. const GDParser::BlockNode *block=context.block;
  737. while(block) {
  738. GDCompletionContext c = context;
  739. c.block=block;
  740. if (_guess_identifier_type_in_block(c,p_line,p_identifier,r_type)) {
  741. return true;
  742. }
  743. block=block->parent_block;
  744. }
  745. //guess from argument if virtual
  746. if (context.function && context.function->name!=StringName()) {
  747. int argindex = -1;
  748. for(int i=0;i<context.function->arguments.size();i++) {
  749. if (context.function->arguments[i]==p_identifier) {
  750. argindex=i;
  751. break;
  752. }
  753. }
  754. if (argindex!=-1) {
  755. GDCompletionIdentifier id =_get_native_class(context);
  756. if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  757. //this kinda sucks but meh
  758. List<MethodInfo> vmethods;
  759. ObjectTypeDB::get_virtual_methods(id.obj_type,&vmethods);
  760. for (List<MethodInfo>::Element *E=vmethods.front();E;E=E->next()) {
  761. if (E->get().name==context.function->name && argindex<E->get().arguments.size()) {
  762. PropertyInfo arg=E->get().arguments[argindex];
  763. int scp = arg.name.find(":");
  764. if (scp!=-1) {
  765. r_type.type=Variant::OBJECT;
  766. r_type.obj_type=arg.name.substr(scp+1,arg.name.length());
  767. return true;
  768. } else {
  769. r_type.type=arg.type;
  770. if (arg.hint==PROPERTY_HINT_RESOURCE_TYPE)
  771. r_type.obj_type=arg.hint_string;
  772. return true;
  773. }
  774. }
  775. }
  776. }
  777. }
  778. }
  779. //guess type in constant
  780. for(int i=0;i<context._class->constant_expressions.size();i++) {
  781. if (context._class->constant_expressions[i].identifier==p_identifier) {
  782. ERR_FAIL_COND_V( context._class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT, false );
  783. r_type=_get_type_from_variant(static_cast<const GDParser::ConstantNode*>(context._class->constant_expressions[i].expression)->value );
  784. return true;
  785. }
  786. }
  787. if (!(context.function && context.function->_static)) {
  788. for(int i=0;i<context._class->variables.size();i++) {
  789. if (context._class->variables[i].identifier==p_identifier) {
  790. if (context._class->variables[i]._export.type!=Variant::NIL) {
  791. r_type=_get_type_from_pinfo(context._class->variables[i]._export);
  792. return true;
  793. } else if (context._class->variables[i].expression) {
  794. bool rtype = _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  795. if (rtype && r_type.type!=Variant::NIL)
  796. return true;
  797. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  798. }
  799. //try to guess from assignment in construtor or _ready
  800. if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_ready",r_type))
  801. return true;
  802. if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_enter_tree",r_type))
  803. return true;
  804. if (_guess_identifier_from_assignment_in_function(context,p_identifier,"_init",r_type))
  805. return true;
  806. return false;
  807. }
  808. }
  809. }
  810. //autoloads as singletons
  811. List<PropertyInfo> props;
  812. Globals::get_singleton()->get_property_list(&props);
  813. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  814. String s = E->get().name;
  815. if (!s.begins_with("autoload/"))
  816. continue;
  817. String name = s.get_slice("/",1);
  818. if (name==String(p_identifier)) {
  819. String path = Globals::get_singleton()->get(s);
  820. if (path.begins_with("*")) {
  821. String script =path.substr(1,path.length());
  822. if (!script.ends_with(".gd")) {
  823. //not a script, try find the script anyway,
  824. //may have some success
  825. script=script.basename()+".gd";
  826. }
  827. if (FileAccess::exists(script)) {
  828. //print_line("is a script");
  829. Ref<Script> scr;
  830. if (ScriptCodeCompletionCache::get_sigleton())
  831. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  832. else
  833. scr = ResourceLoader::load(script);
  834. r_type.obj_type="Node";
  835. r_type.type=Variant::OBJECT;
  836. r_type.script=scr;
  837. r_type.value=Variant();
  838. return true;
  839. }
  840. }
  841. }
  842. }
  843. //global
  844. for(Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  845. if (E->key()==p_identifier) {
  846. r_type=_get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  847. return true;
  848. }
  849. }
  850. return false;
  851. }
  852. static void _find_identifiers_in_block(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  853. if (p_only_functions)
  854. return;
  855. for (int i=0;i<context.block->statements.size();i++) {
  856. if (context.block->statements[i]->line>p_line)
  857. continue;
  858. if (context.block->statements[i]->type==GDParser::BlockNode::TYPE_LOCAL_VAR) {
  859. const GDParser::LocalVarNode *lv=static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  860. result.insert(lv->name.operator String());
  861. }
  862. }
  863. }
  864. static void _find_identifiers_in_class(GDCompletionContext& context,bool p_static,bool p_only_functions,Set<String>& result) {
  865. if (!p_static && !p_only_functions) {
  866. for(int i=0;i<context._class->variables.size();i++) {
  867. result.insert(context._class->variables[i].identifier);
  868. }
  869. }
  870. if (!p_only_functions) {
  871. for(int i=0;i<context._class->constant_expressions.size();i++) {
  872. result.insert(context._class->constant_expressions[i].identifier);
  873. }
  874. for(int i=0;i<context._class->subclasses.size();i++) {
  875. result.insert(context._class->subclasses[i]->name);
  876. }
  877. }
  878. for(int i=0;i<context._class->static_functions.size();i++) {
  879. if (context._class->static_functions[i]->arguments.size())
  880. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  881. else
  882. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  883. }
  884. if (!p_static) {
  885. for(int i=0;i<context._class->functions.size();i++) {
  886. if (context._class->functions[i]->arguments.size())
  887. result.insert(context._class->functions[i]->name.operator String()+"(");
  888. else
  889. result.insert(context._class->functions[i]->name.operator String()+"()");
  890. }
  891. }
  892. //globals
  893. Ref<Reference> base = _get_parent_class(context);
  894. while(true) {
  895. Ref<GDScript> script = base;
  896. Ref<GDNativeClass> nc = base;
  897. if (script.is_valid()) {
  898. if (!p_static && !p_only_functions) {
  899. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  900. result.insert(E->get().operator String());
  901. }
  902. }
  903. if (!p_only_functions) {
  904. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  905. result.insert(E->key().operator String());
  906. }
  907. }
  908. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  909. if (!p_static || E->get().is_static()) {
  910. if (E->get().get_argument_count())
  911. result.insert(E->key().operator String()+"(");
  912. else
  913. result.insert(E->key().operator String()+"()");
  914. }
  915. }
  916. if (!p_only_functions) {
  917. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  918. result.insert(E->key().operator String());
  919. }
  920. }
  921. base=script->get_base();
  922. if (base.is_null())
  923. base=script->get_native();
  924. } else if (nc.is_valid()) {
  925. if (!p_only_functions) {
  926. StringName type = nc->get_name();
  927. List<String> constants;
  928. ObjectTypeDB::get_integer_constant_list(type,&constants);
  929. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  930. result.insert(E->get());
  931. }
  932. List<MethodInfo> methods;
  933. ObjectTypeDB::get_method_list(type,&methods);
  934. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  935. if (E->get().name.begins_with("_"))
  936. continue;
  937. if (E->get().arguments.size())
  938. result.insert(E->get().name+"(");
  939. else
  940. result.insert(E->get().name+"()");
  941. }
  942. }
  943. break;
  944. } else
  945. break;
  946. }
  947. }
  948. static void _find_identifiers(GDCompletionContext& context,int p_line,bool p_only_functions,Set<String>& result) {
  949. const GDParser::BlockNode *block=context.block;
  950. if (context.function) {
  951. const GDParser::FunctionNode* f = context.function;
  952. for (int i=0;i<f->arguments.size();i++) {
  953. result.insert(f->arguments[i].operator String());
  954. }
  955. }
  956. while(block) {
  957. GDCompletionContext c = context;
  958. c.block=block;
  959. _find_identifiers_in_block(c,p_line,p_only_functions,result);
  960. block=block->parent_block;
  961. }
  962. const GDParser::ClassNode *clss=context._class;
  963. bool _static=context.function && context.function->_static;
  964. while(clss) {
  965. GDCompletionContext c = context;
  966. c._class=clss;
  967. c.block=NULL;
  968. c.function=NULL;
  969. _find_identifiers_in_class(c,_static,p_only_functions,result);
  970. clss=clss->owner;
  971. }
  972. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  973. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  974. }
  975. static const char*_type_names[Variant::VARIANT_MAX]={
  976. "null","bool","int","float","String","Vector2","Rect2","Vector3","Matrix32","Plane","Quat","AABB","Matrix3","Transform",
  977. "Color","Image","NodePath","RID","Object","InputEvent","Dictionary","Array","RawArray","IntArray","FloatArray","StringArray",
  978. "Vector2Array","Vector3Array","ColorArray"};
  979. for(int i=0;i<Variant::VARIANT_MAX;i++) {
  980. result.insert(_type_names[i]);
  981. }
  982. //autoload singletons
  983. List<PropertyInfo> props;
  984. Globals::get_singleton()->get_property_list(&props);
  985. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  986. String s = E->get().name;
  987. if (!s.begins_with("autoload/"))
  988. continue;
  989. String name = s.get_slice("/",1);
  990. String path = Globals::get_singleton()->get(s);
  991. if (path.begins_with("*")) {
  992. result.insert(name);
  993. }
  994. }
  995. for(const Map<StringName,int>::Element *E=GDScriptLanguage::get_singleton()->get_global_map().front();E;E=E->next()) {
  996. result.insert(E->key().operator String());
  997. }
  998. }
  999. static String _get_visual_datatype(const PropertyInfo& p_info,bool p_isarg=true) {
  1000. String n = p_info.name;
  1001. int idx = n.find(":");
  1002. if (idx!=-1) {
  1003. return n.substr(idx+1,n.length());
  1004. }
  1005. if (p_info.type==Variant::OBJECT && p_info.hint==PROPERTY_HINT_RESOURCE_TYPE)
  1006. return p_info.hint_string;
  1007. if (p_info.type==Variant::NIL) {
  1008. if (p_isarg)
  1009. return "var";
  1010. else
  1011. return "void";
  1012. }
  1013. return Variant::get_type_name(p_info.type);
  1014. }
  1015. static void _make_function_hint(const GDParser::FunctionNode* p_func,int p_argidx,String& arghint) {
  1016. arghint="func "+p_func->name+"(";
  1017. for (int i=0;i<p_func->arguments.size();i++) {
  1018. if (i>0)
  1019. arghint+=", ";
  1020. else
  1021. arghint+=" ";
  1022. if (i==p_argidx) {
  1023. arghint+=String::chr(0xFFFF);
  1024. }
  1025. arghint+=p_func->arguments[i].operator String();
  1026. int deffrom = p_func->arguments.size()-p_func->default_values.size();
  1027. if (i>=deffrom) {
  1028. int defidx = deffrom-i;
  1029. if (defidx>=0 && defidx<p_func->default_values.size()) {
  1030. if (p_func->default_values[defidx]->type==GDParser::Node::TYPE_OPERATOR) {
  1031. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  1032. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  1033. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1034. arghint+="="+cn->value.get_construct_string();
  1035. }
  1036. } else {
  1037. }
  1038. }
  1039. }
  1040. if (i==p_argidx) {
  1041. arghint+=String::chr(0xFFFF);
  1042. }
  1043. }
  1044. if (p_func->arguments.size()>0)
  1045. arghint+=" ";
  1046. arghint+=")";
  1047. }
  1048. 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) {
  1049. //print_line("find type arguments?");
  1050. if (id.type==Variant::INPUT_EVENT && String(p_method)=="is_action" && p_argidx==0) {
  1051. List<PropertyInfo> pinfo;
  1052. Globals::get_singleton()->get_property_list(&pinfo);
  1053. for(List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  1054. const PropertyInfo &pi=E->get();
  1055. if (!pi.name.begins_with("input/"))
  1056. continue;
  1057. String name = pi.name.substr(pi.name.find("/")+1,pi.name.length());
  1058. result.insert("\""+name+"\"");
  1059. }
  1060. } else if (id.type==Variant::OBJECT && id.obj_type!=StringName()) {
  1061. MethodBind *m = ObjectTypeDB::get_method(id.obj_type,p_method);
  1062. if (!m) {
  1063. //not in static method, see script
  1064. //print_line("not in static: "+String(p_method));
  1065. Ref<GDScript> on_script;
  1066. if (id.value.get_type()) {
  1067. Object *obj=id.value;
  1068. if (obj) {
  1069. GDScript *scr = obj->cast_to<GDScript>();
  1070. if (scr) {
  1071. while (scr) {
  1072. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1073. if (E->get().is_static() && p_method==E->get().get_name()) {
  1074. arghint="static func "+String(p_method)+"(";
  1075. for(int i=0;i<E->get().get_argument_count();i++) {
  1076. if (i>0)
  1077. arghint+=", ";
  1078. else
  1079. arghint+=" ";
  1080. if (i==p_argidx) {
  1081. arghint+=String::chr(0xFFFF);
  1082. }
  1083. arghint+="var "+E->get().get_argument_name(i);
  1084. int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
  1085. if (i>=deffrom) {
  1086. int defidx = deffrom-i;
  1087. if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
  1088. arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
  1089. }
  1090. }
  1091. if (i==p_argidx) {
  1092. arghint+=String::chr(0xFFFF);
  1093. }
  1094. }
  1095. arghint+=")";
  1096. return; //found
  1097. }
  1098. }
  1099. if (scr->get_base().is_valid())
  1100. scr=scr->get_base().ptr();
  1101. else
  1102. scr=NULL;
  1103. }
  1104. } else {
  1105. on_script=obj->get_script();
  1106. }
  1107. }
  1108. }
  1109. //print_line("but it has a script?");
  1110. if (!on_script.is_valid() && id.script.is_valid()) {
  1111. //print_line("yes");
  1112. on_script=id.script;
  1113. }
  1114. if (on_script.is_valid()) {
  1115. GDScript *scr = on_script.ptr();
  1116. if (scr) {
  1117. while (scr) {
  1118. String code = scr->get_source_code();
  1119. //print_line("has source code!");
  1120. if (code!="") {
  1121. //if there is code, parse it. This way is slower but updates in real-time
  1122. GDParser p;
  1123. //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);
  1124. Error err = p.parse(scr->get_source_code(),scr->get_path().get_base_dir(),true,"",false);
  1125. if (err==OK) {
  1126. //print_line("checking the functions...");
  1127. //only if ok, otherwise use what is cached on the script
  1128. //GDParser::ClassNode *base = p.
  1129. const GDParser::Node *root = p.get_parse_tree();
  1130. ERR_FAIL_COND(root->type!=GDParser::Node::TYPE_CLASS);
  1131. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  1132. const GDParser::FunctionNode* func=NULL;
  1133. bool st=false;
  1134. for(int i=0;i<cl->functions.size();i++) {
  1135. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1136. if (cl->functions[i]->name==p_method) {
  1137. func=cl->functions[i];
  1138. }
  1139. }
  1140. for(int i=0;i<cl->static_functions.size();i++) {
  1141. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1142. if (cl->static_functions[i]->name==p_method) {
  1143. func=cl->static_functions[i];
  1144. st=true;
  1145. }
  1146. }
  1147. if (func) {
  1148. arghint="func "+String(p_method)+"(";
  1149. if (st)
  1150. arghint="static "+arghint;
  1151. for(int i=0;i<func->arguments.size();i++) {
  1152. if (i>0)
  1153. arghint+=", ";
  1154. else
  1155. arghint+=" ";
  1156. if (i==p_argidx) {
  1157. arghint+=String::chr(0xFFFF);
  1158. }
  1159. arghint+="var "+String(func->arguments[i]);
  1160. int deffrom = func->arguments.size()-func->default_values.size();
  1161. if (i>=deffrom) {
  1162. int defidx = deffrom-i;
  1163. if (defidx>=0 && defidx<func->default_values.size() && func->default_values[defidx]->type==GDParser::Node::TYPE_OPERATOR) {
  1164. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(func->default_values[defidx]);
  1165. if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
  1166. const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1167. arghint+="="+cn->value.get_construct_string();
  1168. }
  1169. }
  1170. }
  1171. if (i==p_argidx) {
  1172. arghint+=String::chr(0xFFFF);
  1173. }
  1174. }
  1175. arghint+=" )";
  1176. return;
  1177. }
  1178. } else {
  1179. //print_line("failed parsing?");
  1180. code="";
  1181. }
  1182. }
  1183. if (code=="") {
  1184. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1185. if (p_method==E->get().get_name()) {
  1186. arghint="func "+String(p_method)+"(";
  1187. for(int i=0;i<E->get().get_argument_count();i++) {
  1188. if (i>0)
  1189. arghint+=", ";
  1190. else
  1191. arghint+=" ";
  1192. if (i==p_argidx) {
  1193. arghint+=String::chr(0xFFFF);
  1194. }
  1195. arghint+="var "+E->get().get_argument_name(i);
  1196. int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
  1197. if (i>=deffrom) {
  1198. int defidx = deffrom-i;
  1199. if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
  1200. arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
  1201. }
  1202. }
  1203. if (i==p_argidx) {
  1204. arghint+=String::chr(0xFFFF);
  1205. }
  1206. }
  1207. arghint+=")";
  1208. return; //found
  1209. }
  1210. }
  1211. #if 0
  1212. //use class directly, no code was found
  1213. if (!isfunction) {
  1214. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1215. options.insert(E->key());
  1216. }
  1217. }
  1218. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1219. options.insert(String(E->key())+"(");
  1220. }
  1221. for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
  1222. options.insert(E->get());
  1223. }
  1224. #endif
  1225. }
  1226. if (scr->get_base().is_valid())
  1227. scr=scr->get_base().ptr();
  1228. else
  1229. scr=NULL;
  1230. }
  1231. }
  1232. }
  1233. } else {
  1234. //regular method
  1235. if (p_method.operator String()=="connect") {
  1236. if (p_argidx==0) {
  1237. List<MethodInfo> sigs;
  1238. ObjectTypeDB::get_signal_list(id.obj_type,&sigs);
  1239. for (List<MethodInfo>::Element *E=sigs.front();E;E=E->next()) {
  1240. result.insert("\""+E->get().name+"\"");
  1241. }
  1242. }
  1243. /*if (p_argidx==2) {
  1244. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  1245. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  1246. if (op->arguments.size()>)
  1247. }*/
  1248. } else {
  1249. if (p_argidx==0 && (String(p_method)=="get_node" || String(p_method)=="has_node") && ObjectTypeDB::is_type(id.obj_type,"Node")) {
  1250. List<PropertyInfo> props;
  1251. Globals::get_singleton()->get_property_list(&props);
  1252. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  1253. String s = E->get().name;
  1254. if (!s.begins_with("autoload/"))
  1255. continue;
  1256. // print_line("found "+s);
  1257. String name = s.get_slice("/",1);
  1258. result.insert("\"/root/"+name+"\"");
  1259. }
  1260. }
  1261. Object *obj=id.value;
  1262. if (obj) {
  1263. List<String> options;
  1264. obj->get_argument_options(p_method,p_argidx,&options);
  1265. for(List<String>::Element *E=options.front();E;E=E->next()) {
  1266. result.insert(E->get());
  1267. }
  1268. }
  1269. }
  1270. arghint = _get_visual_datatype(m->get_argument_info(-1),false)+" "+p_method.operator String()+String("(");
  1271. for(int i=0;i<m->get_argument_count();i++) {
  1272. if (i>0)
  1273. arghint+=", ";
  1274. else
  1275. arghint+=" ";
  1276. if (i==p_argidx) {
  1277. arghint+=String::chr(0xFFFF);
  1278. }
  1279. String n = m->get_argument_info(i).name;
  1280. int dp = n.find(":");
  1281. if (dp!=-1)
  1282. n=n.substr(0,dp);
  1283. arghint+=_get_visual_datatype(m->get_argument_info(i))+" "+n;
  1284. int deffrom = m->get_argument_count()-m->get_default_argument_count();
  1285. if (i>=deffrom) {
  1286. int defidx = i-deffrom;
  1287. if (defidx>=0 && defidx<m->get_default_argument_count()) {
  1288. Variant v= m->get_default_argument(i);
  1289. arghint+="="+v.get_construct_string();
  1290. }
  1291. }
  1292. if (i==p_argidx) {
  1293. arghint+=String::chr(0xFFFF);
  1294. }
  1295. }
  1296. if (m->get_argument_count()>0)
  1297. arghint+=" ";
  1298. arghint+=")";
  1299. }
  1300. }
  1301. }
  1302. static void _find_call_arguments(GDCompletionContext& context,const GDParser::Node* p_node, int p_line,int p_argidx, Set<String>& result, String& arghint) {
  1303. if (!p_node || p_node->type!=GDParser::Node::TYPE_OPERATOR) {
  1304. return;
  1305. }
  1306. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  1307. if (op->op!=GDParser::OperatorNode::OP_CALL) {
  1308. return;
  1309. }
  1310. if (op->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1311. //complete built-in function
  1312. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode*>(op->arguments[0]);
  1313. MethodInfo mi = GDFunctions::get_info(fn->function);
  1314. arghint = _get_visual_datatype(mi.return_val,false)+" "+GDFunctions::get_func_name(fn->function)+String("(");
  1315. for(int i=0;i<mi.arguments.size();i++) {
  1316. if (i>0)
  1317. arghint+=", ";
  1318. else
  1319. arghint+=" ";
  1320. if (i==p_argidx) {
  1321. arghint+=String::chr(0xFFFF);
  1322. }
  1323. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  1324. if (i==p_argidx) {
  1325. arghint+=String::chr(0xFFFF);
  1326. }
  1327. }
  1328. if (mi.arguments.size()>0)
  1329. arghint+=" ";
  1330. arghint+=")";
  1331. } else if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  1332. //complete constructor
  1333. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode*>(op->arguments[0]);
  1334. List<MethodInfo> mil;
  1335. Variant::get_constructor_list(tn->vtype,&mil);
  1336. for(List<MethodInfo>::Element *E=mil.front();E;E=E->next()) {
  1337. MethodInfo mi = E->get();
  1338. if (mi.arguments.size()==0)
  1339. continue;
  1340. if (E->prev())
  1341. arghint+="\n";
  1342. arghint += Variant::get_type_name(tn->vtype)+" "+Variant::get_type_name(tn->vtype)+String("(");
  1343. for(int i=0;i<mi.arguments.size();i++) {
  1344. if (i>0)
  1345. arghint+=", ";
  1346. else
  1347. arghint+=" ";
  1348. if (i==p_argidx) {
  1349. arghint+=String::chr(0xFFFF);
  1350. }
  1351. arghint+=_get_visual_datatype(mi.arguments[i])+" "+mi.arguments[i].name;
  1352. if (i==p_argidx) {
  1353. arghint+=String::chr(0xFFFF);
  1354. }
  1355. }
  1356. if (mi.arguments.size()>0)
  1357. arghint+=" ";
  1358. arghint+=")";
  1359. }
  1360. } else if (op->arguments.size()>=2 && op->arguments[1]->type==GDParser::Node::TYPE_IDENTIFIER) {
  1361. //make sure identifier exists...
  1362. const GDParser::IdentifierNode *id=static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1363. if (op->arguments[0]->type==GDParser::Node::TYPE_SELF) {
  1364. //self, look up
  1365. for(int i=0;i<context._class->static_functions.size();i++) {
  1366. if (context._class->static_functions[i]->name==id->name) {
  1367. _make_function_hint(context._class->static_functions[i],p_argidx,arghint);
  1368. return;
  1369. }
  1370. }
  1371. if (context.function && !context.function->_static) {
  1372. for(int i=0;i<context._class->functions.size();i++) {
  1373. if (context._class->functions[i]->name==id->name) {
  1374. _make_function_hint(context._class->functions[i],p_argidx,arghint);
  1375. return;
  1376. }
  1377. }
  1378. }
  1379. Ref<Reference> base = _get_parent_class(context);
  1380. while(true) {
  1381. Ref<GDScript> script = base;
  1382. Ref<GDNativeClass> nc = base;
  1383. if (script.is_valid()) {
  1384. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1385. if (E->key()==id->name) {
  1386. if (context.function && context.function->_static && !E->get().is_static())
  1387. continue;
  1388. arghint = "func "+id->name.operator String()+String("(");
  1389. for(int i=0;i<E->get().get_argument_count();i++) {
  1390. if (i>0)
  1391. arghint+=", ";
  1392. else
  1393. arghint+=" ";
  1394. if (i==p_argidx) {
  1395. arghint+=String::chr(0xFFFF);
  1396. }
  1397. arghint+=E->get().get_argument_name(i);
  1398. int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
  1399. if (i>=deffrom) {
  1400. int defidx = deffrom-i;
  1401. if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
  1402. arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
  1403. }
  1404. }
  1405. if (i==p_argidx) {
  1406. arghint+=String::chr(0xFFFF);
  1407. }
  1408. }
  1409. if (E->get().get_argument_count()>0)
  1410. arghint+=" ";
  1411. arghint+=")";
  1412. return;
  1413. }
  1414. }
  1415. base=script->get_base();
  1416. if (base.is_null())
  1417. base=script->get_native();
  1418. } else if (nc.is_valid()) {
  1419. if (context.function && !context.function->_static) {
  1420. GDCompletionIdentifier ci;
  1421. ci.type=Variant::OBJECT;
  1422. ci.obj_type=nc->get_name();
  1423. if (!context._class->owner)
  1424. ci.value=context.base;
  1425. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1426. //guess type..
  1427. /*
  1428. List<MethodInfo> methods;
  1429. ObjectTypeDB::get_method_list(type,&methods);
  1430. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1431. //if (E->get().arguments.size())
  1432. // result.insert(E->get().name+"(");
  1433. //else
  1434. // result.insert(E->get().name+"()");
  1435. }*/
  1436. }
  1437. break;
  1438. } else
  1439. break;
  1440. }
  1441. } else {
  1442. //indexed lookup
  1443. GDCompletionIdentifier ci;
  1444. if (_guess_expression_type(context,op->arguments[0],p_line,ci)) {
  1445. _find_type_arguments(p_node,p_line,id->name,ci,p_argidx,result,arghint);
  1446. return;
  1447. }
  1448. }
  1449. }
  1450. #if 0
  1451. bool _static=context.function->_static;
  1452. for(int i=0;i<context._class->static_functions.size();i++) {
  1453. if (context._class->static_functions[i]->arguments.size())
  1454. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1455. else
  1456. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1457. }
  1458. if (!p_static) {
  1459. for(int i=0;i<context._class->functions.size();i++) {
  1460. if (context._class->functions[i]->arguments.size())
  1461. result.insert(context._class->functions[i]->name.operator String()+"(");
  1462. else
  1463. result.insert(context._class->functions[i]->name.operator String()+"()");
  1464. }
  1465. }
  1466. Ref<Reference> base = _get_parent_class(context);
  1467. while(true) {
  1468. Ref<GDScript> script = base;
  1469. Ref<GDNativeClass> nc = base;
  1470. if (script.is_valid()) {
  1471. if (!p_static && !p_only_functions) {
  1472. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1473. result.insert(E->get().operator String());
  1474. }
  1475. }
  1476. if (!p_only_functions) {
  1477. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1478. result.insert(E->key().operator String());
  1479. }
  1480. }
  1481. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1482. if (!p_static || E->get().is_static()) {
  1483. if (E->get().get_argument_count())
  1484. result.insert(E->key().operator String()+"(");
  1485. else
  1486. result.insert(E->key().operator String()+"()");
  1487. }
  1488. }
  1489. if (!p_only_functions) {
  1490. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1491. result.insert(E->key().operator String());
  1492. }
  1493. }
  1494. base=script->get_base();
  1495. if (base.is_null())
  1496. base=script->get_native();
  1497. } else if (nc.is_valid()) {
  1498. if (!p_only_functions) {
  1499. StringName type = nc->get_name();
  1500. List<String> constants;
  1501. ObjectTypeDB::get_integer_constant_list(type,&constants);
  1502. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1503. result.insert(E->get());
  1504. }
  1505. List<MethodInfo> methods;
  1506. ObjectTypeDB::get_method_list(type,&methods);
  1507. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1508. if (E->get().arguments.size())
  1509. result.insert(E->get().name+"(");
  1510. else
  1511. result.insert(E->get().name+"()");
  1512. }
  1513. }
  1514. break;
  1515. } else
  1516. break;
  1517. }
  1518. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1519. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1520. }
  1521. #endif
  1522. }
  1523. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1524. //print_line( p_code.replace(String::chr(0xFFFF),"<cursor>"));
  1525. GDParser p;
  1526. //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);
  1527. Error err = p.parse(p_code,p_base_path,false,"",true);
  1528. bool isfunction=false;
  1529. Set<String> options;
  1530. GDCompletionContext context;
  1531. context._class=p.get_completion_class();
  1532. context.block=p.get_completion_block();
  1533. context.function=p.get_completion_function();
  1534. context.base=p_owner;
  1535. context.base_path=p_base_path;
  1536. switch(p.get_completion_type()) {
  1537. case GDParser::COMPLETION_NONE: {
  1538. print_line("No completion");
  1539. } break;
  1540. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1541. print_line("Built in type constant");
  1542. List<StringName> constants;
  1543. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(),&constants);
  1544. for(List<StringName>::Element *E=constants.front();E;E=E->next()) {
  1545. options.insert(E->get().operator String());
  1546. }
  1547. } break;
  1548. case GDParser::COMPLETION_FUNCTION:
  1549. isfunction=true;
  1550. case GDParser::COMPLETION_IDENTIFIER: {
  1551. _find_identifiers(context,p.get_completion_line(),isfunction,options);
  1552. } break;
  1553. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1554. print_line("parent function");
  1555. } break;
  1556. case GDParser::COMPLETION_METHOD:
  1557. isfunction=true;
  1558. case GDParser::COMPLETION_INDEX: {
  1559. const GDParser::Node *node = p.get_completion_node();
  1560. if (node->type!=GDParser::Node::TYPE_OPERATOR)
  1561. break;
  1562. GDCompletionIdentifier t;
  1563. if (_guess_expression_type(context,static_cast<const GDParser::OperatorNode *>(node)->arguments[0],p.get_completion_line(),t)) {
  1564. if (t.type==Variant::OBJECT && t.obj_type!=StringName()) {
  1565. Ref<GDScript> on_script;
  1566. if (t.value.get_type()) {
  1567. Object *obj=t.value;
  1568. if (obj) {
  1569. GDScript *scr = obj->cast_to<GDScript>();
  1570. if (scr) {
  1571. while (scr) {
  1572. if (!isfunction) {
  1573. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1574. options.insert(E->key());
  1575. }
  1576. }
  1577. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1578. if (E->get().is_static())
  1579. options.insert(E->key());
  1580. }
  1581. if (scr->get_base().is_valid())
  1582. scr=scr->get_base().ptr();
  1583. else
  1584. scr=NULL;
  1585. }
  1586. } else {
  1587. on_script=obj->get_script();
  1588. }
  1589. }
  1590. }
  1591. if (!on_script.is_valid() && t.script.is_valid()) {
  1592. on_script=t.script;
  1593. }
  1594. if (on_script.is_valid()) {
  1595. GDScript *scr = on_script.ptr();
  1596. if (scr) {
  1597. while (scr) {
  1598. String code = scr->get_source_code();
  1599. if (code!="") {
  1600. //if there is code, parse it. This way is slower but updates in real-time
  1601. GDParser p;
  1602. //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);
  1603. Error err = p.parse(scr->get_source_code(),scr->get_path().get_base_dir(),true,"",false);
  1604. if (err==OK) {
  1605. //only if ok, otherwise use what is cached on the script
  1606. //GDParser::ClassNode *base = p.
  1607. const GDParser::Node *root = p.get_parse_tree();
  1608. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_PARSE_ERROR);
  1609. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
  1610. for(int i=0;i<cl->functions.size();i++) {
  1611. if (cl->functions[i]->arguments.size())
  1612. options.insert(String(cl->functions[i]->name)+"(");
  1613. else
  1614. options.insert(String(cl->functions[i]->name)+"()");
  1615. }
  1616. for(int i=0;i<cl->static_functions.size();i++) {
  1617. if (cl->static_functions[i]->arguments.size())
  1618. options.insert(String(cl->static_functions[i]->name)+"(");
  1619. else
  1620. options.insert(String(cl->static_functions[i]->name)+"()");
  1621. }
  1622. if (!isfunction) {
  1623. for(int i=0;i<cl->variables.size();i++) {
  1624. options.insert(String(cl->variables[i].identifier));
  1625. }
  1626. for(int i=0;i<cl->constant_expressions.size();i++) {
  1627. options.insert(String(cl->constant_expressions[i].identifier));
  1628. }
  1629. }
  1630. } else {
  1631. code=""; //well, then no code
  1632. }
  1633. }
  1634. if (code=="") {
  1635. //use class directly, no code was found
  1636. if (!isfunction) {
  1637. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1638. options.insert(E->key());
  1639. }
  1640. }
  1641. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1642. if (E->get().get_argument_count())
  1643. options.insert(String(E->key())+"()");
  1644. else
  1645. options.insert(String(E->key())+"(");
  1646. }
  1647. for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
  1648. options.insert(E->get());
  1649. }
  1650. }
  1651. if (scr->get_base().is_valid())
  1652. scr=scr->get_base().ptr();
  1653. else
  1654. scr=NULL;
  1655. }
  1656. }
  1657. }
  1658. if (!isfunction) {
  1659. ObjectTypeDB::get_integer_constant_list(t.obj_type,r_options);
  1660. }
  1661. List<MethodInfo> mi;
  1662. ObjectTypeDB::get_method_list(t.obj_type,&mi);
  1663. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1664. if (E->get().name.begins_with("_"))
  1665. continue;
  1666. if (E->get().arguments.size())
  1667. options.insert(E->get().name+"(");
  1668. else
  1669. options.insert(E->get().name+"()");
  1670. }
  1671. } else {
  1672. if (t.type==Variant::INPUT_EVENT) {
  1673. //this is hardcoded otherwise it's not obvious
  1674. Set<String> exclude;
  1675. for(int i=0;i<InputEvent::TYPE_MAX;i++) {
  1676. InputEvent ie;
  1677. ie.type=InputEvent::Type(i);
  1678. static const char*evnames[]={
  1679. "# Common",
  1680. "# Key",
  1681. "# MouseMotion",
  1682. "# MouseButton",
  1683. "# JoyMotion",
  1684. "# JoyButton",
  1685. "# ScreenTouch",
  1686. "# ScreenDrag",
  1687. "# Action"
  1688. };
  1689. r_options->push_back(evnames[i]);
  1690. Variant v = ie;
  1691. if (i==0) {
  1692. List<MethodInfo> mi;
  1693. v.get_method_list(&mi);
  1694. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1695. r_options->push_back(E->get().name+"(");
  1696. }
  1697. }
  1698. List<PropertyInfo> pi;
  1699. v.get_property_list(&pi);
  1700. for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  1701. if (i==0)
  1702. exclude.insert(E->get().name);
  1703. else if (exclude.has(E->get().name))
  1704. continue;
  1705. r_options->push_back(E->get().name);
  1706. }
  1707. }
  1708. return OK;
  1709. } else {
  1710. if (t.value.get_type()==Variant::NIL) {
  1711. Variant::CallError ce;
  1712. t.value=Variant::construct(t.type,NULL,0,ce);
  1713. }
  1714. if (!isfunction) {
  1715. List<PropertyInfo> pl;
  1716. t.value.get_property_list(&pl);
  1717. for (List<PropertyInfo>::Element *E=pl.front();E;E=E->next()) {
  1718. if (E->get().name.find("/")==-1)
  1719. options.insert(E->get().name);
  1720. }
  1721. }
  1722. List<MethodInfo> mi;
  1723. t.value.get_method_list(&mi);
  1724. for (List<MethodInfo>::Element *E=mi.front();E;E=E->next()) {
  1725. if (E->get().arguments.size())
  1726. options.insert(E->get().name+"(");
  1727. else
  1728. options.insert(E->get().name+"()");
  1729. }
  1730. }
  1731. }
  1732. }
  1733. } break;
  1734. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1735. _find_call_arguments(context,p.get_completion_node(),p.get_completion_line(),p.get_completion_argument_index(),options,r_call_hint);
  1736. } break;
  1737. case GDParser::COMPLETION_VIRTUAL_FUNC: {
  1738. GDCompletionIdentifier cid = _get_native_class(context);
  1739. if (cid.obj_type!=StringName()) {
  1740. List<MethodInfo> vm;
  1741. ObjectTypeDB::get_virtual_methods(cid.obj_type,&vm);
  1742. for(List<MethodInfo>::Element *E=vm.front();E;E=E->next()) {
  1743. MethodInfo &mi=E->get();
  1744. String m = mi.name;
  1745. if (m.find(":")!=-1)
  1746. m=m.substr(0,m.find(":"));
  1747. m+="(";
  1748. if (mi.arguments.size()) {
  1749. for(int i=0;i<mi.arguments.size();i++) {
  1750. if (i>0)
  1751. m+=", ";
  1752. String n =mi.arguments[i].name;
  1753. if (n.find(":")!=-1)
  1754. n=n.substr(0,n.find(":"));
  1755. m+=n;
  1756. }
  1757. }
  1758. m+="):";
  1759. options.insert(m);
  1760. }
  1761. }
  1762. } break;
  1763. }
  1764. for(Set<String>::Element *E=options.front();E;E=E->next()) {
  1765. r_options->push_back(E->get());
  1766. }
  1767. return OK;
  1768. }
  1769. #else
  1770. Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base_path, Object*p_owner, List<String>* r_options, String &r_call_hint) {
  1771. return OK;
  1772. }
  1773. #endif
  1774. void GDScriptLanguage::auto_indent_code(String& p_code,int p_from_line,int p_to_line) const {
  1775. Vector<String> lines = p_code.split("\n");
  1776. List<int> indent_stack;
  1777. for(int i=0;i<lines.size();i++) {
  1778. String l = lines[i];
  1779. int tc=0;
  1780. for(int j=0;j<l.length();j++) {
  1781. if (l[j]==' ' || l[j]=='\t') {
  1782. tc++;
  1783. } else {
  1784. break;
  1785. }
  1786. }
  1787. String st = l.substr(tc,l.length()).strip_edges();
  1788. if (st=="" || st.begins_with("#"))
  1789. continue; //ignore!
  1790. int ilevel=0;
  1791. if (indent_stack.size()) {
  1792. ilevel=indent_stack.back()->get();
  1793. }
  1794. if (tc>ilevel) {
  1795. indent_stack.push_back(tc);
  1796. } else if (tc<ilevel) {
  1797. while(indent_stack.size() && indent_stack.back()->get()>tc) {
  1798. indent_stack.pop_back();
  1799. }
  1800. if (indent_stack.size() && indent_stack.back()->get()!=tc)
  1801. indent_stack.push_back(tc); //this is not right but gets the job done
  1802. }
  1803. if (i>=p_from_line) {
  1804. l="";
  1805. for(int j=0;j<indent_stack.size();j++)
  1806. l+="\t";
  1807. l+=st;
  1808. } else if (i>p_to_line) {
  1809. break;
  1810. }
  1811. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  1812. lines[i]=l;
  1813. }
  1814. p_code="";
  1815. for(int i=0;i<lines.size();i++) {
  1816. if (i>0)
  1817. p_code+="\n";
  1818. p_code+=lines[i];
  1819. }
  1820. }