gd_editor.cpp 86 KB

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