gd_editor.cpp 80 KB

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