gdscript_editor.cpp 162 KB

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