gdscript_editor.cpp 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637
  1. /**************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "gdscript_analyzer.h"
  32. #include "gdscript_parser.h"
  33. #include "gdscript_tokenizer.h"
  34. #include "gdscript_utility_functions.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/gdscript_docgen.h"
  37. #include "editor/script_templates/templates.gen.h"
  38. #endif
  39. #include "core/config/engine.h"
  40. #include "core/core_constants.h"
  41. #include "core/io/file_access.h"
  42. #include "core/math/expression.h"
  43. #include "core/variant/container_type_validate.h"
  44. #ifdef TOOLS_ENABLED
  45. #include "core/config/project_settings.h"
  46. #include "editor/editor_node.h"
  47. #include "editor/editor_string_names.h"
  48. #include "editor/file_system/editor_file_system.h"
  49. #include "editor/settings/editor_settings.h"
  50. #endif
  51. Vector<String> GDScriptLanguage::get_comment_delimiters() const {
  52. static const Vector<String> delimiters = { "#" };
  53. return delimiters;
  54. }
  55. Vector<String> GDScriptLanguage::get_doc_comment_delimiters() const {
  56. static const Vector<String> delimiters = { "##" };
  57. return delimiters;
  58. }
  59. Vector<String> GDScriptLanguage::get_string_delimiters() const {
  60. static const Vector<String> delimiters = {
  61. "\" \"",
  62. "' '",
  63. "\"\"\" \"\"\"",
  64. "''' '''",
  65. };
  66. // NOTE: StringName, NodePath and r-strings are not listed here.
  67. return delimiters;
  68. }
  69. bool GDScriptLanguage::is_using_templates() {
  70. return true;
  71. }
  72. Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
  73. Ref<GDScript> scr;
  74. scr.instantiate();
  75. String processed_template = p_template;
  76. #ifdef TOOLS_ENABLED
  77. const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  78. #else
  79. const bool type_hints = true;
  80. #endif
  81. if (!type_hints) {
  82. processed_template = processed_template.replace(": int", "")
  83. .replace(": Shader.Mode", "")
  84. .replace(": VisualShader.Type", "")
  85. .replace(": float", "")
  86. .replace(": String", "")
  87. .replace(": Array[String]", "")
  88. .replace(": Node", "")
  89. .replace(": CharFXTransform", "")
  90. .replace(":=", "=")
  91. .replace(" -> void", "")
  92. .replace(" -> bool", "")
  93. .replace(" -> int", "")
  94. .replace(" -> PortType", "")
  95. .replace(" -> String", "")
  96. .replace(" -> Object", "");
  97. }
  98. processed_template = processed_template.replace("_BASE_", p_base_class_name)
  99. .replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_unicode_identifier())
  100. .replace("_CLASS_", p_class_name.to_pascal_case().validate_unicode_identifier())
  101. .replace("_TS_", _get_indentation());
  102. scr->set_source_code(processed_template);
  103. return scr;
  104. }
  105. Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(const StringName &p_object) {
  106. Vector<ScriptLanguage::ScriptTemplate> templates;
  107. #ifdef TOOLS_ENABLED
  108. for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
  109. if (TEMPLATES[i].inherit == p_object) {
  110. templates.append(TEMPLATES[i]);
  111. }
  112. }
  113. #endif
  114. return templates;
  115. }
  116. static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, HashMap<int, String> &r_funcs) {
  117. for (int i = 0; i < p_class->members.size(); i++) {
  118. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
  119. const GDScriptParser::FunctionNode *function = p_class->members[i].function;
  120. r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
  121. } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  122. String new_prefix = p_class->members[i].m_class->identifier->name;
  123. get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
  124. }
  125. }
  126. }
  127. bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, HashSet<int> *r_safe_lines) const {
  128. GDScriptParser parser;
  129. GDScriptAnalyzer analyzer(&parser);
  130. Error err = parser.parse(p_script, p_path, false);
  131. if (err == OK) {
  132. err = analyzer.analyze();
  133. }
  134. #ifdef DEBUG_ENABLED
  135. if (r_warnings) {
  136. for (const GDScriptWarning &E : parser.get_warnings()) {
  137. const GDScriptWarning &warn = E;
  138. ScriptLanguage::Warning w;
  139. w.start_line = warn.start_line;
  140. w.end_line = warn.end_line;
  141. w.code = (int)warn.code;
  142. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  143. w.message = warn.get_message();
  144. r_warnings->push_back(w);
  145. }
  146. }
  147. #endif
  148. if (err) {
  149. if (r_errors) {
  150. for (const GDScriptParser::ParserError &pe : parser.get_errors()) {
  151. ScriptLanguage::ScriptError e;
  152. e.path = p_path;
  153. e.line = pe.line;
  154. e.column = pe.column;
  155. e.message = pe.message;
  156. r_errors->push_back(e);
  157. }
  158. for (KeyValue<String, Ref<GDScriptParserRef>> E : parser.get_depended_parsers()) {
  159. GDScriptParser *depended_parser = E.value->get_parser();
  160. for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
  161. ScriptLanguage::ScriptError e;
  162. e.path = E.key;
  163. e.line = pe.line;
  164. e.column = pe.column;
  165. e.message = pe.message;
  166. r_errors->push_back(e);
  167. }
  168. }
  169. }
  170. return false;
  171. } else if (r_functions) {
  172. const GDScriptParser::ClassNode *cl = parser.get_tree();
  173. HashMap<int, String> funcs;
  174. get_function_names_recursively(cl, "", funcs);
  175. for (const KeyValue<int, String> &E : funcs) {
  176. r_functions->push_back(E.value + ":" + itos(E.key));
  177. }
  178. }
  179. #ifdef DEBUG_ENABLED
  180. if (r_safe_lines) {
  181. const HashSet<int> &unsafe_lines = parser.get_unsafe_lines();
  182. for (int i = 1; i <= parser.get_last_line_number(); i++) {
  183. if (!unsafe_lines.has(i)) {
  184. r_safe_lines->insert(i);
  185. }
  186. }
  187. }
  188. #endif
  189. return true;
  190. }
  191. bool GDScriptLanguage::supports_builtin_mode() const {
  192. return true;
  193. }
  194. bool GDScriptLanguage::supports_documentation() const {
  195. return true;
  196. }
  197. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  198. GDScriptTokenizerText tokenizer;
  199. tokenizer.set_source_code(p_code);
  200. int indent = 0;
  201. GDScriptTokenizer::Token current = tokenizer.scan();
  202. while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
  203. if (current.type == GDScriptTokenizer::Token::INDENT) {
  204. indent++;
  205. } else if (current.type == GDScriptTokenizer::Token::DEDENT) {
  206. indent--;
  207. }
  208. if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
  209. current = tokenizer.scan();
  210. if (current.is_identifier()) {
  211. String identifier = current.get_identifier();
  212. if (identifier == p_function) {
  213. return current.start_line;
  214. }
  215. }
  216. }
  217. current = tokenizer.scan();
  218. }
  219. return -1;
  220. }
  221. Script *GDScriptLanguage::create_script() const {
  222. return memnew(GDScript);
  223. }
  224. /* DEBUGGER FUNCTIONS */
  225. thread_local int GDScriptLanguage::_debug_parse_err_line = -1;
  226. thread_local String GDScriptLanguage::_debug_parse_err_file;
  227. thread_local String GDScriptLanguage::_debug_error;
  228. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  229. // break because of parse error
  230. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  231. _debug_parse_err_line = p_line;
  232. _debug_parse_err_file = p_file;
  233. _debug_error = p_error;
  234. EngineDebugger::get_script_debugger()->debug(this, false, true);
  235. // Because this is thread local, clear the memory afterwards.
  236. _debug_parse_err_file = String();
  237. _debug_error = String();
  238. return true;
  239. } else {
  240. return false;
  241. }
  242. }
  243. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  244. if (EngineDebugger::is_active()) {
  245. _debug_parse_err_line = -1;
  246. _debug_parse_err_file = "";
  247. _debug_error = p_error;
  248. bool is_error_breakpoint = p_error != "Breakpoint";
  249. EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
  250. // Because this is thread local, clear the memory afterwards.
  251. _debug_parse_err_file = String();
  252. _debug_error = String();
  253. return true;
  254. } else {
  255. return false;
  256. }
  257. }
  258. String GDScriptLanguage::debug_get_error() const {
  259. return _debug_error;
  260. }
  261. int GDScriptLanguage::debug_get_stack_level_count() const {
  262. if (_debug_parse_err_line >= 0) {
  263. return 1;
  264. }
  265. return _call_stack_size;
  266. }
  267. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  268. if (_debug_parse_err_line >= 0) {
  269. return _debug_parse_err_line;
  270. }
  271. ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, -1);
  272. return *(_get_stack_level(p_level)->line);
  273. }
  274. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  275. if (_debug_parse_err_line >= 0) {
  276. return "";
  277. }
  278. ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, "");
  279. GDScriptFunction *func = _get_stack_level(p_level)->function;
  280. return func ? func->get_name().operator String() : "";
  281. }
  282. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  283. if (_debug_parse_err_line >= 0) {
  284. return _debug_parse_err_file;
  285. }
  286. ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, "");
  287. return _get_stack_level(p_level)->function->get_source();
  288. }
  289. 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) {
  290. if (_debug_parse_err_line >= 0) {
  291. return;
  292. }
  293. ERR_FAIL_INDEX(p_level, (int)_call_stack_size);
  294. CallLevel *cl = _get_stack_level(p_level);
  295. GDScriptFunction *f = cl->function;
  296. List<Pair<StringName, int>> locals;
  297. f->debug_get_stack_member_state(*cl->line, &locals);
  298. for (const Pair<StringName, int> &E : locals) {
  299. p_locals->push_back(E.first);
  300. p_values->push_back(cl->stack[E.second]);
  301. }
  302. }
  303. 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) {
  304. if (_debug_parse_err_line >= 0) {
  305. return;
  306. }
  307. ERR_FAIL_INDEX(p_level, (int)_call_stack_size);
  308. CallLevel *cl = _get_stack_level(p_level);
  309. GDScriptInstance *instance = cl->instance;
  310. if (!instance) {
  311. return;
  312. }
  313. Ref<GDScript> scr = instance->get_script();
  314. ERR_FAIL_COND(scr.is_null());
  315. const HashMap<StringName, GDScript::MemberInfo> &mi = scr->debug_get_member_indices();
  316. for (const KeyValue<StringName, GDScript::MemberInfo> &E : mi) {
  317. p_members->push_back(E.key);
  318. p_values->push_back(instance->debug_get_member_by_index(E.value.index));
  319. }
  320. }
  321. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  322. if (_debug_parse_err_line >= 0) {
  323. return nullptr;
  324. }
  325. ERR_FAIL_INDEX_V(p_level, (int)_call_stack_size, nullptr);
  326. return _get_stack_level(p_level)->instance;
  327. }
  328. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  329. const HashMap<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  330. const Variant *gl_array = GDScriptLanguage::get_singleton()->get_global_array();
  331. List<Pair<String, Variant>> cinfo;
  332. get_public_constants(&cinfo);
  333. for (const KeyValue<StringName, int> &E : name_idx) {
  334. if (GDScriptAnalyzer::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  335. continue;
  336. }
  337. bool is_script_constant = false;
  338. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  339. if (CE->get().first == E.key) {
  340. is_script_constant = true;
  341. break;
  342. }
  343. }
  344. if (is_script_constant) {
  345. continue;
  346. }
  347. const Variant &var = gl_array[E.value];
  348. bool freed = false;
  349. const Object *obj = var.get_validated_object_with_check(freed);
  350. if (obj && !freed) {
  351. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  352. continue;
  353. }
  354. }
  355. bool skip = false;
  356. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  357. if (E.key == CoreConstants::get_global_constant_name(i)) {
  358. skip = true;
  359. break;
  360. }
  361. }
  362. if (skip) {
  363. continue;
  364. }
  365. p_globals->push_back(E.key);
  366. p_values->push_back(var);
  367. }
  368. }
  369. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  370. List<String> names;
  371. List<Variant> values;
  372. debug_get_stack_level_locals(p_level, &names, &values, p_max_subitems, p_max_depth);
  373. Vector<String> name_vector;
  374. for (const String &name : names) {
  375. name_vector.push_back(name);
  376. }
  377. Array value_array;
  378. for (const Variant &value : values) {
  379. value_array.push_back(value);
  380. }
  381. Expression expression;
  382. if (expression.parse(p_expression, name_vector) == OK) {
  383. ScriptInstance *instance = debug_get_stack_level_instance(p_level);
  384. if (instance) {
  385. Variant return_val = expression.execute(value_array, instance->get_owner());
  386. return return_val.get_construct_string();
  387. }
  388. }
  389. return String();
  390. }
  391. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  392. p_extensions->push_back("gd");
  393. }
  394. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  395. List<StringName> functions;
  396. GDScriptUtilityFunctions::get_function_list(&functions);
  397. for (const StringName &E : functions) {
  398. p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E));
  399. }
  400. // Not really "functions", but show in documentation.
  401. {
  402. MethodInfo mi;
  403. mi.name = "preload";
  404. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  405. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  406. p_functions->push_back(mi);
  407. }
  408. {
  409. MethodInfo mi;
  410. mi.name = "assert";
  411. mi.return_val.type = Variant::NIL;
  412. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  413. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  414. mi.default_arguments.push_back(String());
  415. p_functions->push_back(mi);
  416. }
  417. }
  418. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  419. Pair<String, Variant> pi;
  420. pi.first = "PI";
  421. pi.second = Math::PI;
  422. p_constants->push_back(pi);
  423. Pair<String, Variant> tau;
  424. tau.first = "TAU";
  425. tau.second = Math::TAU;
  426. p_constants->push_back(tau);
  427. Pair<String, Variant> infinity;
  428. infinity.first = "INF";
  429. infinity.second = Math::INF;
  430. p_constants->push_back(infinity);
  431. Pair<String, Variant> nan;
  432. nan.first = "NAN";
  433. nan.second = Math::NaN;
  434. p_constants->push_back(nan);
  435. }
  436. void GDScriptLanguage::get_public_annotations(List<MethodInfo> *p_annotations) const {
  437. GDScriptParser parser;
  438. List<MethodInfo> annotations;
  439. parser.get_annotation_list(&annotations);
  440. for (const MethodInfo &E : annotations) {
  441. p_annotations->push_back(E);
  442. }
  443. }
  444. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  445. #ifdef TOOLS_ENABLED
  446. const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  447. #else
  448. const bool type_hints = true;
  449. #endif
  450. String result = "func " + p_name + "(";
  451. if (p_args.size()) {
  452. for (int i = 0; i < p_args.size(); i++) {
  453. if (i > 0) {
  454. result += ", ";
  455. }
  456. const String name_unstripped = p_args[i].get_slicec(':', 0);
  457. result += name_unstripped.strip_edges();
  458. if (type_hints) {
  459. const String type_stripped = p_args[i].substr(name_unstripped.length() + 1).strip_edges();
  460. if (!type_stripped.is_empty()) {
  461. result += ": " + type_stripped;
  462. }
  463. }
  464. }
  465. }
  466. result += String(")") + (type_hints ? " -> void" : "") + ":\n" +
  467. _get_indentation() + "pass # Replace with function body.\n";
  468. return result;
  469. }
  470. //////// COMPLETION //////////
  471. #ifdef TOOLS_ENABLED
  472. #define COMPLETION_RECURSION_LIMIT 200
  473. struct GDScriptCompletionIdentifier {
  474. GDScriptParser::DataType type;
  475. String enumeration;
  476. Variant value;
  477. const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
  478. };
  479. // LOCATION METHODS
  480. // These methods are used to populate the `CodeCompletionOption::location` integer.
  481. // For these methods, the location is based on the depth in the inheritance chain that the property
  482. // appears. For example, if you are completing code in a class that inherits Node2D, a property found on Node2D
  483. // will have a "better" (lower) location "score" than a property that is found on CanvasItem.
  484. static int _get_property_location(const StringName &p_class, const StringName &p_property) {
  485. if (!ClassDB::has_property(p_class, p_property)) {
  486. return ScriptLanguage::LOCATION_OTHER;
  487. }
  488. int depth = 0;
  489. StringName class_test = p_class;
  490. while (class_test && !ClassDB::has_property(class_test, p_property, true)) {
  491. class_test = ClassDB::get_parent_class(class_test);
  492. depth++;
  493. }
  494. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  495. }
  496. static int _get_property_location(Ref<Script> p_script, const StringName &p_property) {
  497. int depth = 0;
  498. Ref<Script> scr = p_script;
  499. while (scr.is_valid()) {
  500. if (scr->get_member_line(p_property) != -1) {
  501. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  502. }
  503. depth++;
  504. scr = scr->get_base_script();
  505. }
  506. return depth + _get_property_location(p_script->get_instance_base_type(), p_property);
  507. }
  508. static int _get_constant_location(const StringName &p_class, const StringName &p_constant) {
  509. if (!ClassDB::has_integer_constant(p_class, p_constant)) {
  510. return ScriptLanguage::LOCATION_OTHER;
  511. }
  512. int depth = 0;
  513. StringName class_test = p_class;
  514. while (class_test && !ClassDB::has_integer_constant(class_test, p_constant, true)) {
  515. class_test = ClassDB::get_parent_class(class_test);
  516. depth++;
  517. }
  518. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  519. }
  520. static int _get_constant_location(Ref<Script> p_script, const StringName &p_constant) {
  521. int depth = 0;
  522. Ref<Script> scr = p_script;
  523. while (scr.is_valid()) {
  524. if (scr->get_member_line(p_constant) != -1) {
  525. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  526. }
  527. depth++;
  528. scr = scr->get_base_script();
  529. }
  530. return depth + _get_constant_location(p_script->get_instance_base_type(), p_constant);
  531. }
  532. static int _get_signal_location(const StringName &p_class, const StringName &p_signal) {
  533. if (!ClassDB::has_signal(p_class, p_signal)) {
  534. return ScriptLanguage::LOCATION_OTHER;
  535. }
  536. int depth = 0;
  537. StringName class_test = p_class;
  538. while (class_test && !ClassDB::has_signal(class_test, p_signal, true)) {
  539. class_test = ClassDB::get_parent_class(class_test);
  540. depth++;
  541. }
  542. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  543. }
  544. static int _get_signal_location(Ref<Script> p_script, const StringName &p_signal) {
  545. int depth = 0;
  546. Ref<Script> scr = p_script;
  547. while (scr.is_valid()) {
  548. if (scr->get_member_line(p_signal) != -1) {
  549. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  550. }
  551. depth++;
  552. scr = scr->get_base_script();
  553. }
  554. return depth + _get_signal_location(p_script->get_instance_base_type(), p_signal);
  555. }
  556. static int _get_method_location(const StringName &p_class, const StringName &p_method) {
  557. if (!ClassDB::has_method(p_class, p_method)) {
  558. return ScriptLanguage::LOCATION_OTHER;
  559. }
  560. int depth = 0;
  561. StringName class_test = p_class;
  562. while (class_test && !ClassDB::has_method(class_test, p_method, true)) {
  563. class_test = ClassDB::get_parent_class(class_test);
  564. depth++;
  565. }
  566. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  567. }
  568. static int _get_enum_constant_location(const StringName &p_class, const StringName &p_enum_constant) {
  569. if (!ClassDB::get_integer_constant_enum(p_class, p_enum_constant)) {
  570. return ScriptLanguage::LOCATION_OTHER;
  571. }
  572. int depth = 0;
  573. StringName class_test = p_class;
  574. while (class_test && !ClassDB::get_integer_constant_enum(class_test, p_enum_constant, true)) {
  575. class_test = ClassDB::get_parent_class(class_test);
  576. depth++;
  577. }
  578. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  579. }
  580. static int _get_enum_location(const StringName &p_class, const StringName &p_enum) {
  581. if (!ClassDB::has_enum(p_class, p_enum)) {
  582. return ScriptLanguage::LOCATION_OTHER;
  583. }
  584. int depth = 0;
  585. StringName class_test = p_class;
  586. while (class_test && !ClassDB::has_enum(class_test, p_enum, true)) {
  587. class_test = ClassDB::get_parent_class(class_test);
  588. depth++;
  589. }
  590. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  591. }
  592. // END LOCATION METHODS
  593. static String _trim_parent_class(const String &p_class, const String &p_base_class) {
  594. if (p_base_class.is_empty()) {
  595. return p_class;
  596. }
  597. Vector<String> names = p_class.split(".", false, 1);
  598. if (names.size() == 2) {
  599. const String &first = names[0];
  600. if (GDScriptAnalyzer::class_exists(p_base_class) && GDScriptAnalyzer::class_exists(first) && ClassDB::is_parent_class(p_base_class, first)) {
  601. const String &rest = names[1];
  602. return rest;
  603. }
  604. }
  605. return p_class;
  606. }
  607. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg, const String &p_base_class = "") {
  608. String class_name = p_info.class_name;
  609. bool is_enum = p_info.type == Variant::INT && p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM;
  610. // PROPERTY_USAGE_CLASS_IS_BITFIELD: BitField[T] isn't supported (yet?), use plain int.
  611. if ((p_info.type == Variant::OBJECT || is_enum) && !class_name.is_empty()) {
  612. if (is_enum && CoreConstants::is_global_enum(p_info.class_name)) {
  613. return class_name;
  614. }
  615. return _trim_parent_class(class_name, p_base_class);
  616. } else if (p_info.type == Variant::ARRAY && p_info.hint == PROPERTY_HINT_ARRAY_TYPE && !p_info.hint_string.is_empty()) {
  617. return "Array[" + _trim_parent_class(p_info.hint_string, p_base_class) + "]";
  618. } else if (p_info.type == Variant::DICTIONARY && p_info.hint == PROPERTY_HINT_DICTIONARY_TYPE && !p_info.hint_string.is_empty()) {
  619. const String key = p_info.hint_string.get_slicec(';', 0);
  620. const String value = p_info.hint_string.get_slicec(';', 1);
  621. return "Dictionary[" + _trim_parent_class(key, p_base_class) + ", " + _trim_parent_class(value, p_base_class) + "]";
  622. } else if (p_info.type == Variant::NIL) {
  623. if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  624. return "Variant";
  625. } else {
  626. return "void";
  627. }
  628. }
  629. return Variant::get_type_name(p_info.type);
  630. }
  631. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
  632. String arghint;
  633. if (!p_is_annotation) {
  634. arghint += _get_visual_datatype(p_info.return_val, false) + " ";
  635. }
  636. arghint += p_info.name + "(";
  637. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  638. int i = 0;
  639. for (const PropertyInfo &E : p_info.arguments) {
  640. if (i > 0) {
  641. arghint += ", ";
  642. }
  643. if (i == p_arg_idx) {
  644. arghint += String::chr(0xFFFF);
  645. }
  646. arghint += E.name + ": " + _get_visual_datatype(E, true);
  647. if (i - def_args >= 0) {
  648. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  649. }
  650. if (i == p_arg_idx) {
  651. arghint += String::chr(0xFFFF);
  652. }
  653. i++;
  654. }
  655. if (p_info.flags & METHOD_FLAG_VARARG) {
  656. if (p_info.arguments.size() > 0) {
  657. arghint += ", ";
  658. }
  659. if (p_arg_idx >= p_info.arguments.size()) {
  660. arghint += String::chr(0xFFFF);
  661. }
  662. arghint += "...args: Array"; // `MethodInfo` does not support the rest parameter name.
  663. if (p_arg_idx >= p_info.arguments.size()) {
  664. arghint += String::chr(0xFFFF);
  665. }
  666. }
  667. arghint += ")";
  668. return arghint;
  669. }
  670. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx, bool p_just_args = false) {
  671. String arghint;
  672. if (p_just_args) {
  673. arghint = "(";
  674. } else {
  675. if (p_function->get_datatype().builtin_type == Variant::NIL) {
  676. arghint = "void " + p_function->identifier->name + "(";
  677. } else {
  678. arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name + "(";
  679. }
  680. }
  681. for (int i = 0; i < p_function->parameters.size(); i++) {
  682. if (i > 0) {
  683. arghint += ", ";
  684. }
  685. if (i == p_arg_idx) {
  686. arghint += String::chr(0xFFFF);
  687. }
  688. const GDScriptParser::ParameterNode *par = p_function->parameters[i];
  689. if (!par->get_datatype().is_hard_type()) {
  690. arghint += par->identifier->name.operator String() + ": Variant";
  691. } else {
  692. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  693. }
  694. if (par->initializer) {
  695. String def_val = "<unknown>";
  696. switch (par->initializer->type) {
  697. case GDScriptParser::Node::LITERAL: {
  698. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->initializer);
  699. def_val = literal->value.get_construct_string();
  700. } break;
  701. case GDScriptParser::Node::IDENTIFIER: {
  702. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->initializer);
  703. def_val = id->name.operator String();
  704. } break;
  705. case GDScriptParser::Node::CALL: {
  706. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->initializer);
  707. if (call->is_constant && call->reduced) {
  708. def_val = call->reduced_value.get_construct_string();
  709. } else if (call->get_callee_type() == GDScriptParser::Node::IDENTIFIER) {
  710. def_val = call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)");
  711. }
  712. } break;
  713. case GDScriptParser::Node::ARRAY: {
  714. const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->initializer);
  715. if (arr->is_constant && arr->reduced) {
  716. def_val = arr->reduced_value.get_construct_string();
  717. } else {
  718. def_val = arr->elements.is_empty() ? "[]" : "[...]";
  719. }
  720. } break;
  721. case GDScriptParser::Node::DICTIONARY: {
  722. const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->initializer);
  723. if (dict->is_constant && dict->reduced) {
  724. def_val = dict->reduced_value.get_construct_string();
  725. } else {
  726. def_val = dict->elements.is_empty() ? "{}" : "{...}";
  727. }
  728. } break;
  729. case GDScriptParser::Node::SUBSCRIPT: {
  730. const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->initializer);
  731. if (sub->is_attribute && sub->datatype.kind == GDScriptParser::DataType::ENUM && !sub->datatype.is_meta_type) {
  732. def_val = sub->get_datatype().to_string() + "." + sub->attribute->name;
  733. } else if (sub->is_constant && sub->reduced) {
  734. def_val = sub->reduced_value.get_construct_string();
  735. }
  736. } break;
  737. default:
  738. break;
  739. }
  740. arghint += " = " + def_val;
  741. }
  742. if (i == p_arg_idx) {
  743. arghint += String::chr(0xFFFF);
  744. }
  745. }
  746. if (p_function->is_vararg()) {
  747. if (!p_function->parameters.is_empty()) {
  748. arghint += ", ";
  749. }
  750. if (p_arg_idx >= p_function->parameters.size()) {
  751. arghint += String::chr(0xFFFF);
  752. }
  753. const GDScriptParser::ParameterNode *rest_param = p_function->rest_parameter;
  754. arghint += "..." + rest_param->identifier->name + ": " + rest_param->get_datatype().to_string();
  755. if (p_arg_idx >= p_function->parameters.size()) {
  756. arghint += String::chr(0xFFFF);
  757. }
  758. }
  759. arghint += ")";
  760. return arghint;
  761. }
  762. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_list, const StringName &p_required_type = StringName()) {
  763. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  764. const bool requires_type = !p_required_type.is_empty();
  765. for (int i = 0; i < p_dir->get_file_count(); i++) {
  766. if (requires_type && !ClassDB::is_parent_class(p_dir->get_file_type(i), p_required_type)) {
  767. continue;
  768. }
  769. ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i).quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH);
  770. r_list.insert(option.display, option);
  771. }
  772. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  773. _get_directory_contents(p_dir->get_subdir(i), r_list, p_required_type);
  774. }
  775. }
  776. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
  777. ERR_FAIL_NULL(p_annotation);
  778. if (p_annotation->info != nullptr) {
  779. r_arghint = _make_arguments_hint(p_annotation->info->info, p_argument, true);
  780. }
  781. if (p_annotation->name == SNAME("@export_range")) {
  782. if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
  783. // Slider hint.
  784. ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  785. slider1.insert_text = slider1.display.quote(p_quote_style);
  786. r_result.insert(slider1.display, slider1);
  787. ScriptLanguage::CodeCompletionOption slider2("or_less", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  788. slider2.insert_text = slider2.display.quote(p_quote_style);
  789. r_result.insert(slider2.display, slider2);
  790. ScriptLanguage::CodeCompletionOption slider3("prefer_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  791. slider3.insert_text = slider3.display.quote(p_quote_style);
  792. r_result.insert(slider3.display, slider3);
  793. ScriptLanguage::CodeCompletionOption slider4("hide_control", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  794. slider4.insert_text = slider4.display.quote(p_quote_style);
  795. r_result.insert(slider4.display, slider4);
  796. }
  797. } else if (p_annotation->name == SNAME("@export_exp_easing")) {
  798. if (p_argument == 0 || p_argument == 1) {
  799. // Easing hint.
  800. ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  801. hint1.insert_text = hint1.display.quote(p_quote_style);
  802. r_result.insert(hint1.display, hint1);
  803. ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  804. hint2.insert_text = hint2.display.quote(p_quote_style);
  805. r_result.insert(hint2.display, hint2);
  806. }
  807. } else if (p_annotation->name == SNAME("@export_node_path")) {
  808. ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  809. node.insert_text = node.display.quote(p_quote_style);
  810. r_result.insert(node.display, node);
  811. LocalVector<StringName> native_classes;
  812. ClassDB::get_inheriters_from_class("Node", native_classes);
  813. for (const StringName &E : native_classes) {
  814. if (!ClassDB::is_class_exposed(E)) {
  815. continue;
  816. }
  817. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  818. option.insert_text = option.display.quote(p_quote_style);
  819. r_result.insert(option.display, option);
  820. }
  821. LocalVector<StringName> global_script_classes;
  822. ScriptServer::get_global_class_list(global_script_classes);
  823. for (const StringName &class_name : global_script_classes) {
  824. if (!ClassDB::is_parent_class(ScriptServer::get_global_class_native_base(class_name), "Node")) {
  825. continue;
  826. }
  827. ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  828. option.insert_text = option.display.quote(p_quote_style);
  829. r_result.insert(option.display, option);
  830. }
  831. } else if (p_annotation->name == SNAME("@export_tool_button")) {
  832. if (p_argument == 1) {
  833. const Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  834. if (theme.is_valid()) {
  835. List<StringName> icon_list;
  836. theme->get_icon_list(EditorStringName(EditorIcons), &icon_list);
  837. for (const StringName &E : icon_list) {
  838. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  839. option.insert_text = option.display.quote(p_quote_style);
  840. r_result.insert(option.display, option);
  841. }
  842. }
  843. }
  844. } else if (p_annotation->name == SNAME("@export_custom")) {
  845. switch (p_argument) {
  846. case 0: {
  847. static HashMap<StringName, int64_t> items;
  848. if (unlikely(items.is_empty())) {
  849. CoreConstants::get_enum_values(SNAME("PropertyHint"), &items);
  850. }
  851. for (const KeyValue<StringName, int64_t> &item : items) {
  852. ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  853. r_result.insert(option.display, option);
  854. }
  855. } break;
  856. case 2: {
  857. static HashMap<StringName, int64_t> items;
  858. if (unlikely(items.is_empty())) {
  859. CoreConstants::get_enum_values(SNAME("PropertyUsageFlags"), &items);
  860. }
  861. for (const KeyValue<StringName, int64_t> &item : items) {
  862. ScriptLanguage::CodeCompletionOption option(item.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  863. r_result.insert(option.display, option);
  864. }
  865. } break;
  866. }
  867. } else if (p_annotation->name == SNAME("@warning_ignore") || p_annotation->name == SNAME("@warning_ignore_start") || p_annotation->name == SNAME("@warning_ignore_restore")) {
  868. for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
  869. #ifndef DISABLE_DEPRECATED
  870. if (warning_code >= GDScriptWarning::FIRST_DEPRECATED_WARNING) {
  871. break; // Don't suggest deprecated warnings as they are never produced.
  872. }
  873. #endif // DISABLE_DEPRECATED
  874. ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  875. warning.insert_text = warning.display.quote(p_quote_style);
  876. r_result.insert(warning.display, warning);
  877. }
  878. } else if (p_annotation->name == SNAME("@rpc")) {
  879. if (p_argument == 0 || p_argument == 1 || p_argument == 2) {
  880. static const char *options[7] = { "call_local", "call_remote", "any_peer", "authority", "reliable", "unreliable", "unreliable_ordered" };
  881. for (int i = 0; i < 7; i++) {
  882. ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  883. option.insert_text = option.display.quote(p_quote_style);
  884. r_result.insert(option.display, option);
  885. }
  886. }
  887. }
  888. }
  889. static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  890. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  891. if (Variant::Type(i) == Variant::Type::NIL) {
  892. continue;
  893. }
  894. ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  895. r_result.insert(option.display, option);
  896. }
  897. }
  898. static void _find_global_enums(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  899. List<StringName> global_enums;
  900. CoreConstants::get_global_enums(&global_enums);
  901. for (const StringName &enum_name : global_enums) {
  902. ScriptLanguage::CodeCompletionOption option(enum_name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_OTHER);
  903. r_result.insert(option.display, option);
  904. }
  905. }
  906. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  907. // Built-in Variant Types
  908. _find_built_in_variants(r_result);
  909. // Variant meta-type
  910. if (!p_inherit_only) {
  911. ScriptLanguage::CodeCompletionOption variant_option("Variant", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  912. r_result.insert(variant_option.display, variant_option);
  913. }
  914. LocalVector<StringName> native_types;
  915. ClassDB::get_class_list(native_types);
  916. for (const StringName &type : native_types) {
  917. if (ClassDB::is_class_exposed(type) && !Engine::get_singleton()->has_singleton(type)) {
  918. ScriptLanguage::CodeCompletionOption option(type, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  919. r_result.insert(option.display, option);
  920. }
  921. }
  922. // TODO: Unify with _find_identifiers_in_class.
  923. if (p_context.current_class) {
  924. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  925. // Native enums from base class
  926. List<StringName> enums;
  927. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  928. for (const StringName &E : enums) {
  929. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  930. r_result.insert(option.display, option);
  931. }
  932. }
  933. // Check current class for potential types.
  934. // TODO: Also check classes the current class inherits from.
  935. const GDScriptParser::ClassNode *current = p_context.current_class;
  936. int location_offset = 0;
  937. while (current) {
  938. for (int i = 0; i < current->members.size(); i++) {
  939. const GDScriptParser::ClassNode::Member &member = current->members[i];
  940. switch (member.type) {
  941. case GDScriptParser::ClassNode::Member::CLASS: {
  942. ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
  943. r_result.insert(option.display, option);
  944. } break;
  945. case GDScriptParser::ClassNode::Member::ENUM: {
  946. if (!p_inherit_only) {
  947. ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL + location_offset);
  948. r_result.insert(option.display, option);
  949. }
  950. } break;
  951. case GDScriptParser::ClassNode::Member::CONSTANT: {
  952. if (member.constant->get_datatype().is_meta_type) {
  953. ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL + location_offset);
  954. r_result.insert(option.display, option);
  955. }
  956. } break;
  957. default:
  958. break;
  959. }
  960. }
  961. location_offset += 1;
  962. current = current->outer;
  963. }
  964. }
  965. // Global scripts
  966. LocalVector<StringName> global_classes;
  967. ScriptServer::get_global_class_list(global_classes);
  968. for (const StringName &class_name : global_classes) {
  969. ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  970. r_result.insert(option.display, option);
  971. }
  972. // Global enums
  973. if (!p_inherit_only) {
  974. _find_global_enums(r_result);
  975. }
  976. // Autoload singletons
  977. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  978. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  979. const ProjectSettings::AutoloadInfo &info = E.value;
  980. if (!info.is_singleton || !info.path.has_extension("gd")) {
  981. continue;
  982. }
  983. ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  984. r_result.insert(option.display, option);
  985. }
  986. }
  987. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth = 0) {
  988. for (int i = 0; i < p_suite->locals.size(); i++) {
  989. ScriptLanguage::CodeCompletionOption option;
  990. int location = p_recursion_depth == 0 ? ScriptLanguage::LOCATION_LOCAL : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
  991. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  992. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  993. option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
  994. } else {
  995. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, location);
  996. }
  997. r_result.insert(option.display, option);
  998. }
  999. if (p_suite->parent_block) {
  1000. _find_identifiers_in_suite(p_suite->parent_block, r_result, p_recursion_depth + 1);
  1001. }
  1002. }
  1003. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
  1004. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_types_only, bool p_static, bool p_parent_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1005. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  1006. if (!p_parent_only) {
  1007. bool outer = false;
  1008. const GDScriptParser::ClassNode *clss = p_class;
  1009. int classes_processed = 0;
  1010. while (clss) {
  1011. for (int i = 0; i < clss->members.size(); i++) {
  1012. const int location = p_recursion_depth == 0 ? classes_processed : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
  1013. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  1014. ScriptLanguage::CodeCompletionOption option;
  1015. switch (member.type) {
  1016. case GDScriptParser::ClassNode::Member::VARIABLE:
  1017. if (p_types_only || p_only_functions || outer || (p_static && !member.variable->is_static)) {
  1018. continue;
  1019. }
  1020. option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  1021. break;
  1022. case GDScriptParser::ClassNode::Member::CONSTANT:
  1023. if ((p_types_only && !member.constant->datatype.is_meta_type) || p_only_functions) {
  1024. continue;
  1025. }
  1026. if (r_result.has(member.constant->identifier->name)) {
  1027. continue;
  1028. }
  1029. option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1030. if (member.constant->initializer) {
  1031. option.default_value = member.constant->initializer->reduced_value;
  1032. }
  1033. break;
  1034. case GDScriptParser::ClassNode::Member::CLASS:
  1035. if (p_only_functions) {
  1036. continue;
  1037. }
  1038. option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
  1039. break;
  1040. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1041. if (p_types_only || p_only_functions) {
  1042. continue;
  1043. }
  1044. option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1045. break;
  1046. case GDScriptParser::ClassNode::Member::ENUM:
  1047. if (p_only_functions) {
  1048. continue;
  1049. }
  1050. option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  1051. break;
  1052. case GDScriptParser::ClassNode::Member::FUNCTION:
  1053. if (p_types_only || outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  1054. continue;
  1055. }
  1056. option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  1057. if (p_add_braces) {
  1058. if (member.function->parameters.size() > 0 || (member.function->info.flags & METHOD_FLAG_VARARG)) {
  1059. option.insert_text += "(";
  1060. option.display += U"(\u2026)";
  1061. } else {
  1062. option.insert_text += "()";
  1063. option.display += "()";
  1064. }
  1065. }
  1066. break;
  1067. case GDScriptParser::ClassNode::Member::SIGNAL:
  1068. if (p_types_only || p_only_functions || outer || p_static) {
  1069. continue;
  1070. }
  1071. option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  1072. break;
  1073. case GDScriptParser::ClassNode::Member::GROUP:
  1074. break; // No-op, but silences warnings.
  1075. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1076. break;
  1077. }
  1078. r_result.insert(option.display, option);
  1079. }
  1080. if (p_types_only) {
  1081. break; // Otherwise, it will fill the results with types from the outer class (which is undesired for that case).
  1082. }
  1083. outer = true;
  1084. clss = clss->outer;
  1085. classes_processed++;
  1086. }
  1087. }
  1088. // Parents.
  1089. GDScriptCompletionIdentifier base_type;
  1090. base_type.type = p_class->base_type;
  1091. base_type.type.is_meta_type = p_static;
  1092. _find_identifiers_in_base(base_type, p_only_functions, p_types_only, p_add_braces, r_result, p_recursion_depth + 1);
  1093. }
  1094. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, bool p_types_only, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1095. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  1096. GDScriptParser::DataType base_type = p_base.type;
  1097. if (!p_types_only && base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::BUILTIN && base_type.kind != GDScriptParser::DataType::ENUM) {
  1098. ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
  1099. if (p_add_braces) {
  1100. option.insert_text += "(";
  1101. option.display += U"(\u2026)";
  1102. }
  1103. r_result.insert(option.display, option);
  1104. }
  1105. while (!base_type.has_no_type()) {
  1106. switch (base_type.kind) {
  1107. case GDScriptParser::DataType::CLASS: {
  1108. _find_identifiers_in_class(base_type.class_type, p_only_functions, p_types_only, base_type.is_meta_type, false, p_add_braces, r_result, p_recursion_depth);
  1109. // This already finds all parent identifiers, so we are done.
  1110. base_type = GDScriptParser::DataType();
  1111. } break;
  1112. case GDScriptParser::DataType::SCRIPT: {
  1113. Ref<Script> scr = base_type.script_type;
  1114. if (scr.is_valid()) {
  1115. if (p_types_only) {
  1116. // TODO: Need to implement Script::get_script_enum_list and retrieve the enum list from a script.
  1117. } else if (!p_only_functions) {
  1118. if (!base_type.is_meta_type) {
  1119. List<PropertyInfo> members;
  1120. scr->get_script_property_list(&members);
  1121. for (const PropertyInfo &E : members) {
  1122. if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
  1123. continue;
  1124. }
  1125. if (E.name.contains_char('/')) {
  1126. continue;
  1127. }
  1128. int location = p_recursion_depth + _get_property_location(scr, E.name);
  1129. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  1130. r_result.insert(option.display, option);
  1131. }
  1132. List<MethodInfo> signals;
  1133. scr->get_script_signal_list(&signals);
  1134. for (const MethodInfo &E : signals) {
  1135. if (E.name.begins_with("_")) {
  1136. continue;
  1137. }
  1138. int location = p_recursion_depth + _get_signal_location(scr, E.name);
  1139. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  1140. r_result.insert(option.display, option);
  1141. }
  1142. }
  1143. HashMap<StringName, Variant> constants;
  1144. scr->get_constants(&constants);
  1145. for (const KeyValue<StringName, Variant> &E : constants) {
  1146. int location = p_recursion_depth + _get_constant_location(scr, E.key);
  1147. ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1148. r_result.insert(option.display, option);
  1149. }
  1150. }
  1151. if (!p_types_only) {
  1152. List<MethodInfo> methods;
  1153. scr->get_script_method_list(&methods);
  1154. for (const MethodInfo &E : methods) {
  1155. if (E.name.begins_with("@")) {
  1156. continue;
  1157. }
  1158. int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
  1159. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  1160. if (p_add_braces) {
  1161. if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
  1162. option.insert_text += "(";
  1163. option.display += U"(\u2026)";
  1164. } else {
  1165. option.insert_text += "()";
  1166. option.display += "()";
  1167. }
  1168. }
  1169. r_result.insert(option.display, option);
  1170. }
  1171. }
  1172. Ref<Script> base_script = scr->get_base_script();
  1173. if (base_script.is_valid()) {
  1174. base_type.script_type = base_script;
  1175. } else {
  1176. base_type.kind = GDScriptParser::DataType::NATIVE;
  1177. base_type.builtin_type = Variant::OBJECT;
  1178. base_type.native_type = scr->get_instance_base_type();
  1179. }
  1180. } else {
  1181. return;
  1182. }
  1183. } break;
  1184. case GDScriptParser::DataType::NATIVE: {
  1185. StringName type = base_type.native_type;
  1186. if (!GDScriptAnalyzer::class_exists(type)) {
  1187. return;
  1188. }
  1189. List<StringName> enums;
  1190. ClassDB::get_enum_list(type, &enums);
  1191. for (const StringName &E : enums) {
  1192. int location = p_recursion_depth + _get_enum_location(type, E);
  1193. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  1194. r_result.insert(option.display, option);
  1195. }
  1196. if (p_types_only) {
  1197. return;
  1198. }
  1199. if (!p_only_functions) {
  1200. List<String> constants;
  1201. ClassDB::get_integer_constant_list(type, &constants);
  1202. for (const String &E : constants) {
  1203. int location = p_recursion_depth + _get_constant_location(type, StringName(E));
  1204. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1205. r_result.insert(option.display, option);
  1206. }
  1207. if (!base_type.is_meta_type || Engine::get_singleton()->has_singleton(type)) {
  1208. List<PropertyInfo> pinfo;
  1209. ClassDB::get_property_list(type, &pinfo);
  1210. for (const PropertyInfo &E : pinfo) {
  1211. if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
  1212. continue;
  1213. }
  1214. if (E.name.contains_char('/')) {
  1215. continue;
  1216. }
  1217. int location = p_recursion_depth + _get_property_location(type, E.name);
  1218. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  1219. r_result.insert(option.display, option);
  1220. }
  1221. List<MethodInfo> signals;
  1222. ClassDB::get_signal_list(type, &signals);
  1223. for (const MethodInfo &E : signals) {
  1224. if (E.name.begins_with("_")) {
  1225. continue;
  1226. }
  1227. int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
  1228. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  1229. r_result.insert(option.display, option);
  1230. }
  1231. }
  1232. }
  1233. bool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type);
  1234. List<MethodInfo> methods;
  1235. ClassDB::get_method_list(type, &methods, false, true);
  1236. for (const MethodInfo &E : methods) {
  1237. if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
  1238. continue;
  1239. }
  1240. if (E.name.begins_with("_")) {
  1241. continue;
  1242. }
  1243. int location = p_recursion_depth + _get_method_location(type, E.name);
  1244. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  1245. if (p_add_braces) {
  1246. if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
  1247. option.insert_text += "(";
  1248. option.display += U"(\u2026)";
  1249. } else {
  1250. option.insert_text += "()";
  1251. option.display += "()";
  1252. }
  1253. }
  1254. r_result.insert(option.display, option);
  1255. }
  1256. return;
  1257. } break;
  1258. case GDScriptParser::DataType::ENUM: {
  1259. if (p_types_only) {
  1260. return;
  1261. }
  1262. String type_str = base_type.native_type;
  1263. if (type_str.contains_char('.')) {
  1264. StringName type = type_str.get_slicec('.', 0);
  1265. StringName type_enum = base_type.enum_type;
  1266. List<StringName> enum_values;
  1267. ClassDB::get_enum_constants(type, type_enum, &enum_values);
  1268. for (const StringName &E : enum_values) {
  1269. int location = p_recursion_depth + _get_enum_constant_location(type, E);
  1270. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1271. r_result.insert(option.display, option);
  1272. }
  1273. } else if (CoreConstants::is_global_enum(base_type.enum_type)) {
  1274. HashMap<StringName, int64_t> enum_values;
  1275. CoreConstants::get_enum_values(base_type.enum_type, &enum_values);
  1276. for (const KeyValue<StringName, int64_t> &enum_value : enum_values) {
  1277. int location = p_recursion_depth + ScriptLanguage::LOCATION_OTHER;
  1278. ScriptLanguage::CodeCompletionOption option(enum_value.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1279. r_result.insert(option.display, option);
  1280. }
  1281. }
  1282. }
  1283. [[fallthrough]];
  1284. case GDScriptParser::DataType::BUILTIN: {
  1285. if (p_types_only) {
  1286. return;
  1287. }
  1288. Callable::CallError err;
  1289. Variant tmp;
  1290. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1291. if (err.error != Callable::CallError::CALL_OK) {
  1292. return;
  1293. }
  1294. int location = ScriptLanguage::LOCATION_OTHER;
  1295. if (!p_only_functions) {
  1296. List<PropertyInfo> members;
  1297. if (p_base.value.get_type() != Variant::NIL) {
  1298. p_base.value.get_property_list(&members);
  1299. } else {
  1300. tmp.get_property_list(&members);
  1301. }
  1302. for (const PropertyInfo &E : members) {
  1303. if (E.usage & (PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_INTERNAL)) {
  1304. continue;
  1305. }
  1306. if (!String(E.name).contains_char('/')) {
  1307. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  1308. if (base_type.kind == GDScriptParser::DataType::ENUM) {
  1309. // Sort enum members in their declaration order.
  1310. location += 1;
  1311. }
  1312. if (GDScriptParser::theme_color_names.has(E.name)) {
  1313. option.theme_color_name = GDScriptParser::theme_color_names[E.name];
  1314. }
  1315. r_result.insert(option.display, option);
  1316. }
  1317. }
  1318. }
  1319. List<MethodInfo> methods;
  1320. tmp.get_method_list(&methods);
  1321. for (const MethodInfo &E : methods) {
  1322. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type && !(E.flags & METHOD_FLAG_CONST)) {
  1323. // Enum types are static and cannot change, therefore we skip non-const dictionary methods.
  1324. continue;
  1325. }
  1326. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  1327. if (p_add_braces) {
  1328. if (E.arguments.size() || (E.flags & METHOD_FLAG_VARARG)) {
  1329. option.insert_text += "(";
  1330. option.display += U"(\u2026)";
  1331. } else {
  1332. option.insert_text += "()";
  1333. option.display += "()";
  1334. }
  1335. }
  1336. r_result.insert(option.display, option);
  1337. }
  1338. return;
  1339. } break;
  1340. default: {
  1341. return;
  1342. } break;
  1343. }
  1344. }
  1345. }
  1346. static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, bool p_add_braces, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1347. if (!p_only_functions && p_context.current_suite) {
  1348. // This includes function parameters, since they are also locals.
  1349. _find_identifiers_in_suite(p_context.current_suite, r_result);
  1350. }
  1351. if (p_context.current_class) {
  1352. _find_identifiers_in_class(p_context.current_class, p_only_functions, false, (!p_context.current_function || p_context.current_function->is_static), false, p_add_braces, r_result, p_recursion_depth);
  1353. }
  1354. List<StringName> functions;
  1355. GDScriptUtilityFunctions::get_function_list(&functions);
  1356. for (const StringName &E : functions) {
  1357. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
  1358. ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1359. if (p_add_braces) {
  1360. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  1361. option.insert_text += "(";
  1362. option.display += U"(\u2026)";
  1363. } else {
  1364. option.insert_text += "()";
  1365. option.display += "()";
  1366. }
  1367. }
  1368. r_result.insert(option.display, option);
  1369. }
  1370. if (p_only_functions) {
  1371. return;
  1372. }
  1373. _find_built_in_variants(r_result);
  1374. static const char *_keywords[] = {
  1375. "true", "false", "PI", "TAU", "INF", "NAN", "null", "self", "super",
  1376. "break", "breakpoint", "continue", "pass", "return",
  1377. nullptr
  1378. };
  1379. const char **kw = _keywords;
  1380. while (*kw) {
  1381. ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1382. r_result.insert(option.display, option);
  1383. kw++;
  1384. }
  1385. static const char *_keywords_with_space[] = {
  1386. "and", "not", "or", "in", "as", "class", "class_name", "extends", "is", "func", "signal", "await",
  1387. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "when", "while",
  1388. nullptr
  1389. };
  1390. const char **kws = _keywords_with_space;
  1391. while (*kws) {
  1392. ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1393. option.insert_text += " ";
  1394. r_result.insert(option.display, option);
  1395. kws++;
  1396. }
  1397. static const char *_keywords_with_args[] = {
  1398. "assert", "preload",
  1399. nullptr
  1400. };
  1401. const char **kwa = _keywords_with_args;
  1402. while (*kwa) {
  1403. ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1404. if (p_add_braces) {
  1405. option.insert_text += "(";
  1406. option.display += U"(\u2026)";
  1407. }
  1408. r_result.insert(option.display, option);
  1409. kwa++;
  1410. }
  1411. List<StringName> utility_func_names;
  1412. Variant::get_utility_function_list(&utility_func_names);
  1413. for (const StringName &util_func_name : utility_func_names) {
  1414. ScriptLanguage::CodeCompletionOption option(util_func_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1415. if (p_add_braces) {
  1416. option.insert_text += "(";
  1417. option.display += U"(\u2026)"; // As all utility functions contain an argument or more, this is hardcoded here.
  1418. }
  1419. r_result.insert(option.display, option);
  1420. }
  1421. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1422. if (!E.value.is_singleton) {
  1423. continue;
  1424. }
  1425. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1426. r_result.insert(option.display, option);
  1427. }
  1428. // Native classes and global constants.
  1429. for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
  1430. ScriptLanguage::CodeCompletionOption option;
  1431. if (GDScriptAnalyzer::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  1432. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  1433. } else {
  1434. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1435. }
  1436. r_result.insert(option.display, option);
  1437. }
  1438. // Global enums
  1439. _find_global_enums(r_result);
  1440. // Global classes
  1441. LocalVector<StringName> global_classes;
  1442. ScriptServer::get_global_class_list(global_classes);
  1443. for (const StringName &class_name : global_classes) {
  1444. ScriptLanguage::CodeCompletionOption option(class_name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  1445. r_result.insert(option.display, option);
  1446. }
  1447. }
  1448. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value, GDScriptParser::CompletionContext &p_context) {
  1449. GDScriptCompletionIdentifier ci;
  1450. ci.value = p_value;
  1451. ci.type.is_constant = true;
  1452. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1453. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1454. ci.type.builtin_type = p_value.get_type();
  1455. if (ci.type.builtin_type == Variant::OBJECT) {
  1456. Object *obj = p_value.operator Object *();
  1457. if (!obj) {
  1458. return ci;
  1459. }
  1460. ci.type.native_type = obj->get_class_name();
  1461. Ref<Script> scr = p_value;
  1462. if (scr.is_valid()) {
  1463. ci.type.is_meta_type = true;
  1464. } else {
  1465. ci.type.is_meta_type = false;
  1466. scr = obj->get_script();
  1467. }
  1468. if (scr.is_valid()) {
  1469. ci.type.script_path = scr->get_path();
  1470. ci.type.script_type = scr;
  1471. ci.type.native_type = scr->get_instance_base_type();
  1472. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1473. if (scr->get_path().ends_with(".gd")) {
  1474. Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(scr->get_path());
  1475. if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
  1476. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1477. ci.type.class_type = parser->get_parser()->get_tree();
  1478. ci.type.kind = GDScriptParser::DataType::CLASS;
  1479. return ci;
  1480. }
  1481. }
  1482. } else {
  1483. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1484. }
  1485. }
  1486. return ci;
  1487. }
  1488. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  1489. GDScriptCompletionIdentifier ci;
  1490. if (p_property.type == Variant::NIL) {
  1491. // Variant
  1492. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1493. ci.type.kind = GDScriptParser::DataType::VARIANT;
  1494. return ci;
  1495. }
  1496. if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  1497. ci.enumeration = p_property.class_name;
  1498. }
  1499. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1500. ci.type.builtin_type = p_property.type;
  1501. if (p_property.type == Variant::OBJECT) {
  1502. if (ScriptServer::is_global_class(p_property.class_name)) {
  1503. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1504. ci.type.script_path = ScriptServer::get_global_class_path(p_property.class_name);
  1505. ci.type.native_type = ScriptServer::get_global_class_native_base(p_property.class_name);
  1506. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_property.class_name));
  1507. if (scr.is_valid()) {
  1508. ci.type.script_type = scr;
  1509. }
  1510. } else {
  1511. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1512. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1513. }
  1514. } else {
  1515. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1516. }
  1517. return ci;
  1518. }
  1519. static GDScriptCompletionIdentifier _callable_type_from_method_info(const MethodInfo &p_method) {
  1520. GDScriptCompletionIdentifier ci;
  1521. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1522. ci.type.builtin_type = Variant::CALLABLE;
  1523. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1524. ci.type.is_constant = true;
  1525. ci.type.method_info = p_method;
  1526. return ci;
  1527. }
  1528. #define MAX_COMPLETION_RECURSION 100
  1529. struct RecursionCheck {
  1530. int *counter;
  1531. _FORCE_INLINE_ bool check() {
  1532. return (*counter) > MAX_COMPLETION_RECURSION;
  1533. }
  1534. RecursionCheck(int *p_counter) :
  1535. counter(p_counter) {
  1536. (*counter)++;
  1537. }
  1538. ~RecursionCheck() {
  1539. (*counter)--;
  1540. }
  1541. };
  1542. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type);
  1543. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1544. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1545. static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
  1546. if (p_expression) {
  1547. switch (p_expression->type) {
  1548. case GDScriptParser::Node::IDENTIFIER: {
  1549. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1550. if (id->name == p_name) {
  1551. return true;
  1552. }
  1553. } break;
  1554. case GDScriptParser::Node::CAST: {
  1555. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1556. return _is_expression_named_identifier(cn->operand, p_name);
  1557. } break;
  1558. default:
  1559. break;
  1560. }
  1561. }
  1562. return false;
  1563. }
  1564. // Creates a map of exemplary results for some functions that return a structured dictionary.
  1565. // Setting this example as value allows autocompletion to suggest the specific keys in some cases.
  1566. static HashMap<String, Dictionary> make_structure_samples() {
  1567. HashMap<String, Dictionary> res;
  1568. const Array arr;
  1569. {
  1570. Dictionary d;
  1571. d.set("major", 0);
  1572. d.set("minor", 0);
  1573. d.set("patch", 0);
  1574. d.set("hex", 0);
  1575. d.set("status", String());
  1576. d.set("build", String());
  1577. d.set("hash", String());
  1578. d.set("timestamp", 0);
  1579. d.set("string", String());
  1580. res["Engine::get_version_info"] = d;
  1581. }
  1582. {
  1583. Dictionary d;
  1584. d.set("lead_developers", arr);
  1585. d.set("founders", arr);
  1586. d.set("project_managers", arr);
  1587. d.set("developers", arr);
  1588. res["Engine::get_author_info"] = d;
  1589. }
  1590. {
  1591. Dictionary d;
  1592. d.set("platinum_sponsors", arr);
  1593. d.set("gold_sponsors", arr);
  1594. d.set("silver_sponsors", arr);
  1595. d.set("bronze_sponsors", arr);
  1596. d.set("mini_sponsors", arr);
  1597. d.set("gold_donors", arr);
  1598. d.set("silver_donors", arr);
  1599. d.set("bronze_donors", arr);
  1600. res["Engine::get_donor_info"] = d;
  1601. }
  1602. {
  1603. Dictionary d;
  1604. d.set("physical", -1);
  1605. d.set("free", -1);
  1606. d.set("available", -1);
  1607. d.set("stack", -1);
  1608. res["OS::get_memory_info"] = d;
  1609. }
  1610. {
  1611. Dictionary d;
  1612. d.set("year", 0);
  1613. d.set("month", 0);
  1614. d.set("day", 0);
  1615. d.set("weekday", 0);
  1616. d.set("hour", 0);
  1617. d.set("minute", 0);
  1618. d.set("second", 0);
  1619. d.set("dst", 0);
  1620. res["Time::get_datetime_dict_from_system"] = d;
  1621. }
  1622. {
  1623. Dictionary d;
  1624. d.set("year", 0);
  1625. d.set("month", 0);
  1626. d.set("day", 0);
  1627. d.set("weekday", 0);
  1628. d.set("hour", 0);
  1629. d.set("minute", 0);
  1630. d.set("second", 0);
  1631. res["Time::get_datetime_dict_from_unix_time"] = d;
  1632. }
  1633. {
  1634. Dictionary d;
  1635. d.set("year", 0);
  1636. d.set("month", 0);
  1637. d.set("day", 0);
  1638. d.set("weekday", 0);
  1639. res["Time::get_date_dict_from_system"] = d;
  1640. res["Time::get_date_dict_from_unix_time"] = d;
  1641. }
  1642. {
  1643. Dictionary d;
  1644. d.set("hour", 0);
  1645. d.set("minute", 0);
  1646. d.set("second", 0);
  1647. res["Time::get_time_dict_from_system"] = d;
  1648. res["Time::get_time_dict_from_unix_time"] = d;
  1649. }
  1650. {
  1651. Dictionary d;
  1652. d.set("bias", 0);
  1653. d.set("name", String());
  1654. res["Time::get_time_zone_from_system"] = d;
  1655. }
  1656. return res;
  1657. }
  1658. static const HashMap<String, Dictionary> structure_examples = make_structure_samples();
  1659. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1660. bool found = false;
  1661. if (p_expression == nullptr) {
  1662. return false;
  1663. }
  1664. static int recursion_depth = 0;
  1665. RecursionCheck recursion(&recursion_depth);
  1666. if (unlikely(recursion.check())) {
  1667. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  1668. }
  1669. if (p_expression->is_constant) {
  1670. // Already has a value, so just use that.
  1671. r_type = _type_from_variant(p_expression->reduced_value, p_context);
  1672. switch (p_expression->get_datatype().kind) {
  1673. case GDScriptParser::DataType::ENUM:
  1674. case GDScriptParser::DataType::CLASS:
  1675. r_type.type = p_expression->get_datatype();
  1676. break;
  1677. default:
  1678. break;
  1679. }
  1680. found = true;
  1681. } else {
  1682. switch (p_expression->type) {
  1683. case GDScriptParser::Node::IDENTIFIER: {
  1684. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1685. found = _guess_identifier_type(p_context, id, r_type);
  1686. } break;
  1687. case GDScriptParser::Node::DICTIONARY: {
  1688. // Try to recreate the dictionary.
  1689. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1690. Dictionary d;
  1691. bool full = true;
  1692. for (int i = 0; i < dn->elements.size(); i++) {
  1693. GDScriptCompletionIdentifier key;
  1694. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1695. if (!key.type.is_constant) {
  1696. full = false;
  1697. break;
  1698. }
  1699. GDScriptCompletionIdentifier value;
  1700. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1701. if (!value.type.is_constant) {
  1702. full = false;
  1703. break;
  1704. }
  1705. d[key.value] = value.value;
  1706. } else {
  1707. full = false;
  1708. break;
  1709. }
  1710. } else {
  1711. full = false;
  1712. break;
  1713. }
  1714. }
  1715. if (full) {
  1716. r_type.value = d;
  1717. r_type.type.is_constant = true;
  1718. }
  1719. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1720. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1721. r_type.type.builtin_type = Variant::DICTIONARY;
  1722. found = true;
  1723. } break;
  1724. case GDScriptParser::Node::ARRAY: {
  1725. // Try to recreate the array
  1726. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1727. Array a;
  1728. bool full = true;
  1729. a.resize(an->elements.size());
  1730. for (int i = 0; i < an->elements.size(); i++) {
  1731. GDScriptCompletionIdentifier value;
  1732. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1733. if (value.type.is_constant) {
  1734. a[i] = value.value;
  1735. } else {
  1736. full = false;
  1737. break;
  1738. }
  1739. } else {
  1740. full = false;
  1741. break;
  1742. }
  1743. }
  1744. if (full) {
  1745. // If not fully constant, setting this value is detrimental to the inference.
  1746. r_type.value = a;
  1747. r_type.type.is_constant = true;
  1748. }
  1749. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1750. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1751. r_type.type.builtin_type = Variant::ARRAY;
  1752. found = true;
  1753. } break;
  1754. case GDScriptParser::Node::CAST: {
  1755. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1756. GDScriptCompletionIdentifier value;
  1757. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1758. r_type.type = cn->get_datatype();
  1759. found = true;
  1760. }
  1761. } break;
  1762. case GDScriptParser::Node::CALL: {
  1763. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1764. GDScriptParser::CompletionContext c = p_context;
  1765. c.current_line = call->start_line;
  1766. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1767. GDScriptCompletionIdentifier base;
  1768. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1769. // Simple call, so base is 'self'.
  1770. if (p_context.current_class) {
  1771. if (call->is_super) {
  1772. base.type = p_context.current_class->base_type;
  1773. base.value = p_context.base;
  1774. } else {
  1775. base.type.kind = GDScriptParser::DataType::CLASS;
  1776. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1777. base.type.is_constant = true;
  1778. base.type.class_type = p_context.current_class;
  1779. base.value = p_context.base;
  1780. }
  1781. } else {
  1782. break;
  1783. }
  1784. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1785. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1786. found = false;
  1787. break;
  1788. }
  1789. } else {
  1790. break;
  1791. }
  1792. // Apply additional behavior aware inference that the analyzer can't do.
  1793. if (base.type.is_set()) {
  1794. // Maintain type for duplicate methods.
  1795. if (call->function_name == SNAME("duplicate")) {
  1796. if (base.type.builtin_type == Variant::OBJECT && (ClassDB::is_parent_class(base.type.native_type, SNAME("Resource")) || ClassDB::is_parent_class(base.type.native_type, SNAME("Node")))) {
  1797. r_type.type = base.type;
  1798. found = true;
  1799. break;
  1800. }
  1801. }
  1802. // Simulate generics for some typed array methods.
  1803. if (base.type.builtin_type == Variant::ARRAY && base.type.has_container_element_types() && (call->function_name == SNAME("back") || call->function_name == SNAME("front") || call->function_name == SNAME("get") || call->function_name == SNAME("max") || call->function_name == SNAME("min") || call->function_name == SNAME("pick_random") || call->function_name == SNAME("pop_at") || call->function_name == SNAME("pop_back") || call->function_name == SNAME("pop_front"))) {
  1804. r_type.type = base.type.get_container_element_type(0);
  1805. found = true;
  1806. break;
  1807. }
  1808. // Insert example values for functions which a structured dictionary response.
  1809. if (!base.type.is_meta_type) {
  1810. const Dictionary *example = structure_examples.getptr(base.type.native_type.operator String() + "::" + call->function_name);
  1811. if (example != nullptr) {
  1812. r_type = _type_from_variant(*example, p_context);
  1813. found = true;
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. if (!found) {
  1819. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1820. }
  1821. } break;
  1822. case GDScriptParser::Node::SUBSCRIPT: {
  1823. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1824. if (subscript->is_attribute) {
  1825. GDScriptParser::CompletionContext c = p_context;
  1826. c.current_line = subscript->start_line;
  1827. GDScriptCompletionIdentifier base;
  1828. if (!_guess_expression_type(c, subscript->base, base)) {
  1829. found = false;
  1830. break;
  1831. }
  1832. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1833. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1834. r_type = _type_from_variant(value, p_context);
  1835. found = true;
  1836. break;
  1837. }
  1838. const GDScriptParser::DictionaryNode *dn = nullptr;
  1839. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1840. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1841. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1842. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1843. }
  1844. if (dn) {
  1845. for (int i = 0; i < dn->elements.size(); i++) {
  1846. GDScriptCompletionIdentifier key;
  1847. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1848. continue;
  1849. }
  1850. if (key.value == String(subscript->attribute->name)) {
  1851. r_type.assigned_expression = dn->elements[i].value;
  1852. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. if (!found) {
  1858. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1859. }
  1860. } else {
  1861. if (subscript->index == nullptr) {
  1862. found = false;
  1863. break;
  1864. }
  1865. GDScriptParser::CompletionContext c = p_context;
  1866. c.current_line = subscript->start_line;
  1867. GDScriptCompletionIdentifier base;
  1868. if (!_guess_expression_type(c, subscript->base, base)) {
  1869. found = false;
  1870. break;
  1871. }
  1872. GDScriptCompletionIdentifier index;
  1873. if (!_guess_expression_type(c, subscript->index, index)) {
  1874. found = false;
  1875. break;
  1876. }
  1877. if (base.type.is_constant && index.type.is_constant) {
  1878. if (base.value.get_type() == Variant::DICTIONARY) {
  1879. Dictionary base_dict = base.value.operator Dictionary();
  1880. if (base_dict.get_key_validator().test_validate(index.value) && base_dict.has(index.value)) {
  1881. r_type = _type_from_variant(base_dict[index.value], p_context);
  1882. found = true;
  1883. break;
  1884. }
  1885. } else {
  1886. bool valid;
  1887. Variant value = base.value.get(index.value, &valid);
  1888. if (valid) {
  1889. r_type = _type_from_variant(value, p_context);
  1890. found = true;
  1891. break;
  1892. }
  1893. }
  1894. }
  1895. // Look if it is a dictionary node.
  1896. const GDScriptParser::DictionaryNode *dn = nullptr;
  1897. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1898. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1899. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1900. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1901. }
  1902. if (dn) {
  1903. for (int i = 0; i < dn->elements.size(); i++) {
  1904. GDScriptCompletionIdentifier key;
  1905. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1906. continue;
  1907. }
  1908. if (key.value == index.value) {
  1909. r_type.assigned_expression = dn->elements[i].value;
  1910. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1911. break;
  1912. }
  1913. }
  1914. }
  1915. // Look if it is an array node.
  1916. if (!found && index.value.is_num()) {
  1917. int idx = index.value;
  1918. const GDScriptParser::ArrayNode *an = nullptr;
  1919. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1920. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1921. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1922. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1923. }
  1924. if (an && idx >= 0 && an->elements.size() > idx) {
  1925. r_type.assigned_expression = an->elements[idx];
  1926. found = _guess_expression_type(c, an->elements[idx], r_type);
  1927. break;
  1928. }
  1929. }
  1930. // Look for valid indexing in other types
  1931. if (!found && (index.value.is_string() || index.value.get_type() == Variant::NODE_PATH)) {
  1932. StringName id = index.value;
  1933. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1934. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1935. Callable::CallError err;
  1936. Variant base_val;
  1937. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1938. bool valid = false;
  1939. Variant res = base_val.get(index.value, &valid);
  1940. if (valid) {
  1941. r_type = _type_from_variant(res, p_context);
  1942. r_type.value = Variant();
  1943. r_type.type.is_constant = false;
  1944. found = true;
  1945. }
  1946. }
  1947. }
  1948. } break;
  1949. case GDScriptParser::Node::BINARY_OPERATOR: {
  1950. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1951. if (op->variant_op == Variant::OP_MAX) {
  1952. break;
  1953. }
  1954. GDScriptParser::CompletionContext context = p_context;
  1955. context.current_line = op->start_line;
  1956. GDScriptCompletionIdentifier p1;
  1957. GDScriptCompletionIdentifier p2;
  1958. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1959. found = false;
  1960. break;
  1961. }
  1962. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1963. found = false;
  1964. break;
  1965. }
  1966. Callable::CallError ce;
  1967. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1968. Variant d1;
  1969. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1970. Variant d2;
  1971. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1972. Variant v1 = (v1_use_value) ? p1.value : d1;
  1973. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1974. Variant v2 = (v2_use_value) ? p2.value : d2;
  1975. // avoid potential invalid ops
  1976. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1977. v2 = 1;
  1978. v2_use_value = false;
  1979. }
  1980. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1981. v2 = 1.0;
  1982. v2_use_value = false;
  1983. }
  1984. Variant res;
  1985. bool valid;
  1986. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1987. if (!valid) {
  1988. found = false;
  1989. break;
  1990. }
  1991. r_type = _type_from_variant(res, p_context);
  1992. if (!v1_use_value || !v2_use_value) {
  1993. r_type.value = Variant();
  1994. r_type.type.is_constant = false;
  1995. }
  1996. found = true;
  1997. } break;
  1998. default:
  1999. break;
  2000. }
  2001. }
  2002. // It may have found a null, but that's never useful
  2003. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  2004. found = false;
  2005. }
  2006. // If the found type was not fully analyzed we analyze it now.
  2007. if (found && r_type.type.kind == GDScriptParser::DataType::CLASS && !r_type.type.class_type->resolved_body) {
  2008. Error err;
  2009. Ref<GDScriptParserRef> r = GDScriptCache::get_parser(r_type.type.script_path, GDScriptParserRef::FULLY_SOLVED, err);
  2010. }
  2011. // Check type hint last. For collections we want chance to get the actual value first
  2012. // This way we can detect types from the content of dictionaries and arrays
  2013. if (!found && p_expression->get_datatype().is_hard_type()) {
  2014. r_type.type = p_expression->get_datatype();
  2015. if (!r_type.assigned_expression) {
  2016. r_type.assigned_expression = p_expression;
  2017. }
  2018. found = true;
  2019. }
  2020. return found;
  2021. }
  2022. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type) {
  2023. static int recursion_depth = 0;
  2024. RecursionCheck recursion(&recursion_depth);
  2025. if (unlikely(recursion.check())) {
  2026. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  2027. }
  2028. // Look in blocks first.
  2029. int last_assign_line = -1;
  2030. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  2031. GDScriptCompletionIdentifier id_type;
  2032. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  2033. bool is_function_parameter = false;
  2034. bool can_be_local = true;
  2035. switch (p_identifier->source) {
  2036. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2037. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2038. case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
  2039. case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
  2040. case GDScriptParser::IdentifierNode::MEMBER_CLASS:
  2041. case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
  2042. case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
  2043. case GDScriptParser::IdentifierNode::NATIVE_CLASS:
  2044. can_be_local = false;
  2045. break;
  2046. default:
  2047. break;
  2048. }
  2049. if (can_be_local && suite && suite->has_local(p_identifier->name)) {
  2050. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier->name);
  2051. id_type.type = local.get_datatype();
  2052. // Check initializer as the first assignment.
  2053. switch (local.type) {
  2054. case GDScriptParser::SuiteNode::Local::VARIABLE:
  2055. if (local.variable->initializer) {
  2056. last_assign_line = local.variable->initializer->end_line;
  2057. last_assigned_expression = local.variable->initializer;
  2058. }
  2059. break;
  2060. case GDScriptParser::SuiteNode::Local::CONSTANT:
  2061. if (local.constant->initializer) {
  2062. last_assign_line = local.constant->initializer->end_line;
  2063. last_assigned_expression = local.constant->initializer;
  2064. }
  2065. break;
  2066. case GDScriptParser::SuiteNode::Local::PARAMETER:
  2067. if (local.parameter->initializer) {
  2068. last_assign_line = local.parameter->initializer->end_line;
  2069. last_assigned_expression = local.parameter->initializer;
  2070. }
  2071. is_function_parameter = true;
  2072. break;
  2073. default:
  2074. break;
  2075. }
  2076. } else {
  2077. if (p_context.current_class) {
  2078. GDScriptCompletionIdentifier base_identifier;
  2079. GDScriptCompletionIdentifier base;
  2080. base.value = p_context.base;
  2081. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2082. base.type.kind = GDScriptParser::DataType::CLASS;
  2083. base.type.class_type = p_context.current_class;
  2084. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  2085. if (_guess_identifier_type_from_base(p_context, base, p_identifier->name, base_identifier)) {
  2086. id_type = base_identifier;
  2087. }
  2088. }
  2089. }
  2090. while (suite) {
  2091. for (int i = 0; i < suite->statements.size(); i++) {
  2092. if (suite->statements[i]->end_line >= p_context.current_line) {
  2093. break;
  2094. }
  2095. switch (suite->statements[i]->type) {
  2096. case GDScriptParser::Node::ASSIGNMENT: {
  2097. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  2098. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  2099. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  2100. if (id->name == p_identifier->name && id->source == p_identifier->source) {
  2101. last_assign_line = assign->assigned_value->end_line;
  2102. last_assigned_expression = assign->assigned_value;
  2103. }
  2104. }
  2105. } break;
  2106. default:
  2107. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  2108. break;
  2109. }
  2110. }
  2111. if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::TYPE_TEST) {
  2112. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  2113. // Super dirty hack, but very useful.
  2114. // Credit: Zylann.
  2115. // TODO: this could be hacked to detect AND-ed conditions too...
  2116. const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(suite->parent_if->condition);
  2117. if (type_test->operand && type_test->test_type && type_test->operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->name == p_identifier->name && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->source == p_identifier->source) {
  2118. // Bingo.
  2119. GDScriptParser::CompletionContext c = p_context;
  2120. c.current_line = type_test->operand->start_line;
  2121. c.current_suite = suite;
  2122. if (type_test->test_datatype.is_hard_type()) {
  2123. id_type.type = type_test->test_datatype;
  2124. if (last_assign_line < c.current_line) {
  2125. // Override last assignment.
  2126. last_assign_line = c.current_line;
  2127. last_assigned_expression = nullptr;
  2128. }
  2129. }
  2130. }
  2131. }
  2132. suite = suite->parent_block;
  2133. }
  2134. if (last_assigned_expression && last_assign_line < p_context.current_line) {
  2135. GDScriptParser::CompletionContext c = p_context;
  2136. c.current_line = last_assign_line;
  2137. GDScriptCompletionIdentifier assigned_type;
  2138. if (_guess_expression_type(c, last_assigned_expression, assigned_type)) {
  2139. if (id_type.type.is_set() && (assigned_type.type.kind == GDScriptParser::DataType::VARIANT || (assigned_type.type.is_set() && !GDScriptAnalyzer::check_type_compatibility(id_type.type, assigned_type.type)))) {
  2140. // The assigned type is incompatible. The annotated type takes priority.
  2141. r_type = id_type;
  2142. r_type.assigned_expression = last_assigned_expression;
  2143. } else {
  2144. r_type = assigned_type;
  2145. }
  2146. return true;
  2147. }
  2148. }
  2149. if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
  2150. // Check if it's override of native function, then we can assume the type from the signature.
  2151. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  2152. while (base_type.is_set()) {
  2153. switch (base_type.kind) {
  2154. case GDScriptParser::DataType::CLASS:
  2155. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  2156. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  2157. if (parent_function->parameters_indices.has(p_identifier->name)) {
  2158. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier->name]];
  2159. if ((!id_type.type.is_set() || id_type.type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  2160. id_type.type = parameter->get_datatype();
  2161. }
  2162. if (parameter->initializer) {
  2163. GDScriptParser::CompletionContext c = p_context;
  2164. c.current_function = parent_function;
  2165. c.current_class = base_type.class_type;
  2166. c.base = nullptr;
  2167. if (_guess_expression_type(c, parameter->initializer, r_type)) {
  2168. return true;
  2169. }
  2170. }
  2171. }
  2172. }
  2173. base_type = base_type.class_type->base_type;
  2174. break;
  2175. case GDScriptParser::DataType::NATIVE: {
  2176. if (id_type.type.is_set() && !id_type.type.is_variant()) {
  2177. base_type = GDScriptParser::DataType();
  2178. break;
  2179. }
  2180. MethodInfo info;
  2181. if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
  2182. for (const PropertyInfo &E : info.arguments) {
  2183. if (E.name == p_identifier->name) {
  2184. r_type = _type_from_property(E);
  2185. return true;
  2186. }
  2187. }
  2188. }
  2189. base_type = GDScriptParser::DataType();
  2190. } break;
  2191. default:
  2192. break;
  2193. }
  2194. }
  2195. }
  2196. if (id_type.type.is_set() && !id_type.type.is_variant()) {
  2197. r_type = id_type;
  2198. return true;
  2199. }
  2200. // Check global scripts.
  2201. if (ScriptServer::is_global_class(p_identifier->name)) {
  2202. String script = ScriptServer::get_global_class_path(p_identifier->name);
  2203. if (script.to_lower().ends_with(".gd")) {
  2204. Ref<GDScriptParserRef> parser = p_context.parser->get_depended_parser_for(script);
  2205. if (parser.is_valid() && parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED) == OK) {
  2206. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2207. r_type.type.script_path = script;
  2208. r_type.type.class_type = parser->get_parser()->get_tree();
  2209. r_type.type.is_meta_type = true;
  2210. r_type.type.is_constant = false;
  2211. r_type.type.kind = GDScriptParser::DataType::CLASS;
  2212. r_type.value = Variant();
  2213. return true;
  2214. }
  2215. } else {
  2216. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier->name));
  2217. if (scr.is_valid()) {
  2218. r_type = _type_from_variant(scr, p_context);
  2219. r_type.type.is_meta_type = true;
  2220. return true;
  2221. }
  2222. }
  2223. return false;
  2224. }
  2225. // Check global variables (including autoloads).
  2226. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier->name)) {
  2227. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier->name], p_context);
  2228. return true;
  2229. }
  2230. // Check ClassDB.
  2231. if (GDScriptAnalyzer::class_exists(p_identifier->name)) {
  2232. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2233. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  2234. r_type.type.builtin_type = Variant::OBJECT;
  2235. r_type.type.native_type = p_identifier->name;
  2236. r_type.type.is_constant = true;
  2237. if (Engine::get_singleton()->has_singleton(p_identifier->name)) {
  2238. r_type.type.is_meta_type = false;
  2239. r_type.value = Engine::get_singleton()->get_singleton_object(p_identifier->name);
  2240. } else {
  2241. r_type.type.is_meta_type = true;
  2242. r_type.value = Variant();
  2243. }
  2244. return true;
  2245. }
  2246. return false;
  2247. }
  2248. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  2249. static int recursion_depth = 0;
  2250. RecursionCheck recursion(&recursion_depth);
  2251. if (unlikely(recursion.check())) {
  2252. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  2253. }
  2254. GDScriptParser::DataType base_type = p_base.type;
  2255. bool is_static = base_type.is_meta_type;
  2256. while (base_type.is_set()) {
  2257. switch (base_type.kind) {
  2258. case GDScriptParser::DataType::CLASS:
  2259. if (base_type.class_type->has_member(p_identifier)) {
  2260. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  2261. switch (member.type) {
  2262. case GDScriptParser::ClassNode::Member::CONSTANT:
  2263. r_type.type = member.constant->get_datatype();
  2264. if (member.constant->initializer && member.constant->initializer->is_constant) {
  2265. r_type.value = member.constant->initializer->reduced_value;
  2266. }
  2267. return true;
  2268. case GDScriptParser::ClassNode::Member::VARIABLE:
  2269. if (!is_static || member.variable->is_static) {
  2270. if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  2271. r_type.type = member.variable->get_datatype();
  2272. return true;
  2273. } else if (member.variable->initializer) {
  2274. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  2275. if (init->is_constant) {
  2276. r_type.value = init->reduced_value;
  2277. r_type = _type_from_variant(init->reduced_value, p_context);
  2278. return true;
  2279. } else if (init->start_line == p_context.current_line) {
  2280. return false;
  2281. // Detects if variable is assigned to itself
  2282. } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
  2283. if (member.variable->initializer->get_datatype().is_set()) {
  2284. r_type.type = member.variable->initializer->get_datatype();
  2285. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  2286. r_type.type = member.variable->get_datatype();
  2287. }
  2288. return true;
  2289. } else if (_guess_expression_type(p_context, init, r_type)) {
  2290. return true;
  2291. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  2292. r_type.type = init->get_datatype();
  2293. return true;
  2294. }
  2295. }
  2296. }
  2297. // TODO: Check assignments in constructor.
  2298. return false;
  2299. case GDScriptParser::ClassNode::Member::ENUM:
  2300. r_type.type = member.m_enum->get_datatype();
  2301. r_type.enumeration = member.m_enum->identifier->name;
  2302. return true;
  2303. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2304. r_type = _type_from_variant(member.enum_value.value, p_context);
  2305. return true;
  2306. case GDScriptParser::ClassNode::Member::SIGNAL:
  2307. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2308. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  2309. r_type.type.builtin_type = Variant::SIGNAL;
  2310. r_type.type.method_info = member.signal->method_info;
  2311. return true;
  2312. case GDScriptParser::ClassNode::Member::FUNCTION:
  2313. if (is_static && !member.function->is_static) {
  2314. return false;
  2315. }
  2316. r_type = _callable_type_from_method_info(member.function->info);
  2317. return true;
  2318. case GDScriptParser::ClassNode::Member::CLASS:
  2319. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2320. r_type.type.kind = GDScriptParser::DataType::CLASS;
  2321. r_type.type.class_type = member.m_class;
  2322. r_type.type.is_meta_type = true;
  2323. return true;
  2324. case GDScriptParser::ClassNode::Member::GROUP:
  2325. return false; // No-op, but silences warnings.
  2326. case GDScriptParser::ClassNode::Member::UNDEFINED:
  2327. return false; // Unreachable.
  2328. }
  2329. return false;
  2330. }
  2331. base_type = base_type.class_type->base_type;
  2332. break;
  2333. case GDScriptParser::DataType::SCRIPT: {
  2334. Ref<Script> scr = base_type.script_type;
  2335. if (scr.is_valid()) {
  2336. HashMap<StringName, Variant> constants;
  2337. scr->get_constants(&constants);
  2338. if (constants.has(p_identifier)) {
  2339. r_type = _type_from_variant(constants[p_identifier], p_context);
  2340. return true;
  2341. }
  2342. List<PropertyInfo> members;
  2343. if (is_static) {
  2344. scr->get_property_list(&members);
  2345. } else {
  2346. scr->get_script_property_list(&members);
  2347. }
  2348. for (const PropertyInfo &prop : members) {
  2349. if (prop.name == p_identifier) {
  2350. r_type = _type_from_property(prop);
  2351. return true;
  2352. }
  2353. }
  2354. if (scr->has_method(p_identifier)) {
  2355. MethodInfo mi = scr->get_method_info(p_identifier);
  2356. r_type = _callable_type_from_method_info(mi);
  2357. return true;
  2358. }
  2359. Ref<Script> parent = scr->get_base_script();
  2360. if (parent.is_valid()) {
  2361. base_type.script_type = parent;
  2362. } else {
  2363. base_type.kind = GDScriptParser::DataType::NATIVE;
  2364. base_type.builtin_type = Variant::OBJECT;
  2365. base_type.native_type = scr->get_instance_base_type();
  2366. }
  2367. } else {
  2368. return false;
  2369. }
  2370. } break;
  2371. case GDScriptParser::DataType::NATIVE: {
  2372. StringName class_name = base_type.native_type;
  2373. if (!GDScriptAnalyzer::class_exists(class_name)) {
  2374. return false;
  2375. }
  2376. // Skip constants since they're all integers. Type does not matter because int has no members.
  2377. PropertyInfo prop;
  2378. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  2379. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  2380. if (getter != StringName()) {
  2381. MethodBind *g = ClassDB::get_method(class_name, getter);
  2382. if (g) {
  2383. r_type = _type_from_property(g->get_return_info());
  2384. return true;
  2385. }
  2386. } else {
  2387. r_type = _type_from_property(prop);
  2388. return true;
  2389. }
  2390. }
  2391. MethodInfo method;
  2392. if (ClassDB::get_method_info(class_name, p_identifier, &method)) {
  2393. r_type = _callable_type_from_method_info(method);
  2394. return true;
  2395. }
  2396. if (ClassDB::has_enum(class_name, p_identifier)) {
  2397. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2398. r_type.type.kind = GDScriptParser::DataType::ENUM;
  2399. r_type.type.enum_type = p_identifier;
  2400. r_type.type.is_constant = true;
  2401. r_type.type.is_meta_type = true;
  2402. r_type.type.native_type = String(class_name) + "." + p_identifier;
  2403. return true;
  2404. }
  2405. return false;
  2406. } break;
  2407. case GDScriptParser::DataType::BUILTIN: {
  2408. if (Variant::has_builtin_method(base_type.builtin_type, p_identifier)) {
  2409. r_type = _callable_type_from_method_info(Variant::get_builtin_method_info(base_type.builtin_type, p_identifier));
  2410. return true;
  2411. } else {
  2412. Callable::CallError err;
  2413. Variant tmp;
  2414. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2415. if (err.error != Callable::CallError::CALL_OK) {
  2416. return false;
  2417. }
  2418. bool valid = false;
  2419. Variant res = tmp.get(p_identifier, &valid);
  2420. if (valid) {
  2421. r_type = _type_from_variant(res, p_context);
  2422. r_type.value = Variant();
  2423. r_type.type.is_constant = false;
  2424. return true;
  2425. }
  2426. }
  2427. return false;
  2428. } break;
  2429. default: {
  2430. return false;
  2431. } break;
  2432. }
  2433. }
  2434. return false;
  2435. }
  2436. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  2437. if (!p_context.current_suite) {
  2438. return;
  2439. }
  2440. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  2441. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  2442. break;
  2443. }
  2444. GDScriptParser::CompletionContext c = p_context;
  2445. switch (p_context.current_suite->statements[i]->type) {
  2446. case GDScriptParser::Node::FOR:
  2447. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  2448. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2449. break;
  2450. case GDScriptParser::Node::WHILE:
  2451. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  2452. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2453. break;
  2454. case GDScriptParser::Node::IF: {
  2455. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  2456. c.current_suite = _if->true_block;
  2457. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2458. if (_if->false_block) {
  2459. c.current_suite = _if->false_block;
  2460. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2461. }
  2462. } break;
  2463. case GDScriptParser::Node::MATCH: {
  2464. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  2465. for (int j = 0; j < match->branches.size(); j++) {
  2466. c.current_suite = match->branches[j]->block;
  2467. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2468. }
  2469. } break;
  2470. case GDScriptParser::Node::RETURN: {
  2471. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  2472. if (ret->return_value) {
  2473. if (ret->start_line > r_last_return_line) {
  2474. r_last_return_line = ret->start_line;
  2475. *r_last_returned_value = ret->return_value;
  2476. }
  2477. }
  2478. } break;
  2479. default:
  2480. break;
  2481. }
  2482. }
  2483. }
  2484. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  2485. static int recursion_depth = 0;
  2486. RecursionCheck recursion(&recursion_depth);
  2487. if (unlikely(recursion.check())) {
  2488. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  2489. }
  2490. GDScriptParser::DataType base_type = p_base.type;
  2491. bool is_static = base_type.is_meta_type;
  2492. if (is_static && p_method == SNAME("new")) {
  2493. r_type.type = base_type;
  2494. r_type.type.is_meta_type = false;
  2495. r_type.type.is_constant = false;
  2496. return true;
  2497. }
  2498. while (base_type.is_set() && !base_type.is_variant()) {
  2499. switch (base_type.kind) {
  2500. case GDScriptParser::DataType::CLASS:
  2501. if (base_type.class_type->has_function(p_method)) {
  2502. GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  2503. if (!is_static || method->is_static) {
  2504. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  2505. r_type.type = method->get_datatype();
  2506. return true;
  2507. }
  2508. int last_return_line = -1;
  2509. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  2510. GDScriptParser::CompletionContext c = p_context;
  2511. c.current_class = base_type.class_type;
  2512. c.current_function = method;
  2513. c.current_suite = method->body;
  2514. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  2515. if (last_returned_value) {
  2516. c.current_line = c.current_suite->end_line;
  2517. if (_guess_expression_type(c, last_returned_value, r_type)) {
  2518. return true;
  2519. }
  2520. }
  2521. }
  2522. }
  2523. base_type = base_type.class_type->base_type;
  2524. break;
  2525. case GDScriptParser::DataType::SCRIPT: {
  2526. Ref<Script> scr = base_type.script_type;
  2527. if (scr.is_valid()) {
  2528. List<MethodInfo> methods;
  2529. scr->get_script_method_list(&methods);
  2530. for (const MethodInfo &mi : methods) {
  2531. if (mi.name == p_method) {
  2532. r_type = _type_from_property(mi.return_val);
  2533. return true;
  2534. }
  2535. }
  2536. Ref<Script> base_script = scr->get_base_script();
  2537. if (base_script.is_valid()) {
  2538. base_type.script_type = base_script;
  2539. } else {
  2540. base_type.kind = GDScriptParser::DataType::NATIVE;
  2541. base_type.builtin_type = Variant::OBJECT;
  2542. base_type.native_type = scr->get_instance_base_type();
  2543. }
  2544. } else {
  2545. return false;
  2546. }
  2547. } break;
  2548. case GDScriptParser::DataType::NATIVE: {
  2549. if (!GDScriptAnalyzer::class_exists(base_type.native_type)) {
  2550. return false;
  2551. }
  2552. MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
  2553. if (mb) {
  2554. r_type = _type_from_property(mb->get_return_info());
  2555. return true;
  2556. }
  2557. return false;
  2558. } break;
  2559. case GDScriptParser::DataType::BUILTIN: {
  2560. Callable::CallError err;
  2561. Variant tmp;
  2562. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2563. if (err.error != Callable::CallError::CALL_OK) {
  2564. return false;
  2565. }
  2566. List<MethodInfo> methods;
  2567. tmp.get_method_list(&methods);
  2568. for (const MethodInfo &mi : methods) {
  2569. if (mi.name == p_method) {
  2570. r_type = _type_from_property(mi.return_val);
  2571. return true;
  2572. }
  2573. }
  2574. return false;
  2575. } break;
  2576. default: {
  2577. return false;
  2578. }
  2579. }
  2580. }
  2581. return false;
  2582. }
  2583. static bool _guess_expecting_callable(GDScriptParser::CompletionContext &p_context) {
  2584. if (p_context.call.call != nullptr && p_context.call.call->type == GDScriptParser::Node::CALL) {
  2585. GDScriptParser::CallNode *call_node = static_cast<GDScriptParser::CallNode *>(p_context.call.call);
  2586. GDScriptCompletionIdentifier ci;
  2587. if (_guess_expression_type(p_context, call_node->callee, ci)) {
  2588. if (ci.type.kind == GDScriptParser::DataType::BUILTIN && ci.type.builtin_type == Variant::CALLABLE) {
  2589. if (p_context.call.argument >= 0 && p_context.call.argument < ci.type.method_info.arguments.size()) {
  2590. return ci.type.method_info.arguments.get(p_context.call.argument).type == Variant::CALLABLE;
  2591. }
  2592. }
  2593. }
  2594. }
  2595. return false;
  2596. }
  2597. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  2598. if (!p_enum_hint.contains_char('.')) {
  2599. // Global constant or in the current class.
  2600. StringName current_enum = p_enum_hint;
  2601. if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) {
  2602. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  2603. for (int i = 0; i < _enum->values.size(); i++) {
  2604. ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2605. r_result.insert(option.display, option);
  2606. }
  2607. } else {
  2608. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  2609. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  2610. ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2611. r_result.insert(option.display, option);
  2612. }
  2613. }
  2614. }
  2615. } else {
  2616. String class_name = p_enum_hint.get_slicec('.', 0);
  2617. String enum_name = p_enum_hint.get_slicec('.', 1);
  2618. if (!GDScriptAnalyzer::class_exists(class_name)) {
  2619. return;
  2620. }
  2621. List<StringName> enum_constants;
  2622. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  2623. for (const StringName &E : enum_constants) {
  2624. String candidate = class_name + "." + E;
  2625. int location = _get_enum_constant_location(class_name, E);
  2626. ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  2627. r_result.insert(option.display, option);
  2628. }
  2629. }
  2630. }
  2631. static void _list_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const GDScriptParser::CallNode *p_call, int p_argidx, bool p_static, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
  2632. Variant base = p_base.value;
  2633. GDScriptParser::DataType base_type = p_base.type;
  2634. const StringName &method = p_call->function_name;
  2635. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2636. const bool use_string_names = EDITOR_GET("text_editor/completion/add_string_name_literals");
  2637. const bool use_node_paths = EDITOR_GET("text_editor/completion/add_node_path_literals");
  2638. while (base_type.is_set() && !base_type.is_variant()) {
  2639. switch (base_type.kind) {
  2640. case GDScriptParser::DataType::CLASS: {
  2641. if (base_type.is_meta_type && method == SNAME("new")) {
  2642. const GDScriptParser::ClassNode *current = base_type.class_type;
  2643. do {
  2644. if (current->has_member("_init")) {
  2645. const GDScriptParser::ClassNode::Member &member = current->get_member("_init");
  2646. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2647. r_arghint = base_type.class_type->get_datatype().to_string() + " new" + _make_arguments_hint(member.function, p_argidx, true);
  2648. return;
  2649. }
  2650. }
  2651. current = current->base_type.class_type;
  2652. } while (current != nullptr);
  2653. r_arghint = base_type.class_type->get_datatype().to_string() + " new()";
  2654. return;
  2655. }
  2656. if (base_type.class_type->has_member(method)) {
  2657. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(method);
  2658. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2659. r_arghint = _make_arguments_hint(member.function, p_argidx);
  2660. return;
  2661. }
  2662. }
  2663. base_type = base_type.class_type->base_type;
  2664. } break;
  2665. case GDScriptParser::DataType::SCRIPT: {
  2666. if (base_type.script_type->is_valid() && base_type.script_type->has_method(method)) {
  2667. r_arghint = _make_arguments_hint(base_type.script_type->get_method_info(method), p_argidx);
  2668. return;
  2669. }
  2670. Ref<Script> base_script = base_type.script_type->get_base_script();
  2671. if (base_script.is_valid()) {
  2672. base_type.script_type = base_script;
  2673. } else {
  2674. base_type.kind = GDScriptParser::DataType::NATIVE;
  2675. base_type.builtin_type = Variant::OBJECT;
  2676. base_type.native_type = base_type.script_type->get_instance_base_type();
  2677. }
  2678. } break;
  2679. case GDScriptParser::DataType::NATIVE: {
  2680. StringName class_name = base_type.native_type;
  2681. if (!GDScriptAnalyzer::class_exists(class_name)) {
  2682. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2683. break;
  2684. }
  2685. MethodInfo info;
  2686. int method_args = 0;
  2687. if (ClassDB::get_method_info(class_name, method, &info)) {
  2688. method_args = info.arguments.size();
  2689. if (base.get_type() == Variant::OBJECT) {
  2690. Object *obj = base.operator Object *();
  2691. if (obj) {
  2692. List<String> options;
  2693. obj->get_argument_options(method, p_argidx, &options);
  2694. for (String &opt : options) {
  2695. // Handle user preference.
  2696. if (opt.is_quoted()) {
  2697. opt = opt.unquote().quote(quote_style);
  2698. if (use_string_names && info.arguments[p_argidx].type == Variant::STRING_NAME) {
  2699. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2700. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2701. if (literal->value.get_type() == Variant::STRING) {
  2702. opt = "&" + opt;
  2703. }
  2704. } else {
  2705. opt = "&" + opt;
  2706. }
  2707. } else if (use_node_paths && info.arguments[p_argidx].type == Variant::NODE_PATH) {
  2708. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2709. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2710. if (literal->value.get_type() == Variant::STRING) {
  2711. opt = "^" + opt;
  2712. }
  2713. } else {
  2714. opt = "^" + opt;
  2715. }
  2716. }
  2717. }
  2718. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2719. r_result.insert(option.display, option);
  2720. }
  2721. }
  2722. }
  2723. if (p_argidx < method_args) {
  2724. const PropertyInfo &arg_info = info.arguments[p_argidx];
  2725. if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  2726. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  2727. }
  2728. }
  2729. r_arghint = _make_arguments_hint(info, p_argidx);
  2730. }
  2731. if (p_argidx == 1 && p_call && ClassDB::is_parent_class(class_name, SNAME("Tween")) && method == SNAME("tween_property")) {
  2732. // Get tweened objects properties.
  2733. if (p_call->arguments.is_empty()) {
  2734. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2735. break;
  2736. }
  2737. GDScriptParser::ExpressionNode *tweened_object = p_call->arguments[0];
  2738. if (!tweened_object) {
  2739. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2740. break;
  2741. }
  2742. StringName native_type = tweened_object->datatype.native_type;
  2743. switch (tweened_object->datatype.kind) {
  2744. case GDScriptParser::DataType::SCRIPT: {
  2745. Ref<Script> script = tweened_object->datatype.script_type;
  2746. native_type = script->get_instance_base_type();
  2747. int n = 0;
  2748. while (script.is_valid()) {
  2749. List<PropertyInfo> properties;
  2750. script->get_script_property_list(&properties);
  2751. for (const PropertyInfo &E : properties) {
  2752. if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
  2753. continue;
  2754. }
  2755. String name = E.name.quote(quote_style);
  2756. if (use_node_paths) {
  2757. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2758. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2759. if (literal->value.get_type() == Variant::STRING) {
  2760. name = "^" + name;
  2761. }
  2762. } else {
  2763. name = "^" + name;
  2764. }
  2765. }
  2766. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
  2767. r_result.insert(option.display, option);
  2768. }
  2769. script = script->get_base_script();
  2770. n++;
  2771. }
  2772. } break;
  2773. case GDScriptParser::DataType::CLASS: {
  2774. GDScriptParser::ClassNode *clss = tweened_object->datatype.class_type;
  2775. native_type = clss->base_type.native_type;
  2776. int n = 0;
  2777. while (clss) {
  2778. for (GDScriptParser::ClassNode::Member member : clss->members) {
  2779. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  2780. String name = member.get_name().quote(quote_style);
  2781. if (use_node_paths) {
  2782. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2783. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2784. if (literal->value.get_type() == Variant::STRING) {
  2785. name = "^" + name;
  2786. }
  2787. } else {
  2788. name = "^" + name;
  2789. }
  2790. }
  2791. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::CodeCompletionLocation::LOCATION_LOCAL + n);
  2792. r_result.insert(option.display, option);
  2793. }
  2794. }
  2795. if (clss->base_type.kind == GDScriptParser::DataType::Kind::CLASS) {
  2796. clss = clss->base_type.class_type;
  2797. n++;
  2798. } else {
  2799. native_type = clss->base_type.native_type;
  2800. clss = nullptr;
  2801. }
  2802. }
  2803. } break;
  2804. default:
  2805. break;
  2806. }
  2807. List<PropertyInfo> properties;
  2808. ClassDB::get_property_list(native_type, &properties);
  2809. for (const PropertyInfo &E : properties) {
  2810. if (E.usage & (PROPERTY_USAGE_SUBGROUP | PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY | PROPERTY_USAGE_INTERNAL)) {
  2811. continue;
  2812. }
  2813. String name = E.name.quote(quote_style);
  2814. if (use_node_paths) {
  2815. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2816. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2817. if (literal->value.get_type() == Variant::STRING) {
  2818. name = "^" + name;
  2819. }
  2820. } else {
  2821. name = "^" + name;
  2822. }
  2823. }
  2824. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  2825. r_result.insert(option.display, option);
  2826. }
  2827. }
  2828. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (method == SNAME("get_node") || method == SNAME("has_node"))) {
  2829. // Get autoloads
  2830. List<PropertyInfo> props;
  2831. ProjectSettings::get_singleton()->get_property_list(&props);
  2832. for (const PropertyInfo &E : props) {
  2833. String s = E.name;
  2834. if (!s.begins_with("autoload/")) {
  2835. continue;
  2836. }
  2837. String name = s.get_slicec('/', 1);
  2838. String path = ("/root/" + name).quote(quote_style);
  2839. if (use_node_paths) {
  2840. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2841. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2842. if (literal->value.get_type() == Variant::STRING) {
  2843. path = "^" + path;
  2844. }
  2845. } else {
  2846. path = "^" + path;
  2847. }
  2848. }
  2849. ScriptLanguage::CodeCompletionOption option(path, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2850. r_result.insert(option.display, option);
  2851. }
  2852. }
  2853. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && method.operator String().contains("action")) {
  2854. // Get input actions
  2855. List<PropertyInfo> props;
  2856. ProjectSettings::get_singleton()->get_property_list(&props);
  2857. for (const PropertyInfo &E : props) {
  2858. String s = E.name;
  2859. if (!s.begins_with("input/")) {
  2860. continue;
  2861. }
  2862. String name = s.get_slicec('/', 1).quote(quote_style);
  2863. if (use_string_names) {
  2864. if (p_call->arguments.size() > p_argidx && p_call->arguments[p_argidx] && p_call->arguments[p_argidx]->type == GDScriptParser::Node::LITERAL) {
  2865. GDScriptParser::LiteralNode *literal = static_cast<GDScriptParser::LiteralNode *>(p_call->arguments[p_argidx]);
  2866. if (literal->value.get_type() == Variant::STRING) {
  2867. name = "&" + name;
  2868. }
  2869. } else {
  2870. name = "&" + name;
  2871. }
  2872. }
  2873. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2874. r_result.insert(option.display, option);
  2875. }
  2876. }
  2877. if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
  2878. if (p_argidx == 0 && method == SNAME("change_scene_to_file") && ClassDB::is_parent_class(class_name, SNAME("SceneTree"))) {
  2879. HashMap<String, ScriptLanguage::CodeCompletionOption> list;
  2880. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene"));
  2881. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &key_value_pair : list) {
  2882. ScriptLanguage::CodeCompletionOption option = key_value_pair.value;
  2883. r_result.insert(option.display, option);
  2884. }
  2885. }
  2886. }
  2887. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2888. } break;
  2889. case GDScriptParser::DataType::BUILTIN: {
  2890. if (base.get_type() == Variant::NIL) {
  2891. Callable::CallError err;
  2892. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2893. if (err.error != Callable::CallError::CALL_OK) {
  2894. return;
  2895. }
  2896. }
  2897. List<MethodInfo> methods;
  2898. base.get_method_list(&methods);
  2899. for (const MethodInfo &E : methods) {
  2900. if (E.name == method) {
  2901. r_arghint = _make_arguments_hint(E, p_argidx);
  2902. return;
  2903. }
  2904. }
  2905. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2906. } break;
  2907. default: {
  2908. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2909. } break;
  2910. }
  2911. }
  2912. }
  2913. static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) {
  2914. if (p_context.base == nullptr) {
  2915. return false;
  2916. }
  2917. const GDScriptParser::GetNodeNode *get_node = nullptr;
  2918. switch (p_subscript->base->type) {
  2919. case GDScriptParser::Node::GET_NODE: {
  2920. get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base);
  2921. } break;
  2922. case GDScriptParser::Node::IDENTIFIER: {
  2923. const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base);
  2924. switch (identifier_node->source) {
  2925. case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: {
  2926. if (p_context.current_class != nullptr) {
  2927. const StringName &member_name = identifier_node->name;
  2928. const GDScriptParser::ClassNode *current_class = p_context.current_class;
  2929. if (current_class->has_member(member_name)) {
  2930. const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name);
  2931. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  2932. const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable);
  2933. if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) {
  2934. get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer);
  2935. }
  2936. }
  2937. }
  2938. }
  2939. } break;
  2940. case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: {
  2941. // TODO: Do basic assignment flow analysis like in `_guess_expression_type`.
  2942. const GDScriptParser::SuiteNode::Local local = identifier_node->suite->get_local(identifier_node->name);
  2943. switch (local.type) {
  2944. case GDScriptParser::SuiteNode::Local::CONSTANT: {
  2945. if (local.constant->initializer && local.constant->initializer->type == GDScriptParser::Node::GET_NODE) {
  2946. get_node = static_cast<GDScriptParser::GetNodeNode *>(local.constant->initializer);
  2947. }
  2948. } break;
  2949. case GDScriptParser::SuiteNode::Local::VARIABLE: {
  2950. if (local.variable->initializer && local.variable->initializer->type == GDScriptParser::Node::GET_NODE) {
  2951. get_node = static_cast<GDScriptParser::GetNodeNode *>(local.variable->initializer);
  2952. }
  2953. } break;
  2954. default: {
  2955. } break;
  2956. }
  2957. } break;
  2958. default: {
  2959. } break;
  2960. }
  2961. } break;
  2962. default: {
  2963. } break;
  2964. }
  2965. if (get_node != nullptr) {
  2966. const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path));
  2967. if (node != nullptr) {
  2968. GDScriptParser::DataType assigned_type = _type_from_variant(node, p_context).type;
  2969. GDScriptParser::DataType base_type = p_subscript->base->datatype;
  2970. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER && base_type.type_source == GDScriptParser::DataType::ANNOTATED_EXPLICIT && (assigned_type.kind != base_type.kind || assigned_type.script_path != base_type.script_path || assigned_type.native_type != base_type.native_type)) {
  2971. // Annotated type takes precedence.
  2972. return false;
  2973. }
  2974. if (r_base != nullptr) {
  2975. *r_base = node;
  2976. }
  2977. r_base_type.type_source = GDScriptParser::DataType::INFERRED;
  2978. r_base_type.builtin_type = Variant::OBJECT;
  2979. r_base_type.native_type = node->get_class_name();
  2980. Ref<Script> scr = node->get_script();
  2981. if (scr.is_null()) {
  2982. r_base_type.kind = GDScriptParser::DataType::NATIVE;
  2983. } else {
  2984. r_base_type.kind = GDScriptParser::DataType::SCRIPT;
  2985. r_base_type.script_type = scr;
  2986. }
  2987. return true;
  2988. }
  2989. }
  2990. return false;
  2991. }
  2992. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2993. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2994. if (p_argidx == 0 && bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) {
  2995. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2996. }
  2997. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2998. r_arghint = _make_arguments_hint(mi, p_argidx);
  2999. return;
  3000. } else if (p_call->type != GDScriptParser::Node::CALL) {
  3001. return;
  3002. }
  3003. Variant base;
  3004. GDScriptParser::DataType base_type;
  3005. bool _static = false;
  3006. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  3007. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  3008. if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  3009. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  3010. if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  3011. const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
  3012. Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
  3013. if (method_type < Variant::VARIANT_MAX) {
  3014. Variant v;
  3015. Callable::CallError err;
  3016. Variant::construct(method_type, v, nullptr, 0, err);
  3017. if (err.error != Callable::CallError::CALL_OK) {
  3018. return;
  3019. }
  3020. List<MethodInfo> methods;
  3021. v.get_method_list(&methods);
  3022. for (MethodInfo &E : methods) {
  3023. if (p_argidx >= E.arguments.size()) {
  3024. continue;
  3025. }
  3026. if (E.name == call->function_name) {
  3027. r_arghint += _make_arguments_hint(E, p_argidx);
  3028. return;
  3029. }
  3030. }
  3031. }
  3032. }
  3033. if (subscript->is_attribute) {
  3034. bool found_type = _get_subscript_type(p_context, subscript, base_type, &base);
  3035. if (!found_type) {
  3036. GDScriptCompletionIdentifier ci;
  3037. if (_guess_expression_type(p_context, subscript->base, ci)) {
  3038. base_type = ci.type;
  3039. base = ci.value;
  3040. } else {
  3041. return;
  3042. }
  3043. }
  3044. _static = base_type.is_meta_type;
  3045. }
  3046. } else if (Variant::has_utility_function(call->function_name)) {
  3047. MethodInfo info = Variant::get_utility_function_info(call->function_name);
  3048. r_arghint = _make_arguments_hint(info, p_argidx);
  3049. return;
  3050. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  3051. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  3052. r_arghint = _make_arguments_hint(info, p_argidx);
  3053. return;
  3054. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  3055. // Complete constructor.
  3056. List<MethodInfo> constructors;
  3057. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  3058. int i = 0;
  3059. for (const MethodInfo &E : constructors) {
  3060. if (p_argidx >= E.arguments.size()) {
  3061. continue;
  3062. }
  3063. if (i > 0) {
  3064. r_arghint += "\n";
  3065. }
  3066. r_arghint += _make_arguments_hint(E, p_argidx);
  3067. i++;
  3068. }
  3069. return;
  3070. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  3071. base = p_context.base;
  3072. if (p_context.current_class) {
  3073. base_type = p_context.current_class->get_datatype();
  3074. _static = !p_context.current_function || p_context.current_function->is_static;
  3075. }
  3076. } else {
  3077. return;
  3078. }
  3079. GDScriptCompletionIdentifier ci;
  3080. ci.type = base_type;
  3081. ci.value = base;
  3082. _list_call_arguments(p_context, ci, call, p_argidx, _static, r_result, r_arghint);
  3083. r_forced = r_result.size() > 0;
  3084. }
  3085. ::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  3086. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  3087. GDScriptParser parser;
  3088. GDScriptAnalyzer analyzer(&parser);
  3089. parser.parse(p_code, p_path, true);
  3090. analyzer.analyze();
  3091. r_forced = false;
  3092. HashMap<String, ScriptLanguage::CodeCompletionOption> options;
  3093. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  3094. if (completion_context.current_class != nullptr && completion_context.current_class->outer == nullptr) {
  3095. completion_context.base = p_owner;
  3096. }
  3097. bool is_function = false;
  3098. switch (completion_context.type) {
  3099. case GDScriptParser::COMPLETION_NONE:
  3100. break;
  3101. case GDScriptParser::COMPLETION_ANNOTATION: {
  3102. List<MethodInfo> annotations;
  3103. parser.get_annotation_list(&annotations);
  3104. for (const MethodInfo &E : annotations) {
  3105. ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3106. if (E.arguments.size() > 0) {
  3107. option.insert_text += "(";
  3108. }
  3109. options.insert(option.display, option);
  3110. }
  3111. r_forced = true;
  3112. } break;
  3113. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  3114. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  3115. break;
  3116. }
  3117. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  3118. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options, r_call_hint);
  3119. r_forced = true;
  3120. } break;
  3121. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  3122. // Constants.
  3123. {
  3124. List<StringName> constants;
  3125. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  3126. for (const StringName &E : constants) {
  3127. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  3128. bool valid = false;
  3129. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
  3130. if (valid) {
  3131. option.default_value = default_value;
  3132. }
  3133. options.insert(option.display, option);
  3134. }
  3135. }
  3136. // Methods.
  3137. {
  3138. List<StringName> methods;
  3139. Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
  3140. for (const StringName &E : methods) {
  3141. if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
  3142. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  3143. if (!_guess_expecting_callable(completion_context)) {
  3144. if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
  3145. option.insert_text += "(";
  3146. } else {
  3147. option.insert_text += "()";
  3148. }
  3149. }
  3150. options.insert(option.display, option);
  3151. }
  3152. }
  3153. }
  3154. } break;
  3155. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  3156. _list_available_types(true, completion_context, options);
  3157. r_forced = true;
  3158. } break;
  3159. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  3160. ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3161. options.insert(option.display, option);
  3162. }
  3163. [[fallthrough]];
  3164. case GDScriptParser::COMPLETION_TYPE_NAME: {
  3165. _list_available_types(false, completion_context, options);
  3166. r_forced = true;
  3167. } break;
  3168. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  3169. _list_available_types(false, completion_context, options);
  3170. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3171. options.insert(get.display, get);
  3172. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3173. options.insert(set.display, set);
  3174. r_forced = true;
  3175. } break;
  3176. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  3177. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3178. options.insert(get.display, get);
  3179. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  3180. options.insert(set.display, set);
  3181. r_forced = true;
  3182. } break;
  3183. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  3184. if (!completion_context.current_class) {
  3185. break;
  3186. }
  3187. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  3188. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  3189. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3190. continue;
  3191. }
  3192. if (member.function->is_static) {
  3193. continue;
  3194. }
  3195. ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  3196. options.insert(option.display, option);
  3197. }
  3198. r_forced = true;
  3199. } break;
  3200. case GDScriptParser::COMPLETION_ASSIGN: {
  3201. GDScriptCompletionIdentifier type;
  3202. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  3203. break;
  3204. }
  3205. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  3206. _find_identifiers(completion_context, false, true, options, 0);
  3207. r_forced = true;
  3208. break;
  3209. }
  3210. if (!type.enumeration.is_empty()) {
  3211. _find_enumeration_candidates(completion_context, type.enumeration, options);
  3212. r_forced = options.size() > 0;
  3213. } else {
  3214. _find_identifiers(completion_context, false, true, options, 0);
  3215. r_forced = true;
  3216. }
  3217. } break;
  3218. case GDScriptParser::COMPLETION_METHOD:
  3219. is_function = true;
  3220. [[fallthrough]];
  3221. case GDScriptParser::COMPLETION_IDENTIFIER: {
  3222. _find_identifiers(completion_context, is_function, !_guess_expecting_callable(completion_context), options, 0);
  3223. } break;
  3224. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  3225. is_function = true;
  3226. [[fallthrough]];
  3227. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  3228. r_forced = true;
  3229. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  3230. if (attr->base) {
  3231. GDScriptCompletionIdentifier base;
  3232. bool found_type = _get_subscript_type(completion_context, attr, base.type);
  3233. if (!found_type && !_guess_expression_type(completion_context, attr->base, base)) {
  3234. break;
  3235. }
  3236. _find_identifiers_in_base(base, is_function, false, !_guess_expecting_callable(completion_context), options, 0);
  3237. }
  3238. } break;
  3239. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  3240. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  3241. GDScriptCompletionIdentifier base;
  3242. const bool res = _guess_expression_type(completion_context, subscript->base, base);
  3243. // If the type is not known, we assume it is BUILTIN, since indices on arrays is the most common use case.
  3244. if (!subscript->is_attribute && (!res || base.type.kind == GDScriptParser::DataType::BUILTIN || base.type.is_variant())) {
  3245. if (base.value.get_type() == Variant::DICTIONARY) {
  3246. List<PropertyInfo> members;
  3247. base.value.get_property_list(&members);
  3248. for (const PropertyInfo &E : members) {
  3249. ScriptLanguage::CodeCompletionOption option(E.name.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, ScriptLanguage::LOCATION_LOCAL);
  3250. options.insert(option.display, option);
  3251. }
  3252. }
  3253. if (!subscript->index || subscript->index->type != GDScriptParser::Node::LITERAL) {
  3254. _find_identifiers(completion_context, false, !_guess_expecting_callable(completion_context), options, 0);
  3255. }
  3256. } else if (res) {
  3257. if (!subscript->is_attribute) {
  3258. // Quote the options if they are not accessed as attribute.
  3259. HashMap<String, ScriptLanguage::CodeCompletionOption> opt;
  3260. _find_identifiers_in_base(base, false, false, false, opt, 0);
  3261. for (const KeyValue<String, CodeCompletionOption> &E : opt) {
  3262. ScriptLanguage::CodeCompletionOption option(E.value.insert_text.quote(quote_style), E.value.kind, E.value.location);
  3263. options.insert(option.display, option);
  3264. }
  3265. } else {
  3266. _find_identifiers_in_base(base, false, false, !_guess_expecting_callable(completion_context), options, 0);
  3267. }
  3268. }
  3269. } break;
  3270. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  3271. if (!completion_context.current_class) {
  3272. break;
  3273. }
  3274. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  3275. ERR_FAIL_INDEX_V_MSG(completion_context.type_chain_index - 1, type->type_chain.size(), Error::ERR_BUG, "Could not complete type argument with out of bounds type chain index.");
  3276. GDScriptCompletionIdentifier base;
  3277. if (_guess_identifier_type(completion_context, type->type_chain[0], base)) {
  3278. bool found = true;
  3279. for (int i = 1; i < completion_context.type_chain_index; i++) {
  3280. GDScriptCompletionIdentifier ci;
  3281. found = _guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci);
  3282. base = ci;
  3283. if (!found) {
  3284. break;
  3285. }
  3286. }
  3287. if (found) {
  3288. _find_identifiers_in_base(base, false, true, true, options, 0);
  3289. }
  3290. }
  3291. r_forced = true;
  3292. } break;
  3293. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  3294. if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
  3295. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  3296. r_forced = true;
  3297. }
  3298. } break;
  3299. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  3300. if (!completion_context.node) {
  3301. break;
  3302. }
  3303. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  3304. } break;
  3305. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  3306. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  3307. GDScriptParser::FunctionNode *function_node = static_cast<GDScriptParser::FunctionNode *>(completion_context.node);
  3308. bool is_static = function_node != nullptr && function_node->is_static;
  3309. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  3310. switch (native_type.kind) {
  3311. case GDScriptParser::DataType::CLASS: {
  3312. for (const GDScriptParser::ClassNode::Member &member : native_type.class_type->members) {
  3313. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3314. continue;
  3315. }
  3316. if (options.has(member.function->identifier->name)) {
  3317. continue;
  3318. }
  3319. if (completion_context.current_class->has_function(member.get_name()) && completion_context.current_class->get_member(member.get_name()).function != function_node) {
  3320. continue;
  3321. }
  3322. if (is_static != member.function->is_static) {
  3323. continue;
  3324. }
  3325. String display_name = member.function->identifier->name;
  3326. display_name += member.function->signature + ":";
  3327. ScriptLanguage::CodeCompletionOption option(display_name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  3328. options.insert(member.function->identifier->name, option); // Insert name instead of display to track duplicates.
  3329. }
  3330. native_type = native_type.class_type->base_type;
  3331. } break;
  3332. default: {
  3333. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  3334. } break;
  3335. }
  3336. }
  3337. if (!native_type.is_set()) {
  3338. break;
  3339. }
  3340. StringName class_name = native_type.native_type;
  3341. if (!GDScriptAnalyzer::class_exists(class_name)) {
  3342. break;
  3343. }
  3344. const bool type_hints = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  3345. List<MethodInfo> virtual_methods;
  3346. if (is_static) {
  3347. // Not truly a virtual method, but can also be "overridden".
  3348. MethodInfo static_init("_static_init");
  3349. static_init.return_val.type = Variant::NIL;
  3350. static_init.flags |= METHOD_FLAG_STATIC | METHOD_FLAG_VIRTUAL;
  3351. virtual_methods.push_back(static_init);
  3352. } else {
  3353. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  3354. }
  3355. for (const MethodInfo &mi : virtual_methods) {
  3356. if (options.has(mi.name)) {
  3357. continue;
  3358. }
  3359. if (completion_context.current_class->has_function(mi.name) && completion_context.current_class->get_member(mi.name).function != function_node) {
  3360. continue;
  3361. }
  3362. String method_hint = mi.name;
  3363. if (method_hint.contains_char(':')) {
  3364. method_hint = method_hint.get_slicec(':', 0);
  3365. }
  3366. method_hint += "(";
  3367. for (int64_t i = 0; i < mi.arguments.size(); ++i) {
  3368. if (i > 0) {
  3369. method_hint += ", ";
  3370. }
  3371. String arg = mi.arguments[i].name;
  3372. if (arg.contains_char(':')) {
  3373. arg = arg.substr(0, arg.find_char(':'));
  3374. }
  3375. method_hint += arg;
  3376. if (type_hints) {
  3377. method_hint += ": " + _get_visual_datatype(mi.arguments[i], true, class_name);
  3378. }
  3379. }
  3380. if (mi.flags & METHOD_FLAG_VARARG) {
  3381. if (!mi.arguments.is_empty()) {
  3382. method_hint += ", ";
  3383. }
  3384. method_hint += "...args"; // `MethodInfo` does not support the rest parameter name.
  3385. if (type_hints) {
  3386. method_hint += ": Array";
  3387. }
  3388. }
  3389. method_hint += ")";
  3390. if (type_hints) {
  3391. method_hint += " -> " + _get_visual_datatype(mi.return_val, false, class_name);
  3392. }
  3393. method_hint += ":";
  3394. ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  3395. options.insert(option.display, option);
  3396. }
  3397. } break;
  3398. case GDScriptParser::COMPLETION_GET_NODE: {
  3399. // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
  3400. if (p_owner) {
  3401. List<String> opts;
  3402. p_owner->get_argument_options("get_node", 0, &opts);
  3403. bool for_unique_name = false;
  3404. if (completion_context.node != nullptr && completion_context.node->type == GDScriptParser::Node::GET_NODE && !static_cast<GDScriptParser::GetNodeNode *>(completion_context.node)->use_dollar) {
  3405. for_unique_name = true;
  3406. }
  3407. for (const String &E : opts) {
  3408. r_forced = true;
  3409. String opt = E.strip_edges();
  3410. if (opt.is_quoted()) {
  3411. // Remove quotes so that we can handle user preferred quote style,
  3412. // or handle NodePaths which are valid identifiers and don't need quotes.
  3413. opt = opt.unquote();
  3414. }
  3415. if (for_unique_name) {
  3416. if (!opt.begins_with("%")) {
  3417. continue;
  3418. }
  3419. opt = opt.substr(1);
  3420. }
  3421. // The path needs quotes if at least one of its components (excluding `%` prefix and `/` separations)
  3422. // is not a valid identifier.
  3423. bool path_needs_quote = false;
  3424. for (const String &part : opt.trim_prefix("%").split("/")) {
  3425. if (!part.is_valid_ascii_identifier()) {
  3426. path_needs_quote = true;
  3427. break;
  3428. }
  3429. }
  3430. if (path_needs_quote) {
  3431. // Ignore quote_style and just use double quotes for paths with apostrophes.
  3432. // Double quotes don't need to be checked because they're not valid in node and property names.
  3433. opt = opt.quote(opt.contains_char('\'') ? "\"" : quote_style); // Handle user preference.
  3434. }
  3435. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  3436. options.insert(option.display, option);
  3437. }
  3438. if (!for_unique_name) {
  3439. // Get autoloads.
  3440. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  3441. String path = "/root/" + E.key;
  3442. ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  3443. options.insert(option.display, option);
  3444. }
  3445. }
  3446. }
  3447. } break;
  3448. case GDScriptParser::COMPLETION_SUPER:
  3449. break;
  3450. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  3451. if (!completion_context.current_class) {
  3452. break;
  3453. }
  3454. _find_identifiers_in_class(completion_context.current_class, true, false, false, true, !_guess_expecting_callable(completion_context), options, 0);
  3455. } break;
  3456. }
  3457. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
  3458. r_options->push_back(E.value);
  3459. }
  3460. return OK;
  3461. }
  3462. #else // !TOOLS_ENABLED
  3463. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  3464. return OK;
  3465. }
  3466. #endif // TOOLS_ENABLED
  3467. //////// END COMPLETION //////////
  3468. String GDScriptLanguage::_get_indentation() const {
  3469. #ifdef TOOLS_ENABLED
  3470. if (Engine::get_singleton()->is_editor_hint()) {
  3471. bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
  3472. if (use_space_indentation) {
  3473. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  3474. return String(" ").repeat(indent_size);
  3475. }
  3476. }
  3477. #endif
  3478. return "\t";
  3479. }
  3480. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  3481. String indent = _get_indentation();
  3482. Vector<String> lines = p_code.split("\n");
  3483. List<int> indent_stack;
  3484. for (int i = 0; i < lines.size(); i++) {
  3485. String l = lines[i];
  3486. int tc = 0;
  3487. for (int j = 0; j < l.length(); j++) {
  3488. if (l[j] == ' ' || l[j] == '\t') {
  3489. tc++;
  3490. } else {
  3491. break;
  3492. }
  3493. }
  3494. String st = l.substr(tc).strip_edges();
  3495. if (st.is_empty() || st.begins_with("#")) {
  3496. continue; //ignore!
  3497. }
  3498. int ilevel = 0;
  3499. if (indent_stack.size()) {
  3500. ilevel = indent_stack.back()->get();
  3501. }
  3502. if (tc > ilevel) {
  3503. indent_stack.push_back(tc);
  3504. } else if (tc < ilevel) {
  3505. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  3506. indent_stack.pop_back();
  3507. }
  3508. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  3509. indent_stack.push_back(tc); // this is not right but gets the job done
  3510. }
  3511. }
  3512. if (i >= p_from_line) {
  3513. l = indent.repeat(indent_stack.size()) + st;
  3514. } else if (i > p_to_line) {
  3515. break;
  3516. }
  3517. lines.write[i] = l;
  3518. }
  3519. p_code = "";
  3520. for (int i = 0; i < lines.size(); i++) {
  3521. if (i > 0) {
  3522. p_code += "\n";
  3523. }
  3524. p_code += lines[i];
  3525. }
  3526. }
  3527. #ifdef TOOLS_ENABLED
  3528. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, GDScriptLanguage::LookupResult &r_result) {
  3529. GDScriptParser::DataType base_type = p_base;
  3530. while (true) {
  3531. switch (base_type.kind) {
  3532. case GDScriptParser::DataType::CLASS: {
  3533. ERR_FAIL_NULL_V(base_type.class_type, ERR_BUG);
  3534. String name = p_symbol;
  3535. if (name == "new") {
  3536. name = "_init";
  3537. }
  3538. if (!base_type.class_type->has_member(name)) {
  3539. base_type = base_type.class_type->base_type;
  3540. break;
  3541. }
  3542. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(name);
  3543. switch (member.type) {
  3544. case GDScriptParser::ClassNode::Member::UNDEFINED:
  3545. case GDScriptParser::ClassNode::Member::GROUP:
  3546. return ERR_BUG;
  3547. case GDScriptParser::ClassNode::Member::CLASS: {
  3548. String doc_type_name;
  3549. String doc_enum_name;
  3550. GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(member.get_datatype()), doc_type_name, doc_enum_name);
  3551. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3552. r_result.class_name = doc_type_name;
  3553. } break;
  3554. case GDScriptParser::ClassNode::Member::CONSTANT:
  3555. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3556. break;
  3557. case GDScriptParser::ClassNode::Member::FUNCTION:
  3558. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3559. break;
  3560. case GDScriptParser::ClassNode::Member::SIGNAL:
  3561. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  3562. break;
  3563. case GDScriptParser::ClassNode::Member::VARIABLE:
  3564. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  3565. break;
  3566. case GDScriptParser::ClassNode::Member::ENUM:
  3567. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3568. break;
  3569. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  3570. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3571. break;
  3572. }
  3573. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  3574. String doc_type_name;
  3575. String doc_enum_name;
  3576. GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
  3577. r_result.class_name = doc_type_name;
  3578. r_result.class_member = name;
  3579. }
  3580. Error err = OK;
  3581. r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
  3582. r_result.script_path = base_type.script_path;
  3583. r_result.location = member.get_line();
  3584. return err;
  3585. } break;
  3586. case GDScriptParser::DataType::SCRIPT: {
  3587. const Ref<Script> scr = base_type.script_type;
  3588. if (scr.is_null()) {
  3589. return ERR_CANT_RESOLVE;
  3590. }
  3591. String name = p_symbol;
  3592. if (name == "new") {
  3593. name = "_init";
  3594. }
  3595. const int line = scr->get_member_line(name);
  3596. if (line >= 0) {
  3597. bool found_type = false;
  3598. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  3599. {
  3600. List<PropertyInfo> properties;
  3601. scr->get_script_property_list(&properties);
  3602. for (const PropertyInfo &property : properties) {
  3603. if (property.name == name && (property.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  3604. found_type = true;
  3605. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  3606. r_result.class_name = scr->get_doc_class_name();
  3607. r_result.class_member = name;
  3608. break;
  3609. }
  3610. }
  3611. }
  3612. if (!found_type) {
  3613. List<MethodInfo> methods;
  3614. scr->get_script_method_list(&methods);
  3615. for (const MethodInfo &method : methods) {
  3616. if (method.name == name) {
  3617. found_type = true;
  3618. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3619. r_result.class_name = scr->get_doc_class_name();
  3620. r_result.class_member = name;
  3621. break;
  3622. }
  3623. }
  3624. }
  3625. if (!found_type) {
  3626. List<MethodInfo> signals;
  3627. scr->get_script_method_list(&signals);
  3628. for (const MethodInfo &signal : signals) {
  3629. if (signal.name == name) {
  3630. found_type = true;
  3631. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  3632. r_result.class_name = scr->get_doc_class_name();
  3633. r_result.class_member = name;
  3634. break;
  3635. }
  3636. }
  3637. }
  3638. if (!found_type) {
  3639. const Ref<GDScript> gds = scr;
  3640. if (gds.is_valid()) {
  3641. const Ref<GDScript> *subclass = gds->get_subclasses().getptr(name);
  3642. if (subclass != nullptr) {
  3643. found_type = true;
  3644. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3645. r_result.class_name = subclass->ptr()->get_doc_class_name();
  3646. }
  3647. // TODO: enums.
  3648. }
  3649. }
  3650. if (!found_type) {
  3651. HashMap<StringName, Variant> constants;
  3652. scr->get_constants(&constants);
  3653. if (constants.has(name)) {
  3654. found_type = true;
  3655. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3656. r_result.class_name = scr->get_doc_class_name();
  3657. r_result.class_member = name;
  3658. }
  3659. }
  3660. r_result.script = scr;
  3661. r_result.script_path = base_type.script_path;
  3662. r_result.location = line;
  3663. return OK;
  3664. }
  3665. const Ref<Script> base_script = scr->get_base_script();
  3666. if (base_script.is_valid()) {
  3667. base_type.script_type = base_script;
  3668. } else {
  3669. base_type.kind = GDScriptParser::DataType::NATIVE;
  3670. base_type.builtin_type = Variant::OBJECT;
  3671. base_type.native_type = scr->get_instance_base_type();
  3672. }
  3673. } break;
  3674. case GDScriptParser::DataType::NATIVE: {
  3675. const StringName &class_name = base_type.native_type;
  3676. ERR_FAIL_COND_V(!GDScriptAnalyzer::class_exists(class_name), ERR_BUG);
  3677. if (ClassDB::has_method(class_name, p_symbol, true)) {
  3678. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3679. r_result.class_name = class_name;
  3680. r_result.class_member = p_symbol;
  3681. return OK;
  3682. }
  3683. List<MethodInfo> virtual_methods;
  3684. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  3685. for (const MethodInfo &E : virtual_methods) {
  3686. if (E.name == p_symbol) {
  3687. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3688. r_result.class_name = class_name;
  3689. r_result.class_member = p_symbol;
  3690. return OK;
  3691. }
  3692. }
  3693. if (ClassDB::has_signal(class_name, p_symbol, true)) {
  3694. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  3695. r_result.class_name = class_name;
  3696. r_result.class_member = p_symbol;
  3697. return OK;
  3698. }
  3699. List<StringName> enums;
  3700. ClassDB::get_enum_list(class_name, &enums);
  3701. for (const StringName &E : enums) {
  3702. if (E == p_symbol) {
  3703. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3704. r_result.class_name = class_name;
  3705. r_result.class_member = p_symbol;
  3706. return OK;
  3707. }
  3708. }
  3709. if (!String(ClassDB::get_integer_constant_enum(class_name, p_symbol, true)).is_empty()) {
  3710. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3711. r_result.class_name = class_name;
  3712. r_result.class_member = p_symbol;
  3713. return OK;
  3714. }
  3715. List<String> constants;
  3716. ClassDB::get_integer_constant_list(class_name, &constants, true);
  3717. for (const String &E : constants) {
  3718. if (E == p_symbol) {
  3719. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3720. r_result.class_name = class_name;
  3721. r_result.class_member = p_symbol;
  3722. return OK;
  3723. }
  3724. }
  3725. if (ClassDB::has_property(class_name, p_symbol, true)) {
  3726. PropertyInfo prop_info;
  3727. ClassDB::get_property_info(class_name, p_symbol, &prop_info, true);
  3728. if (prop_info.usage & PROPERTY_USAGE_INTERNAL) {
  3729. return ERR_CANT_RESOLVE;
  3730. }
  3731. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  3732. r_result.class_name = class_name;
  3733. r_result.class_member = p_symbol;
  3734. return OK;
  3735. }
  3736. const StringName parent_class = ClassDB::get_parent_class(class_name);
  3737. if (parent_class != StringName()) {
  3738. base_type.native_type = parent_class;
  3739. } else {
  3740. return ERR_CANT_RESOLVE;
  3741. }
  3742. } break;
  3743. case GDScriptParser::DataType::BUILTIN: {
  3744. if (base_type.is_meta_type) {
  3745. if (Variant::has_enum(base_type.builtin_type, p_symbol)) {
  3746. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3747. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3748. r_result.class_member = p_symbol;
  3749. return OK;
  3750. }
  3751. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  3752. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3753. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3754. r_result.class_member = p_symbol;
  3755. return OK;
  3756. }
  3757. } else {
  3758. if (Variant::has_member(base_type.builtin_type, p_symbol)) {
  3759. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  3760. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3761. r_result.class_member = p_symbol;
  3762. return OK;
  3763. }
  3764. }
  3765. if (Variant::has_builtin_method(base_type.builtin_type, p_symbol)) {
  3766. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3767. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3768. r_result.class_member = p_symbol;
  3769. return OK;
  3770. }
  3771. return ERR_CANT_RESOLVE;
  3772. } break;
  3773. case GDScriptParser::DataType::ENUM: {
  3774. if (base_type.is_meta_type) {
  3775. if (base_type.enum_values.has(p_symbol)) {
  3776. String doc_type_name;
  3777. String doc_enum_name;
  3778. GDScriptDocGen::doctype_from_gdtype(GDScriptAnalyzer::type_from_metatype(base_type), doc_type_name, doc_enum_name);
  3779. if (CoreConstants::is_global_enum(doc_enum_name)) {
  3780. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3781. r_result.class_name = "@GlobalScope";
  3782. r_result.class_member = p_symbol;
  3783. return OK;
  3784. } else {
  3785. const int dot_pos = doc_enum_name.rfind_char('.');
  3786. if (dot_pos >= 0) {
  3787. Error err = OK;
  3788. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3789. if (base_type.class_type != nullptr) {
  3790. // For script enums the value isn't accessible as class constant so we need the full enum name.
  3791. r_result.class_name = doc_enum_name;
  3792. r_result.class_member = p_symbol;
  3793. r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
  3794. r_result.script_path = base_type.script_path;
  3795. const String enum_name = doc_enum_name.substr(dot_pos + 1);
  3796. if (base_type.class_type->has_member(enum_name)) {
  3797. const GDScriptParser::ClassNode::Member member = base_type.class_type->get_member(enum_name);
  3798. if (member.type == GDScriptParser::ClassNode::Member::ENUM) {
  3799. for (const GDScriptParser::EnumNode::Value &value : member.m_enum->values) {
  3800. if (value.identifier->name == p_symbol) {
  3801. r_result.location = value.line;
  3802. break;
  3803. }
  3804. }
  3805. }
  3806. }
  3807. } else if (base_type.script_type.is_valid()) {
  3808. // For script enums the value isn't accessible as class constant so we need the full enum name.
  3809. r_result.class_name = doc_enum_name;
  3810. r_result.class_member = p_symbol;
  3811. r_result.script = base_type.script_type;
  3812. r_result.script_path = base_type.script_path;
  3813. // TODO: Find a way to obtain enum value location for a script
  3814. r_result.location = base_type.script_type->get_member_line(doc_enum_name.substr(dot_pos + 1));
  3815. } else {
  3816. r_result.class_name = doc_enum_name.left(dot_pos);
  3817. r_result.class_member = p_symbol;
  3818. }
  3819. return err;
  3820. }
  3821. }
  3822. } else if (Variant::has_builtin_method(Variant::DICTIONARY, p_symbol)) {
  3823. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3824. r_result.class_name = "Dictionary";
  3825. r_result.class_member = p_symbol;
  3826. return OK;
  3827. }
  3828. }
  3829. return ERR_CANT_RESOLVE;
  3830. } break;
  3831. case GDScriptParser::DataType::VARIANT: {
  3832. if (base_type.is_meta_type) {
  3833. const String enum_name = "Variant." + p_symbol;
  3834. if (CoreConstants::is_global_enum(enum_name)) {
  3835. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3836. r_result.class_name = "@GlobalScope";
  3837. r_result.class_member = enum_name;
  3838. return OK;
  3839. }
  3840. }
  3841. return ERR_CANT_RESOLVE;
  3842. } break;
  3843. case GDScriptParser::DataType::RESOLVING:
  3844. case GDScriptParser::DataType::UNRESOLVED: {
  3845. return ERR_CANT_RESOLVE;
  3846. } break;
  3847. }
  3848. }
  3849. return ERR_CANT_RESOLVE;
  3850. }
  3851. ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  3852. // Before parsing, try the usual stuff.
  3853. if (GDScriptAnalyzer::class_exists(p_symbol)) {
  3854. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3855. r_result.class_name = p_symbol;
  3856. return OK;
  3857. }
  3858. if (Variant::get_type_by_name(p_symbol) < Variant::VARIANT_MAX) {
  3859. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3860. r_result.class_name = p_symbol;
  3861. return OK;
  3862. }
  3863. if (p_symbol == "Variant") {
  3864. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3865. r_result.class_name = "Variant";
  3866. return OK;
  3867. }
  3868. if (p_symbol == "PI" || p_symbol == "TAU" || p_symbol == "INF" || p_symbol == "NAN") {
  3869. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3870. r_result.class_name = "@GDScript";
  3871. r_result.class_member = p_symbol;
  3872. return OK;
  3873. }
  3874. GDScriptParser parser;
  3875. parser.parse(p_code, p_path, true);
  3876. GDScriptParser::CompletionContext context = parser.get_completion_context();
  3877. context.base = p_owner;
  3878. // Allows class functions with the names like built-ins to be handled properly.
  3879. if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) {
  3880. // Need special checks for `assert` and `preload` as they are technically
  3881. // keywords, so are not registered in `GDScriptUtilityFunctions`.
  3882. if (GDScriptUtilityFunctions::function_exists(p_symbol) || p_symbol == "assert" || p_symbol == "preload") {
  3883. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3884. r_result.class_name = "@GDScript";
  3885. r_result.class_member = p_symbol;
  3886. return OK;
  3887. }
  3888. }
  3889. GDScriptAnalyzer analyzer(&parser);
  3890. analyzer.analyze();
  3891. if (context.current_class && context.current_class->extends.size() > 0) {
  3892. StringName class_name = context.current_class->extends[0]->name;
  3893. bool success = false;
  3894. ClassDB::get_integer_constant(class_name, p_symbol, &success);
  3895. if (success) {
  3896. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3897. r_result.class_name = class_name;
  3898. r_result.class_member = p_symbol;
  3899. return OK;
  3900. }
  3901. do {
  3902. List<StringName> enums;
  3903. ClassDB::get_enum_list(class_name, &enums, true);
  3904. for (const StringName &enum_name : enums) {
  3905. if (enum_name == p_symbol) {
  3906. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3907. r_result.class_name = class_name;
  3908. r_result.class_member = p_symbol;
  3909. return OK;
  3910. }
  3911. }
  3912. class_name = ClassDB::get_parent_class_nocheck(class_name);
  3913. } while (class_name != StringName());
  3914. }
  3915. const GDScriptParser::TypeNode *type_node = dynamic_cast<const GDScriptParser::TypeNode *>(context.node);
  3916. if (type_node != nullptr && !type_node->type_chain.is_empty()) {
  3917. StringName class_name = type_node->type_chain[0]->name;
  3918. if (ScriptServer::is_global_class(class_name)) {
  3919. class_name = ScriptServer::get_global_class_native_base(class_name);
  3920. }
  3921. do {
  3922. List<StringName> enums;
  3923. ClassDB::get_enum_list(class_name, &enums, true);
  3924. for (const StringName &enum_name : enums) {
  3925. if (enum_name == p_symbol) {
  3926. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3927. r_result.class_name = class_name;
  3928. r_result.class_member = p_symbol;
  3929. return OK;
  3930. }
  3931. }
  3932. class_name = ClassDB::get_parent_class_nocheck(class_name);
  3933. } while (class_name != StringName());
  3934. }
  3935. bool is_function = false;
  3936. switch (context.type) {
  3937. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  3938. GDScriptParser::DataType base_type;
  3939. base_type.kind = GDScriptParser::DataType::BUILTIN;
  3940. base_type.builtin_type = context.builtin_type;
  3941. base_type.is_meta_type = true;
  3942. if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
  3943. return OK;
  3944. }
  3945. } break;
  3946. case GDScriptParser::COMPLETION_SUPER: {
  3947. if (context.current_class && context.current_function) {
  3948. if (_lookup_symbol_from_base(context.current_class->base_type, context.current_function->info.name, r_result) == OK) {
  3949. return OK;
  3950. }
  3951. }
  3952. } break;
  3953. case GDScriptParser::COMPLETION_SUPER_METHOD:
  3954. case GDScriptParser::COMPLETION_METHOD:
  3955. case GDScriptParser::COMPLETION_ASSIGN:
  3956. case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
  3957. case GDScriptParser::COMPLETION_IDENTIFIER:
  3958. case GDScriptParser::COMPLETION_PROPERTY_METHOD:
  3959. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  3960. GDScriptParser::DataType base_type;
  3961. if (context.current_class) {
  3962. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  3963. base_type = context.current_class->get_datatype();
  3964. } else {
  3965. base_type = context.current_class->base_type;
  3966. }
  3967. } else {
  3968. break;
  3969. }
  3970. if (!is_function && context.current_suite) {
  3971. // Lookup local variables.
  3972. const GDScriptParser::SuiteNode *suite = context.current_suite;
  3973. while (suite) {
  3974. if (suite->has_local(p_symbol)) {
  3975. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_symbol);
  3976. switch (local.type) {
  3977. case GDScriptParser::SuiteNode::Local::UNDEFINED:
  3978. return ERR_BUG;
  3979. case GDScriptParser::SuiteNode::Local::CONSTANT:
  3980. r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT;
  3981. r_result.description = local.constant->doc_data.description;
  3982. r_result.is_deprecated = local.constant->doc_data.is_deprecated;
  3983. r_result.deprecated_message = local.constant->doc_data.deprecated_message;
  3984. r_result.is_experimental = local.constant->doc_data.is_experimental;
  3985. r_result.experimental_message = local.constant->doc_data.experimental_message;
  3986. if (local.constant->initializer != nullptr) {
  3987. r_result.value = GDScriptDocGen::docvalue_from_expression(local.constant->initializer);
  3988. }
  3989. break;
  3990. case GDScriptParser::SuiteNode::Local::VARIABLE:
  3991. r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
  3992. r_result.description = local.variable->doc_data.description;
  3993. r_result.is_deprecated = local.variable->doc_data.is_deprecated;
  3994. r_result.deprecated_message = local.variable->doc_data.deprecated_message;
  3995. r_result.is_experimental = local.variable->doc_data.is_experimental;
  3996. r_result.experimental_message = local.variable->doc_data.experimental_message;
  3997. if (local.variable->initializer != nullptr) {
  3998. r_result.value = GDScriptDocGen::docvalue_from_expression(local.variable->initializer);
  3999. }
  4000. break;
  4001. case GDScriptParser::SuiteNode::Local::PARAMETER:
  4002. case GDScriptParser::SuiteNode::Local::FOR_VARIABLE:
  4003. case GDScriptParser::SuiteNode::Local::PATTERN_BIND:
  4004. r_result.type = ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE;
  4005. break;
  4006. }
  4007. GDScriptDocGen::doctype_from_gdtype(local.get_datatype(), r_result.doc_type, r_result.enumeration);
  4008. Error err = OK;
  4009. r_result.script = GDScriptCache::get_shallow_script(base_type.script_path, err);
  4010. r_result.script_path = base_type.script_path;
  4011. r_result.location = local.start_line;
  4012. return err;
  4013. }
  4014. suite = suite->parent_block;
  4015. }
  4016. }
  4017. if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
  4018. return OK;
  4019. }
  4020. if (!is_function) {
  4021. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  4022. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  4023. if (autoload.is_singleton) {
  4024. String scr_path = autoload.path;
  4025. if (!scr_path.ends_with(".gd")) {
  4026. // Not a script, try find the script anyway, may have some success.
  4027. scr_path = scr_path.get_basename() + ".gd";
  4028. }
  4029. if (FileAccess::exists(scr_path)) {
  4030. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  4031. r_result.class_name = p_symbol;
  4032. r_result.script = ResourceLoader::load(scr_path);
  4033. r_result.script_path = scr_path;
  4034. r_result.location = 0;
  4035. return OK;
  4036. }
  4037. }
  4038. }
  4039. if (ScriptServer::is_global_class(p_symbol)) {
  4040. const String scr_path = ScriptServer::get_global_class_path(p_symbol);
  4041. const Ref<Script> scr = ResourceLoader::load(scr_path);
  4042. if (scr.is_null()) {
  4043. return ERR_BUG;
  4044. }
  4045. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  4046. r_result.class_name = scr->get_doc_class_name();
  4047. r_result.script = scr;
  4048. r_result.script_path = scr_path;
  4049. r_result.location = 0;
  4050. return OK;
  4051. }
  4052. const HashMap<StringName, int> &global_map = GDScriptLanguage::get_singleton()->get_global_map();
  4053. if (global_map.has(p_symbol)) {
  4054. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[global_map[p_symbol]];
  4055. if (value.get_type() == Variant::OBJECT) {
  4056. const Object *obj = value;
  4057. if (obj) {
  4058. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  4059. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  4060. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  4061. } else {
  4062. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  4063. r_result.class_name = obj->get_class();
  4064. }
  4065. return OK;
  4066. }
  4067. }
  4068. }
  4069. if (CoreConstants::is_global_enum(p_symbol)) {
  4070. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  4071. r_result.class_name = "@GlobalScope";
  4072. r_result.class_member = p_symbol;
  4073. return OK;
  4074. }
  4075. if (CoreConstants::is_global_constant(p_symbol)) {
  4076. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  4077. r_result.class_name = "@GlobalScope";
  4078. r_result.class_member = p_symbol;
  4079. return OK;
  4080. }
  4081. if (Variant::has_utility_function(p_symbol)) {
  4082. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  4083. r_result.class_name = "@GlobalScope";
  4084. r_result.class_member = p_symbol;
  4085. return OK;
  4086. }
  4087. }
  4088. } break;
  4089. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  4090. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  4091. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  4092. break;
  4093. }
  4094. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  4095. if (!subscript->is_attribute) {
  4096. break;
  4097. }
  4098. GDScriptCompletionIdentifier base;
  4099. bool found_type = _get_subscript_type(context, subscript, base.type);
  4100. if (!found_type && !_guess_expression_type(context, subscript->base, base)) {
  4101. break;
  4102. }
  4103. if (_lookup_symbol_from_base(base.type, p_symbol, r_result) == OK) {
  4104. return OK;
  4105. }
  4106. } break;
  4107. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  4108. if (context.node == nullptr || context.node->type != GDScriptParser::Node::TYPE) {
  4109. break;
  4110. }
  4111. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(context.node);
  4112. GDScriptParser::DataType base_type;
  4113. const GDScriptParser::IdentifierNode *prev = nullptr;
  4114. for (const GDScriptParser::IdentifierNode *E : type->type_chain) {
  4115. if (E->name == p_symbol && prev != nullptr) {
  4116. base_type = prev->get_datatype();
  4117. break;
  4118. }
  4119. prev = E;
  4120. }
  4121. if (base_type.kind != GDScriptParser::DataType::CLASS) {
  4122. GDScriptCompletionIdentifier base;
  4123. if (!_guess_expression_type(context, prev, base)) {
  4124. break;
  4125. }
  4126. base_type = base.type;
  4127. }
  4128. if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
  4129. return OK;
  4130. }
  4131. } break;
  4132. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  4133. GDScriptParser::DataType base_type = context.current_class->base_type;
  4134. if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
  4135. return OK;
  4136. }
  4137. } break;
  4138. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE:
  4139. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  4140. case GDScriptParser::COMPLETION_TYPE_NAME: {
  4141. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  4142. if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) {
  4143. return OK;
  4144. }
  4145. } break;
  4146. case GDScriptParser::COMPLETION_ANNOTATION: {
  4147. const String annotation_symbol = "@" + p_symbol;
  4148. if (parser.annotation_exists(annotation_symbol)) {
  4149. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION;
  4150. r_result.class_name = "@GDScript";
  4151. r_result.class_member = annotation_symbol;
  4152. return OK;
  4153. }
  4154. } break;
  4155. default: {
  4156. }
  4157. }
  4158. return ERR_CANT_RESOLVE;
  4159. }
  4160. #endif // TOOLS_ENABLED