gdscript_editor.cpp 141 KB

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