gdscript_editor.cpp 167 KB

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