2
0

gdscript_editor.cpp 162 KB

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