gdscript_editor.cpp 110 KB

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