gdscript_editor.cpp 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/global_constants.h"
  33. #include "core/os/file_access.h"
  34. #include "gdscript_compiler.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_file_system.h"
  37. #include "editor/editor_settings.h"
  38. #endif
  39. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  40. p_delimiters->push_back("#");
  41. }
  42. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  43. p_delimiters->push_back("\" \"");
  44. p_delimiters->push_back("' '");
  45. p_delimiters->push_back("\"\"\" \"\"\"");
  46. }
  47. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  48. String _template = "extends %BASE%\n"
  49. "\n"
  50. "# Declare member variables here. Examples:\n"
  51. "# var a%INT_TYPE% = 2\n"
  52. "# var b%STRING_TYPE% = \"text\"\n"
  53. "\n"
  54. "# Called when the node enters the scene tree for the first time.\n"
  55. "func _ready()%VOID_RETURN%:\n"
  56. "%TS%pass # Replace with function body.\n"
  57. "\n"
  58. "# Called every frame. 'delta' is the elapsed time since the previous frame.\n"
  59. "#func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:\n"
  60. "#%TS%pass\n";
  61. #ifdef TOOLS_ENABLED
  62. if (EDITOR_DEF("text_editor/completion/add_type_hints", false)) {
  63. _template = _template.replace("%INT_TYPE%", ": int");
  64. _template = _template.replace("%STRING_TYPE%", ": String");
  65. _template = _template.replace("%FLOAT_TYPE%", ": float");
  66. _template = _template.replace("%VOID_RETURN%", " -> void");
  67. } else {
  68. _template = _template.replace("%INT_TYPE%", "");
  69. _template = _template.replace("%STRING_TYPE%", "");
  70. _template = _template.replace("%FLOAT_TYPE%", "");
  71. _template = _template.replace("%VOID_RETURN%", "");
  72. }
  73. #else
  74. _template = _template.replace("%INT_TYPE%", "");
  75. _template = _template.replace("%STRING_TYPE%", "");
  76. _template = _template.replace("%FLOAT_TYPE%", "");
  77. _template = _template.replace("%VOID_RETURN%", "");
  78. #endif
  79. _template = _template.replace("%BASE%", p_base_class_name);
  80. _template = _template.replace("%TS%", _get_indentation());
  81. Ref<GDScript> script;
  82. script.instance();
  83. script->set_source_code(_template);
  84. return script;
  85. }
  86. bool GDScriptLanguage::is_using_templates() {
  87. return true;
  88. }
  89. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  90. String src = p_script->get_source_code();
  91. src = src.replace("%BASE%", p_base_class_name);
  92. src = src.replace("%TS%", _get_indentation());
  93. p_script->set_source_code(src);
  94. }
  95. 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, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  96. GDScriptParser parser;
  97. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path, false, r_safe_lines);
  98. #ifdef DEBUG_ENABLED
  99. if (r_warnings) {
  100. for (const List<GDScriptWarning>::Element *E = parser.get_warnings().front(); E; E = E->next()) {
  101. const GDScriptWarning &warn = E->get();
  102. ScriptLanguage::Warning w;
  103. w.line = warn.line;
  104. w.code = (int)warn.code;
  105. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  106. w.message = warn.get_message();
  107. r_warnings->push_back(w);
  108. }
  109. }
  110. #endif
  111. if (err) {
  112. r_line_error = parser.get_error_line();
  113. r_col_error = parser.get_error_column();
  114. r_test_error = parser.get_error();
  115. return false;
  116. } else {
  117. const GDScriptParser::Node *root = parser.get_parse_tree();
  118. ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, false);
  119. const GDScriptParser::ClassNode *cl = static_cast<const GDScriptParser::ClassNode *>(root);
  120. Map<int, String> funcs;
  121. for (int i = 0; i < cl->functions.size(); i++) {
  122. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  123. }
  124. for (int i = 0; i < cl->static_functions.size(); i++) {
  125. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  126. }
  127. for (int i = 0; i < cl->subclasses.size(); i++) {
  128. for (int j = 0; j < cl->subclasses[i]->functions.size(); j++) {
  129. funcs[cl->subclasses[i]->functions[j]->line] = String(cl->subclasses[i]->name) + "." + String(cl->subclasses[i]->functions[j]->name);
  130. }
  131. }
  132. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  133. r_functions->push_back(E->get() + ":" + itos(E->key()));
  134. }
  135. }
  136. return true;
  137. }
  138. bool GDScriptLanguage::has_named_classes() const {
  139. return false;
  140. }
  141. bool GDScriptLanguage::supports_builtin_mode() const {
  142. return true;
  143. }
  144. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  145. GDScriptTokenizerText tokenizer;
  146. tokenizer.set_code(p_code);
  147. int indent = 0;
  148. while (tokenizer.get_token() != GDScriptTokenizer::TK_EOF && tokenizer.get_token() != GDScriptTokenizer::TK_ERROR) {
  149. if (tokenizer.get_token() == GDScriptTokenizer::TK_NEWLINE) {
  150. indent = tokenizer.get_token_line_indent();
  151. }
  152. if (indent == 0 && tokenizer.get_token() == GDScriptTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDScriptTokenizer::TK_IDENTIFIER) {
  153. String identifier = tokenizer.get_token_identifier(1);
  154. if (identifier == p_function) {
  155. return tokenizer.get_token_line();
  156. }
  157. }
  158. tokenizer.advance();
  159. }
  160. return -1;
  161. }
  162. Script *GDScriptLanguage::create_script() const {
  163. return memnew(GDScript);
  164. }
  165. /* DEBUGGER FUNCTIONS */
  166. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  167. //break because of parse error
  168. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  169. _debug_parse_err_line = p_line;
  170. _debug_parse_err_file = p_file;
  171. _debug_error = p_error;
  172. ScriptDebugger::get_singleton()->debug(this, false);
  173. return true;
  174. } else {
  175. return false;
  176. }
  177. }
  178. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  179. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  180. _debug_parse_err_line = -1;
  181. _debug_parse_err_file = "";
  182. _debug_error = p_error;
  183. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. String GDScriptLanguage::debug_get_error() const {
  190. return _debug_error;
  191. }
  192. int GDScriptLanguage::debug_get_stack_level_count() const {
  193. if (_debug_parse_err_line >= 0)
  194. return 1;
  195. return _debug_call_stack_pos;
  196. }
  197. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  198. if (_debug_parse_err_line >= 0)
  199. return _debug_parse_err_line;
  200. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  201. int l = _debug_call_stack_pos - p_level - 1;
  202. return *(_call_stack[l].line);
  203. }
  204. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  205. if (_debug_parse_err_line >= 0)
  206. return "";
  207. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  208. int l = _debug_call_stack_pos - p_level - 1;
  209. return _call_stack[l].function->get_name();
  210. }
  211. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  212. if (_debug_parse_err_line >= 0)
  213. return _debug_parse_err_file;
  214. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  215. int l = _debug_call_stack_pos - p_level - 1;
  216. return _call_stack[l].function->get_source();
  217. }
  218. 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) {
  219. if (_debug_parse_err_line >= 0)
  220. return;
  221. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  222. int l = _debug_call_stack_pos - p_level - 1;
  223. GDScriptFunction *f = _call_stack[l].function;
  224. List<Pair<StringName, int> > locals;
  225. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  226. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  227. p_locals->push_back(E->get().first);
  228. p_values->push_back(_call_stack[l].stack[E->get().second]);
  229. }
  230. }
  231. 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) {
  232. if (_debug_parse_err_line >= 0)
  233. return;
  234. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  235. int l = _debug_call_stack_pos - p_level - 1;
  236. GDScriptInstance *instance = _call_stack[l].instance;
  237. if (!instance)
  238. return;
  239. Ref<GDScript> script = instance->get_script();
  240. ERR_FAIL_COND(script.is_null());
  241. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  242. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  243. p_members->push_back(E->key());
  244. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  245. }
  246. }
  247. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  248. ERR_FAIL_COND_V(_debug_parse_err_line >= 0, NULL);
  249. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, NULL);
  250. int l = _debug_call_stack_pos - p_level - 1;
  251. ScriptInstance *instance = _call_stack[l].instance;
  252. return instance;
  253. }
  254. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  255. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  256. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  257. List<Pair<String, Variant> > cinfo;
  258. get_public_constants(&cinfo);
  259. for (const Map<StringName, int>::Element *E = name_idx.front(); E; E = E->next()) {
  260. if (ClassDB::class_exists(E->key()) || Engine::get_singleton()->has_singleton(E->key()))
  261. continue;
  262. bool is_script_constant = false;
  263. for (List<Pair<String, Variant> >::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  264. if (CE->get().first == E->key()) {
  265. is_script_constant = true;
  266. break;
  267. }
  268. }
  269. if (is_script_constant)
  270. continue;
  271. const Variant &var = globals[E->value()];
  272. if (Object *obj = var) {
  273. if (Object::cast_to<GDScriptNativeClass>(obj))
  274. continue;
  275. }
  276. bool skip = false;
  277. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  278. if (E->key() == GlobalConstants::get_global_constant_name(i)) {
  279. skip = true;
  280. break;
  281. }
  282. }
  283. if (skip)
  284. continue;
  285. p_globals->push_back(E->key());
  286. p_values->push_back(var);
  287. }
  288. }
  289. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  290. if (_debug_parse_err_line >= 0)
  291. return "";
  292. return "";
  293. }
  294. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  295. p_extensions->push_back("gd");
  296. }
  297. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  298. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  299. p_functions->push_back(GDScriptFunctions::get_info(GDScriptFunctions::Function(i)));
  300. }
  301. //not really "functions", but..
  302. {
  303. MethodInfo mi;
  304. mi.name = "preload";
  305. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  306. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  307. p_functions->push_back(mi);
  308. }
  309. {
  310. MethodInfo mi;
  311. mi.name = "yield";
  312. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  313. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  314. mi.default_arguments.push_back(Variant());
  315. mi.default_arguments.push_back(String());
  316. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "GDScriptFunctionState");
  317. p_functions->push_back(mi);
  318. }
  319. {
  320. MethodInfo mi;
  321. mi.name = "assert";
  322. mi.return_val.type = Variant::NIL;
  323. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  324. p_functions->push_back(mi);
  325. }
  326. }
  327. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  328. Pair<String, Variant> pi;
  329. pi.first = "PI";
  330. pi.second = Math_PI;
  331. p_constants->push_back(pi);
  332. Pair<String, Variant> tau;
  333. tau.first = "TAU";
  334. tau.second = Math_TAU;
  335. p_constants->push_back(tau);
  336. Pair<String, Variant> infinity;
  337. infinity.first = "INF";
  338. infinity.second = Math_INF;
  339. p_constants->push_back(infinity);
  340. Pair<String, Variant> nan;
  341. nan.first = "NAN";
  342. nan.second = Math_NAN;
  343. p_constants->push_back(nan);
  344. }
  345. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  346. #ifdef TOOLS_ENABLED
  347. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  348. #else
  349. bool th = false;
  350. #endif
  351. String s = "func " + p_name + "(";
  352. if (p_args.size()) {
  353. for (int i = 0; i < p_args.size(); i++) {
  354. if (i > 0)
  355. s += ", ";
  356. s += p_args[i].get_slice(":", 0);
  357. if (th) {
  358. String type = p_args[i].get_slice(":", 1);
  359. if (!type.empty() && type != "var") {
  360. s += ": " + type;
  361. }
  362. }
  363. }
  364. }
  365. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  366. return s;
  367. }
  368. //////// COMPLETION //////////
  369. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  370. struct GDScriptCompletionContext {
  371. const GDScriptParser::ClassNode *_class;
  372. const GDScriptParser::FunctionNode *function;
  373. const GDScriptParser::BlockNode *block;
  374. Object *base;
  375. String base_path;
  376. int line;
  377. GDScriptCompletionContext() :
  378. _class(NULL),
  379. function(NULL),
  380. block(NULL),
  381. base(NULL) {}
  382. };
  383. struct GDScriptCompletionIdentifier {
  384. GDScriptParser::DataType type;
  385. String enumeration;
  386. Variant value;
  387. const GDScriptParser::Node *assigned_expression;
  388. GDScriptCompletionIdentifier() :
  389. assigned_expression(NULL) {}
  390. };
  391. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
  392. for (int i = 0; i < p_dir->get_file_count(); i++) {
  393. r_list.insert("\"" + p_dir->get_file_path(i) + "\"");
  394. }
  395. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  396. _get_directory_contents(p_dir->get_subdir(i), r_list);
  397. }
  398. }
  399. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  400. if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  401. String enum_name = p_info.class_name;
  402. if (enum_name.find(".") == -1) {
  403. return enum_name;
  404. }
  405. return enum_name.get_slice(".", 1);
  406. }
  407. String n = p_info.name;
  408. int idx = n.find(":");
  409. if (idx != -1) {
  410. return n.substr(idx + 1, n.length());
  411. }
  412. if (p_info.type == Variant::OBJECT) {
  413. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  414. return p_info.hint_string;
  415. } else {
  416. return p_info.class_name.operator String();
  417. }
  418. }
  419. if (p_info.type == Variant::NIL) {
  420. if (p_isarg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  421. return "var";
  422. } else {
  423. return "void";
  424. }
  425. }
  426. return Variant::get_type_name(p_info.type);
  427. }
  428. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  429. GDScriptCompletionIdentifier ci;
  430. ci.value = p_value;
  431. ci.type.is_constant = true;
  432. ci.type.has_type = true;
  433. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  434. ci.type.builtin_type = p_value.get_type();
  435. if (ci.type.builtin_type == Variant::OBJECT) {
  436. Object *obj = p_value.operator Object *();
  437. if (!obj) {
  438. return ci;
  439. }
  440. ci.type.native_type = obj->get_class_name();
  441. Ref<Script> scr = p_value;
  442. if (scr.is_valid()) {
  443. ci.type.is_meta_type = true;
  444. } else {
  445. ci.type.is_meta_type = false;
  446. scr = obj->get_script();
  447. }
  448. if (scr.is_valid()) {
  449. ci.type.script_type = scr;
  450. Ref<GDScript> gds = scr;
  451. if (gds.is_valid()) {
  452. ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
  453. } else {
  454. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  455. }
  456. ci.type.native_type = scr->get_instance_base_type();
  457. } else {
  458. ci.type.kind = GDScriptParser::DataType::NATIVE;
  459. }
  460. }
  461. return ci;
  462. }
  463. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  464. GDScriptCompletionIdentifier ci;
  465. if (p_property.type == Variant::NIL) {
  466. // Variant
  467. return ci;
  468. }
  469. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  470. ci.enumeration = p_property.class_name;
  471. }
  472. ci.type.has_type = true;
  473. ci.type.builtin_type = p_property.type;
  474. if (p_property.type == Variant::OBJECT) {
  475. ci.type.kind = GDScriptParser::DataType::NATIVE;
  476. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  477. } else {
  478. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  479. }
  480. return ci;
  481. }
  482. static GDScriptCompletionIdentifier _type_from_gdtype(const GDScriptDataType &p_gdtype) {
  483. GDScriptCompletionIdentifier ci;
  484. if (!p_gdtype.has_type) {
  485. return ci;
  486. }
  487. ci.type.has_type = true;
  488. ci.type.builtin_type = p_gdtype.builtin_type;
  489. ci.type.native_type = p_gdtype.native_type;
  490. ci.type.script_type = p_gdtype.script_type;
  491. switch (p_gdtype.kind) {
  492. case GDScriptDataType::UNINITIALIZED: {
  493. ERR_EXPLAIN("Uninitialized completion. Please report a bug.");
  494. } break;
  495. case GDScriptDataType::BUILTIN: {
  496. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  497. } break;
  498. case GDScriptDataType::NATIVE: {
  499. ci.type.kind = GDScriptParser::DataType::NATIVE;
  500. } break;
  501. case GDScriptDataType::GDSCRIPT: {
  502. ci.type.kind = GDScriptParser::DataType::GDSCRIPT;
  503. } break;
  504. case GDScriptDataType::SCRIPT: {
  505. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  506. } break;
  507. }
  508. return ci;
  509. }
  510. static bool _guess_identifier_type(const GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  511. static bool _guess_identifier_type_from_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  512. static bool _guess_method_return_type_from_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  513. static bool _guess_expression_type(const GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_expression, GDScriptCompletionIdentifier &r_type) {
  514. bool found = false;
  515. switch (p_expression->type) {
  516. case GDScriptParser::Node::TYPE_CONSTANT: {
  517. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(p_expression);
  518. r_type = _type_from_variant(cn->value);
  519. found = true;
  520. } break;
  521. case GDScriptParser::Node::TYPE_SELF: {
  522. if (p_context._class) {
  523. r_type.type.has_type = true;
  524. r_type.type.kind = GDScriptParser::DataType::CLASS;
  525. r_type.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  526. r_type.type.is_constant = true;
  527. r_type.value = p_context.base;
  528. found = true;
  529. }
  530. } break;
  531. case GDScriptParser::Node::TYPE_IDENTIFIER: {
  532. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  533. found = _guess_identifier_type(p_context, id->name, r_type);
  534. } break;
  535. case GDScriptParser::Node::TYPE_DICTIONARY: {
  536. // Try to recreate the dictionary
  537. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  538. Dictionary d;
  539. bool full = true;
  540. for (int i = 0; i < dn->elements.size(); i++) {
  541. GDScriptCompletionIdentifier key;
  542. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  543. GDScriptCompletionIdentifier value;
  544. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  545. if (!value.type.is_constant) {
  546. full = false;
  547. break;
  548. }
  549. d[key.value] = value.value;
  550. } else {
  551. full = false;
  552. break;
  553. }
  554. } else {
  555. full = false;
  556. break;
  557. }
  558. }
  559. if (full) {
  560. // If not fully constant, setting this value is detrimental to the inference
  561. r_type.value = d;
  562. r_type.type.is_constant = true;
  563. }
  564. r_type.type.has_type = true;
  565. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  566. r_type.type.builtin_type = Variant::DICTIONARY;
  567. } break;
  568. case GDScriptParser::Node::TYPE_ARRAY: {
  569. // Try to recreate the array
  570. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  571. Array a;
  572. bool full = true;
  573. a.resize(an->elements.size());
  574. for (int i = 0; i < an->elements.size(); i++) {
  575. GDScriptCompletionIdentifier value;
  576. if (_guess_expression_type(p_context, an->elements[i], value)) {
  577. a[i] = value.value;
  578. } else {
  579. full = false;
  580. break;
  581. }
  582. }
  583. if (full) {
  584. // If not fully constant, setting this value is detrimental to the inference
  585. r_type.value = a;
  586. }
  587. r_type.type.has_type = true;
  588. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  589. r_type.type.builtin_type = Variant::ARRAY;
  590. } break;
  591. case GDScriptParser::Node::TYPE_OPERATOR: {
  592. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_expression);
  593. switch (op->op) {
  594. case GDScriptParser::OperatorNode::OP_CALL: {
  595. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  596. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  597. r_type.type.has_type = true;
  598. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  599. r_type.type.builtin_type = tn->vtype;
  600. found = true;
  601. break;
  602. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  603. const GDScriptParser::BuiltInFunctionNode *bin = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  604. MethodInfo mi = GDScriptFunctions::get_info(bin->function);
  605. r_type = _type_from_property(mi.return_val);
  606. found = true;
  607. break;
  608. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  609. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1])->name;
  610. GDScriptCompletionContext c = p_context;
  611. c.line = op->line;
  612. GDScriptCompletionIdentifier base;
  613. if (!_guess_expression_type(c, op->arguments[0], base)) {
  614. found = false;
  615. break;
  616. }
  617. // Try call if constant methods with constant arguments
  618. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  619. GDScriptParser::DataType native_type = base.type;
  620. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  621. native_type = native_type.class_type->base_type;
  622. }
  623. while (native_type.kind == GDScriptParser::DataType::GDSCRIPT || native_type.kind == GDScriptParser::DataType::SCRIPT) {
  624. if (native_type.script_type.is_valid()) {
  625. Ref<Script> parent = native_type.script_type->get_base_script();
  626. if (parent.is_valid()) {
  627. native_type.script_type = parent;
  628. } else {
  629. native_type.kind = GDScriptParser::DataType::NATIVE;
  630. native_type.native_type = native_type.script_type->get_instance_base_type();
  631. if (!ClassDB::class_exists(native_type.native_type)) {
  632. native_type.native_type = String("_") + native_type.native_type;
  633. if (!ClassDB::class_exists(native_type.native_type)) {
  634. native_type.has_type = false;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. if (native_type.has_type && native_type.kind == GDScriptParser::DataType::NATIVE) {
  641. MethodBind *mb = ClassDB::get_method(native_type.native_type, id);
  642. if (mb && mb->is_const()) {
  643. bool all_is_const = true;
  644. Vector<Variant> args;
  645. GDScriptCompletionContext c = p_context;
  646. c.line = op->line;
  647. for (int i = 2; all_is_const && i < op->arguments.size(); i++) {
  648. GDScriptCompletionIdentifier arg;
  649. if (_guess_expression_type(c, op->arguments[i], arg)) {
  650. if (arg.type.has_type && arg.type.is_constant && arg.value.get_type() != Variant::OBJECT) {
  651. args.push_back(arg.value);
  652. } else {
  653. all_is_const = false;
  654. }
  655. } else {
  656. all_is_const = false;
  657. }
  658. }
  659. Object *baseptr = base.value;
  660. if (all_is_const && String(id) == "get_node" && ClassDB::is_parent_class(native_type.native_type, "Node") && args.size()) {
  661. String arg1 = args[0];
  662. if (arg1.begins_with("/root/")) {
  663. String which = arg1.get_slice("/", 2);
  664. if (which != "") {
  665. // Try singletons first
  666. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  667. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  668. found = true;
  669. } else {
  670. List<PropertyInfo> props;
  671. ProjectSettings::get_singleton()->get_property_list(&props);
  672. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  673. String s = E->get().name;
  674. if (!s.begins_with("autoload/")) {
  675. continue;
  676. }
  677. String name = s.get_slice("/", 1);
  678. if (name == which) {
  679. String script = ProjectSettings::get_singleton()->get(s);
  680. if (script.begins_with("*")) {
  681. script = script.right(1);
  682. }
  683. if (!script.begins_with("res://")) {
  684. script = "res://" + script;
  685. }
  686. if (!script.ends_with(".gd")) {
  687. //not a script, try find the script anyway,
  688. //may have some success
  689. script = script.get_basename() + ".gd";
  690. }
  691. if (FileAccess::exists(script)) {
  692. Ref<Script> scr;
  693. if (ScriptCodeCompletionCache::get_singleton()) {
  694. scr = ScriptCodeCompletionCache::get_singleton()->get_cached_resource(script);
  695. } else {
  696. scr = ResourceLoader::load(script);
  697. }
  698. if (scr.is_valid()) {
  699. r_type.type.has_type = true;
  700. r_type.type.script_type = scr;
  701. r_type.type.is_constant = false;
  702. Ref<GDScript> gds = scr;
  703. if (gds.is_valid()) {
  704. r_type.type.kind = GDScriptParser::DataType::GDSCRIPT;
  705. } else {
  706. r_type.type.kind = GDScriptParser::DataType::SCRIPT;
  707. }
  708. r_type.value = Variant();
  709. found = true;
  710. }
  711. }
  712. break;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. }
  719. if (!found && all_is_const && baseptr) {
  720. Vector<const Variant *> argptr;
  721. for (int i = 0; i < args.size(); i++) {
  722. argptr.push_back(&args[i]);
  723. }
  724. Variant::CallError ce;
  725. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  726. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  727. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  728. r_type = _type_from_variant(ret);
  729. found = true;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. }
  736. if (!found) {
  737. found = _guess_method_return_type_from_base(c, base, id, r_type);
  738. }
  739. }
  740. } break;
  741. case GDScriptParser::OperatorNode::OP_PARENT_CALL: {
  742. if (!p_context._class || !op->arguments.size() || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  743. break;
  744. }
  745. StringName id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  746. GDScriptCompletionIdentifier base;
  747. base.value = p_context.base;
  748. base.type = p_context._class->base_type;
  749. GDScriptCompletionContext c = p_context;
  750. c.line = op->line;
  751. found = _guess_method_return_type_from_base(c, base, id, r_type);
  752. } break;
  753. case GDScriptParser::OperatorNode::OP_INDEX_NAMED: {
  754. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  755. found = false;
  756. break;
  757. }
  758. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  759. GDScriptCompletionContext c = p_context;
  760. c.line = op->line;
  761. GDScriptCompletionIdentifier base;
  762. if (!_guess_expression_type(c, op->arguments[0], base)) {
  763. found = false;
  764. break;
  765. }
  766. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(id->name))) {
  767. Variant value = base.value.operator Dictionary()[String(id->name)];
  768. r_type = _type_from_variant(value);
  769. found = true;
  770. break;
  771. }
  772. const GDScriptParser::DictionaryNode *dn = NULL;
  773. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  774. dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  775. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  776. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  777. }
  778. if (dn) {
  779. for (int i = 0; i < dn->elements.size(); i++) {
  780. GDScriptCompletionIdentifier key;
  781. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  782. continue;
  783. }
  784. if (key.value == String(id->name)) {
  785. r_type.assigned_expression = dn->elements[i].value;
  786. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  787. break;
  788. }
  789. }
  790. }
  791. if (!found) {
  792. found = _guess_identifier_type_from_base(c, base, id->name, r_type);
  793. }
  794. } break;
  795. case GDScriptParser::OperatorNode::OP_INDEX: {
  796. if (op->arguments.size() < 2) {
  797. found = false;
  798. break;
  799. }
  800. GDScriptCompletionContext c = p_context;
  801. c.line = op->line;
  802. GDScriptCompletionIdentifier base;
  803. if (!_guess_expression_type(c, op->arguments[0], base)) {
  804. found = false;
  805. break;
  806. }
  807. GDScriptCompletionIdentifier index;
  808. if (!_guess_expression_type(c, op->arguments[1], index)) {
  809. found = false;
  810. break;
  811. }
  812. if (base.value.in(index.value)) {
  813. Variant value = base.value.get(index.value);
  814. r_type = _type_from_variant(value);
  815. found = true;
  816. break;
  817. }
  818. // Look if it is a dictionary node
  819. const GDScriptParser::DictionaryNode *dn = NULL;
  820. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  821. dn = static_cast<const GDScriptParser::DictionaryNode *>(op->arguments[0]);
  822. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_DICTIONARY) {
  823. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  824. }
  825. if (dn) {
  826. for (int i = 0; i < dn->elements.size(); i++) {
  827. GDScriptCompletionIdentifier key;
  828. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  829. continue;
  830. }
  831. if (key.value == index.value) {
  832. r_type.assigned_expression = dn->elements[i].value;
  833. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  834. break;
  835. }
  836. }
  837. }
  838. // Look if it is an array node
  839. if (!found && index.value.is_num()) {
  840. int idx = index.value;
  841. const GDScriptParser::ArrayNode *an = NULL;
  842. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_ARRAY) {
  843. an = static_cast<const GDScriptParser::ArrayNode *>(op->arguments[0]);
  844. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::TYPE_ARRAY) {
  845. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  846. }
  847. if (an && idx >= 0 && an->elements.size() > idx) {
  848. r_type.assigned_expression = an->elements[idx];
  849. found = _guess_expression_type(c, an->elements[idx], r_type);
  850. break;
  851. }
  852. }
  853. // Look for valid indexing in other types
  854. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  855. StringName id = index.value;
  856. found = _guess_identifier_type_from_base(c, base, id, r_type);
  857. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  858. Variant::CallError err;
  859. Variant base_val = Variant::construct(base.type.builtin_type, NULL, 0, err);
  860. bool valid = false;
  861. Variant res = base_val.get(index.value, &valid);
  862. if (valid) {
  863. r_type = _type_from_variant(res);
  864. r_type.value = Variant();
  865. r_type.type.is_constant = false;
  866. found = true;
  867. }
  868. }
  869. } break;
  870. default: {
  871. if (op->arguments.size() < 2) {
  872. found = false;
  873. break;
  874. }
  875. Variant::Operator vop = Variant::OP_MAX;
  876. switch (op->op) {
  877. case GDScriptParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  878. case GDScriptParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBTRACT; break;
  879. case GDScriptParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  880. case GDScriptParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  881. case GDScriptParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  882. case GDScriptParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  883. case GDScriptParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  884. case GDScriptParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  885. case GDScriptParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  886. case GDScriptParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  887. default: {}
  888. }
  889. if (vop == Variant::OP_MAX) {
  890. break;
  891. }
  892. GDScriptCompletionContext context = p_context;
  893. context.line = op->line;
  894. GDScriptCompletionIdentifier p1;
  895. GDScriptCompletionIdentifier p2;
  896. if (!_guess_expression_type(context, op->arguments[0], p1)) {
  897. found = false;
  898. break;
  899. }
  900. if (!_guess_expression_type(context, op->arguments[1], p2)) {
  901. found = false;
  902. break;
  903. }
  904. Variant::CallError ce;
  905. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  906. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type.builtin_type, NULL, 0, ce);
  907. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  908. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type.builtin_type, NULL, 0, ce);
  909. // avoid potential invalid ops
  910. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  911. v2 = 1;
  912. v2_use_value = false;
  913. }
  914. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  915. v2 = 1.0;
  916. v2_use_value = false;
  917. }
  918. Variant res;
  919. bool valid;
  920. Variant::evaluate(vop, v1, v2, res, valid);
  921. if (!valid) {
  922. found = false;
  923. break;
  924. }
  925. r_type = _type_from_variant(res);
  926. if (!v1_use_value || !v2_use_value) {
  927. r_type.value = Variant();
  928. r_type.type.is_constant = false;
  929. }
  930. found = true;
  931. } break;
  932. }
  933. } break;
  934. default: {}
  935. }
  936. // It may have found a null, but that's never useful
  937. if (found && r_type.type.has_type && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  938. found = false;
  939. }
  940. // Check type hint last. For collections we want chance to get the actual value first
  941. // This way we can detect types from the content of dictionaries and arrays
  942. if (!found && p_expression->get_datatype().has_type) {
  943. r_type.type = p_expression->get_datatype();
  944. if (!r_type.assigned_expression) {
  945. r_type.assigned_expression = p_expression;
  946. }
  947. found = true;
  948. }
  949. return found;
  950. }
  951. static bool _guess_identifier_type(const GDScriptCompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  952. // Look in blocks first
  953. const GDScriptParser::BlockNode *blk = p_context.block;
  954. int last_assign_line = -1;
  955. const GDScriptParser::Node *last_assigned_expression = NULL;
  956. GDScriptParser::DataType var_type;
  957. while (blk) {
  958. if (blk->variables.has(p_identifier)) {
  959. if (blk->variables[p_identifier]->line > p_context.line) {
  960. return false;
  961. }
  962. var_type = blk->variables[p_identifier]->datatype;
  963. if (!last_assigned_expression && blk->variables[p_identifier]->assign && blk->variables[p_identifier]->assign->type == GDScriptParser::Node::TYPE_OPERATOR) {
  964. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->variables[p_identifier]->assign);
  965. if (op->op == GDScriptParser::OperatorNode::OP_ASSIGN && op->arguments.size() >= 2) {
  966. last_assign_line = op->line;
  967. last_assigned_expression = op->arguments[1];
  968. }
  969. }
  970. }
  971. for (const List<GDScriptParser::Node *>::Element *E = blk->statements.front(); E; E = E->next()) {
  972. const GDScriptParser::Node *expr = E->get();
  973. if (expr->line > p_context.line || expr->type != GDScriptParser::Node::TYPE_OPERATOR) {
  974. continue;
  975. }
  976. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(expr);
  977. if (op->op != GDScriptParser::OperatorNode::OP_ASSIGN || op->line < last_assign_line) {
  978. continue;
  979. }
  980. if (op->arguments.size() >= 2 && op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  981. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0]);
  982. if (id->name == p_identifier) {
  983. last_assign_line = op->line;
  984. last_assigned_expression = op->arguments[1];
  985. }
  986. }
  987. }
  988. if (blk->if_condition && blk->if_condition->type == GDScriptParser::Node::TYPE_OPERATOR && static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition)->op == GDScriptParser::OperatorNode::OP_IS) {
  989. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  990. //super dirty hack, but very useful
  991. //credit: Zylann
  992. //TODO: this could be hacked to detect ANDed conditions too..
  993. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(blk->if_condition);
  994. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  995. //bingo
  996. GDScriptCompletionContext c = p_context;
  997. c.line = op->line;
  998. c.block = blk;
  999. if (_guess_expression_type(p_context, op->arguments[1], r_type)) {
  1000. r_type.type.is_meta_type = false; // Right-hand of `is` will be a meta type, but the left-hand value is not
  1001. // Not an assignment, it shouldn't carry any value
  1002. r_type.value = Variant();
  1003. r_type.assigned_expression = NULL;
  1004. return true;
  1005. }
  1006. }
  1007. }
  1008. blk = blk->parent_block;
  1009. }
  1010. if (last_assigned_expression && last_assign_line != p_context.line) {
  1011. GDScriptCompletionContext c = p_context;
  1012. c.line = last_assign_line;
  1013. r_type.assigned_expression = last_assigned_expression;
  1014. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1015. return true;
  1016. }
  1017. }
  1018. if (var_type.has_type) {
  1019. r_type.type = var_type;
  1020. return true;
  1021. }
  1022. if (p_context.function) {
  1023. for (int i = 0; i < p_context.function->arguments.size(); i++) {
  1024. if (p_context.function->arguments[i] == p_identifier) {
  1025. if (p_context.function->argument_types[i].has_type) {
  1026. r_type.type = p_context.function->argument_types[i];
  1027. return true;
  1028. }
  1029. int def_from = p_context.function->arguments.size() - p_context.function->default_values.size();
  1030. if (i >= def_from) {
  1031. int def_idx = i - def_from;
  1032. if (p_context.function->default_values[def_idx]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1033. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_context.function->default_values[def_idx]);
  1034. if (op->arguments.size() < 2) {
  1035. return false;
  1036. }
  1037. GDScriptCompletionContext c = p_context;
  1038. c.function = NULL;
  1039. c.block = NULL;
  1040. return _guess_expression_type(c, op->arguments[1], r_type);
  1041. }
  1042. }
  1043. break;
  1044. }
  1045. }
  1046. GDScriptParser::DataType base_type = p_context._class->base_type;
  1047. while (base_type.has_type) {
  1048. switch (base_type.kind) {
  1049. case GDScriptParser::DataType::GDSCRIPT: {
  1050. Ref<GDScript> gds = base_type.script_type;
  1051. if (gds.is_valid() && gds->has_method(p_context.function->name)) {
  1052. GDScriptFunction *func = gds->get_member_functions()[p_context.function->name];
  1053. if (func) {
  1054. for (int i = 0; i < func->get_argument_count(); i++) {
  1055. if (func->get_argument_name(i) == p_identifier) {
  1056. r_type = _type_from_gdtype(func->get_argument_type(i));
  1057. return true;
  1058. }
  1059. }
  1060. }
  1061. Ref<GDScript> base_gds = gds->get_base_script();
  1062. if (base_gds.is_valid()) {
  1063. base_type.kind = GDScriptParser::DataType::GDSCRIPT;
  1064. base_type.script_type = base_gds;
  1065. } else {
  1066. base_type.kind = GDScriptParser::DataType::NATIVE;
  1067. base_type.native_type = gds->get_instance_base_type();
  1068. }
  1069. } else {
  1070. base_type.kind = GDScriptParser::DataType::NATIVE;
  1071. base_type.native_type = gds->get_instance_base_type();
  1072. }
  1073. } break;
  1074. case GDScriptParser::DataType::NATIVE: {
  1075. List<MethodInfo> methods;
  1076. ClassDB::get_method_list(base_type.native_type, &methods);
  1077. ClassDB::get_virtual_methods(base_type.native_type, &methods);
  1078. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1079. if (E->get().name == p_context.function->name) {
  1080. MethodInfo &mi = E->get();
  1081. for (List<PropertyInfo>::Element *E = mi.arguments.front(); E; E = E->next()) {
  1082. if (E->get().name == p_identifier) {
  1083. r_type = _type_from_property(E->get());
  1084. return true;
  1085. }
  1086. }
  1087. }
  1088. }
  1089. base_type.has_type = false;
  1090. } break;
  1091. default: {
  1092. base_type.has_type = false;
  1093. } break;
  1094. }
  1095. }
  1096. }
  1097. // Check current class (including inheritance)
  1098. if (p_context._class) {
  1099. GDScriptCompletionIdentifier context_base;
  1100. context_base.value = p_context.base;
  1101. context_base.type.has_type = true;
  1102. context_base.type.kind = GDScriptParser::DataType::CLASS;
  1103. context_base.type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  1104. context_base.type.is_meta_type = p_context.function && p_context.function->_static;
  1105. if (_guess_identifier_type_from_base(p_context, context_base, p_identifier, r_type)) {
  1106. return true;
  1107. }
  1108. }
  1109. // Check named scripts
  1110. if (ScriptServer::is_global_class(p_identifier)) {
  1111. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1112. if (scr.is_valid()) {
  1113. r_type = _type_from_variant(scr);
  1114. r_type.type.is_meta_type = true;
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. for (int i = 0; i < 2; i++) {
  1120. StringName target_id;
  1121. switch (i) {
  1122. case 0:
  1123. // Check ClassDB
  1124. target_id = p_identifier;
  1125. break;
  1126. case 1:
  1127. // ClassDB again for underscore-prefixed classes
  1128. target_id = String("_") + p_identifier;
  1129. break;
  1130. }
  1131. if (ClassDB::class_exists(target_id)) {
  1132. r_type.type.has_type = true;
  1133. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1134. r_type.type.native_type = target_id;
  1135. if (Engine::get_singleton()->has_singleton(target_id)) {
  1136. r_type.type.is_meta_type = false;
  1137. r_type.value = Engine::get_singleton()->get_singleton_object(target_id);
  1138. } else {
  1139. r_type.type.is_meta_type = true;
  1140. const Map<StringName, int>::Element *target_elem = GDScriptLanguage::get_singleton()->get_global_map().find(target_id);
  1141. // Check because classes like EditorNode are in ClassDB by now, but unknown to GDScript
  1142. if (!target_elem) {
  1143. return false;
  1144. }
  1145. int idx = target_elem->get();
  1146. r_type.value = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  1147. }
  1148. return true;
  1149. }
  1150. }
  1151. // Check autoload singletons
  1152. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  1153. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1154. return true;
  1155. }
  1156. return false;
  1157. }
  1158. static bool _guess_identifier_type_from_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1159. GDScriptParser::DataType base_type = p_base.type;
  1160. bool _static = base_type.is_meta_type;
  1161. while (base_type.has_type) {
  1162. switch (base_type.kind) {
  1163. case GDScriptParser::DataType::CLASS: {
  1164. if (base_type.class_type->constant_expressions.has(p_identifier)) {
  1165. GDScriptParser::ClassNode::Constant c = base_type.class_type->constant_expressions[p_identifier];
  1166. r_type.type = c.type;
  1167. if (c.expression->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1168. r_type.value = static_cast<const GDScriptParser::ConstantNode *>(c.expression)->value;
  1169. }
  1170. return true;
  1171. }
  1172. if (!_static) {
  1173. for (int i = 0; i < base_type.class_type->variables.size(); i++) {
  1174. GDScriptParser::ClassNode::Member m = base_type.class_type->variables[i];
  1175. if (m.identifier == p_identifier) {
  1176. if (m.expression) {
  1177. if (p_context.line == m.expression->line) {
  1178. // Variable used in the same expression
  1179. return false;
  1180. }
  1181. if (_guess_expression_type(p_context, m.expression, r_type)) {
  1182. return true;
  1183. }
  1184. if (m.expression->get_datatype().has_type) {
  1185. r_type.type = m.expression->get_datatype();
  1186. return true;
  1187. }
  1188. }
  1189. if (m.data_type.has_type) {
  1190. r_type.type = m.data_type;
  1191. return true;
  1192. }
  1193. return false;
  1194. }
  1195. }
  1196. }
  1197. base_type = base_type.class_type->base_type;
  1198. } break;
  1199. case GDScriptParser::DataType::GDSCRIPT: {
  1200. Ref<GDScript> gds = base_type.script_type;
  1201. if (gds.is_valid()) {
  1202. if (gds->get_constants().has(p_identifier)) {
  1203. r_type = _type_from_variant(gds->get_constants()[p_identifier]);
  1204. return true;
  1205. }
  1206. if (!_static) {
  1207. const Set<StringName>::Element *m = gds->get_members().find(p_identifier);
  1208. if (m) {
  1209. r_type = _type_from_gdtype(gds->get_member_type(p_identifier));
  1210. return true;
  1211. }
  1212. }
  1213. Ref<GDScript> parent = gds->get_base_script();
  1214. if (parent.is_valid()) {
  1215. base_type.script_type = parent;
  1216. } else {
  1217. base_type.kind = GDScriptParser::DataType::NATIVE;
  1218. base_type.native_type = gds->get_instance_base_type();
  1219. }
  1220. } else {
  1221. return false;
  1222. }
  1223. } break;
  1224. case GDScriptParser::DataType::SCRIPT: {
  1225. Ref<Script> scr = base_type.script_type;
  1226. if (scr.is_valid()) {
  1227. Map<StringName, Variant> constants;
  1228. scr->get_constants(&constants);
  1229. if (constants.has(p_identifier)) {
  1230. r_type = _type_from_variant(constants[p_identifier]);
  1231. return true;
  1232. }
  1233. if (!_static) {
  1234. List<PropertyInfo> members;
  1235. scr->get_script_property_list(&members);
  1236. for (const List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1237. const PropertyInfo &prop = E->get();
  1238. if (prop.name == p_identifier) {
  1239. r_type = _type_from_property(prop);
  1240. return true;
  1241. }
  1242. }
  1243. }
  1244. Ref<Script> parent = scr->get_base_script();
  1245. if (parent.is_valid()) {
  1246. base_type.script_type = parent;
  1247. } else {
  1248. base_type.kind = GDScriptParser::DataType::NATIVE;
  1249. base_type.native_type = scr->get_instance_base_type();
  1250. }
  1251. } else {
  1252. return false;
  1253. }
  1254. } break;
  1255. case GDScriptParser::DataType::NATIVE: {
  1256. StringName class_name = base_type.native_type;
  1257. if (!ClassDB::class_exists(class_name)) {
  1258. class_name = String("_") + class_name;
  1259. if (!ClassDB::class_exists(class_name)) {
  1260. return false;
  1261. }
  1262. }
  1263. // Skip constants since they're all integers. Type does not matter because int has no members
  1264. List<PropertyInfo> props;
  1265. ClassDB::get_property_list(class_name, &props);
  1266. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1267. const PropertyInfo &prop = E->get();
  1268. if (prop.name == p_identifier) {
  1269. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1270. if (getter != StringName()) {
  1271. MethodBind *g = ClassDB::get_method(class_name, getter);
  1272. if (g) {
  1273. r_type = _type_from_property(g->get_return_info());
  1274. return true;
  1275. }
  1276. } else {
  1277. r_type = _type_from_property(prop);
  1278. return true;
  1279. }
  1280. break;
  1281. }
  1282. }
  1283. return false;
  1284. } break;
  1285. case GDScriptParser::DataType::BUILTIN: {
  1286. Variant::CallError err;
  1287. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  1288. if (err.error != Variant::CallError::CALL_OK) {
  1289. return false;
  1290. }
  1291. bool valid = false;
  1292. Variant res = tmp.get(p_identifier, &valid);
  1293. if (valid) {
  1294. r_type = _type_from_variant(res);
  1295. r_type.value = Variant();
  1296. r_type.type.is_constant = false;
  1297. return true;
  1298. }
  1299. return false;
  1300. } break;
  1301. default: {
  1302. return false;
  1303. } break;
  1304. }
  1305. }
  1306. return false;
  1307. }
  1308. static bool _find_last_return_in_block(const GDScriptCompletionContext &p_context, int &r_last_return_line, const GDScriptParser::Node **r_last_returned_value) {
  1309. if (!p_context.block) {
  1310. return false;
  1311. }
  1312. for (int i = 0; i < p_context.block->statements.size(); i++) {
  1313. if (p_context.block->statements[i]->line < r_last_return_line) {
  1314. continue;
  1315. }
  1316. if (p_context.block->statements[i]->type != GDScriptParser::Node::TYPE_CONTROL_FLOW) {
  1317. continue;
  1318. }
  1319. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(p_context.block->statements[i]);
  1320. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_RETURN && cf->arguments.size() > 0) {
  1321. if (cf->line > r_last_return_line) {
  1322. r_last_return_line = cf->line;
  1323. *r_last_returned_value = cf->arguments[0];
  1324. }
  1325. }
  1326. }
  1327. // Recurse into subblocks
  1328. for (int i = 0; i < p_context.block->sub_blocks.size(); i++) {
  1329. GDScriptCompletionContext c = p_context;
  1330. c.block = p_context.block->sub_blocks[i];
  1331. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1332. }
  1333. return false;
  1334. }
  1335. static bool _guess_method_return_type_from_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1336. GDScriptParser::DataType base_type = p_base.type;
  1337. bool _static = base_type.is_meta_type;
  1338. if (_static && p_method == "new") {
  1339. r_type.type = base_type;
  1340. r_type.type.is_meta_type = false;
  1341. r_type.type.is_constant = false;
  1342. return true;
  1343. }
  1344. while (base_type.has_type) {
  1345. switch (base_type.kind) {
  1346. case GDScriptParser::DataType::CLASS: {
  1347. if (!base_type.class_type) {
  1348. base_type.has_type = false;
  1349. break;
  1350. }
  1351. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  1352. if (base_type.class_type->static_functions[i]->name == p_method) {
  1353. int last_return_line = -1;
  1354. const GDScriptParser::Node *last_returned_value = NULL;
  1355. GDScriptCompletionContext c = p_context;
  1356. c._class = base_type.class_type;
  1357. c.function = base_type.class_type->static_functions[i];
  1358. c.block = c.function->body;
  1359. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1360. if (last_returned_value) {
  1361. c.line = c.block->end_line;
  1362. return _guess_expression_type(c, last_returned_value, r_type);
  1363. }
  1364. }
  1365. }
  1366. if (!_static) {
  1367. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  1368. if (base_type.class_type->functions[i]->name == p_method) {
  1369. int last_return_line = -1;
  1370. const GDScriptParser::Node *last_returned_value = NULL;
  1371. GDScriptCompletionContext c = p_context;
  1372. c._class = base_type.class_type;
  1373. c.function = base_type.class_type->functions[i];
  1374. c.block = c.function->body;
  1375. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1376. if (last_returned_value) {
  1377. c.line = c.block->end_line;
  1378. return _guess_expression_type(c, last_returned_value, r_type);
  1379. }
  1380. }
  1381. }
  1382. }
  1383. base_type = base_type.class_type->base_type;
  1384. } break;
  1385. case GDScriptParser::DataType::GDSCRIPT: {
  1386. Ref<GDScript> gds = base_type.script_type;
  1387. if (gds.is_valid()) {
  1388. if (gds->get_member_functions().has(p_method)) {
  1389. r_type = _type_from_gdtype(gds->get_member_functions()[p_method]->get_return_type());
  1390. return true;
  1391. }
  1392. Ref<GDScript> base_script = gds->get_base_script();
  1393. if (base_script.is_valid()) {
  1394. base_type.script_type = base_script;
  1395. } else {
  1396. base_type.kind = GDScriptParser::DataType::NATIVE;
  1397. base_type.native_type = gds->get_instance_base_type();
  1398. }
  1399. } else {
  1400. return false;
  1401. }
  1402. } break;
  1403. case GDScriptParser::DataType::SCRIPT: {
  1404. Ref<Script> scr = base_type.script_type;
  1405. if (scr.is_valid()) {
  1406. List<MethodInfo> methods;
  1407. scr->get_script_method_list(&methods);
  1408. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1409. MethodInfo &mi = E->get();
  1410. if (mi.name == p_method) {
  1411. r_type = _type_from_property(mi.return_val);
  1412. return true;
  1413. }
  1414. }
  1415. Ref<Script> base_script = scr->get_base_script();
  1416. if (base_script.is_valid()) {
  1417. base_type.script_type = base_script;
  1418. } else {
  1419. base_type.kind = GDScriptParser::DataType::NATIVE;
  1420. base_type.native_type = scr->get_instance_base_type();
  1421. }
  1422. } else {
  1423. return false;
  1424. }
  1425. } break;
  1426. case GDScriptParser::DataType::NATIVE: {
  1427. StringName native = base_type.native_type;
  1428. if (!ClassDB::class_exists(native)) {
  1429. native = String("_") + native;
  1430. if (!ClassDB::class_exists(native)) {
  1431. return false;
  1432. }
  1433. }
  1434. MethodBind *mb = ClassDB::get_method(native, p_method);
  1435. if (mb) {
  1436. r_type = _type_from_property(mb->get_return_info());
  1437. return true;
  1438. }
  1439. return false;
  1440. } break;
  1441. case GDScriptParser::DataType::BUILTIN: {
  1442. Variant::CallError err;
  1443. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  1444. if (err.error != Variant::CallError::CALL_OK) {
  1445. return false;
  1446. }
  1447. List<MethodInfo> methods;
  1448. tmp.get_method_list(&methods);
  1449. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1450. MethodInfo &mi = E->get();
  1451. if (mi.name == p_method) {
  1452. r_type = _type_from_property(mi.return_val);
  1453. return true;
  1454. }
  1455. }
  1456. return false;
  1457. } break;
  1458. default: {
  1459. return false;
  1460. }
  1461. }
  1462. }
  1463. return false;
  1464. }
  1465. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) {
  1466. String arghint = _get_visual_datatype(p_info.return_val, false) + " " + p_info.name + "(";
  1467. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  1468. int i = 0;
  1469. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
  1470. if (i > 0) {
  1471. arghint += ", ";
  1472. } else {
  1473. arghint += " ";
  1474. }
  1475. if (i == p_arg_idx) {
  1476. arghint += String::chr(0xFFFF);
  1477. }
  1478. arghint += _get_visual_datatype(E->get(), true) + " " + E->get().name;
  1479. if (i - def_args >= 0) {
  1480. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  1481. }
  1482. if (i == p_arg_idx) {
  1483. arghint += String::chr(0xFFFF);
  1484. }
  1485. i++;
  1486. }
  1487. if (p_info.flags & METHOD_FLAG_VARARG) {
  1488. if (p_info.arguments.size() > 0) {
  1489. arghint += ", ";
  1490. } else {
  1491. arghint += " ";
  1492. }
  1493. if (p_arg_idx >= p_info.arguments.size()) {
  1494. arghint += String::chr(0xFFFF);
  1495. }
  1496. arghint += "...";
  1497. if (p_arg_idx >= p_info.arguments.size()) {
  1498. arghint += String::chr(0xFFFF);
  1499. }
  1500. }
  1501. if (p_info.arguments.size() > 0 || (p_info.flags & METHOD_FLAG_VARARG)) {
  1502. arghint += " ";
  1503. }
  1504. arghint += ")";
  1505. return arghint;
  1506. }
  1507. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  1508. String arghint = p_function->return_type.to_string() + " " + p_function->name.operator String() + "(";
  1509. int def_args = p_function->arguments.size() - p_function->default_values.size();
  1510. for (int i = 0; i < p_function->arguments.size(); i++) {
  1511. if (i > 0) {
  1512. arghint += ", ";
  1513. } else {
  1514. arghint += " ";
  1515. }
  1516. if (i == p_arg_idx) {
  1517. arghint += String::chr(0xFFFF);
  1518. }
  1519. arghint += p_function->argument_types[i].to_string() + " " + p_function->arguments[i].operator String();
  1520. if (i - def_args >= 0) {
  1521. String def_val = "<unknown>";
  1522. if (p_function->default_values[i - def_args] && p_function->default_values[i - def_args]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1523. const GDScriptParser::OperatorNode *assign = static_cast<const GDScriptParser::OperatorNode *>(p_function->default_values[i - def_args]);
  1524. if (assign->arguments.size() >= 2) {
  1525. if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1526. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(assign->arguments[1]);
  1527. def_val = cn->value.get_construct_string();
  1528. } else if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1529. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->arguments[1]);
  1530. def_val = id->name.operator String();
  1531. }
  1532. }
  1533. }
  1534. arghint += " = " + def_val;
  1535. }
  1536. if (i == p_arg_idx) {
  1537. arghint += String::chr(0xFFFF);
  1538. }
  1539. }
  1540. if (p_function->arguments.size() > 0) {
  1541. arghint += " ";
  1542. }
  1543. arghint += ")";
  1544. return arghint;
  1545. }
  1546. static void _find_enumeration_candidates(const String p_enum_hint, Set<String> &r_result) {
  1547. if (p_enum_hint.find(".") == -1) {
  1548. // Global constant
  1549. StringName current_enum = p_enum_hint;
  1550. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1551. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1552. r_result.insert(GlobalConstants::get_global_constant_name(i));
  1553. }
  1554. }
  1555. } else {
  1556. String class_name = p_enum_hint.get_slice(".", 0);
  1557. String enum_name = p_enum_hint.get_slice(".", 1);
  1558. if (!ClassDB::class_exists(class_name)) {
  1559. return;
  1560. }
  1561. List<StringName> enum_constants;
  1562. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  1563. for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  1564. String candidate = class_name + "." + E->get();
  1565. r_result.insert(candidate);
  1566. }
  1567. }
  1568. }
  1569. static void _find_identifiers_in_block(const GDScriptCompletionContext &p_context, Set<String> &r_result) {
  1570. for (Map<StringName, GDScriptParser::LocalVarNode *>::Element *E = p_context.block->variables.front(); E; E = E->next()) {
  1571. if (E->get()->line < p_context.line) {
  1572. r_result.insert(E->key().operator String());
  1573. }
  1574. }
  1575. if (p_context.block->parent_block) {
  1576. GDScriptCompletionContext c = p_context;
  1577. c.block = p_context.block->parent_block;
  1578. _find_identifiers_in_block(c, r_result);
  1579. }
  1580. }
  1581. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Set<String> &r_result);
  1582. static void _find_identifiers_in_class(const GDScriptCompletionContext &p_context, bool p_static, bool p_only_functions, bool p_parent_only, Set<String> &r_result) {
  1583. if (!p_parent_only) {
  1584. if (!p_static && !p_only_functions) {
  1585. for (int i = 0; i < p_context._class->variables.size(); i++) {
  1586. r_result.insert(p_context._class->variables[i].identifier);
  1587. }
  1588. }
  1589. if (!p_only_functions) {
  1590. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_context._class->constant_expressions.front(); E; E = E->next()) {
  1591. r_result.insert(E->key());
  1592. }
  1593. for (int i = 0; i < p_context._class->subclasses.size(); i++) {
  1594. r_result.insert(p_context._class->subclasses[i]->name);
  1595. }
  1596. }
  1597. for (int i = 0; i < p_context._class->static_functions.size(); i++) {
  1598. if (p_context._class->static_functions[i]->arguments.size()) {
  1599. r_result.insert(p_context._class->static_functions[i]->name.operator String() + "(");
  1600. } else {
  1601. r_result.insert(p_context._class->static_functions[i]->name.operator String() + "()");
  1602. }
  1603. }
  1604. if (!p_static) {
  1605. for (int i = 0; i < p_context._class->functions.size(); i++) {
  1606. if (p_context._class->functions[i]->arguments.size()) {
  1607. r_result.insert(p_context._class->functions[i]->name.operator String() + "(");
  1608. } else {
  1609. r_result.insert(p_context._class->functions[i]->name.operator String() + "()");
  1610. }
  1611. }
  1612. }
  1613. }
  1614. // Parents
  1615. GDScriptCompletionIdentifier base_type;
  1616. base_type.type = p_context._class->base_type;
  1617. base_type.type.is_meta_type = p_static;
  1618. base_type.value = p_context.base;
  1619. GDScriptCompletionContext c = p_context;
  1620. c.block = NULL;
  1621. c.function = NULL;
  1622. _find_identifiers_in_base(c, base_type, p_only_functions, r_result);
  1623. }
  1624. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Set<String> &r_result) {
  1625. GDScriptParser::DataType base_type = p_base.type;
  1626. bool _static = base_type.is_meta_type;
  1627. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  1628. r_result.insert("new(");
  1629. }
  1630. while (base_type.has_type) {
  1631. switch (base_type.kind) {
  1632. case GDScriptParser::DataType::CLASS: {
  1633. GDScriptCompletionContext c = p_context;
  1634. c._class = base_type.class_type;
  1635. c.block = NULL;
  1636. c.function = NULL;
  1637. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1638. base_type = base_type.class_type->base_type;
  1639. } break;
  1640. case GDScriptParser::DataType::GDSCRIPT: {
  1641. Ref<GDScript> script = base_type.script_type;
  1642. if (script.is_valid()) {
  1643. if (!_static && !p_only_functions) {
  1644. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1645. r_result.insert(E->get().operator String());
  1646. }
  1647. }
  1648. if (!p_only_functions) {
  1649. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1650. r_result.insert(E->key().operator String());
  1651. }
  1652. }
  1653. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1654. if (!_static || E->get()->is_static()) {
  1655. if (E->get()->get_argument_count()) {
  1656. r_result.insert(E->key().operator String() + "(");
  1657. } else {
  1658. r_result.insert(E->key().operator String() + "()");
  1659. }
  1660. }
  1661. }
  1662. if (!p_only_functions) {
  1663. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1664. r_result.insert(E->key().operator String());
  1665. }
  1666. }
  1667. base_type = GDScriptParser::DataType();
  1668. if (script->get_base().is_valid()) {
  1669. base_type.has_type = true;
  1670. base_type.kind = GDScriptParser::DataType::GDSCRIPT;
  1671. base_type.script_type = script->get_base();
  1672. } else {
  1673. base_type.has_type = script->get_instance_base_type() != StringName();
  1674. base_type.kind = GDScriptParser::DataType::NATIVE;
  1675. base_type.script_type = script->get_instance_base_type();
  1676. }
  1677. } else {
  1678. return;
  1679. }
  1680. } break;
  1681. case GDScriptParser::DataType::SCRIPT: {
  1682. Ref<Script> scr = base_type.script_type;
  1683. if (scr.is_valid()) {
  1684. if (!_static && !p_only_functions) {
  1685. List<PropertyInfo> members;
  1686. scr->get_script_property_list(&members);
  1687. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1688. r_result.insert(E->get().name);
  1689. }
  1690. }
  1691. if (!p_only_functions) {
  1692. Map<StringName, Variant> constants;
  1693. scr->get_constants(&constants);
  1694. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  1695. r_result.insert(E->key().operator String());
  1696. }
  1697. }
  1698. List<MethodInfo> methods;
  1699. scr->get_script_method_list(&methods);
  1700. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1701. if (E->get().arguments.size()) {
  1702. r_result.insert(E->get().name + "(");
  1703. } else {
  1704. r_result.insert(E->get().name + "()");
  1705. }
  1706. }
  1707. Ref<Script> base_script = scr->get_base_script();
  1708. if (base_script.is_valid()) {
  1709. base_type.script_type = base_script;
  1710. } else {
  1711. base_type.kind = GDScriptParser::DataType::NATIVE;
  1712. base_type.native_type = scr->get_instance_base_type();
  1713. }
  1714. } else {
  1715. return;
  1716. }
  1717. } break;
  1718. case GDScriptParser::DataType::NATIVE: {
  1719. StringName type = base_type.native_type;
  1720. if (!ClassDB::class_exists(type)) {
  1721. type = String("_") + type;
  1722. if (!ClassDB::class_exists(type)) {
  1723. return;
  1724. }
  1725. }
  1726. if (!p_only_functions) {
  1727. List<String> constants;
  1728. ClassDB::get_integer_constant_list(type, &constants);
  1729. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1730. r_result.insert(E->get());
  1731. }
  1732. if (!_static) {
  1733. List<PropertyInfo> pinfo;
  1734. ClassDB::get_property_list(type, &pinfo);
  1735. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1736. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1737. continue;
  1738. }
  1739. if (E->get().name.find("/") != -1) {
  1740. continue;
  1741. }
  1742. r_result.insert(E->get().name);
  1743. }
  1744. }
  1745. }
  1746. if (!_static) {
  1747. List<MethodInfo> methods;
  1748. bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
  1749. ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
  1750. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1751. if (E->get().name.begins_with("_")) {
  1752. continue;
  1753. }
  1754. if (E->get().arguments.size()) {
  1755. r_result.insert(E->get().name + "(");
  1756. } else {
  1757. r_result.insert(E->get().name + "()");
  1758. }
  1759. }
  1760. }
  1761. return;
  1762. } break;
  1763. case GDScriptParser::DataType::BUILTIN: {
  1764. Variant::CallError err;
  1765. Variant tmp = Variant::construct(base_type.builtin_type, NULL, 0, err);
  1766. if (err.error != Variant::CallError::CALL_OK) {
  1767. return;
  1768. }
  1769. if (!p_only_functions) {
  1770. List<PropertyInfo> members;
  1771. tmp.get_property_list(&members);
  1772. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1773. if (String(E->get().name).find("/") == -1) {
  1774. r_result.insert(E->get().name);
  1775. }
  1776. }
  1777. }
  1778. List<MethodInfo> methods;
  1779. tmp.get_method_list(&methods);
  1780. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1781. if (E->get().arguments.size()) {
  1782. r_result.insert(E->get().name + "(");
  1783. } else {
  1784. r_result.insert(E->get().name + "()");
  1785. }
  1786. }
  1787. return;
  1788. } break;
  1789. default: {
  1790. return;
  1791. } break;
  1792. }
  1793. }
  1794. }
  1795. static void _find_identifiers(const GDScriptCompletionContext &p_context, bool p_only_functions, Set<String> &r_result) {
  1796. const GDScriptParser::BlockNode *block = p_context.block;
  1797. if (p_context.function) {
  1798. const GDScriptParser::FunctionNode *f = p_context.function;
  1799. for (int i = 0; i < f->arguments.size(); i++) {
  1800. r_result.insert(f->arguments[i].operator String());
  1801. }
  1802. }
  1803. if (!p_only_functions && block) {
  1804. GDScriptCompletionContext c = p_context;
  1805. c.block = block;
  1806. _find_identifiers_in_block(c, r_result);
  1807. }
  1808. const GDScriptParser::ClassNode *clss = p_context._class;
  1809. bool _static = !p_context.function || p_context.function->_static;
  1810. while (clss) {
  1811. GDScriptCompletionContext c = p_context;
  1812. c._class = clss;
  1813. c.block = NULL;
  1814. c.function = NULL;
  1815. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1816. _static = true;
  1817. clss = clss->owner;
  1818. }
  1819. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1820. MethodInfo mi = GDScriptFunctions::get_info(GDScriptFunctions::Function(i));
  1821. if (mi.arguments.size() || (mi.flags & METHOD_FLAG_VARARG)) {
  1822. r_result.insert(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))) + "(");
  1823. } else {
  1824. r_result.insert(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))) + "()");
  1825. }
  1826. }
  1827. static const char *_type_names[Variant::VARIANT_MAX] = {
  1828. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1829. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1830. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1831. };
  1832. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1833. r_result.insert(_type_names[i]);
  1834. }
  1835. static const char *_keywords[] = {
  1836. "and", "in", "not", "or", "false", "PI", "TAU", "INF", "NAN", "self", "true", "as", "assert",
  1837. "breakpoint", "class", "extends", "is", "func", "preload", "setget", "signal", "tool", "yield",
  1838. "const", "enum", "export", "onready", "static", "var", "break", "continue", "if", "elif",
  1839. "else", "for", "pass", "return", "match", "while", "remote", "sync", "master", "puppet", "slave",
  1840. "remotesync", "mastersync", "puppetsync",
  1841. 0
  1842. };
  1843. const char **kw = _keywords;
  1844. while (*kw) {
  1845. r_result.insert(*kw);
  1846. kw++;
  1847. }
  1848. // Autoload singletons
  1849. List<PropertyInfo> props;
  1850. ProjectSettings::get_singleton()->get_property_list(&props);
  1851. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1852. String s = E->get().name;
  1853. if (!s.begins_with("autoload/")) {
  1854. continue;
  1855. }
  1856. String path = ProjectSettings::get_singleton()->get(s);
  1857. if (path.begins_with("*")) {
  1858. r_result.insert(s.get_slice("/", 1));
  1859. }
  1860. }
  1861. // Named scripts
  1862. List<StringName> named_scripts;
  1863. ScriptServer::get_global_class_list(&named_scripts);
  1864. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  1865. r_result.insert(E->get().operator String());
  1866. }
  1867. // Native classes
  1868. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1869. r_result.insert(E->key().operator String());
  1870. }
  1871. }
  1872. static void _find_call_arguments(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Set<String> &r_result, String &r_arghint) {
  1873. Variant base = p_base.value;
  1874. GDScriptParser::DataType base_type = p_base.type;
  1875. bool _static = false;
  1876. while (base_type.has_type) {
  1877. switch (base_type.kind) {
  1878. case GDScriptParser::DataType::CLASS: {
  1879. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  1880. if (base_type.class_type->static_functions[i]->name == p_method) {
  1881. r_arghint = _make_arguments_hint(base_type.class_type->static_functions[i], p_argidx);
  1882. return;
  1883. }
  1884. }
  1885. if (!_static) {
  1886. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  1887. if (base_type.class_type->functions[i]->name == p_method) {
  1888. r_arghint = _make_arguments_hint(base_type.class_type->functions[i], p_argidx);
  1889. return;
  1890. }
  1891. }
  1892. }
  1893. if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
  1894. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  1895. r_result.insert("\"" + base_type.class_type->_signals[i].name.operator String() + "\"");
  1896. }
  1897. }
  1898. base_type = base_type.class_type->base_type;
  1899. } break;
  1900. case GDScriptParser::DataType::GDSCRIPT: {
  1901. Ref<GDScript> gds = base_type.script_type;
  1902. if (gds.is_valid()) {
  1903. if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
  1904. List<MethodInfo> signals;
  1905. gds->get_script_signal_list(&signals);
  1906. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  1907. r_result.insert("\"" + E->get().name + "\"");
  1908. }
  1909. }
  1910. Ref<GDScript> base_script = gds->get_base_script();
  1911. if (base_script.is_valid()) {
  1912. base_type.script_type = base_script;
  1913. } else {
  1914. base_type.kind = GDScriptParser::DataType::NATIVE;
  1915. base_type.native_type = gds->get_instance_base_type();
  1916. }
  1917. } else {
  1918. return;
  1919. }
  1920. } break;
  1921. case GDScriptParser::DataType::NATIVE: {
  1922. StringName class_name = base_type.native_type;
  1923. if (!ClassDB::class_exists(class_name)) {
  1924. class_name = String("_") + class_name;
  1925. if (!ClassDB::class_exists(class_name)) {
  1926. base_type.has_type = false;
  1927. break;
  1928. }
  1929. }
  1930. List<MethodInfo> methods;
  1931. ClassDB::get_method_list(class_name, &methods);
  1932. ClassDB::get_virtual_methods(class_name, &methods);
  1933. int method_args = 0;
  1934. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1935. if (E->get().name == p_method) {
  1936. method_args = E->get().arguments.size();
  1937. if (base.get_type() == Variant::OBJECT) {
  1938. Object *obj = base.operator Object *();
  1939. if (obj) {
  1940. List<String> options;
  1941. obj->get_argument_options(p_method, p_argidx, &options);
  1942. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1943. r_result.insert(E->get());
  1944. }
  1945. }
  1946. }
  1947. if (p_argidx < method_args) {
  1948. PropertyInfo arg_info = E->get().arguments[p_argidx];
  1949. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1950. _find_enumeration_candidates(arg_info.class_name, r_result);
  1951. }
  1952. }
  1953. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  1954. break;
  1955. }
  1956. }
  1957. if ((p_method == "connect" || p_method == "emit_signal") && p_argidx == 0) {
  1958. List<MethodInfo> signals;
  1959. ClassDB::get_signal_list(class_name, &signals);
  1960. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  1961. r_result.insert("\"" + E->get().name + "\"");
  1962. }
  1963. }
  1964. if (ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node") && p_argidx == 0) {
  1965. // Get autoloads
  1966. List<PropertyInfo> props;
  1967. ProjectSettings::get_singleton()->get_property_list(&props);
  1968. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1969. String s = E->get().name;
  1970. if (!s.begins_with("autoload/")) {
  1971. continue;
  1972. }
  1973. String name = s.get_slice("/", 1);
  1974. r_result.insert("\"/root/" + name + "\"");
  1975. }
  1976. }
  1977. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
  1978. // Get input actions
  1979. List<PropertyInfo> props;
  1980. ProjectSettings::get_singleton()->get_property_list(&props);
  1981. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1982. String s = E->get().name;
  1983. if (!s.begins_with("input/")) {
  1984. continue;
  1985. }
  1986. String name = s.get_slice("/", 1);
  1987. r_result.insert("\"" + name + "\"");
  1988. }
  1989. }
  1990. base_type.has_type = false;
  1991. } break;
  1992. case GDScriptParser::DataType::BUILTIN: {
  1993. if (base.get_type() == Variant::NIL) {
  1994. Variant::CallError err;
  1995. base = Variant::construct(base_type.builtin_type, NULL, 0, err);
  1996. if (err.error != Variant::CallError::CALL_OK) {
  1997. return;
  1998. }
  1999. }
  2000. List<MethodInfo> methods;
  2001. base.get_method_list(&methods);
  2002. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2003. if (E->get().name == p_method) {
  2004. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2005. return;
  2006. }
  2007. }
  2008. base_type.has_type = false;
  2009. } break;
  2010. default: {
  2011. base_type.has_type = false;
  2012. } break;
  2013. }
  2014. }
  2015. }
  2016. static void _find_call_arguments(const GDScriptCompletionContext &p_context, const GDScriptParser::Node *p_node, int p_argidx, Set<String> &r_result, bool &r_forced, String &r_arghint) {
  2017. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2018. return;
  2019. }
  2020. Variant base;
  2021. GDScriptParser::DataType base_type;
  2022. StringName function;
  2023. bool _static = false;
  2024. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  2025. GDScriptCompletionIdentifier connect_base;
  2026. if (op->op != GDScriptParser::OperatorNode::OP_CALL && op->op != GDScriptParser::OperatorNode::OP_PARENT_CALL) {
  2027. return;
  2028. }
  2029. if (!op->arguments.size()) {
  2030. return;
  2031. }
  2032. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  2033. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  2034. // Complete built-in function
  2035. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  2036. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  2037. if ((mi.name == "load" || mi.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2038. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2039. }
  2040. r_arghint = _make_arguments_hint(mi, p_argidx);
  2041. return;
  2042. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  2043. // Complete constructor
  2044. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  2045. List<MethodInfo> constructors;
  2046. Variant::get_constructor_list(tn->vtype, &constructors);
  2047. int i = 0;
  2048. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2049. if (p_argidx >= E->get().arguments.size()) {
  2050. continue;
  2051. }
  2052. if (i > 0) {
  2053. r_arghint += "\n";
  2054. }
  2055. r_arghint += _make_arguments_hint(E->get(), p_argidx);
  2056. i++;
  2057. }
  2058. return;
  2059. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  2060. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2061. return;
  2062. }
  2063. base = p_context.base;
  2064. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2065. function = id->name;
  2066. base_type.has_type = true;
  2067. base_type.kind = GDScriptParser::DataType::CLASS;
  2068. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2069. _static = p_context.function && p_context.function->_static;
  2070. if (function == "connect" && op->arguments.size() >= 4) {
  2071. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2072. }
  2073. } else {
  2074. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2075. return;
  2076. }
  2077. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2078. function = id->name;
  2079. GDScriptCompletionIdentifier ci;
  2080. if (_guess_expression_type(p_context, op->arguments[0], ci)) {
  2081. base_type = ci.type;
  2082. base = ci.value;
  2083. } else {
  2084. return;
  2085. }
  2086. _static = ci.type.is_meta_type;
  2087. if (function == "connect" && op->arguments.size() >= 4) {
  2088. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2089. }
  2090. }
  2091. } else {
  2092. if (!p_context._class || op->arguments.size() < 1 || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2093. return;
  2094. }
  2095. base_type.has_type = true;
  2096. base_type.kind = GDScriptParser::DataType::CLASS;
  2097. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2098. base_type.is_meta_type = p_context.function && p_context.function->_static;
  2099. base = p_context.base;
  2100. function = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  2101. if (function == "connect" && op->arguments.size() >= 4) {
  2102. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2103. }
  2104. }
  2105. GDScriptCompletionIdentifier ci;
  2106. ci.type = base_type;
  2107. ci.value = base;
  2108. _find_call_arguments(p_context, ci, function, p_argidx, _static, r_result, r_arghint);
  2109. if (function == "connect" && p_argidx == 2) {
  2110. Set<String> methods;
  2111. _find_identifiers_in_base(p_context, connect_base, true, methods);
  2112. for (Set<String>::Element *E = methods.front(); E; E = E->next()) {
  2113. r_result.insert("\"" + E->get().replace("(", "").replace(")", "") + "\"");
  2114. }
  2115. }
  2116. r_forced = r_result.size() > 0;
  2117. }
  2118. 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) {
  2119. GDScriptParser parser;
  2120. parser.parse(p_code, p_base_path, false, "", true);
  2121. r_forced = false;
  2122. Set<String> options;
  2123. GDScriptCompletionContext context;
  2124. context._class = parser.get_completion_class();
  2125. context.block = parser.get_completion_block();
  2126. context.function = parser.get_completion_function();
  2127. context.line = parser.get_completion_line();
  2128. if (!context._class || context._class->owner == NULL) {
  2129. context.base = p_owner;
  2130. context.base_path = p_base_path;
  2131. }
  2132. bool is_function = false;
  2133. switch (parser.get_completion_type()) {
  2134. case GDScriptParser::COMPLETION_NONE: {
  2135. } break;
  2136. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2137. List<StringName> constants;
  2138. Variant::get_constants_for_type(parser.get_completion_built_in_constant(), &constants);
  2139. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  2140. options.insert(E->get().operator String());
  2141. }
  2142. } break;
  2143. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2144. _find_identifiers_in_class(context, !context.function || context.function->_static, true, true, options);
  2145. } break;
  2146. case GDScriptParser::COMPLETION_FUNCTION: {
  2147. is_function = true;
  2148. } // fallthrough
  2149. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2150. _find_identifiers(context, is_function, options);
  2151. } break;
  2152. case GDScriptParser::COMPLETION_GET_NODE: {
  2153. if (p_owner) {
  2154. List<String> opts;
  2155. p_owner->get_argument_options("get_node", 0, &opts);
  2156. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  2157. String opt = E->get().strip_edges();
  2158. if (opt.is_quoted()) {
  2159. r_forced = true;
  2160. String idopt = opt.unquote();
  2161. if (idopt.replace("/", "_").is_valid_identifier()) {
  2162. options.insert(idopt);
  2163. } else {
  2164. options.insert(opt);
  2165. }
  2166. }
  2167. }
  2168. // Get autoloads
  2169. List<PropertyInfo> props;
  2170. ProjectSettings::get_singleton()->get_property_list(&props);
  2171. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2172. String s = E->get().name;
  2173. if (!s.begins_with("autoload/")) {
  2174. continue;
  2175. }
  2176. String name = s.get_slice("/", 1);
  2177. options.insert("\"/root/" + name + "\"");
  2178. }
  2179. }
  2180. } break;
  2181. case GDScriptParser::COMPLETION_METHOD: {
  2182. is_function = true;
  2183. } // fallthrough
  2184. case GDScriptParser::COMPLETION_INDEX: {
  2185. const GDScriptParser::Node *node = parser.get_completion_node();
  2186. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2187. break;
  2188. }
  2189. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(node);
  2190. if (op->arguments.size() < 1) {
  2191. break;
  2192. }
  2193. GDScriptCompletionIdentifier base;
  2194. if (!_guess_expression_type(context, op->arguments[0], base)) {
  2195. break;
  2196. }
  2197. GDScriptCompletionContext c = context;
  2198. c.function = NULL;
  2199. c.block = NULL;
  2200. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : NULL;
  2201. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2202. c._class = base.type.class_type;
  2203. } else {
  2204. c._class = NULL;
  2205. }
  2206. _find_identifiers_in_base(c, base, is_function, options);
  2207. } break;
  2208. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2209. _find_call_arguments(context, parser.get_completion_node(), parser.get_completion_argument_index(), options, r_forced, r_call_hint);
  2210. } break;
  2211. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2212. GDScriptParser::DataType native_type = context._class->base_type;
  2213. while (native_type.has_type && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2214. switch (native_type.kind) {
  2215. case GDScriptParser::DataType::CLASS: {
  2216. native_type = native_type.class_type->base_type;
  2217. } break;
  2218. case GDScriptParser::DataType::GDSCRIPT: {
  2219. Ref<GDScript> gds = native_type.script_type;
  2220. if (gds.is_valid()) {
  2221. Ref<GDScript> base = gds->get_base_script();
  2222. if (base.is_valid()) {
  2223. native_type.script_type = base;
  2224. } else {
  2225. native_type.native_type = gds->get_instance_base_type();
  2226. native_type.kind = GDScriptParser::DataType::NATIVE;
  2227. }
  2228. } else {
  2229. native_type.has_type = false;
  2230. }
  2231. } break;
  2232. default: {
  2233. native_type.has_type = false;
  2234. } break;
  2235. }
  2236. }
  2237. if (!native_type.has_type) {
  2238. break;
  2239. }
  2240. StringName class_name = native_type.native_type;
  2241. if (!ClassDB::class_exists(class_name)) {
  2242. class_name = String("_") + class_name;
  2243. if (!ClassDB::class_exists(class_name)) {
  2244. break;
  2245. }
  2246. }
  2247. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2248. List<MethodInfo> virtual_methods;
  2249. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2250. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2251. MethodInfo &mi = E->get();
  2252. String method_hint = mi.name;
  2253. if (method_hint.find(":") != -1) {
  2254. method_hint = method_hint.get_slice(":", 0);
  2255. }
  2256. method_hint += "(";
  2257. if (mi.arguments.size()) {
  2258. for (int i = 0; i < mi.arguments.size(); i++) {
  2259. if (i > 0) {
  2260. method_hint += ", ";
  2261. }
  2262. String arg = mi.arguments[i].name;
  2263. if (arg.find(":") != -1) {
  2264. arg = arg.substr(0, arg.find(":"));
  2265. }
  2266. method_hint += arg;
  2267. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2268. method_hint += ": ";
  2269. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2270. method_hint += mi.arguments[i].class_name.operator String();
  2271. } else {
  2272. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2273. }
  2274. }
  2275. }
  2276. }
  2277. method_hint += ")";
  2278. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2279. method_hint += " -> ";
  2280. if (mi.return_val.type == Variant::NIL) {
  2281. method_hint += "void";
  2282. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2283. method_hint += mi.return_val.class_name.operator String();
  2284. } else {
  2285. method_hint += Variant::get_type_name(mi.return_val.type);
  2286. }
  2287. }
  2288. method_hint += ":";
  2289. options.insert(method_hint);
  2290. }
  2291. } break;
  2292. case GDScriptParser::COMPLETION_YIELD: {
  2293. const GDScriptParser::Node *node = parser.get_completion_node();
  2294. GDScriptCompletionContext c = context;
  2295. c.line = node->line;
  2296. GDScriptCompletionIdentifier type;
  2297. if (!_guess_expression_type(c, node, type)) {
  2298. break;
  2299. }
  2300. GDScriptParser::DataType base_type = type.type;
  2301. while (base_type.has_type) {
  2302. switch (base_type.kind) {
  2303. case GDScriptParser::DataType::CLASS: {
  2304. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  2305. options.insert("\"" + base_type.class_type->_signals[i].name.operator String() + "\"");
  2306. }
  2307. base_type = base_type.class_type->base_type;
  2308. } break;
  2309. case GDScriptParser::DataType::SCRIPT:
  2310. case GDScriptParser::DataType::GDSCRIPT: {
  2311. Ref<Script> scr = base_type.script_type;
  2312. if (scr.is_valid()) {
  2313. List<MethodInfo> signals;
  2314. scr->get_script_signal_list(&signals);
  2315. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2316. options.insert("\"" + E->get().name + "\"");
  2317. }
  2318. Ref<Script> base_script = scr->get_base_script();
  2319. if (base_script.is_valid()) {
  2320. base_type.script_type = base_script;
  2321. } else {
  2322. base_type.kind = GDScriptParser::DataType::NATIVE;
  2323. base_type.native_type = scr->get_instance_base_type();
  2324. }
  2325. } else {
  2326. base_type.has_type = false;
  2327. }
  2328. } break;
  2329. case GDScriptParser::DataType::NATIVE: {
  2330. base_type.has_type = false;
  2331. StringName class_name = base_type.native_type;
  2332. if (!ClassDB::class_exists(class_name)) {
  2333. class_name = String("_") + class_name;
  2334. if (!ClassDB::class_exists(class_name)) {
  2335. break;
  2336. }
  2337. }
  2338. List<MethodInfo> signals;
  2339. ClassDB::get_signal_list(class_name, &signals);
  2340. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2341. options.insert("\"" + E->get().name + "\"");
  2342. }
  2343. } break;
  2344. default: {
  2345. base_type.has_type = false;
  2346. }
  2347. }
  2348. }
  2349. } break;
  2350. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2351. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2352. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2353. r_forced = true;
  2354. }
  2355. } break;
  2356. case GDScriptParser::COMPLETION_ASSIGN: {
  2357. GDScriptCompletionIdentifier type;
  2358. if (!_guess_expression_type(context, parser.get_completion_node(), type)) {
  2359. break;
  2360. }
  2361. if (!type.enumeration.empty()) {
  2362. _find_enumeration_candidates(type.enumeration, options);
  2363. r_forced = options.size() > 0;
  2364. }
  2365. } break;
  2366. case GDScriptParser::COMPLETION_TYPE_HINT: {
  2367. const GDScriptParser::ClassNode *clss = context._class;
  2368. while (clss) {
  2369. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = clss->constant_expressions.front(); E; E = E->next()) {
  2370. GDScriptCompletionIdentifier constant;
  2371. GDScriptCompletionContext c = context;
  2372. c.function = NULL;
  2373. c.block = NULL;
  2374. c.line = E->value().expression->line;
  2375. if (_guess_expression_type(c, E->value().expression, constant)) {
  2376. if (constant.type.has_type && constant.type.is_meta_type) {
  2377. options.insert(E->key().operator String());
  2378. }
  2379. }
  2380. }
  2381. for (int i = 0; i < clss->subclasses.size(); i++) {
  2382. if (clss->subclasses[i]->name != StringName()) {
  2383. options.insert(clss->subclasses[i]->name.operator String());
  2384. }
  2385. }
  2386. clss = clss->owner;
  2387. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2388. options.insert(Variant::get_type_name((Variant::Type)i));
  2389. }
  2390. }
  2391. List<StringName> native_classes;
  2392. ClassDB::get_class_list(&native_classes);
  2393. for (List<StringName>::Element *E = native_classes.front(); E; E = E->next()) {
  2394. String class_name = E->get().operator String();
  2395. if (class_name.begins_with("_")) {
  2396. class_name = class_name.right(1);
  2397. }
  2398. if (Engine::get_singleton()->has_singleton(class_name)) {
  2399. continue;
  2400. }
  2401. options.insert(class_name);
  2402. }
  2403. // Named scripts
  2404. List<StringName> named_scripts;
  2405. ScriptServer::get_global_class_list(&named_scripts);
  2406. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  2407. options.insert(E->get().operator String());
  2408. }
  2409. if (parser.get_completion_identifier_is_function()) {
  2410. options.insert("void");
  2411. }
  2412. r_forced = true;
  2413. } break;
  2414. case GDScriptParser::COMPLETION_TYPE_HINT_INDEX: {
  2415. GDScriptCompletionIdentifier base;
  2416. String index = parser.get_completion_cursor().operator String();
  2417. if (!_guess_identifier_type(context, index.get_slice(".", 0), base)) {
  2418. break;
  2419. }
  2420. GDScriptCompletionContext c = context;
  2421. c._class = NULL;
  2422. c.function = NULL;
  2423. c.block = NULL;
  2424. bool finding = true;
  2425. index = index.right(index.find(".") + 1);
  2426. while (index.find(".") != -1) {
  2427. String id = index.get_slice(".", 0);
  2428. GDScriptCompletionIdentifier sub_base;
  2429. if (!_guess_identifier_type_from_base(c, base, id, sub_base)) {
  2430. finding = false;
  2431. break;
  2432. }
  2433. index = index.right(index.find(".") + 1);
  2434. base = sub_base;
  2435. }
  2436. if (!finding) {
  2437. break;
  2438. }
  2439. GDScriptParser::DataType base_type = base.type;
  2440. while (base_type.has_type) {
  2441. switch (base_type.kind) {
  2442. case GDScriptParser::DataType::CLASS: {
  2443. if (base_type.class_type) {
  2444. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = base_type.class_type->constant_expressions.front(); E; E = E->next()) {
  2445. GDScriptCompletionIdentifier constant;
  2446. GDScriptCompletionContext c = context;
  2447. c._class = base_type.class_type;
  2448. c.function = NULL;
  2449. c.block = NULL;
  2450. c.line = E->value().expression->line;
  2451. if (_guess_expression_type(c, E->value().expression, constant)) {
  2452. if (constant.type.has_type && constant.type.is_meta_type) {
  2453. options.insert(E->key().operator String());
  2454. }
  2455. }
  2456. }
  2457. for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
  2458. if (base_type.class_type->subclasses[i]->name != StringName()) {
  2459. options.insert(base_type.class_type->subclasses[i]->name.operator String());
  2460. }
  2461. }
  2462. base_type = base_type.class_type->base_type;
  2463. } else {
  2464. base_type.has_type = false;
  2465. }
  2466. } break;
  2467. case GDScriptParser::DataType::SCRIPT:
  2468. case GDScriptParser::DataType::GDSCRIPT: {
  2469. Ref<Script> scr = base_type.script_type;
  2470. if (scr.is_valid()) {
  2471. Map<StringName, Variant> constants;
  2472. scr->get_constants(&constants);
  2473. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  2474. Ref<Script> const_scr = E->value();
  2475. if (const_scr.is_valid()) {
  2476. options.insert(E->key().operator String());
  2477. }
  2478. }
  2479. Ref<Script> base_script = scr->get_base_script();
  2480. if (base_script.is_valid()) {
  2481. base_type.script_type = base_script;
  2482. } else {
  2483. base_type.has_type = false;
  2484. }
  2485. } else {
  2486. base_type.has_type = false;
  2487. }
  2488. } break;
  2489. default: {
  2490. base_type.has_type = false;
  2491. } break;
  2492. }
  2493. }
  2494. r_forced = options.size() > 0;
  2495. } break;
  2496. }
  2497. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  2498. r_options->push_back(E->get());
  2499. }
  2500. return OK;
  2501. }
  2502. #else
  2503. 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) {
  2504. return OK;
  2505. }
  2506. #endif
  2507. //////// END COMPLETION //////////
  2508. String GDScriptLanguage::_get_indentation() const {
  2509. #ifdef TOOLS_ENABLED
  2510. if (Engine::get_singleton()->is_editor_hint()) {
  2511. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  2512. if (use_space_indentation) {
  2513. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  2514. String space_indent = "";
  2515. for (int i = 0; i < indent_size; i++) {
  2516. space_indent += " ";
  2517. }
  2518. return space_indent;
  2519. }
  2520. }
  2521. #endif
  2522. return "\t";
  2523. }
  2524. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2525. String indent = _get_indentation();
  2526. Vector<String> lines = p_code.split("\n");
  2527. List<int> indent_stack;
  2528. for (int i = 0; i < lines.size(); i++) {
  2529. String l = lines[i];
  2530. int tc = 0;
  2531. for (int j = 0; j < l.length(); j++) {
  2532. if (l[j] == ' ' || l[j] == '\t') {
  2533. tc++;
  2534. } else {
  2535. break;
  2536. }
  2537. }
  2538. String st = l.substr(tc, l.length()).strip_edges();
  2539. if (st == "" || st.begins_with("#"))
  2540. continue; //ignore!
  2541. int ilevel = 0;
  2542. if (indent_stack.size()) {
  2543. ilevel = indent_stack.back()->get();
  2544. }
  2545. if (tc > ilevel) {
  2546. indent_stack.push_back(tc);
  2547. } else if (tc < ilevel) {
  2548. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2549. indent_stack.pop_back();
  2550. }
  2551. if (indent_stack.size() && indent_stack.back()->get() != tc)
  2552. indent_stack.push_back(tc); //this is not right but gets the job done
  2553. }
  2554. if (i >= p_from_line) {
  2555. l = "";
  2556. for (int j = 0; j < indent_stack.size(); j++) {
  2557. l += indent;
  2558. }
  2559. l += st;
  2560. } else if (i > p_to_line) {
  2561. break;
  2562. }
  2563. lines.write[i] = l;
  2564. }
  2565. p_code = "";
  2566. for (int i = 0; i < lines.size(); i++) {
  2567. if (i > 0)
  2568. p_code += "\n";
  2569. p_code += lines[i];
  2570. }
  2571. }
  2572. #ifdef TOOLS_ENABLED
  2573. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2574. GDScriptParser::DataType base_type = p_base;
  2575. while (base_type.has_type) {
  2576. switch (base_type.kind) {
  2577. case GDScriptParser::DataType::CLASS: {
  2578. if (base_type.class_type) {
  2579. if (p_is_function) {
  2580. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  2581. if (base_type.class_type->functions[i]->name == p_symbol) {
  2582. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2583. r_result.location = base_type.class_type->functions[i]->line;
  2584. return OK;
  2585. }
  2586. }
  2587. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  2588. if (base_type.class_type->static_functions[i]->name == p_symbol) {
  2589. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2590. r_result.location = base_type.class_type->static_functions[i]->line;
  2591. return OK;
  2592. }
  2593. }
  2594. } else {
  2595. if (base_type.class_type->constant_expressions.has(p_symbol)) {
  2596. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2597. r_result.location = base_type.class_type->constant_expressions[p_symbol].expression->line;
  2598. return OK;
  2599. }
  2600. for (int i = 0; i < base_type.class_type->variables.size(); i++) {
  2601. if (base_type.class_type->variables[i].identifier == p_symbol) {
  2602. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2603. r_result.location = base_type.class_type->variables[i].line;
  2604. return OK;
  2605. }
  2606. }
  2607. }
  2608. }
  2609. base_type = base_type.class_type->base_type;
  2610. } break;
  2611. case GDScriptParser::DataType::SCRIPT:
  2612. case GDScriptParser::DataType::GDSCRIPT: {
  2613. Ref<Script> scr = base_type.script_type;
  2614. if (scr.is_valid()) {
  2615. int line = scr->get_member_line(p_symbol);
  2616. if (line >= 0) {
  2617. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2618. r_result.location = line;
  2619. r_result.script = scr;
  2620. return OK;
  2621. }
  2622. Ref<Script> base_script = scr->get_base_script();
  2623. if (base_script.is_valid()) {
  2624. base_type.script_type = base_script;
  2625. } else {
  2626. base_type.kind = GDScriptParser::DataType::NATIVE;
  2627. base_type.native_type = scr->get_instance_base_type();
  2628. }
  2629. } else {
  2630. base_type.has_type = false;
  2631. }
  2632. } break;
  2633. case GDScriptParser::DataType::NATIVE: {
  2634. StringName class_name = base_type.native_type;
  2635. if (!ClassDB::class_exists(class_name)) {
  2636. class_name = String("_") + class_name;
  2637. if (!ClassDB::class_exists(class_name)) {
  2638. base_type.has_type = false;
  2639. break;
  2640. }
  2641. }
  2642. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2643. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2644. r_result.class_name = base_type.native_type;
  2645. r_result.class_member = p_symbol;
  2646. return OK;
  2647. }
  2648. List<MethodInfo> virtual_methods;
  2649. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2650. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2651. if (E->get().name == p_symbol) {
  2652. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2653. r_result.class_name = base_type.native_type;
  2654. r_result.class_member = p_symbol;
  2655. return OK;
  2656. }
  2657. }
  2658. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2659. if (enum_name != StringName()) {
  2660. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2661. r_result.class_name = base_type.native_type;
  2662. r_result.class_member = enum_name;
  2663. return OK;
  2664. }
  2665. List<String> constants;
  2666. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2667. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2668. if (E->get() == p_symbol) {
  2669. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2670. r_result.class_name = base_type.native_type;
  2671. r_result.class_member = p_symbol;
  2672. return OK;
  2673. }
  2674. }
  2675. List<PropertyInfo> properties;
  2676. ClassDB::get_property_list(class_name, &properties, true);
  2677. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  2678. if (E->get().name == p_symbol) {
  2679. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2680. r_result.class_name = base_type.native_type;
  2681. r_result.class_member = p_symbol;
  2682. return OK;
  2683. }
  2684. }
  2685. StringName parent = ClassDB::get_parent_class(class_name);
  2686. if (parent != StringName()) {
  2687. if (String(parent).begins_with("_")) {
  2688. base_type.native_type = String(parent).right(1);
  2689. } else {
  2690. base_type.native_type = parent;
  2691. }
  2692. } else {
  2693. base_type.has_type = false;
  2694. }
  2695. } break;
  2696. case GDScriptParser::DataType::BUILTIN: {
  2697. base_type.has_type = false;
  2698. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2699. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2700. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2701. r_result.class_member = p_symbol;
  2702. return OK;
  2703. }
  2704. Variant v;
  2705. REF v_ref;
  2706. if (base_type.builtin_type == Variant::OBJECT) {
  2707. v_ref.instance();
  2708. v = v_ref;
  2709. } else {
  2710. Variant::CallError err;
  2711. v = Variant::construct(base_type.builtin_type, NULL, 0, err);
  2712. if (err.error != Variant::CallError::CALL_OK) {
  2713. break;
  2714. }
  2715. }
  2716. if (v.has_method(p_symbol)) {
  2717. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2718. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2719. r_result.class_member = p_symbol;
  2720. return OK;
  2721. }
  2722. bool valid = false;
  2723. v.get(p_symbol, &valid);
  2724. if (valid) {
  2725. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2726. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2727. r_result.class_member = p_symbol;
  2728. return OK;
  2729. }
  2730. } break;
  2731. default: {
  2732. base_type.has_type = false;
  2733. } break;
  2734. }
  2735. }
  2736. return ERR_CANT_RESOLVE;
  2737. }
  2738. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
  2739. //before parsing, try the usual stuff
  2740. if (ClassDB::class_exists(p_symbol)) {
  2741. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2742. r_result.class_name = p_symbol;
  2743. return OK;
  2744. } else {
  2745. String under_prefix = "_" + p_symbol;
  2746. if (ClassDB::class_exists(under_prefix)) {
  2747. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2748. r_result.class_name = p_symbol;
  2749. return OK;
  2750. }
  2751. }
  2752. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2753. Variant::Type t = Variant::Type(i);
  2754. if (Variant::get_type_name(t) == p_symbol) {
  2755. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2756. r_result.class_name = Variant::get_type_name(t);
  2757. return OK;
  2758. }
  2759. }
  2760. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  2761. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  2762. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2763. r_result.class_name = "@GDScript";
  2764. r_result.class_member = p_symbol;
  2765. return OK;
  2766. }
  2767. }
  2768. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2769. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2770. r_result.class_name = "@GDScript";
  2771. r_result.class_member = p_symbol;
  2772. return OK;
  2773. }
  2774. GDScriptParser parser;
  2775. parser.parse(p_code, p_base_path, false, "", true);
  2776. if (parser.get_completion_type() == GDScriptParser::COMPLETION_NONE) {
  2777. return ERR_CANT_RESOLVE;
  2778. }
  2779. GDScriptCompletionContext context;
  2780. context._class = parser.get_completion_class();
  2781. context.function = parser.get_completion_function();
  2782. context.block = parser.get_completion_block();
  2783. context.line = parser.get_completion_line();
  2784. context.base = p_owner;
  2785. context.base_path = p_base_path;
  2786. if (context._class && context._class->extends_class.size() > 0) {
  2787. bool success = false;
  2788. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  2789. if (success) {
  2790. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2791. r_result.class_name = context._class->extends_class[0];
  2792. r_result.class_member = p_symbol;
  2793. return OK;
  2794. }
  2795. }
  2796. bool is_function = false;
  2797. switch (parser.get_completion_type()) {
  2798. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2799. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2800. r_result.class_name = Variant::get_type_name(parser.get_completion_built_in_constant());
  2801. r_result.class_member = p_symbol;
  2802. return OK;
  2803. } break;
  2804. case GDScriptParser::COMPLETION_PARENT_FUNCTION:
  2805. case GDScriptParser::COMPLETION_FUNCTION: {
  2806. is_function = true;
  2807. } // fallthrough
  2808. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2809. if (!is_function) {
  2810. is_function = parser.get_completion_identifier_is_function();
  2811. }
  2812. GDScriptParser::DataType base_type;
  2813. if (context._class) {
  2814. if (parser.get_completion_type() != GDScriptParser::COMPLETION_PARENT_FUNCTION) {
  2815. base_type.has_type = true;
  2816. base_type.kind = GDScriptParser::DataType::CLASS;
  2817. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
  2818. } else {
  2819. base_type = context._class->base_type;
  2820. }
  2821. } else {
  2822. break;
  2823. }
  2824. if (!is_function && context.block) {
  2825. // Lookup local variables
  2826. const GDScriptParser::BlockNode *block = context.block;
  2827. while (block) {
  2828. if (block->variables.has(p_symbol)) {
  2829. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2830. r_result.location = block->variables[p_symbol]->line;
  2831. return OK;
  2832. }
  2833. block = block->parent_block;
  2834. }
  2835. }
  2836. if (context.function && context.function->name != StringName()) {
  2837. // Lookup function arguments
  2838. for (int i = 0; i < context.function->arguments.size(); i++) {
  2839. if (context.function->arguments[i] == p_symbol) {
  2840. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2841. r_result.location = context.function->line;
  2842. return OK;
  2843. }
  2844. }
  2845. }
  2846. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2847. return OK;
  2848. }
  2849. if (!is_function) {
  2850. // Guess in autoloads as singletons
  2851. List<PropertyInfo> props;
  2852. ProjectSettings::get_singleton()->get_property_list(&props);
  2853. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2854. String s = E->get().name;
  2855. if (!s.begins_with("autoload/"))
  2856. continue;
  2857. String name = s.get_slice("/", 1);
  2858. if (name == String(p_symbol)) {
  2859. String path = ProjectSettings::get_singleton()->get(s);
  2860. if (path.begins_with("*")) {
  2861. String script = path.substr(1, path.length());
  2862. if (!script.ends_with(".gd")) {
  2863. // Not a script, try find the script anyway,
  2864. // may have some success
  2865. script = script.get_basename() + ".gd";
  2866. }
  2867. if (FileAccess::exists(script)) {
  2868. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2869. r_result.location = 0;
  2870. r_result.script = ResourceLoader::load(script);
  2871. return OK;
  2872. }
  2873. }
  2874. }
  2875. }
  2876. // Global
  2877. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2878. if (classes.has(p_symbol)) {
  2879. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2880. if (value.get_type() == Variant::OBJECT) {
  2881. Object *obj = value;
  2882. if (obj) {
  2883. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2884. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2885. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2886. } else {
  2887. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2888. r_result.class_name = obj->get_class();
  2889. }
  2890. // proxy class remove the underscore.
  2891. if (r_result.class_name.begins_with("_")) {
  2892. r_result.class_name = r_result.class_name.right(1);
  2893. }
  2894. return OK;
  2895. }
  2896. } else {
  2897. /*
  2898. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2899. // We cannot determine the exact nature of the identifier here
  2900. // Otherwise these codes would work
  2901. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2902. if (enumName != NULL) {
  2903. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2904. r_result.class_name = "@GlobalScope";
  2905. r_result.class_member = enumName;
  2906. return OK;
  2907. }
  2908. else {
  2909. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2910. r_result.class_name = "@GlobalScope";
  2911. r_result.class_member = p_symbol;
  2912. return OK;
  2913. }*/
  2914. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  2915. r_result.class_name = "@GlobalScope";
  2916. r_result.class_member = p_symbol;
  2917. return OK;
  2918. }
  2919. }
  2920. }
  2921. } break;
  2922. case GDScriptParser::COMPLETION_METHOD: {
  2923. is_function = true;
  2924. } // fallthrough
  2925. case GDScriptParser::COMPLETION_INDEX: {
  2926. const GDScriptParser::Node *node = parser.get_completion_node();
  2927. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2928. break;
  2929. }
  2930. GDScriptCompletionIdentifier base;
  2931. if (!_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], base)) {
  2932. break;
  2933. }
  2934. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  2935. return OK;
  2936. }
  2937. } break;
  2938. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2939. GDScriptParser::DataType base_type = context._class->base_type;
  2940. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2941. return OK;
  2942. }
  2943. } break;
  2944. default: {}
  2945. }
  2946. return ERR_CANT_RESOLVE;
  2947. }
  2948. #endif