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