gdscript_editor.cpp 109 KB

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