gdscript_editor.cpp 119 KB

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