gdscript_editor.cpp 110 KB

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