gdscript_editor.cpp 119 KB

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