gdscript_editor.cpp 160 KB

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