gd_editor.cpp 66 KB

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