gdscript_editor.cpp 110 KB

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