gdscript_editor.cpp 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386
  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, HashMap<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, HashSet<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. HashMap<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 HashSet<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 HashMap<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 HashMap<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, HashMap<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, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  625. if (p_annotation->name == SNAME("@export_range")) {
  626. if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
  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. ScriptLanguage::CodeCompletionOption slider3("no_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  635. slider3.insert_text = slider3.display.quote(p_quote_style);
  636. r_result.insert(slider3.display, slider3);
  637. }
  638. } else if (p_annotation->name == SNAME("@export_exp_easing")) {
  639. if (p_argument == 0 || p_argument == 1) {
  640. // Easing hint.
  641. ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  642. hint1.insert_text = hint1.display.quote(p_quote_style);
  643. r_result.insert(hint1.display, hint1);
  644. ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  645. hint2.insert_text = hint2.display.quote(p_quote_style);
  646. r_result.insert(hint2.display, hint2);
  647. }
  648. } else if (p_annotation->name == SNAME("@export_node_path")) {
  649. ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  650. r_result.insert(node.display, node);
  651. List<StringName> node_types;
  652. ClassDB::get_inheriters_from_class("Node", &node_types);
  653. for (const StringName &E : node_types) {
  654. if (!ClassDB::is_class_exposed(E)) {
  655. continue;
  656. }
  657. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  658. r_result.insert(option.display, option);
  659. }
  660. } else if (p_annotation->name == SNAME("@warning_ignore")) {
  661. for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
  662. ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  663. r_result.insert(warning.display, warning);
  664. }
  665. }
  666. }
  667. static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool exclude_nil = false) {
  668. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  669. if (!exclude_nil && Variant::Type(i) == Variant::Type::NIL) {
  670. ScriptLanguage::CodeCompletionOption option("null", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  671. r_result.insert(option.display, option);
  672. } else {
  673. ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  674. r_result.insert(option.display, option);
  675. }
  676. }
  677. }
  678. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  679. // Built-in Variant Types
  680. _find_built_in_variants(r_result, true);
  681. List<StringName> native_types;
  682. ClassDB::get_class_list(&native_types);
  683. for (const StringName &E : native_types) {
  684. if (ClassDB::is_class_exposed(E) && !Engine::get_singleton()->has_singleton(E)) {
  685. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  686. r_result.insert(option.display, option);
  687. }
  688. }
  689. if (p_context.current_class) {
  690. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  691. // Native enums from base class
  692. List<StringName> enums;
  693. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  694. for (const StringName &E : enums) {
  695. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  696. r_result.insert(option.display, option);
  697. }
  698. }
  699. // Check current class for potential types
  700. const GDScriptParser::ClassNode *current = p_context.current_class;
  701. while (current) {
  702. for (int i = 0; i < current->members.size(); i++) {
  703. const GDScriptParser::ClassNode::Member &member = current->members[i];
  704. switch (member.type) {
  705. case GDScriptParser::ClassNode::Member::CLASS: {
  706. ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  707. r_result.insert(option.display, option);
  708. } break;
  709. case GDScriptParser::ClassNode::Member::ENUM: {
  710. if (!p_inherit_only) {
  711. ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL);
  712. r_result.insert(option.display, option);
  713. }
  714. } break;
  715. case GDScriptParser::ClassNode::Member::CONSTANT: {
  716. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  717. ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  718. r_result.insert(option.display, option);
  719. }
  720. } break;
  721. default:
  722. break;
  723. }
  724. }
  725. current = current->outer;
  726. }
  727. }
  728. // Global scripts
  729. List<StringName> global_classes;
  730. ScriptServer::get_global_class_list(&global_classes);
  731. for (const StringName &E : global_classes) {
  732. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  733. r_result.insert(option.display, option);
  734. }
  735. // Autoload singletons
  736. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  737. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  738. const ProjectSettings::AutoloadInfo &info = E.value;
  739. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  740. continue;
  741. }
  742. ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  743. r_result.insert(option.display, option);
  744. }
  745. }
  746. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  747. for (int i = 0; i < p_suite->locals.size(); i++) {
  748. ScriptLanguage::CodeCompletionOption option;
  749. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  750. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, ScriptLanguage::LOCATION_LOCAL);
  751. option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
  752. } else {
  753. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, ScriptLanguage::LOCATION_LOCAL);
  754. }
  755. r_result.insert(option.display, option);
  756. }
  757. if (p_suite->parent_block) {
  758. _find_identifiers_in_suite(p_suite->parent_block, r_result);
  759. }
  760. }
  761. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
  762. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_static, bool p_parent_only, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  763. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  764. if (!p_parent_only) {
  765. bool outer = false;
  766. const GDScriptParser::ClassNode *clss = p_class;
  767. int classes_processed = 0;
  768. while (clss) {
  769. for (int i = 0; i < clss->members.size(); i++) {
  770. const int location = (classes_processed + p_recursion_depth) | ScriptLanguage::LOCATION_PARENT_MASK;
  771. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  772. ScriptLanguage::CodeCompletionOption option;
  773. switch (member.type) {
  774. case GDScriptParser::ClassNode::Member::VARIABLE:
  775. if (p_only_functions || outer || (p_static)) {
  776. continue;
  777. }
  778. option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  779. break;
  780. case GDScriptParser::ClassNode::Member::CONSTANT:
  781. if (p_only_functions) {
  782. continue;
  783. }
  784. if (r_result.has(member.constant->identifier->name)) {
  785. continue;
  786. }
  787. option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  788. if (member.constant->initializer) {
  789. option.default_value = member.constant->initializer->reduced_value;
  790. }
  791. break;
  792. case GDScriptParser::ClassNode::Member::CLASS:
  793. if (p_only_functions) {
  794. continue;
  795. }
  796. option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
  797. break;
  798. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  799. if (p_only_functions) {
  800. continue;
  801. }
  802. option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  803. break;
  804. case GDScriptParser::ClassNode::Member::ENUM:
  805. if (p_only_functions) {
  806. continue;
  807. }
  808. option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  809. break;
  810. case GDScriptParser::ClassNode::Member::FUNCTION:
  811. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  812. continue;
  813. }
  814. option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  815. if (member.function->parameters.size() > 0) {
  816. option.insert_text += "(";
  817. } else {
  818. option.insert_text += "()";
  819. }
  820. break;
  821. case GDScriptParser::ClassNode::Member::SIGNAL:
  822. if (p_only_functions || outer) {
  823. continue;
  824. }
  825. option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  826. break;
  827. case GDScriptParser::ClassNode::Member::UNDEFINED:
  828. break;
  829. }
  830. r_result.insert(option.display, option);
  831. }
  832. outer = true;
  833. clss = clss->outer;
  834. classes_processed++;
  835. }
  836. }
  837. // Parents.
  838. GDScriptCompletionIdentifier base_type;
  839. base_type.type = p_class->base_type;
  840. base_type.type.is_meta_type = p_static;
  841. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  842. }
  843. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  844. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  845. GDScriptParser::DataType base_type = p_base.type;
  846. bool _static = base_type.is_meta_type;
  847. if (_static && base_type.kind != GDScriptParser::DataType::BUILTIN) {
  848. ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
  849. option.insert_text += "(";
  850. r_result.insert(option.display, option);
  851. }
  852. while (!base_type.has_no_type()) {
  853. switch (base_type.kind) {
  854. case GDScriptParser::DataType::CLASS: {
  855. _find_identifiers_in_class(base_type.class_type, p_only_functions, _static, false, r_result, p_recursion_depth + 1);
  856. // This already finds all parent identifiers, so we are done.
  857. base_type = GDScriptParser::DataType();
  858. } break;
  859. case GDScriptParser::DataType::SCRIPT: {
  860. Ref<Script> scr = base_type.script_type;
  861. if (scr.is_valid()) {
  862. if (!p_only_functions) {
  863. if (!_static) {
  864. List<PropertyInfo> members;
  865. scr->get_script_property_list(&members);
  866. for (const PropertyInfo &E : members) {
  867. int location = p_recursion_depth + _get_property_location(scr->get_class_name(), E.class_name);
  868. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  869. r_result.insert(option.display, option);
  870. }
  871. }
  872. HashMap<StringName, Variant> constants;
  873. scr->get_constants(&constants);
  874. for (const KeyValue<StringName, Variant> &E : constants) {
  875. int location = p_recursion_depth + _get_constant_location(scr->get_class_name(), E.key);
  876. ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  877. r_result.insert(option.display, option);
  878. }
  879. List<MethodInfo> signals;
  880. scr->get_script_signal_list(&signals);
  881. for (const MethodInfo &E : signals) {
  882. int location = p_recursion_depth + _get_signal_location(scr->get_class_name(), E.name);
  883. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  884. r_result.insert(option.display, option);
  885. }
  886. }
  887. List<MethodInfo> methods;
  888. scr->get_script_method_list(&methods);
  889. for (const MethodInfo &E : methods) {
  890. if (E.name.begins_with("@")) {
  891. continue;
  892. }
  893. int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
  894. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  895. if (E.arguments.size()) {
  896. option.insert_text += "(";
  897. } else {
  898. option.insert_text += "()";
  899. }
  900. r_result.insert(option.display, option);
  901. }
  902. Ref<Script> base_script = scr->get_base_script();
  903. if (base_script.is_valid()) {
  904. base_type.script_type = base_script;
  905. } else {
  906. base_type.kind = GDScriptParser::DataType::NATIVE;
  907. base_type.native_type = scr->get_instance_base_type();
  908. }
  909. } else {
  910. return;
  911. }
  912. } break;
  913. case GDScriptParser::DataType::NATIVE: {
  914. StringName type = base_type.native_type;
  915. if (!ClassDB::class_exists(type)) {
  916. return;
  917. }
  918. if (!p_only_functions) {
  919. List<String> constants;
  920. ClassDB::get_integer_constant_list(type, &constants);
  921. for (const String &E : constants) {
  922. int location = p_recursion_depth + _get_constant_location(type, StringName(E));
  923. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  924. r_result.insert(option.display, option);
  925. }
  926. List<MethodInfo> signals;
  927. ClassDB::get_signal_list(type, &signals);
  928. for (const MethodInfo &E : signals) {
  929. int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
  930. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  931. r_result.insert(option.display, option);
  932. }
  933. if (!_static || Engine::get_singleton()->has_singleton(type)) {
  934. List<PropertyInfo> pinfo;
  935. ClassDB::get_property_list(type, &pinfo);
  936. for (const PropertyInfo &E : pinfo) {
  937. if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  938. continue;
  939. }
  940. if (E.name.contains("/")) {
  941. continue;
  942. }
  943. int location = p_recursion_depth + _get_property_location(type, E.class_name);
  944. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  945. r_result.insert(option.display, option);
  946. }
  947. }
  948. }
  949. bool only_static = _static && !Engine::get_singleton()->has_singleton(type);
  950. List<MethodInfo> methods;
  951. ClassDB::get_method_list(type, &methods, false, true);
  952. for (const MethodInfo &E : methods) {
  953. if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
  954. continue;
  955. }
  956. if (E.name.begins_with("_")) {
  957. continue;
  958. }
  959. int location = p_recursion_depth + _get_method_location(type, E.name);
  960. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  961. if (E.arguments.size()) {
  962. option.insert_text += "(";
  963. } else {
  964. option.insert_text += "()";
  965. }
  966. r_result.insert(option.display, option);
  967. }
  968. return;
  969. } break;
  970. case GDScriptParser::DataType::BUILTIN: {
  971. Callable::CallError err;
  972. Variant tmp;
  973. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  974. if (err.error != Callable::CallError::CALL_OK) {
  975. return;
  976. }
  977. if (!p_only_functions) {
  978. List<PropertyInfo> members;
  979. if (p_base.value.get_type() != Variant::NIL) {
  980. p_base.value.get_property_list(&members);
  981. } else {
  982. tmp.get_property_list(&members);
  983. }
  984. for (const PropertyInfo &E : members) {
  985. if (!String(E.name).contains("/")) {
  986. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  987. r_result.insert(option.display, option);
  988. }
  989. }
  990. }
  991. List<MethodInfo> methods;
  992. tmp.get_method_list(&methods);
  993. for (const MethodInfo &E : methods) {
  994. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  995. if (E.arguments.size()) {
  996. option.insert_text += "(";
  997. } else {
  998. option.insert_text += "()";
  999. }
  1000. r_result.insert(option.display, option);
  1001. }
  1002. return;
  1003. } break;
  1004. default: {
  1005. return;
  1006. } break;
  1007. }
  1008. }
  1009. }
  1010. static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1011. if (!p_only_functions && p_context.current_suite) {
  1012. // This includes function parameters, since they are also locals.
  1013. _find_identifiers_in_suite(p_context.current_suite, r_result);
  1014. }
  1015. if (p_context.current_class) {
  1016. _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);
  1017. }
  1018. List<StringName> functions;
  1019. GDScriptUtilityFunctions::get_function_list(&functions);
  1020. for (const StringName &E : functions) {
  1021. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
  1022. ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1023. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  1024. option.insert_text += "(";
  1025. } else {
  1026. option.insert_text += "()";
  1027. }
  1028. r_result.insert(option.display, option);
  1029. }
  1030. if (p_only_functions) {
  1031. return;
  1032. }
  1033. _find_built_in_variants(r_result);
  1034. static const char *_keywords[] = {
  1035. "false", "PI", "TAU", "INF", "NAN", "self", "true", "breakpoint", "tool", "super",
  1036. "break", "continue", "pass", "return",
  1037. nullptr
  1038. };
  1039. const char **kw = _keywords;
  1040. while (*kw) {
  1041. ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1042. r_result.insert(option.display, option);
  1043. kw++;
  1044. }
  1045. static const char *_keywords_with_space[] = {
  1046. "and", "in", "not", "or", "as", "class", "extends", "is", "func", "signal", "await",
  1047. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "while",
  1048. nullptr
  1049. };
  1050. const char **kws = _keywords_with_space;
  1051. while (*kws) {
  1052. ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1053. option.insert_text += " ";
  1054. r_result.insert(option.display, option);
  1055. kws++;
  1056. }
  1057. static const char *_keywords_with_args[] = {
  1058. "assert", "preload",
  1059. nullptr
  1060. };
  1061. const char **kwa = _keywords_with_args;
  1062. while (*kwa) {
  1063. ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1064. option.insert_text += "(";
  1065. r_result.insert(option.display, option);
  1066. kwa++;
  1067. }
  1068. List<StringName> utility_func_names;
  1069. Variant::get_utility_function_list(&utility_func_names);
  1070. for (List<StringName>::Element *E = utility_func_names.front(); E; E = E->next()) {
  1071. ScriptLanguage::CodeCompletionOption option(E->get(), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1072. option.insert_text += "(";
  1073. r_result.insert(option.display, option);
  1074. }
  1075. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1076. if (!E.value.is_singleton) {
  1077. continue;
  1078. }
  1079. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1080. r_result.insert(option.display, option);
  1081. }
  1082. // Native classes and global constants.
  1083. for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
  1084. ScriptLanguage::CodeCompletionOption option;
  1085. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  1086. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  1087. } else {
  1088. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1089. }
  1090. r_result.insert(option.display, option);
  1091. }
  1092. }
  1093. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  1094. GDScriptCompletionIdentifier ci;
  1095. ci.value = p_value;
  1096. ci.type.is_constant = true;
  1097. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1098. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1099. ci.type.builtin_type = p_value.get_type();
  1100. if (ci.type.builtin_type == Variant::OBJECT) {
  1101. Object *obj = p_value.operator Object *();
  1102. if (!obj) {
  1103. return ci;
  1104. }
  1105. ci.type.native_type = obj->get_class_name();
  1106. Ref<Script> scr = p_value;
  1107. if (scr.is_valid()) {
  1108. ci.type.is_meta_type = true;
  1109. } else {
  1110. ci.type.is_meta_type = false;
  1111. scr = obj->get_script();
  1112. }
  1113. if (scr.is_valid()) {
  1114. ci.type.script_type = scr;
  1115. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1116. ci.type.native_type = scr->get_instance_base_type();
  1117. } else {
  1118. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1119. }
  1120. }
  1121. return ci;
  1122. }
  1123. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  1124. GDScriptCompletionIdentifier ci;
  1125. if (p_property.type == Variant::NIL) {
  1126. // Variant
  1127. return ci;
  1128. }
  1129. if (p_property.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1130. ci.enumeration = p_property.class_name;
  1131. }
  1132. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1133. ci.type.builtin_type = p_property.type;
  1134. if (p_property.type == Variant::OBJECT) {
  1135. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1136. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1137. } else {
  1138. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1139. }
  1140. return ci;
  1141. }
  1142. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1143. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1144. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1145. static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
  1146. if (p_expression) {
  1147. switch (p_expression->type) {
  1148. case GDScriptParser::Node::IDENTIFIER: {
  1149. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1150. if (id->name == p_name) {
  1151. return true;
  1152. }
  1153. } break;
  1154. case GDScriptParser::Node::CAST: {
  1155. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1156. return _is_expression_named_identifier(cn->operand, p_name);
  1157. } break;
  1158. default:
  1159. break;
  1160. }
  1161. }
  1162. return false;
  1163. }
  1164. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1165. bool found = false;
  1166. if (p_expression == nullptr) {
  1167. return false;
  1168. }
  1169. if (p_expression->is_constant) {
  1170. // Already has a value, so just use that.
  1171. r_type = _type_from_variant(p_expression->reduced_value);
  1172. found = true;
  1173. } else {
  1174. switch (p_expression->type) {
  1175. case GDScriptParser::Node::LITERAL: {
  1176. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1177. r_type = _type_from_variant(literal->value);
  1178. found = true;
  1179. } break;
  1180. case GDScriptParser::Node::SELF: {
  1181. if (p_context.current_class) {
  1182. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1183. r_type.type.type_source = GDScriptParser::DataType::INFERRED;
  1184. r_type.type.is_constant = true;
  1185. r_type.type.class_type = p_context.current_class;
  1186. r_type.value = p_context.base;
  1187. found = true;
  1188. }
  1189. } break;
  1190. case GDScriptParser::Node::IDENTIFIER: {
  1191. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1192. found = _guess_identifier_type(p_context, id->name, r_type);
  1193. } break;
  1194. case GDScriptParser::Node::DICTIONARY: {
  1195. // Try to recreate the dictionary.
  1196. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1197. Dictionary d;
  1198. bool full = true;
  1199. for (int i = 0; i < dn->elements.size(); i++) {
  1200. GDScriptCompletionIdentifier key;
  1201. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1202. if (!key.type.is_constant) {
  1203. full = false;
  1204. break;
  1205. }
  1206. GDScriptCompletionIdentifier value;
  1207. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1208. if (!value.type.is_constant) {
  1209. full = false;
  1210. break;
  1211. }
  1212. d[key.value] = value.value;
  1213. } else {
  1214. full = false;
  1215. break;
  1216. }
  1217. } else {
  1218. full = false;
  1219. break;
  1220. }
  1221. }
  1222. if (full) {
  1223. r_type.value = d;
  1224. r_type.type.is_constant = true;
  1225. }
  1226. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1227. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1228. r_type.type.builtin_type = Variant::DICTIONARY;
  1229. found = true;
  1230. } break;
  1231. case GDScriptParser::Node::ARRAY: {
  1232. // Try to recreate the array
  1233. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1234. Array a;
  1235. bool full = true;
  1236. a.resize(an->elements.size());
  1237. for (int i = 0; i < an->elements.size(); i++) {
  1238. GDScriptCompletionIdentifier value;
  1239. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1240. if (value.type.is_constant) {
  1241. a[i] = value.value;
  1242. } else {
  1243. full = false;
  1244. break;
  1245. }
  1246. } else {
  1247. full = false;
  1248. break;
  1249. }
  1250. }
  1251. if (full) {
  1252. // If not fully constant, setting this value is detrimental to the inference.
  1253. r_type.value = a;
  1254. }
  1255. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1256. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1257. r_type.type.builtin_type = Variant::ARRAY;
  1258. found = true;
  1259. } break;
  1260. case GDScriptParser::Node::CAST: {
  1261. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1262. GDScriptCompletionIdentifier value;
  1263. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1264. r_type.type = cn->get_datatype();
  1265. found = true;
  1266. }
  1267. } break;
  1268. case GDScriptParser::Node::CALL: {
  1269. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1270. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1271. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1272. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1273. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1274. found = true;
  1275. break;
  1276. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  1277. MethodInfo mi = GDScriptUtilityFunctions::get_function_info(call->function_name);
  1278. r_type = _type_from_property(mi.return_val);
  1279. found = true;
  1280. break;
  1281. } else {
  1282. GDScriptParser::CompletionContext c = p_context;
  1283. c.current_line = call->start_line;
  1284. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1285. GDScriptCompletionIdentifier base;
  1286. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1287. // Simple call, so base is 'self'.
  1288. if (p_context.current_class) {
  1289. base.type.kind = GDScriptParser::DataType::CLASS;
  1290. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1291. base.type.is_constant = true;
  1292. base.type.class_type = p_context.current_class;
  1293. base.value = p_context.base;
  1294. } else {
  1295. break;
  1296. }
  1297. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1298. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1299. found = false;
  1300. break;
  1301. }
  1302. } else {
  1303. break;
  1304. }
  1305. // Try call if constant methods with constant arguments
  1306. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1307. GDScriptParser::DataType native_type = base.type;
  1308. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1309. native_type = native_type.class_type->base_type;
  1310. }
  1311. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1312. if (native_type.script_type.is_valid()) {
  1313. Ref<Script> parent = native_type.script_type->get_base_script();
  1314. if (parent.is_valid()) {
  1315. native_type.script_type = parent;
  1316. } else {
  1317. native_type.kind = GDScriptParser::DataType::NATIVE;
  1318. native_type.native_type = native_type.script_type->get_instance_base_type();
  1319. if (!ClassDB::class_exists(native_type.native_type)) {
  1320. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1321. }
  1322. }
  1323. }
  1324. }
  1325. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1326. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1327. if (mb && mb->is_const()) {
  1328. bool all_is_const = true;
  1329. Vector<Variant> args;
  1330. GDScriptParser::CompletionContext c2 = p_context;
  1331. c2.current_line = call->start_line;
  1332. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1333. GDScriptCompletionIdentifier arg;
  1334. if (!call->arguments[i]->is_constant) {
  1335. all_is_const = false;
  1336. }
  1337. }
  1338. Object *baseptr = base.value;
  1339. if (all_is_const && call->function_name == SNAME("get_node") && ClassDB::is_parent_class(native_type.native_type, SNAME("Node")) && args.size()) {
  1340. String arg1 = args[0];
  1341. if (arg1.begins_with("/root/")) {
  1342. String which = arg1.get_slice("/", 2);
  1343. if (!which.is_empty()) {
  1344. // Try singletons first
  1345. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1346. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1347. found = true;
  1348. } else {
  1349. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1350. String name = E.key;
  1351. if (name == which) {
  1352. String script = E.value.path;
  1353. if (!script.begins_with("res://")) {
  1354. script = "res://" + script;
  1355. }
  1356. if (!script.ends_with(".gd")) {
  1357. // not a script, try find the script anyway,
  1358. // may have some success
  1359. script = script.get_basename() + ".gd";
  1360. }
  1361. if (FileAccess::exists(script)) {
  1362. Error err = OK;
  1363. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1364. if (err == OK) {
  1365. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1366. r_type.type.script_path = script;
  1367. r_type.type.class_type = parser->get_parser()->get_tree();
  1368. r_type.type.is_constant = false;
  1369. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1370. r_type.value = Variant();
  1371. p_context.dependent_parsers.push_back(parser);
  1372. found = true;
  1373. }
  1374. }
  1375. break;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. if (!found && all_is_const && baseptr) {
  1383. Vector<const Variant *> argptr;
  1384. for (int i = 0; i < args.size(); i++) {
  1385. argptr.push_back(&args[i]);
  1386. }
  1387. Callable::CallError ce;
  1388. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1389. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1390. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1391. r_type = _type_from_variant(ret);
  1392. found = true;
  1393. }
  1394. }
  1395. }
  1396. }
  1397. }
  1398. }
  1399. if (!found) {
  1400. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1401. }
  1402. }
  1403. } break;
  1404. case GDScriptParser::Node::SUBSCRIPT: {
  1405. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1406. if (subscript->is_attribute) {
  1407. GDScriptParser::CompletionContext c = p_context;
  1408. c.current_line = subscript->start_line;
  1409. GDScriptCompletionIdentifier base;
  1410. if (!_guess_expression_type(c, subscript->base, base)) {
  1411. found = false;
  1412. break;
  1413. }
  1414. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1415. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1416. r_type = _type_from_variant(value);
  1417. found = true;
  1418. break;
  1419. }
  1420. const GDScriptParser::DictionaryNode *dn = nullptr;
  1421. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1422. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1423. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1424. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1425. }
  1426. if (dn) {
  1427. for (int i = 0; i < dn->elements.size(); i++) {
  1428. GDScriptCompletionIdentifier key;
  1429. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1430. continue;
  1431. }
  1432. if (key.value == String(subscript->attribute->name)) {
  1433. r_type.assigned_expression = dn->elements[i].value;
  1434. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. if (!found) {
  1440. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1441. }
  1442. } else {
  1443. if (subscript->index == nullptr) {
  1444. found = false;
  1445. break;
  1446. }
  1447. GDScriptParser::CompletionContext c = p_context;
  1448. c.current_line = subscript->start_line;
  1449. GDScriptCompletionIdentifier base;
  1450. if (!_guess_expression_type(c, subscript->base, base)) {
  1451. found = false;
  1452. break;
  1453. }
  1454. GDScriptCompletionIdentifier index;
  1455. if (!_guess_expression_type(c, subscript->index, index)) {
  1456. found = false;
  1457. break;
  1458. }
  1459. if (base.value.in(index.value)) {
  1460. Variant value = base.value.get(index.value);
  1461. r_type = _type_from_variant(value);
  1462. found = true;
  1463. break;
  1464. }
  1465. // Look if it is a dictionary node.
  1466. const GDScriptParser::DictionaryNode *dn = nullptr;
  1467. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1468. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1469. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1470. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1471. }
  1472. if (dn) {
  1473. for (int i = 0; i < dn->elements.size(); i++) {
  1474. GDScriptCompletionIdentifier key;
  1475. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1476. continue;
  1477. }
  1478. if (key.value == index.value) {
  1479. r_type.assigned_expression = dn->elements[i].value;
  1480. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1481. break;
  1482. }
  1483. }
  1484. }
  1485. // Look if it is an array node.
  1486. if (!found && index.value.is_num()) {
  1487. int idx = index.value;
  1488. const GDScriptParser::ArrayNode *an = nullptr;
  1489. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1490. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1491. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1492. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1493. }
  1494. if (an && idx >= 0 && an->elements.size() > idx) {
  1495. r_type.assigned_expression = an->elements[idx];
  1496. found = _guess_expression_type(c, an->elements[idx], r_type);
  1497. break;
  1498. }
  1499. }
  1500. // Look for valid indexing in other types
  1501. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1502. StringName id = index.value;
  1503. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1504. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1505. Callable::CallError err;
  1506. Variant base_val;
  1507. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1508. bool valid = false;
  1509. Variant res = base_val.get(index.value, &valid);
  1510. if (valid) {
  1511. r_type = _type_from_variant(res);
  1512. r_type.value = Variant();
  1513. r_type.type.is_constant = false;
  1514. found = true;
  1515. }
  1516. }
  1517. }
  1518. } break;
  1519. case GDScriptParser::Node::BINARY_OPERATOR: {
  1520. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1521. if (op->variant_op == Variant::OP_MAX) {
  1522. break;
  1523. }
  1524. GDScriptParser::CompletionContext context = p_context;
  1525. context.current_line = op->start_line;
  1526. GDScriptCompletionIdentifier p1;
  1527. GDScriptCompletionIdentifier p2;
  1528. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1529. found = false;
  1530. break;
  1531. }
  1532. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1533. found = false;
  1534. break;
  1535. }
  1536. Callable::CallError ce;
  1537. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1538. Variant d1;
  1539. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1540. Variant d2;
  1541. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1542. Variant v1 = (v1_use_value) ? p1.value : d1;
  1543. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1544. Variant v2 = (v2_use_value) ? p2.value : d2;
  1545. // avoid potential invalid ops
  1546. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1547. v2 = 1;
  1548. v2_use_value = false;
  1549. }
  1550. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1551. v2 = 1.0;
  1552. v2_use_value = false;
  1553. }
  1554. Variant res;
  1555. bool valid;
  1556. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1557. if (!valid) {
  1558. found = false;
  1559. break;
  1560. }
  1561. r_type = _type_from_variant(res);
  1562. if (!v1_use_value || !v2_use_value) {
  1563. r_type.value = Variant();
  1564. r_type.type.is_constant = false;
  1565. }
  1566. found = true;
  1567. } break;
  1568. default:
  1569. break;
  1570. }
  1571. }
  1572. // It may have found a null, but that's never useful
  1573. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1574. found = false;
  1575. }
  1576. // Check type hint last. For collections we want chance to get the actual value first
  1577. // This way we can detect types from the content of dictionaries and arrays
  1578. if (!found && p_expression->get_datatype().is_hard_type()) {
  1579. r_type.type = p_expression->get_datatype();
  1580. if (!r_type.assigned_expression) {
  1581. r_type.assigned_expression = p_expression;
  1582. }
  1583. found = true;
  1584. }
  1585. return found;
  1586. }
  1587. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1588. // Look in blocks first.
  1589. int last_assign_line = -1;
  1590. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1591. GDScriptParser::DataType id_type;
  1592. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1593. bool is_function_parameter = false;
  1594. if (suite) {
  1595. if (suite->has_local(p_identifier)) {
  1596. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier);
  1597. id_type = local.get_datatype();
  1598. // Check initializer as the first assignment.
  1599. switch (local.type) {
  1600. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1601. if (local.variable->initializer) {
  1602. last_assign_line = local.variable->initializer->end_line;
  1603. last_assigned_expression = local.variable->initializer;
  1604. }
  1605. break;
  1606. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1607. if (local.constant->initializer) {
  1608. last_assign_line = local.constant->initializer->end_line;
  1609. last_assigned_expression = local.constant->initializer;
  1610. }
  1611. break;
  1612. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1613. if (local.parameter->default_value) {
  1614. last_assign_line = local.parameter->default_value->end_line;
  1615. last_assigned_expression = local.parameter->default_value;
  1616. }
  1617. is_function_parameter = true;
  1618. break;
  1619. default:
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. while (suite) {
  1625. for (int i = 0; i < suite->statements.size(); i++) {
  1626. if (suite->statements[i]->start_line > p_context.current_line) {
  1627. break;
  1628. }
  1629. switch (suite->statements[i]->type) {
  1630. case GDScriptParser::Node::ASSIGNMENT: {
  1631. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1632. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1633. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1634. if (id->name == p_identifier) {
  1635. last_assign_line = assign->assigned_value->end_line;
  1636. last_assigned_expression = assign->assigned_value;
  1637. }
  1638. }
  1639. } break;
  1640. default:
  1641. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1642. break;
  1643. }
  1644. }
  1645. 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) {
  1646. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1647. // Super dirty hack, but very useful.
  1648. // Credit: Zylann.
  1649. // TODO: this could be hacked to detect ANDed conditions too...
  1650. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(suite->parent_if->condition);
  1651. 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) {
  1652. // Bingo.
  1653. GDScriptParser::CompletionContext c = p_context;
  1654. c.current_line = op->left_operand->start_line;
  1655. c.current_suite = suite;
  1656. GDScriptCompletionIdentifier is_type;
  1657. if (_guess_expression_type(c, op->right_operand, is_type)) {
  1658. id_type = is_type.type;
  1659. id_type.is_meta_type = false;
  1660. if (last_assign_line < c.current_line) {
  1661. // Override last assignment.
  1662. last_assign_line = c.current_line;
  1663. last_assigned_expression = nullptr;
  1664. }
  1665. }
  1666. }
  1667. }
  1668. suite = suite->parent_block;
  1669. }
  1670. if (last_assigned_expression && last_assign_line != p_context.current_line) {
  1671. GDScriptParser::CompletionContext c = p_context;
  1672. c.current_line = last_assign_line;
  1673. r_type.assigned_expression = last_assigned_expression;
  1674. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1675. return true;
  1676. }
  1677. }
  1678. if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
  1679. // Check if it's override of native function, then we can assume the type from the signature.
  1680. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1681. while (base_type.is_set()) {
  1682. switch (base_type.kind) {
  1683. case GDScriptParser::DataType::CLASS:
  1684. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1685. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1686. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier]];
  1687. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1688. id_type = parameter->get_datatype();
  1689. }
  1690. if (parameter->default_value) {
  1691. GDScriptParser::CompletionContext c = p_context;
  1692. c.current_function = parent_function;
  1693. c.current_class = base_type.class_type;
  1694. c.base = nullptr;
  1695. if (_guess_expression_type(c, parameter->default_value, r_type)) {
  1696. return true;
  1697. }
  1698. }
  1699. }
  1700. base_type = base_type.class_type->base_type;
  1701. break;
  1702. case GDScriptParser::DataType::NATIVE: {
  1703. if (id_type.is_set() && !id_type.is_variant()) {
  1704. base_type = GDScriptParser::DataType();
  1705. break;
  1706. }
  1707. MethodInfo info;
  1708. if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
  1709. for (const PropertyInfo &E : info.arguments) {
  1710. if (E.name == p_identifier) {
  1711. r_type = _type_from_property(E);
  1712. return true;
  1713. }
  1714. }
  1715. }
  1716. base_type = GDScriptParser::DataType();
  1717. } break;
  1718. default:
  1719. break;
  1720. }
  1721. }
  1722. }
  1723. if (id_type.is_set() && !id_type.is_variant()) {
  1724. r_type.type = id_type;
  1725. return true;
  1726. }
  1727. // Check current class (including inheritance).
  1728. if (p_context.current_class) {
  1729. GDScriptCompletionIdentifier base;
  1730. base.value = p_context.base;
  1731. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1732. base.type.kind = GDScriptParser::DataType::CLASS;
  1733. base.type.class_type = p_context.current_class;
  1734. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1735. if (_guess_identifier_type_from_base(p_context, base, p_identifier, r_type)) {
  1736. return true;
  1737. }
  1738. }
  1739. // Check global scripts.
  1740. if (ScriptServer::is_global_class(p_identifier)) {
  1741. String script = ScriptServer::get_global_class_path(p_identifier);
  1742. if (script.to_lower().ends_with(".gd")) {
  1743. Error err = OK;
  1744. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1745. if (err == OK) {
  1746. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1747. r_type.type.script_path = script;
  1748. r_type.type.class_type = parser->get_parser()->get_tree();
  1749. r_type.type.is_constant = false;
  1750. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1751. r_type.value = Variant();
  1752. p_context.dependent_parsers.push_back(parser);
  1753. return true;
  1754. }
  1755. } else {
  1756. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier));
  1757. if (scr.is_valid()) {
  1758. r_type = _type_from_variant(scr);
  1759. r_type.type.is_meta_type = true;
  1760. return true;
  1761. }
  1762. }
  1763. return false;
  1764. }
  1765. // Check autoloads.
  1766. if (ProjectSettings::get_singleton()->has_autoload(p_identifier)) {
  1767. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier]);
  1768. return true;
  1769. }
  1770. // Check ClassDB.
  1771. if (ClassDB::class_exists(p_identifier) && ClassDB::is_class_exposed(p_identifier)) {
  1772. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1773. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1774. r_type.type.native_type = p_identifier;
  1775. r_type.type.is_constant = true;
  1776. r_type.type.is_meta_type = !Engine::get_singleton()->has_singleton(p_identifier);
  1777. r_type.value = Variant();
  1778. }
  1779. return false;
  1780. }
  1781. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1782. GDScriptParser::DataType base_type = p_base.type;
  1783. bool is_static = base_type.is_meta_type;
  1784. while (base_type.is_set()) {
  1785. switch (base_type.kind) {
  1786. case GDScriptParser::DataType::CLASS:
  1787. if (base_type.class_type->has_member(p_identifier)) {
  1788. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1789. switch (member.type) {
  1790. case GDScriptParser::ClassNode::Member::CONSTANT:
  1791. r_type.type = member.constant->get_datatype();
  1792. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1793. r_type.value = member.constant->initializer->reduced_value;
  1794. }
  1795. return true;
  1796. case GDScriptParser::ClassNode::Member::VARIABLE:
  1797. if (!is_static) {
  1798. if (member.variable->initializer) {
  1799. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1800. if (init->is_constant) {
  1801. r_type.value = init->reduced_value;
  1802. r_type = _type_from_variant(init->reduced_value);
  1803. return true;
  1804. } else if (init->start_line == p_context.current_line) {
  1805. return false;
  1806. // Detects if variable is assigned to itself
  1807. } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
  1808. if (member.variable->initializer->get_datatype().is_set()) {
  1809. r_type.type = member.variable->initializer->get_datatype();
  1810. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1811. r_type.type = member.variable->get_datatype();
  1812. }
  1813. return true;
  1814. } else if (_guess_expression_type(p_context, init, r_type)) {
  1815. return true;
  1816. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1817. r_type.type = init->get_datatype();
  1818. return true;
  1819. }
  1820. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1821. r_type.type = member.variable->get_datatype();
  1822. return true;
  1823. }
  1824. }
  1825. // TODO: Check assignments in constructor.
  1826. return false;
  1827. case GDScriptParser::ClassNode::Member::ENUM:
  1828. r_type.type = member.m_enum->get_datatype();
  1829. r_type.enumeration = member.m_enum->identifier->name;
  1830. return true;
  1831. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1832. r_type = _type_from_variant(member.enum_value.value);
  1833. return true;
  1834. case GDScriptParser::ClassNode::Member::SIGNAL:
  1835. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1836. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1837. r_type.type.builtin_type = Variant::SIGNAL;
  1838. return true;
  1839. case GDScriptParser::ClassNode::Member::FUNCTION:
  1840. if (is_static && !member.function->is_static) {
  1841. return false;
  1842. }
  1843. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1844. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1845. r_type.type.builtin_type = Variant::CALLABLE;
  1846. return true;
  1847. case GDScriptParser::ClassNode::Member::CLASS:
  1848. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1849. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1850. r_type.type.class_type = member.m_class;
  1851. return true;
  1852. case GDScriptParser::ClassNode::Member::UNDEFINED:
  1853. return false; // Unreachable.
  1854. }
  1855. return false;
  1856. }
  1857. base_type = base_type.class_type->base_type;
  1858. break;
  1859. case GDScriptParser::DataType::SCRIPT: {
  1860. Ref<Script> scr = base_type.script_type;
  1861. if (scr.is_valid()) {
  1862. HashMap<StringName, Variant> constants;
  1863. scr->get_constants(&constants);
  1864. if (constants.has(p_identifier)) {
  1865. r_type = _type_from_variant(constants[p_identifier]);
  1866. return true;
  1867. }
  1868. if (!is_static) {
  1869. List<PropertyInfo> members;
  1870. scr->get_script_property_list(&members);
  1871. for (const PropertyInfo &prop : members) {
  1872. if (prop.name == p_identifier) {
  1873. r_type = _type_from_property(prop);
  1874. return true;
  1875. }
  1876. }
  1877. }
  1878. Ref<Script> parent = scr->get_base_script();
  1879. if (parent.is_valid()) {
  1880. base_type.script_type = parent;
  1881. } else {
  1882. base_type.kind = GDScriptParser::DataType::NATIVE;
  1883. base_type.native_type = scr->get_instance_base_type();
  1884. }
  1885. } else {
  1886. return false;
  1887. }
  1888. } break;
  1889. case GDScriptParser::DataType::NATIVE: {
  1890. StringName class_name = base_type.native_type;
  1891. if (!ClassDB::class_exists(class_name)) {
  1892. return false;
  1893. }
  1894. // Skip constants since they're all integers. Type does not matter because int has no members.
  1895. PropertyInfo prop;
  1896. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  1897. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  1898. if (getter != StringName()) {
  1899. MethodBind *g = ClassDB::get_method(class_name, getter);
  1900. if (g) {
  1901. r_type = _type_from_property(g->get_return_info());
  1902. return true;
  1903. }
  1904. } else {
  1905. r_type = _type_from_property(prop);
  1906. return true;
  1907. }
  1908. }
  1909. return false;
  1910. } break;
  1911. case GDScriptParser::DataType::BUILTIN: {
  1912. Callable::CallError err;
  1913. Variant tmp;
  1914. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1915. if (err.error != Callable::CallError::CALL_OK) {
  1916. return false;
  1917. }
  1918. bool valid = false;
  1919. Variant res = tmp.get(p_identifier, &valid);
  1920. if (valid) {
  1921. r_type = _type_from_variant(res);
  1922. r_type.value = Variant();
  1923. r_type.type.is_constant = false;
  1924. return true;
  1925. }
  1926. return false;
  1927. } break;
  1928. default: {
  1929. return false;
  1930. } break;
  1931. }
  1932. }
  1933. return false;
  1934. }
  1935. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  1936. if (!p_context.current_suite) {
  1937. return;
  1938. }
  1939. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  1940. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  1941. break;
  1942. }
  1943. GDScriptParser::CompletionContext c = p_context;
  1944. switch (p_context.current_suite->statements[i]->type) {
  1945. case GDScriptParser::Node::FOR:
  1946. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  1947. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1948. break;
  1949. case GDScriptParser::Node::WHILE:
  1950. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  1951. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1952. break;
  1953. case GDScriptParser::Node::IF: {
  1954. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  1955. c.current_suite = _if->true_block;
  1956. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1957. if (_if->false_block) {
  1958. c.current_suite = _if->false_block;
  1959. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1960. }
  1961. } break;
  1962. case GDScriptParser::Node::MATCH: {
  1963. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  1964. for (int j = 0; j < match->branches.size(); j++) {
  1965. c.current_suite = match->branches[j]->block;
  1966. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  1967. }
  1968. } break;
  1969. case GDScriptParser::Node::RETURN: {
  1970. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  1971. if (ret->return_value) {
  1972. if (ret->start_line > r_last_return_line) {
  1973. r_last_return_line = ret->start_line;
  1974. *r_last_returned_value = ret->return_value;
  1975. }
  1976. }
  1977. } break;
  1978. default:
  1979. break;
  1980. }
  1981. }
  1982. }
  1983. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  1984. GDScriptParser::DataType base_type = p_base.type;
  1985. bool is_static = base_type.is_meta_type;
  1986. if (is_static && p_method == SNAME("new")) {
  1987. r_type.type = base_type;
  1988. r_type.type.is_meta_type = false;
  1989. r_type.type.is_constant = false;
  1990. return true;
  1991. }
  1992. while (base_type.is_set() && !base_type.is_variant()) {
  1993. switch (base_type.kind) {
  1994. case GDScriptParser::DataType::CLASS:
  1995. if (base_type.class_type->has_function(p_method)) {
  1996. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  1997. if (!is_static || method->is_static) {
  1998. int last_return_line = -1;
  1999. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  2000. GDScriptParser::CompletionContext c = p_context;
  2001. c.current_class = base_type.class_type;
  2002. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  2003. c.current_suite = method->body;
  2004. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  2005. if (last_returned_value) {
  2006. c.current_line = c.current_suite->end_line;
  2007. if (_guess_expression_type(c, last_returned_value, r_type)) {
  2008. return true;
  2009. }
  2010. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  2011. r_type.type = method->get_datatype();
  2012. return true;
  2013. }
  2014. }
  2015. }
  2016. }
  2017. base_type = base_type.class_type->base_type;
  2018. break;
  2019. case GDScriptParser::DataType::SCRIPT: {
  2020. Ref<Script> scr = base_type.script_type;
  2021. if (scr.is_valid()) {
  2022. List<MethodInfo> methods;
  2023. scr->get_script_method_list(&methods);
  2024. for (const MethodInfo &mi : methods) {
  2025. if (mi.name == p_method) {
  2026. r_type = _type_from_property(mi.return_val);
  2027. return true;
  2028. }
  2029. }
  2030. Ref<Script> base_script = scr->get_base_script();
  2031. if (base_script.is_valid()) {
  2032. base_type.script_type = base_script;
  2033. } else {
  2034. base_type.kind = GDScriptParser::DataType::NATIVE;
  2035. base_type.native_type = scr->get_instance_base_type();
  2036. }
  2037. } else {
  2038. return false;
  2039. }
  2040. } break;
  2041. case GDScriptParser::DataType::NATIVE: {
  2042. if (!ClassDB::class_exists(base_type.native_type)) {
  2043. return false;
  2044. }
  2045. MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
  2046. if (mb) {
  2047. r_type = _type_from_property(mb->get_return_info());
  2048. return true;
  2049. }
  2050. return false;
  2051. } break;
  2052. case GDScriptParser::DataType::BUILTIN: {
  2053. Callable::CallError err;
  2054. Variant tmp;
  2055. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2056. if (err.error != Callable::CallError::CALL_OK) {
  2057. return false;
  2058. }
  2059. List<MethodInfo> methods;
  2060. tmp.get_method_list(&methods);
  2061. for (const MethodInfo &mi : methods) {
  2062. if (mi.name == p_method) {
  2063. r_type = _type_from_property(mi.return_val);
  2064. return true;
  2065. }
  2066. }
  2067. return false;
  2068. } break;
  2069. default: {
  2070. return false;
  2071. }
  2072. }
  2073. }
  2074. return false;
  2075. }
  2076. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  2077. if (!p_enum_hint.contains(".")) {
  2078. // Global constant or in the current class.
  2079. StringName current_enum = p_enum_hint;
  2080. 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) {
  2081. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  2082. for (int i = 0; i < _enum->values.size(); i++) {
  2083. ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2084. r_result.insert(option.display, option);
  2085. }
  2086. } else {
  2087. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  2088. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  2089. ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2090. r_result.insert(option.display, option);
  2091. }
  2092. }
  2093. }
  2094. } else {
  2095. String class_name = p_enum_hint.get_slice(".", 0);
  2096. String enum_name = p_enum_hint.get_slice(".", 1);
  2097. if (!ClassDB::class_exists(class_name)) {
  2098. return;
  2099. }
  2100. List<StringName> enum_constants;
  2101. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  2102. for (const StringName &E : enum_constants) {
  2103. String candidate = class_name + "." + E;
  2104. int location = _get_enum_constant_location(class_name, E);
  2105. ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  2106. r_result.insert(option.display, option);
  2107. }
  2108. }
  2109. }
  2110. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
  2111. Variant base = p_base.value;
  2112. GDScriptParser::DataType base_type = p_base.type;
  2113. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2114. while (base_type.is_set() && !base_type.is_variant()) {
  2115. switch (base_type.kind) {
  2116. case GDScriptParser::DataType::CLASS: {
  2117. if (base_type.class_type->has_member(p_method)) {
  2118. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  2119. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2120. r_arghint = _make_arguments_hint(member.function, p_argidx);
  2121. return;
  2122. }
  2123. }
  2124. base_type = base_type.class_type->base_type;
  2125. } break;
  2126. case GDScriptParser::DataType::NATIVE: {
  2127. StringName class_name = base_type.native_type;
  2128. if (!ClassDB::class_exists(class_name)) {
  2129. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2130. break;
  2131. }
  2132. MethodInfo info;
  2133. int method_args = 0;
  2134. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  2135. method_args = info.arguments.size();
  2136. if (base.get_type() == Variant::OBJECT) {
  2137. Object *obj = base.operator Object *();
  2138. if (obj) {
  2139. List<String> options;
  2140. obj->get_argument_options(p_method, p_argidx, &options);
  2141. for (String &opt : options) {
  2142. if (opt.is_quoted()) {
  2143. opt = opt.unquote().quote(quote_style); // Handle user preference.
  2144. }
  2145. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2146. r_result.insert(option.display, option);
  2147. }
  2148. }
  2149. }
  2150. if (p_argidx < method_args) {
  2151. PropertyInfo arg_info = info.arguments[p_argidx];
  2152. if (arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2153. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  2154. }
  2155. }
  2156. r_arghint = _make_arguments_hint(info, p_argidx);
  2157. }
  2158. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (p_method == SNAME("get_node") || p_method == SNAME("has_node"))) {
  2159. // Get autoloads
  2160. List<PropertyInfo> props;
  2161. ProjectSettings::get_singleton()->get_property_list(&props);
  2162. for (const PropertyInfo &E : props) {
  2163. String s = E.name;
  2164. if (!s.begins_with("autoload/")) {
  2165. continue;
  2166. }
  2167. String name = s.get_slice("/", 1);
  2168. ScriptLanguage::CodeCompletionOption option("/root/" + name, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2169. option.insert_text = option.display.quote(quote_style);
  2170. r_result.insert(option.display, option);
  2171. }
  2172. }
  2173. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && p_method.operator String().contains("action")) {
  2174. // Get input actions
  2175. List<PropertyInfo> props;
  2176. ProjectSettings::get_singleton()->get_property_list(&props);
  2177. for (const PropertyInfo &E : props) {
  2178. String s = E.name;
  2179. if (!s.begins_with("input/")) {
  2180. continue;
  2181. }
  2182. String name = s.get_slice("/", 1);
  2183. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2184. option.insert_text = option.display.quote(quote_style);
  2185. r_result.insert(option.display, option);
  2186. }
  2187. }
  2188. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2189. } break;
  2190. case GDScriptParser::DataType::BUILTIN: {
  2191. if (base.get_type() == Variant::NIL) {
  2192. Callable::CallError err;
  2193. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2194. if (err.error != Callable::CallError::CALL_OK) {
  2195. return;
  2196. }
  2197. }
  2198. List<MethodInfo> methods;
  2199. base.get_method_list(&methods);
  2200. for (const MethodInfo &E : methods) {
  2201. if (E.name == p_method) {
  2202. r_arghint = _make_arguments_hint(E, p_argidx);
  2203. return;
  2204. }
  2205. }
  2206. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2207. } break;
  2208. default: {
  2209. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2210. } break;
  2211. }
  2212. }
  2213. }
  2214. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2215. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2216. if (p_argidx == 0 && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  2217. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2218. }
  2219. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2220. r_arghint = _make_arguments_hint(mi, p_argidx);
  2221. return;
  2222. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2223. return;
  2224. }
  2225. Variant base;
  2226. GDScriptParser::DataType base_type;
  2227. bool _static = false;
  2228. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2229. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  2230. GDScriptCompletionIdentifier connect_base;
  2231. if (Variant::has_utility_function(call->function_name)) {
  2232. MethodInfo info = Variant::get_utility_function_info(call->function_name);
  2233. r_arghint = _make_arguments_hint(info, p_argidx);
  2234. return;
  2235. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  2236. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  2237. r_arghint = _make_arguments_hint(info, p_argidx);
  2238. return;
  2239. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2240. // Complete constructor.
  2241. List<MethodInfo> constructors;
  2242. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2243. int i = 0;
  2244. for (const MethodInfo &E : constructors) {
  2245. if (p_argidx >= E.arguments.size()) {
  2246. continue;
  2247. }
  2248. if (i > 0) {
  2249. r_arghint += "\n";
  2250. }
  2251. r_arghint += _make_arguments_hint(E, p_argidx);
  2252. i++;
  2253. }
  2254. return;
  2255. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2256. base = p_context.base;
  2257. if (p_context.current_class) {
  2258. base_type = p_context.current_class->get_datatype();
  2259. _static = !p_context.current_function || p_context.current_function->is_static;
  2260. }
  2261. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2262. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2263. if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2264. const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
  2265. Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
  2266. if (method_type < Variant::VARIANT_MAX) {
  2267. Variant v;
  2268. Callable::CallError err;
  2269. Variant::construct(method_type, v, nullptr, 0, err);
  2270. if (err.error != Callable::CallError::CALL_OK) {
  2271. return;
  2272. }
  2273. List<MethodInfo> methods;
  2274. v.get_method_list(&methods);
  2275. for (MethodInfo &E : methods) {
  2276. if (p_argidx >= E.arguments.size()) {
  2277. continue;
  2278. }
  2279. if (E.name == call->function_name) {
  2280. r_arghint += _make_arguments_hint(E, p_argidx);
  2281. return;
  2282. }
  2283. }
  2284. }
  2285. }
  2286. if (subscript->is_attribute) {
  2287. GDScriptCompletionIdentifier ci;
  2288. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2289. base_type = ci.type;
  2290. base = ci.value;
  2291. } else {
  2292. return;
  2293. }
  2294. _static = base_type.is_meta_type;
  2295. }
  2296. } else {
  2297. return;
  2298. }
  2299. GDScriptCompletionIdentifier ci;
  2300. ci.type = base_type;
  2301. ci.value = base;
  2302. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2303. r_forced = r_result.size() > 0;
  2304. }
  2305. ::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) {
  2306. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2307. GDScriptParser parser;
  2308. GDScriptAnalyzer analyzer(&parser);
  2309. parser.parse(p_code, p_path, true);
  2310. analyzer.analyze();
  2311. r_forced = false;
  2312. HashMap<String, ScriptLanguage::CodeCompletionOption> options;
  2313. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2314. completion_context.base = p_owner;
  2315. bool is_function = false;
  2316. switch (completion_context.type) {
  2317. case GDScriptParser::COMPLETION_NONE:
  2318. break;
  2319. case GDScriptParser::COMPLETION_ANNOTATION: {
  2320. List<MethodInfo> annotations;
  2321. parser.get_annotation_list(&annotations);
  2322. for (const MethodInfo &E : annotations) {
  2323. ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2324. if (E.arguments.size() > 0) {
  2325. option.insert_text += "(";
  2326. }
  2327. options.insert(option.display, option);
  2328. }
  2329. r_forced = true;
  2330. } break;
  2331. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2332. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2333. break;
  2334. }
  2335. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2336. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2337. r_forced = true;
  2338. } break;
  2339. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  2340. // Constants.
  2341. {
  2342. List<StringName> constants;
  2343. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2344. for (const StringName &E : constants) {
  2345. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2346. bool valid = false;
  2347. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
  2348. if (valid) {
  2349. option.default_value = default_value;
  2350. }
  2351. options.insert(option.display, option);
  2352. }
  2353. }
  2354. // Methods.
  2355. {
  2356. List<StringName> methods;
  2357. Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
  2358. for (const StringName &E : methods) {
  2359. if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
  2360. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2361. if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
  2362. option.insert_text += "(";
  2363. } else {
  2364. option.insert_text += "()";
  2365. }
  2366. options.insert(option.display, option);
  2367. }
  2368. }
  2369. }
  2370. } break;
  2371. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2372. _list_available_types(true, completion_context, options);
  2373. r_forced = true;
  2374. } break;
  2375. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2376. ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2377. options.insert(option.display, option);
  2378. }
  2379. [[fallthrough]];
  2380. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2381. _list_available_types(false, completion_context, options);
  2382. r_forced = true;
  2383. } break;
  2384. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2385. _list_available_types(false, completion_context, options);
  2386. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2387. options.insert(get.display, get);
  2388. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2389. options.insert(set.display, set);
  2390. r_forced = true;
  2391. } break;
  2392. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2393. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2394. options.insert(get.display, get);
  2395. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2396. options.insert(set.display, set);
  2397. r_forced = true;
  2398. } break;
  2399. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2400. if (!completion_context.current_class) {
  2401. break;
  2402. }
  2403. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2404. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2405. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2406. continue;
  2407. }
  2408. if (member.function->is_static) {
  2409. continue;
  2410. }
  2411. ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2412. options.insert(option.display, option);
  2413. }
  2414. r_forced = true;
  2415. } break;
  2416. case GDScriptParser::COMPLETION_ASSIGN: {
  2417. GDScriptCompletionIdentifier type;
  2418. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2419. break;
  2420. }
  2421. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2422. _find_identifiers(completion_context, false, options, 0);
  2423. r_forced = true;
  2424. break;
  2425. }
  2426. if (!type.enumeration.is_empty()) {
  2427. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2428. r_forced = options.size() > 0;
  2429. } else {
  2430. _find_identifiers(completion_context, false, options, 0);
  2431. r_forced = true;
  2432. }
  2433. } break;
  2434. case GDScriptParser::COMPLETION_METHOD:
  2435. is_function = true;
  2436. [[fallthrough]];
  2437. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2438. _find_identifiers(completion_context, is_function, options, 0);
  2439. } break;
  2440. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2441. is_function = true;
  2442. [[fallthrough]];
  2443. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2444. r_forced = true;
  2445. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2446. if (attr->base) {
  2447. GDScriptCompletionIdentifier base;
  2448. if (!_guess_expression_type(completion_context, attr->base, base)) {
  2449. break;
  2450. }
  2451. _find_identifiers_in_base(base, is_function, options, 0);
  2452. }
  2453. } break;
  2454. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2455. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2456. GDScriptCompletionIdentifier base;
  2457. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2458. break;
  2459. }
  2460. GDScriptParser::CompletionContext c = completion_context;
  2461. c.current_function = nullptr;
  2462. c.current_suite = nullptr;
  2463. c.base = base.value.get_type() == Variant::OBJECT ? base.value.operator Object *() : nullptr;
  2464. if (base.type.kind == GDScriptParser::DataType::CLASS) {
  2465. c.current_class = base.type.class_type;
  2466. } else {
  2467. c.current_class = nullptr;
  2468. }
  2469. _find_identifiers_in_base(base, false, options, 0);
  2470. } break;
  2471. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2472. if (!completion_context.current_class) {
  2473. break;
  2474. }
  2475. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2476. bool found = true;
  2477. GDScriptCompletionIdentifier base;
  2478. base.type.kind = GDScriptParser::DataType::CLASS;
  2479. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2480. base.type.is_constant = true;
  2481. base.type.class_type = completion_context.current_class;
  2482. base.value = completion_context.base;
  2483. for (int i = 0; i < completion_context.current_argument; i++) {
  2484. GDScriptCompletionIdentifier ci;
  2485. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2486. found = false;
  2487. break;
  2488. }
  2489. base = ci;
  2490. }
  2491. // TODO: Improve this to only list types.
  2492. if (found) {
  2493. _find_identifiers_in_base(base, false, options, 0);
  2494. }
  2495. r_forced = true;
  2496. } break;
  2497. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2498. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths")) {
  2499. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2500. r_forced = true;
  2501. }
  2502. } break;
  2503. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2504. if (!completion_context.node) {
  2505. break;
  2506. }
  2507. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2508. } break;
  2509. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2510. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2511. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2512. switch (native_type.kind) {
  2513. case GDScriptParser::DataType::CLASS: {
  2514. native_type = native_type.class_type->base_type;
  2515. } break;
  2516. default: {
  2517. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2518. } break;
  2519. }
  2520. }
  2521. if (!native_type.is_set()) {
  2522. break;
  2523. }
  2524. StringName class_name = native_type.native_type;
  2525. if (!ClassDB::class_exists(class_name)) {
  2526. break;
  2527. }
  2528. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2529. List<MethodInfo> virtual_methods;
  2530. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2531. for (const MethodInfo &mi : virtual_methods) {
  2532. String method_hint = mi.name;
  2533. if (method_hint.contains(":")) {
  2534. method_hint = method_hint.get_slice(":", 0);
  2535. }
  2536. method_hint += "(";
  2537. if (mi.arguments.size()) {
  2538. for (int i = 0; i < mi.arguments.size(); i++) {
  2539. if (i > 0) {
  2540. method_hint += ", ";
  2541. }
  2542. String arg = mi.arguments[i].name;
  2543. if (arg.contains(":")) {
  2544. arg = arg.substr(0, arg.find(":"));
  2545. }
  2546. method_hint += arg;
  2547. if (use_type_hint && mi.arguments[i].type != Variant::NIL) {
  2548. method_hint += ": ";
  2549. if (mi.arguments[i].type == Variant::OBJECT && mi.arguments[i].class_name != StringName()) {
  2550. method_hint += mi.arguments[i].class_name.operator String();
  2551. } else {
  2552. method_hint += Variant::get_type_name(mi.arguments[i].type);
  2553. }
  2554. }
  2555. }
  2556. }
  2557. method_hint += ")";
  2558. if (use_type_hint && (mi.return_val.type != Variant::NIL || !(mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT))) {
  2559. method_hint += " -> ";
  2560. if (mi.return_val.type == Variant::NIL) {
  2561. method_hint += "void";
  2562. } else if (mi.return_val.type == Variant::OBJECT && mi.return_val.class_name != StringName()) {
  2563. method_hint += mi.return_val.class_name.operator String();
  2564. } else {
  2565. method_hint += Variant::get_type_name(mi.return_val.type);
  2566. }
  2567. }
  2568. method_hint += ":";
  2569. ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2570. options.insert(option.display, option);
  2571. }
  2572. } break;
  2573. case GDScriptParser::COMPLETION_GET_NODE: {
  2574. // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
  2575. if (p_owner) {
  2576. List<String> opts;
  2577. p_owner->get_argument_options("get_node", 0, &opts);
  2578. for (const String &E : opts) {
  2579. r_forced = true;
  2580. String opt = E.strip_edges();
  2581. if (opt.is_quoted()) {
  2582. // Remove quotes so that we can handle user preferred quote style,
  2583. // or handle NodePaths which are valid identifiers and don't need quotes.
  2584. opt = opt.unquote();
  2585. }
  2586. // The path needs quotes if it's not a valid identifier (with an exception
  2587. // for "/" as path separator, which also doesn't require quotes).
  2588. if (!opt.replace("/", "_").is_valid_identifier()) {
  2589. opt = opt.quote(quote_style); // Handle user preference.
  2590. }
  2591. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2592. options.insert(option.display, option);
  2593. }
  2594. // Get autoloads.
  2595. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  2596. String path = "/root/" + E.key;
  2597. ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2598. options.insert(option.display, option);
  2599. }
  2600. }
  2601. } break;
  2602. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2603. if (!completion_context.current_class) {
  2604. break;
  2605. }
  2606. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2607. } break;
  2608. }
  2609. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
  2610. r_options->push_back(E.value);
  2611. }
  2612. return OK;
  2613. }
  2614. #else
  2615. 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) {
  2616. return OK;
  2617. }
  2618. #endif
  2619. //////// END COMPLETION //////////
  2620. String GDScriptLanguage::_get_indentation() const {
  2621. #ifdef TOOLS_ENABLED
  2622. if (Engine::get_singleton()->is_editor_hint()) {
  2623. bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
  2624. if (use_space_indentation) {
  2625. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  2626. String space_indent = "";
  2627. for (int i = 0; i < indent_size; i++) {
  2628. space_indent += " ";
  2629. }
  2630. return space_indent;
  2631. }
  2632. }
  2633. #endif
  2634. return "\t";
  2635. }
  2636. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2637. String indent = _get_indentation();
  2638. Vector<String> lines = p_code.split("\n");
  2639. List<int> indent_stack;
  2640. for (int i = 0; i < lines.size(); i++) {
  2641. String l = lines[i];
  2642. int tc = 0;
  2643. for (int j = 0; j < l.length(); j++) {
  2644. if (l[j] == ' ' || l[j] == '\t') {
  2645. tc++;
  2646. } else {
  2647. break;
  2648. }
  2649. }
  2650. String st = l.substr(tc, l.length()).strip_edges();
  2651. if (st.is_empty() || st.begins_with("#")) {
  2652. continue; //ignore!
  2653. }
  2654. int ilevel = 0;
  2655. if (indent_stack.size()) {
  2656. ilevel = indent_stack.back()->get();
  2657. }
  2658. if (tc > ilevel) {
  2659. indent_stack.push_back(tc);
  2660. } else if (tc < ilevel) {
  2661. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2662. indent_stack.pop_back();
  2663. }
  2664. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2665. indent_stack.push_back(tc); // this is not right but gets the job done
  2666. }
  2667. }
  2668. if (i >= p_from_line) {
  2669. l = "";
  2670. for (int j = 0; j < indent_stack.size(); j++) {
  2671. l += indent;
  2672. }
  2673. l += st;
  2674. } else if (i > p_to_line) {
  2675. break;
  2676. }
  2677. lines.write[i] = l;
  2678. }
  2679. p_code = "";
  2680. for (int i = 0; i < lines.size(); i++) {
  2681. if (i > 0) {
  2682. p_code += "\n";
  2683. }
  2684. p_code += lines[i];
  2685. }
  2686. }
  2687. #ifdef TOOLS_ENABLED
  2688. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2689. GDScriptParser::DataType base_type = p_base;
  2690. while (base_type.is_set()) {
  2691. switch (base_type.kind) {
  2692. case GDScriptParser::DataType::CLASS: {
  2693. if (base_type.class_type) {
  2694. if (base_type.class_type->has_member(p_symbol)) {
  2695. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2696. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2697. r_result.class_path = base_type.script_path;
  2698. r_result.script = GDScriptCache::get_shallow_script(r_result.class_path);
  2699. return OK;
  2700. }
  2701. base_type = base_type.class_type->base_type;
  2702. }
  2703. } break;
  2704. case GDScriptParser::DataType::SCRIPT: {
  2705. Ref<Script> scr = base_type.script_type;
  2706. if (scr.is_valid()) {
  2707. int line = scr->get_member_line(p_symbol);
  2708. if (line >= 0) {
  2709. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2710. r_result.location = line;
  2711. r_result.script = scr;
  2712. return OK;
  2713. }
  2714. Ref<Script> base_script = scr->get_base_script();
  2715. if (base_script.is_valid()) {
  2716. base_type.script_type = base_script;
  2717. } else {
  2718. base_type.kind = GDScriptParser::DataType::NATIVE;
  2719. base_type.native_type = scr->get_instance_base_type();
  2720. }
  2721. } else {
  2722. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2723. }
  2724. } break;
  2725. case GDScriptParser::DataType::NATIVE: {
  2726. StringName class_name = base_type.native_type;
  2727. if (!ClassDB::class_exists(class_name)) {
  2728. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2729. break;
  2730. }
  2731. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2732. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2733. r_result.class_name = base_type.native_type;
  2734. r_result.class_member = p_symbol;
  2735. return OK;
  2736. }
  2737. List<MethodInfo> virtual_methods;
  2738. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2739. for (const MethodInfo &E : virtual_methods) {
  2740. if (E.name == p_symbol) {
  2741. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2742. r_result.class_name = base_type.native_type;
  2743. r_result.class_member = p_symbol;
  2744. return OK;
  2745. }
  2746. }
  2747. if (ClassDB::has_signal(class_name, p_symbol, true)) {
  2748. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  2749. r_result.class_name = base_type.native_type;
  2750. r_result.class_member = p_symbol;
  2751. return OK;
  2752. }
  2753. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2754. if (enum_name != StringName()) {
  2755. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2756. r_result.class_name = base_type.native_type;
  2757. r_result.class_member = enum_name;
  2758. return OK;
  2759. }
  2760. List<String> constants;
  2761. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2762. for (const String &E : constants) {
  2763. if (E == p_symbol) {
  2764. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2765. r_result.class_name = base_type.native_type;
  2766. r_result.class_member = p_symbol;
  2767. return OK;
  2768. }
  2769. }
  2770. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2771. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2772. r_result.class_name = base_type.native_type;
  2773. r_result.class_member = p_symbol;
  2774. return OK;
  2775. }
  2776. StringName parent = ClassDB::get_parent_class(class_name);
  2777. if (parent != StringName()) {
  2778. base_type.native_type = parent;
  2779. } else {
  2780. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2781. }
  2782. } break;
  2783. case GDScriptParser::DataType::BUILTIN: {
  2784. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2785. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2786. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2787. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2788. r_result.class_member = p_symbol;
  2789. return OK;
  2790. }
  2791. Variant v;
  2792. Ref<RefCounted> v_ref;
  2793. if (base_type.builtin_type == Variant::OBJECT) {
  2794. v_ref.instantiate();
  2795. v = v_ref;
  2796. } else {
  2797. Callable::CallError err;
  2798. Variant::construct(base_type.builtin_type, v, nullptr, 0, err);
  2799. if (err.error != Callable::CallError::CALL_OK) {
  2800. break;
  2801. }
  2802. }
  2803. if (v.has_method(p_symbol)) {
  2804. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2805. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2806. r_result.class_member = p_symbol;
  2807. return OK;
  2808. }
  2809. bool valid = false;
  2810. v.get(p_symbol, &valid);
  2811. if (valid) {
  2812. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2813. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2814. r_result.class_member = p_symbol;
  2815. return OK;
  2816. }
  2817. } break;
  2818. default: {
  2819. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2820. } break;
  2821. }
  2822. }
  2823. return ERR_CANT_RESOLVE;
  2824. }
  2825. ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  2826. // Before parsing, try the usual stuff.
  2827. if (ClassDB::class_exists(p_symbol)) {
  2828. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2829. r_result.class_name = p_symbol;
  2830. return OK;
  2831. }
  2832. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2833. Variant::Type t = Variant::Type(i);
  2834. if (Variant::get_type_name(t) == p_symbol) {
  2835. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2836. r_result.class_name = Variant::get_type_name(t);
  2837. return OK;
  2838. }
  2839. }
  2840. // Need special checks for assert and preload as they are technically
  2841. // keywords, so are not registered in GDScriptUtilityFunctions.
  2842. if (GDScriptUtilityFunctions::function_exists(p_symbol) || "assert" == p_symbol || "preload" == p_symbol) {
  2843. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2844. r_result.class_name = "@GDScript";
  2845. r_result.class_member = p_symbol;
  2846. return OK;
  2847. }
  2848. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  2849. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2850. r_result.class_name = "@GDScript";
  2851. r_result.class_member = p_symbol;
  2852. return OK;
  2853. }
  2854. GDScriptParser parser;
  2855. parser.parse(p_code, p_path, true);
  2856. GDScriptAnalyzer analyzer(&parser);
  2857. analyzer.analyze();
  2858. GDScriptParser::CompletionContext context = parser.get_completion_context();
  2859. if (context.current_class && context.current_class->extends.size() > 0) {
  2860. bool success = false;
  2861. ClassDB::get_integer_constant(context.current_class->extends[0], p_symbol, &success);
  2862. if (success) {
  2863. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2864. r_result.class_name = context.current_class->extends[0];
  2865. r_result.class_member = p_symbol;
  2866. return OK;
  2867. }
  2868. }
  2869. bool is_function = false;
  2870. switch (context.type) {
  2871. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  2872. if (!Variant::has_builtin_method(context.builtin_type, StringName(p_symbol))) {
  2873. // A constant.
  2874. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2875. r_result.class_name = Variant::get_type_name(context.builtin_type);
  2876. r_result.class_member = p_symbol;
  2877. return OK;
  2878. }
  2879. // A method.
  2880. GDScriptParser::DataType base_type;
  2881. base_type.kind = GDScriptParser::DataType::BUILTIN;
  2882. base_type.builtin_type = context.builtin_type;
  2883. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  2884. return OK;
  2885. }
  2886. } break;
  2887. case GDScriptParser::COMPLETION_SUPER_METHOD:
  2888. case GDScriptParser::COMPLETION_METHOD: {
  2889. is_function = true;
  2890. [[fallthrough]];
  2891. }
  2892. case GDScriptParser::COMPLETION_ASSIGN:
  2893. case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
  2894. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2895. GDScriptParser::DataType base_type;
  2896. if (context.current_class) {
  2897. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  2898. base_type = context.current_class->get_datatype();
  2899. } else {
  2900. base_type = context.current_class->base_type;
  2901. }
  2902. } else {
  2903. break;
  2904. }
  2905. if (!is_function && context.current_suite) {
  2906. // Lookup local variables.
  2907. const GDScriptParser::SuiteNode *suite = context.current_suite;
  2908. while (suite) {
  2909. if (suite->has_local(p_symbol)) {
  2910. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2911. r_result.location = suite->get_local(p_symbol).start_line;
  2912. return OK;
  2913. }
  2914. suite = suite->parent_block;
  2915. }
  2916. }
  2917. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  2918. return OK;
  2919. }
  2920. if (!is_function) {
  2921. // Guess in autoloads as singletons.
  2922. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  2923. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  2924. if (autoload.is_singleton) {
  2925. String script = autoload.path;
  2926. if (!script.ends_with(".gd")) {
  2927. // Not a script, try find the script anyway,
  2928. // may have some success.
  2929. script = script.get_basename() + ".gd";
  2930. }
  2931. if (FileAccess::exists(script)) {
  2932. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2933. r_result.location = 0;
  2934. r_result.script = ResourceLoader::load(script);
  2935. return OK;
  2936. }
  2937. }
  2938. }
  2939. // Global.
  2940. HashMap<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  2941. if (classes.has(p_symbol)) {
  2942. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  2943. if (value.get_type() == Variant::OBJECT) {
  2944. Object *obj = value;
  2945. if (obj) {
  2946. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  2947. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2948. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  2949. } else {
  2950. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  2951. r_result.class_name = obj->get_class();
  2952. }
  2953. // proxy class remove the underscore.
  2954. if (r_result.class_name.begins_with("_")) {
  2955. r_result.class_name = r_result.class_name.substr(1);
  2956. }
  2957. return OK;
  2958. }
  2959. } else {
  2960. /*
  2961. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  2962. // We cannot determine the exact nature of the identifier here
  2963. // Otherwise these codes would work
  2964. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  2965. if (enumName != nullptr) {
  2966. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2967. r_result.class_name = "@GlobalScope";
  2968. r_result.class_member = enumName;
  2969. return OK;
  2970. }
  2971. else {
  2972. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2973. r_result.class_name = "@GlobalScope";
  2974. r_result.class_member = p_symbol;
  2975. return OK;
  2976. }*/
  2977. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  2978. r_result.class_name = "@GlobalScope";
  2979. r_result.class_member = p_symbol;
  2980. return OK;
  2981. }
  2982. } else {
  2983. List<StringName> utility_functions;
  2984. Variant::get_utility_function_list(&utility_functions);
  2985. if (utility_functions.find(p_symbol) != nullptr) {
  2986. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  2987. r_result.class_name = "@GlobalScope";
  2988. r_result.class_member = p_symbol;
  2989. return OK;
  2990. }
  2991. }
  2992. }
  2993. } break;
  2994. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  2995. is_function = true;
  2996. [[fallthrough]];
  2997. }
  2998. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2999. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  3000. break;
  3001. }
  3002. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  3003. if (!subscript->is_attribute) {
  3004. break;
  3005. }
  3006. GDScriptCompletionIdentifier base;
  3007. if (!_guess_expression_type(context, subscript->base, base)) {
  3008. break;
  3009. }
  3010. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  3011. return OK;
  3012. }
  3013. } break;
  3014. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  3015. GDScriptParser::DataType base_type = context.current_class->base_type;
  3016. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3017. return OK;
  3018. }
  3019. } break;
  3020. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  3021. case GDScriptParser::COMPLETION_TYPE_NAME: {
  3022. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  3023. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  3024. return OK;
  3025. }
  3026. } break;
  3027. default: {
  3028. }
  3029. }
  3030. return ERR_CANT_RESOLVE;
  3031. }
  3032. #endif