gdscript_editor.cpp 162 KB

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