Ver código fonte

Implemented, The Amazing Zylann Hack (tm), fixes #10603

Juan Linietsky 8 anos atrás
pai
commit
b1c0e45b03

+ 14 - 0
modules/gdscript/gd_editor.cpp

@@ -930,6 +930,20 @@ static bool _guess_expression_type(GDCompletionContext &context, const GDParser:
 
 
 static bool _guess_identifier_type_in_block(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
 static bool _guess_identifier_type_in_block(GDCompletionContext &context, int p_line, const StringName &p_identifier, GDCompletionIdentifier &r_type) {
 
 
+	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) {
+		//is used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
+		//super dirty hack, but very useful
+		//credit: Zylann
+		//TODO: this could be hacked to detect ANDed conditions too..
+		const GDParser::OperatorNode *op = static_cast<const GDParser::OperatorNode *>(context.block->if_condition);
+		if (op->arguments[0]->type == GDParser::Node::TYPE_IDENTIFIER && static_cast<const GDParser::IdentifierNode *>(op->arguments[0])->name == p_identifier) {
+			//bingo
+			if (_guess_expression_type(context, op->arguments[1], op->line, r_type)) {
+				return true;
+			}
+		}
+	}
+
 	GDCompletionIdentifier gdi = _get_native_class(context);
 	GDCompletionIdentifier gdi = _get_native_class(context);
 	if (gdi.obj_type != StringName()) {
 	if (gdi.obj_type != StringName()) {
 		bool valid;
 		bool valid;

+ 2 - 0
modules/gdscript/gd_parser.cpp

@@ -2470,6 +2470,8 @@ void GDParser::_parse_block(BlockNode *p_block, bool p_static) {
 
 
 				cf_if->body = alloc_node<BlockNode>();
 				cf_if->body = alloc_node<BlockNode>();
 				cf_if->body->parent_block = p_block;
 				cf_if->body->parent_block = p_block;
+				cf_if->body->if_condition = condition; //helps code completion
+
 				p_block->sub_blocks.push_back(cf_if->body);
 				p_block->sub_blocks.push_back(cf_if->body);
 
 
 				if (!_enter_indent_block(cf_if->body)) {
 				if (!_enter_indent_block(cf_if->body)) {

+ 3 - 0
modules/gdscript/gd_parser.h

@@ -146,10 +146,13 @@ public:
 		Vector<StringName> variables;
 		Vector<StringName> variables;
 		Vector<int> variable_lines;
 		Vector<int> variable_lines;
 
 
+		Node *if_condition; //tiny hack to improve code completion on if () blocks
+
 		//the following is useful for code completion
 		//the following is useful for code completion
 		List<BlockNode *> sub_blocks;
 		List<BlockNode *> sub_blocks;
 		int end_line;
 		int end_line;
 		BlockNode() {
 		BlockNode() {
+			if_condition = NULL;
 			type = TYPE_BLOCK;
 			type = TYPE_BLOCK;
 			end_line = -1;
 			end_line = -1;
 			parent_block = NULL;
 			parent_block = NULL;