gdscript_editor.cpp 111 KB

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