gd_editor.cpp 87 KB

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