gdscript_editor.cpp 147 KB

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