gdscript_editor.cpp 109 KB

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