gdscript_editor.cpp 164 KB

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