gdscript_editor.cpp 118 KB

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