gd_editor.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  1. /*************************************************************************/
  2. /* gd_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/editor_settings.h"
  31. #include "gd_compiler.h"
  32. #include "gd_script.h"
  33. #include "global_constants.h"
  34. #include "os/file_access.h"
  35. #include "project_settings.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_file_system.h"
  38. #include "editor/editor_settings.h"
  39. #include "engine.h"
  40. #endif
  41. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  42. p_delimiters->push_back("#");
  43. p_delimiters->push_back("\"\"\" \"\"\"");
  44. }
  45. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  46. p_delimiters->push_back("\" \"");
  47. p_delimiters->push_back("' '");
  48. }
  49. Ref<Script> GDScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  50. String _template = String() +
  51. "extends %BASE%\n\n" +
  52. "# class member variables go here, for example:\n" +
  53. "# var a = 2\n" +
  54. "# var b = \"textvar\"\n\n" +
  55. "func _ready():\n" +
  56. "%TS%# Called every time the node is added to the scene.\n" +
  57. "%TS%# Initialization here\n" +
  58. "%TS%pass\n";
  59. _template = _template.replace("%BASE%", p_base_class_name);
  60. _template = _template.replace("%TS%", _get_indentation());
  61. Ref<GDScript> script;
  62. script.instance();
  63. script->set_source_code(_template);
  64. return script;
  65. }
  66. bool GDScriptLanguage::is_using_templates() {
  67. return true;
  68. }
  69. void GDScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  70. String src = p_script->get_source_code();
  71. src = src.replace("%BASE%", p_base_class_name);
  72. src = src.replace("%TS%", _get_indentation());
  73. p_script->set_source_code(src);
  74. }
  75. 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) const {
  76. GDParser parser;
  77. Error err = parser.parse(p_script, p_path.get_base_dir(), true, p_path);
  78. if (err) {
  79. r_line_error = parser.get_error_line();
  80. r_col_error = parser.get_error_column();
  81. r_test_error = parser.get_error();
  82. return false;
  83. } else {
  84. const GDParser::Node *root = parser.get_parse_tree();
  85. ERR_FAIL_COND_V(root->type != GDParser::Node::TYPE_CLASS, false);
  86. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  87. Map<int, String> funcs;
  88. for (int i = 0; i < cl->functions.size(); i++) {
  89. funcs[cl->functions[i]->line] = cl->functions[i]->name;
  90. }
  91. for (int i = 0; i < cl->static_functions.size(); i++) {
  92. funcs[cl->static_functions[i]->line] = cl->static_functions[i]->name;
  93. }
  94. for (Map<int, String>::Element *E = funcs.front(); E; E = E->next()) {
  95. r_functions->push_back(E->get() + ":" + itos(E->key()));
  96. }
  97. }
  98. return true;
  99. }
  100. bool GDScriptLanguage::has_named_classes() const {
  101. return false;
  102. }
  103. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  104. GDTokenizerText tokenizer;
  105. tokenizer.set_code(p_code);
  106. int indent = 0;
  107. while (tokenizer.get_token() != GDTokenizer::TK_EOF && tokenizer.get_token() != GDTokenizer::TK_ERROR) {
  108. if (tokenizer.get_token() == GDTokenizer::TK_NEWLINE) {
  109. indent = tokenizer.get_token_line_indent();
  110. }
  111. //print_line("TOKEN: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  112. if (indent == 0 && tokenizer.get_token() == GDTokenizer::TK_PR_FUNCTION && tokenizer.get_token(1) == GDTokenizer::TK_IDENTIFIER) {
  113. String identifier = tokenizer.get_token_identifier(1);
  114. if (identifier == p_function) {
  115. return tokenizer.get_token_line();
  116. }
  117. }
  118. tokenizer.advance();
  119. //print_line("NEXT: "+String(GDTokenizer::get_token_name(tokenizer.get_token())));
  120. }
  121. return -1;
  122. }
  123. Script *GDScriptLanguage::create_script() const {
  124. return memnew(GDScript);
  125. }
  126. /* DEBUGGER FUNCTIONS */
  127. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  128. //break because of parse error
  129. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  130. _debug_parse_err_line = p_line;
  131. _debug_parse_err_file = p_file;
  132. _debug_error = p_error;
  133. ScriptDebugger::get_singleton()->debug(this, false);
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  140. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  141. _debug_parse_err_line = -1;
  142. _debug_parse_err_file = "";
  143. _debug_error = p_error;
  144. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  145. return true;
  146. } else {
  147. return false;
  148. }
  149. }
  150. String GDScriptLanguage::debug_get_error() const {
  151. return _debug_error;
  152. }
  153. int GDScriptLanguage::debug_get_stack_level_count() const {
  154. if (_debug_parse_err_line >= 0)
  155. return 1;
  156. return _debug_call_stack_pos;
  157. }
  158. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  159. if (_debug_parse_err_line >= 0)
  160. return _debug_parse_err_line;
  161. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  162. int l = _debug_call_stack_pos - p_level - 1;
  163. return *(_call_stack[l].line);
  164. }
  165. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  166. if (_debug_parse_err_line >= 0)
  167. return "";
  168. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  169. int l = _debug_call_stack_pos - p_level - 1;
  170. return _call_stack[l].function->get_name();
  171. }
  172. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  173. if (_debug_parse_err_line >= 0)
  174. return _debug_parse_err_file;
  175. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  176. int l = _debug_call_stack_pos - p_level - 1;
  177. return _call_stack[l].function->get_source();
  178. }
  179. 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) {
  180. if (_debug_parse_err_line >= 0)
  181. return;
  182. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  183. int l = _debug_call_stack_pos - p_level - 1;
  184. GDFunction *f = _call_stack[l].function;
  185. List<Pair<StringName, int> > locals;
  186. f->debug_get_stack_member_state(*_call_stack[l].line, &locals);
  187. for (List<Pair<StringName, int> >::Element *E = locals.front(); E; E = E->next()) {
  188. p_locals->push_back(E->get().first);
  189. p_values->push_back(_call_stack[l].stack[E->get().second]);
  190. }
  191. }
  192. 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) {
  193. if (_debug_parse_err_line >= 0)
  194. return;
  195. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  196. int l = _debug_call_stack_pos - p_level - 1;
  197. GDInstance *instance = _call_stack[l].instance;
  198. if (!instance)
  199. return;
  200. Ref<GDScript> script = instance->get_script();
  201. ERR_FAIL_COND(script.is_null());
  202. const Map<StringName, GDScript::MemberInfo> &mi = script->debug_get_member_indices();
  203. for (const Map<StringName, GDScript::MemberInfo>::Element *E = mi.front(); E; E = E->next()) {
  204. p_members->push_back(E->key());
  205. p_values->push_back(instance->debug_get_member_by_index(E->get().index));
  206. }
  207. }
  208. void GDScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  209. //no globals are really reachable in gdscript
  210. }
  211. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  212. if (_debug_parse_err_line >= 0)
  213. return "";
  214. return "";
  215. }
  216. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  217. p_extensions->push_back("gd");
  218. }
  219. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  220. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  221. p_functions->push_back(GDFunctions::get_info(GDFunctions::Function(i)));
  222. }
  223. //not really "functions", but..
  224. {
  225. MethodInfo mi;
  226. mi.name = "preload:Resource";
  227. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  228. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  229. p_functions->push_back(mi);
  230. }
  231. {
  232. MethodInfo mi;
  233. mi.name = "yield:GDFunctionState";
  234. mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  235. mi.arguments.push_back(PropertyInfo(Variant::STRING, "signal"));
  236. mi.default_arguments.push_back(Variant::NIL);
  237. mi.default_arguments.push_back(Variant::STRING);
  238. p_functions->push_back(mi);
  239. }
  240. {
  241. MethodInfo mi;
  242. mi.name = "assert";
  243. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  244. p_functions->push_back(mi);
  245. }
  246. }
  247. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  248. Pair<String, Variant> pi;
  249. pi.first = "PI";
  250. pi.second = Math_PI;
  251. p_constants->push_back(pi);
  252. Pair<String, Variant> infinity;
  253. infinity.first = "INF";
  254. infinity.second = Math_INF;
  255. p_constants->push_back(infinity);
  256. Pair<String, Variant> nan;
  257. nan.first = "NAN";
  258. nan.second = Math_NAN;
  259. p_constants->push_back(nan);
  260. }
  261. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  262. String s = "func " + p_name + "(";
  263. if (p_args.size()) {
  264. s += " ";
  265. for (int i = 0; i < p_args.size(); i++) {
  266. if (i > 0)
  267. s += ", ";
  268. s += p_args[i].get_slice(":", 0);
  269. }
  270. s += " ";
  271. }
  272. s += "):\n\tpass # replace with function body\n";
  273. return s;
  274. }
  275. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  276. struct GDCompletionIdentifier {
  277. String enumeration;
  278. StringName obj_type;
  279. Ref<GDScript> script;
  280. Variant::Type type;
  281. Variant value; //im case there is a value, also return it
  282. };
  283. static GDCompletionIdentifier _get_type_from_variant(const Variant &p_variant) {
  284. GDCompletionIdentifier t;
  285. t.type = p_variant.get_type();
  286. t.value = p_variant;
  287. if (p_variant.get_type() == Variant::OBJECT) {
  288. Object *obj = p_variant;
  289. if (obj) {
  290. /*
  291. if (obj->cast_to<GDNativeClass>()) {
  292. t.obj_type=obj->cast_to<GDNativeClass>()->get_name();
  293. t.value=Variant();
  294. } else {
  295. */
  296. t.obj_type = obj->get_class();
  297. //}
  298. }
  299. }
  300. return t;
  301. }
  302. static GDCompletionIdentifier _get_type_from_pinfo(const PropertyInfo &p_info) {
  303. GDCompletionIdentifier t;
  304. t.type = p_info.type;
  305. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  306. t.obj_type = p_info.hint_string;
  307. }
  308. return t;
  309. }
  310. struct GDCompletionContext {
  311. const GDParser::ClassNode *_class;
  312. const GDParser::FunctionNode *function;
  313. const GDParser::BlockNode *block;
  314. Object *base;
  315. String base_path;
  316. };
  317. static Ref<Reference> _get_parent_class(GDCompletionContext &context) {
  318. if (context._class->extends_used) {
  319. //do inheritance
  320. String path = context._class->extends_file;
  321. Ref<GDScript> script;
  322. Ref<GDNativeClass> native;
  323. if (path != "") {
  324. //path (and optionally subclasses)
  325. if (path.is_rel_path()) {
  326. path = context.base_path.plus_file(path);
  327. }
  328. if (ScriptCodeCompletionCache::get_sigleton())
  329. script = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(path);
  330. else
  331. script = ResourceLoader::load(path);
  332. if (script.is_null()) {
  333. return REF();
  334. }
  335. if (!script->is_valid()) {
  336. return REF();
  337. }
  338. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  339. if (context._class->extends_class.size()) {
  340. for (int i = 0; i < context._class->extends_class.size(); i++) {
  341. String sub = context._class->extends_class[i];
  342. if (script->get_subclasses().has(sub)) {
  343. script = script->get_subclasses()[sub];
  344. } else {
  345. return REF();
  346. }
  347. }
  348. }
  349. if (script.is_valid())
  350. return script;
  351. } else {
  352. if (context._class->extends_class.size() == 0) {
  353. ERR_PRINT("BUG");
  354. return REF();
  355. }
  356. String base = context._class->extends_class[0];
  357. if (context._class->extends_class.size() > 1) {
  358. return REF();
  359. }
  360. //if not found, try engine classes
  361. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  362. return REF();
  363. }
  364. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  365. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  366. return native;
  367. }
  368. }
  369. return Ref<Reference>();
  370. }
  371. static GDCompletionIdentifier _get_native_class(GDCompletionContext &context) {
  372. //eeh...
  373. GDCompletionIdentifier id;
  374. id.type = Variant::NIL;
  375. REF pc = _get_parent_class(context);
  376. if (!pc.is_valid()) {
  377. return id;
  378. }
  379. Ref<GDNativeClass> nc = pc;
  380. Ref<GDScript> s = pc;
  381. if (s.is_null() && nc.is_null()) {
  382. return id;
  383. }
  384. while (!s.is_null()) {
  385. nc = s->get_native();
  386. s = s->get_base();
  387. }
  388. if (nc.is_null()) {
  389. return id;
  390. }
  391. id.type = Variant::OBJECT;
  392. if (context.base)
  393. id.value = context.base;
  394. id.obj_type = nc->get_name();
  395. return id;
  396. }
  397. static bool _guess_identifier_type(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type);
  398. static bool _guess_expression_type(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, GDCompletionIdentifier &r_type) {
  399. if (p_node->type == GDParser::Node::TYPE_CONSTANT) {
  400. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(p_node);
  401. r_type = _get_type_from_variant(cn->value);
  402. return true;
  403. } else if (p_node->type == GDParser::Node::TYPE_DICTIONARY) {
  404. r_type.type = Variant::DICTIONARY;
  405. //what the heck, fill it anyway
  406. const GDParser::DictionaryNode *an = static_cast<const GDParser::DictionaryNode *>(p_node);
  407. Dictionary d;
  408. for (int i = 0; i < an->elements.size(); i++) {
  409. GDCompletionIdentifier k;
  410. if (_guess_expression_type(context, an->elements[i].key, p_line, k) && k.value.get_type() != Variant::NIL) {
  411. GDCompletionIdentifier v;
  412. if (_guess_expression_type(context, an->elements[i].value, p_line, v)) {
  413. d[k.value] = v.value;
  414. }
  415. }
  416. }
  417. r_type.value = d;
  418. return true;
  419. } else if (p_node->type == GDParser::Node::TYPE_ARRAY) {
  420. r_type.type = Variant::ARRAY;
  421. //what the heck, fill it anyway
  422. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(p_node);
  423. Array arr;
  424. arr.resize(an->elements.size());
  425. for (int i = 0; i < an->elements.size(); i++) {
  426. GDCompletionIdentifier ci;
  427. if (_guess_expression_type(context, an->elements[i], p_line, ci)) {
  428. arr[i] = ci.value;
  429. }
  430. }
  431. r_type.value = arr;
  432. return true;
  433. } else if (p_node->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  434. MethodInfo mi = GDFunctions::get_info(static_cast<const GDParser::BuiltInFunctionNode *>(p_node)->function);
  435. r_type = _get_type_from_pinfo(mi.return_val);
  436. return true;
  437. } else if (p_node->type == GDParser::Node::TYPE_IDENTIFIER) {
  438. return _guess_identifier_type(context, p_line - 1, static_cast<const GDParser::IdentifierNode *>(p_node)->name, r_type);
  439. } else if (p_node->type == GDParser::Node::TYPE_SELF) {
  440. //eeh...
  441. r_type = _get_native_class(context);
  442. return r_type.type != Variant::NIL;
  443. } else if (p_node->type == GDParser::Node::TYPE_OPERATOR) {
  444. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  445. if (op->op == GDParser::OperatorNode::OP_CALL) {
  446. if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) {
  447. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  448. r_type.type = tn->vtype;
  449. return true;
  450. } else if (op->arguments[0]->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  451. const GDParser::BuiltInFunctionNode *bin = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  452. return _guess_expression_type(context, bin, p_line, r_type);
  453. } else if (op->arguments.size() > 1 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  454. GDCompletionIdentifier base;
  455. if (!_guess_expression_type(context, op->arguments[0], p_line, base))
  456. return false;
  457. StringName id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  458. if (base.type == Variant::OBJECT) {
  459. if (id.operator String() == "new" && base.value.get_type() == Variant::OBJECT) {
  460. Object *obj = base.value;
  461. if (obj && obj->cast_to<GDNativeClass>()) {
  462. GDNativeClass *gdnc = obj->cast_to<GDNativeClass>();
  463. r_type.type = Variant::OBJECT;
  464. r_type.value = Variant();
  465. r_type.obj_type = gdnc->get_name();
  466. return true;
  467. }
  468. }
  469. if (ClassDB::has_method(base.obj_type, id)) {
  470. #ifdef TOOLS_ENABLED
  471. MethodBind *mb = ClassDB::get_method(base.obj_type, id);
  472. PropertyInfo pi = mb->get_return_info();
  473. //try calling the function if constant and all args are constant, should not crash..
  474. Object *baseptr = base.value;
  475. if (mb->is_const() && pi.type == Variant::OBJECT) {
  476. bool all_valid = true;
  477. Vector<Variant> args;
  478. for (int i = 2; i < op->arguments.size(); i++) {
  479. GDCompletionIdentifier arg;
  480. if (_guess_expression_type(context, op->arguments[i], p_line, arg)) {
  481. if (arg.value.get_type() != Variant::NIL && arg.value.get_type() != Variant::OBJECT) { // calling with object seems dangerous, i don' t know
  482. args.push_back(arg.value);
  483. } else {
  484. all_valid = false;
  485. break;
  486. }
  487. } else {
  488. all_valid = false;
  489. }
  490. }
  491. if (all_valid && String(id) == "get_node" && ClassDB::is_parent_class(base.obj_type, "Node") && args.size()) {
  492. String arg1 = args[0];
  493. if (arg1.begins_with("/root/")) {
  494. String which = arg1.get_slice("/", 2);
  495. if (which != "") {
  496. List<PropertyInfo> props;
  497. ProjectSettings::get_singleton()->get_property_list(&props);
  498. //print_line("find singleton");
  499. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  500. String s = E->get().name;
  501. if (!s.begins_with("autoload/"))
  502. continue;
  503. //print_line("found "+s);
  504. String name = s.get_slice("/", 1);
  505. //print_line("name: "+name+", which: "+which);
  506. if (name == which) {
  507. String script = ProjectSettings::get_singleton()->get(s);
  508. if (!script.begins_with("res://")) {
  509. script = "res://" + script;
  510. }
  511. if (!script.ends_with(".gd")) {
  512. //not a script, try find the script anyway,
  513. //may have some success
  514. script = script.get_basename() + ".gd";
  515. }
  516. if (FileAccess::exists(script)) {
  517. //print_line("is a script");
  518. Ref<Script> scr;
  519. if (ScriptCodeCompletionCache::get_sigleton())
  520. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  521. else
  522. scr = ResourceLoader::load(script);
  523. r_type.obj_type = "Node";
  524. r_type.type = Variant::OBJECT;
  525. r_type.script = scr;
  526. r_type.value = Variant();
  527. return true;
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. if (baseptr) {
  535. if (all_valid) {
  536. Vector<const Variant *> argptr;
  537. for (int i = 0; i < args.size(); i++) {
  538. argptr.push_back(&args[i]);
  539. }
  540. Variant::CallError ce;
  541. Variant ret = mb->call(baseptr, argptr.ptr(), argptr.size(), ce);
  542. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  543. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != NULL) {
  544. r_type = _get_type_from_variant(ret);
  545. return true;
  546. }
  547. }
  548. }
  549. }
  550. }
  551. r_type.type = pi.type;
  552. if (pi.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  553. r_type.obj_type = pi.hint_string;
  554. }
  555. return true;
  556. #else
  557. return false;
  558. #endif
  559. } else {
  560. return false;
  561. }
  562. } else {
  563. //method for some variant..
  564. Variant::CallError ce;
  565. Variant v = Variant::construct(base.type, NULL, 0, ce);
  566. List<MethodInfo> mi;
  567. v.get_method_list(&mi);
  568. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  569. if (!E->get().name.begins_with("_") && E->get().name == id.operator String()) {
  570. MethodInfo mi = E->get();
  571. r_type.type = mi.return_val.type;
  572. if (mi.return_val.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  573. r_type.obj_type = mi.return_val.hint_string;
  574. }
  575. return true;
  576. }
  577. }
  578. }
  579. }
  580. } else if (op->op == GDParser::OperatorNode::OP_INDEX || op->op == GDParser::OperatorNode::OP_INDEX_NAMED) {
  581. GDCompletionIdentifier p1;
  582. GDCompletionIdentifier p2;
  583. if (op->op == GDParser::OperatorNode::OP_INDEX_NAMED) {
  584. if (op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  585. String id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1])->name;
  586. p2.type = Variant::STRING;
  587. p2.value = id;
  588. }
  589. } else {
  590. if (op->arguments[1]) {
  591. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  592. return false;
  593. }
  594. }
  595. }
  596. if (op->arguments[0]->type == GDParser::Node::TYPE_ARRAY) {
  597. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode *>(op->arguments[0]);
  598. if (p2.value.is_num()) {
  599. int index = p2.value;
  600. if (index < 0 || index >= an->elements.size())
  601. return false;
  602. return _guess_expression_type(context, an->elements[index], p_line, r_type);
  603. }
  604. } else if (op->arguments[0]->type == GDParser::Node::TYPE_DICTIONARY) {
  605. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode *>(op->arguments[0]);
  606. if (p2.value.get_type() == Variant::NIL)
  607. return false;
  608. for (int i = 0; i < dn->elements.size(); i++) {
  609. GDCompletionIdentifier k;
  610. if (!_guess_expression_type(context, dn->elements[i].key, p_line, k)) {
  611. return false;
  612. }
  613. if (k.value.get_type() == Variant::NIL)
  614. return false;
  615. if (k.value == p2.value) {
  616. return _guess_expression_type(context, dn->elements[i].value, p_line, r_type);
  617. }
  618. }
  619. } else {
  620. if (op->arguments[0]) {
  621. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  622. return false;
  623. }
  624. }
  625. if (p1.value.get_type() == Variant::OBJECT) {
  626. //??
  627. if (p1.obj_type != StringName() && p2.type == Variant::STRING) {
  628. StringName index = p2.value;
  629. bool valid;
  630. Variant::Type t = ClassDB::get_property_type(p1.obj_type, index, &valid);
  631. if (t != Variant::NIL && valid) {
  632. r_type.type = t;
  633. if (t == Variant::INT) {
  634. //check for enum!
  635. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  636. StringName getter = ClassDB::get_property_getter(p1.obj_type, index);
  637. if (getter != StringName()) {
  638. MethodBind *mb = ClassDB::get_method(p1.obj_type, getter);
  639. if (mb) {
  640. PropertyInfo rt = mb->get_return_info();
  641. if (rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  642. r_type.enumeration = rt.class_name;
  643. }
  644. }
  645. }
  646. #endif
  647. }
  648. return true;
  649. }
  650. }
  651. } else if (p1.value.get_type() != Variant::NIL) {
  652. bool valid;
  653. Variant ret = p1.value.get(p2.value, &valid);
  654. if (valid) {
  655. r_type = _get_type_from_variant(ret);
  656. return true;
  657. }
  658. } else {
  659. if (p1.type != Variant::NIL) {
  660. Variant::CallError ce;
  661. Variant base = Variant::construct(p1.type, NULL, 0, ce);
  662. bool valid;
  663. Variant ret = base.get(p2.value, &valid);
  664. if (valid) {
  665. r_type = _get_type_from_variant(ret);
  666. return true;
  667. }
  668. }
  669. }
  670. }
  671. } else {
  672. Variant::Operator vop = Variant::OP_MAX;
  673. switch (op->op) {
  674. case GDParser::OperatorNode::OP_ADD: vop = Variant::OP_ADD; break;
  675. case GDParser::OperatorNode::OP_SUB: vop = Variant::OP_SUBSTRACT; break;
  676. case GDParser::OperatorNode::OP_MUL: vop = Variant::OP_MULTIPLY; break;
  677. case GDParser::OperatorNode::OP_DIV: vop = Variant::OP_DIVIDE; break;
  678. case GDParser::OperatorNode::OP_MOD: vop = Variant::OP_MODULE; break;
  679. case GDParser::OperatorNode::OP_SHIFT_LEFT: vop = Variant::OP_SHIFT_LEFT; break;
  680. case GDParser::OperatorNode::OP_SHIFT_RIGHT: vop = Variant::OP_SHIFT_RIGHT; break;
  681. case GDParser::OperatorNode::OP_BIT_AND: vop = Variant::OP_BIT_AND; break;
  682. case GDParser::OperatorNode::OP_BIT_OR: vop = Variant::OP_BIT_OR; break;
  683. case GDParser::OperatorNode::OP_BIT_XOR: vop = Variant::OP_BIT_XOR; break;
  684. default: {}
  685. }
  686. if (vop == Variant::OP_MAX)
  687. return false;
  688. GDCompletionIdentifier p1;
  689. GDCompletionIdentifier p2;
  690. if (op->arguments[0]) {
  691. if (!_guess_expression_type(context, op->arguments[0], p_line, p1)) {
  692. return false;
  693. }
  694. }
  695. if (op->arguments.size() > 1) {
  696. if (!_guess_expression_type(context, op->arguments[1], p_line, p2)) {
  697. return false;
  698. }
  699. }
  700. Variant::CallError ce;
  701. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  702. Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type, NULL, 0, ce);
  703. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  704. Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type, NULL, 0, ce);
  705. // avoid potential invalid ops
  706. if ((vop == Variant::OP_DIVIDE || vop == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  707. v2 = 1;
  708. v2_use_value = false;
  709. }
  710. if (vop == Variant::OP_DIVIDE && v2.get_type() == Variant::REAL) {
  711. v2 = 1.0;
  712. v2_use_value = false;
  713. }
  714. Variant r;
  715. bool valid;
  716. Variant::evaluate(vop, v1, v2, r, valid);
  717. if (!valid)
  718. return false;
  719. r_type.type = r.get_type();
  720. if (v1_use_value && v2_use_value)
  721. r_type.value = r;
  722. return true;
  723. }
  724. }
  725. return false;
  726. }
  727. static bool _guess_identifier_type_in_block(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  728. if (context.block->if_condition && context.block->if_condition->type == GDParser::Node::TYPE_OPERATOR && static_cast<const GDParser::OperatorNode *>(context.block->if_condition)->op == GDParser::OperatorNode::OP_IS) {
  729. //is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  730. //super dirty hack, but very useful
  731. //credit: Zylann
  732. //TODO: this could be hacked to detect ANDed conditions too..
  733. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->if_condition);
  734. if (op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER && static_cast<const GDParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
  735. //bingo
  736. if (_guess_expression_type(context, op->arguments[1], op->line, r_type)) {
  737. return true;
  738. }
  739. }
  740. }
  741. GDCompletionIdentifier gdi = _get_native_class(context);
  742. if (gdi.obj_type != StringName()) {
  743. bool valid;
  744. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_identifier, &valid);
  745. if (t != Variant::NIL && valid) {
  746. r_type.type = t;
  747. if (t == Variant::INT) {
  748. //check for enum!
  749. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  750. StringName getter = ClassDB::get_property_getter(gdi.obj_type, p_identifier);
  751. if (getter != StringName()) {
  752. MethodBind *mb = ClassDB::get_method(gdi.obj_type, getter);
  753. if (mb) {
  754. PropertyInfo rt = mb->get_return_info();
  755. if (rt.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  756. r_type.enumeration = rt.class_name;
  757. }
  758. }
  759. }
  760. #endif
  761. }
  762. return true;
  763. }
  764. }
  765. const GDParser::Node *last_assign = NULL;
  766. int last_assign_line = -1;
  767. for (int i = 0; i < context.block->statements.size(); i++) {
  768. if (context.block->statements[i]->line > p_line)
  769. continue;
  770. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  771. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  772. if (lv->assign && lv->name == p_identifier) {
  773. last_assign = lv->assign;
  774. last_assign_line = context.block->statements[i]->line;
  775. }
  776. }
  777. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  778. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->statements[i]);
  779. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  780. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  781. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  782. if (id->name == p_identifier) {
  783. last_assign = op->arguments[1];
  784. last_assign_line = context.block->statements[i]->line;
  785. }
  786. }
  787. }
  788. }
  789. }
  790. //use the last assignment, (then backwards?)
  791. if (last_assign) {
  792. return _guess_expression_type(context, last_assign, last_assign_line, r_type);
  793. }
  794. return false;
  795. }
  796. static bool _guess_identifier_from_assignment_in_function(GDCompletionContext &context, int p_src_line, const StringName &p_identifier, const StringName &p_function, GDCompletionIdentifier &r_type) {
  797. const GDParser::FunctionNode *func = NULL;
  798. for (int i = 0; i < context._class->functions.size(); i++) {
  799. if (context._class->functions[i]->name == p_function) {
  800. func = context._class->functions[i];
  801. break;
  802. }
  803. }
  804. if (!func)
  805. return false;
  806. for (int i = 0; i < func->body->statements.size(); i++) {
  807. if (func->body->statements[i]->line == p_src_line) {
  808. break;
  809. }
  810. if (func->body->statements[i]->type == GDParser::BlockNode::TYPE_OPERATOR) {
  811. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->body->statements[i]);
  812. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  813. if (op->arguments.size() && op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER) {
  814. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[0]);
  815. if (id->name == p_identifier) {
  816. return _guess_expression_type(context, op->arguments[1], func->body->statements[i]->line, r_type);
  817. }
  818. }
  819. }
  820. }
  821. }
  822. return false;
  823. }
  824. static bool _guess_identifier_type(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
  825. //go to block first
  826. const GDParser::BlockNode *block = context.block;
  827. while (block) {
  828. GDCompletionContext c = context;
  829. c.block = block;
  830. if (_guess_identifier_type_in_block(c, p_line, p_identifier, r_type)) {
  831. return true;
  832. }
  833. block = block->parent_block;
  834. }
  835. //guess from argument if virtual
  836. if (context.function && context.function->name != StringName()) {
  837. int argindex = -1;
  838. for (int i = 0; i < context.function->arguments.size(); i++) {
  839. if (context.function->arguments[i] == p_identifier) {
  840. argindex = i;
  841. break;
  842. }
  843. }
  844. if (argindex != -1) {
  845. GDCompletionIdentifier id = _get_native_class(context);
  846. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  847. //this kinda sucks but meh
  848. List<MethodInfo> vmethods;
  849. ClassDB::get_virtual_methods(id.obj_type, &vmethods);
  850. for (List<MethodInfo>::Element *E = vmethods.front(); E; E = E->next()) {
  851. if (E->get().name == context.function->name && argindex < E->get().arguments.size()) {
  852. PropertyInfo arg = E->get().arguments[argindex];
  853. int scp = String(arg.name).find(":");
  854. if (scp != -1) {
  855. r_type.type = Variant::OBJECT;
  856. r_type.obj_type = String(arg.name).substr(scp + 1, String(arg.name).length());
  857. return true;
  858. } else {
  859. r_type.type = arg.type;
  860. if (arg.hint == PROPERTY_HINT_RESOURCE_TYPE)
  861. r_type.obj_type = arg.hint_string;
  862. return true;
  863. }
  864. }
  865. }
  866. }
  867. }
  868. }
  869. //guess type in constant
  870. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  871. if (context._class->constant_expressions[i].identifier == p_identifier) {
  872. ERR_FAIL_COND_V(context._class->constant_expressions[i].expression->type != GDParser::Node::TYPE_CONSTANT, false);
  873. r_type = _get_type_from_variant(static_cast<const GDParser::ConstantNode *>(context._class->constant_expressions[i].expression)->value);
  874. return true;
  875. }
  876. }
  877. if (!(context.function && context.function->_static)) {
  878. for (int i = 0; i < context._class->variables.size(); i++) {
  879. if (context._class->variables[i].identifier == p_identifier) {
  880. if (context._class->variables[i]._export.type != Variant::NIL) {
  881. r_type = _get_type_from_pinfo(context._class->variables[i]._export);
  882. return true;
  883. } else if (context._class->variables[i].expression) {
  884. bool rtype = _guess_expression_type(context, context._class->variables[i].expression, context._class->variables[i].line, r_type);
  885. if (rtype && r_type.type != Variant::NIL)
  886. return true;
  887. //return _guess_expression_type(context,context._class->variables[i].expression,context._class->variables[i].line,r_type);
  888. }
  889. //try to guess from assignment in constructor or _ready
  890. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_ready", r_type))
  891. return true;
  892. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_enter_tree", r_type))
  893. return true;
  894. if (_guess_identifier_from_assignment_in_function(context, p_line + 1, p_identifier, "_init", r_type))
  895. return true;
  896. return false;
  897. }
  898. }
  899. }
  900. //autoloads as singletons
  901. List<PropertyInfo> props;
  902. ProjectSettings::get_singleton()->get_property_list(&props);
  903. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  904. String s = E->get().name;
  905. if (!s.begins_with("autoload/"))
  906. continue;
  907. String name = s.get_slice("/", 1);
  908. if (name == String(p_identifier)) {
  909. String path = ProjectSettings::get_singleton()->get(s);
  910. if (path.begins_with("*")) {
  911. String script = path.substr(1, path.length());
  912. if (!script.ends_with(".gd")) {
  913. //not a script, try find the script anyway,
  914. //may have some success
  915. script = script.get_basename() + ".gd";
  916. }
  917. if (FileAccess::exists(script)) {
  918. //print_line("is a script");
  919. Ref<Script> scr;
  920. if (ScriptCodeCompletionCache::get_sigleton())
  921. scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
  922. else
  923. scr = ResourceLoader::load(script);
  924. r_type.obj_type = "Node";
  925. r_type.type = Variant::OBJECT;
  926. r_type.script = scr;
  927. r_type.value = Variant();
  928. return true;
  929. }
  930. }
  931. }
  932. }
  933. //global
  934. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  935. if (E->key() == p_identifier) {
  936. r_type = _get_type_from_variant(GDScriptLanguage::get_singleton()->get_global_array()[E->get()]);
  937. return true;
  938. }
  939. }
  940. return false;
  941. }
  942. static void _find_identifiers_in_block(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  943. if (p_only_functions)
  944. return;
  945. for (int i = 0; i < context.block->statements.size(); i++) {
  946. if (context.block->statements[i]->line > p_line)
  947. continue;
  948. if (context.block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  949. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(context.block->statements[i]);
  950. result.insert(lv->name.operator String());
  951. }
  952. }
  953. }
  954. static void _find_identifiers_in_class(GDCompletionContext &context, bool p_static, bool p_only_functions, Set<String> &result) {
  955. if (!p_static && !p_only_functions) {
  956. for (int i = 0; i < context._class->variables.size(); i++) {
  957. result.insert(context._class->variables[i].identifier);
  958. }
  959. }
  960. if (!p_only_functions) {
  961. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  962. result.insert(context._class->constant_expressions[i].identifier);
  963. }
  964. for (int i = 0; i < context._class->subclasses.size(); i++) {
  965. result.insert(context._class->subclasses[i]->name);
  966. }
  967. }
  968. for (int i = 0; i < context._class->static_functions.size(); i++) {
  969. if (context._class->static_functions[i]->arguments.size())
  970. result.insert(context._class->static_functions[i]->name.operator String() + "(");
  971. else
  972. result.insert(context._class->static_functions[i]->name.operator String() + "()");
  973. }
  974. if (!p_static) {
  975. for (int i = 0; i < context._class->functions.size(); i++) {
  976. if (context._class->functions[i]->arguments.size())
  977. result.insert(context._class->functions[i]->name.operator String() + "(");
  978. else
  979. result.insert(context._class->functions[i]->name.operator String() + "()");
  980. }
  981. }
  982. //globals
  983. Ref<Reference> base = _get_parent_class(context);
  984. while (true) {
  985. Ref<GDScript> script = base;
  986. Ref<GDNativeClass> nc = base;
  987. if (script.is_valid()) {
  988. if (!p_static && !p_only_functions) {
  989. for (const Set<StringName>::Element *E = script->get_members().front(); E; E = E->next()) {
  990. result.insert(E->get().operator String());
  991. }
  992. }
  993. if (!p_only_functions) {
  994. for (const Map<StringName, Variant>::Element *E = script->get_constants().front(); E; E = E->next()) {
  995. result.insert(E->key().operator String());
  996. }
  997. }
  998. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  999. if (!p_static || E->get()->is_static()) {
  1000. if (E->get()->get_argument_count())
  1001. result.insert(E->key().operator String() + "(");
  1002. else
  1003. result.insert(E->key().operator String() + "()");
  1004. }
  1005. }
  1006. if (!p_only_functions) {
  1007. for (const Map<StringName, Ref<GDScript> >::Element *E = script->get_subclasses().front(); E; E = E->next()) {
  1008. result.insert(E->key().operator String());
  1009. }
  1010. }
  1011. base = script->get_base();
  1012. if (base.is_null())
  1013. base = script->get_native();
  1014. } else if (nc.is_valid()) {
  1015. StringName type = nc->get_name();
  1016. if (!p_only_functions) {
  1017. List<String> constants;
  1018. ClassDB::get_integer_constant_list(type, &constants);
  1019. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1020. result.insert(E->get());
  1021. }
  1022. List<PropertyInfo> pinfo;
  1023. ClassDB::get_property_list(type, &pinfo);
  1024. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1025. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1026. continue;
  1027. if (String(E->get().name).find("/") != -1)
  1028. continue;
  1029. result.insert(E->get().name);
  1030. }
  1031. }
  1032. List<MethodInfo> methods;
  1033. ClassDB::get_method_list(type, &methods, false, true);
  1034. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1035. if (E->get().name.begins_with("_"))
  1036. continue;
  1037. if (E->get().arguments.size())
  1038. result.insert(E->get().name + "(");
  1039. else
  1040. result.insert(E->get().name + "()");
  1041. }
  1042. break;
  1043. } else
  1044. break;
  1045. }
  1046. }
  1047. static void _find_identifiers(GDCompletionContext &context, int p_line, bool p_only_functions, Set<String> &result) {
  1048. const GDParser::BlockNode *block = context.block;
  1049. if (context.function) {
  1050. const GDParser::FunctionNode *f = context.function;
  1051. for (int i = 0; i < f->arguments.size(); i++) {
  1052. result.insert(f->arguments[i].operator String());
  1053. }
  1054. }
  1055. while (block) {
  1056. GDCompletionContext c = context;
  1057. c.block = block;
  1058. _find_identifiers_in_block(c, p_line, p_only_functions, result);
  1059. block = block->parent_block;
  1060. }
  1061. const GDParser::ClassNode *clss = context._class;
  1062. bool _static = context.function && context.function->_static;
  1063. while (clss) {
  1064. GDCompletionContext c = context;
  1065. c._class = clss;
  1066. c.block = NULL;
  1067. c.function = NULL;
  1068. _find_identifiers_in_class(c, _static, p_only_functions, result);
  1069. clss = clss->owner;
  1070. }
  1071. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  1072. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1073. }
  1074. static const char *_type_names[Variant::VARIANT_MAX] = {
  1075. "null", "bool", "int", "float", "String", "Vector2", "Rect2", "Vector3", "Transform2D", "Plane", "Quat", "AABB", "Basis", "Transform",
  1076. "Color", "NodePath", "RID", "Object", "Dictionary", "Array", "PoolByteArray", "PoolIntArray", "PoolRealArray", "PoolStringArray",
  1077. "PoolVector2Array", "PoolVector3Array", "PoolColorArray"
  1078. };
  1079. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1080. result.insert(_type_names[i]);
  1081. }
  1082. //autoload singletons
  1083. List<PropertyInfo> props;
  1084. ProjectSettings::get_singleton()->get_property_list(&props);
  1085. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1086. String s = E->get().name;
  1087. if (!s.begins_with("autoload/"))
  1088. continue;
  1089. String name = s.get_slice("/", 1);
  1090. String path = ProjectSettings::get_singleton()->get(s);
  1091. if (path.begins_with("*")) {
  1092. result.insert(name);
  1093. }
  1094. }
  1095. for (const Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  1096. result.insert(E->key().operator String());
  1097. }
  1098. }
  1099. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_isarg = true) {
  1100. String n = p_info.name;
  1101. int idx = n.find(":");
  1102. if (idx != -1) {
  1103. return n.substr(idx + 1, n.length());
  1104. }
  1105. if (p_info.type == Variant::OBJECT && p_info.hint == PROPERTY_HINT_RESOURCE_TYPE)
  1106. return p_info.hint_string;
  1107. if (p_info.type == Variant::NIL) {
  1108. if (p_isarg)
  1109. return "var";
  1110. else
  1111. return "void";
  1112. }
  1113. return Variant::get_type_name(p_info.type);
  1114. }
  1115. static void _make_function_hint(const GDParser::FunctionNode *p_func, int p_argidx, String &arghint) {
  1116. arghint = "func " + p_func->name + "(";
  1117. for (int i = 0; i < p_func->arguments.size(); i++) {
  1118. if (i > 0)
  1119. arghint += ", ";
  1120. else
  1121. arghint += " ";
  1122. if (i == p_argidx) {
  1123. arghint += String::chr(0xFFFF);
  1124. }
  1125. arghint += p_func->arguments[i].operator String();
  1126. int deffrom = p_func->arguments.size() - p_func->default_values.size();
  1127. if (i >= deffrom) {
  1128. int defidx = deffrom - i;
  1129. if (defidx >= 0 && defidx < p_func->default_values.size()) {
  1130. if (p_func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1131. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_func->default_values[defidx]);
  1132. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1133. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1134. arghint += "=" + cn->value.get_construct_string();
  1135. }
  1136. } else {
  1137. }
  1138. }
  1139. }
  1140. if (i == p_argidx) {
  1141. arghint += String::chr(0xFFFF);
  1142. }
  1143. }
  1144. if (p_func->arguments.size() > 0)
  1145. arghint += " ";
  1146. arghint += ")";
  1147. }
  1148. void get_directory_contents(EditorFileSystemDirectory *p_dir, Set<String> &r_list) {
  1149. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  1150. get_directory_contents(p_dir->get_subdir(i), r_list);
  1151. }
  1152. for (int i = 0; i < p_dir->get_file_count(); i++) {
  1153. r_list.insert("\"" + p_dir->get_file_path(i) + "\"");
  1154. }
  1155. }
  1156. static void _find_type_arguments(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, const StringName &p_method, const GDCompletionIdentifier &id, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1157. //print_line("find type arguments?");
  1158. if (id.type == Variant::OBJECT && id.obj_type != StringName()) {
  1159. MethodBind *m = ClassDB::get_method(id.obj_type, p_method);
  1160. if (!m) {
  1161. //not in static method, see script
  1162. //print_line("not in static: "+String(p_method));
  1163. Ref<GDScript> on_script;
  1164. if (id.value.get_type()) {
  1165. Object *obj = id.value;
  1166. if (obj) {
  1167. GDScript *scr = obj->cast_to<GDScript>();
  1168. if (scr) {
  1169. while (scr) {
  1170. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1171. if (E->get()->is_static() && p_method == E->get()->get_name()) {
  1172. arghint = "static func " + String(p_method) + "(";
  1173. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1174. if (i > 0)
  1175. arghint += ", ";
  1176. else
  1177. arghint += " ";
  1178. if (i == p_argidx) {
  1179. arghint += String::chr(0xFFFF);
  1180. }
  1181. arghint += "var " + E->get()->get_argument_name(i);
  1182. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1183. if (i >= deffrom) {
  1184. int defidx = deffrom - i;
  1185. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1186. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1187. }
  1188. }
  1189. if (i == p_argidx) {
  1190. arghint += String::chr(0xFFFF);
  1191. }
  1192. }
  1193. arghint += ")";
  1194. return; //found
  1195. }
  1196. }
  1197. if (scr->get_base().is_valid())
  1198. scr = scr->get_base().ptr();
  1199. else
  1200. scr = NULL;
  1201. }
  1202. } else {
  1203. on_script = obj->get_script();
  1204. }
  1205. }
  1206. }
  1207. //print_line("but it has a script?");
  1208. if (!on_script.is_valid() && id.script.is_valid()) {
  1209. //print_line("yes");
  1210. on_script = id.script;
  1211. }
  1212. if (on_script.is_valid()) {
  1213. GDScript *scr = on_script.ptr();
  1214. if (scr) {
  1215. while (scr) {
  1216. String code = scr->get_source_code();
  1217. //print_line("has source code!");
  1218. if (code != "") {
  1219. //if there is code, parse it. This way is slower but updates in real-time
  1220. GDParser p;
  1221. //Error parse(const String& p_code, const String& p_base_path="", bool p_just_validate=false,const String& p_self_path="",bool p_for_completion=false);
  1222. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1223. if (err == OK) {
  1224. //print_line("checking the functions...");
  1225. //only if ok, otherwise use what is cached on the script
  1226. //GDParser::ClassNode *base = p.
  1227. const GDParser::Node *root = p.get_parse_tree();
  1228. ERR_FAIL_COND(root->type != GDParser::Node::TYPE_CLASS);
  1229. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1230. const GDParser::FunctionNode *func = NULL;
  1231. bool st = false;
  1232. for (int i = 0; i < cl->functions.size(); i++) {
  1233. //print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
  1234. if (cl->functions[i]->name == p_method) {
  1235. func = cl->functions[i];
  1236. }
  1237. }
  1238. for (int i = 0; i < cl->static_functions.size(); i++) {
  1239. //print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
  1240. if (cl->static_functions[i]->name == p_method) {
  1241. func = cl->static_functions[i];
  1242. st = true;
  1243. }
  1244. }
  1245. if (func) {
  1246. arghint = "func " + String(p_method) + "(";
  1247. if (st)
  1248. arghint = "static " + arghint;
  1249. for (int i = 0; i < func->arguments.size(); i++) {
  1250. if (i > 0)
  1251. arghint += ", ";
  1252. else
  1253. arghint += " ";
  1254. if (i == p_argidx) {
  1255. arghint += String::chr(0xFFFF);
  1256. }
  1257. arghint += "var " + String(func->arguments[i]);
  1258. int deffrom = func->arguments.size() - func->default_values.size();
  1259. if (i >= deffrom) {
  1260. int defidx = deffrom - i;
  1261. if (defidx >= 0 && defidx < func->default_values.size() && func->default_values[defidx]->type == GDParser::Node::TYPE_OPERATOR) {
  1262. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(func->default_values[defidx]);
  1263. if (op->op == GDParser::OperatorNode::OP_ASSIGN) {
  1264. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
  1265. arghint += "=" + cn->value.get_construct_string();
  1266. }
  1267. }
  1268. }
  1269. if (i == p_argidx) {
  1270. arghint += String::chr(0xFFFF);
  1271. }
  1272. }
  1273. arghint += " )";
  1274. return;
  1275. }
  1276. } else {
  1277. //print_line("failed parsing?");
  1278. code = "";
  1279. }
  1280. }
  1281. if (code == "") {
  1282. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1283. if (p_method == E->get()->get_name()) {
  1284. arghint = "func " + String(p_method) + "(";
  1285. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1286. if (i > 0)
  1287. arghint += ", ";
  1288. else
  1289. arghint += " ";
  1290. if (i == p_argidx) {
  1291. arghint += String::chr(0xFFFF);
  1292. }
  1293. arghint += "var " + E->get()->get_argument_name(i);
  1294. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1295. if (i >= deffrom) {
  1296. int defidx = deffrom - i;
  1297. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1298. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1299. }
  1300. }
  1301. if (i == p_argidx) {
  1302. arghint += String::chr(0xFFFF);
  1303. }
  1304. }
  1305. arghint += ")";
  1306. return; //found
  1307. }
  1308. }
  1309. #if 0
  1310. //use class directly, no code was found
  1311. if (!isfunction) {
  1312. for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
  1313. options.insert(E->key());
  1314. }
  1315. }
  1316. for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
  1317. options.insert(String(E->key())+"(");
  1318. }
  1319. for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
  1320. options.insert(E->get());
  1321. }
  1322. #endif
  1323. }
  1324. if (scr->get_base().is_valid())
  1325. scr = scr->get_base().ptr();
  1326. else
  1327. scr = NULL;
  1328. }
  1329. }
  1330. }
  1331. } else {
  1332. //regular method
  1333. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1334. if (p_argidx < m->get_argument_count()) {
  1335. PropertyInfo pi = m->get_argument_info(p_argidx);
  1336. if (pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  1337. String enumeration = pi.class_name;
  1338. if (enumeration.find(".") != -1) {
  1339. //class constant
  1340. List<StringName> constants;
  1341. String cls = enumeration.get_slice(".", 0);
  1342. String enm = enumeration.get_slice(".", 1);
  1343. ClassDB::get_enum_constants(cls, enm, &constants);
  1344. //constants.sort_custom<StringName::AlphCompare>();
  1345. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1346. String add = cls + "." + E->get();
  1347. result.insert(add);
  1348. r_forced = true;
  1349. }
  1350. } else {
  1351. //global constant
  1352. StringName current_enum = enumeration;
  1353. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1354. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1355. result.insert(GlobalConstants::get_global_constant_name(i));
  1356. r_forced = true;
  1357. }
  1358. }
  1359. //global
  1360. }
  1361. }
  1362. }
  1363. #endif
  1364. if (p_method.operator String() == "connect" || (p_method.operator String() == "emit_signal" && p_argidx == 0)) {
  1365. if (p_argidx == 0) {
  1366. List<MethodInfo> sigs;
  1367. ClassDB::get_signal_list(id.obj_type, &sigs);
  1368. if (id.script.is_valid()) {
  1369. id.script->get_script_signal_list(&sigs);
  1370. } else if (id.value.get_type() == Variant::OBJECT) {
  1371. Object *obj = id.value;
  1372. if (obj && !obj->get_script().is_null()) {
  1373. Ref<Script> scr = obj->get_script();
  1374. if (scr.is_valid()) {
  1375. scr->get_script_signal_list(&sigs);
  1376. }
  1377. }
  1378. }
  1379. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1380. result.insert("\"" + E->get().name + "\"");
  1381. r_forced = true;
  1382. }
  1383. } else if (p_argidx == 2) {
  1384. if (context._class) {
  1385. for (int i = 0; i < context._class->functions.size(); i++) {
  1386. result.insert("\"" + context._class->functions[i]->name + "\"");
  1387. r_forced = true;
  1388. }
  1389. }
  1390. }
  1391. /*if (p_argidx==2) {
  1392. ERR_FAIL_COND(p_node->type!=GDParser::Node::TYPE_OPERATOR);
  1393. const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(p_node);
  1394. if (op->arguments.size()>)
  1395. }*/
  1396. } else {
  1397. if (p_argidx == 0 && (String(p_method) == "get_node" || String(p_method) == "has_node") && ClassDB::is_parent_class(id.obj_type, "Node")) {
  1398. List<PropertyInfo> props;
  1399. ProjectSettings::get_singleton()->get_property_list(&props);
  1400. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1401. String s = E->get().name;
  1402. if (!s.begins_with("autoload/"))
  1403. continue;
  1404. //print_line("found "+s);
  1405. String name = s.get_slice("/", 1);
  1406. result.insert("\"/root/" + name + "\"");
  1407. r_forced = true;
  1408. }
  1409. }
  1410. Object *obj = id.value;
  1411. if (obj) {
  1412. List<String> options;
  1413. obj->get_argument_options(p_method, p_argidx, &options);
  1414. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1415. result.insert(E->get());
  1416. r_forced = true;
  1417. }
  1418. }
  1419. }
  1420. arghint = _get_visual_datatype(m->get_return_info(), false) + " " + p_method.operator String() + String("(");
  1421. for (int i = 0; i < m->get_argument_count(); i++) {
  1422. if (i > 0)
  1423. arghint += ", ";
  1424. else
  1425. arghint += " ";
  1426. if (i == p_argidx) {
  1427. arghint += String::chr(0xFFFF);
  1428. }
  1429. String n = m->get_argument_info(i).name;
  1430. int dp = n.find(":");
  1431. if (dp != -1)
  1432. n = n.substr(0, dp);
  1433. arghint += _get_visual_datatype(m->get_argument_info(i)) + " " + n;
  1434. int deffrom = m->get_argument_count() - m->get_default_argument_count();
  1435. if (i >= deffrom) {
  1436. int defidx = i - deffrom;
  1437. if (defidx >= 0 && defidx < m->get_default_argument_count()) {
  1438. Variant v = m->get_default_argument(i);
  1439. arghint += "=" + v.get_construct_string();
  1440. }
  1441. }
  1442. if (i == p_argidx) {
  1443. arghint += String::chr(0xFFFF);
  1444. }
  1445. }
  1446. if (m->get_argument_count() > 0)
  1447. arghint += " ";
  1448. arghint += ")";
  1449. }
  1450. }
  1451. }
  1452. static void _find_call_arguments(GDCompletionContext &context, const GDParser::Node *p_node, int p_line, int p_argidx, Set<String> &result, bool &r_forced, String &arghint) {
  1453. if (!p_node || p_node->type != GDParser::Node::TYPE_OPERATOR) {
  1454. return;
  1455. }
  1456. const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(p_node);
  1457. if (op->op != GDParser::OperatorNode::OP_CALL) {
  1458. return;
  1459. }
  1460. if (op->arguments[0]->type == GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  1461. //complete built-in function
  1462. const GDParser::BuiltInFunctionNode *fn = static_cast<const GDParser::BuiltInFunctionNode *>(op->arguments[0]);
  1463. MethodInfo mi = GDFunctions::get_info(fn->function);
  1464. if (mi.name == "load" && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) {
  1465. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), result);
  1466. }
  1467. arghint = _get_visual_datatype(mi.return_val, false) + " " + GDFunctions::get_func_name(fn->function) + String("(");
  1468. for (int i = 0; i < mi.arguments.size(); i++) {
  1469. if (i > 0)
  1470. arghint += ", ";
  1471. else
  1472. arghint += " ";
  1473. if (i == p_argidx) {
  1474. arghint += String::chr(0xFFFF);
  1475. }
  1476. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1477. if (i == p_argidx) {
  1478. arghint += String::chr(0xFFFF);
  1479. }
  1480. }
  1481. if (mi.arguments.size() > 0)
  1482. arghint += " ";
  1483. arghint += ")";
  1484. } else if (op->arguments[0]->type == GDParser::Node::TYPE_TYPE) {
  1485. //complete constructor
  1486. const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode *>(op->arguments[0]);
  1487. List<MethodInfo> mil;
  1488. Variant::get_constructor_list(tn->vtype, &mil);
  1489. for (List<MethodInfo>::Element *E = mil.front(); E; E = E->next()) {
  1490. MethodInfo mi = E->get();
  1491. if (mi.arguments.size() == 0)
  1492. continue;
  1493. if (E->prev())
  1494. arghint += "\n";
  1495. arghint += Variant::get_type_name(tn->vtype) + " " + Variant::get_type_name(tn->vtype) + String("(");
  1496. for (int i = 0; i < mi.arguments.size(); i++) {
  1497. if (i > 0)
  1498. arghint += ", ";
  1499. else
  1500. arghint += " ";
  1501. if (i == p_argidx) {
  1502. arghint += String::chr(0xFFFF);
  1503. }
  1504. arghint += _get_visual_datatype(mi.arguments[i]) + " " + mi.arguments[i].name;
  1505. if (i == p_argidx) {
  1506. arghint += String::chr(0xFFFF);
  1507. }
  1508. }
  1509. if (mi.arguments.size() > 0)
  1510. arghint += " ";
  1511. arghint += ")";
  1512. }
  1513. } else if (op->arguments.size() >= 2 && op->arguments[1]->type == GDParser::Node::TYPE_IDENTIFIER) {
  1514. //make sure identifier exists...
  1515. const GDParser::IdentifierNode *id = static_cast<const GDParser::IdentifierNode *>(op->arguments[1]);
  1516. if (op->arguments[0]->type == GDParser::Node::TYPE_SELF) {
  1517. //self, look up
  1518. for (int i = 0; i < context._class->static_functions.size(); i++) {
  1519. if (context._class->static_functions[i]->name == id->name) {
  1520. _make_function_hint(context._class->static_functions[i], p_argidx, arghint);
  1521. return;
  1522. }
  1523. }
  1524. if (context.function && !context.function->_static) {
  1525. for (int i = 0; i < context._class->functions.size(); i++) {
  1526. if (context._class->functions[i]->name == id->name) {
  1527. _make_function_hint(context._class->functions[i], p_argidx, arghint);
  1528. return;
  1529. }
  1530. }
  1531. }
  1532. Ref<Reference> base = _get_parent_class(context);
  1533. while (true) {
  1534. Ref<GDScript> script = base;
  1535. Ref<GDNativeClass> nc = base;
  1536. if (script.is_valid()) {
  1537. for (const Map<StringName, GDFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) {
  1538. if (E->key() == id->name) {
  1539. if (context.function && context.function->_static && !E->get()->is_static())
  1540. continue;
  1541. arghint = "func " + id->name.operator String() + String("(");
  1542. for (int i = 0; i < E->get()->get_argument_count(); i++) {
  1543. if (i > 0)
  1544. arghint += ", ";
  1545. else
  1546. arghint += " ";
  1547. if (i == p_argidx) {
  1548. arghint += String::chr(0xFFFF);
  1549. }
  1550. arghint += E->get()->get_argument_name(i);
  1551. int deffrom = E->get()->get_argument_count() - E->get()->get_default_argument_count();
  1552. if (i >= deffrom) {
  1553. int defidx = deffrom - i;
  1554. if (defidx >= 0 && defidx < E->get()->get_default_argument_count()) {
  1555. arghint += "=" + E->get()->get_default_argument(defidx).get_construct_string();
  1556. }
  1557. }
  1558. if (i == p_argidx) {
  1559. arghint += String::chr(0xFFFF);
  1560. }
  1561. }
  1562. if (E->get()->get_argument_count() > 0)
  1563. arghint += " ";
  1564. arghint += ")";
  1565. return;
  1566. }
  1567. }
  1568. base = script->get_base();
  1569. if (base.is_null())
  1570. base = script->get_native();
  1571. } else if (nc.is_valid()) {
  1572. if (context.function && !context.function->_static) {
  1573. GDCompletionIdentifier ci;
  1574. ci.type = Variant::OBJECT;
  1575. ci.obj_type = nc->get_name();
  1576. if (!context._class->owner)
  1577. ci.value = context.base;
  1578. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1579. //guess type..
  1580. /*
  1581. List<MethodInfo> methods;
  1582. ClassDB::get_method_list(type,&methods);
  1583. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1584. if (E->get().arguments.size())
  1585. result.insert(E->get().name+"(");
  1586. else
  1587. result.insert(E->get().name+"()");
  1588. }*/
  1589. }
  1590. break;
  1591. } else
  1592. break;
  1593. }
  1594. } else {
  1595. //indexed lookup
  1596. GDCompletionIdentifier ci;
  1597. if (_guess_expression_type(context, op->arguments[0], p_line, ci)) {
  1598. _find_type_arguments(context, p_node, p_line, id->name, ci, p_argidx, result, r_forced, arghint);
  1599. return;
  1600. }
  1601. }
  1602. }
  1603. #if 0
  1604. bool _static=context.function->_static;
  1605. for(int i=0;i<context._class->static_functions.size();i++) {
  1606. if (context._class->static_functions[i]->arguments.size())
  1607. result.insert(context._class->static_functions[i]->name.operator String()+"(");
  1608. else
  1609. result.insert(context._class->static_functions[i]->name.operator String()+"()");
  1610. }
  1611. if (!p_static) {
  1612. for(int i=0;i<context._class->functions.size();i++) {
  1613. if (context._class->functions[i]->arguments.size())
  1614. result.insert(context._class->functions[i]->name.operator String()+"(");
  1615. else
  1616. result.insert(context._class->functions[i]->name.operator String()+"()");
  1617. }
  1618. }
  1619. Ref<Reference> base = _get_parent_class(context);
  1620. while(true) {
  1621. Ref<GDScript> script = base;
  1622. Ref<GDNativeClass> nc = base;
  1623. if (script.is_valid()) {
  1624. if (!p_static && !p_only_functions) {
  1625. for (const Set<StringName>::Element *E=script->get_members().front();E;E=E->next()) {
  1626. result.insert(E->get().operator String());
  1627. }
  1628. }
  1629. if (!p_only_functions) {
  1630. for (const Map<StringName,Variant>::Element *E=script->get_constants().front();E;E=E->next()) {
  1631. result.insert(E->key().operator String());
  1632. }
  1633. }
  1634. for (const Map<StringName,GDFunction>::Element *E=script->get_member_functions().front();E;E=E->next()) {
  1635. if (!p_static || E->get().is_static()) {
  1636. if (E->get().get_argument_count())
  1637. result.insert(E->key().operator String()+"(");
  1638. else
  1639. result.insert(E->key().operator String()+"()");
  1640. }
  1641. }
  1642. if (!p_only_functions) {
  1643. for (const Map<StringName,Ref<GDScript> >::Element *E=script->get_subclasses().front();E;E=E->next()) {
  1644. result.insert(E->key().operator String());
  1645. }
  1646. }
  1647. base=script->get_base();
  1648. if (base.is_null())
  1649. base=script->get_native();
  1650. } else if (nc.is_valid()) {
  1651. if (!p_only_functions) {
  1652. StringName type = nc->get_name();
  1653. List<String> constants;
  1654. ClassDB::get_integer_constant_list(type,&constants);
  1655. for(List<String>::Element *E=constants.front();E;E=E->next()) {
  1656. result.insert(E->get());
  1657. }
  1658. List<MethodInfo> methods;
  1659. ClassDB::get_method_list(type,&methods);
  1660. for(List<MethodInfo>::Element *E=methods.front();E;E=E->next()) {
  1661. if (E->get().arguments.size())
  1662. result.insert(E->get().name+"(");
  1663. else
  1664. result.insert(E->get().name+"()");
  1665. }
  1666. }
  1667. break;
  1668. } else
  1669. break;
  1670. }
  1671. for(int i=0;i<GDFunctions::FUNC_MAX;i++) {
  1672. result.insert(GDFunctions::get_func_name(GDFunctions::Function(i)));
  1673. }
  1674. #endif
  1675. }
  1676. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1677. GDParser p;
  1678. p.parse(p_code, p_base_path, false, "", true);
  1679. bool isfunction = false;
  1680. Set<String> options;
  1681. r_forced = false;
  1682. GDCompletionContext context;
  1683. context._class = p.get_completion_class();
  1684. context.block = p.get_completion_block();
  1685. context.function = p.get_completion_function();
  1686. context.base = p_owner;
  1687. context.base_path = p_base_path;
  1688. switch (p.get_completion_type()) {
  1689. case GDParser::COMPLETION_NONE: {
  1690. } break;
  1691. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  1692. List<StringName> constants;
  1693. Variant::get_numeric_constants_for_type(p.get_completion_built_in_constant(), &constants);
  1694. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1695. options.insert(E->get().operator String());
  1696. }
  1697. } break;
  1698. case GDParser::COMPLETION_FUNCTION:
  1699. isfunction = true;
  1700. case GDParser::COMPLETION_IDENTIFIER: {
  1701. _find_identifiers(context, p.get_completion_line(), isfunction, options);
  1702. } break;
  1703. case GDParser::COMPLETION_PARENT_FUNCTION: {
  1704. } break;
  1705. case GDParser::COMPLETION_GET_NODE: {
  1706. if (p_owner) {
  1707. List<String> opts;
  1708. p_owner->get_argument_options("get_node", 0, &opts);
  1709. for (List<String>::Element *E = opts.front(); E; E = E->next()) {
  1710. String opt = E->get().strip_edges();
  1711. if (opt.begins_with("\"") && opt.ends_with("\"")) {
  1712. r_forced = true;
  1713. String idopt = opt.substr(1, opt.length() - 2);
  1714. if (idopt.replace("/", "_").is_valid_identifier()) {
  1715. options.insert(idopt);
  1716. } else {
  1717. options.insert(opt);
  1718. }
  1719. }
  1720. }
  1721. }
  1722. } break;
  1723. case GDParser::COMPLETION_METHOD:
  1724. isfunction = true;
  1725. case GDParser::COMPLETION_INDEX: {
  1726. const GDParser::Node *node = p.get_completion_node();
  1727. if (node->type != GDParser::Node::TYPE_OPERATOR)
  1728. break;
  1729. GDCompletionIdentifier t;
  1730. if (_guess_expression_type(context, static_cast<const GDParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  1731. if (t.type == Variant::OBJECT && t.obj_type == "GDNativeClass") {
  1732. //native enum
  1733. Ref<GDNativeClass> gdn = t.value;
  1734. if (gdn.is_valid()) {
  1735. StringName cn = gdn->get_name();
  1736. List<String> cnames;
  1737. ClassDB::get_integer_constant_list(cn, &cnames);
  1738. for (List<String>::Element *E = cnames.front(); E; E = E->next()) {
  1739. options.insert(E->get());
  1740. }
  1741. List<PropertyInfo> pinfo;
  1742. ClassDB::get_property_list(cn, &pinfo);
  1743. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1744. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1745. continue;
  1746. if (String(E->get().name).find("/") != -1)
  1747. continue;
  1748. options.insert(E->get().name);
  1749. }
  1750. }
  1751. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1752. Ref<GDScript> on_script;
  1753. if (t.value.get_type()) {
  1754. Object *obj = t.value;
  1755. if (obj) {
  1756. GDScript *scr = obj->cast_to<GDScript>();
  1757. if (scr) {
  1758. while (scr) {
  1759. if (!isfunction) {
  1760. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1761. options.insert(E->key());
  1762. }
  1763. }
  1764. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1765. if (E->get()->is_static())
  1766. options.insert(E->key());
  1767. }
  1768. if (scr->get_base().is_valid())
  1769. scr = scr->get_base().ptr();
  1770. else
  1771. scr = NULL;
  1772. }
  1773. } else {
  1774. on_script = obj->get_script();
  1775. }
  1776. }
  1777. }
  1778. if (!on_script.is_valid() && t.script.is_valid()) {
  1779. on_script = t.script;
  1780. }
  1781. if (on_script.is_valid()) {
  1782. GDScript *scr = on_script.ptr();
  1783. if (scr) {
  1784. while (scr) {
  1785. String code = scr->get_source_code();
  1786. if (code != "") {
  1787. //if there is code, parse it. This way is slower but updates in real-time
  1788. GDParser p;
  1789. Error err = p.parse(scr->get_source_code(), scr->get_path().get_base_dir(), true, "", false);
  1790. if (err == OK) {
  1791. //only if ok, otherwise use what is cached on the script
  1792. //GDParser::ClassNode *base = p.
  1793. const GDParser::Node *root = p.get_parse_tree();
  1794. ERR_FAIL_COND_V(root->type != GDParser::Node::TYPE_CLASS, ERR_PARSE_ERROR);
  1795. const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode *>(root);
  1796. for (int i = 0; i < cl->functions.size(); i++) {
  1797. if (cl->functions[i]->arguments.size())
  1798. options.insert(String(cl->functions[i]->name) + "(");
  1799. else
  1800. options.insert(String(cl->functions[i]->name) + "()");
  1801. }
  1802. for (int i = 0; i < cl->static_functions.size(); i++) {
  1803. if (cl->static_functions[i]->arguments.size())
  1804. options.insert(String(cl->static_functions[i]->name) + "(");
  1805. else
  1806. options.insert(String(cl->static_functions[i]->name) + "()");
  1807. }
  1808. if (!isfunction) {
  1809. for (int i = 0; i < cl->variables.size(); i++) {
  1810. options.insert(String(cl->variables[i].identifier));
  1811. }
  1812. for (int i = 0; i < cl->constant_expressions.size(); i++) {
  1813. options.insert(String(cl->constant_expressions[i].identifier));
  1814. }
  1815. }
  1816. } else {
  1817. code = ""; //well, then no code
  1818. }
  1819. }
  1820. if (code == "") {
  1821. //use class directly, no code was found
  1822. if (!isfunction) {
  1823. for (const Map<StringName, Variant>::Element *E = scr->get_constants().front(); E; E = E->next()) {
  1824. options.insert(E->key());
  1825. }
  1826. }
  1827. for (const Map<StringName, GDFunction *>::Element *E = scr->get_member_functions().front(); E; E = E->next()) {
  1828. if (E->get()->get_argument_count())
  1829. options.insert(String(E->key()) + "()");
  1830. else
  1831. options.insert(String(E->key()) + "(");
  1832. }
  1833. for (const Set<StringName>::Element *E = scr->get_members().front(); E; E = E->next()) {
  1834. options.insert(E->get());
  1835. }
  1836. }
  1837. if (scr->get_base().is_valid())
  1838. scr = scr->get_base().ptr();
  1839. else
  1840. scr = NULL;
  1841. }
  1842. }
  1843. }
  1844. if (!isfunction) {
  1845. ClassDB::get_integer_constant_list(t.obj_type, r_options);
  1846. List<PropertyInfo> pinfo;
  1847. ClassDB::get_property_list(t.obj_type, &pinfo);
  1848. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1849. if (E->get().usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY))
  1850. continue;
  1851. if (String(E->get().name).find("/") != -1)
  1852. continue;
  1853. r_options->push_back(E->get().name);
  1854. }
  1855. }
  1856. List<MethodInfo> mi;
  1857. ClassDB::get_method_list(t.obj_type, &mi, false, true);
  1858. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1859. if (E->get().name.begins_with("_"))
  1860. continue;
  1861. if (E->get().arguments.size())
  1862. options.insert(E->get().name + "(");
  1863. else
  1864. options.insert(E->get().name + "()");
  1865. }
  1866. } else {
  1867. //check InputEvent hint
  1868. {
  1869. if (t.value.get_type() == Variant::NIL) {
  1870. Variant::CallError ce;
  1871. t.value = Variant::construct(t.type, NULL, 0, ce);
  1872. }
  1873. if (!isfunction) {
  1874. List<PropertyInfo> pl;
  1875. t.value.get_property_list(&pl);
  1876. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  1877. if (String(E->get().name).find("/") == -1)
  1878. options.insert(E->get().name);
  1879. }
  1880. }
  1881. List<MethodInfo> mi;
  1882. t.value.get_method_list(&mi);
  1883. for (List<MethodInfo>::Element *E = mi.front(); E; E = E->next()) {
  1884. if (E->get().arguments.size())
  1885. options.insert(E->get().name + "(");
  1886. else
  1887. options.insert(E->get().name + "()");
  1888. }
  1889. }
  1890. }
  1891. }
  1892. } break;
  1893. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  1894. _find_call_arguments(context, p.get_completion_node(), p.get_completion_line(), p.get_completion_argument_index(), options, r_forced, r_call_hint);
  1895. } break;
  1896. case GDParser::COMPLETION_VIRTUAL_FUNC: {
  1897. GDCompletionIdentifier cid = _get_native_class(context);
  1898. if (cid.obj_type != StringName()) {
  1899. List<MethodInfo> vm;
  1900. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  1901. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  1902. MethodInfo &mi = E->get();
  1903. String m = mi.name;
  1904. if (m.find(":") != -1)
  1905. m = m.substr(0, m.find(":"));
  1906. m += "(";
  1907. if (mi.arguments.size()) {
  1908. for (int i = 0; i < mi.arguments.size(); i++) {
  1909. if (i > 0)
  1910. m += ", ";
  1911. String n = mi.arguments[i].name;
  1912. if (n.find(":") != -1)
  1913. n = n.substr(0, n.find(":"));
  1914. m += n;
  1915. }
  1916. }
  1917. m += "):";
  1918. options.insert(m);
  1919. }
  1920. }
  1921. } break;
  1922. case GDParser::COMPLETION_YIELD: {
  1923. const GDParser::Node *node = p.get_completion_node();
  1924. GDCompletionIdentifier t;
  1925. if (!_guess_expression_type(context, node, p.get_completion_line(), t))
  1926. break;
  1927. if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  1928. List<MethodInfo> sigs;
  1929. ClassDB::get_signal_list(t.obj_type, &sigs);
  1930. for (List<MethodInfo>::Element *E = sigs.front(); E; E = E->next()) {
  1931. options.insert("\"" + E->get().name + "\"");
  1932. r_forced = true;
  1933. }
  1934. }
  1935. } break;
  1936. case GDParser::COMPLETION_RESOURCE_PATH: {
  1937. if (EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))
  1938. get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  1939. } break;
  1940. case GDParser::COMPLETION_ASSIGN: {
  1941. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  1942. GDCompletionIdentifier ci;
  1943. if (_guess_expression_type(context, p.get_completion_node(), p.get_completion_line(), ci)) {
  1944. String enumeration = ci.enumeration;
  1945. if (enumeration.find(".") != -1) {
  1946. //class constant
  1947. List<StringName> constants;
  1948. String cls = enumeration.get_slice(".", 0);
  1949. String enm = enumeration.get_slice(".", 1);
  1950. ClassDB::get_enum_constants(cls, enm, &constants);
  1951. //constants.sort_custom<StringName::AlphCompare>();
  1952. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  1953. String add = cls + "." + E->get();
  1954. r_options->push_back(add);
  1955. r_forced = true;
  1956. }
  1957. } else {
  1958. //global constant
  1959. StringName current_enum = enumeration;
  1960. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  1961. if (GlobalConstants::get_global_constant_enum(i) == current_enum) {
  1962. r_options->push_back(GlobalConstants::get_global_constant_name(i));
  1963. r_forced = true;
  1964. }
  1965. }
  1966. //global
  1967. }
  1968. }
  1969. #endif
  1970. } break;
  1971. }
  1972. for (Set<String>::Element *E = options.front(); E; E = E->next()) {
  1973. r_options->push_back(E->get());
  1974. }
  1975. return OK;
  1976. }
  1977. #else
  1978. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, bool &r_forced, String &r_call_hint) {
  1979. return OK;
  1980. }
  1981. #endif
  1982. String GDScriptLanguage::_get_indentation() const {
  1983. #ifdef TOOLS_ENABLED
  1984. if (Engine::get_singleton()->is_editor_hint()) {
  1985. bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
  1986. if (use_space_indentation) {
  1987. int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
  1988. String space_indent = "";
  1989. for (int i = 0; i < indent_size; i++) {
  1990. space_indent += " ";
  1991. }
  1992. return space_indent;
  1993. }
  1994. }
  1995. #endif
  1996. return "\t";
  1997. }
  1998. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  1999. String indent = _get_indentation();
  2000. Vector<String> lines = p_code.split("\n");
  2001. List<int> indent_stack;
  2002. for (int i = 0; i < lines.size(); i++) {
  2003. String l = lines[i];
  2004. int tc = 0;
  2005. for (int j = 0; j < l.length(); j++) {
  2006. if (l[j] == ' ' || l[j] == '\t') {
  2007. tc++;
  2008. } else {
  2009. break;
  2010. }
  2011. }
  2012. String st = l.substr(tc, l.length()).strip_edges();
  2013. if (st == "" || st.begins_with("#"))
  2014. continue; //ignore!
  2015. int ilevel = 0;
  2016. if (indent_stack.size()) {
  2017. ilevel = indent_stack.back()->get();
  2018. }
  2019. if (tc > ilevel) {
  2020. indent_stack.push_back(tc);
  2021. } else if (tc < ilevel) {
  2022. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2023. indent_stack.pop_back();
  2024. }
  2025. if (indent_stack.size() && indent_stack.back()->get() != tc)
  2026. indent_stack.push_back(tc); //this is not right but gets the job done
  2027. }
  2028. if (i >= p_from_line) {
  2029. l = "";
  2030. for (int j = 0; j < indent_stack.size(); j++) {
  2031. l += indent;
  2032. }
  2033. l += st;
  2034. } else if (i > p_to_line) {
  2035. break;
  2036. }
  2037. //print_line(itos(indent_stack.size())+","+itos(tc)+": "+l);
  2038. lines[i] = l;
  2039. }
  2040. p_code = "";
  2041. for (int i = 0; i < lines.size(); i++) {
  2042. if (i > 0)
  2043. p_code += "\n";
  2044. p_code += lines[i];
  2045. }
  2046. }
  2047. #ifdef TOOLS_ENABLED
  2048. Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_base_path, Object *p_owner, LookupResult &r_result) {
  2049. //before parsing, try the usual stuff
  2050. if (ClassDB::class_exists(p_symbol)) {
  2051. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2052. r_result.class_name = p_symbol;
  2053. return OK;
  2054. }
  2055. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2056. Variant::Type t = Variant::Type(i);
  2057. if (Variant::get_type_name(t) == p_symbol) {
  2058. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2059. r_result.class_name = Variant::get_type_name(t);
  2060. return OK;
  2061. }
  2062. }
  2063. for (int i = 0; i < GDFunctions::FUNC_MAX; i++) {
  2064. if (GDFunctions::get_func_name(GDFunctions::Function(i)) == p_symbol) {
  2065. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2066. r_result.class_name = "@GDScript";
  2067. r_result.class_member = p_symbol;
  2068. return OK;
  2069. }
  2070. }
  2071. GDParser p;
  2072. p.parse(p_code, p_base_path, false, "", true);
  2073. if (p.get_completion_type() == GDParser::COMPLETION_NONE)
  2074. return ERR_CANT_RESOLVE;
  2075. GDCompletionContext context;
  2076. context._class = p.get_completion_class();
  2077. context.block = p.get_completion_block();
  2078. context.function = p.get_completion_function();
  2079. context.base = p_owner;
  2080. context.base_path = p_base_path;
  2081. bool isfunction = false;
  2082. switch (p.get_completion_type()) {
  2083. case GDParser::COMPLETION_GET_NODE:
  2084. case GDParser::COMPLETION_NONE: {
  2085. } break;
  2086. case GDParser::COMPLETION_BUILT_IN_TYPE_CONSTANT: {
  2087. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2088. r_result.class_name = Variant::get_type_name(p.get_completion_built_in_constant());
  2089. r_result.class_member = p_symbol;
  2090. return OK;
  2091. } break;
  2092. case GDParser::COMPLETION_FUNCTION: {
  2093. if (context._class && context._class->functions.size()) {
  2094. for (int i = 0; i < context._class->functions.size(); i++) {
  2095. if (context._class->functions[i]->name == p_symbol) {
  2096. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2097. r_result.location = context._class->functions[i]->line;
  2098. return OK;
  2099. }
  2100. }
  2101. }
  2102. Ref<GDScript> parent = _get_parent_class(context);
  2103. while (parent.is_valid()) {
  2104. int line = parent->get_member_line(p_symbol);
  2105. if (line >= 0) {
  2106. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2107. r_result.location = line;
  2108. r_result.script = parent;
  2109. return OK;
  2110. }
  2111. parent = parent->get_base();
  2112. }
  2113. GDCompletionIdentifier identifier = _get_native_class(context);
  2114. print_line("identifier: " + String(identifier.obj_type));
  2115. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2116. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2117. r_result.class_name = identifier.obj_type;
  2118. r_result.class_member = p_symbol;
  2119. return OK;
  2120. }
  2121. } break;
  2122. case GDParser::COMPLETION_IDENTIFIER: {
  2123. //check if a function
  2124. if (p.get_completion_identifier_is_function()) {
  2125. if (context._class && context._class->functions.size()) {
  2126. for (int i = 0; i < context._class->functions.size(); i++) {
  2127. if (context._class->functions[i]->name == p_symbol) {
  2128. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2129. r_result.location = context._class->functions[i]->line;
  2130. return OK;
  2131. }
  2132. }
  2133. }
  2134. Ref<GDScript> parent = _get_parent_class(context);
  2135. while (parent.is_valid()) {
  2136. int line = parent->get_member_line(p_symbol);
  2137. if (line >= 0) {
  2138. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2139. r_result.location = line;
  2140. r_result.script = parent;
  2141. return OK;
  2142. }
  2143. parent = parent->get_base();
  2144. }
  2145. GDCompletionIdentifier identifier = _get_native_class(context);
  2146. if (ClassDB::has_method(identifier.obj_type, p_symbol)) {
  2147. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2148. r_result.class_name = identifier.obj_type;
  2149. r_result.class_member = p_symbol;
  2150. return OK;
  2151. }
  2152. } else {
  2153. GDCompletionIdentifier gdi = _get_native_class(context);
  2154. if (gdi.obj_type != StringName()) {
  2155. bool valid;
  2156. Variant::Type t = ClassDB::get_property_type(gdi.obj_type, p_symbol, &valid);
  2157. if (t != Variant::NIL && valid) {
  2158. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2159. r_result.class_name = gdi.obj_type;
  2160. r_result.class_member = p_symbol;
  2161. return OK;
  2162. }
  2163. }
  2164. const GDParser::BlockNode *block = context.block;
  2165. //search in blocks going up (local var?)
  2166. while (block) {
  2167. for (int i = 0; i < block->statements.size(); i++) {
  2168. if (block->statements[i]->line > p.get_completion_line())
  2169. continue;
  2170. if (block->statements[i]->type == GDParser::BlockNode::TYPE_LOCAL_VAR) {
  2171. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode *>(block->statements[i]);
  2172. if (lv->assign && lv->name == p_symbol) {
  2173. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2174. r_result.location = block->statements[i]->line;
  2175. return OK;
  2176. }
  2177. }
  2178. }
  2179. block = block->parent_block;
  2180. }
  2181. //guess from function arguments
  2182. if (context.function && context.function->name != StringName()) {
  2183. for (int i = 0; i < context.function->arguments.size(); i++) {
  2184. if (context.function->arguments[i] == p_symbol) {
  2185. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2186. r_result.location = context.function->line;
  2187. return OK;
  2188. }
  2189. }
  2190. }
  2191. //guess in class constants
  2192. for (int i = 0; i < context._class->constant_expressions.size(); i++) {
  2193. if (context._class->constant_expressions[i].identifier == p_symbol) {
  2194. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2195. r_result.location = context._class->constant_expressions[i].expression->line;
  2196. return OK;
  2197. }
  2198. }
  2199. //guess in class variables
  2200. if (!(context.function && context.function->_static)) {
  2201. for (int i = 0; i < context._class->variables.size(); i++) {
  2202. if (context._class->variables[i].identifier == p_symbol) {
  2203. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2204. r_result.location = context._class->variables[i].line;
  2205. return OK;
  2206. }
  2207. }
  2208. }
  2209. //guess in autoloads as singletons
  2210. List<PropertyInfo> props;
  2211. ProjectSettings::get_singleton()->get_property_list(&props);
  2212. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2213. String s = E->get().name;
  2214. if (!s.begins_with("autoload/"))
  2215. continue;
  2216. String name = s.get_slice("/", 1);
  2217. if (name == String(p_symbol)) {
  2218. String path = ProjectSettings::get_singleton()->get(s);
  2219. if (path.begins_with("*")) {
  2220. String script = path.substr(1, path.length());
  2221. if (!script.ends_with(".gd")) {
  2222. //not a script, try find the script anyway,
  2223. //may have some success
  2224. script = script.get_basename() + ".gd";
  2225. }
  2226. if (FileAccess::exists(script)) {
  2227. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2228. r_result.location = 0;
  2229. r_result.script = ResourceLoader::load(script);
  2230. return OK;
  2231. }
  2232. }
  2233. }
  2234. }
  2235. //global
  2236. for (Map<StringName, int>::Element *E = GDScriptLanguage::get_singleton()->get_global_map().front(); E; E = E->next()) {
  2237. if (E->key() == p_symbol) {
  2238. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[E->get()];
  2239. if (value.get_type() == Variant::OBJECT) {
  2240. Object *obj = value;
  2241. if (obj) {
  2242. if (obj->cast_to<GDNativeClass>()) {
  2243. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2244. r_result.class_name = obj->cast_to<GDNativeClass>()->get_name();
  2245. } else {
  2246. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS;
  2247. r_result.class_name = obj->get_class();
  2248. }
  2249. return OK;
  2250. }
  2251. } else {
  2252. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2253. r_result.class_name = "@Global Scope";
  2254. r_result.class_member = p_symbol;
  2255. return OK;
  2256. }
  2257. }
  2258. }
  2259. #if 0
  2260. GDCompletionIdentifier identifier;
  2261. if (_guess_identifier_type(context,p.get_completion_line(),p_symbol,identifier)) {
  2262. print_line("var type: "+Variant::get_type_name(identifier.type));
  2263. if (identifier.script.is_valid()) {
  2264. print_line("var script: "+identifier.script->get_path());
  2265. }
  2266. print_line("obj type: "+String(identifier.obj_type));
  2267. print_line("value: "+String(identifier.value));
  2268. }
  2269. #endif
  2270. }
  2271. } break;
  2272. case GDParser::COMPLETION_PARENT_FUNCTION: {
  2273. } break;
  2274. case GDParser::COMPLETION_METHOD:
  2275. isfunction = true;
  2276. case GDParser::COMPLETION_INDEX: {
  2277. const GDParser::Node *node = p.get_completion_node();
  2278. if (node->type != GDParser::Node::TYPE_OPERATOR)
  2279. break;
  2280. GDCompletionIdentifier t;
  2281. if (_guess_expression_type(context, static_cast<const GDParser::OperatorNode *>(node)->arguments[0], p.get_completion_line(), t)) {
  2282. if (t.type == Variant::OBJECT && t.obj_type == "GDNativeClass") {
  2283. //native enum
  2284. Ref<GDNativeClass> gdn = t.value;
  2285. if (gdn.is_valid()) {
  2286. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2287. r_result.class_name = gdn->get_name();
  2288. r_result.class_member = p_symbol;
  2289. return OK;
  2290. }
  2291. } else if (t.type == Variant::OBJECT && t.obj_type != StringName()) {
  2292. Ref<GDScript> on_script;
  2293. if (t.value.get_type()) {
  2294. Object *obj = t.value;
  2295. if (obj) {
  2296. on_script = obj->get_script();
  2297. if (on_script.is_valid()) {
  2298. int loc = on_script->get_member_line(p_symbol);
  2299. if (loc >= 0) {
  2300. r_result.script = on_script;
  2301. r_result.type = ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION;
  2302. r_result.location = loc;
  2303. return OK;
  2304. }
  2305. }
  2306. }
  2307. }
  2308. if (ClassDB::has_method(t.obj_type, p_symbol)) {
  2309. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2310. r_result.class_name = t.obj_type;
  2311. r_result.class_member = p_symbol;
  2312. return OK;
  2313. }
  2314. bool success;
  2315. ClassDB::get_integer_constant(t.obj_type, p_symbol, &success);
  2316. if (success) {
  2317. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2318. r_result.class_name = t.obj_type;
  2319. r_result.class_member = p_symbol;
  2320. return OK;
  2321. }
  2322. ClassDB::get_property_type(t.obj_type, p_symbol, &success);
  2323. if (success) {
  2324. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2325. r_result.class_name = t.obj_type;
  2326. r_result.class_member = p_symbol;
  2327. return OK;
  2328. }
  2329. } else {
  2330. Variant::CallError ce;
  2331. Variant v = Variant::construct(t.type, NULL, 0, ce);
  2332. bool valid;
  2333. v.get_numeric_constant_value(t.type, p_symbol, &valid);
  2334. if (valid) {
  2335. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT;
  2336. r_result.class_name = Variant::get_type_name(t.type);
  2337. r_result.class_member = p_symbol;
  2338. return OK;
  2339. }
  2340. //todo check all inputevent types for property
  2341. v.get(p_symbol, &valid);
  2342. if (valid) {
  2343. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY;
  2344. r_result.class_name = Variant::get_type_name(t.type);
  2345. r_result.class_member = p_symbol;
  2346. return OK;
  2347. }
  2348. if (v.has_method(p_symbol)) {
  2349. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2350. r_result.class_name = Variant::get_type_name(t.type);
  2351. r_result.class_member = p_symbol;
  2352. return OK;
  2353. }
  2354. }
  2355. }
  2356. } break;
  2357. case GDParser::COMPLETION_CALL_ARGUMENTS: {
  2358. return ERR_CANT_RESOLVE;
  2359. } break;
  2360. case GDParser::COMPLETION_VIRTUAL_FUNC: {
  2361. GDCompletionIdentifier cid = _get_native_class(context);
  2362. if (cid.obj_type != StringName()) {
  2363. List<MethodInfo> vm;
  2364. ClassDB::get_virtual_methods(cid.obj_type, &vm);
  2365. for (List<MethodInfo>::Element *E = vm.front(); E; E = E->next()) {
  2366. if (p_symbol == E->get().name) {
  2367. r_result.type = ScriptLanguage::LookupResult::RESULT_CLASS_METHOD;
  2368. r_result.class_name = cid.obj_type;
  2369. r_result.class_member = p_symbol;
  2370. return OK;
  2371. }
  2372. }
  2373. }
  2374. } break;
  2375. case GDParser::COMPLETION_YIELD: {
  2376. return ERR_CANT_RESOLVE;
  2377. } break;
  2378. }
  2379. return ERR_CANT_RESOLVE;
  2380. }
  2381. #endif