gdscript_editor.cpp 121 KB

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