gdscript_editor.cpp 109 KB

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