gdscript_editor.cpp 109 KB

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