gdscript_editor.cpp 119 KB

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