2
0

gdscript_editor.cpp 135 KB

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