gdscript_editor.cpp 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  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. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  565. if (par->default_value) {
  566. String def_val = "<unknown>";
  567. switch (par->default_value->type) {
  568. case GDScriptParser::Node::LITERAL: {
  569. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->default_value);
  570. def_val = literal->value.get_construct_string();
  571. } break;
  572. case GDScriptParser::Node::IDENTIFIER: {
  573. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->default_value);
  574. def_val = id->name.operator String();
  575. } break;
  576. case GDScriptParser::Node::CALL: {
  577. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->default_value);
  578. if (call->is_constant && call->reduced) {
  579. def_val = call->function_name.operator String() + call->reduced_value.operator String();
  580. }
  581. } break;
  582. case GDScriptParser::Node::ARRAY: {
  583. const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->default_value);
  584. if (arr->is_constant && arr->reduced) {
  585. def_val = arr->reduced_value.operator String();
  586. }
  587. } break;
  588. case GDScriptParser::Node::DICTIONARY: {
  589. const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->default_value);
  590. if (dict->is_constant && dict->reduced) {
  591. def_val = dict->reduced_value.operator String();
  592. }
  593. } break;
  594. case GDScriptParser::Node::SUBSCRIPT: {
  595. const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->default_value);
  596. if (sub->is_constant) {
  597. if (sub->datatype.kind == GDScriptParser::DataType::ENUM) {
  598. def_val = sub->get_datatype().to_string();
  599. } else if (sub->reduced) {
  600. const Variant::Type vt = sub->reduced_value.get_type();
  601. 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) {
  602. def_val = sub->reduced_value.operator String();
  603. } else {
  604. def_val = sub->get_datatype().to_string() + sub->reduced_value.operator String();
  605. }
  606. }
  607. }
  608. } break;
  609. default:
  610. break;
  611. }
  612. arghint += " = " + def_val;
  613. }
  614. if (i == p_arg_idx) {
  615. arghint += String::chr(0xFFFF);
  616. }
  617. }
  618. arghint += ")";
  619. return arghint;
  620. }
  621. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_list) {
  622. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  623. for (int i = 0; i < p_dir->get_file_count(); i++) {
  624. ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH);
  625. option.insert_text = option.display.quote(quote_style);
  626. r_list.insert(option.display, option);
  627. }
  628. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  629. _get_directory_contents(p_dir->get_subdir(i), r_list);
  630. }
  631. }
  632. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  633. if (p_annotation->name == SNAME("@export_range")) {
  634. if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
  635. // Slider hint.
  636. ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  637. slider1.insert_text = slider1.display.quote(p_quote_style);
  638. r_result.insert(slider1.display, slider1);
  639. ScriptLanguage::CodeCompletionOption slider2("or_lesser", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  640. slider2.insert_text = slider2.display.quote(p_quote_style);
  641. r_result.insert(slider2.display, slider2);
  642. ScriptLanguage::CodeCompletionOption slider3("no_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  643. slider3.insert_text = slider3.display.quote(p_quote_style);
  644. r_result.insert(slider3.display, slider3);
  645. }
  646. } else if (p_annotation->name == SNAME("@export_exp_easing")) {
  647. if (p_argument == 0 || p_argument == 1) {
  648. // Easing hint.
  649. ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  650. hint1.insert_text = hint1.display.quote(p_quote_style);
  651. r_result.insert(hint1.display, hint1);
  652. ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  653. hint2.insert_text = hint2.display.quote(p_quote_style);
  654. r_result.insert(hint2.display, hint2);
  655. }
  656. } else if (p_annotation->name == SNAME("@export_node_path")) {
  657. ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  658. r_result.insert(node.display, node);
  659. List<StringName> node_types;
  660. ClassDB::get_inheriters_from_class("Node", &node_types);
  661. for (const StringName &E : node_types) {
  662. if (!ClassDB::is_class_exposed(E)) {
  663. continue;
  664. }
  665. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  666. r_result.insert(option.display, option);
  667. }
  668. } else if (p_annotation->name == SNAME("@warning_ignore")) {
  669. for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
  670. ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  671. r_result.insert(warning.display, warning);
  672. }
  673. }
  674. }
  675. static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool exclude_nil = false) {
  676. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  677. if (!exclude_nil && Variant::Type(i) == Variant::Type::NIL) {
  678. ScriptLanguage::CodeCompletionOption option("null", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  679. r_result.insert(option.display, option);
  680. } else {
  681. ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  682. r_result.insert(option.display, option);
  683. }
  684. }
  685. }
  686. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  687. // Built-in Variant Types
  688. _find_built_in_variants(r_result, true);
  689. List<StringName> native_types;
  690. ClassDB::get_class_list(&native_types);
  691. for (const StringName &E : native_types) {
  692. if (ClassDB::is_class_exposed(E) && !Engine::get_singleton()->has_singleton(E)) {
  693. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  694. r_result.insert(option.display, option);
  695. }
  696. }
  697. if (p_context.current_class) {
  698. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  699. // Native enums from base class
  700. List<StringName> enums;
  701. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  702. for (const StringName &E : enums) {
  703. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  704. r_result.insert(option.display, option);
  705. }
  706. }
  707. // Check current class for potential types
  708. const GDScriptParser::ClassNode *current = p_context.current_class;
  709. while (current) {
  710. for (int i = 0; i < current->members.size(); i++) {
  711. const GDScriptParser::ClassNode::Member &member = current->members[i];
  712. switch (member.type) {
  713. case GDScriptParser::ClassNode::Member::CLASS: {
  714. ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  715. r_result.insert(option.display, option);
  716. } break;
  717. case GDScriptParser::ClassNode::Member::ENUM: {
  718. if (!p_inherit_only) {
  719. ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL);
  720. r_result.insert(option.display, option);
  721. }
  722. } break;
  723. case GDScriptParser::ClassNode::Member::CONSTANT: {
  724. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  725. ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  726. r_result.insert(option.display, option);
  727. }
  728. } break;
  729. default:
  730. break;
  731. }
  732. }
  733. current = current->outer;
  734. }
  735. }
  736. // Global scripts
  737. List<StringName> global_classes;
  738. ScriptServer::get_global_class_list(&global_classes);
  739. for (const StringName &E : global_classes) {
  740. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  741. r_result.insert(option.display, option);
  742. }
  743. // Autoload singletons
  744. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  745. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  746. const ProjectSettings::AutoloadInfo &info = E.value;
  747. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  748. continue;
  749. }
  750. ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  751. r_result.insert(option.display, option);
  752. }
  753. }
  754. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  755. for (int i = 0; i < p_suite->locals.size(); i++) {
  756. ScriptLanguage::CodeCompletionOption option;
  757. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  758. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, ScriptLanguage::LOCATION_LOCAL);
  759. option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
  760. } else {
  761. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, ScriptLanguage::LOCATION_LOCAL);
  762. }
  763. r_result.insert(option.display, option);
  764. }
  765. if (p_suite->parent_block) {
  766. _find_identifiers_in_suite(p_suite->parent_block, r_result);
  767. }
  768. }
  769. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
  770. 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) {
  771. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  772. if (!p_parent_only) {
  773. bool outer = false;
  774. const GDScriptParser::ClassNode *clss = p_class;
  775. int classes_processed = 0;
  776. while (clss) {
  777. for (int i = 0; i < clss->members.size(); i++) {
  778. const int location = (classes_processed + p_recursion_depth) | ScriptLanguage::LOCATION_PARENT_MASK;
  779. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  780. ScriptLanguage::CodeCompletionOption option;
  781. switch (member.type) {
  782. case GDScriptParser::ClassNode::Member::VARIABLE:
  783. if (p_only_functions || outer || (p_static)) {
  784. continue;
  785. }
  786. option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  787. break;
  788. case GDScriptParser::ClassNode::Member::CONSTANT:
  789. if (p_only_functions) {
  790. continue;
  791. }
  792. if (r_result.has(member.constant->identifier->name)) {
  793. continue;
  794. }
  795. option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  796. if (member.constant->initializer) {
  797. option.default_value = member.constant->initializer->reduced_value;
  798. }
  799. break;
  800. case GDScriptParser::ClassNode::Member::CLASS:
  801. if (p_only_functions) {
  802. continue;
  803. }
  804. option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
  805. break;
  806. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  807. if (p_only_functions) {
  808. continue;
  809. }
  810. option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  811. break;
  812. case GDScriptParser::ClassNode::Member::ENUM:
  813. if (p_only_functions) {
  814. continue;
  815. }
  816. option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  817. break;
  818. case GDScriptParser::ClassNode::Member::FUNCTION:
  819. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  820. continue;
  821. }
  822. option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  823. if (member.function->parameters.size() > 0) {
  824. option.insert_text += "(";
  825. } else {
  826. option.insert_text += "()";
  827. }
  828. break;
  829. case GDScriptParser::ClassNode::Member::SIGNAL:
  830. if (p_only_functions || outer) {
  831. continue;
  832. }
  833. option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  834. break;
  835. case GDScriptParser::ClassNode::Member::GROUP:
  836. break; // No-op, but silences warnings.
  837. case GDScriptParser::ClassNode::Member::UNDEFINED:
  838. break;
  839. }
  840. r_result.insert(option.display, option);
  841. }
  842. outer = true;
  843. clss = clss->outer;
  844. classes_processed++;
  845. }
  846. }
  847. // Parents.
  848. GDScriptCompletionIdentifier base_type;
  849. base_type.type = p_class->base_type;
  850. base_type.type.is_meta_type = p_static;
  851. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  852. }
  853. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  854. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  855. GDScriptParser::DataType base_type = p_base.type;
  856. bool _static = base_type.is_meta_type;
  857. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  858. ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
  859. option.insert_text += "(";
  860. r_result.insert(option.display, option);
  861. }
  862. while (!base_type.has_no_type()) {
  863. switch (base_type.kind) {
  864. case GDScriptParser::DataType::CLASS: {
  865. _find_identifiers_in_class(base_type.class_type, p_only_functions, _static, false, r_result, p_recursion_depth + 1);
  866. // This already finds all parent identifiers, so we are done.
  867. base_type = GDScriptParser::DataType();
  868. } break;
  869. case GDScriptParser::DataType::SCRIPT: {
  870. Ref<Script> scr = base_type.script_type;
  871. if (scr.is_valid()) {
  872. if (!p_only_functions) {
  873. if (!_static) {
  874. List<PropertyInfo> members;
  875. scr->get_script_property_list(&members);
  876. for (const PropertyInfo &E : members) {
  877. int location = p_recursion_depth + _get_property_location(scr->get_class_name(), E.class_name);
  878. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  879. r_result.insert(option.display, option);
  880. }
  881. }
  882. HashMap<StringName, Variant> constants;
  883. scr->get_constants(&constants);
  884. for (const KeyValue<StringName, Variant> &E : constants) {
  885. int location = p_recursion_depth + _get_constant_location(scr->get_class_name(), E.key);
  886. ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  887. r_result.insert(option.display, option);
  888. }
  889. List<MethodInfo> signals;
  890. scr->get_script_signal_list(&signals);
  891. for (const MethodInfo &E : signals) {
  892. int location = p_recursion_depth + _get_signal_location(scr->get_class_name(), E.name);
  893. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  894. r_result.insert(option.display, option);
  895. }
  896. }
  897. List<MethodInfo> methods;
  898. scr->get_script_method_list(&methods);
  899. for (const MethodInfo &E : methods) {
  900. if (E.name.begins_with("@")) {
  901. continue;
  902. }
  903. int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
  904. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  905. if (E.arguments.size()) {
  906. option.insert_text += "(";
  907. } else {
  908. option.insert_text += "()";
  909. }
  910. r_result.insert(option.display, option);
  911. }
  912. Ref<Script> base_script = scr->get_base_script();
  913. if (base_script.is_valid()) {
  914. base_type.script_type = base_script;
  915. } else {
  916. base_type.kind = GDScriptParser::DataType::NATIVE;
  917. base_type.native_type = scr->get_instance_base_type();
  918. }
  919. } else {
  920. return;
  921. }
  922. } break;
  923. case GDScriptParser::DataType::NATIVE: {
  924. StringName type = base_type.native_type;
  925. if (!ClassDB::class_exists(type)) {
  926. return;
  927. }
  928. if (!p_only_functions) {
  929. List<String> constants;
  930. ClassDB::get_integer_constant_list(type, &constants);
  931. for (const String &E : constants) {
  932. int location = p_recursion_depth + _get_constant_location(type, StringName(E));
  933. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  934. r_result.insert(option.display, option);
  935. }
  936. List<MethodInfo> signals;
  937. ClassDB::get_signal_list(type, &signals);
  938. for (const MethodInfo &E : signals) {
  939. int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
  940. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  941. r_result.insert(option.display, option);
  942. }
  943. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  944. List<PropertyInfo> pinfo;
  945. ClassDB::get_property_list(type, &pinfo);
  946. for (const PropertyInfo &E : pinfo) {
  947. if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  948. continue;
  949. }
  950. if (E.name.contains("/")) {
  951. continue;
  952. }
  953. int location = p_recursion_depth + _get_property_location(type, E.class_name);
  954. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  955. r_result.insert(option.display, option);
  956. }
  957. }
  958. }
  959. bool only_static = _static && !Engine::get_singleton()->has_singleton(type);
  960. List<MethodInfo> methods;
  961. ClassDB::get_method_list(type, &methods, false, true);
  962. for (const MethodInfo &E : methods) {
  963. if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
  964. continue;
  965. }
  966. if (E.name.begins_with("_")) {
  967. continue;
  968. }
  969. int location = p_recursion_depth + _get_method_location(type, E.name);
  970. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  971. if (E.arguments.size()) {
  972. option.insert_text += "(";
  973. } else {
  974. option.insert_text += "()";
  975. }
  976. r_result.insert(option.display, option);
  977. }
  978. return;
  979. } break;
  980. case GDScriptParser::DataType::BUILTIN: {
  981. Callable::CallError err;
  982. Variant tmp;
  983. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  984. if (err.error != Callable::CallError::CALL_OK) {
  985. return;
  986. }
  987. if (!p_only_functions) {
  988. List<PropertyInfo> members;
  989. if (p_base.value.get_type() != Variant::NIL) {
  990. p_base.value.get_property_list(&members);
  991. } else {
  992. tmp.get_property_list(&members);
  993. }
  994. for (const PropertyInfo &E : members) {
  995. if (!String(E.name).contains("/")) {
  996. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  997. r_result.insert(option.display, option);
  998. }
  999. }
  1000. }
  1001. List<MethodInfo> methods;
  1002. tmp.get_method_list(&methods);
  1003. for (const MethodInfo &E : methods) {
  1004. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1005. if (E.arguments.size()) {
  1006. option.insert_text += "(";
  1007. } else {
  1008. option.insert_text += "()";
  1009. }
  1010. r_result.insert(option.display, option);
  1011. }
  1012. return;
  1013. } break;
  1014. default: {
  1015. return;
  1016. } break;
  1017. }
  1018. }
  1019. }
  1020. static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1021. if (!p_only_functions && p_context.current_suite) {
  1022. // This includes function parameters, since they are also locals.
  1023. _find_identifiers_in_suite(p_context.current_suite, r_result);
  1024. }
  1025. if (p_context.current_class) {
  1026. _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);
  1027. }
  1028. List<StringName> functions;
  1029. GDScriptUtilityFunctions::get_function_list(&functions);
  1030. for (const StringName &E : functions) {
  1031. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
  1032. ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1033. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  1034. option.insert_text += "(";
  1035. } else {
  1036. option.insert_text += "()";
  1037. }
  1038. r_result.insert(option.display, option);
  1039. }
  1040. if (p_only_functions) {
  1041. return;
  1042. }
  1043. _find_built_in_variants(r_result);
  1044. static const char *_keywords[] = {
  1045. "false", "PI", "TAU", "INF", "NAN", "self", "true", "breakpoint", "tool", "super",
  1046. "break", "continue", "pass", "return",
  1047. nullptr
  1048. };
  1049. const char **kw = _keywords;
  1050. while (*kw) {
  1051. ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1052. r_result.insert(option.display, option);
  1053. kw++;
  1054. }
  1055. static const char *_keywords_with_space[] = {
  1056. "and", "in", "not", "or", "as", "class", "extends", "is", "func", "signal", "await",
  1057. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "while",
  1058. nullptr
  1059. };
  1060. const char **kws = _keywords_with_space;
  1061. while (*kws) {
  1062. ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1063. option.insert_text += " ";
  1064. r_result.insert(option.display, option);
  1065. kws++;
  1066. }
  1067. static const char *_keywords_with_args[] = {
  1068. "assert", "preload",
  1069. nullptr
  1070. };
  1071. const char **kwa = _keywords_with_args;
  1072. while (*kwa) {
  1073. ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1074. option.insert_text += "(";
  1075. r_result.insert(option.display, option);
  1076. kwa++;
  1077. }
  1078. List<StringName> utility_func_names;
  1079. Variant::get_utility_function_list(&utility_func_names);
  1080. for (List<StringName>::Element *E = utility_func_names.front(); E; E = E->next()) {
  1081. ScriptLanguage::CodeCompletionOption option(E->get(), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1082. option.insert_text += "(";
  1083. r_result.insert(option.display, option);
  1084. }
  1085. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1086. if (!E.value.is_singleton) {
  1087. continue;
  1088. }
  1089. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1090. r_result.insert(option.display, option);
  1091. }
  1092. // Native classes and global constants.
  1093. for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
  1094. ScriptLanguage::CodeCompletionOption option;
  1095. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  1096. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  1097. } else {
  1098. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1099. }
  1100. r_result.insert(option.display, option);
  1101. }
  1102. }
  1103. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  1104. GDScriptCompletionIdentifier ci;
  1105. ci.value = p_value;
  1106. ci.type.is_constant = true;
  1107. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1108. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1109. ci.type.builtin_type = p_value.get_type();
  1110. if (ci.type.builtin_type == Variant::OBJECT) {
  1111. Object *obj = p_value.operator Object *();
  1112. if (!obj) {
  1113. return ci;
  1114. }
  1115. ci.type.native_type = obj->get_class_name();
  1116. Ref<Script> scr = p_value;
  1117. if (scr.is_valid()) {
  1118. ci.type.is_meta_type = true;
  1119. } else {
  1120. ci.type.is_meta_type = false;
  1121. scr = obj->get_script();
  1122. }
  1123. if (scr.is_valid()) {
  1124. ci.type.script_type = scr;
  1125. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1126. ci.type.native_type = scr->get_instance_base_type();
  1127. } else {
  1128. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1129. }
  1130. }
  1131. return ci;
  1132. }
  1133. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  1134. GDScriptCompletionIdentifier ci;
  1135. if (p_property.type == Variant::NIL) {
  1136. // Variant
  1137. return ci;
  1138. }
  1139. if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  1140. ci.enumeration = p_property.class_name;
  1141. }
  1142. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1143. ci.type.builtin_type = p_property.type;
  1144. if (p_property.type == Variant::OBJECT) {
  1145. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1146. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1147. } else {
  1148. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1149. }
  1150. return ci;
  1151. }
  1152. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1153. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1154. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1155. static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
  1156. if (p_expression) {
  1157. switch (p_expression->type) {
  1158. case GDScriptParser::Node::IDENTIFIER: {
  1159. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1160. if (id->name == p_name) {
  1161. return true;
  1162. }
  1163. } break;
  1164. case GDScriptParser::Node::CAST: {
  1165. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1166. return _is_expression_named_identifier(cn->operand, p_name);
  1167. } break;
  1168. default:
  1169. break;
  1170. }
  1171. }
  1172. return false;
  1173. }
  1174. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1175. bool found = false;
  1176. if (p_expression == nullptr) {
  1177. return false;
  1178. }
  1179. if (p_expression->is_constant) {
  1180. // Already has a value, so just use that.
  1181. r_type = _type_from_variant(p_expression->reduced_value);
  1182. found = true;
  1183. } else {
  1184. switch (p_expression->type) {
  1185. case GDScriptParser::Node::LITERAL: {
  1186. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1187. r_type = _type_from_variant(literal->value);
  1188. found = true;
  1189. } break;
  1190. case GDScriptParser::Node::SELF: {
  1191. if (p_context.current_class) {
  1192. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1193. r_type.type.type_source = GDScriptParser::DataType::INFERRED;
  1194. r_type.type.is_constant = true;
  1195. r_type.type.class_type = p_context.current_class;
  1196. r_type.value = p_context.base;
  1197. found = true;
  1198. }
  1199. } break;
  1200. case GDScriptParser::Node::IDENTIFIER: {
  1201. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1202. found = _guess_identifier_type(p_context, id->name, r_type);
  1203. } break;
  1204. case GDScriptParser::Node::DICTIONARY: {
  1205. // Try to recreate the dictionary.
  1206. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1207. Dictionary d;
  1208. bool full = true;
  1209. for (int i = 0; i < dn->elements.size(); i++) {
  1210. GDScriptCompletionIdentifier key;
  1211. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1212. if (!key.type.is_constant) {
  1213. full = false;
  1214. break;
  1215. }
  1216. GDScriptCompletionIdentifier value;
  1217. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1218. if (!value.type.is_constant) {
  1219. full = false;
  1220. break;
  1221. }
  1222. d[key.value] = value.value;
  1223. } else {
  1224. full = false;
  1225. break;
  1226. }
  1227. } else {
  1228. full = false;
  1229. break;
  1230. }
  1231. }
  1232. if (full) {
  1233. r_type.value = d;
  1234. r_type.type.is_constant = true;
  1235. }
  1236. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1237. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1238. r_type.type.builtin_type = Variant::DICTIONARY;
  1239. found = true;
  1240. } break;
  1241. case GDScriptParser::Node::ARRAY: {
  1242. // Try to recreate the array
  1243. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1244. Array a;
  1245. bool full = true;
  1246. a.resize(an->elements.size());
  1247. for (int i = 0; i < an->elements.size(); i++) {
  1248. GDScriptCompletionIdentifier value;
  1249. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1250. if (value.type.is_constant) {
  1251. a[i] = value.value;
  1252. } else {
  1253. full = false;
  1254. break;
  1255. }
  1256. } else {
  1257. full = false;
  1258. break;
  1259. }
  1260. }
  1261. if (full) {
  1262. // If not fully constant, setting this value is detrimental to the inference.
  1263. r_type.value = a;
  1264. }
  1265. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1266. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1267. r_type.type.builtin_type = Variant::ARRAY;
  1268. found = true;
  1269. } break;
  1270. case GDScriptParser::Node::CAST: {
  1271. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1272. GDScriptCompletionIdentifier value;
  1273. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1274. r_type.type = cn->get_datatype();
  1275. found = true;
  1276. }
  1277. } break;
  1278. case GDScriptParser::Node::CALL: {
  1279. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1280. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1281. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1282. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1283. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1284. found = true;
  1285. break;
  1286. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  1287. MethodInfo mi = GDScriptUtilityFunctions::get_function_info(call->function_name);
  1288. r_type = _type_from_property(mi.return_val);
  1289. found = true;
  1290. break;
  1291. } else {
  1292. GDScriptParser::CompletionContext c = p_context;
  1293. c.current_line = call->start_line;
  1294. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1295. GDScriptCompletionIdentifier base;
  1296. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1297. // Simple call, so base is 'self'.
  1298. if (p_context.current_class) {
  1299. base.type.kind = GDScriptParser::DataType::CLASS;
  1300. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1301. base.type.is_constant = true;
  1302. base.type.class_type = p_context.current_class;
  1303. base.value = p_context.base;
  1304. } else {
  1305. break;
  1306. }
  1307. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1308. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1309. found = false;
  1310. break;
  1311. }
  1312. } else {
  1313. break;
  1314. }
  1315. // Try call if constant methods with constant arguments
  1316. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1317. GDScriptParser::DataType native_type = base.type;
  1318. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1319. native_type = native_type.class_type->base_type;
  1320. }
  1321. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1322. if (native_type.script_type.is_valid()) {
  1323. Ref<Script> parent = native_type.script_type->get_base_script();
  1324. if (parent.is_valid()) {
  1325. native_type.script_type = parent;
  1326. } else {
  1327. native_type.kind = GDScriptParser::DataType::NATIVE;
  1328. native_type.native_type = native_type.script_type->get_instance_base_type();
  1329. if (!ClassDB::class_exists(native_type.native_type)) {
  1330. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1336. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1337. if (mb && mb->is_const()) {
  1338. bool all_is_const = true;
  1339. Vector<Variant> args;
  1340. GDScriptParser::CompletionContext c2 = p_context;
  1341. c2.current_line = call->start_line;
  1342. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1343. GDScriptCompletionIdentifier arg;
  1344. if (!call->arguments[i]->is_constant) {
  1345. all_is_const = false;
  1346. }
  1347. }
  1348. Object *baseptr = base.value;
  1349. if (all_is_const && call->function_name == SNAME("get_node") && ClassDB::is_parent_class(native_type.native_type, SNAME("Node")) && args.size()) {
  1350. String arg1 = args[0];
  1351. if (arg1.begins_with("/root/")) {
  1352. String which = arg1.get_slice("/", 2);
  1353. if (!which.is_empty()) {
  1354. // Try singletons first
  1355. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1356. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1357. found = true;
  1358. } else {
  1359. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1360. String name = E.key;
  1361. if (name == which) {
  1362. String script = E.value.path;
  1363. if (!script.begins_with("res://")) {
  1364. script = "res://" + script;
  1365. }
  1366. if (!script.ends_with(".gd")) {
  1367. // not a script, try find the script anyway,
  1368. // may have some success
  1369. script = script.get_basename() + ".gd";
  1370. }
  1371. if (FileAccess::exists(script)) {
  1372. Error err = OK;
  1373. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1374. if (err == OK) {
  1375. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1376. r_type.type.script_path = script;
  1377. r_type.type.class_type = parser->get_parser()->get_tree();
  1378. r_type.type.is_constant = false;
  1379. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1380. r_type.value = Variant();
  1381. p_context.dependent_parsers.push_back(parser);
  1382. found = true;
  1383. }
  1384. }
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. if (!found && all_is_const && baseptr) {
  1393. Vector<const Variant *> argptr;
  1394. for (int i = 0; i < args.size(); i++) {
  1395. argptr.push_back(&args[i]);
  1396. }
  1397. Callable::CallError ce;
  1398. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1399. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1400. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1401. r_type = _type_from_variant(ret);
  1402. found = true;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. }
  1409. if (!found) {
  1410. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1411. }
  1412. }
  1413. } break;
  1414. case GDScriptParser::Node::SUBSCRIPT: {
  1415. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1416. if (subscript->is_attribute) {
  1417. GDScriptParser::CompletionContext c = p_context;
  1418. c.current_line = subscript->start_line;
  1419. GDScriptCompletionIdentifier base;
  1420. if (!_guess_expression_type(c, subscript->base, base)) {
  1421. found = false;
  1422. break;
  1423. }
  1424. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1425. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1426. r_type = _type_from_variant(value);
  1427. found = true;
  1428. break;
  1429. }
  1430. const GDScriptParser::DictionaryNode *dn = nullptr;
  1431. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1432. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1433. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1434. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1435. }
  1436. if (dn) {
  1437. for (int i = 0; i < dn->elements.size(); i++) {
  1438. GDScriptCompletionIdentifier key;
  1439. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1440. continue;
  1441. }
  1442. if (key.value == String(subscript->attribute->name)) {
  1443. r_type.assigned_expression = dn->elements[i].value;
  1444. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. if (!found) {
  1450. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1451. }
  1452. } else {
  1453. if (subscript->index == nullptr) {
  1454. found = false;
  1455. break;
  1456. }
  1457. GDScriptParser::CompletionContext c = p_context;
  1458. c.current_line = subscript->start_line;
  1459. GDScriptCompletionIdentifier base;
  1460. if (!_guess_expression_type(c, subscript->base, base)) {
  1461. found = false;
  1462. break;
  1463. }
  1464. GDScriptCompletionIdentifier index;
  1465. if (!_guess_expression_type(c, subscript->index, index)) {
  1466. found = false;
  1467. break;
  1468. }
  1469. if (base.value.in(index.value)) {
  1470. Variant value = base.value.get(index.value);
  1471. r_type = _type_from_variant(value);
  1472. found = true;
  1473. break;
  1474. }
  1475. // Look if it is a dictionary node.
  1476. const GDScriptParser::DictionaryNode *dn = nullptr;
  1477. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1478. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1479. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1480. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1481. }
  1482. if (dn) {
  1483. for (int i = 0; i < dn->elements.size(); i++) {
  1484. GDScriptCompletionIdentifier key;
  1485. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1486. continue;
  1487. }
  1488. if (key.value == index.value) {
  1489. r_type.assigned_expression = dn->elements[i].value;
  1490. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1491. break;
  1492. }
  1493. }
  1494. }
  1495. // Look if it is an array node.
  1496. if (!found && index.value.is_num()) {
  1497. int idx = index.value;
  1498. const GDScriptParser::ArrayNode *an = nullptr;
  1499. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1500. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1501. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1502. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1503. }
  1504. if (an && idx >= 0 && an->elements.size() > idx) {
  1505. r_type.assigned_expression = an->elements[idx];
  1506. found = _guess_expression_type(c, an->elements[idx], r_type);
  1507. break;
  1508. }
  1509. }
  1510. // Look for valid indexing in other types
  1511. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1512. StringName id = index.value;
  1513. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1514. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1515. Callable::CallError err;
  1516. Variant base_val;
  1517. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1518. bool valid = false;
  1519. Variant res = base_val.get(index.value, &valid);
  1520. if (valid) {
  1521. r_type = _type_from_variant(res);
  1522. r_type.value = Variant();
  1523. r_type.type.is_constant = false;
  1524. found = true;
  1525. }
  1526. }
  1527. }
  1528. } break;
  1529. case GDScriptParser::Node::BINARY_OPERATOR: {
  1530. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1531. if (op->variant_op == Variant::OP_MAX) {
  1532. break;
  1533. }
  1534. GDScriptParser::CompletionContext context = p_context;
  1535. context.current_line = op->start_line;
  1536. GDScriptCompletionIdentifier p1;
  1537. GDScriptCompletionIdentifier p2;
  1538. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1539. found = false;
  1540. break;
  1541. }
  1542. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1543. found = false;
  1544. break;
  1545. }
  1546. Callable::CallError ce;
  1547. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1548. Variant d1;
  1549. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1550. Variant d2;
  1551. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1552. Variant v1 = (v1_use_value) ? p1.value : d1;
  1553. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1554. Variant v2 = (v2_use_value) ? p2.value : d2;
  1555. // avoid potential invalid ops
  1556. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1557. v2 = 1;
  1558. v2_use_value = false;
  1559. }
  1560. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1561. v2 = 1.0;
  1562. v2_use_value = false;
  1563. }
  1564. Variant res;
  1565. bool valid;
  1566. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1567. if (!valid) {
  1568. found = false;
  1569. break;
  1570. }
  1571. r_type = _type_from_variant(res);
  1572. if (!v1_use_value || !v2_use_value) {
  1573. r_type.value = Variant();
  1574. r_type.type.is_constant = false;
  1575. }
  1576. found = true;
  1577. } break;
  1578. default:
  1579. break;
  1580. }
  1581. }
  1582. // It may have found a null, but that's never useful
  1583. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1584. found = false;
  1585. }
  1586. // Check type hint last. For collections we want chance to get the actual value first
  1587. // This way we can detect types from the content of dictionaries and arrays
  1588. if (!found && p_expression->get_datatype().is_hard_type()) {
  1589. r_type.type = p_expression->get_datatype();
  1590. if (!r_type.assigned_expression) {
  1591. r_type.assigned_expression = p_expression;
  1592. }
  1593. found = true;
  1594. }
  1595. return found;
  1596. }
  1597. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1598. // Look in blocks first.
  1599. int last_assign_line = -1;
  1600. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1601. GDScriptParser::DataType id_type;
  1602. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1603. bool is_function_parameter = false;
  1604. if (suite) {
  1605. if (suite->has_local(p_identifier)) {
  1606. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier);
  1607. id_type = local.get_datatype();
  1608. // Check initializer as the first assignment.
  1609. switch (local.type) {
  1610. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1611. if (local.variable->initializer) {
  1612. last_assign_line = local.variable->initializer->end_line;
  1613. last_assigned_expression = local.variable->initializer;
  1614. }
  1615. break;
  1616. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1617. if (local.constant->initializer) {
  1618. last_assign_line = local.constant->initializer->end_line;
  1619. last_assigned_expression = local.constant->initializer;
  1620. }
  1621. break;
  1622. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1623. if (local.parameter->default_value) {
  1624. last_assign_line = local.parameter->default_value->end_line;
  1625. last_assigned_expression = local.parameter->default_value;
  1626. }
  1627. is_function_parameter = true;
  1628. break;
  1629. default:
  1630. break;
  1631. }
  1632. }
  1633. }
  1634. while (suite) {
  1635. for (int i = 0; i < suite->statements.size(); i++) {
  1636. if (suite->statements[i]->end_line >= p_context.current_line) {
  1637. break;
  1638. }
  1639. switch (suite->statements[i]->type) {
  1640. case GDScriptParser::Node::ASSIGNMENT: {
  1641. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1642. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1643. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1644. if (id->name == p_identifier) {
  1645. last_assign_line = assign->assigned_value->end_line;
  1646. last_assigned_expression = assign->assigned_value;
  1647. }
  1648. }
  1649. } break;
  1650. default:
  1651. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1652. break;
  1653. }
  1654. }
  1655. 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) {
  1656. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1657. // Super dirty hack, but very useful.
  1658. // Credit: Zylann.
  1659. // TODO: this could be hacked to detect ANDed conditions too...
  1660. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition);
  1661. 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) {
  1662. // Bingo.
  1663. GDScriptParser::CompletionContext c = p_context;
  1664. c.current_line = op->left_operand->start_line;
  1665. c.current_suite = suite;
  1666. GDScriptCompletionIdentifier is_type;
  1667. if (_guess_expression_type(c, op->right_operand, is_type)) {
  1668. id_type = is_type.type;
  1669. id_type.is_meta_type = false;
  1670. if (last_assign_line < c.current_line) {
  1671. // Override last assignment.
  1672. last_assign_line = c.current_line;
  1673. last_assigned_expression = nullptr;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. suite = suite->parent_block;
  1679. }
  1680. if (last_assigned_expression && last_assign_line < p_context.current_line) {
  1681. GDScriptParser::CompletionContext c = p_context;
  1682. c.current_line = last_assign_line;
  1683. r_type.assigned_expression = last_assigned_expression;
  1684. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1685. return true;
  1686. }
  1687. }
  1688. if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
  1689. // Check if it's override of native function, then we can assume the type from the signature.
  1690. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1691. while (base_type.is_set()) {
  1692. switch (base_type.kind) {
  1693. case GDScriptParser::DataType::CLASS:
  1694. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1695. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1696. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier]];
  1697. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1698. id_type = parameter->get_datatype();
  1699. }
  1700. if (parameter->default_value) {
  1701. GDScriptParser::CompletionContext c = p_context;
  1702. c.current_function = parent_function;
  1703. c.current_class = base_type.class_type;
  1704. c.base = nullptr;
  1705. if (_guess_expression_type(c, parameter->default_value, r_type)) {
  1706. return true;
  1707. }
  1708. }
  1709. }
  1710. base_type = base_type.class_type->base_type;
  1711. break;
  1712. case GDScriptParser::DataType::NATIVE: {
  1713. if (id_type.is_set() && !id_type.is_variant()) {
  1714. base_type = GDScriptParser::DataType();
  1715. break;
  1716. }
  1717. MethodInfo info;
  1718. if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
  1719. for (const PropertyInfo &E : info.arguments) {
  1720. if (E.name == p_identifier) {
  1721. r_type = _type_from_property(E);
  1722. return true;
  1723. }
  1724. }
  1725. }
  1726. base_type = GDScriptParser::DataType();
  1727. } break;
  1728. default:
  1729. break;
  1730. }
  1731. }
  1732. }
  1733. if (id_type.is_set() && !id_type.is_variant()) {
  1734. r_type.type = id_type;
  1735. return true;
  1736. }
  1737. // Check current class (including inheritance).
  1738. if (p_context.current_class) {
  1739. GDScriptCompletionIdentifier base;
  1740. base.value = p_context.base;
  1741. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1742. base.type.kind = GDScriptParser::DataType::CLASS;
  1743. base.type.class_type = p_context.current_class;
  1744. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1745. if (_guess_identifier_type_from_base(p_context, base, p_identifier, r_type)) {
  1746. return true;
  1747. }
  1748. }
  1749. // Check global scripts.
  1750. if (ScriptServer::is_global_class(p_identifier)) {
  1751. String script = ScriptServer::get_global_class_path(p_identifier);
  1752. if (script.to_lower().ends_with(".gd")) {
  1753. Error err = OK;
  1754. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1755. if (err == OK) {
  1756. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1757. r_type.type.script_path = script;
  1758. r_type.type.class_type = parser->get_parser()->get_tree();
  1759. r_type.type.is_constant = false;
  1760. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1761. r_type.value = Variant();
  1762. p_context.dependent_parsers.push_back(parser);
  1763. return true;
  1764. }
  1765. } else {
  1766. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1767. if (scr.is_valid()) {
  1768. r_type = _type_from_variant(scr);
  1769. r_type.type.is_meta_type = true;
  1770. return true;
  1771. }
  1772. }
  1773. return false;
  1774. }
  1775. // Check global variables (including autoloads).
  1776. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier)) {
  1777. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1778. return true;
  1779. }
  1780. // Check ClassDB.
  1781. if (ClassDB::class_exists(p_identifier) && ClassDB::is_class_exposed(p_identifier)) {
  1782. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1783. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1784. r_type.type.native_type = p_identifier;
  1785. r_type.type.is_constant = true;
  1786. r_type.type.is_meta_type = !Engine::get_singleton()->has_singleton(p_identifier);
  1787. r_type.value = Variant();
  1788. }
  1789. return false;
  1790. }
  1791. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1792. GDScriptParser::DataType base_type = p_base.type;
  1793. bool is_static = base_type.is_meta_type;
  1794. while (base_type.is_set()) {
  1795. switch (base_type.kind) {
  1796. case GDScriptParser::DataType::CLASS:
  1797. if (base_type.class_type->has_member(p_identifier)) {
  1798. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1799. switch (member.type) {
  1800. case GDScriptParser::ClassNode::Member::CONSTANT:
  1801. r_type.type = member.constant->get_datatype();
  1802. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1803. r_type.value = member.constant->initializer->reduced_value;
  1804. }
  1805. return true;
  1806. case GDScriptParser::ClassNode::Member::VARIABLE:
  1807. if (!is_static) {
  1808. if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1809. r_type.type = member.variable->get_datatype();
  1810. return true;
  1811. } else if (member.variable->initializer) {
  1812. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1813. if (init->is_constant) {
  1814. r_type.value = init->reduced_value;
  1815. r_type = _type_from_variant(init->reduced_value);
  1816. return true;
  1817. } else if (init->start_line == p_context.current_line) {
  1818. return false;
  1819. // Detects if variable is assigned to itself
  1820. } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
  1821. if (member.variable->initializer->get_datatype().is_set()) {
  1822. r_type.type = member.variable->initializer->get_datatype();
  1823. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1824. r_type.type = member.variable->get_datatype();
  1825. }
  1826. return true;
  1827. } else if (_guess_expression_type(p_context, init, r_type)) {
  1828. return true;
  1829. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1830. r_type.type = init->get_datatype();
  1831. return true;
  1832. }
  1833. }
  1834. }
  1835. // TODO: Check assignments in constructor.
  1836. return false;
  1837. case GDScriptParser::ClassNode::Member::ENUM:
  1838. r_type.type = member.m_enum->get_datatype();
  1839. r_type.enumeration = member.m_enum->identifier->name;
  1840. return true;
  1841. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1842. r_type = _type_from_variant(member.enum_value.value);
  1843. return true;
  1844. case GDScriptParser::ClassNode::Member::SIGNAL:
  1845. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1846. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1847. r_type.type.builtin_type = Variant::SIGNAL;
  1848. return true;
  1849. case GDScriptParser::ClassNode::Member::FUNCTION:
  1850. if (is_static && !member.function->is_static) {
  1851. return false;
  1852. }
  1853. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1854. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1855. r_type.type.builtin_type = Variant::CALLABLE;
  1856. return true;
  1857. case GDScriptParser::ClassNode::Member::CLASS:
  1858. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1859. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1860. r_type.type.class_type = member.m_class;
  1861. return true;
  1862. case GDScriptParser::ClassNode::Member::GROUP:
  1863. return false; // No-op, but silences warnings.
  1864. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1865. return false; // Unreachable.
  1866. }
  1867. return false;
  1868. }
  1869. base_type = base_type.class_type->base_type;
  1870. break;
  1871. case GDScriptParser::DataType::SCRIPT: {
  1872. Ref<Script> scr = base_type.script_type;
  1873. if (scr.is_valid()) {
  1874. HashMap<StringName, Variant> constants;
  1875. scr->get_constants(&constants);
  1876. if (constants.has(p_identifier)) {
  1877. r_type = _type_from_variant(constants[p_identifier]);
  1878. return true;
  1879. }
  1880. if (!is_static) {
  1881. List<PropertyInfo> members;
  1882. scr->get_script_property_list(&members);
  1883. for (const PropertyInfo &prop : members) {
  1884. if (prop.name == p_identifier) {
  1885. r_type = _type_from_property(prop);
  1886. return true;
  1887. }
  1888. }
  1889. }
  1890. Ref<Script> parent = scr->get_base_script();
  1891. if (parent.is_valid()) {
  1892. base_type.script_type = parent;
  1893. } else {
  1894. base_type.kind = GDScriptParser::DataType::NATIVE;
  1895. base_type.native_type = scr->get_instance_base_type();
  1896. }
  1897. } else {
  1898. return false;
  1899. }
  1900. } break;
  1901. case GDScriptParser::DataType::NATIVE: {
  1902. StringName class_name = base_type.native_type;
  1903. if (!ClassDB::class_exists(class_name)) {
  1904. return false;
  1905. }
  1906. // Skip constants since they're all integers. Type does not matter because int has no members.
  1907. PropertyInfo prop;
  1908. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  1909. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1910. if (getter != StringName()) {
  1911. MethodBind *g = ClassDB::get_method(class_name, getter);
  1912. if (g) {
  1913. r_type = _type_from_property(g->get_return_info());
  1914. return true;
  1915. }
  1916. } else {
  1917. r_type = _type_from_property(prop);
  1918. return true;
  1919. }
  1920. }
  1921. return false;
  1922. } break;
  1923. case GDScriptParser::DataType::BUILTIN: {
  1924. Callable::CallError err;
  1925. Variant tmp;
  1926. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1927. if (err.error != Callable::CallError::CALL_OK) {
  1928. return false;
  1929. }
  1930. bool valid = false;
  1931. Variant res = tmp.get(p_identifier, &valid);
  1932. if (valid) {
  1933. r_type = _type_from_variant(res);
  1934. r_type.value = Variant();
  1935. r_type.type.is_constant = false;
  1936. return true;
  1937. }
  1938. return false;
  1939. } break;
  1940. default: {
  1941. return false;
  1942. } break;
  1943. }
  1944. }
  1945. return false;
  1946. }
  1947. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  1948. if (!p_context.current_suite) {
  1949. return;
  1950. }
  1951. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  1952. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  1953. break;
  1954. }
  1955. GDScriptParser::CompletionContext c = p_context;
  1956. switch (p_context.current_suite->statements[i]->type) {
  1957. case GDScriptParser::Node::FOR:
  1958. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  1959. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1960. break;
  1961. case GDScriptParser::Node::WHILE:
  1962. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  1963. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1964. break;
  1965. case GDScriptParser::Node::IF: {
  1966. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  1967. c.current_suite = _if->true_block;
  1968. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1969. if (_if->false_block) {
  1970. c.current_suite = _if->false_block;
  1971. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1972. }
  1973. } break;
  1974. case GDScriptParser::Node::MATCH: {
  1975. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  1976. for (int j = 0; j < match->branches.size(); j++) {
  1977. c.current_suite = match->branches[j]->block;
  1978. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1979. }
  1980. } break;
  1981. case GDScriptParser::Node::RETURN: {
  1982. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  1983. if (ret->return_value) {
  1984. if (ret->start_line > r_last_return_line) {
  1985. r_last_return_line = ret->start_line;
  1986. *r_last_returned_value = ret->return_value;
  1987. }
  1988. }
  1989. } break;
  1990. default:
  1991. break;
  1992. }
  1993. }
  1994. }
  1995. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1996. GDScriptParser::DataType base_type = p_base.type;
  1997. bool is_static = base_type.is_meta_type;
  1998. if (is_static && p_method == SNAME("new")) {
  1999. r_type.type = base_type;
  2000. r_type.type.is_meta_type = false;
  2001. r_type.type.is_constant = false;
  2002. return true;
  2003. }
  2004. while (base_type.is_set() && !base_type.is_variant()) {
  2005. switch (base_type.kind) {
  2006. case GDScriptParser::DataType::CLASS:
  2007. if (base_type.class_type->has_function(p_method)) {
  2008. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  2009. if (!is_static || method->is_static) {
  2010. int last_return_line = -1;
  2011. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  2012. GDScriptParser::CompletionContext c = p_context;
  2013. c.current_class = base_type.class_type;
  2014. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  2015. c.current_suite = method->body;
  2016. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  2017. if (last_returned_value) {
  2018. c.current_line = c.current_suite->end_line;
  2019. if (_guess_expression_type(c, last_returned_value, r_type)) {
  2020. return true;
  2021. }
  2022. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  2023. r_type.type = method->get_datatype();
  2024. return true;
  2025. }
  2026. }
  2027. }
  2028. }
  2029. base_type = base_type.class_type->base_type;
  2030. break;
  2031. case GDScriptParser::DataType::SCRIPT: {
  2032. Ref<Script> scr = base_type.script_type;
  2033. if (scr.is_valid()) {
  2034. List<MethodInfo> methods;
  2035. scr->get_script_method_list(&methods);
  2036. for (const MethodInfo &mi : methods) {
  2037. if (mi.name == p_method) {
  2038. r_type = _type_from_property(mi.return_val);
  2039. return true;
  2040. }
  2041. }
  2042. Ref<Script> base_script = scr->get_base_script();
  2043. if (base_script.is_valid()) {
  2044. base_type.script_type = base_script;
  2045. } else {
  2046. base_type.kind = GDScriptParser::DataType::NATIVE;
  2047. base_type.native_type = scr->get_instance_base_type();
  2048. }
  2049. } else {
  2050. return false;
  2051. }
  2052. } break;
  2053. case GDScriptParser::DataType::NATIVE: {
  2054. if (!ClassDB::class_exists(base_type.native_type)) {
  2055. return false;
  2056. }
  2057. MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
  2058. if (mb) {
  2059. r_type = _type_from_property(mb->get_return_info());
  2060. return true;
  2061. }
  2062. return false;
  2063. } break;
  2064. case GDScriptParser::DataType::BUILTIN: {
  2065. Callable::CallError err;
  2066. Variant tmp;
  2067. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2068. if (err.error != Callable::CallError::CALL_OK) {
  2069. return false;
  2070. }
  2071. List<MethodInfo> methods;
  2072. tmp.get_method_list(&methods);
  2073. for (const MethodInfo &mi : methods) {
  2074. if (mi.name == p_method) {
  2075. r_type = _type_from_property(mi.return_val);
  2076. return true;
  2077. }
  2078. }
  2079. return false;
  2080. } break;
  2081. default: {
  2082. return false;
  2083. }
  2084. }
  2085. }
  2086. return false;
  2087. }
  2088. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  2089. if (!p_enum_hint.contains(".")) {
  2090. // Global constant or in the current class.
  2091. StringName current_enum = p_enum_hint;
  2092. 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) {
  2093. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  2094. for (int i = 0; i < _enum->values.size(); i++) {
  2095. ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2096. r_result.insert(option.display, option);
  2097. }
  2098. } else {
  2099. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  2100. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  2101. ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2102. r_result.insert(option.display, option);
  2103. }
  2104. }
  2105. }
  2106. } else {
  2107. String class_name = p_enum_hint.get_slice(".", 0);
  2108. String enum_name = p_enum_hint.get_slice(".", 1);
  2109. if (!ClassDB::class_exists(class_name)) {
  2110. return;
  2111. }
  2112. List<StringName> enum_constants;
  2113. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  2114. for (const StringName &E : enum_constants) {
  2115. String candidate = class_name + "." + E;
  2116. int location = _get_enum_constant_location(class_name, E);
  2117. ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  2118. r_result.insert(option.display, option);
  2119. }
  2120. }
  2121. }
  2122. 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) {
  2123. Variant base = p_base.value;
  2124. GDScriptParser::DataType base_type = p_base.type;
  2125. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2126. while (base_type.is_set() && !base_type.is_variant()) {
  2127. switch (base_type.kind) {
  2128. case GDScriptParser::DataType::CLASS: {
  2129. if (base_type.class_type->has_member(p_method)) {
  2130. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  2131. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2132. r_arghint = _make_arguments_hint(member.function, p_argidx);
  2133. return;
  2134. }
  2135. }
  2136. base_type = base_type.class_type->base_type;
  2137. } break;
  2138. case GDScriptParser::DataType::NATIVE: {
  2139. StringName class_name = base_type.native_type;
  2140. if (!ClassDB::class_exists(class_name)) {
  2141. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2142. break;
  2143. }
  2144. MethodInfo info;
  2145. int method_args = 0;
  2146. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  2147. method_args = info.arguments.size();
  2148. if (base.get_type() == Variant::OBJECT) {
  2149. Object *obj = base.operator Object *();
  2150. if (obj) {
  2151. List<String> options;
  2152. obj->get_argument_options(p_method, p_argidx, &options);
  2153. for (String &opt : options) {
  2154. if (opt.is_quoted()) {
  2155. opt = opt.unquote().quote(quote_style); // Handle user preference.
  2156. }
  2157. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2158. r_result.insert(option.display, option);
  2159. }
  2160. }
  2161. }
  2162. if (p_argidx < method_args) {
  2163. PropertyInfo arg_info = info.arguments[p_argidx];
  2164. if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  2165. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  2166. }
  2167. }
  2168. r_arghint = _make_arguments_hint(info, p_argidx);
  2169. }
  2170. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (p_method == SNAME("get_node") || p_method == SNAME("has_node"))) {
  2171. // Get autoloads
  2172. List<PropertyInfo> props;
  2173. ProjectSettings::get_singleton()->get_property_list(&props);
  2174. for (const PropertyInfo &E : props) {
  2175. String s = E.name;
  2176. if (!s.begins_with("autoload/")) {
  2177. continue;
  2178. }
  2179. String name = s.get_slice("/", 1);
  2180. ScriptLanguage::CodeCompletionOption option("/root/" + name, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2181. option.insert_text = option.display.quote(quote_style);
  2182. r_result.insert(option.display, option);
  2183. }
  2184. }
  2185. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && p_method.operator String().contains("action")) {
  2186. // Get input actions
  2187. List<PropertyInfo> props;
  2188. ProjectSettings::get_singleton()->get_property_list(&props);
  2189. for (const PropertyInfo &E : props) {
  2190. String s = E.name;
  2191. if (!s.begins_with("input/")) {
  2192. continue;
  2193. }
  2194. String name = s.get_slice("/", 1);
  2195. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2196. option.insert_text = option.display.quote(quote_style);
  2197. r_result.insert(option.display, option);
  2198. }
  2199. }
  2200. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2201. } break;
  2202. case GDScriptParser::DataType::BUILTIN: {
  2203. if (base.get_type() == Variant::NIL) {
  2204. Callable::CallError err;
  2205. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2206. if (err.error != Callable::CallError::CALL_OK) {
  2207. return;
  2208. }
  2209. }
  2210. List<MethodInfo> methods;
  2211. base.get_method_list(&methods);
  2212. for (const MethodInfo &E : methods) {
  2213. if (E.name == p_method) {
  2214. r_arghint = _make_arguments_hint(E, p_argidx);
  2215. return;
  2216. }
  2217. }
  2218. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2219. } break;
  2220. default: {
  2221. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2222. } break;
  2223. }
  2224. }
  2225. }
  2226. 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) {
  2227. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2228. if (p_argidx == 0 && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2229. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2230. }
  2231. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2232. r_arghint = _make_arguments_hint(mi, p_argidx);
  2233. return;
  2234. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2235. return;
  2236. }
  2237. Variant base;
  2238. GDScriptParser::DataType base_type;
  2239. bool _static = false;
  2240. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2241. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  2242. GDScriptCompletionIdentifier connect_base;
  2243. if (Variant::has_utility_function(call->function_name)) {
  2244. MethodInfo info = Variant::get_utility_function_info(call->function_name);
  2245. r_arghint = _make_arguments_hint(info, p_argidx);
  2246. return;
  2247. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  2248. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  2249. r_arghint = _make_arguments_hint(info, p_argidx);
  2250. return;
  2251. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2252. // Complete constructor.
  2253. List<MethodInfo> constructors;
  2254. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2255. int i = 0;
  2256. for (const MethodInfo &E : constructors) {
  2257. if (p_argidx >= E.arguments.size()) {
  2258. continue;
  2259. }
  2260. if (i > 0) {
  2261. r_arghint += "\n";
  2262. }
  2263. r_arghint += _make_arguments_hint(E, p_argidx);
  2264. i++;
  2265. }
  2266. return;
  2267. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2268. base = p_context.base;
  2269. if (p_context.current_class) {
  2270. base_type = p_context.current_class->get_datatype();
  2271. _static = !p_context.current_function || p_context.current_function->is_static;
  2272. }
  2273. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2274. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2275. if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2276. const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
  2277. Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
  2278. if (method_type < Variant::VARIANT_MAX) {
  2279. Variant v;
  2280. Callable::CallError err;
  2281. Variant::construct(method_type, v, nullptr, 0, err);
  2282. if (err.error != Callable::CallError::CALL_OK) {
  2283. return;
  2284. }
  2285. List<MethodInfo> methods;
  2286. v.get_method_list(&methods);
  2287. for (MethodInfo &E : methods) {
  2288. if (p_argidx >= E.arguments.size()) {
  2289. continue;
  2290. }
  2291. if (E.name == call->function_name) {
  2292. r_arghint += _make_arguments_hint(E, p_argidx);
  2293. return;
  2294. }
  2295. }
  2296. }
  2297. }
  2298. if (subscript->is_attribute) {
  2299. GDScriptCompletionIdentifier ci;
  2300. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2301. base_type = ci.type;
  2302. base = ci.value;
  2303. } else {
  2304. return;
  2305. }
  2306. _static = base_type.is_meta_type;
  2307. }
  2308. } else {
  2309. return;
  2310. }
  2311. GDScriptCompletionIdentifier ci;
  2312. ci.type = base_type;
  2313. ci.value = base;
  2314. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2315. r_forced = r_result.size() > 0;
  2316. }
  2317. ::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) {
  2318. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2319. GDScriptParser parser;
  2320. GDScriptAnalyzer analyzer(&parser);
  2321. parser.parse(p_code, p_path, true);
  2322. analyzer.analyze();
  2323. r_forced = false;
  2324. HashMap<String, ScriptLanguage::CodeCompletionOption> options;
  2325. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2326. completion_context.base = p_owner;
  2327. bool is_function = false;
  2328. switch (completion_context.type) {
  2329. case GDScriptParser::COMPLETION_NONE:
  2330. break;
  2331. case GDScriptParser::COMPLETION_ANNOTATION: {
  2332. List<MethodInfo> annotations;
  2333. parser.get_annotation_list(&annotations);
  2334. for (const MethodInfo &E : annotations) {
  2335. ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2336. if (E.arguments.size() > 0) {
  2337. option.insert_text += "(";
  2338. }
  2339. options.insert(option.display, option);
  2340. }
  2341. r_forced = true;
  2342. } break;
  2343. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2344. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2345. break;
  2346. }
  2347. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2348. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2349. r_forced = true;
  2350. } break;
  2351. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  2352. // Constants.
  2353. {
  2354. List<StringName> constants;
  2355. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2356. for (const StringName &E : constants) {
  2357. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2358. bool valid = false;
  2359. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
  2360. if (valid) {
  2361. option.default_value = default_value;
  2362. }
  2363. options.insert(option.display, option);
  2364. }
  2365. }
  2366. // Methods.
  2367. {
  2368. List<StringName> methods;
  2369. Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
  2370. for (const StringName &E : methods) {
  2371. if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
  2372. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2373. if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
  2374. option.insert_text += "(";
  2375. } else {
  2376. option.insert_text += "()";
  2377. }
  2378. options.insert(option.display, option);
  2379. }
  2380. }
  2381. }
  2382. } break;
  2383. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2384. _list_available_types(true, completion_context, options);
  2385. r_forced = true;
  2386. } break;
  2387. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2388. ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2389. options.insert(option.display, option);
  2390. }
  2391. [[fallthrough]];
  2392. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2393. _list_available_types(false, completion_context, options);
  2394. r_forced = true;
  2395. } break;
  2396. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2397. _list_available_types(false, completion_context, options);
  2398. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2399. options.insert(get.display, get);
  2400. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2401. options.insert(set.display, set);
  2402. r_forced = true;
  2403. } break;
  2404. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2405. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2406. options.insert(get.display, get);
  2407. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2408. options.insert(set.display, set);
  2409. r_forced = true;
  2410. } break;
  2411. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2412. if (!completion_context.current_class) {
  2413. break;
  2414. }
  2415. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2416. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2417. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2418. continue;
  2419. }
  2420. if (member.function->is_static) {
  2421. continue;
  2422. }
  2423. ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2424. options.insert(option.display, option);
  2425. }
  2426. r_forced = true;
  2427. } break;
  2428. case GDScriptParser::COMPLETION_ASSIGN: {
  2429. GDScriptCompletionIdentifier type;
  2430. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2431. break;
  2432. }
  2433. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2434. _find_identifiers(completion_context, false, options, 0);
  2435. r_forced = true;
  2436. break;
  2437. }
  2438. if (!type.enumeration.is_empty()) {
  2439. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2440. r_forced = options.size() > 0;
  2441. } else {
  2442. _find_identifiers(completion_context, false, options, 0);
  2443. r_forced = true;
  2444. }
  2445. } break;
  2446. case GDScriptParser::COMPLETION_METHOD:
  2447. is_function = true;
  2448. [[fallthrough]];
  2449. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2450. _find_identifiers(completion_context, is_function, options, 0);
  2451. } break;
  2452. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2453. is_function = true;
  2454. [[fallthrough]];
  2455. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2456. r_forced = true;
  2457. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2458. if (attr->base) {
  2459. GDScriptCompletionIdentifier base;
  2460. if (!_guess_expression_type(completion_context, attr->base, base)) {
  2461. break;
  2462. }
  2463. _find_identifiers_in_base(base, is_function, options, 0);
  2464. }
  2465. } break;
  2466. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2467. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2468. GDScriptCompletionIdentifier base;
  2469. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2470. break;
  2471. }
  2472. GDScriptParser::CompletionContext c = completion_context;
  2473. c.current_function = nullptr;
  2474. c.current_suite = nullptr;
  2475. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2476. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2477. c.current_class = base.type.class_type;
  2478. } else {
  2479. c.current_class = nullptr;
  2480. }
  2481. _find_identifiers_in_base(base, false, options, 0);
  2482. } break;
  2483. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2484. if (!completion_context.current_class) {
  2485. break;
  2486. }
  2487. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2488. bool found = true;
  2489. GDScriptCompletionIdentifier base;
  2490. base.type.kind = GDScriptParser::DataType::CLASS;
  2491. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2492. base.type.is_constant = true;
  2493. base.type.class_type = completion_context.current_class;
  2494. base.value = completion_context.base;
  2495. for (int i = 0; i < completion_context.current_argument; i++) {
  2496. GDScriptCompletionIdentifier ci;
  2497. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2498. found = false;
  2499. break;
  2500. }
  2501. base = ci;
  2502. }
  2503. // TODO: Improve this to only list types.
  2504. if (found) {
  2505. _find_identifiers_in_base(base, false, options, 0);
  2506. }
  2507. r_forced = true;
  2508. } break;
  2509. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2510. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2511. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2512. r_forced = true;
  2513. }
  2514. } break;
  2515. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2516. if (!completion_context.node) {
  2517. break;
  2518. }
  2519. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2520. } break;
  2521. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2522. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2523. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2524. switch (native_type.kind) {
  2525. case GDScriptParser::DataType::CLASS: {
  2526. native_type = native_type.class_type->base_type;
  2527. } break;
  2528. default: {
  2529. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2530. } break;
  2531. }
  2532. }
  2533. if (!native_type.is_set()) {
  2534. break;
  2535. }
  2536. StringName class_name = native_type.native_type;
  2537. if (!ClassDB::class_exists(class_name)) {
  2538. break;
  2539. }
  2540. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2541. List<MethodInfo> virtual_methods;
  2542. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2543. for (const MethodInfo &mi : virtual_methods) {
  2544. String method_hint = mi.name;
  2545. if (method_hint.contains(":")) {
  2546. method_hint = method_hint.get_slice(":", 0);
  2547. }
  2548. method_hint += "(";
  2549. if (mi.arguments.size()) {
  2550. for (int i = 0; i < mi.arguments.size(); i++) {
  2551. if (i > 0) {
  2552. method_hint += ", ";
  2553. }
  2554. String arg = mi.arguments[i].name;
  2555. if (arg.contains(":")) {
  2556. arg = arg.substr(0, arg.find(":"));
  2557. }
  2558. method_hint += arg;
  2559. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2560. method_hint += ": ";
  2561. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2562. method_hint += mi.arguments[i].class_name.operator String();
  2563. } else {
  2564. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2565. }
  2566. }
  2567. }
  2568. }
  2569. method_hint += ")";
  2570. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2571. method_hint += " -> ";
  2572. if (mi.return_val.type == Variant::NIL) {
  2573. method_hint += "void";
  2574. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2575. method_hint += mi.return_val.class_name.operator String();
  2576. } else {
  2577. method_hint += Variant::get_type_name(mi.return_val.type);
  2578. }
  2579. }
  2580. method_hint += ":";
  2581. ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2582. options.insert(option.display, option);
  2583. }
  2584. } break;
  2585. case GDScriptParser::COMPLETION_GET_NODE: {
  2586. // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
  2587. if (p_owner) {
  2588. List<String> opts;
  2589. p_owner->get_argument_options("get_node", 0, &opts);
  2590. for (const String &E : opts) {
  2591. r_forced = true;
  2592. String opt = E.strip_edges();
  2593. if (opt.is_quoted()) {
  2594. // Remove quotes so that we can handle user preferred quote style,
  2595. // or handle NodePaths which are valid identifiers and don't need quotes.
  2596. opt = opt.unquote();
  2597. }
  2598. // The path needs quotes if it's not a valid identifier (with an exception
  2599. // for "/" as path separator, which also doesn't require quotes).
  2600. if (!opt.replace("/", "_").is_valid_identifier()) {
  2601. opt = opt.quote(quote_style); // Handle user preference.
  2602. }
  2603. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2604. options.insert(option.display, option);
  2605. }
  2606. // Get autoloads.
  2607. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  2608. String path = "/root/" + E.key;
  2609. ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2610. options.insert(option.display, option);
  2611. }
  2612. }
  2613. } break;
  2614. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2615. if (!completion_context.current_class) {
  2616. break;
  2617. }
  2618. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2619. } break;
  2620. }
  2621. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
  2622. r_options->push_back(E.value);
  2623. }
  2624. return OK;
  2625. }
  2626. #else
  2627. 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) {
  2628. return OK;
  2629. }
  2630. #endif
  2631. //////// END COMPLETION //////////
  2632. String GDScriptLanguage::_get_indentation() const {
  2633. #ifdef TOOLS_ENABLED
  2634. if (Engine::get_singleton()->is_editor_hint()) {
  2635. bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
  2636. if (use_space_indentation) {
  2637. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  2638. String space_indent = "";
  2639. for (int i = 0; i < indent_size; i++) {
  2640. space_indent += " ";
  2641. }
  2642. return space_indent;
  2643. }
  2644. }
  2645. #endif
  2646. return "\t";
  2647. }
  2648. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2649. String indent = _get_indentation();
  2650. Vector<String> lines = p_code.split("\n");
  2651. List<int> indent_stack;
  2652. for (int i = 0; i < lines.size(); i++) {
  2653. String l = lines[i];
  2654. int tc = 0;
  2655. for (int j = 0; j < l.length(); j++) {
  2656. if (l[j] == ' ' || l[j] == '\t') {
  2657. tc++;
  2658. } else {
  2659. break;
  2660. }
  2661. }
  2662. String st = l.substr(tc, l.length()).strip_edges();
  2663. if (st.is_empty() || st.begins_with("#")) {
  2664. continue; //ignore!
  2665. }
  2666. int ilevel = 0;
  2667. if (indent_stack.size()) {
  2668. ilevel = indent_stack.back()->get();
  2669. }
  2670. if (tc > ilevel) {
  2671. indent_stack.push_back(tc);
  2672. } else if (tc < ilevel) {
  2673. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2674. indent_stack.pop_back();
  2675. }
  2676. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2677. indent_stack.push_back(tc); // this is not right but gets the job done
  2678. }
  2679. }
  2680. if (i >= p_from_line) {
  2681. l = "";
  2682. for (int j = 0; j < indent_stack.size(); j++) {
  2683. l += indent;
  2684. }
  2685. l += st;
  2686. } else if (i > p_to_line) {
  2687. break;
  2688. }
  2689. lines.write[i] = l;
  2690. }
  2691. p_code = "";
  2692. for (int i = 0; i < lines.size(); i++) {
  2693. if (i > 0) {
  2694. p_code += "\n";
  2695. }
  2696. p_code += lines[i];
  2697. }
  2698. }
  2699. #ifdef TOOLS_ENABLED
  2700. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2701. GDScriptParser::DataType base_type = p_base;
  2702. while (base_type.is_set()) {
  2703. switch (base_type.kind) {
  2704. case GDScriptParser::DataType::CLASS: {
  2705. if (base_type.class_type) {
  2706. if (base_type.class_type->has_member(p_symbol)) {
  2707. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2708. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2709. r_result.class_path = base_type.script_path;
  2710. r_result.script = GDScriptCache::get_shallow_script(r_result.class_path);
  2711. return OK;
  2712. }
  2713. base_type = base_type.class_type->base_type;
  2714. }
  2715. } break;
  2716. case GDScriptParser::DataType::SCRIPT: {
  2717. Ref<Script> scr = base_type.script_type;
  2718. if (scr.is_valid()) {
  2719. int line = scr->get_member_line(p_symbol);
  2720. if (line >= 0) {
  2721. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2722. r_result.location = line;
  2723. r_result.script = scr;
  2724. return OK;
  2725. }
  2726. Ref<Script> base_script = scr->get_base_script();
  2727. if (base_script.is_valid()) {
  2728. base_type.script_type = base_script;
  2729. } else {
  2730. base_type.kind = GDScriptParser::DataType::NATIVE;
  2731. base_type.native_type = scr->get_instance_base_type();
  2732. }
  2733. } else {
  2734. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2735. }
  2736. } break;
  2737. case GDScriptParser::DataType::NATIVE: {
  2738. StringName class_name = base_type.native_type;
  2739. if (!ClassDB::class_exists(class_name)) {
  2740. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2741. break;
  2742. }
  2743. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2744. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2745. r_result.class_name = base_type.native_type;
  2746. r_result.class_member = p_symbol;
  2747. return OK;
  2748. }
  2749. List<MethodInfo> virtual_methods;
  2750. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2751. for (const MethodInfo &E : virtual_methods) {
  2752. if (E.name == p_symbol) {
  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. }
  2759. if (ClassDB::has_signal(class_name, p_symbol, true)) {
  2760. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  2761. r_result.class_name = base_type.native_type;
  2762. r_result.class_member = p_symbol;
  2763. return OK;
  2764. }
  2765. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2766. if (enum_name != StringName()) {
  2767. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2768. r_result.class_name = base_type.native_type;
  2769. r_result.class_member = enum_name;
  2770. return OK;
  2771. }
  2772. List<String> constants;
  2773. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2774. for (const String &E : constants) {
  2775. if (E == p_symbol) {
  2776. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2777. r_result.class_name = base_type.native_type;
  2778. r_result.class_member = p_symbol;
  2779. return OK;
  2780. }
  2781. }
  2782. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2783. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2784. r_result.class_name = base_type.native_type;
  2785. r_result.class_member = p_symbol;
  2786. return OK;
  2787. }
  2788. StringName parent = ClassDB::get_parent_class(class_name);
  2789. if (parent != StringName()) {
  2790. base_type.native_type = parent;
  2791. } else {
  2792. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2793. }
  2794. } break;
  2795. case GDScriptParser::DataType::BUILTIN: {
  2796. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2797. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2798. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2799. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2800. r_result.class_member = p_symbol;
  2801. return OK;
  2802. }
  2803. Variant v;
  2804. Ref<RefCounted> v_ref;
  2805. if (base_type.builtin_type == Variant::OBJECT) {
  2806. v_ref.instantiate();
  2807. v = v_ref;
  2808. } else {
  2809. Callable::CallError err;
  2810. Variant::construct(base_type.builtin_type, v, nullptr, 0, err);
  2811. if (err.error != Callable::CallError::CALL_OK) {
  2812. break;
  2813. }
  2814. }
  2815. if (v.has_method(p_symbol)) {
  2816. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2817. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2818. r_result.class_member = p_symbol;
  2819. return OK;
  2820. }
  2821. bool valid = false;
  2822. v.get(p_symbol, &valid);
  2823. if (valid) {
  2824. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2825. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2826. r_result.class_member = p_symbol;
  2827. return OK;
  2828. }
  2829. } break;
  2830. default: {
  2831. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2832. } break;
  2833. }
  2834. }
  2835. return ERR_CANT_RESOLVE;
  2836. }
  2837. ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2838. // Before parsing, try the usual stuff.
  2839. if (ClassDB::class_exists(p_symbol)) {
  2840. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2841. r_result.class_name = p_symbol;
  2842. return OK;
  2843. }
  2844. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2845. Variant::Type t = Variant::Type(i);
  2846. if (Variant::get_type_name(t) == p_symbol) {
  2847. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2848. r_result.class_name = Variant::get_type_name(t);
  2849. return OK;
  2850. }
  2851. }
  2852. // Need special checks for assert and preload as they are technically
  2853. // keywords, so are not registered in GDScriptUtilityFunctions.
  2854. if (GDScriptUtilityFunctions::function_exists(p_symbol) || "assert" == p_symbol || "preload" == p_symbol) {
  2855. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2856. r_result.class_name = "@GDScript";
  2857. r_result.class_member = p_symbol;
  2858. return OK;
  2859. }
  2860. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2861. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2862. r_result.class_name = "@GDScript";
  2863. r_result.class_member = p_symbol;
  2864. return OK;
  2865. }
  2866. GDScriptParser parser;
  2867. parser.parse(p_code, p_path, true);
  2868. GDScriptAnalyzer analyzer(&parser);
  2869. analyzer.analyze();
  2870. GDScriptParser::CompletionContext context = parser.get_completion_context();
  2871. if (context.current_class && context.current_class->extends.size() > 0) {
  2872. bool success = false;
  2873. ClassDB::get_integer_constant(context.current_class->extends[0], p_symbol, &success);
  2874. if (success) {
  2875. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2876. r_result.class_name = context.current_class->extends[0];
  2877. r_result.class_member = p_symbol;
  2878. return OK;
  2879. }
  2880. }
  2881. bool is_function = false;
  2882. switch (context.type) {
  2883. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  2884. if (!Variant::has_builtin_method(context.builtin_type, StringName(p_symbol))) {
  2885. // A constant.
  2886. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2887. r_result.class_name = Variant::get_type_name(context.builtin_type);
  2888. r_result.class_member = p_symbol;
  2889. return OK;
  2890. }
  2891. // A method.
  2892. GDScriptParser::DataType base_type;
  2893. base_type.kind = GDScriptParser::DataType::BUILTIN;
  2894. base_type.builtin_type = context.builtin_type;
  2895. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2896. return OK;
  2897. }
  2898. } break;
  2899. case GDScriptParser::COMPLETION_SUPER_METHOD:
  2900. case GDScriptParser::COMPLETION_METHOD: {
  2901. is_function = true;
  2902. [[fallthrough]];
  2903. }
  2904. case GDScriptParser::COMPLETION_ASSIGN:
  2905. case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
  2906. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2907. GDScriptParser::DataType base_type;
  2908. if (context.current_class) {
  2909. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  2910. base_type = context.current_class->get_datatype();
  2911. } else {
  2912. base_type = context.current_class->base_type;
  2913. }
  2914. } else {
  2915. break;
  2916. }
  2917. if (!is_function && context.current_suite) {
  2918. // Lookup local variables.
  2919. const GDScriptParser::SuiteNode *suite = context.current_suite;
  2920. while (suite) {
  2921. if (suite->has_local(p_symbol)) {
  2922. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2923. r_result.location = suite->get_local(p_symbol).start_line;
  2924. return OK;
  2925. }
  2926. suite = suite->parent_block;
  2927. }
  2928. }
  2929. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2930. return OK;
  2931. }
  2932. if (!is_function) {
  2933. // Guess in autoloads as singletons.
  2934. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  2935. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  2936. if (autoload.is_singleton) {
  2937. String script = autoload.path;
  2938. if (!script.ends_with(".gd")) {
  2939. // Not a script, try find the script anyway,
  2940. // may have some success.
  2941. script = script.get_basename() + ".gd";
  2942. }
  2943. if (FileAccess::exists(script)) {
  2944. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2945. r_result.location = 0;
  2946. r_result.script = ResourceLoader::load(script);
  2947. return OK;
  2948. }
  2949. }
  2950. }
  2951. // Global.
  2952. HashMap<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2953. if (classes.has(p_symbol)) {
  2954. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2955. if (value.get_type() == Variant::OBJECT) {
  2956. Object *obj = value;
  2957. if (obj) {
  2958. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2959. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2960. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2961. } else {
  2962. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2963. r_result.class_name = obj->get_class();
  2964. }
  2965. // proxy class remove the underscore.
  2966. if (r_result.class_name.begins_with("_")) {
  2967. r_result.class_name = r_result.class_name.substr(1);
  2968. }
  2969. return OK;
  2970. }
  2971. } else {
  2972. /*
  2973. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2974. // We cannot determine the exact nature of the identifier here
  2975. // Otherwise these codes would work
  2976. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2977. if (enumName != nullptr) {
  2978. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2979. r_result.class_name = "@GlobalScope";
  2980. r_result.class_member = enumName;
  2981. return OK;
  2982. }
  2983. else {
  2984. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2985. r_result.class_name = "@GlobalScope";
  2986. r_result.class_member = p_symbol;
  2987. return OK;
  2988. }*/
  2989. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  2990. r_result.class_name = "@GlobalScope";
  2991. r_result.class_member = p_symbol;
  2992. return OK;
  2993. }
  2994. } else {
  2995. List<StringName> utility_functions;
  2996. Variant::get_utility_function_list(&utility_functions);
  2997. if (utility_functions.find(p_symbol) != nullptr) {
  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. }
  3004. }
  3005. } break;
  3006. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  3007. is_function = true;
  3008. [[fallthrough]];
  3009. }
  3010. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  3011. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  3012. break;
  3013. }
  3014. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  3015. if (!subscript->is_attribute) {
  3016. break;
  3017. }
  3018. GDScriptCompletionIdentifier base;
  3019. if (!_guess_expression_type(context, subscript->base, base)) {
  3020. break;
  3021. }
  3022. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  3023. return OK;
  3024. }
  3025. } break;
  3026. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  3027. GDScriptParser::DataType base_type = context.current_class->base_type;
  3028. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3029. return OK;
  3030. }
  3031. } break;
  3032. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  3033. case GDScriptParser::COMPLETION_TYPE_NAME: {
  3034. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  3035. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  3036. return OK;
  3037. }
  3038. } break;
  3039. case GDScriptParser::COMPLETION_ANNOTATION: {
  3040. const String annotation_symbol = "@" + p_symbol;
  3041. if (parser.annotation_exists(annotation_symbol)) {
  3042. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION;
  3043. r_result.class_name = "@GDScript";
  3044. r_result.class_member = annotation_symbol;
  3045. return OK;
  3046. }
  3047. } break;
  3048. default: {
  3049. }
  3050. }
  3051. return ERR_CANT_RESOLVE;
  3052. }
  3053. #endif