2
0

gdscript_editor.cpp 110 KB

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