gdscript_editor.cpp 116 KB

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