gdscript_editor.cpp 119 KB

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