gdscript_editor.cpp 109 KB

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