gdscript_editor.cpp 129 KB

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