gdscript_editor.cpp 162 KB

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