gdscript_editor.cpp 163 KB

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