gdscript_editor.cpp 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571
  1. /**************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  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 (int z = 0; z < blk->statements.size(); z++) {
  1042. const GDScriptParser::Node *expr = blk->statements[z];
  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. // Ignore groups and categories in code completion.
  1342. if (prop.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1343. continue;
  1344. }
  1345. if (prop.name == p_identifier) {
  1346. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1347. if (getter != StringName()) {
  1348. MethodBind *g = ClassDB::get_method(class_name, getter);
  1349. if (g) {
  1350. r_type = _type_from_property(g->get_return_info());
  1351. return true;
  1352. }
  1353. } else {
  1354. r_type = _type_from_property(prop);
  1355. return true;
  1356. }
  1357. break;
  1358. }
  1359. }
  1360. return false;
  1361. } break;
  1362. case GDScriptParser::DataType::BUILTIN: {
  1363. Variant::CallError err;
  1364. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1365. if (err.error != Variant::CallError::CALL_OK) {
  1366. return false;
  1367. }
  1368. bool valid = false;
  1369. Variant res = tmp.get(p_identifier, &valid);
  1370. if (valid) {
  1371. r_type = _type_from_variant(res);
  1372. r_type.value = Variant();
  1373. r_type.type.is_constant = false;
  1374. return true;
  1375. }
  1376. return false;
  1377. } break;
  1378. default: {
  1379. return false;
  1380. } break;
  1381. }
  1382. }
  1383. return false;
  1384. }
  1385. static bool _find_last_return_in_block(const GDScriptCompletionContext &p_context, int &r_last_return_line, const GDScriptParser::Node **r_last_returned_value) {
  1386. if (!p_context.block) {
  1387. return false;
  1388. }
  1389. for (int i = 0; i < p_context.block->statements.size(); i++) {
  1390. if (p_context.block->statements[i]->line < r_last_return_line) {
  1391. continue;
  1392. }
  1393. if (p_context.block->statements[i]->type != GDScriptParser::Node::TYPE_CONTROL_FLOW) {
  1394. continue;
  1395. }
  1396. const GDScriptParser::ControlFlowNode *cf = static_cast<const GDScriptParser::ControlFlowNode *>(p_context.block->statements[i]);
  1397. if (cf->cf_type == GDScriptParser::ControlFlowNode::CF_RETURN && cf->arguments.size() > 0) {
  1398. if (cf->line > r_last_return_line) {
  1399. r_last_return_line = cf->line;
  1400. *r_last_returned_value = cf->arguments[0];
  1401. }
  1402. }
  1403. }
  1404. // Recurse into subblocks
  1405. for (int i = 0; i < p_context.block->sub_blocks.size(); i++) {
  1406. GDScriptCompletionContext c = p_context;
  1407. c.block = p_context.block->sub_blocks[i];
  1408. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1409. }
  1410. return false;
  1411. }
  1412. static bool _guess_method_return_type_from_base(GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1413. GDScriptParser::DataType base_type = p_base.type;
  1414. bool _static = base_type.is_meta_type;
  1415. if (_static && p_method == "new") {
  1416. r_type.type = base_type;
  1417. r_type.type.is_meta_type = false;
  1418. r_type.type.is_constant = false;
  1419. return true;
  1420. }
  1421. while (base_type.has_type) {
  1422. switch (base_type.kind) {
  1423. case GDScriptParser::DataType::CLASS: {
  1424. if (!base_type.class_type) {
  1425. base_type.has_type = false;
  1426. break;
  1427. }
  1428. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  1429. if (base_type.class_type->static_functions[i]->name == p_method) {
  1430. int last_return_line = -1;
  1431. const GDScriptParser::Node *last_returned_value = nullptr;
  1432. GDScriptCompletionContext c = p_context;
  1433. c._class = base_type.class_type;
  1434. c.function = base_type.class_type->static_functions[i];
  1435. c.block = c.function->body;
  1436. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1437. if (last_returned_value) {
  1438. c.line = c.block->end_line;
  1439. return _guess_expression_type(c, last_returned_value, r_type);
  1440. }
  1441. }
  1442. }
  1443. if (!_static) {
  1444. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  1445. if (base_type.class_type->functions[i]->name == p_method) {
  1446. int last_return_line = -1;
  1447. const GDScriptParser::Node *last_returned_value = nullptr;
  1448. GDScriptCompletionContext c = p_context;
  1449. c._class = base_type.class_type;
  1450. c.function = base_type.class_type->functions[i];
  1451. c.block = c.function->body;
  1452. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1453. if (last_returned_value) {
  1454. c.line = c.block->end_line;
  1455. return _guess_expression_type(c, last_returned_value, r_type);
  1456. }
  1457. }
  1458. }
  1459. }
  1460. base_type = base_type.class_type->base_type;
  1461. } break;
  1462. case GDScriptParser::DataType::GDSCRIPT: {
  1463. Ref<GDScript> gds = base_type.script_type;
  1464. if (gds.is_valid()) {
  1465. if (gds->get_member_functions().has(p_method)) {
  1466. r_type = _type_from_gdtype(gds->get_member_functions()[p_method]->get_return_type());
  1467. return true;
  1468. }
  1469. Ref<GDScript> base_script = gds->get_base_script();
  1470. if (base_script.is_valid()) {
  1471. base_type.script_type = base_script;
  1472. } else {
  1473. base_type.kind = GDScriptParser::DataType::NATIVE;
  1474. base_type.native_type = gds->get_instance_base_type();
  1475. }
  1476. } else {
  1477. return false;
  1478. }
  1479. } break;
  1480. case GDScriptParser::DataType::SCRIPT: {
  1481. Ref<Script> scr = base_type.script_type;
  1482. if (scr.is_valid()) {
  1483. List<MethodInfo> methods;
  1484. scr->get_script_method_list(&methods);
  1485. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1486. MethodInfo &mi = E->get();
  1487. if (mi.name == p_method) {
  1488. r_type = _type_from_property(mi.return_val);
  1489. return true;
  1490. }
  1491. }
  1492. Ref<Script> base_script = scr->get_base_script();
  1493. if (base_script.is_valid()) {
  1494. base_type.script_type = base_script;
  1495. } else {
  1496. base_type.kind = GDScriptParser::DataType::NATIVE;
  1497. base_type.native_type = scr->get_instance_base_type();
  1498. }
  1499. } else {
  1500. return false;
  1501. }
  1502. } break;
  1503. case GDScriptParser::DataType::NATIVE: {
  1504. StringName native = base_type.native_type;
  1505. if (!ClassDB::class_exists(native)) {
  1506. native = String("_") + native;
  1507. if (!ClassDB::class_exists(native)) {
  1508. return false;
  1509. }
  1510. }
  1511. MethodBind *mb = ClassDB::get_method(native, p_method);
  1512. if (mb) {
  1513. r_type = _type_from_property(mb->get_return_info());
  1514. return true;
  1515. }
  1516. return false;
  1517. } break;
  1518. case GDScriptParser::DataType::BUILTIN: {
  1519. Variant::CallError err;
  1520. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1521. if (err.error != Variant::CallError::CALL_OK) {
  1522. return false;
  1523. }
  1524. List<MethodInfo> methods;
  1525. tmp.get_method_list(&methods);
  1526. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1527. MethodInfo &mi = E->get();
  1528. if (mi.name == p_method) {
  1529. r_type = _type_from_property(mi.return_val);
  1530. return true;
  1531. }
  1532. }
  1533. return false;
  1534. } break;
  1535. default: {
  1536. return false;
  1537. }
  1538. }
  1539. }
  1540. return false;
  1541. }
  1542. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx) {
  1543. String arghint = _get_visual_datatype(p_info.return_val, false) + " " + p_info.name + "(";
  1544. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  1545. int i = 0;
  1546. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E; E = E->next()) {
  1547. if (i > 0) {
  1548. arghint += ", ";
  1549. }
  1550. if (i == p_arg_idx) {
  1551. arghint += String::chr(0xFFFF);
  1552. }
  1553. arghint += E->get().name + ": " + _get_visual_datatype(E->get(), true);
  1554. if (i - def_args >= 0) {
  1555. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  1556. }
  1557. if (i == p_arg_idx) {
  1558. arghint += String::chr(0xFFFF);
  1559. }
  1560. i++;
  1561. }
  1562. if (p_info.flags & METHOD_FLAG_VARARG) {
  1563. if (p_info.arguments.size() > 0) {
  1564. arghint += ", ";
  1565. }
  1566. if (p_arg_idx >= p_info.arguments.size()) {
  1567. arghint += String::chr(0xFFFF);
  1568. }
  1569. arghint += "...";
  1570. if (p_arg_idx >= p_info.arguments.size()) {
  1571. arghint += String::chr(0xFFFF);
  1572. }
  1573. }
  1574. arghint += ")";
  1575. return arghint;
  1576. }
  1577. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  1578. String arghint = p_function->return_type.to_string() + " " + p_function->name.operator String() + "(";
  1579. int def_args = p_function->arguments.size() - p_function->default_values.size();
  1580. for (int i = 0; i < p_function->arguments.size(); i++) {
  1581. if (i > 0) {
  1582. arghint += ", ";
  1583. }
  1584. if (i == p_arg_idx) {
  1585. arghint += String::chr(0xFFFF);
  1586. }
  1587. arghint += p_function->arguments[i].operator String() + ": " + p_function->argument_types[i].to_string();
  1588. if (i - def_args >= 0) {
  1589. String def_val = "<unknown>";
  1590. if (p_function->default_values[i - def_args] && p_function->default_values[i - def_args]->type == GDScriptParser::Node::TYPE_OPERATOR) {
  1591. const GDScriptParser::OperatorNode *assign = static_cast<const GDScriptParser::OperatorNode *>(p_function->default_values[i - def_args]);
  1592. if (assign->arguments.size() >= 2) {
  1593. if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1594. const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(assign->arguments[1]);
  1595. def_val = cn->value.get_construct_string();
  1596. } else if (assign->arguments[1]->type == GDScriptParser::Node::TYPE_IDENTIFIER) {
  1597. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->arguments[1]);
  1598. def_val = id->name.operator String();
  1599. }
  1600. }
  1601. }
  1602. arghint += " = " + def_val;
  1603. }
  1604. if (i == p_arg_idx) {
  1605. arghint += String::chr(0xFFFF);
  1606. }
  1607. }
  1608. arghint += ")";
  1609. return arghint;
  1610. }
  1611. static void _find_enumeration_candidates(const String p_enum_hint, Map<String, ScriptCodeCompletionOption> &r_result) {
  1612. if (p_enum_hint.find(".") == -1) {
  1613. // Global constant
  1614. StringName current_enum = p_enum_hint;
  1615. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1616. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1617. ScriptCodeCompletionOption option(GlobalConstants::get_global_constant_name(i), ScriptCodeCompletionOption::KIND_ENUM);
  1618. r_result.insert(option.display, option);
  1619. }
  1620. }
  1621. } else {
  1622. String class_name = p_enum_hint.get_slice(".", 0);
  1623. String enum_name = p_enum_hint.get_slice(".", 1);
  1624. if (!ClassDB::class_exists(class_name)) {
  1625. return;
  1626. }
  1627. List<StringName> enum_constants;
  1628. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  1629. for (List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  1630. String candidate = class_name + "." + E->get();
  1631. if (candidate[0] == '_') { // Trim leading underscore.
  1632. candidate = candidate.substr(1);
  1633. }
  1634. ScriptCodeCompletionOption option(candidate, ScriptCodeCompletionOption::KIND_ENUM);
  1635. r_result.insert(option.display, option);
  1636. }
  1637. }
  1638. }
  1639. static void _find_identifiers_in_block(const GDScriptCompletionContext &p_context, Map<String, ScriptCodeCompletionOption> &r_result) {
  1640. for (Map<StringName, GDScriptParser::LocalVarNode *>::Element *E = p_context.block->variables.front(); E; E = E->next()) {
  1641. if (E->get()->line < p_context.line) {
  1642. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_VARIABLE);
  1643. r_result.insert(option.display, option);
  1644. }
  1645. }
  1646. if (p_context.block->parent_block) {
  1647. GDScriptCompletionContext c = p_context;
  1648. c.block = p_context.block->parent_block;
  1649. _find_identifiers_in_block(c, r_result);
  1650. }
  1651. }
  1652. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result);
  1653. 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) {
  1654. if (!p_parent_only) {
  1655. if (!p_static && !p_only_functions) {
  1656. for (int i = 0; i < p_context._class->variables.size(); i++) {
  1657. ScriptCodeCompletionOption option(p_context._class->variables[i].identifier, ScriptCodeCompletionOption::KIND_MEMBER);
  1658. r_result.insert(option.display, option);
  1659. }
  1660. }
  1661. if (!p_only_functions) {
  1662. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = p_context._class->constant_expressions.front(); E; E = E->next()) {
  1663. ScriptCodeCompletionOption option(E->key(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1664. if (E->get().expression && E->get().expression->type == GDScriptParser::Node::TYPE_CONSTANT) {
  1665. GDScriptParser::ConstantNode *cnode = (GDScriptParser::ConstantNode *)E->get().expression;
  1666. if (cnode) {
  1667. option.default_value = cnode->value;
  1668. }
  1669. }
  1670. r_result.insert(option.display, option);
  1671. }
  1672. for (int i = 0; i < p_context._class->subclasses.size(); i++) {
  1673. ScriptCodeCompletionOption option(p_context._class->subclasses[i]->name, ScriptCodeCompletionOption::KIND_CLASS);
  1674. r_result.insert(option.display, option);
  1675. }
  1676. }
  1677. for (int i = 0; i < p_context._class->static_functions.size(); i++) {
  1678. ScriptCodeCompletionOption option(p_context._class->static_functions[i]->name.operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1679. if (p_context._class->static_functions[i]->arguments.size()) {
  1680. option.insert_text += "(";
  1681. } else {
  1682. option.insert_text += "()";
  1683. }
  1684. r_result.insert(option.display, option);
  1685. }
  1686. if (!p_static) {
  1687. for (int i = 0; i < p_context._class->functions.size(); i++) {
  1688. ScriptCodeCompletionOption option(p_context._class->functions[i]->name.operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1689. if (p_context._class->functions[i]->arguments.size()) {
  1690. option.insert_text += "(";
  1691. } else {
  1692. option.insert_text += "()";
  1693. }
  1694. r_result.insert(option.display, option);
  1695. }
  1696. }
  1697. }
  1698. // Parents
  1699. GDScriptCompletionIdentifier base_type;
  1700. base_type.type = p_context._class->base_type;
  1701. base_type.type.is_meta_type = p_static;
  1702. base_type.value = p_context.base;
  1703. GDScriptCompletionContext c = p_context;
  1704. c.block = nullptr;
  1705. c.function = nullptr;
  1706. _find_identifiers_in_base(c, base_type, p_only_functions, r_result);
  1707. }
  1708. static void _find_identifiers_in_base(const GDScriptCompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result) {
  1709. GDScriptParser::DataType base_type = p_base.type;
  1710. bool _static = base_type.is_meta_type;
  1711. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  1712. ScriptCodeCompletionOption option("new", ScriptCodeCompletionOption::KIND_FUNCTION);
  1713. option.insert_text += "(";
  1714. r_result.insert(option.display, option);
  1715. }
  1716. while (base_type.has_type) {
  1717. switch (base_type.kind) {
  1718. case GDScriptParser::DataType::CLASS: {
  1719. GDScriptCompletionContext c = p_context;
  1720. c._class = base_type.class_type;
  1721. c.block = nullptr;
  1722. c.function = nullptr;
  1723. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1724. base_type = base_type.class_type->base_type;
  1725. } break;
  1726. case GDScriptParser::DataType::GDSCRIPT: {
  1727. Ref<GDScript> script = base_type.script_type;
  1728. if (script.is_valid()) {
  1729. if (!_static && !p_only_functions) {
  1730. if (p_context.base && p_context.base->get_script_instance()) {
  1731. List<PropertyInfo> members;
  1732. p_context.base->get_script_instance()->get_property_list(&members);
  1733. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1734. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1735. r_result.insert(option.display, option);
  1736. }
  1737. }
  1738. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  1739. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_MEMBER);
  1740. r_result.insert(option.display, option);
  1741. }
  1742. }
  1743. if (!p_only_functions) {
  1744. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  1745. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1746. r_result.insert(option.display, option);
  1747. }
  1748. }
  1749. for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1750. if (!_static || E->get()->is_static()) {
  1751. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_FUNCTION);
  1752. if (E->get()->get_argument_count()) {
  1753. option.insert_text += "(";
  1754. } else {
  1755. option.insert_text += "()";
  1756. }
  1757. r_result.insert(option.display, option);
  1758. }
  1759. }
  1760. if (!p_only_functions) {
  1761. for (const Map<StringName, Ref<GDScript>>::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1762. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1763. r_result.insert(option.display, option);
  1764. }
  1765. }
  1766. base_type = GDScriptParser::DataType();
  1767. if (script->get_base().is_valid()) {
  1768. base_type.has_type = true;
  1769. base_type.kind = GDScriptParser::DataType::GDSCRIPT;
  1770. base_type.script_type = script->get_base();
  1771. } else {
  1772. base_type.has_type = script->get_instance_base_type() != StringName();
  1773. base_type.kind = GDScriptParser::DataType::NATIVE;
  1774. base_type.native_type = script->get_instance_base_type();
  1775. }
  1776. } else {
  1777. return;
  1778. }
  1779. } break;
  1780. case GDScriptParser::DataType::SCRIPT: {
  1781. Ref<Script> scr = base_type.script_type;
  1782. if (scr.is_valid()) {
  1783. if (!_static && !p_only_functions) {
  1784. List<PropertyInfo> members;
  1785. scr->get_script_property_list(&members);
  1786. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1787. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1788. r_result.insert(option.display, option);
  1789. }
  1790. }
  1791. if (!p_only_functions) {
  1792. Map<StringName, Variant> constants;
  1793. scr->get_constants(&constants);
  1794. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  1795. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1796. r_result.insert(option.display, option);
  1797. }
  1798. }
  1799. List<MethodInfo> methods;
  1800. scr->get_script_method_list(&methods);
  1801. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1802. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1803. if (E->get().arguments.size()) {
  1804. option.insert_text += "(";
  1805. } else {
  1806. option.insert_text += "()";
  1807. }
  1808. r_result.insert(option.display, option);
  1809. }
  1810. Ref<Script> base_script = scr->get_base_script();
  1811. if (base_script.is_valid()) {
  1812. base_type.script_type = base_script;
  1813. } else {
  1814. base_type.kind = GDScriptParser::DataType::NATIVE;
  1815. base_type.native_type = scr->get_instance_base_type();
  1816. }
  1817. } else {
  1818. return;
  1819. }
  1820. } break;
  1821. case GDScriptParser::DataType::NATIVE: {
  1822. StringName type = base_type.native_type;
  1823. if (!ClassDB::class_exists(type)) {
  1824. type = String("_") + type;
  1825. if (!ClassDB::class_exists(type)) {
  1826. return;
  1827. }
  1828. }
  1829. if (!p_only_functions) {
  1830. List<String> constants;
  1831. ClassDB::get_integer_constant_list(type, &constants);
  1832. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1833. ScriptCodeCompletionOption option(E->get(), ScriptCodeCompletionOption::KIND_CONSTANT);
  1834. r_result.insert(option.display, option);
  1835. }
  1836. if (!_static) {
  1837. List<PropertyInfo> pinfo;
  1838. ClassDB::get_property_list(type, &pinfo);
  1839. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1840. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1841. continue;
  1842. }
  1843. if (E->get().name.find("/") != -1) {
  1844. continue;
  1845. }
  1846. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1847. r_result.insert(option.display, option);
  1848. }
  1849. }
  1850. }
  1851. if (!_static) {
  1852. List<MethodInfo> methods;
  1853. bool is_autocompleting_getters = GLOBAL_GET("debug/gdscript/completion/autocomplete_setters_and_getters").booleanize();
  1854. ClassDB::get_method_list(type, &methods, false, !is_autocompleting_getters);
  1855. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1856. if (E->get().name.begins_with("_")) {
  1857. continue;
  1858. }
  1859. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1860. if (E->get().arguments.size()) {
  1861. option.insert_text += "(";
  1862. } else {
  1863. option.insert_text += "()";
  1864. }
  1865. r_result.insert(option.display, option);
  1866. }
  1867. }
  1868. return;
  1869. } break;
  1870. case GDScriptParser::DataType::BUILTIN: {
  1871. Variant::CallError err;
  1872. Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  1873. if (err.error != Variant::CallError::CALL_OK) {
  1874. return;
  1875. }
  1876. if (!p_only_functions) {
  1877. List<PropertyInfo> members;
  1878. if (p_base.value.get_type() != Variant::NIL) {
  1879. p_base.value.get_property_list(&members);
  1880. } else {
  1881. tmp.get_property_list(&members);
  1882. }
  1883. for (List<PropertyInfo>::Element *E = members.front(); E; E = E->next()) {
  1884. if (String(E->get().name).find("/") == -1) {
  1885. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_MEMBER);
  1886. r_result.insert(option.display, option);
  1887. }
  1888. }
  1889. }
  1890. List<MethodInfo> methods;
  1891. tmp.get_method_list(&methods);
  1892. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1893. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_FUNCTION);
  1894. if (E->get().arguments.size()) {
  1895. option.insert_text += "(";
  1896. } else {
  1897. option.insert_text += "()";
  1898. }
  1899. r_result.insert(option.display, option);
  1900. }
  1901. return;
  1902. } break;
  1903. default: {
  1904. return;
  1905. } break;
  1906. }
  1907. }
  1908. }
  1909. static void _find_identifiers(const GDScriptCompletionContext &p_context, bool p_only_functions, Map<String, ScriptCodeCompletionOption> &r_result) {
  1910. const GDScriptParser::BlockNode *block = p_context.block;
  1911. if (p_context.function) {
  1912. const GDScriptParser::FunctionNode *f = p_context.function;
  1913. for (int i = 0; i < f->arguments.size(); i++) {
  1914. ScriptCodeCompletionOption option(f->arguments[i].operator String(), ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  1915. r_result.insert(option.display, option);
  1916. }
  1917. }
  1918. if (!p_only_functions && block) {
  1919. GDScriptCompletionContext c = p_context;
  1920. c.block = block;
  1921. _find_identifiers_in_block(c, r_result);
  1922. }
  1923. const GDScriptParser::ClassNode *clss = p_context._class;
  1924. bool _static = p_context.function && p_context.function->_static;
  1925. while (clss) {
  1926. GDScriptCompletionContext c = p_context;
  1927. c._class = clss;
  1928. c.block = nullptr;
  1929. c.function = nullptr;
  1930. _find_identifiers_in_class(c, _static, p_only_functions, false, r_result);
  1931. _static = true;
  1932. clss = clss->owner;
  1933. }
  1934. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  1935. MethodInfo mi = GDScriptFunctions::get_info(GDScriptFunctions::Function(i));
  1936. ScriptCodeCompletionOption option(String(GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i))), ScriptCodeCompletionOption::KIND_FUNCTION);
  1937. if (mi.arguments.size() || (mi.flags & METHOD_FLAG_VARARG)) {
  1938. option.insert_text += "(";
  1939. } else {
  1940. option.insert_text += "()";
  1941. }
  1942. r_result.insert(option.display, option);
  1943. }
  1944. static const char *_type_names[Variant::VARIANT_MAX] = {
  1945. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1946. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1947. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1948. };
  1949. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1950. ScriptCodeCompletionOption option(_type_names[i], ScriptCodeCompletionOption::KIND_CLASS);
  1951. r_result.insert(option.display, option);
  1952. }
  1953. static const char *_keywords[] = {
  1954. "and", "in", "not", "or", "false", "PI", "TAU", "INF", "NAN", "self", "true", "as", "assert",
  1955. "breakpoint", "class", "class_name", "extends", "is", "func", "preload", "setget", "signal", "tool", "yield",
  1956. "const", "enum", "export", "onready", "static", "var", "break", "continue", "if", "elif",
  1957. "else", "for", "pass", "return", "match", "while", "remote", "sync", "master", "puppet", "slave",
  1958. "remotesync", "mastersync", "puppetsync",
  1959. nullptr
  1960. };
  1961. const char **kw = _keywords;
  1962. while (*kw) {
  1963. ScriptCodeCompletionOption option(*kw, ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  1964. r_result.insert(option.display, option);
  1965. kw++;
  1966. }
  1967. // Autoload singletons
  1968. List<PropertyInfo> props;
  1969. ProjectSettings::get_singleton()->get_property_list(&props);
  1970. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1971. String s = E->get().name;
  1972. if (!s.begins_with("autoload/")) {
  1973. continue;
  1974. }
  1975. String path = ProjectSettings::get_singleton()->get(s);
  1976. if (path.begins_with("*")) {
  1977. ScriptCodeCompletionOption option(s.get_slice("/", 1), ScriptCodeCompletionOption::KIND_CONSTANT);
  1978. r_result.insert(option.display, option);
  1979. }
  1980. }
  1981. // Named scripts
  1982. List<StringName> named_scripts;
  1983. ScriptServer::get_global_class_list(&named_scripts);
  1984. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  1985. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1986. r_result.insert(option.display, option);
  1987. }
  1988. // Native classes
  1989. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1990. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  1991. r_result.insert(option.display, option);
  1992. }
  1993. }
  1994. 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) {
  1995. Variant base = p_base.value;
  1996. GDScriptParser::DataType base_type = p_base.type;
  1997. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  1998. #define IS_METHOD_SIGNAL(m_method) (m_method == "connect" || m_method == "disconnect" || m_method == "is_connected" || m_method == "emit_signal")
  1999. while (base_type.has_type) {
  2000. switch (base_type.kind) {
  2001. case GDScriptParser::DataType::CLASS: {
  2002. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  2003. if (base_type.class_type->static_functions[i]->name == p_method) {
  2004. r_arghint = _make_arguments_hint(base_type.class_type->static_functions[i], p_argidx);
  2005. return;
  2006. }
  2007. }
  2008. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  2009. if (base_type.class_type->functions[i]->name == p_method) {
  2010. r_arghint = _make_arguments_hint(base_type.class_type->functions[i], p_argidx);
  2011. return;
  2012. }
  2013. }
  2014. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2015. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  2016. ScriptCodeCompletionOption option(base_type.class_type->_signals[i].name.operator String(), ScriptCodeCompletionOption::KIND_SIGNAL);
  2017. option.insert_text = quote_style + option.display + quote_style;
  2018. r_result.insert(option.display, option);
  2019. }
  2020. }
  2021. base_type = base_type.class_type->base_type;
  2022. } break;
  2023. case GDScriptParser::DataType::GDSCRIPT: {
  2024. Ref<GDScript> gds = base_type.script_type;
  2025. if (gds.is_valid()) {
  2026. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2027. List<MethodInfo> signals;
  2028. gds->get_script_signal_list(&signals);
  2029. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2030. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  2031. option.insert_text = quote_style + option.display + quote_style;
  2032. r_result.insert(option.display, option);
  2033. }
  2034. }
  2035. Ref<GDScript> base_script = gds->get_base_script();
  2036. if (base_script.is_valid()) {
  2037. base_type.script_type = base_script;
  2038. } else {
  2039. base_type.kind = GDScriptParser::DataType::NATIVE;
  2040. base_type.native_type = gds->get_instance_base_type();
  2041. }
  2042. } else {
  2043. return;
  2044. }
  2045. } break;
  2046. case GDScriptParser::DataType::NATIVE: {
  2047. StringName class_name = base_type.native_type;
  2048. if (!ClassDB::class_exists(class_name)) {
  2049. class_name = String("_") + class_name;
  2050. if (!ClassDB::class_exists(class_name)) {
  2051. base_type.has_type = false;
  2052. break;
  2053. }
  2054. }
  2055. List<MethodInfo> methods;
  2056. ClassDB::get_method_list(class_name, &methods);
  2057. ClassDB::get_virtual_methods(class_name, &methods);
  2058. int method_args = 0;
  2059. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2060. if (E->get().name == p_method) {
  2061. method_args = E->get().arguments.size();
  2062. if (base.get_type() == Variant::OBJECT) {
  2063. Object *obj = base.operator Object *();
  2064. if (obj) {
  2065. List<String> options;
  2066. obj->get_argument_options(p_method, p_argidx, &options);
  2067. for (List<String>::Element *F = options.front(); F; F = F->next()) {
  2068. ScriptCodeCompletionOption option(F->get(), ScriptCodeCompletionOption::KIND_FUNCTION);
  2069. r_result.insert(option.display, option);
  2070. }
  2071. }
  2072. }
  2073. if (p_argidx < method_args) {
  2074. PropertyInfo arg_info = E->get().arguments[p_argidx];
  2075. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2076. _find_enumeration_candidates(arg_info.class_name, r_result);
  2077. }
  2078. }
  2079. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2080. break;
  2081. }
  2082. }
  2083. if (IS_METHOD_SIGNAL(p_method) && p_argidx == 0) {
  2084. List<MethodInfo> signals;
  2085. ClassDB::get_signal_list(class_name, &signals);
  2086. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2087. ScriptCodeCompletionOption option(E->get().name, ScriptCodeCompletionOption::KIND_SIGNAL);
  2088. option.insert_text = quote_style + option.display + quote_style;
  2089. r_result.insert(option.display, option);
  2090. }
  2091. }
  2092. #undef IS_METHOD_SIGNAL
  2093. if (ClassDB::is_parent_class(class_name, "Node") && (p_method == "get_node" || p_method == "has_node") && p_argidx == 0) {
  2094. // Get autoloads
  2095. List<PropertyInfo> props;
  2096. ProjectSettings::get_singleton()->get_property_list(&props);
  2097. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2098. String s = E->get().name;
  2099. if (!s.begins_with("autoload/")) {
  2100. continue;
  2101. }
  2102. String name = s.get_slice("/", 1);
  2103. ScriptCodeCompletionOption option("/root/" + name, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2104. option.insert_text = quote_style + option.display + quote_style;
  2105. r_result.insert(option.display, option);
  2106. }
  2107. }
  2108. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, "InputEvent") && p_method.operator String().find("action") != -1) {
  2109. // Get input actions
  2110. List<PropertyInfo> props;
  2111. ProjectSettings::get_singleton()->get_property_list(&props);
  2112. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2113. String s = E->get().name;
  2114. if (!s.begins_with("input/")) {
  2115. continue;
  2116. }
  2117. String name = s.get_slice("/", 1);
  2118. ScriptCodeCompletionOption option(name, ScriptCodeCompletionOption::KIND_CONSTANT);
  2119. option.insert_text = quote_style + option.display + quote_style;
  2120. r_result.insert(option.display, option);
  2121. }
  2122. }
  2123. base_type.has_type = false;
  2124. } break;
  2125. case GDScriptParser::DataType::BUILTIN: {
  2126. if (base.get_type() == Variant::NIL) {
  2127. Variant::CallError err;
  2128. base = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  2129. if (err.error != Variant::CallError::CALL_OK) {
  2130. return;
  2131. }
  2132. }
  2133. List<MethodInfo> methods;
  2134. base.get_method_list(&methods);
  2135. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2136. if (E->get().name == p_method) {
  2137. r_arghint = _make_arguments_hint(E->get(), p_argidx);
  2138. return;
  2139. }
  2140. }
  2141. base_type.has_type = false;
  2142. } break;
  2143. default: {
  2144. base_type.has_type = false;
  2145. } break;
  2146. }
  2147. }
  2148. }
  2149. 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) {
  2150. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2151. if (!p_node || p_node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2152. return;
  2153. }
  2154. Variant base;
  2155. GDScriptParser::DataType base_type;
  2156. StringName function;
  2157. bool _static = false;
  2158. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(p_node);
  2159. GDScriptCompletionIdentifier connect_base;
  2160. if (op->op != GDScriptParser::OperatorNode::OP_CALL && op->op != GDScriptParser::OperatorNode::OP_PARENT_CALL) {
  2161. return;
  2162. }
  2163. if (!op->arguments.size()) {
  2164. return;
  2165. }
  2166. if (op->op == GDScriptParser::OperatorNode::OP_CALL) {
  2167. if (op->arguments[0]->type == GDScriptParser::Node::TYPE_BUILT_IN_FUNCTION) {
  2168. // Complete built-in function
  2169. const GDScriptParser::BuiltInFunctionNode *fn = static_cast<const GDScriptParser::BuiltInFunctionNode *>(op->arguments[0]);
  2170. MethodInfo mi = GDScriptFunctions::get_info(fn->function);
  2171. if ((mi.name == "load" || mi.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2172. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2173. }
  2174. r_arghint = _make_arguments_hint(mi, p_argidx);
  2175. return;
  2176. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_TYPE) {
  2177. // Complete constructor
  2178. const GDScriptParser::TypeNode *tn = static_cast<const GDScriptParser::TypeNode *>(op->arguments[0]);
  2179. List<MethodInfo> constructors;
  2180. Variant::get_constructor_list(tn->vtype, &constructors);
  2181. int i = 0;
  2182. for (List<MethodInfo>::Element *E = constructors.front(); E; E = E->next()) {
  2183. if (p_argidx >= E->get().arguments.size()) {
  2184. continue;
  2185. }
  2186. if (i > 0) {
  2187. r_arghint += "\n";
  2188. }
  2189. r_arghint += _make_arguments_hint(E->get(), p_argidx);
  2190. i++;
  2191. }
  2192. return;
  2193. } else if (op->arguments[0]->type == GDScriptParser::Node::TYPE_SELF) {
  2194. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2195. return;
  2196. }
  2197. base = p_context.base;
  2198. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2199. function = id->name;
  2200. base_type.has_type = true;
  2201. base_type.kind = GDScriptParser::DataType::CLASS;
  2202. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2203. _static = p_context.function && p_context.function->_static;
  2204. if (function == "connect" && op->arguments.size() >= 4) {
  2205. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2206. }
  2207. } else {
  2208. if (op->arguments.size() < 2 || op->arguments[1]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2209. return;
  2210. }
  2211. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[1]);
  2212. function = id->name;
  2213. GDScriptCompletionIdentifier ci;
  2214. if (_guess_expression_type(p_context, op->arguments[0], ci)) {
  2215. base_type = ci.type;
  2216. base = ci.value;
  2217. } else {
  2218. return;
  2219. }
  2220. _static = ci.type.is_meta_type;
  2221. if (function == "connect" && op->arguments.size() >= 4) {
  2222. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2223. }
  2224. }
  2225. } else {
  2226. if (!p_context._class || op->arguments.size() < 1 || op->arguments[0]->type != GDScriptParser::Node::TYPE_IDENTIFIER) {
  2227. return;
  2228. }
  2229. base_type.has_type = true;
  2230. base_type.kind = GDScriptParser::DataType::CLASS;
  2231. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(p_context._class);
  2232. base_type.is_meta_type = p_context.function && p_context.function->_static;
  2233. base = p_context.base;
  2234. function = static_cast<const GDScriptParser::IdentifierNode *>(op->arguments[0])->name;
  2235. if (function == "connect" && op->arguments.size() >= 4) {
  2236. _guess_expression_type(p_context, op->arguments[3], connect_base);
  2237. }
  2238. }
  2239. GDScriptCompletionIdentifier ci;
  2240. ci.type = base_type;
  2241. ci.value = base;
  2242. _find_call_arguments(p_context, ci, function, p_argidx, _static, r_result, r_arghint);
  2243. if (function == "connect" && p_argidx == 2) {
  2244. Map<String, ScriptCodeCompletionOption> methods;
  2245. _find_identifiers_in_base(p_context, connect_base, true, methods);
  2246. for (Map<String, ScriptCodeCompletionOption>::Element *E = methods.front(); E; E = E->next()) {
  2247. ScriptCodeCompletionOption &option = E->value();
  2248. option.insert_text = quote_style + option.display + quote_style;
  2249. r_result.insert(option.display, option);
  2250. }
  2251. }
  2252. r_forced = r_result.size() > 0;
  2253. }
  2254. 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) {
  2255. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2256. GDScriptParser parser;
  2257. parser.parse(p_code, p_path.get_base_dir(), false, p_path, true);
  2258. r_forced = false;
  2259. Map<String, ScriptCodeCompletionOption> options;
  2260. GDScriptCompletionContext context;
  2261. context._class = parser.get_completion_class();
  2262. context.block = parser.get_completion_block();
  2263. context.function = parser.get_completion_function();
  2264. context.line = parser.get_completion_line();
  2265. if (!context._class || context._class->owner == nullptr) {
  2266. context.base = p_owner;
  2267. context.base_path = p_path.get_base_dir();
  2268. }
  2269. bool is_function = false;
  2270. switch (parser.get_completion_type()) {
  2271. case GDScriptParser::COMPLETION_NONE: {
  2272. } break;
  2273. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2274. List<StringName> constants;
  2275. Variant::get_constants_for_type(parser.get_completion_built_in_constant(), &constants);
  2276. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  2277. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CONSTANT);
  2278. bool valid = false;
  2279. Variant default_value = Variant::get_constant_value(parser.get_completion_built_in_constant(), E->get(), &valid);
  2280. if (valid) {
  2281. option.default_value = default_value;
  2282. }
  2283. options.insert(option.display, option);
  2284. }
  2285. } break;
  2286. case GDScriptParser::COMPLETION_PARENT_FUNCTION: {
  2287. _find_identifiers_in_class(context, !context.function || context.function->_static, true, true, options);
  2288. } break;
  2289. case GDScriptParser::COMPLETION_FUNCTION: {
  2290. is_function = true;
  2291. FALLTHROUGH;
  2292. }
  2293. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2294. _find_identifiers(context, is_function, options);
  2295. } break;
  2296. case GDScriptParser::COMPLETION_EXTENDS: {
  2297. // Native classes.
  2298. List<StringName> class_list;
  2299. ClassDB::get_class_list(&class_list);
  2300. for (int i = 0; i < class_list.size(); i++) {
  2301. ScriptCodeCompletionOption option(class_list[i], ScriptCodeCompletionOption::KIND_CLASS);
  2302. options.insert(option.display, option);
  2303. }
  2304. // GDScript classes.
  2305. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2306. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2307. if (ScriptServer::get_language(i)->get_name() == "GDScript") {
  2308. List<String> extensions;
  2309. ScriptServer::get_language(i)->get_recognized_extensions(&extensions);
  2310. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  2311. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options, String("." + E->get()));
  2312. }
  2313. }
  2314. }
  2315. r_forced = true;
  2316. }
  2317. // Named Scripts.
  2318. List<StringName> named_scripts;
  2319. ScriptServer::get_global_class_list(&named_scripts);
  2320. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  2321. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2322. options.insert(option.display, option);
  2323. }
  2324. } break;
  2325. case GDScriptParser::COMPLETION_GET_NODE: {
  2326. if (p_owner) {
  2327. List<String> opts;
  2328. p_owner->get_argument_options("get_node", 0, &opts);
  2329. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  2330. String opt = E->get().strip_edges();
  2331. if (opt.is_quoted()) {
  2332. r_forced = true;
  2333. String idopt = opt.unquote();
  2334. if (idopt.replace("/", "_").is_valid_identifier()) {
  2335. ScriptCodeCompletionOption option(idopt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2336. options.insert(option.display, option);
  2337. } else {
  2338. ScriptCodeCompletionOption option(opt, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2339. options.insert(option.display, option);
  2340. }
  2341. }
  2342. }
  2343. // Get autoloads
  2344. List<PropertyInfo> props;
  2345. ProjectSettings::get_singleton()->get_property_list(&props);
  2346. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2347. String s = E->get().name;
  2348. if (!s.begins_with("autoload/")) {
  2349. continue;
  2350. }
  2351. String name = s.get_slice("/", 1);
  2352. ScriptCodeCompletionOption option(quote_style + "/root/" + name + quote_style, ScriptCodeCompletionOption::KIND_NODE_PATH);
  2353. options.insert(option.display, option);
  2354. }
  2355. }
  2356. } break;
  2357. case GDScriptParser::COMPLETION_METHOD: {
  2358. is_function = true;
  2359. FALLTHROUGH;
  2360. }
  2361. case GDScriptParser::COMPLETION_INDEX: {
  2362. const GDScriptParser::Node *node = parser.get_completion_node();
  2363. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  2364. break;
  2365. }
  2366. const GDScriptParser::OperatorNode *op = static_cast<const GDScriptParser::OperatorNode *>(node);
  2367. if (op->arguments.size() < 1) {
  2368. break;
  2369. }
  2370. GDScriptCompletionIdentifier base;
  2371. if (!_guess_expression_type(context, op->arguments[0], base)) {
  2372. break;
  2373. }
  2374. GDScriptCompletionContext c = context;
  2375. c.function = nullptr;
  2376. c.block = nullptr;
  2377. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2378. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2379. c._class = base.type.class_type;
  2380. } else {
  2381. c._class = nullptr;
  2382. }
  2383. _find_identifiers_in_base(c, base, is_function, options);
  2384. } break;
  2385. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2386. _find_call_arguments(context, parser.get_completion_node(), parser.get_completion_argument_index(), options, r_forced, r_call_hint);
  2387. } break;
  2388. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  2389. GDScriptParser::DataType native_type = context._class->base_type;
  2390. while (native_type.has_type && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2391. switch (native_type.kind) {
  2392. case GDScriptParser::DataType::CLASS: {
  2393. native_type = native_type.class_type->base_type;
  2394. } break;
  2395. case GDScriptParser::DataType::GDSCRIPT: {
  2396. Ref<GDScript> gds = native_type.script_type;
  2397. if (gds.is_valid()) {
  2398. Ref<GDScript> base = gds->get_base_script();
  2399. if (base.is_valid()) {
  2400. native_type.script_type = base;
  2401. } else {
  2402. native_type.native_type = gds->get_instance_base_type();
  2403. native_type.kind = GDScriptParser::DataType::NATIVE;
  2404. }
  2405. } else {
  2406. native_type.has_type = false;
  2407. }
  2408. } break;
  2409. default: {
  2410. native_type.has_type = false;
  2411. } break;
  2412. }
  2413. }
  2414. if (!native_type.has_type) {
  2415. break;
  2416. }
  2417. StringName class_name = native_type.native_type;
  2418. if (!ClassDB::class_exists(class_name)) {
  2419. class_name = String("_") + class_name;
  2420. if (!ClassDB::class_exists(class_name)) {
  2421. break;
  2422. }
  2423. }
  2424. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2425. List<MethodInfo> virtual_methods;
  2426. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2427. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2428. MethodInfo &mi = E->get();
  2429. String method_hint = mi.name;
  2430. if (method_hint.find(":") != -1) {
  2431. method_hint = method_hint.get_slice(":", 0);
  2432. }
  2433. method_hint += "(";
  2434. if (mi.arguments.size()) {
  2435. for (int i = 0; i < mi.arguments.size(); i++) {
  2436. if (i > 0) {
  2437. method_hint += ", ";
  2438. }
  2439. String arg = mi.arguments[i].name;
  2440. if (arg.find(":") != -1) {
  2441. arg = arg.substr(0, arg.find(":"));
  2442. }
  2443. method_hint += arg;
  2444. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2445. method_hint += ": ";
  2446. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2447. method_hint += mi.arguments[i].class_name.operator String();
  2448. } else {
  2449. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2450. }
  2451. }
  2452. }
  2453. }
  2454. method_hint += ")";
  2455. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2456. method_hint += " -> ";
  2457. if (mi.return_val.type == Variant::NIL) {
  2458. method_hint += "void";
  2459. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2460. method_hint += mi.return_val.class_name.operator String();
  2461. } else {
  2462. method_hint += Variant::get_type_name(mi.return_val.type);
  2463. }
  2464. }
  2465. method_hint += ":";
  2466. ScriptCodeCompletionOption option(method_hint, ScriptCodeCompletionOption::KIND_FUNCTION);
  2467. options.insert(option.display, option);
  2468. }
  2469. } break;
  2470. case GDScriptParser::COMPLETION_YIELD: {
  2471. const GDScriptParser::Node *node = parser.get_completion_node();
  2472. GDScriptCompletionContext c = context;
  2473. c.line = node->line;
  2474. GDScriptCompletionIdentifier type;
  2475. if (!_guess_expression_type(c, node, type)) {
  2476. break;
  2477. }
  2478. GDScriptParser::DataType base_type = type.type;
  2479. while (base_type.has_type) {
  2480. switch (base_type.kind) {
  2481. case GDScriptParser::DataType::CLASS: {
  2482. for (int i = 0; i < base_type.class_type->_signals.size(); i++) {
  2483. ScriptCodeCompletionOption option(base_type.class_type->_signals[i].name.operator String(), ScriptCodeCompletionOption::KIND_SIGNAL);
  2484. option.insert_text = quote_style + option.display + quote_style;
  2485. options.insert(option.display, option);
  2486. }
  2487. base_type = base_type.class_type->base_type;
  2488. } break;
  2489. case GDScriptParser::DataType::SCRIPT:
  2490. case GDScriptParser::DataType::GDSCRIPT: {
  2491. Ref<Script> scr = base_type.script_type;
  2492. if (scr.is_valid()) {
  2493. List<MethodInfo> signals;
  2494. scr->get_script_signal_list(&signals);
  2495. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2496. ScriptCodeCompletionOption option(quote_style + E->get().name + quote_style, ScriptCodeCompletionOption::KIND_SIGNAL);
  2497. options.insert(option.display, option);
  2498. }
  2499. Ref<Script> base_script = scr->get_base_script();
  2500. if (base_script.is_valid()) {
  2501. base_type.script_type = base_script;
  2502. } else {
  2503. base_type.kind = GDScriptParser::DataType::NATIVE;
  2504. base_type.native_type = scr->get_instance_base_type();
  2505. }
  2506. } else {
  2507. base_type.has_type = false;
  2508. }
  2509. } break;
  2510. case GDScriptParser::DataType::NATIVE: {
  2511. base_type.has_type = false;
  2512. StringName class_name = base_type.native_type;
  2513. if (!ClassDB::class_exists(class_name)) {
  2514. class_name = String("_") + class_name;
  2515. if (!ClassDB::class_exists(class_name)) {
  2516. break;
  2517. }
  2518. }
  2519. List<MethodInfo> signals;
  2520. ClassDB::get_signal_list(class_name, &signals);
  2521. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2522. ScriptCodeCompletionOption option(quote_style + E->get().name + quote_style, ScriptCodeCompletionOption::KIND_SIGNAL);
  2523. options.insert(option.display, option);
  2524. }
  2525. } break;
  2526. default: {
  2527. base_type.has_type = false;
  2528. }
  2529. }
  2530. }
  2531. } break;
  2532. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2533. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2534. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2535. r_forced = true;
  2536. }
  2537. } break;
  2538. case GDScriptParser::COMPLETION_ASSIGN: {
  2539. GDScriptCompletionIdentifier type;
  2540. if (!_guess_expression_type(context, parser.get_completion_node(), type)) {
  2541. break;
  2542. }
  2543. if (!type.enumeration.empty()) {
  2544. _find_enumeration_candidates(type.enumeration, options);
  2545. r_forced = options.size() > 0;
  2546. }
  2547. } break;
  2548. case GDScriptParser::COMPLETION_TYPE_HINT: {
  2549. const GDScriptParser::ClassNode *clss = context._class;
  2550. while (clss) {
  2551. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = clss->constant_expressions.front(); E; E = E->next()) {
  2552. GDScriptCompletionIdentifier constant;
  2553. GDScriptCompletionContext c = context;
  2554. c.function = nullptr;
  2555. c.block = nullptr;
  2556. c.line = E->value().expression->line;
  2557. if (_guess_expression_type(c, E->value().expression, constant)) {
  2558. if (constant.type.has_type && constant.type.is_meta_type) {
  2559. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2560. options.insert(option.display, option);
  2561. }
  2562. }
  2563. }
  2564. for (int i = 0; i < clss->subclasses.size(); i++) {
  2565. if (clss->subclasses[i]->name != StringName()) {
  2566. ScriptCodeCompletionOption option(clss->subclasses[i]->name.operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2567. options.insert(option.display, option);
  2568. }
  2569. }
  2570. clss = clss->owner;
  2571. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2572. ScriptCodeCompletionOption option(Variant::get_type_name((Variant::Type)i), ScriptCodeCompletionOption::KIND_CLASS);
  2573. options.insert(option.display, option);
  2574. }
  2575. List<PropertyInfo> props;
  2576. ProjectSettings::get_singleton()->get_property_list(&props);
  2577. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2578. String s = E->get().name;
  2579. if (!s.begins_with("autoload/")) {
  2580. continue;
  2581. }
  2582. ScriptCodeCompletionOption option(s.get_slice("/", 1), ScriptCodeCompletionOption::KIND_CLASS);
  2583. options.insert(option.display, option);
  2584. }
  2585. }
  2586. List<StringName> native_classes;
  2587. ClassDB::get_class_list(&native_classes);
  2588. for (List<StringName>::Element *E = native_classes.front(); E; E = E->next()) {
  2589. String class_name = E->get().operator String();
  2590. if (class_name.begins_with("_")) {
  2591. class_name = class_name.right(1);
  2592. }
  2593. if (Engine::get_singleton()->has_singleton(class_name)) {
  2594. continue;
  2595. }
  2596. ScriptCodeCompletionOption option(class_name, ScriptCodeCompletionOption::KIND_CLASS);
  2597. options.insert(option.display, option);
  2598. }
  2599. // Named scripts
  2600. List<StringName> named_scripts;
  2601. ScriptServer::get_global_class_list(&named_scripts);
  2602. for (List<StringName>::Element *E = named_scripts.front(); E; E = E->next()) {
  2603. ScriptCodeCompletionOption option(E->get().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2604. options.insert(option.display, option);
  2605. }
  2606. if (parser.get_completion_identifier_is_function()) {
  2607. ScriptCodeCompletionOption option("void", ScriptCodeCompletionOption::KIND_PLAIN_TEXT);
  2608. options.insert(option.display, option);
  2609. }
  2610. r_forced = true;
  2611. } break;
  2612. case GDScriptParser::COMPLETION_TYPE_HINT_INDEX: {
  2613. GDScriptCompletionIdentifier base;
  2614. String index = parser.get_completion_cursor().operator String();
  2615. if (!_guess_identifier_type(context, index.get_slice(".", 0), base)) {
  2616. break;
  2617. }
  2618. GDScriptCompletionContext c = context;
  2619. c._class = nullptr;
  2620. c.function = nullptr;
  2621. c.block = nullptr;
  2622. bool finding = true;
  2623. index = index.right(index.find(".") + 1);
  2624. while (index.find(".") != -1) {
  2625. String id = index.get_slice(".", 0);
  2626. GDScriptCompletionIdentifier sub_base;
  2627. if (!_guess_identifier_type_from_base(c, base, id, sub_base)) {
  2628. finding = false;
  2629. break;
  2630. }
  2631. index = index.right(index.find(".") + 1);
  2632. base = sub_base;
  2633. }
  2634. if (!finding) {
  2635. break;
  2636. }
  2637. GDScriptParser::DataType base_type = base.type;
  2638. while (base_type.has_type) {
  2639. switch (base_type.kind) {
  2640. case GDScriptParser::DataType::CLASS: {
  2641. if (base_type.class_type) {
  2642. for (Map<StringName, GDScriptParser::ClassNode::Constant>::Element *E = base_type.class_type->constant_expressions.front(); E; E = E->next()) {
  2643. GDScriptCompletionIdentifier constant;
  2644. GDScriptCompletionContext c2 = context;
  2645. c2._class = base_type.class_type;
  2646. c2.function = nullptr;
  2647. c2.block = nullptr;
  2648. c2.line = E->value().expression->line;
  2649. if (_guess_expression_type(c2, E->value().expression, constant)) {
  2650. if (constant.type.has_type && constant.type.is_meta_type) {
  2651. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2652. options.insert(option.display, option);
  2653. }
  2654. }
  2655. }
  2656. for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
  2657. if (base_type.class_type->subclasses[i]->name != StringName()) {
  2658. ScriptCodeCompletionOption option(base_type.class_type->subclasses[i]->name.operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2659. options.insert(option.display, option);
  2660. }
  2661. }
  2662. base_type = base_type.class_type->base_type;
  2663. } else {
  2664. base_type.has_type = false;
  2665. }
  2666. } break;
  2667. case GDScriptParser::DataType::GDSCRIPT: {
  2668. Ref<GDScript> scr = base_type.script_type;
  2669. if (scr.is_valid()) {
  2670. for (const Map<StringName, Ref<GDScript>>::Element *E = scr->get_subclasses().front(); E; E = E->next()) {
  2671. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2672. options.insert(option.display, option);
  2673. }
  2674. }
  2675. FALLTHROUGH;
  2676. }
  2677. case GDScriptParser::DataType::SCRIPT: {
  2678. Ref<Script> scr = base_type.script_type;
  2679. if (scr.is_valid()) {
  2680. Map<StringName, Variant> constants;
  2681. scr->get_constants(&constants);
  2682. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  2683. Ref<Script> const_scr = E->value();
  2684. if (const_scr.is_valid()) {
  2685. ScriptCodeCompletionOption option(E->key().operator String(), ScriptCodeCompletionOption::KIND_CLASS);
  2686. options.insert(option.display, option);
  2687. }
  2688. }
  2689. Ref<Script> base_script = scr->get_base_script();
  2690. if (base_script.is_valid()) {
  2691. base_type.script_type = base_script;
  2692. } else {
  2693. base_type.has_type = false;
  2694. }
  2695. } else {
  2696. base_type.has_type = false;
  2697. }
  2698. } break;
  2699. default: {
  2700. base_type.has_type = false;
  2701. } break;
  2702. }
  2703. }
  2704. r_forced = options.size() > 0;
  2705. } break;
  2706. }
  2707. for (Map<String, ScriptCodeCompletionOption>::Element *E = options.front(); E; E = E->next()) {
  2708. r_options->push_back(E->get());
  2709. }
  2710. return OK;
  2711. }
  2712. #else
  2713. 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) {
  2714. return OK;
  2715. }
  2716. #endif
  2717. //////// END COMPLETION //////////
  2718. String GDScriptLanguage::_get_indentation() const {
  2719. #ifdef TOOLS_ENABLED
  2720. if (Engine::get_singleton()->is_editor_hint()) {
  2721. bool use_space_indentation = EDITOR_GET("text_editor/indent/type");
  2722. if (use_space_indentation) {
  2723. int indent_size = EDITOR_GET("text_editor/indent/size");
  2724. String space_indent = "";
  2725. for (int i = 0; i < indent_size; i++) {
  2726. space_indent += " ";
  2727. }
  2728. return space_indent;
  2729. }
  2730. }
  2731. #endif
  2732. return "\t";
  2733. }
  2734. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2735. String indent = _get_indentation();
  2736. Vector<String> lines = p_code.split("\n");
  2737. List<int> indent_stack;
  2738. for (int i = 0; i < lines.size(); i++) {
  2739. String l = lines[i];
  2740. int tc = 0;
  2741. for (int j = 0; j < l.length(); j++) {
  2742. if (l[j] == ' ' || l[j] == '\t') {
  2743. tc++;
  2744. } else {
  2745. break;
  2746. }
  2747. }
  2748. String st = l.substr(tc, l.length()).strip_edges();
  2749. if (st == "" || st.begins_with("#")) {
  2750. continue; //ignore!
  2751. }
  2752. int ilevel = 0;
  2753. if (indent_stack.size()) {
  2754. ilevel = indent_stack.back()->get();
  2755. }
  2756. if (tc > ilevel) {
  2757. indent_stack.push_back(tc);
  2758. } else if (tc < ilevel) {
  2759. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2760. indent_stack.pop_back();
  2761. }
  2762. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2763. indent_stack.push_back(tc); //this is not right but gets the job done
  2764. }
  2765. }
  2766. if (i >= p_from_line) {
  2767. l = "";
  2768. for (int j = 0; j < indent_stack.size(); j++) {
  2769. l += indent;
  2770. }
  2771. l += st;
  2772. } else if (i > p_to_line) {
  2773. break;
  2774. }
  2775. lines.write[i] = l;
  2776. }
  2777. p_code = "";
  2778. for (int i = 0; i < lines.size(); i++) {
  2779. if (i > 0) {
  2780. p_code += "\n";
  2781. }
  2782. p_code += lines[i];
  2783. }
  2784. }
  2785. #ifdef TOOLS_ENABLED
  2786. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2787. GDScriptParser::DataType base_type = p_base;
  2788. while (base_type.has_type) {
  2789. switch (base_type.kind) {
  2790. case GDScriptParser::DataType::CLASS: {
  2791. if (base_type.class_type) {
  2792. if (p_is_function) {
  2793. for (int i = 0; i < base_type.class_type->functions.size(); i++) {
  2794. if (base_type.class_type->functions[i]->name == p_symbol) {
  2795. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2796. r_result.location = base_type.class_type->functions[i]->line;
  2797. return OK;
  2798. }
  2799. }
  2800. for (int i = 0; i < base_type.class_type->static_functions.size(); i++) {
  2801. if (base_type.class_type->static_functions[i]->name == p_symbol) {
  2802. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2803. r_result.location = base_type.class_type->static_functions[i]->line;
  2804. return OK;
  2805. }
  2806. }
  2807. } else {
  2808. if (base_type.class_type->constant_expressions.has(p_symbol)) {
  2809. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2810. r_result.location = base_type.class_type->constant_expressions[p_symbol].expression->line;
  2811. return OK;
  2812. }
  2813. for (int i = 0; i < base_type.class_type->variables.size(); i++) {
  2814. if (base_type.class_type->variables[i].identifier == p_symbol) {
  2815. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2816. r_result.location = base_type.class_type->variables[i].line;
  2817. return OK;
  2818. }
  2819. }
  2820. for (int i = 0; i < base_type.class_type->subclasses.size(); i++) {
  2821. if (base_type.class_type->subclasses[i]->name == p_symbol) {
  2822. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2823. r_result.location = base_type.class_type->subclasses[i]->line;
  2824. return OK;
  2825. }
  2826. }
  2827. }
  2828. base_type = base_type.class_type->base_type;
  2829. }
  2830. } break;
  2831. case GDScriptParser::DataType::SCRIPT:
  2832. case GDScriptParser::DataType::GDSCRIPT: {
  2833. Ref<Script> scr = base_type.script_type;
  2834. if (scr.is_valid()) {
  2835. int line = scr->get_member_line(p_symbol);
  2836. if (line >= 0) {
  2837. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2838. r_result.location = line;
  2839. r_result.script = scr;
  2840. return OK;
  2841. }
  2842. Ref<Script> base_script = scr->get_base_script();
  2843. if (base_script.is_valid()) {
  2844. base_type.script_type = base_script;
  2845. } else {
  2846. base_type.kind = GDScriptParser::DataType::NATIVE;
  2847. base_type.native_type = scr->get_instance_base_type();
  2848. }
  2849. } else {
  2850. base_type.has_type = false;
  2851. }
  2852. } break;
  2853. case GDScriptParser::DataType::NATIVE: {
  2854. StringName class_name = base_type.native_type;
  2855. if (!ClassDB::class_exists(class_name)) {
  2856. class_name = String("_") + class_name;
  2857. if (!ClassDB::class_exists(class_name)) {
  2858. base_type.has_type = false;
  2859. break;
  2860. }
  2861. }
  2862. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2863. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2864. r_result.class_name = base_type.native_type;
  2865. r_result.class_member = p_symbol;
  2866. return OK;
  2867. }
  2868. List<MethodInfo> virtual_methods;
  2869. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2870. for (List<MethodInfo>::Element *E = virtual_methods.front(); E; E = E->next()) {
  2871. if (E->get().name == p_symbol) {
  2872. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2873. r_result.class_name = base_type.native_type;
  2874. r_result.class_member = p_symbol;
  2875. return OK;
  2876. }
  2877. }
  2878. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2879. if (enum_name != StringName()) {
  2880. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  2881. r_result.class_name = base_type.native_type;
  2882. r_result.class_member = enum_name;
  2883. return OK;
  2884. }
  2885. List<String> constants;
  2886. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2887. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2888. if (E->get() == p_symbol) {
  2889. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2890. r_result.class_name = base_type.native_type;
  2891. r_result.class_member = p_symbol;
  2892. return OK;
  2893. }
  2894. }
  2895. List<PropertyInfo> properties;
  2896. ClassDB::get_property_list(class_name, &properties, true);
  2897. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  2898. if (E->get().name == p_symbol) {
  2899. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2900. r_result.class_name = base_type.native_type;
  2901. r_result.class_member = p_symbol;
  2902. return OK;
  2903. }
  2904. }
  2905. StringName parent = ClassDB::get_parent_class(class_name);
  2906. if (parent != StringName()) {
  2907. if (String(parent).begins_with("_")) {
  2908. base_type.native_type = String(parent).right(1);
  2909. } else {
  2910. base_type.native_type = parent;
  2911. }
  2912. } else {
  2913. base_type.has_type = false;
  2914. }
  2915. } break;
  2916. case GDScriptParser::DataType::BUILTIN: {
  2917. base_type.has_type = false;
  2918. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2919. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2920. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2921. r_result.class_member = p_symbol;
  2922. return OK;
  2923. }
  2924. Variant v;
  2925. REF v_ref;
  2926. if (base_type.builtin_type == Variant::OBJECT) {
  2927. v_ref.instance();
  2928. v = v_ref;
  2929. } else {
  2930. Variant::CallError err;
  2931. v = Variant::construct(base_type.builtin_type, nullptr, 0, err);
  2932. if (err.error != Variant::CallError::CALL_OK) {
  2933. break;
  2934. }
  2935. }
  2936. if (v.has_method(p_symbol)) {
  2937. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2938. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2939. r_result.class_member = p_symbol;
  2940. return OK;
  2941. }
  2942. bool valid = false;
  2943. v.get(p_symbol, &valid);
  2944. if (valid) {
  2945. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2946. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2947. r_result.class_member = p_symbol;
  2948. return OK;
  2949. }
  2950. } break;
  2951. default: {
  2952. base_type.has_type = false;
  2953. } break;
  2954. }
  2955. }
  2956. return ERR_CANT_RESOLVE;
  2957. }
  2958. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2959. // Before parsing, try the usual stuff.
  2960. if (ClassDB::class_exists(p_symbol)) {
  2961. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2962. r_result.class_name = p_symbol;
  2963. return OK;
  2964. } else {
  2965. String under_prefix = "_" + p_symbol;
  2966. if (ClassDB::class_exists(under_prefix)) {
  2967. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2968. r_result.class_name = p_symbol;
  2969. return OK;
  2970. }
  2971. }
  2972. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2973. Variant::Type t = Variant::Type(i);
  2974. if (Variant::get_type_name(t) == p_symbol) {
  2975. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2976. r_result.class_name = Variant::get_type_name(t);
  2977. return OK;
  2978. }
  2979. }
  2980. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2981. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2982. r_result.class_name = "@GDScript";
  2983. r_result.class_member = p_symbol;
  2984. return OK;
  2985. }
  2986. GDScriptParser parser;
  2987. parser.parse(p_code, p_path.get_base_dir(), false, p_path, true);
  2988. if (parser.get_completion_type() == GDScriptParser::COMPLETION_NONE) {
  2989. return ERR_CANT_RESOLVE;
  2990. }
  2991. GDScriptCompletionContext context;
  2992. context._class = parser.get_completion_class();
  2993. context.function = parser.get_completion_function();
  2994. context.block = parser.get_completion_block();
  2995. context.line = parser.get_completion_line();
  2996. context.base = p_owner;
  2997. context.base_path = p_path.get_base_dir();
  2998. if (context._class && context._class->extends_class.size() > 0) {
  2999. bool success = false;
  3000. ClassDB::get_integer_constant(context._class->extends_class[0], p_symbol, &success);
  3001. if (success) {
  3002. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3003. r_result.class_name = context._class->extends_class[0];
  3004. r_result.class_member = p_symbol;
  3005. return OK;
  3006. }
  3007. }
  3008. bool is_function = false;
  3009. switch (parser.get_completion_type()) {
  3010. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  3011. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3012. r_result.class_name = Variant::get_type_name(parser.get_completion_built_in_constant());
  3013. r_result.class_member = p_symbol;
  3014. return OK;
  3015. } break;
  3016. case GDScriptParser::COMPLETION_PARENT_FUNCTION:
  3017. case GDScriptParser::COMPLETION_FUNCTION: {
  3018. is_function = true;
  3019. FALLTHROUGH;
  3020. }
  3021. case GDScriptParser::COMPLETION_IDENTIFIER: {
  3022. if (!is_function) {
  3023. is_function = parser.get_completion_identifier_is_function();
  3024. }
  3025. GDScriptParser::DataType base_type;
  3026. if (context._class) {
  3027. if (parser.get_completion_type() != GDScriptParser::COMPLETION_PARENT_FUNCTION) {
  3028. base_type.has_type = true;
  3029. base_type.kind = GDScriptParser::DataType::CLASS;
  3030. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
  3031. } else {
  3032. base_type = context._class->base_type;
  3033. }
  3034. } else {
  3035. break;
  3036. }
  3037. if (!is_function && context.block) {
  3038. // Lookup local variables
  3039. const GDScriptParser::BlockNode *block = context.block;
  3040. while (block) {
  3041. if (block->variables.has(p_symbol)) {
  3042. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3043. r_result.location = block->variables[p_symbol]->line;
  3044. return OK;
  3045. }
  3046. block = block->parent_block;
  3047. }
  3048. }
  3049. if (context.function && context.function->name != StringName()) {
  3050. // Lookup function arguments
  3051. for (int i = 0; i < context.function->arguments.size(); i++) {
  3052. if (context.function->arguments[i] == p_symbol) {
  3053. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3054. r_result.location = context.function->line;
  3055. return OK;
  3056. }
  3057. }
  3058. }
  3059. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  3060. return OK;
  3061. }
  3062. if (!is_function) {
  3063. // Guess in autoloads as singletons
  3064. List<PropertyInfo> props;
  3065. ProjectSettings::get_singleton()->get_property_list(&props);
  3066. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3067. String s = E->get().name;
  3068. if (!s.begins_with("autoload/")) {
  3069. continue;
  3070. }
  3071. String name = s.get_slice("/", 1);
  3072. if (name == String(p_symbol)) {
  3073. String path = ProjectSettings::get_singleton()->get(s);
  3074. if (path.begins_with("*")) {
  3075. String script = path.substr(1, path.length());
  3076. if (!script.ends_with(".gd")) {
  3077. // Not a script, try find the script anyway,
  3078. // may have some success
  3079. script = script.get_basename() + ".gd";
  3080. }
  3081. if (FileAccess::exists(script)) {
  3082. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  3083. r_result.location = 0;
  3084. r_result.script = ResourceLoader::load(script);
  3085. return OK;
  3086. }
  3087. }
  3088. }
  3089. }
  3090. // Global
  3091. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  3092. if (classes.has(p_symbol)) {
  3093. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  3094. if (value.get_type() == Variant::OBJECT) {
  3095. Object *obj = value;
  3096. if (obj) {
  3097. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  3098. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  3099. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  3100. } else {
  3101. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  3102. r_result.class_name = obj->get_class();
  3103. }
  3104. // proxy class remove the underscore.
  3105. if (r_result.class_name.begins_with("_")) {
  3106. r_result.class_name = r_result.class_name.right(1);
  3107. }
  3108. return OK;
  3109. }
  3110. } else {
  3111. /*
  3112. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  3113. // We cannot determine the exact nature of the identifier here
  3114. // Otherwise these codes would work
  3115. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  3116. if (enumName != NULL) {
  3117. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_ENUM;
  3118. r_result.class_name = "@GlobalScope";
  3119. r_result.class_member = enumName;
  3120. return OK;
  3121. }
  3122. else {
  3123. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  3124. r_result.class_name = "@GlobalScope";
  3125. r_result.class_member = p_symbol;
  3126. return OK;
  3127. }*/
  3128. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE;
  3129. r_result.class_name = "@GlobalScope";
  3130. r_result.class_member = p_symbol;
  3131. return OK;
  3132. }
  3133. }
  3134. }
  3135. } break;
  3136. case GDScriptParser::COMPLETION_METHOD: {
  3137. is_function = true;
  3138. FALLTHROUGH;
  3139. }
  3140. case GDScriptParser::COMPLETION_INDEX: {
  3141. const GDScriptParser::Node *node = parser.get_completion_node();
  3142. if (node->type != GDScriptParser::Node::TYPE_OPERATOR) {
  3143. break;
  3144. }
  3145. GDScriptCompletionIdentifier base;
  3146. if (!_guess_expression_type(context, static_cast<const GDScriptParser::OperatorNode *>(node)->arguments[0], base)) {
  3147. break;
  3148. }
  3149. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  3150. return OK;
  3151. }
  3152. } break;
  3153. case GDScriptParser::COMPLETION_VIRTUAL_FUNC: {
  3154. GDScriptParser::DataType base_type = context._class->base_type;
  3155. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3156. return OK;
  3157. }
  3158. } break;
  3159. case GDScriptParser::COMPLETION_TYPE_HINT: {
  3160. GDScriptParser::DataType base_type = context._class->base_type;
  3161. base_type.has_type = true;
  3162. base_type.kind = GDScriptParser::DataType::CLASS;
  3163. base_type.class_type = const_cast<GDScriptParser::ClassNode *>(context._class);
  3164. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  3165. return OK;
  3166. }
  3167. } break;
  3168. default: {
  3169. }
  3170. }
  3171. // Must check GDScript functions after parsing to ensure functions like Array.max() are handled correctly.
  3172. bool function_in_gdscript_functions = false;
  3173. for (int i = 0; i < GDScriptFunctions::FUNC_MAX; i++) {
  3174. if (GDScriptFunctions::get_func_name(GDScriptFunctions::Function(i)) == p_symbol) {
  3175. function_in_gdscript_functions = true;
  3176. break;
  3177. }
  3178. }
  3179. // Need special checks for assert, preload, and yield as they are technically
  3180. // keywords, so are not registered in GDScriptFunctions.
  3181. if (function_in_gdscript_functions || "yield" == p_symbol || "assert" == p_symbol || "preload" == p_symbol) {
  3182. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  3183. r_result.class_name = "@GDScript";
  3184. r_result.class_member = p_symbol;
  3185. return OK;
  3186. }
  3187. return ERR_CANT_RESOLVE;
  3188. }
  3189. #endif