gdscript_editor.cpp 89 KB

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