gdscript_editor.cpp 157 KB

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