gdscript_editor.cpp 120 KB

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