gdscript_editor.cpp 125 KB

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