gd_editor.cpp 80 KB

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