2
0

gdscript_editor.cpp 167 KB

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