gdscript_editor.cpp 126 KB

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