gdscript_editor.cpp 118 KB

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