gdscript_editor.cpp 120 KB

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