gdscript_editor.cpp 109 KB

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