gdscript_editor.cpp 110 KB

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