gdscript_editor.cpp 108 KB

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