gdscript_editor.cpp 118 KB

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