gdscript_editor.cpp 111 KB

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