gdscript_editor.cpp 159 KB

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