2
0

gdscript_editor.cpp 127 KB

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