gdscript_editor.cpp 127 KB

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