gdscript_editor.cpp 109 KB

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