more code completion improvements

-calltip dissapears with more types of keypresses or when pressing ')'
-properly looks into autoloaded scripts or nodes with another script for
script functions/variables/etc.
This commit is contained in:
Juan Linietsky 2015-06-27 15:52:39 -03:00
parent a67486a39e
commit 2b64f73b04
5 changed files with 487 additions and 90 deletions

View File

@ -8,12 +8,12 @@ var max_points = 0
func _ready():
var f = File.new()
#load high score
if (f.open("user://highscore",File.READ)==OK):
max_points=f.get_var()
func game_over():
if (points>max_points):
max_points=points

View File

@ -14,7 +14,6 @@ var offset=0
func _process(delta):
offset+=delta*SPEED
set_pos(Vector2(offset,0))

View File

@ -29,6 +29,7 @@
#include "gd_script.h"
#include "gd_compiler.h"
#include "globals.h"
#include "os/file_access.h"
void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
@ -317,6 +318,7 @@ String GDScriptLanguage::make_function(const String& p_class,const String& p_nam
struct GDCompletionIdentifier {
StringName obj_type;
Ref<GDScript> script;
Variant::Type type;
Variant value; //im case there is a value, also return it
};
@ -446,7 +448,7 @@ static Ref<Reference> _get_parent_class(GDCompletionContext& context) {
base_class=base_class->subclasses[subclass];
} else {
print_line("Could not find subclass: "+subclass);
//print_line("Could not find subclass: "+subclass);
return _get_type_from_class(context); //fail please
}
}
@ -631,7 +633,9 @@ static bool _guess_expression_type(GDCompletionContext& context,const GDParser::
//try calling the function if constant and all args are constant, should not crash..
Object *baseptr = base.value;
if (baseptr && mb->is_const() && pi.type==Variant::OBJECT) {
if (mb->is_const() && pi.type==Variant::OBJECT) {
bool all_valid=true;
Vector<Variant> args;
for(int i=2;i<op->arguments.size();i++) {
@ -648,6 +652,68 @@ static bool _guess_expression_type(GDCompletionContext& context,const GDParser::
all_valid=false;
}
}
if (all_valid && String(id)=="get_node" && ObjectTypeDB::is_type(base.obj_type,"Node") && args.size()) {
String arg1=args[0];
if (arg1.begins_with("/root/")) {
String which = arg1.get_slice("/",2);
if (which!="") {
List<PropertyInfo> props;
Globals::get_singleton()->get_property_list(&props);
//print_line("find singleton");
for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
String s = E->get().name;
if (!s.begins_with("autoload/"))
continue;
//print_line("found "+s);
String name = s.get_slice("/",1);
//print_line("name: "+name+", which: "+which);
if (name==which) {
String script = Globals::get_singleton()->get(s);
if (!script.begins_with("res://")) {
script="res://"+script;
}
if (!script.ends_with(".gd")) {
//not a script, try find the script anyway,
//may have some success
script=script.basename()+".gd";
}
if (FileAccess::exists(script)) {
//print_line("is a script");
Ref<Script> scr;
if (ScriptCodeCompletionCache::get_sigleton())
scr = ScriptCodeCompletionCache::get_sigleton()->get_cached_resource(script);
else
scr = ResourceLoader::load(script);
r_type.obj_type="Node";
r_type.type=Variant::OBJECT;
r_type.script=scr;
r_type.value=Variant();
return true;
}
}
}
}
}
}
if (baseptr) {
if (all_valid) {
Vector<const Variant*> argptr;
for(int i=0;i<args.size();i++) {
@ -669,6 +735,7 @@ static bool _guess_expression_type(GDCompletionContext& context,const GDParser::
}
}
}
r_type.type=pi.type;
if (pi.hint==PROPERTY_HINT_RESOURCE_TYPE) {
@ -1281,6 +1348,7 @@ static void _make_function_hint(const GDParser::FunctionNode* p_func,int p_argid
static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const StringName& p_method,const GDCompletionIdentifier& id, int p_argidx, Set<String>& result, String& arghint) {
//print_line("find type arguments?");
if (id.type==Variant::INPUT_EVENT && String(p_method)=="is_action" && p_argidx==0) {
List<PropertyInfo> pinfo;
@ -1301,8 +1369,212 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
MethodBind *m = ObjectTypeDB::get_method(id.obj_type,p_method);
if (!m)
if (!m) {
//not in static method, see script
//print_line("not in static: "+String(p_method));
Ref<GDScript> on_script;
if (id.value.get_type()) {
Object *obj=id.value;
if (obj) {
GDScript *scr = obj->cast_to<GDScript>();
if (scr) {
while (scr) {
for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
if (E->get().is_static() && p_method==E->get().get_name()) {
arghint="static func "+String(p_method)+"(";
for(int i=0;i<E->get().get_argument_count();i++) {
if (i>0)
arghint+=", ";
else
arghint+=" ";
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
arghint+="var "+E->get().get_argument_name(i);
int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
if (i>=deffrom) {
int defidx = deffrom-i;
if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
}
}
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
}
arghint+=")";
return; //found
}
}
if (scr->get_base().is_valid())
scr=scr->get_base().ptr();
else
scr=NULL;
}
} else {
on_script=obj->get_script();
}
}
}
//print_line("but it has a script?");
if (!on_script.is_valid() && id.script.is_valid()) {
//print_line("yes");
on_script=id.script;
}
if (on_script.is_valid()) {
GDScript *scr = on_script.ptr();
if (scr) {
while (scr) {
String code = scr->get_source_code();
//print_line("has source code!");
if (code!="") {
//if there is code, parse it. This way is slower but updates in real-time
GDParser p;
//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);
Error err = p.parse(scr->get_source_code(),scr->get_path().get_base_dir(),true,"",false);
if (err==OK) {
//print_line("checking the functions...");
//only if ok, otherwise use what is cached on the script
//GDParser::ClassNode *base = p.
const GDParser::Node *root = p.get_parse_tree();
ERR_FAIL_COND(root->type!=GDParser::Node::TYPE_CLASS);
const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
const GDParser::FunctionNode* func=NULL;
bool st=false;
for(int i=0;i<cl->functions.size();i++) {
//print_line(String(cl->functions[i]->name)+" vs "+String(p_method));
if (cl->functions[i]->name==p_method) {
func=cl->functions[i];
}
}
for(int i=0;i<cl->static_functions.size();i++) {
//print_line(String(cl->static_functions[i]->name)+" vs "+String(p_method));
if (cl->static_functions[i]->name==p_method) {
func=cl->static_functions[i];
st=true;
}
}
if (func) {
arghint="func "+String(p_method)+"(";
if (st)
arghint="static "+arghint;
for(int i=0;i<func->arguments.size();i++) {
if (i>0)
arghint+=", ";
else
arghint+=" ";
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
arghint+="var "+String(func->arguments[i]);
int deffrom = func->arguments.size()-func->default_values.size();
if (i>=deffrom) {
int defidx = deffrom-i;
if (defidx>=0 && defidx<func->default_values.size() && func->default_values[defidx]->type==GDParser::Node::TYPE_OPERATOR) {
const GDParser::OperatorNode *op=static_cast<const GDParser::OperatorNode *>(func->default_values[defidx]);
if (op->op==GDParser::OperatorNode::OP_ASSIGN) {
const GDParser::ConstantNode *cn=static_cast<const GDParser::ConstantNode *>(op->arguments[1]);
arghint+="="+cn->value.get_construct_string();
}
}
}
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
}
arghint+=" )";
return;
}
} else {
//print_line("failed parsing?");
code="";
}
}
if (code=="") {
for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
if (p_method==E->get().get_name()) {
arghint="func "+String(p_method)+"(";
for(int i=0;i<E->get().get_argument_count();i++) {
if (i>0)
arghint+=", ";
else
arghint+=" ";
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
arghint+="var "+E->get().get_argument_name(i);
int deffrom = E->get().get_argument_count()-E->get().get_default_argument_count();
if (i>=deffrom) {
int defidx = deffrom-i;
if (defidx>=0 && defidx<E->get().get_default_argument_count()) {
arghint+="="+E->get().get_default_argument(defidx).get_construct_string();
}
}
if (i==p_argidx) {
arghint+=String::chr(0xFFFF);
}
}
arghint+=")";
return; //found
}
}
#if 0
//use class directly, no code was found
if (!isfunction) {
for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
options.insert(E->key());
}
}
for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
options.insert(String(E->key())+"(");
}
for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
options.insert(E->get());
}
#endif
}
if (scr->get_base().is_valid())
scr=scr->get_base().ptr();
else
scr=NULL;
}
}
}
} else {
//regular method
if (p_method.operator String()=="connect") {
@ -1323,11 +1595,7 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
}*/
} else {
Object *obj=id.value;
if (obj) {
List<String> options;
obj->get_argument_options(p_method,p_argidx,&options);
if (obj->is_type("Node") && p_argidx==0 && (String(p_method)=="get_node" || String(p_method)=="has_node")) {
if (p_argidx==0 && (String(p_method)=="get_node" || String(p_method)=="has_node") && ObjectTypeDB::is_type(id.obj_type,"Node")) {
List<PropertyInfo> props;
Globals::get_singleton()->get_property_list(&props);
@ -1339,9 +1607,15 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
continue;
// print_line("found "+s);
String name = s.get_slice("/",1);
options.push_back("\"/root/"+name+"\"");
result.insert("\"/root/"+name+"\"");
}
}
Object *obj=id.value;
if (obj) {
List<String> options;
obj->get_argument_options(p_method,p_argidx,&options);
for(List<String>::Element *E=options.front();E;E=E->next()) {
result.insert(E->get());
@ -1388,6 +1662,7 @@ static void _find_type_arguments(const GDParser::Node*p_node,int p_line,const St
arghint+=")";
}
}
}
@ -1434,7 +1709,7 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
arghint+=")";
} else if (op->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
//complete built-in function
//complete constructor
const GDParser::TypeNode *tn = static_cast<const GDParser::TypeNode*>(op->arguments[0]);
List<MethodInfo> mil;
@ -1569,7 +1844,7 @@ static void _find_call_arguments(GDCompletionContext& context,const GDParser::No
}
} else {
//indexed lookup
GDCompletionIdentifier ci;
if (_guess_expression_type(context,op->arguments[0],p_line,ci)) {
@ -1735,11 +2010,17 @@ Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base
if (t.type==Variant::OBJECT && t.obj_type!=StringName()) {
Ref<GDScript> on_script;
if (t.value.get_type()) {
Object *obj=t.value;
if (obj) {
GDScript *scr = obj->cast_to<GDScript>();
if (scr) {
while (scr) {
if (!isfunction) {
@ -1748,9 +2029,106 @@ Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base
}
}
for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
if (E->get().is_static())
options.insert(E->key());
}
if (scr->get_base().is_valid())
scr=scr->get_base().ptr();
else
scr=NULL;
}
} else {
on_script=obj->get_script();
}
}
}
if (!on_script.is_valid() && t.script.is_valid()) {
on_script=t.script;
}
if (on_script.is_valid()) {
GDScript *scr = on_script.ptr();
if (scr) {
while (scr) {
String code = scr->get_source_code();
if (code!="") {
//if there is code, parse it. This way is slower but updates in real-time
GDParser p;
//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);
Error err = p.parse(scr->get_source_code(),scr->get_path().get_base_dir(),true,"",false);
if (err==OK) {
//only if ok, otherwise use what is cached on the script
//GDParser::ClassNode *base = p.
const GDParser::Node *root = p.get_parse_tree();
ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_PARSE_ERROR);
const GDParser::ClassNode *cl = static_cast<const GDParser::ClassNode*>(root);
for(int i=0;i<cl->functions.size();i++) {
if (cl->functions[i]->arguments.size())
options.insert(String(cl->functions[i]->name)+"(");
else
options.insert(String(cl->functions[i]->name)+"()");
}
for(int i=0;i<cl->static_functions.size();i++) {
if (cl->static_functions[i]->arguments.size())
options.insert(String(cl->static_functions[i]->name)+"(");
else
options.insert(String(cl->static_functions[i]->name)+"()");
}
if (!isfunction) {
for(int i=0;i<cl->variables.size();i++) {
options.insert(String(cl->variables[i].identifier));
}
for(int i=0;i<cl->constant_expressions.size();i++) {
options.insert(String(cl->constant_expressions[i].identifier));
}
}
} else {
code=""; //well, then no code
}
}
if (code=="") {
//use class directly, no code was found
if (!isfunction) {
for (const Map<StringName,Variant>::Element *E=scr->get_constants().front();E;E=E->next()) {
options.insert(E->key());
}
}
for (const Map<StringName,GDFunction>::Element *E=scr->get_member_functions().front();E;E=E->next()) {
if (E->get().get_argument_count())
options.insert(String(E->key())+"()");
else
options.insert(String(E->key())+"(");
}
for (const Set<StringName>::Element *E=scr->get_members().front();E;E=E->next()) {
options.insert(E->get());
}
}
if (scr->get_base().is_valid())
scr=scr->get_base().ptr();
else
@ -1760,6 +2138,10 @@ Error GDScriptLanguage::complete_code(const String& p_code, const String& p_base
}
if (!isfunction) {
ObjectTypeDB::get_integer_constant_list(t.obj_type,r_options);
}

View File

@ -1040,7 +1040,7 @@ void GDTokenizerText::advance(int p_amount) {
//////////////////////////////////////////////////////////////////////////////////////////////////////
#define BYTECODE_VERSION 4
#define BYTECODE_VERSION 5
Error GDTokenizerBuffer::set_code_buffer(const Vector<uint8_t> & p_buffer) {

View File

@ -1842,6 +1842,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.mod.shift)
_post_shift_selection();
_cancel_completion();
completion_hint="";
} break;
case KEY_END: {
@ -1856,6 +1858,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.mod.shift)
_post_shift_selection();
_cancel_completion();
completion_hint="";
} break;
#endif
case KEY_PAGEUP: {
@ -1868,6 +1873,10 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.mod.shift)
_post_shift_selection();
_cancel_completion();
completion_hint="";
} break;
case KEY_PAGEDOWN: {
@ -1879,6 +1888,10 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (k.mod.shift)
_post_shift_selection();
_cancel_completion();
completion_hint="";
} break;
case KEY_A: {
@ -2064,6 +2077,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
const CharType chr[2] = {(CharType)k.unicode, 0};
if (completion_hint!="" && k.unicode==')') {
completion_hint="";
}
if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
_consume_pair_symbol(chr[0]);
} else {