gdscript_editor.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213
  1. /*************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/config/engine.h"
  32. #include "core/core_constants.h"
  33. #include "core/io/file_access.h"
  34. #include "gdscript_analyzer.h"
  35. #include "gdscript_compiler.h"
  36. #include "gdscript_parser.h"
  37. #include "gdscript_tokenizer.h"
  38. #include "gdscript_utility_functions.h"
  39. #ifdef TOOLS_ENABLED
  40. #include "core/config/project_settings.h"
  41. #include "editor/editor_file_system.h"
  42. #include "editor/editor_settings.h"
  43. #include "editor/script_templates/templates.gen.h"
  44. #endif
  45. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  46. p_delimiters->push_back("#");
  47. }
  48. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  49. p_delimiters->push_back("\" \"");
  50. p_delimiters->push_back("' '");
  51. p_delimiters->push_back("\"\"\" \"\"\"");
  52. }
  53. bool GDScriptLanguage::is_using_templates() {
  54. return true;
  55. }
  56. Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
  57. Ref<GDScript> script;
  58. script.instantiate();
  59. String processed_template = p_template;
  60. bool type_hints = false;
  61. #ifdef TOOLS_ENABLED
  62. type_hints = EDITOR_GET("text_editor/completion/add_type_hints");
  63. #endif
  64. if (!type_hints) {
  65. processed_template = processed_template.replace(": int", "")
  66. .replace(": String", "")
  67. .replace(": Array[String]", "")
  68. .replace(": float", "")
  69. .replace(":=", "=")
  70. .replace(" -> String", "")
  71. .replace(" -> int", "")
  72. .replace(" -> void", "");
  73. }
  74. processed_template = processed_template.replace("_BASE_", p_base_class_name)
  75. .replace("_CLASS_", p_class_name)
  76. .replace("_TS_", _get_indentation());
  77. script->set_source_code(processed_template);
  78. return script;
  79. }
  80. Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(StringName p_object) {
  81. Vector<ScriptLanguage::ScriptTemplate> templates;
  82. #ifdef TOOLS_ENABLED
  83. for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
  84. if (TEMPLATES[i].inherit == p_object) {
  85. templates.append(TEMPLATES[i]);
  86. }
  87. }
  88. #endif
  89. return templates;
  90. }
  91. static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, Map<int, String> &r_funcs) {
  92. for (int i = 0; i < p_class->members.size(); i++) {
  93. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
  94. const GDScriptParser::FunctionNode *function = p_class->members[i].function;
  95. r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
  96. } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  97. String new_prefix = p_class->members[i].m_class->identifier->name;
  98. get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
  99. }
  100. }
  101. }
  102. bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
  103. GDScriptParser parser;
  104. GDScriptAnalyzer analyzer(&parser);
  105. Error err = parser.parse(p_script, p_path, false);
  106. if (err == OK) {
  107. err = analyzer.analyze();
  108. }
  109. #ifdef DEBUG_ENABLED
  110. if (r_warnings) {
  111. for (const GDScriptWarning &E : parser.get_warnings()) {
  112. const GDScriptWarning &warn = E;
  113. ScriptLanguage::Warning w;
  114. w.start_line = warn.start_line;
  115. w.end_line = warn.end_line;
  116. w.leftmost_column = warn.leftmost_column;
  117. w.rightmost_column = warn.rightmost_column;
  118. w.code = (int)warn.code;
  119. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  120. w.message = warn.get_message();
  121. r_warnings->push_back(w);
  122. }
  123. }
  124. #endif
  125. if (err) {
  126. if (r_errors) {
  127. for (const GDScriptParser::ParserError &E : parser.get_errors()) {
  128. const GDScriptParser::ParserError &pe = E;
  129. ScriptLanguage::ScriptError e;
  130. e.line = pe.line;
  131. e.column = pe.column;
  132. e.message = pe.message;
  133. r_errors->push_back(e);
  134. }
  135. }
  136. return false;
  137. } else {
  138. const GDScriptParser::ClassNode *cl = parser.get_tree();
  139. Map<int, String> funcs;
  140. get_function_names_recursively(cl, "", funcs);
  141. for (const KeyValue<int, String> &E : funcs) {
  142. r_functions->push_back(E.value + ":" + itos(E.key));
  143. }
  144. }
  145. #ifdef DEBUG_ENABLED
  146. if (r_safe_lines) {
  147. const Set<int> &unsafe_lines = parser.get_unsafe_lines();
  148. for (int i = 1; i <= parser.get_last_line_number(); i++) {
  149. if (!unsafe_lines.has(i)) {
  150. r_safe_lines->insert(i);
  151. }
  152. }
  153. }
  154. #endif
  155. return true;
  156. }
  157. bool GDScriptLanguage::has_named_classes() const {
  158. return false;
  159. }
  160. bool GDScriptLanguage::supports_builtin_mode() const {
  161. return true;
  162. }
  163. bool GDScriptLanguage::supports_documentation() const {
  164. return true;
  165. }
  166. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  167. GDScriptTokenizer tokenizer;
  168. tokenizer.set_source_code(p_code);
  169. int indent = 0;
  170. GDScriptTokenizer::Token current = tokenizer.scan();
  171. while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
  172. if (current.type == GDScriptTokenizer::Token::INDENT) {
  173. indent++;
  174. } else if (current.type == GDScriptTokenizer::Token::DEDENT) {
  175. indent--;
  176. }
  177. if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
  178. current = tokenizer.scan();
  179. if (current.is_identifier()) {
  180. String identifier = current.get_identifier();
  181. if (identifier == p_function) {
  182. return current.start_line;
  183. }
  184. }
  185. }
  186. current = tokenizer.scan();
  187. }
  188. return -1;
  189. }
  190. Script *GDScriptLanguage::create_script() const {
  191. return memnew(GDScript);
  192. }
  193. /* DEBUGGER FUNCTIONS */
  194. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  195. // break because of parse error
  196. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  197. _debug_parse_err_line = p_line;
  198. _debug_parse_err_file = p_file;
  199. _debug_error = p_error;
  200. EngineDebugger::get_script_debugger()->debug(this, false, true);
  201. return true;
  202. } else {
  203. return false;
  204. }
  205. }
  206. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  207. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  208. _debug_parse_err_line = -1;
  209. _debug_parse_err_file = "";
  210. _debug_error = p_error;
  211. bool is_error_breakpoint = p_error != "Breakpoint";
  212. EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
  213. return true;
  214. } else {
  215. return false;
  216. }
  217. }
  218. String GDScriptLanguage::debug_get_error() const {
  219. return _debug_error;
  220. }
  221. int GDScriptLanguage::debug_get_stack_level_count() const {
  222. if (_debug_parse_err_line >= 0) {
  223. return 1;
  224. }
  225. return _debug_call_stack_pos;
  226. }
  227. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  228. if (_debug_parse_err_line >= 0) {
  229. return _debug_parse_err_line;
  230. }
  231. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  232. int l = _debug_call_stack_pos - p_level - 1;
  233. return *(_call_stack[l].line);
  234. }
  235. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  236. if (_debug_parse_err_line >= 0) {
  237. return "";
  238. }
  239. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  240. int l = _debug_call_stack_pos - p_level - 1;
  241. return _call_stack[l].function->get_name();
  242. }
  243. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  244. if (_debug_parse_err_line >= 0) {
  245. return _debug_parse_err_file;
  246. }
  247. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  248. int l = _debug_call_stack_pos - p_level - 1;
  249. return _call_stack[l].function->get_source();
  250. }
  251. 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) {
  252. if (_debug_parse_err_line >= 0) {
  253. return;
  254. }
  255. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  256. int l = _debug_call_stack_pos - p_level - 1;
  257. GDScriptFunction *f = _call_stack[l].function;
  258. List<Pair<StringName, int>> locals;
  259. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  260. for (const Pair<StringName, int> &E : locals) {
  261. p_locals->push_back(E.first);
  262. p_values->push_back(_call_stack[l].stack[E.second]);
  263. }
  264. }
  265. 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) {
  266. if (_debug_parse_err_line >= 0) {
  267. return;
  268. }
  269. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  270. int l = _debug_call_stack_pos - p_level - 1;
  271. GDScriptInstance *instance = _call_stack[l].instance;
  272. if (!instance) {
  273. return;
  274. }
  275. Ref<GDScript> script = instance->get_script();
  276. ERR_FAIL_COND(script.is_null());
  277. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  278. for (const KeyValue<StringName, GDScript::MemberInfo> &E : mi) {
  279. p_members->push_back(E.key);
  280. p_values->push_back(instance->debug_get_member_by_index(E.value.index));
  281. }
  282. }
  283. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  284. if (_debug_parse_err_line >= 0) {
  285. return nullptr;
  286. }
  287. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, nullptr);
  288. int l = _debug_call_stack_pos - p_level - 1;
  289. ScriptInstance *instance = _call_stack[l].instance;
  290. return instance;
  291. }
  292. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  293. const Map<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  294. const Variant *globals = GDScriptLanguage::get_singleton()->get_global_array();
  295. List<Pair<String, Variant>> cinfo;
  296. get_public_constants(&cinfo);
  297. for (const KeyValue<StringName, int> &E : name_idx) {
  298. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  299. continue;
  300. }
  301. bool is_script_constant = false;
  302. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  303. if (CE->get().first == E.key) {
  304. is_script_constant = true;
  305. break;
  306. }
  307. }
  308. if (is_script_constant) {
  309. continue;
  310. }
  311. const Variant &var = globals[E.value];
  312. if (Object *obj = var) {
  313. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  314. continue;
  315. }
  316. }
  317. bool skip = false;
  318. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  319. if (E.key == CoreConstants::get_global_constant_name(i)) {
  320. skip = true;
  321. break;
  322. }
  323. }
  324. if (skip) {
  325. continue;
  326. }
  327. p_globals->push_back(E.key);
  328. p_values->push_back(var);
  329. }
  330. }
  331. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  332. return "";
  333. }
  334. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  335. p_extensions->push_back("gd");
  336. }
  337. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  338. List<StringName> functions;
  339. GDScriptUtilityFunctions::get_function_list(&functions);
  340. for (const StringName &E : functions) {
  341. p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E));
  342. }
  343. // Not really "functions", but show in documentation.
  344. {
  345. MethodInfo mi;
  346. mi.name = "preload";
  347. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  348. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  349. p_functions->push_back(mi);
  350. }
  351. {
  352. MethodInfo mi;
  353. mi.name = "assert";
  354. mi.return_val.type = Variant::NIL;
  355. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  356. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  357. mi.default_arguments.push_back(String());
  358. p_functions->push_back(mi);
  359. }
  360. }
  361. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  362. Pair<String, Variant> pi;
  363. pi.first = "PI";
  364. pi.second = Math_PI;
  365. p_constants->push_back(pi);
  366. Pair<String, Variant> tau;
  367. tau.first = "TAU";
  368. tau.second = Math_TAU;
  369. p_constants->push_back(tau);
  370. Pair<String, Variant> infinity;
  371. infinity.first = "INF";
  372. infinity.second = INFINITY;
  373. p_constants->push_back(infinity);
  374. Pair<String, Variant> nan;
  375. nan.first = "NAN";
  376. nan.second = NAN;
  377. p_constants->push_back(nan);
  378. }
  379. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  380. #ifdef TOOLS_ENABLED
  381. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  382. #else
  383. bool th = false;
  384. #endif
  385. String s = "func " + p_name + "(";
  386. if (p_args.size()) {
  387. for (int i = 0; i < p_args.size(); i++) {
  388. if (i > 0) {
  389. s += ", ";
  390. }
  391. s += p_args[i].get_slice(":", 0);
  392. if (th) {
  393. String type = p_args[i].get_slice(":", 1);
  394. if (!type.is_empty() && type != "var") {
  395. s += ": " + type;
  396. }
  397. }
  398. }
  399. }
  400. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  401. return s;
  402. }
  403. //////// COMPLETION //////////
  404. #ifdef TOOLS_ENABLED
  405. #define COMPLETION_RECURSION_LIMIT 200
  406. struct GDScriptCompletionIdentifier {
  407. GDScriptParser::DataType type;
  408. String enumeration;
  409. Variant value;
  410. const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
  411. };
  412. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg = true) {
  413. if (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  414. String enum_name = p_info.class_name;
  415. if (!enum_name.contains(".")) {
  416. return enum_name;
  417. }
  418. return enum_name.get_slice(".", 1);
  419. }
  420. String n = p_info.name;
  421. int idx = n.find(":");
  422. if (idx != -1) {
  423. return n.substr(idx + 1, n.length());
  424. }
  425. if (p_info.type == Variant::OBJECT) {
  426. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  427. return p_info.hint_string;
  428. } else {
  429. return p_info.class_name.operator String();
  430. }
  431. }
  432. if (p_info.type == Variant::NIL) {
  433. if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  434. return "Variant";
  435. } else {
  436. return "void";
  437. }
  438. }
  439. return Variant::get_type_name(p_info.type);
  440. }
  441. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
  442. String arghint;
  443. if (!p_is_annotation) {
  444. arghint += _get_visual_datatype(p_info.return_val, false) + " ";
  445. }
  446. arghint += p_info.name + "(";
  447. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  448. int i = 0;
  449. for (const PropertyInfo &E : p_info.arguments) {
  450. if (i > 0) {
  451. arghint += ", ";
  452. }
  453. if (i == p_arg_idx) {
  454. arghint += String::chr(0xFFFF);
  455. }
  456. arghint += E.name + ": " + _get_visual_datatype(E, true);
  457. if (i - def_args >= 0) {
  458. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  459. }
  460. if (i == p_arg_idx) {
  461. arghint += String::chr(0xFFFF);
  462. }
  463. i++;
  464. }
  465. if (p_info.flags & METHOD_FLAG_VARARG) {
  466. if (p_info.arguments.size() > 0) {
  467. arghint += ", ";
  468. }
  469. if (p_arg_idx >= p_info.arguments.size()) {
  470. arghint += String::chr(0xFFFF);
  471. }
  472. arghint += "...";
  473. if (p_arg_idx >= p_info.arguments.size()) {
  474. arghint += String::chr(0xFFFF);
  475. }
  476. }
  477. arghint += ")";
  478. return arghint;
  479. }
  480. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  481. String arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name.operator String() + "(";
  482. for (int i = 0; i < p_function->parameters.size(); i++) {
  483. if (i > 0) {
  484. arghint += ", ";
  485. }
  486. if (i == p_arg_idx) {
  487. arghint += String::chr(0xFFFF);
  488. }
  489. const GDScriptParser::ParameterNode *par = p_function->parameters[i];
  490. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  491. if (par->default_value) {
  492. String def_val = "<unknown>";
  493. switch (par->default_value->type) {
  494. case GDScriptParser::Node::LITERAL: {
  495. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->default_value);
  496. def_val = literal->value.get_construct_string();
  497. } break;
  498. case GDScriptParser::Node::IDENTIFIER: {
  499. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->default_value);
  500. def_val = id->name.operator String();
  501. } break;
  502. case GDScriptParser::Node::CALL: {
  503. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->default_value);
  504. if (call->is_constant && call->reduced) {
  505. def_val = call->function_name.operator String() + call->reduced_value.operator String();
  506. }
  507. } break;
  508. case GDScriptParser::Node::ARRAY: {
  509. const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->default_value);
  510. if (arr->is_constant && arr->reduced) {
  511. def_val = arr->reduced_value.operator String();
  512. }
  513. } break;
  514. case GDScriptParser::Node::DICTIONARY: {
  515. const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->default_value);
  516. if (dict->is_constant && dict->reduced) {
  517. def_val = dict->reduced_value.operator String();
  518. }
  519. } break;
  520. case GDScriptParser::Node::SUBSCRIPT: {
  521. const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->default_value);
  522. if (sub->is_constant) {
  523. if (sub->datatype.kind == GDScriptParser::DataType::ENUM) {
  524. def_val = sub->get_datatype().to_string();
  525. } else if (sub->reduced) {
  526. const Variant::Type vt = sub->reduced_value.get_type();
  527. if (vt == Variant::Type::NIL || vt == Variant::Type::FLOAT || vt == Variant::Type::INT || vt == Variant::Type::STRING || vt == Variant::Type::STRING_NAME || vt == Variant::Type::BOOL || vt == Variant::Type::NODE_PATH) {
  528. def_val = sub->reduced_value.operator String();
  529. } else {
  530. def_val = sub->get_datatype().to_string() + sub->reduced_value.operator String();
  531. }
  532. }
  533. }
  534. } break;
  535. default:
  536. break;
  537. }
  538. arghint += " = " + def_val;
  539. }
  540. if (i == p_arg_idx) {
  541. arghint += String::chr(0xFFFF);
  542. }
  543. }
  544. arghint += ")";
  545. return arghint;
  546. }
  547. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, Map<String, ScriptLanguage::CodeCompletionOption> &r_list) {
  548. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  549. for (int i = 0; i < p_dir->get_file_count(); i++) {
  550. ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH);
  551. option.insert_text = option.display.quote(quote_style);
  552. r_list.insert(option.display, option);
  553. }
  554. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  555. _get_directory_contents(p_dir->get_subdir(i), r_list);
  556. }
  557. }
  558. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, Map<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  559. if (p_annotation->name == SNAME("@export_range")) {
  560. if (p_argument == 3 || p_argument == 4) {
  561. // Slider hint.
  562. ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  563. slider1.insert_text = slider1.display.quote(p_quote_style);
  564. r_result.insert(slider1.display, slider1);
  565. ScriptLanguage::CodeCompletionOption slider2("or_lesser", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  566. slider2.insert_text = slider2.display.quote(p_quote_style);
  567. r_result.insert(slider2.display, slider2);
  568. }
  569. } else if (p_annotation->name == SNAME("@export_exp_easing")) {
  570. if (p_argument == 0 || p_argument == 1) {
  571. // Easing hint.
  572. ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  573. hint1.insert_text = hint1.display.quote(p_quote_style);
  574. r_result.insert(hint1.display, hint1);
  575. ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  576. hint2.insert_text = hint2.display.quote(p_quote_style);
  577. r_result.insert(hint2.display, hint2);
  578. }
  579. } else if (p_annotation->name == SNAME("@export_node_path")) {
  580. ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  581. r_result.insert(node.display, node);
  582. List<StringName> node_types;
  583. ClassDB::get_inheriters_from_class("Node", &node_types);
  584. for (const StringName &E : node_types) {
  585. if (!ClassDB::is_class_exposed(E)) {
  586. continue;
  587. }
  588. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  589. r_result.insert(option.display, option);
  590. }
  591. } else if (p_annotation->name == SNAME("@warning_ignore")) {
  592. for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
  593. ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  594. r_result.insert(warning.display, warning);
  595. }
  596. }
  597. }
  598. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, Map<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  599. List<StringName> native_types;
  600. ClassDB::get_class_list(&native_types);
  601. for (const StringName &E : native_types) {
  602. if (ClassDB::is_class_exposed(E) && !Engine::get_singleton()->has_singleton(E)) {
  603. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  604. r_result.insert(option.display, option);
  605. }
  606. }
  607. if (p_context.current_class) {
  608. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  609. // Native enums from base class
  610. List<StringName> enums;
  611. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  612. for (const StringName &E : enums) {
  613. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  614. r_result.insert(option.display, option);
  615. }
  616. }
  617. // Check current class for potential types
  618. const GDScriptParser::ClassNode *current = p_context.current_class;
  619. while (current) {
  620. for (int i = 0; i < current->members.size(); i++) {
  621. const GDScriptParser::ClassNode::Member &member = current->members[i];
  622. switch (member.type) {
  623. case GDScriptParser::ClassNode::Member::CLASS: {
  624. ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  625. r_result.insert(option.display, option);
  626. } break;
  627. case GDScriptParser::ClassNode::Member::ENUM: {
  628. if (!p_inherit_only) {
  629. ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  630. r_result.insert(option.display, option);
  631. }
  632. } break;
  633. case GDScriptParser::ClassNode::Member::CONSTANT: {
  634. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  635. ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  636. r_result.insert(option.display, option);
  637. }
  638. } break;
  639. default:
  640. break;
  641. }
  642. }
  643. current = current->outer;
  644. }
  645. }
  646. // Global scripts
  647. List<StringName> global_classes;
  648. ScriptServer::get_global_class_list(&global_classes);
  649. for (const StringName &E : global_classes) {
  650. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  651. r_result.insert(option.display, option);
  652. }
  653. // Autoload singletons
  654. OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  655. for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
  656. const ProjectSettings::AutoloadInfo &info = E.get();
  657. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  658. continue;
  659. }
  660. ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  661. r_result.insert(option.display, option);
  662. }
  663. }
  664. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, Map<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  665. for (int i = 0; i < p_suite->locals.size(); i++) {
  666. ScriptLanguage::CodeCompletionOption option;
  667. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  668. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  669. option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
  670. } else {
  671. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE);
  672. }
  673. r_result.insert(option.display, option);
  674. }
  675. if (p_suite->parent_block) {
  676. _find_identifiers_in_suite(p_suite->parent_block, r_result);
  677. }
  678. }
  679. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
  680. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_static, bool p_parent_only, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  681. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  682. if (!p_parent_only) {
  683. bool outer = false;
  684. const GDScriptParser::ClassNode *clss = p_class;
  685. while (clss) {
  686. for (int i = 0; i < clss->members.size(); i++) {
  687. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  688. ScriptLanguage::CodeCompletionOption option;
  689. switch (member.type) {
  690. case GDScriptParser::ClassNode::Member::VARIABLE:
  691. if (p_only_functions || outer || (p_static)) {
  692. continue;
  693. }
  694. option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  695. break;
  696. case GDScriptParser::ClassNode::Member::CONSTANT:
  697. if (p_only_functions) {
  698. continue;
  699. }
  700. if (r_result.has(member.constant->identifier->name)) {
  701. continue;
  702. }
  703. option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  704. if (member.constant->initializer) {
  705. option.default_value = member.constant->initializer->reduced_value;
  706. }
  707. break;
  708. case GDScriptParser::ClassNode::Member::CLASS:
  709. if (p_only_functions) {
  710. continue;
  711. }
  712. option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  713. break;
  714. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  715. if (p_only_functions) {
  716. continue;
  717. }
  718. option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  719. break;
  720. case GDScriptParser::ClassNode::Member::ENUM:
  721. if (p_only_functions) {
  722. continue;
  723. }
  724. option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  725. break;
  726. case GDScriptParser::ClassNode::Member::FUNCTION:
  727. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  728. continue;
  729. }
  730. option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  731. if (member.function->parameters.size() > 0) {
  732. option.insert_text += "(";
  733. } else {
  734. option.insert_text += "()";
  735. }
  736. break;
  737. case GDScriptParser::ClassNode::Member::SIGNAL:
  738. if (p_only_functions || outer) {
  739. continue;
  740. }
  741. option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
  742. break;
  743. case GDScriptParser::ClassNode::Member::UNDEFINED:
  744. break;
  745. }
  746. r_result.insert(option.display, option);
  747. }
  748. outer = true;
  749. clss = clss->outer;
  750. }
  751. }
  752. // Parents.
  753. GDScriptCompletionIdentifier base_type;
  754. base_type.type = p_class->base_type;
  755. base_type.type.is_meta_type = p_static;
  756. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  757. }
  758. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  759. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  760. GDScriptParser::DataType base_type = p_base.type;
  761. bool _static = base_type.is_meta_type;
  762. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  763. ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  764. option.insert_text += "(";
  765. r_result.insert(option.display, option);
  766. }
  767. while (!base_type.has_no_type()) {
  768. switch (base_type.kind) {
  769. case GDScriptParser::DataType::CLASS: {
  770. _find_identifiers_in_class(base_type.class_type, p_only_functions, _static, false, r_result, p_recursion_depth + 1);
  771. // This already finds all parent identifiers, so we are done.
  772. base_type = GDScriptParser::DataType();
  773. } break;
  774. case GDScriptParser::DataType::SCRIPT: {
  775. Ref<Script> scr = base_type.script_type;
  776. if (scr.is_valid()) {
  777. if (!p_only_functions) {
  778. if (!_static) {
  779. List<PropertyInfo> members;
  780. scr->get_script_property_list(&members);
  781. for (const PropertyInfo &E : members) {
  782. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  783. r_result.insert(option.display, option);
  784. }
  785. }
  786. Map<StringName, Variant> constants;
  787. scr->get_constants(&constants);
  788. for (const KeyValue<StringName, Variant> &E : constants) {
  789. ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  790. r_result.insert(option.display, option);
  791. }
  792. List<MethodInfo> signals;
  793. scr->get_script_signal_list(&signals);
  794. for (const MethodInfo &E : signals) {
  795. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
  796. r_result.insert(option.display, option);
  797. }
  798. }
  799. List<MethodInfo> methods;
  800. scr->get_script_method_list(&methods);
  801. for (const MethodInfo &E : methods) {
  802. if (E.name.begins_with("@")) {
  803. continue;
  804. }
  805. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  806. if (E.arguments.size()) {
  807. option.insert_text += "(";
  808. } else {
  809. option.insert_text += "()";
  810. }
  811. r_result.insert(option.display, option);
  812. }
  813. Ref<Script> base_script = scr->get_base_script();
  814. if (base_script.is_valid()) {
  815. base_type.script_type = base_script;
  816. } else {
  817. base_type.kind = GDScriptParser::DataType::NATIVE;
  818. base_type.native_type = scr->get_instance_base_type();
  819. }
  820. } else {
  821. return;
  822. }
  823. } break;
  824. case GDScriptParser::DataType::NATIVE: {
  825. StringName type = base_type.native_type;
  826. if (!ClassDB::class_exists(type)) {
  827. return;
  828. }
  829. if (!p_only_functions) {
  830. List<String> constants;
  831. ClassDB::get_integer_constant_list(type, &constants);
  832. for (const String &E : constants) {
  833. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  834. r_result.insert(option.display, option);
  835. }
  836. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  837. List<PropertyInfo> pinfo;
  838. ClassDB::get_property_list(type, &pinfo);
  839. for (const PropertyInfo &E : pinfo) {
  840. if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  841. continue;
  842. }
  843. if (E.name.contains("/")) {
  844. continue;
  845. }
  846. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  847. r_result.insert(option.display, option);
  848. }
  849. }
  850. }
  851. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  852. List<MethodInfo> methods;
  853. ClassDB::get_method_list(type, &methods, false, true);
  854. for (const MethodInfo &E : methods) {
  855. if (E.name.begins_with("_")) {
  856. continue;
  857. }
  858. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  859. if (E.arguments.size()) {
  860. option.insert_text += "(";
  861. } else {
  862. option.insert_text += "()";
  863. }
  864. r_result.insert(option.display, option);
  865. }
  866. }
  867. return;
  868. } break;
  869. case GDScriptParser::DataType::BUILTIN: {
  870. Callable::CallError err;
  871. Variant tmp;
  872. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  873. if (err.error != Callable::CallError::CALL_OK) {
  874. return;
  875. }
  876. if (!p_only_functions) {
  877. List<PropertyInfo> members;
  878. if (p_base.value.get_type() != Variant::NIL) {
  879. p_base.value.get_property_list(&members);
  880. } else {
  881. tmp.get_property_list(&members);
  882. }
  883. for (const PropertyInfo &E : members) {
  884. if (!String(E.name).contains("/")) {
  885. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  886. r_result.insert(option.display, option);
  887. }
  888. }
  889. }
  890. List<MethodInfo> methods;
  891. tmp.get_method_list(&methods);
  892. for (const MethodInfo &E : methods) {
  893. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  894. if (E.arguments.size()) {
  895. option.insert_text += "(";
  896. } else {
  897. option.insert_text += "()";
  898. }
  899. r_result.insert(option.display, option);
  900. }
  901. return;
  902. } break;
  903. default: {
  904. return;
  905. } break;
  906. }
  907. }
  908. }
  909. static void _find_identifiers(GDScriptParser::CompletionContext &p_context, bool p_only_functions, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  910. if (!p_only_functions && p_context.current_suite) {
  911. // This includes function parameters, since they are also locals.
  912. _find_identifiers_in_suite(p_context.current_suite, r_result);
  913. }
  914. if (p_context.current_class) {
  915. _find_identifiers_in_class(p_context.current_class, p_only_functions, (!p_context.current_function || p_context.current_function->is_static), false, r_result, p_recursion_depth + 1);
  916. }
  917. List<StringName> functions;
  918. GDScriptUtilityFunctions::get_function_list(&functions);
  919. for (const StringName &E : functions) {
  920. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
  921. ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  922. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  923. option.insert_text += "(";
  924. } else {
  925. option.insert_text += "()";
  926. }
  927. r_result.insert(option.display, option);
  928. }
  929. if (p_only_functions) {
  930. return;
  931. }
  932. static const char *_type_names[Variant::VARIANT_MAX] = {
  933. "null", "bool", "int", "float", "String", "StringName", "Vector2", "Vector2i", "Rect2", "Rect2i", "Vector3", "Vector3i", "Transform2D", "Plane", "Quaternion", "AABB", "Basis", "Transform3D",
  934. "Color", "NodePath", "RID", "Signal", "Callable", "Object", "Dictionary", "Array", "PackedByteArray", "PackedInt32Array", "PackedInt64Array", "PackedFloat32Array", "PackedFloat64Array", "PackedStringArray",
  935. "PackedVector2Array", "PackedVector3Array", "PackedColorArray"
  936. };
  937. static_assert((sizeof(_type_names) / sizeof(*_type_names)) == Variant::VARIANT_MAX, "Completion for builtin types is incomplete");
  938. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  939. ScriptLanguage::CodeCompletionOption option(_type_names[i], ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  940. r_result.insert(option.display, option);
  941. }
  942. static const char *_keywords[] = {
  943. "false", "PI", "TAU", "INF", "NAN", "self", "true", "breakpoint", "tool", "super",
  944. "break", "continue", "pass", "return",
  945. nullptr
  946. };
  947. const char **kw = _keywords;
  948. while (*kw) {
  949. ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  950. r_result.insert(option.display, option);
  951. kw++;
  952. }
  953. static const char *_keywords_with_space[] = {
  954. "and", "in", "not", "or", "as", "class", "extends", "is", "func", "signal", "await",
  955. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "while",
  956. nullptr
  957. };
  958. const char **kws = _keywords_with_space;
  959. while (*kws) {
  960. ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  961. option.insert_text += " ";
  962. r_result.insert(option.display, option);
  963. kws++;
  964. }
  965. static const char *_keywords_with_args[] = {
  966. "assert", "preload",
  967. nullptr
  968. };
  969. const char **kwa = _keywords_with_args;
  970. while (*kwa) {
  971. ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  972. option.insert_text += "(";
  973. r_result.insert(option.display, option);
  974. kwa++;
  975. }
  976. List<StringName> utility_func_names;
  977. Variant::get_utility_function_list(&utility_func_names);
  978. for (List<StringName>::Element *E = utility_func_names.front(); E; E = E->next()) {
  979. ScriptLanguage::CodeCompletionOption option(E->get(), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  980. option.insert_text += "(";
  981. r_result.insert(option.display, option);
  982. }
  983. OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  984. for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
  985. if (!E.value().is_singleton) {
  986. continue;
  987. }
  988. ScriptLanguage::CodeCompletionOption option(E.key(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  989. r_result.insert(option.display, option);
  990. }
  991. // Native classes and global constants.
  992. for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
  993. ScriptLanguage::CodeCompletionOption option;
  994. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  995. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  996. } else {
  997. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  998. }
  999. r_result.insert(option.display, option);
  1000. }
  1001. }
  1002. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  1003. GDScriptCompletionIdentifier ci;
  1004. ci.value = p_value;
  1005. ci.type.is_constant = true;
  1006. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1007. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1008. ci.type.builtin_type = p_value.get_type();
  1009. if (ci.type.builtin_type == Variant::OBJECT) {
  1010. Object *obj = p_value.operator Object *();
  1011. if (!obj) {
  1012. return ci;
  1013. }
  1014. ci.type.native_type = obj->get_class_name();
  1015. Ref<Script> scr = p_value;
  1016. if (scr.is_valid()) {
  1017. ci.type.is_meta_type = true;
  1018. } else {
  1019. ci.type.is_meta_type = false;
  1020. scr = obj->get_script();
  1021. }
  1022. if (scr.is_valid()) {
  1023. ci.type.script_type = scr;
  1024. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1025. ci.type.native_type = scr->get_instance_base_type();
  1026. } else {
  1027. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1028. }
  1029. }
  1030. return ci;
  1031. }
  1032. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  1033. GDScriptCompletionIdentifier ci;
  1034. if (p_property.type == Variant::NIL) {
  1035. // Variant
  1036. return ci;
  1037. }
  1038. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1039. ci.enumeration = p_property.class_name;
  1040. }
  1041. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1042. ci.type.builtin_type = p_property.type;
  1043. if (p_property.type == Variant::OBJECT) {
  1044. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1045. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1046. } else {
  1047. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1048. }
  1049. return ci;
  1050. }
  1051. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1052. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1053. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1054. static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
  1055. if (p_expression) {
  1056. switch (p_expression->type) {
  1057. case GDScriptParser::Node::IDENTIFIER: {
  1058. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1059. if (id->name == p_name) {
  1060. return true;
  1061. }
  1062. } break;
  1063. case GDScriptParser::Node::CAST: {
  1064. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1065. return _is_expression_named_identifier(cn->operand, p_name);
  1066. } break;
  1067. default:
  1068. break;
  1069. }
  1070. }
  1071. return false;
  1072. }
  1073. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1074. bool found = false;
  1075. if (p_expression == nullptr) {
  1076. return false;
  1077. }
  1078. if (p_expression->is_constant) {
  1079. // Already has a value, so just use that.
  1080. r_type = _type_from_variant(p_expression->reduced_value);
  1081. found = true;
  1082. } else {
  1083. switch (p_expression->type) {
  1084. case GDScriptParser::Node::LITERAL: {
  1085. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1086. r_type = _type_from_variant(literal->value);
  1087. found = true;
  1088. } break;
  1089. case GDScriptParser::Node::SELF: {
  1090. if (p_context.current_class) {
  1091. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1092. r_type.type.type_source = GDScriptParser::DataType::INFERRED;
  1093. r_type.type.is_constant = true;
  1094. r_type.type.class_type = p_context.current_class;
  1095. r_type.value = p_context.base;
  1096. found = true;
  1097. }
  1098. } break;
  1099. case GDScriptParser::Node::IDENTIFIER: {
  1100. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1101. found = _guess_identifier_type(p_context, id->name, r_type);
  1102. } break;
  1103. case GDScriptParser::Node::DICTIONARY: {
  1104. // Try to recreate the dictionary.
  1105. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1106. Dictionary d;
  1107. bool full = true;
  1108. for (int i = 0; i < dn->elements.size(); i++) {
  1109. GDScriptCompletionIdentifier key;
  1110. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1111. if (!key.type.is_constant) {
  1112. full = false;
  1113. break;
  1114. }
  1115. GDScriptCompletionIdentifier value;
  1116. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1117. if (!value.type.is_constant) {
  1118. full = false;
  1119. break;
  1120. }
  1121. d[key.value] = value.value;
  1122. } else {
  1123. full = false;
  1124. break;
  1125. }
  1126. } else {
  1127. full = false;
  1128. break;
  1129. }
  1130. }
  1131. if (full) {
  1132. r_type.value = d;
  1133. r_type.type.is_constant = true;
  1134. }
  1135. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1136. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1137. r_type.type.builtin_type = Variant::DICTIONARY;
  1138. found = true;
  1139. } break;
  1140. case GDScriptParser::Node::ARRAY: {
  1141. // Try to recreate the array
  1142. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1143. Array a;
  1144. bool full = true;
  1145. a.resize(an->elements.size());
  1146. for (int i = 0; i < an->elements.size(); i++) {
  1147. GDScriptCompletionIdentifier value;
  1148. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1149. if (value.type.is_constant) {
  1150. a[i] = value.value;
  1151. } else {
  1152. full = false;
  1153. break;
  1154. }
  1155. } else {
  1156. full = false;
  1157. break;
  1158. }
  1159. }
  1160. if (full) {
  1161. // If not fully constant, setting this value is detrimental to the inference.
  1162. r_type.value = a;
  1163. }
  1164. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1165. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1166. r_type.type.builtin_type = Variant::ARRAY;
  1167. found = true;
  1168. } break;
  1169. case GDScriptParser::Node::CAST: {
  1170. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1171. GDScriptCompletionIdentifier value;
  1172. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1173. r_type.type = cn->get_datatype();
  1174. found = true;
  1175. }
  1176. } break;
  1177. case GDScriptParser::Node::CALL: {
  1178. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1179. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1180. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1181. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1182. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1183. found = true;
  1184. break;
  1185. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  1186. MethodInfo mi = GDScriptUtilityFunctions::get_function_info(call->function_name);
  1187. r_type = _type_from_property(mi.return_val);
  1188. found = true;
  1189. break;
  1190. } else {
  1191. GDScriptParser::CompletionContext c = p_context;
  1192. c.current_line = call->start_line;
  1193. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1194. GDScriptCompletionIdentifier base;
  1195. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1196. // Simple call, so base is 'self'.
  1197. if (p_context.current_class) {
  1198. base.type.kind = GDScriptParser::DataType::CLASS;
  1199. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1200. base.type.is_constant = true;
  1201. base.type.class_type = p_context.current_class;
  1202. base.value = p_context.base;
  1203. } else {
  1204. break;
  1205. }
  1206. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1207. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1208. found = false;
  1209. break;
  1210. }
  1211. } else {
  1212. break;
  1213. }
  1214. // Try call if constant methods with constant arguments
  1215. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1216. GDScriptParser::DataType native_type = base.type;
  1217. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1218. native_type = native_type.class_type->base_type;
  1219. }
  1220. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1221. if (native_type.script_type.is_valid()) {
  1222. Ref<Script> parent = native_type.script_type->get_base_script();
  1223. if (parent.is_valid()) {
  1224. native_type.script_type = parent;
  1225. } else {
  1226. native_type.kind = GDScriptParser::DataType::NATIVE;
  1227. native_type.native_type = native_type.script_type->get_instance_base_type();
  1228. if (!ClassDB::class_exists(native_type.native_type)) {
  1229. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1235. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1236. if (mb && mb->is_const()) {
  1237. bool all_is_const = true;
  1238. Vector<Variant> args;
  1239. GDScriptParser::CompletionContext c2 = p_context;
  1240. c2.current_line = call->start_line;
  1241. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1242. GDScriptCompletionIdentifier arg;
  1243. if (!call->arguments[i]->is_constant) {
  1244. all_is_const = false;
  1245. }
  1246. }
  1247. Object *baseptr = base.value;
  1248. if (all_is_const && call->function_name == SNAME("get_node") && ClassDB::is_parent_class(native_type.native_type, SNAME("Node")) && args.size()) {
  1249. String arg1 = args[0];
  1250. if (arg1.begins_with("/root/")) {
  1251. String which = arg1.get_slice("/", 2);
  1252. if (!which.is_empty()) {
  1253. // Try singletons first
  1254. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1255. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1256. found = true;
  1257. } else {
  1258. OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  1259. for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
  1260. String name = E.key();
  1261. if (name == which) {
  1262. String script = E.value().path;
  1263. if (!script.begins_with("res://")) {
  1264. script = "res://" + script;
  1265. }
  1266. if (!script.ends_with(".gd")) {
  1267. // not a script, try find the script anyway,
  1268. // may have some success
  1269. script = script.get_basename() + ".gd";
  1270. }
  1271. if (FileAccess::exists(script)) {
  1272. Error err = OK;
  1273. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1274. if (err == OK) {
  1275. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1276. r_type.type.script_path = script;
  1277. r_type.type.class_type = parser->get_parser()->get_tree();
  1278. r_type.type.is_constant = false;
  1279. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1280. r_type.value = Variant();
  1281. p_context.dependent_parsers.push_back(parser);
  1282. found = true;
  1283. }
  1284. }
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292. if (!found && all_is_const && baseptr) {
  1293. Vector<const Variant *> argptr;
  1294. for (int i = 0; i < args.size(); i++) {
  1295. argptr.push_back(&args[i]);
  1296. }
  1297. Callable::CallError ce;
  1298. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1299. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1300. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1301. r_type = _type_from_variant(ret);
  1302. found = true;
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. }
  1309. if (!found) {
  1310. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1311. }
  1312. }
  1313. } break;
  1314. case GDScriptParser::Node::SUBSCRIPT: {
  1315. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1316. if (subscript->is_attribute) {
  1317. GDScriptParser::CompletionContext c = p_context;
  1318. c.current_line = subscript->start_line;
  1319. GDScriptCompletionIdentifier base;
  1320. if (!_guess_expression_type(c, subscript->base, base)) {
  1321. found = false;
  1322. break;
  1323. }
  1324. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1325. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1326. r_type = _type_from_variant(value);
  1327. found = true;
  1328. break;
  1329. }
  1330. const GDScriptParser::DictionaryNode *dn = nullptr;
  1331. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1332. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1333. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1334. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1335. }
  1336. if (dn) {
  1337. for (int i = 0; i < dn->elements.size(); i++) {
  1338. GDScriptCompletionIdentifier key;
  1339. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1340. continue;
  1341. }
  1342. if (key.value == String(subscript->attribute->name)) {
  1343. r_type.assigned_expression = dn->elements[i].value;
  1344. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. if (!found) {
  1350. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1351. }
  1352. } else {
  1353. if (subscript->index == nullptr) {
  1354. found = false;
  1355. break;
  1356. }
  1357. GDScriptParser::CompletionContext c = p_context;
  1358. c.current_line = subscript->start_line;
  1359. GDScriptCompletionIdentifier base;
  1360. if (!_guess_expression_type(c, subscript->base, base)) {
  1361. found = false;
  1362. break;
  1363. }
  1364. GDScriptCompletionIdentifier index;
  1365. if (!_guess_expression_type(c, subscript->index, index)) {
  1366. found = false;
  1367. break;
  1368. }
  1369. if (base.value.in(index.value)) {
  1370. Variant value = base.value.get(index.value);
  1371. r_type = _type_from_variant(value);
  1372. found = true;
  1373. break;
  1374. }
  1375. // Look if it is a dictionary node.
  1376. const GDScriptParser::DictionaryNode *dn = nullptr;
  1377. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1378. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1379. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1380. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1381. }
  1382. if (dn) {
  1383. for (int i = 0; i < dn->elements.size(); i++) {
  1384. GDScriptCompletionIdentifier key;
  1385. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1386. continue;
  1387. }
  1388. if (key.value == index.value) {
  1389. r_type.assigned_expression = dn->elements[i].value;
  1390. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1391. break;
  1392. }
  1393. }
  1394. }
  1395. // Look if it is an array node.
  1396. if (!found && index.value.is_num()) {
  1397. int idx = index.value;
  1398. const GDScriptParser::ArrayNode *an = nullptr;
  1399. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1400. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1401. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1402. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1403. }
  1404. if (an && idx >= 0 && an->elements.size() > idx) {
  1405. r_type.assigned_expression = an->elements[idx];
  1406. found = _guess_expression_type(c, an->elements[idx], r_type);
  1407. break;
  1408. }
  1409. }
  1410. // Look for valid indexing in other types
  1411. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1412. StringName id = index.value;
  1413. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1414. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1415. Callable::CallError err;
  1416. Variant base_val;
  1417. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1418. bool valid = false;
  1419. Variant res = base_val.get(index.value, &valid);
  1420. if (valid) {
  1421. r_type = _type_from_variant(res);
  1422. r_type.value = Variant();
  1423. r_type.type.is_constant = false;
  1424. found = true;
  1425. }
  1426. }
  1427. }
  1428. } break;
  1429. case GDScriptParser::Node::BINARY_OPERATOR: {
  1430. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1431. if (op->variant_op == Variant::OP_MAX) {
  1432. break;
  1433. }
  1434. GDScriptParser::CompletionContext context = p_context;
  1435. context.current_line = op->start_line;
  1436. GDScriptCompletionIdentifier p1;
  1437. GDScriptCompletionIdentifier p2;
  1438. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1439. found = false;
  1440. break;
  1441. }
  1442. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1443. found = false;
  1444. break;
  1445. }
  1446. Callable::CallError ce;
  1447. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1448. Variant d1;
  1449. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1450. Variant d2;
  1451. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1452. Variant v1 = (v1_use_value) ? p1.value : d1;
  1453. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1454. Variant v2 = (v2_use_value) ? p2.value : d2;
  1455. // avoid potential invalid ops
  1456. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1457. v2 = 1;
  1458. v2_use_value = false;
  1459. }
  1460. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1461. v2 = 1.0;
  1462. v2_use_value = false;
  1463. }
  1464. Variant res;
  1465. bool valid;
  1466. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1467. if (!valid) {
  1468. found = false;
  1469. break;
  1470. }
  1471. r_type = _type_from_variant(res);
  1472. if (!v1_use_value || !v2_use_value) {
  1473. r_type.value = Variant();
  1474. r_type.type.is_constant = false;
  1475. }
  1476. found = true;
  1477. } break;
  1478. default:
  1479. break;
  1480. }
  1481. }
  1482. // It may have found a null, but that's never useful
  1483. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1484. found = false;
  1485. }
  1486. // Check type hint last. For collections we want chance to get the actual value first
  1487. // This way we can detect types from the content of dictionaries and arrays
  1488. if (!found && p_expression->get_datatype().is_hard_type()) {
  1489. r_type.type = p_expression->get_datatype();
  1490. if (!r_type.assigned_expression) {
  1491. r_type.assigned_expression = p_expression;
  1492. }
  1493. found = true;
  1494. }
  1495. return found;
  1496. }
  1497. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1498. // Look in blocks first.
  1499. int last_assign_line = -1;
  1500. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1501. GDScriptParser::DataType id_type;
  1502. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1503. bool is_function_parameter = false;
  1504. if (suite) {
  1505. if (suite->has_local(p_identifier)) {
  1506. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier);
  1507. id_type = local.get_datatype();
  1508. // Check initializer as the first assignment.
  1509. switch (local.type) {
  1510. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1511. if (local.variable->initializer) {
  1512. last_assign_line = local.variable->initializer->end_line;
  1513. last_assigned_expression = local.variable->initializer;
  1514. }
  1515. break;
  1516. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1517. if (local.constant->initializer) {
  1518. last_assign_line = local.constant->initializer->end_line;
  1519. last_assigned_expression = local.constant->initializer;
  1520. }
  1521. break;
  1522. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1523. if (local.parameter->default_value) {
  1524. last_assign_line = local.parameter->default_value->end_line;
  1525. last_assigned_expression = local.parameter->default_value;
  1526. }
  1527. is_function_parameter = true;
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. }
  1533. }
  1534. while (suite) {
  1535. for (int i = 0; i < suite->statements.size(); i++) {
  1536. if (suite->statements[i]->start_line > p_context.current_line) {
  1537. break;
  1538. }
  1539. switch (suite->statements[i]->type) {
  1540. case GDScriptParser::Node::ASSIGNMENT: {
  1541. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1542. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1543. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1544. if (id->name == p_identifier) {
  1545. last_assign_line = assign->assigned_value->end_line;
  1546. last_assigned_expression = assign->assigned_value;
  1547. }
  1548. }
  1549. } break;
  1550. default:
  1551. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1552. break;
  1553. }
  1554. }
  1555. if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::BINARY_OPERATOR && static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition)->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1556. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1557. // Super dirty hack, but very useful.
  1558. // Credit: Zylann.
  1559. // TODO: this could be hacked to detect ANDed conditions too...
  1560. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition);
  1561. if (op->left_operand && op->right_operand && op->left_operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(op->left_operand)->name == p_identifier) {
  1562. // Bingo.
  1563. GDScriptParser::CompletionContext c = p_context;
  1564. c.current_line = op->left_operand->start_line;
  1565. c.current_suite = suite;
  1566. GDScriptCompletionIdentifier is_type;
  1567. if (_guess_expression_type(c, op->right_operand, is_type)) {
  1568. id_type = is_type.type;
  1569. id_type.is_meta_type = false;
  1570. if (last_assign_line < c.current_line) {
  1571. // Override last assignment.
  1572. last_assign_line = c.current_line;
  1573. last_assigned_expression = nullptr;
  1574. }
  1575. }
  1576. }
  1577. }
  1578. suite = suite->parent_block;
  1579. }
  1580. if (last_assigned_expression && last_assign_line != p_context.current_line) {
  1581. GDScriptParser::CompletionContext c = p_context;
  1582. c.current_line = last_assign_line;
  1583. r_type.assigned_expression = last_assigned_expression;
  1584. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1585. return true;
  1586. }
  1587. }
  1588. if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
  1589. // Check if it's override of native function, then we can assume the type from the signature.
  1590. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1591. while (base_type.is_set()) {
  1592. switch (base_type.kind) {
  1593. case GDScriptParser::DataType::CLASS:
  1594. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1595. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1596. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier]];
  1597. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1598. id_type = parameter->get_datatype();
  1599. }
  1600. if (parameter->default_value) {
  1601. GDScriptParser::CompletionContext c = p_context;
  1602. c.current_function = parent_function;
  1603. c.current_class = base_type.class_type;
  1604. c.base = nullptr;
  1605. if (_guess_expression_type(c, parameter->default_value, r_type)) {
  1606. return true;
  1607. }
  1608. }
  1609. }
  1610. base_type = base_type.class_type->base_type;
  1611. break;
  1612. case GDScriptParser::DataType::NATIVE: {
  1613. if (id_type.is_set() && !id_type.is_variant()) {
  1614. base_type = GDScriptParser::DataType();
  1615. break;
  1616. }
  1617. MethodInfo info;
  1618. if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
  1619. for (const PropertyInfo &E : info.arguments) {
  1620. if (E.name == p_identifier) {
  1621. r_type = _type_from_property(E);
  1622. return true;
  1623. }
  1624. }
  1625. }
  1626. base_type = GDScriptParser::DataType();
  1627. } break;
  1628. default:
  1629. break;
  1630. }
  1631. }
  1632. }
  1633. if (id_type.is_set() && !id_type.is_variant()) {
  1634. r_type.type = id_type;
  1635. return true;
  1636. }
  1637. // Check current class (including inheritance).
  1638. if (p_context.current_class) {
  1639. GDScriptCompletionIdentifier base;
  1640. base.value = p_context.base;
  1641. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1642. base.type.kind = GDScriptParser::DataType::CLASS;
  1643. base.type.class_type = p_context.current_class;
  1644. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1645. if (_guess_identifier_type_from_base(p_context, base, p_identifier, r_type)) {
  1646. return true;
  1647. }
  1648. }
  1649. // Check global scripts.
  1650. if (ScriptServer::is_global_class(p_identifier)) {
  1651. String script = ScriptServer::get_global_class_path(p_identifier);
  1652. if (script.to_lower().ends_with(".gd")) {
  1653. Error err = OK;
  1654. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1655. if (err == OK) {
  1656. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1657. r_type.type.script_path = script;
  1658. r_type.type.class_type = parser->get_parser()->get_tree();
  1659. r_type.type.is_constant = false;
  1660. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1661. r_type.value = Variant();
  1662. p_context.dependent_parsers.push_back(parser);
  1663. return true;
  1664. }
  1665. } else {
  1666. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1667. if (scr.is_valid()) {
  1668. r_type = _type_from_variant(scr);
  1669. r_type.type.is_meta_type = true;
  1670. return true;
  1671. }
  1672. }
  1673. return false;
  1674. }
  1675. // Check autoloads.
  1676. if (ProjectSettings::get_singleton()->has_autoload(p_identifier)) {
  1677. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1678. return true;
  1679. }
  1680. // Check ClassDB.
  1681. if (ClassDB::class_exists(p_identifier) && ClassDB::is_class_exposed(p_identifier)) {
  1682. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1683. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1684. r_type.type.native_type = p_identifier;
  1685. r_type.type.is_constant = true;
  1686. r_type.type.is_meta_type = !Engine::get_singleton()->has_singleton(p_identifier);
  1687. r_type.value = Variant();
  1688. }
  1689. return false;
  1690. }
  1691. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1692. GDScriptParser::DataType base_type = p_base.type;
  1693. bool is_static = base_type.is_meta_type;
  1694. while (base_type.is_set()) {
  1695. switch (base_type.kind) {
  1696. case GDScriptParser::DataType::CLASS:
  1697. if (base_type.class_type->has_member(p_identifier)) {
  1698. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1699. switch (member.type) {
  1700. case GDScriptParser::ClassNode::Member::CONSTANT:
  1701. r_type.type = member.constant->get_datatype();
  1702. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1703. r_type.value = member.constant->initializer->reduced_value;
  1704. }
  1705. return true;
  1706. case GDScriptParser::ClassNode::Member::VARIABLE:
  1707. if (!is_static) {
  1708. if (member.variable->initializer) {
  1709. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1710. if (init->is_constant) {
  1711. r_type.value = init->reduced_value;
  1712. r_type = _type_from_variant(init->reduced_value);
  1713. return true;
  1714. } else if (init->start_line == p_context.current_line) {
  1715. return false;
  1716. // Detects if variable is assigned to itself
  1717. } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
  1718. if (member.variable->initializer->get_datatype().is_set()) {
  1719. r_type.type = member.variable->initializer->get_datatype();
  1720. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1721. r_type.type = member.variable->get_datatype();
  1722. }
  1723. return true;
  1724. } else if (_guess_expression_type(p_context, init, r_type)) {
  1725. return true;
  1726. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1727. r_type.type = init->get_datatype();
  1728. return true;
  1729. }
  1730. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1731. r_type.type = member.variable->get_datatype();
  1732. return true;
  1733. }
  1734. }
  1735. // TODO: Check assignments in constructor.
  1736. return false;
  1737. case GDScriptParser::ClassNode::Member::ENUM:
  1738. r_type.type = member.m_enum->get_datatype();
  1739. r_type.enumeration = member.m_enum->identifier->name;
  1740. return true;
  1741. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1742. r_type = _type_from_variant(member.enum_value.value);
  1743. return true;
  1744. case GDScriptParser::ClassNode::Member::SIGNAL:
  1745. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1746. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1747. r_type.type.builtin_type = Variant::SIGNAL;
  1748. return true;
  1749. case GDScriptParser::ClassNode::Member::FUNCTION:
  1750. if (is_static && !member.function->is_static) {
  1751. return false;
  1752. }
  1753. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1754. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1755. r_type.type.builtin_type = Variant::CALLABLE;
  1756. return true;
  1757. case GDScriptParser::ClassNode::Member::CLASS:
  1758. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1759. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1760. r_type.type.class_type = member.m_class;
  1761. return true;
  1762. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1763. return false; // Unreachable.
  1764. }
  1765. return false;
  1766. }
  1767. base_type = base_type.class_type->base_type;
  1768. break;
  1769. case GDScriptParser::DataType::SCRIPT: {
  1770. Ref<Script> scr = base_type.script_type;
  1771. if (scr.is_valid()) {
  1772. Map<StringName, Variant> constants;
  1773. scr->get_constants(&constants);
  1774. if (constants.has(p_identifier)) {
  1775. r_type = _type_from_variant(constants[p_identifier]);
  1776. return true;
  1777. }
  1778. if (!is_static) {
  1779. List<PropertyInfo> members;
  1780. scr->get_script_property_list(&members);
  1781. for (const PropertyInfo &prop : members) {
  1782. if (prop.name == p_identifier) {
  1783. r_type = _type_from_property(prop);
  1784. return true;
  1785. }
  1786. }
  1787. }
  1788. Ref<Script> parent = scr->get_base_script();
  1789. if (parent.is_valid()) {
  1790. base_type.script_type = parent;
  1791. } else {
  1792. base_type.kind = GDScriptParser::DataType::NATIVE;
  1793. base_type.native_type = scr->get_instance_base_type();
  1794. }
  1795. } else {
  1796. return false;
  1797. }
  1798. } break;
  1799. case GDScriptParser::DataType::NATIVE: {
  1800. StringName class_name = base_type.native_type;
  1801. if (!ClassDB::class_exists(class_name)) {
  1802. return false;
  1803. }
  1804. // Skip constants since they're all integers. Type does not matter because int has no members.
  1805. PropertyInfo prop;
  1806. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  1807. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1808. if (getter != StringName()) {
  1809. MethodBind *g = ClassDB::get_method(class_name, getter);
  1810. if (g) {
  1811. r_type = _type_from_property(g->get_return_info());
  1812. return true;
  1813. }
  1814. } else {
  1815. r_type = _type_from_property(prop);
  1816. return true;
  1817. }
  1818. }
  1819. return false;
  1820. } break;
  1821. case GDScriptParser::DataType::BUILTIN: {
  1822. Callable::CallError err;
  1823. Variant tmp;
  1824. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1825. if (err.error != Callable::CallError::CALL_OK) {
  1826. return false;
  1827. }
  1828. bool valid = false;
  1829. Variant res = tmp.get(p_identifier, &valid);
  1830. if (valid) {
  1831. r_type = _type_from_variant(res);
  1832. r_type.value = Variant();
  1833. r_type.type.is_constant = false;
  1834. return true;
  1835. }
  1836. return false;
  1837. } break;
  1838. default: {
  1839. return false;
  1840. } break;
  1841. }
  1842. }
  1843. return false;
  1844. }
  1845. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  1846. if (!p_context.current_suite) {
  1847. return;
  1848. }
  1849. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  1850. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  1851. break;
  1852. }
  1853. GDScriptParser::CompletionContext c = p_context;
  1854. switch (p_context.current_suite->statements[i]->type) {
  1855. case GDScriptParser::Node::FOR:
  1856. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  1857. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1858. break;
  1859. case GDScriptParser::Node::WHILE:
  1860. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  1861. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1862. break;
  1863. case GDScriptParser::Node::IF: {
  1864. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  1865. c.current_suite = _if->true_block;
  1866. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1867. if (_if->false_block) {
  1868. c.current_suite = _if->false_block;
  1869. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1870. }
  1871. } break;
  1872. case GDScriptParser::Node::MATCH: {
  1873. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  1874. for (int j = 0; j < match->branches.size(); j++) {
  1875. c.current_suite = match->branches[j]->block;
  1876. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1877. }
  1878. } break;
  1879. case GDScriptParser::Node::RETURN: {
  1880. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  1881. if (ret->return_value) {
  1882. if (ret->start_line > r_last_return_line) {
  1883. r_last_return_line = ret->start_line;
  1884. *r_last_returned_value = ret->return_value;
  1885. }
  1886. }
  1887. } break;
  1888. default:
  1889. break;
  1890. }
  1891. }
  1892. }
  1893. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1894. GDScriptParser::DataType base_type = p_base.type;
  1895. bool is_static = base_type.is_meta_type;
  1896. if (is_static && p_method == SNAME("new")) {
  1897. r_type.type = base_type;
  1898. r_type.type.is_meta_type = false;
  1899. r_type.type.is_constant = false;
  1900. return true;
  1901. }
  1902. while (base_type.is_set() && !base_type.is_variant()) {
  1903. switch (base_type.kind) {
  1904. case GDScriptParser::DataType::CLASS:
  1905. if (base_type.class_type->has_function(p_method)) {
  1906. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  1907. if (!is_static || method->is_static) {
  1908. int last_return_line = -1;
  1909. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  1910. GDScriptParser::CompletionContext c = p_context;
  1911. c.current_class = base_type.class_type;
  1912. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  1913. c.current_suite = method->body;
  1914. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  1915. if (last_returned_value) {
  1916. c.current_line = c.current_suite->end_line;
  1917. if (_guess_expression_type(c, last_returned_value, r_type)) {
  1918. return true;
  1919. }
  1920. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  1921. r_type.type = method->get_datatype();
  1922. return true;
  1923. }
  1924. }
  1925. }
  1926. }
  1927. base_type = base_type.class_type->base_type;
  1928. break;
  1929. case GDScriptParser::DataType::SCRIPT: {
  1930. Ref<Script> scr = base_type.script_type;
  1931. if (scr.is_valid()) {
  1932. List<MethodInfo> methods;
  1933. scr->get_script_method_list(&methods);
  1934. for (const MethodInfo &mi : methods) {
  1935. if (mi.name == p_method) {
  1936. r_type = _type_from_property(mi.return_val);
  1937. return true;
  1938. }
  1939. }
  1940. Ref<Script> base_script = scr->get_base_script();
  1941. if (base_script.is_valid()) {
  1942. base_type.script_type = base_script;
  1943. } else {
  1944. base_type.kind = GDScriptParser::DataType::NATIVE;
  1945. base_type.native_type = scr->get_instance_base_type();
  1946. }
  1947. } else {
  1948. return false;
  1949. }
  1950. } break;
  1951. case GDScriptParser::DataType::NATIVE: {
  1952. if (!ClassDB::class_exists(base_type.native_type)) {
  1953. return false;
  1954. }
  1955. MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
  1956. if (mb) {
  1957. r_type = _type_from_property(mb->get_return_info());
  1958. return true;
  1959. }
  1960. return false;
  1961. } break;
  1962. case GDScriptParser::DataType::BUILTIN: {
  1963. Callable::CallError err;
  1964. Variant tmp;
  1965. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1966. if (err.error != Callable::CallError::CALL_OK) {
  1967. return false;
  1968. }
  1969. List<MethodInfo> methods;
  1970. tmp.get_method_list(&methods);
  1971. for (const MethodInfo &mi : methods) {
  1972. if (mi.name == p_method) {
  1973. r_type = _type_from_property(mi.return_val);
  1974. return true;
  1975. }
  1976. }
  1977. return false;
  1978. } break;
  1979. default: {
  1980. return false;
  1981. }
  1982. }
  1983. }
  1984. return false;
  1985. }
  1986. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, Map<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  1987. if (!p_enum_hint.contains(".")) {
  1988. // Global constant or in the current class.
  1989. StringName current_enum = p_enum_hint;
  1990. if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) {
  1991. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  1992. for (int i = 0; i < _enum->values.size(); i++) {
  1993. ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  1994. r_result.insert(option.display, option);
  1995. }
  1996. } else {
  1997. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  1998. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  1999. ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2000. r_result.insert(option.display, option);
  2001. }
  2002. }
  2003. }
  2004. } else {
  2005. String class_name = p_enum_hint.get_slice(".", 0);
  2006. String enum_name = p_enum_hint.get_slice(".", 1);
  2007. if (!ClassDB::class_exists(class_name)) {
  2008. return;
  2009. }
  2010. List<StringName> enum_constants;
  2011. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  2012. for (const StringName &E : enum_constants) {
  2013. String candidate = class_name + "." + E;
  2014. ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2015. r_result.insert(option.display, option);
  2016. }
  2017. }
  2018. }
  2019. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
  2020. Variant base = p_base.value;
  2021. GDScriptParser::DataType base_type = p_base.type;
  2022. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2023. while (base_type.is_set() && !base_type.is_variant()) {
  2024. switch (base_type.kind) {
  2025. case GDScriptParser::DataType::CLASS: {
  2026. if (base_type.class_type->has_member(p_method)) {
  2027. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  2028. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2029. r_arghint = _make_arguments_hint(member.function, p_argidx);
  2030. return;
  2031. }
  2032. }
  2033. base_type = base_type.class_type->base_type;
  2034. } break;
  2035. case GDScriptParser::DataType::NATIVE: {
  2036. StringName class_name = base_type.native_type;
  2037. if (!ClassDB::class_exists(class_name)) {
  2038. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2039. break;
  2040. }
  2041. MethodInfo info;
  2042. int method_args = 0;
  2043. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  2044. method_args = info.arguments.size();
  2045. if (base.get_type() == Variant::OBJECT) {
  2046. Object *obj = base.operator Object *();
  2047. if (obj) {
  2048. List<String> options;
  2049. obj->get_argument_options(p_method, p_argidx, &options);
  2050. for (String &opt : options) {
  2051. if (opt.is_quoted()) {
  2052. opt = opt.unquote().quote(quote_style); // Handle user preference.
  2053. }
  2054. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2055. r_result.insert(option.display, option);
  2056. }
  2057. }
  2058. }
  2059. if (p_argidx < method_args) {
  2060. PropertyInfo arg_info = info.arguments[p_argidx];
  2061. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2062. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  2063. }
  2064. }
  2065. r_arghint = _make_arguments_hint(info, p_argidx);
  2066. }
  2067. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (p_method == SNAME("get_node") || p_method == SNAME("has_node"))) {
  2068. // Get autoloads
  2069. List<PropertyInfo> props;
  2070. ProjectSettings::get_singleton()->get_property_list(&props);
  2071. for (const PropertyInfo &E : props) {
  2072. String s = E.name;
  2073. if (!s.begins_with("autoload/")) {
  2074. continue;
  2075. }
  2076. String name = s.get_slice("/", 1);
  2077. ScriptLanguage::CodeCompletionOption option("/root/" + name, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2078. option.insert_text = option.display.quote(quote_style);
  2079. r_result.insert(option.display, option);
  2080. }
  2081. }
  2082. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && p_method.operator String().contains("action")) {
  2083. // Get input actions
  2084. List<PropertyInfo> props;
  2085. ProjectSettings::get_singleton()->get_property_list(&props);
  2086. for (const PropertyInfo &E : props) {
  2087. String s = E.name;
  2088. if (!s.begins_with("input/")) {
  2089. continue;
  2090. }
  2091. String name = s.get_slice("/", 1);
  2092. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2093. option.insert_text = option.display.quote(quote_style);
  2094. r_result.insert(option.display, option);
  2095. }
  2096. }
  2097. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2098. } break;
  2099. case GDScriptParser::DataType::BUILTIN: {
  2100. if (base.get_type() == Variant::NIL) {
  2101. Callable::CallError err;
  2102. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2103. if (err.error != Callable::CallError::CALL_OK) {
  2104. return;
  2105. }
  2106. }
  2107. List<MethodInfo> methods;
  2108. base.get_method_list(&methods);
  2109. for (const MethodInfo &E : methods) {
  2110. if (E.name == p_method) {
  2111. r_arghint = _make_arguments_hint(E, p_argidx);
  2112. return;
  2113. }
  2114. }
  2115. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2116. } break;
  2117. default: {
  2118. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2119. } break;
  2120. }
  2121. }
  2122. }
  2123. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, Map<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2124. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2125. if (p_argidx == 0 && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2126. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2127. }
  2128. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2129. r_arghint = _make_arguments_hint(mi, p_argidx);
  2130. return;
  2131. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2132. return;
  2133. }
  2134. Variant base;
  2135. GDScriptParser::DataType base_type;
  2136. bool _static = false;
  2137. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2138. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  2139. GDScriptCompletionIdentifier connect_base;
  2140. if (Variant::has_utility_function(call->function_name)) {
  2141. MethodInfo info = Variant::get_utility_function_info(call->function_name);
  2142. r_arghint = _make_arguments_hint(info, p_argidx);
  2143. return;
  2144. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  2145. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  2146. r_arghint = _make_arguments_hint(info, p_argidx);
  2147. return;
  2148. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2149. // Complete constructor
  2150. List<MethodInfo> constructors;
  2151. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2152. int i = 0;
  2153. for (const MethodInfo &E : constructors) {
  2154. if (p_argidx >= E.arguments.size()) {
  2155. continue;
  2156. }
  2157. if (i > 0) {
  2158. r_arghint += "\n";
  2159. }
  2160. r_arghint += _make_arguments_hint(E, p_argidx);
  2161. i++;
  2162. }
  2163. return;
  2164. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2165. base = p_context.base;
  2166. if (p_context.current_class) {
  2167. base_type = p_context.current_class->get_datatype();
  2168. _static = !p_context.current_function || p_context.current_function->is_static;
  2169. }
  2170. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2171. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2172. if (subscript->is_attribute) {
  2173. GDScriptCompletionIdentifier ci;
  2174. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2175. base_type = ci.type;
  2176. base = ci.value;
  2177. } else {
  2178. return;
  2179. }
  2180. _static = base_type.is_meta_type;
  2181. }
  2182. } else {
  2183. return;
  2184. }
  2185. GDScriptCompletionIdentifier ci;
  2186. ci.type = base_type;
  2187. ci.value = base;
  2188. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2189. r_forced = r_result.size() > 0;
  2190. }
  2191. ::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2192. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2193. GDScriptParser parser;
  2194. GDScriptAnalyzer analyzer(&parser);
  2195. parser.parse(p_code, p_path, true);
  2196. analyzer.analyze();
  2197. r_forced = false;
  2198. Map<String, ScriptLanguage::CodeCompletionOption> options;
  2199. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2200. completion_context.base = p_owner;
  2201. bool is_function = false;
  2202. switch (completion_context.type) {
  2203. case GDScriptParser::COMPLETION_NONE:
  2204. break;
  2205. case GDScriptParser::COMPLETION_ANNOTATION: {
  2206. List<MethodInfo> annotations;
  2207. parser.get_annotation_list(&annotations);
  2208. for (const MethodInfo &E : annotations) {
  2209. ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2210. if (E.arguments.size() > 0) {
  2211. option.insert_text += "(";
  2212. }
  2213. options.insert(option.display, option);
  2214. }
  2215. r_forced = true;
  2216. } break;
  2217. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2218. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2219. break;
  2220. }
  2221. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2222. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2223. r_forced = true;
  2224. } break;
  2225. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2226. List<StringName> constants;
  2227. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2228. for (const StringName &E : constants) {
  2229. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2230. bool valid = false;
  2231. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
  2232. if (valid) {
  2233. option.default_value = default_value;
  2234. }
  2235. options.insert(option.display, option);
  2236. }
  2237. } break;
  2238. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2239. _list_available_types(true, completion_context, options);
  2240. r_forced = true;
  2241. } break;
  2242. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2243. ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2244. options.insert(option.display, option);
  2245. }
  2246. [[fallthrough]];
  2247. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2248. _list_available_types(false, completion_context, options);
  2249. r_forced = true;
  2250. } break;
  2251. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2252. _list_available_types(false, completion_context, options);
  2253. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2254. options.insert(get.display, get);
  2255. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2256. options.insert(set.display, set);
  2257. r_forced = true;
  2258. } break;
  2259. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2260. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2261. options.insert(get.display, get);
  2262. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2263. options.insert(set.display, set);
  2264. r_forced = true;
  2265. } break;
  2266. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2267. if (!completion_context.current_class) {
  2268. break;
  2269. }
  2270. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2271. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2272. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2273. continue;
  2274. }
  2275. if (member.function->is_static) {
  2276. continue;
  2277. }
  2278. ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2279. options.insert(option.display, option);
  2280. }
  2281. r_forced = true;
  2282. } break;
  2283. case GDScriptParser::COMPLETION_ASSIGN: {
  2284. GDScriptCompletionIdentifier type;
  2285. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2286. break;
  2287. }
  2288. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2289. _find_identifiers(completion_context, false, options, 0);
  2290. r_forced = true;
  2291. break;
  2292. }
  2293. if (!type.enumeration.is_empty()) {
  2294. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2295. r_forced = options.size() > 0;
  2296. } else {
  2297. _find_identifiers(completion_context, false, options, 0);
  2298. r_forced = true;
  2299. }
  2300. } break;
  2301. case GDScriptParser::COMPLETION_METHOD:
  2302. is_function = true;
  2303. [[fallthrough]];
  2304. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2305. _find_identifiers(completion_context, is_function, options, 0);
  2306. } break;
  2307. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2308. is_function = true;
  2309. [[fallthrough]];
  2310. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2311. r_forced = true;
  2312. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2313. if (attr->base) {
  2314. GDScriptCompletionIdentifier base;
  2315. if (!_guess_expression_type(completion_context, attr->base, base)) {
  2316. break;
  2317. }
  2318. _find_identifiers_in_base(base, is_function, options, 0);
  2319. }
  2320. } break;
  2321. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2322. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2323. GDScriptCompletionIdentifier base;
  2324. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2325. break;
  2326. }
  2327. GDScriptParser::CompletionContext c = completion_context;
  2328. c.current_function = nullptr;
  2329. c.current_suite = nullptr;
  2330. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2331. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2332. c.current_class = base.type.class_type;
  2333. } else {
  2334. c.current_class = nullptr;
  2335. }
  2336. _find_identifiers_in_base(base, false, options, 0);
  2337. } break;
  2338. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2339. if (!completion_context.current_class) {
  2340. break;
  2341. }
  2342. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2343. bool found = true;
  2344. GDScriptCompletionIdentifier base;
  2345. base.type.kind = GDScriptParser::DataType::CLASS;
  2346. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2347. base.type.is_constant = true;
  2348. base.type.class_type = completion_context.current_class;
  2349. base.value = completion_context.base;
  2350. for (int i = 0; i < completion_context.current_argument; i++) {
  2351. GDScriptCompletionIdentifier ci;
  2352. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2353. found = false;
  2354. break;
  2355. }
  2356. base = ci;
  2357. }
  2358. // TODO: Improve this to only list types.
  2359. if (found) {
  2360. _find_identifiers_in_base(base, false, options, 0);
  2361. }
  2362. r_forced = true;
  2363. } break;
  2364. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2365. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2366. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2367. r_forced = true;
  2368. }
  2369. } break;
  2370. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2371. if (!completion_context.node) {
  2372. break;
  2373. }
  2374. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2375. } break;
  2376. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2377. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2378. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2379. switch (native_type.kind) {
  2380. case GDScriptParser::DataType::CLASS: {
  2381. native_type = native_type.class_type->base_type;
  2382. } break;
  2383. default: {
  2384. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2385. } break;
  2386. }
  2387. }
  2388. if (!native_type.is_set()) {
  2389. break;
  2390. }
  2391. StringName class_name = native_type.native_type;
  2392. if (!ClassDB::class_exists(class_name)) {
  2393. break;
  2394. }
  2395. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2396. List<MethodInfo> virtual_methods;
  2397. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2398. for (const MethodInfo &mi : virtual_methods) {
  2399. String method_hint = mi.name;
  2400. if (method_hint.contains(":")) {
  2401. method_hint = method_hint.get_slice(":", 0);
  2402. }
  2403. method_hint += "(";
  2404. if (mi.arguments.size()) {
  2405. for (int i = 0; i < mi.arguments.size(); i++) {
  2406. if (i > 0) {
  2407. method_hint += ", ";
  2408. }
  2409. String arg = mi.arguments[i].name;
  2410. if (arg.contains(":")) {
  2411. arg = arg.substr(0, arg.find(":"));
  2412. }
  2413. method_hint += arg;
  2414. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2415. method_hint += ": ";
  2416. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2417. method_hint += mi.arguments[i].class_name.operator String();
  2418. } else {
  2419. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2420. }
  2421. }
  2422. }
  2423. }
  2424. method_hint += ")";
  2425. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2426. method_hint += " -> ";
  2427. if (mi.return_val.type == Variant::NIL) {
  2428. method_hint += "void";
  2429. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2430. method_hint += mi.return_val.class_name.operator String();
  2431. } else {
  2432. method_hint += Variant::get_type_name(mi.return_val.type);
  2433. }
  2434. }
  2435. method_hint += ":";
  2436. ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2437. options.insert(option.display, option);
  2438. }
  2439. } break;
  2440. case GDScriptParser::COMPLETION_GET_NODE: {
  2441. // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
  2442. if (p_owner) {
  2443. List<String> opts;
  2444. p_owner->get_argument_options("get_node", 0, &opts);
  2445. for (const String &E : opts) {
  2446. r_forced = true;
  2447. String opt = E.strip_edges();
  2448. if (opt.is_quoted()) {
  2449. // Remove quotes so that we can handle user preferred quote style,
  2450. // or handle NodePaths which are valid identifiers and don't need quotes.
  2451. opt = opt.unquote();
  2452. }
  2453. // The path needs quotes if it's not a valid identifier (with an exception
  2454. // for "/" as path separator, which also doesn't require quotes).
  2455. if (!opt.replace("/", "_").is_valid_identifier()) {
  2456. opt = opt.quote(quote_style); // Handle user preference.
  2457. }
  2458. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2459. options.insert(option.display, option);
  2460. }
  2461. // Get autoloads.
  2462. OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  2463. for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
  2464. String path = "/root/" + E.key();
  2465. ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2466. options.insert(option.display, option);
  2467. }
  2468. }
  2469. } break;
  2470. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2471. if (!completion_context.current_class) {
  2472. break;
  2473. }
  2474. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2475. } break;
  2476. }
  2477. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
  2478. r_options->push_back(E.value);
  2479. }
  2480. return OK;
  2481. }
  2482. #else
  2483. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2484. return OK;
  2485. }
  2486. #endif
  2487. //////// END COMPLETION //////////
  2488. String GDScriptLanguage::_get_indentation() const {
  2489. #ifdef TOOLS_ENABLED
  2490. if (Engine::get_singleton()->is_editor_hint()) {
  2491. bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
  2492. if (use_space_indentation) {
  2493. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  2494. String space_indent = "";
  2495. for (int i = 0; i < indent_size; i++) {
  2496. space_indent += " ";
  2497. }
  2498. return space_indent;
  2499. }
  2500. }
  2501. #endif
  2502. return "\t";
  2503. }
  2504. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2505. String indent = _get_indentation();
  2506. Vector<String> lines = p_code.split("\n");
  2507. List<int> indent_stack;
  2508. for (int i = 0; i < lines.size(); i++) {
  2509. String l = lines[i];
  2510. int tc = 0;
  2511. for (int j = 0; j < l.length(); j++) {
  2512. if (l[j] == ' ' || l[j] == '\t') {
  2513. tc++;
  2514. } else {
  2515. break;
  2516. }
  2517. }
  2518. String st = l.substr(tc, l.length()).strip_edges();
  2519. if (st.is_empty() || st.begins_with("#")) {
  2520. continue; //ignore!
  2521. }
  2522. int ilevel = 0;
  2523. if (indent_stack.size()) {
  2524. ilevel = indent_stack.back()->get();
  2525. }
  2526. if (tc > ilevel) {
  2527. indent_stack.push_back(tc);
  2528. } else if (tc < ilevel) {
  2529. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2530. indent_stack.pop_back();
  2531. }
  2532. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2533. indent_stack.push_back(tc); // this is not right but gets the job done
  2534. }
  2535. }
  2536. if (i >= p_from_line) {
  2537. l = "";
  2538. for (int j = 0; j < indent_stack.size(); j++) {
  2539. l += indent;
  2540. }
  2541. l += st;
  2542. } else if (i > p_to_line) {
  2543. break;
  2544. }
  2545. lines.write[i] = l;
  2546. }
  2547. p_code = "";
  2548. for (int i = 0; i < lines.size(); i++) {
  2549. if (i > 0) {
  2550. p_code += "\n";
  2551. }
  2552. p_code += lines[i];
  2553. }
  2554. }
  2555. #ifdef TOOLS_ENABLED
  2556. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2557. GDScriptParser::DataType base_type = p_base;
  2558. while (base_type.is_set()) {
  2559. switch (base_type.kind) {
  2560. case GDScriptParser::DataType::CLASS: {
  2561. if (base_type.class_type) {
  2562. if (base_type.class_type->has_member(p_symbol)) {
  2563. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2564. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2565. r_result.class_path = base_type.script_path;
  2566. r_result.script = GDScriptCache::get_shallow_script(r_result.class_path);
  2567. return OK;
  2568. }
  2569. base_type = base_type.class_type->base_type;
  2570. }
  2571. } break;
  2572. case GDScriptParser::DataType::SCRIPT: {
  2573. Ref<Script> scr = base_type.script_type;
  2574. if (scr.is_valid()) {
  2575. int line = scr->get_member_line(p_symbol);
  2576. if (line >= 0) {
  2577. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2578. r_result.location = line;
  2579. r_result.script = scr;
  2580. return OK;
  2581. }
  2582. Ref<Script> base_script = scr->get_base_script();
  2583. if (base_script.is_valid()) {
  2584. base_type.script_type = base_script;
  2585. } else {
  2586. base_type.kind = GDScriptParser::DataType::NATIVE;
  2587. base_type.native_type = scr->get_instance_base_type();
  2588. }
  2589. } else {
  2590. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2591. }
  2592. } break;
  2593. case GDScriptParser::DataType::NATIVE: {
  2594. StringName class_name = base_type.native_type;
  2595. if (!ClassDB::class_exists(class_name)) {
  2596. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2597. break;
  2598. }
  2599. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2600. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2601. r_result.class_name = base_type.native_type;
  2602. r_result.class_member = p_symbol;
  2603. return OK;
  2604. }
  2605. List<MethodInfo> virtual_methods;
  2606. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2607. for (const MethodInfo &E : virtual_methods) {
  2608. if (E.name == p_symbol) {
  2609. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2610. r_result.class_name = base_type.native_type;
  2611. r_result.class_member = p_symbol;
  2612. return OK;
  2613. }
  2614. }
  2615. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2616. if (enum_name != StringName()) {
  2617. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2618. r_result.class_name = base_type.native_type;
  2619. r_result.class_member = enum_name;
  2620. return OK;
  2621. }
  2622. List<String> constants;
  2623. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2624. for (const String &E : constants) {
  2625. if (E == p_symbol) {
  2626. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2627. r_result.class_name = base_type.native_type;
  2628. r_result.class_member = p_symbol;
  2629. return OK;
  2630. }
  2631. }
  2632. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2633. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2634. r_result.class_name = base_type.native_type;
  2635. r_result.class_member = p_symbol;
  2636. return OK;
  2637. }
  2638. StringName parent = ClassDB::get_parent_class(class_name);
  2639. if (parent != StringName()) {
  2640. base_type.native_type = parent;
  2641. } else {
  2642. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2643. }
  2644. } break;
  2645. case GDScriptParser::DataType::BUILTIN: {
  2646. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2647. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2648. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2649. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2650. r_result.class_member = p_symbol;
  2651. return OK;
  2652. }
  2653. Variant v;
  2654. REF v_ref;
  2655. if (base_type.builtin_type == Variant::OBJECT) {
  2656. v_ref.instantiate();
  2657. v = v_ref;
  2658. } else {
  2659. Callable::CallError err;
  2660. Variant::construct(base_type.builtin_type, v, nullptr, 0, err);
  2661. if (err.error != Callable::CallError::CALL_OK) {
  2662. break;
  2663. }
  2664. }
  2665. if (v.has_method(p_symbol)) {
  2666. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2667. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2668. r_result.class_member = p_symbol;
  2669. return OK;
  2670. }
  2671. bool valid = false;
  2672. v.get(p_symbol, &valid);
  2673. if (valid) {
  2674. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2675. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2676. r_result.class_member = p_symbol;
  2677. return OK;
  2678. }
  2679. } break;
  2680. default: {
  2681. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2682. } break;
  2683. }
  2684. }
  2685. return ERR_CANT_RESOLVE;
  2686. }
  2687. ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2688. // Before parsing, try the usual stuff
  2689. if (ClassDB::class_exists(p_symbol)) {
  2690. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2691. r_result.class_name = p_symbol;
  2692. return OK;
  2693. }
  2694. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2695. Variant::Type t = Variant::Type(i);
  2696. if (Variant::get_type_name(t) == p_symbol) {
  2697. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2698. r_result.class_name = Variant::get_type_name(t);
  2699. return OK;
  2700. }
  2701. }
  2702. if (GDScriptUtilityFunctions::function_exists(p_symbol)) {
  2703. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2704. r_result.class_name = "@GDScript";
  2705. r_result.class_member = p_symbol;
  2706. return OK;
  2707. }
  2708. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2709. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2710. r_result.class_name = "@GDScript";
  2711. r_result.class_member = p_symbol;
  2712. return OK;
  2713. }
  2714. GDScriptParser parser;
  2715. parser.parse(p_code, p_path, true);
  2716. GDScriptAnalyzer analyzer(&parser);
  2717. analyzer.analyze();
  2718. GDScriptParser::CompletionContext context = parser.get_completion_context();
  2719. if (context.current_class && context.current_class->extends.size() > 0) {
  2720. bool success = false;
  2721. ClassDB::get_integer_constant(context.current_class->extends[0], p_symbol, &success);
  2722. if (success) {
  2723. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2724. r_result.class_name = context.current_class->extends[0];
  2725. r_result.class_member = p_symbol;
  2726. return OK;
  2727. }
  2728. }
  2729. bool is_function = false;
  2730. switch (context.type) {
  2731. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2732. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2733. r_result.class_name = Variant::get_type_name(context.builtin_type);
  2734. r_result.class_member = p_symbol;
  2735. return OK;
  2736. } break;
  2737. case GDScriptParser::COMPLETION_SUPER_METHOD:
  2738. case GDScriptParser::COMPLETION_METHOD: {
  2739. is_function = true;
  2740. [[fallthrough]];
  2741. }
  2742. case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
  2743. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2744. GDScriptParser::DataType base_type;
  2745. if (context.current_class) {
  2746. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  2747. base_type = context.current_class->get_datatype();
  2748. } else {
  2749. base_type = context.current_class->base_type;
  2750. }
  2751. } else {
  2752. break;
  2753. }
  2754. if (!is_function && context.current_suite) {
  2755. // Lookup local variables.
  2756. const GDScriptParser::SuiteNode *suite = context.current_suite;
  2757. while (suite) {
  2758. if (suite->has_local(p_symbol)) {
  2759. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2760. r_result.location = suite->get_local(p_symbol).start_line;
  2761. return OK;
  2762. }
  2763. suite = suite->parent_block;
  2764. }
  2765. }
  2766. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2767. return OK;
  2768. }
  2769. if (!is_function) {
  2770. // Guess in autoloads as singletons.
  2771. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  2772. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  2773. if (autoload.is_singleton) {
  2774. String script = autoload.path;
  2775. if (!script.ends_with(".gd")) {
  2776. // Not a script, try find the script anyway,
  2777. // may have some success.
  2778. script = script.get_basename() + ".gd";
  2779. }
  2780. if (FileAccess::exists(script)) {
  2781. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2782. r_result.location = 0;
  2783. r_result.script = ResourceLoader::load(script);
  2784. return OK;
  2785. }
  2786. }
  2787. }
  2788. // Global.
  2789. Map<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2790. if (classes.has(p_symbol)) {
  2791. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2792. if (value.get_type() == Variant::OBJECT) {
  2793. Object *obj = value;
  2794. if (obj) {
  2795. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2796. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2797. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2798. } else {
  2799. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2800. r_result.class_name = obj->get_class();
  2801. }
  2802. // proxy class remove the underscore.
  2803. if (r_result.class_name.begins_with("_")) {
  2804. r_result.class_name = r_result.class_name.substr(1);
  2805. }
  2806. return OK;
  2807. }
  2808. } else {
  2809. /*
  2810. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2811. // We cannot determine the exact nature of the identifier here
  2812. // Otherwise these codes would work
  2813. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2814. if (enumName != nullptr) {
  2815. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2816. r_result.class_name = "@GlobalScope";
  2817. r_result.class_member = enumName;
  2818. return OK;
  2819. }
  2820. else {
  2821. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2822. r_result.class_name = "@GlobalScope";
  2823. r_result.class_member = p_symbol;
  2824. return OK;
  2825. }*/
  2826. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  2827. r_result.class_name = "@GlobalScope";
  2828. r_result.class_member = p_symbol;
  2829. return OK;
  2830. }
  2831. } else {
  2832. List<StringName> utility_functions;
  2833. Variant::get_utility_function_list(&utility_functions);
  2834. if (utility_functions.find(p_symbol) != nullptr) {
  2835. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  2836. r_result.class_name = "@GlobalScope";
  2837. r_result.class_member = p_symbol;
  2838. return OK;
  2839. }
  2840. }
  2841. }
  2842. } break;
  2843. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  2844. is_function = true;
  2845. [[fallthrough]];
  2846. }
  2847. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2848. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  2849. break;
  2850. }
  2851. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  2852. if (!subscript->is_attribute) {
  2853. break;
  2854. }
  2855. GDScriptCompletionIdentifier base;
  2856. if (!_guess_expression_type(context, subscript->base, base)) {
  2857. break;
  2858. }
  2859. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  2860. return OK;
  2861. }
  2862. } break;
  2863. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2864. GDScriptParser::DataType base_type = context.current_class->base_type;
  2865. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2866. return OK;
  2867. }
  2868. } break;
  2869. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  2870. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2871. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  2872. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  2873. return OK;
  2874. }
  2875. } break;
  2876. default: {
  2877. }
  2878. }
  2879. return ERR_CANT_RESOLVE;
  2880. }
  2881. #endif