gdscript_editor.cpp 159 KB

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