gdscript_editor.cpp 111 KB

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