Skip to content

Commit 24ebf65

Browse files
committed
Remove tokens in GDScriptParser::Node
- Add `GDScriptTokenizer::CodeArea` - Add `GDScriptParser::get_token(int p_line, int p_column)`
1 parent 46cd07a commit 24ebf65

File tree

4 files changed

+343
-248
lines changed

4 files changed

+343
-248
lines changed

modules/gdscript/gdscript_editor.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,33 +4693,32 @@ ::Error GDScriptLanguage::refactor_rename_symbol_code(const String &p_code, cons
46934693
r_result.sentinel = cursor_position;
46944694
r_result.code = p_code;
46954695

4696+
GDScriptTokenizer::Token context_token;
4697+
46964698
if (context.node) {
4697-
GDScriptTokenizer::Token context_token;
4699+
GDScriptTokenizer::CodeArea context_code_area = context.node->get_code_area();
4700+
4701+
if (context_code_area.contains(Pair<int, int>{ cursor_position.y, cursor_position.x - 1 })) {
4702+
context_token = parser.get_token(cursor_position.y, cursor_position.x - 1);
4703+
if (!context_code_area.contains(context_token.get_code_area())) {
4704+
context_token = GDScriptTokenizer::Token();
4705+
}
4706+
}
4707+
4708+
GDScriptTokenizer::Token context_token_temp = parser.get_token(cursor_position.y, cursor_position.x);
4709+
if (context_code_area.contains(context_token_temp.get_code_area())) {
4710+
context_token = context_token_temp;
4711+
}
4712+
46984713
switch (context.node->type) {
46994714
case GDScriptParser::Node::ANNOTATION: {
47004715
symbol = static_cast<GDScriptParser::AnnotationNode *>(context.node)->name;
47014716
} break;
47024717
case GDScriptParser::Node::ARRAY: {
4703-
// Non-specific result, cursor is in tokens.
4704-
GDScriptParser::ArrayNode *array_node = static_cast<GDScriptParser::ArrayNode *>(context.node);
4705-
if (array_node->token_array_bracket_open.has_cursor()) {
4706-
context_token = array_node->token_array_bracket_open;
4707-
} else if (array_node->token_array_bracket_close.has_cursor()) {
4708-
context_token = array_node->token_array_bracket_close;
4709-
} else {
4710-
for (GDScriptTokenizer::Token &argument_comma : array_node->token_array_argument_commas) {
4711-
if (argument_comma.has_cursor()) {
4712-
context_token = argument_comma;
4713-
break;
4714-
}
4715-
}
4716-
}
47174718
} break;
47184719
case GDScriptParser::Node::BREAK: {
4719-
context_token = static_cast<GDScriptParser::BreakNode *>(context.node)->token_break_keyword;
47204720
} break;
47214721
case GDScriptParser::Node::CONTINUE: {
4722-
context_token = static_cast<GDScriptParser::ContinueNode *>(context.node)->token_continue_keyword;
47234722
} break;
47244723
case GDScriptParser::Node::IDENTIFIER: {
47254724
symbol = static_cast<GDScriptParser::IdentifierNode *>(context.node)->name;

modules/gdscript/gdscript_parser.cpp

Lines changed: 26 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ bool GDScriptParser::annotation_exists(const String &p_annotation_name) const {
122122
return valid_annotations.has(p_annotation_name);
123123
}
124124

125+
GDScriptTokenizer::Token GDScriptParser::get_token(int p_line, int p_column) const {
126+
ERR_FAIL_COND_V(p_line < 1, {});
127+
ERR_FAIL_COND_V(p_column < 1, {});
128+
129+
Pair<int, int> position = { p_line, p_column };
130+
131+
for (const GDScriptTokenizer::Token &token : tokens) {
132+
GDScriptTokenizer::CodeArea token_area = token.get_code_area();
133+
if (token_area.contains(position)) {
134+
return token;
135+
}
136+
if (token_area.is_after(position)) {
137+
return {};
138+
}
139+
}
140+
return {};
141+
}
142+
125143
GDScriptParser::GDScriptParser() {
126144
// Register valid annotations.
127145
if (unlikely(valid_annotations.is_empty())) {
@@ -377,42 +395,13 @@ void GDScriptParser::set_last_completion_call_arg(int p_argument) {
377395
completion_call_stack.back()->get().argument = p_argument;
378396
}
379397

380-
bool GDScriptParser::refactor_rename_is_cursor_between_tokens(const GDScriptTokenizer::Token &p_token_start, const GDScriptTokenizer::Token &p_token_end) const {
381-
if (!is_for_refactor_rename()) {
382-
return false;
383-
}
384-
if (p_token_start.type == GDScriptTokenizer::Token::Type::EMPTY || p_token_end.type == GDScriptTokenizer::Token::Type::EMPTY) {
385-
return false;
386-
}
387-
388-
if (previous.start_line < p_token_start.start_line) {
389-
return false;
390-
}
391-
if (p_token_end.end_line < previous.end_line) {
392-
return false;
393-
}
394-
if (previous.start_column < p_token_start.start_column) {
395-
return false;
396-
}
397-
if (p_token_end.end_column < previous.end_column) {
398-
return false;
399-
}
400-
401-
return true;
402-
}
403-
404398
bool GDScriptParser::refactor_rename_does_node_contains_cursor(const GDScriptParser::Node *p_node) const {
405399
if (!is_for_refactor_rename()) {
406400
return false;
407401
}
408402
ERR_FAIL_NULL_V(p_node, false);
409-
if (tokenizer->get_cursor_line() < p_node->start_line || tokenizer->get_cursor_line() > p_node->end_line) {
410-
return false;
411-
}
412-
if (tokenizer->get_cursor_column() < p_node->start_column || tokenizer->get_cursor_column() > p_node->end_column) {
413-
return false;
414-
}
415-
return true;
403+
404+
return p_node->get_code_area().contains(tokenizer->get_cursor_line(), tokenizer->get_cursor_column());
416405
}
417406

418407
bool GDScriptParser::refactor_rename_does_token_have_cursor(const GDScriptTokenizer::Token &p_token) const {
@@ -436,30 +425,12 @@ bool GDScriptParser::refactor_rename_is_node_more_specific(const GDScriptParser:
436425
ERR_FAIL_NULL_V(refactor_rename_context.node, false);
437426

438427
GDScriptParser::Node *other_node = refactor_rename_context.node;
439-
440-
// `p_node` must start after `other_node`.
441-
if (p_node->start_line < other_node->start_line) {
442-
return false;
443-
}
444-
if (p_node->start_line == other_node->start_line && p_node->start_column < other_node->start_column) {
445-
return false;
446-
}
447-
// If `other_node` ends before `p_node` starts, `p_node` is more specific.
448-
if (other_node->end_line < p_node->start_line) {
428+
GDScriptTokenizer::CodeArea other_code_area = other_node->get_code_area();
429+
GDScriptTokenizer::CodeArea node_code_area = p_node->get_code_area();
430+
if (node_code_area.is_after(other_code_area)) {
449431
return true;
450432
}
451-
if (other_node->end_line == p_node->start_line && other_node->end_column < p_node->start_column) {
452-
return true;
453-
}
454-
// `p_node` must end before `other_node`.
455-
if (p_node->end_line > other_node->end_line) {
456-
return false;
457-
}
458-
if (p_node->end_line == other_node->end_line && p_node->end_column > other_node->end_column) {
459-
return false;
460-
}
461-
462-
return true;
433+
return other_code_area.contains(node_code_area);
463434
}
464435

465436
void GDScriptParser::refactor_rename_set_context(RefactorRenameType p_type) {
@@ -478,87 +449,6 @@ void GDScriptParser::refactor_rename_set_context(RefactorRenameType p_type) {
478449
refactor_rename_context = context;
479450
}
480451

481-
// void GDScriptParser::make_refactor_rename_context(RefactorRenameContextSetup p_setup) {
482-
// if (!is_for_refactor_rename()) {
483-
// return;
484-
// }
485-
486-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::OVERRIDE)) {
487-
// if (p_setup.overriden_node == nullptr) {
488-
// return;
489-
// }
490-
// if (refactor_rename_context.node != p_setup.overriden_node) {
491-
// return;
492-
// }
493-
// }
494-
495-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::CHECK_TOKEN_FOR_CURSOR)) {
496-
// ERR_FAIL_NULL(p_setup.token);
497-
// if (p_setup.token->cursor_place == GDScriptTokenizer::CURSOR_NONE) {
498-
// return;
499-
// }
500-
// }
501-
502-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::CHECK_IF_CURSOR_IS_INBETWEEN_TOKEN_START_END)) {
503-
// ERR_FAIL_NULL(p_setup.token_start);
504-
// ERR_FAIL_NULL(p_setup.token_end);
505-
// if (p_setup.token_start->type == GDScriptTokenizer::Token::Type::EMPTY || p_setup.token_end->type == GDScriptTokenizer::Token::Type::EMPTY) {
506-
// return;
507-
// }
508-
509-
// if (previous.start_line < p_setup.token_start->start_line) {
510-
// return;
511-
// }
512-
// if (p_setup.token_end->end_line < previous.end_line) {
513-
// return;
514-
// }
515-
// if (previous.start_column < p_setup.token_start->start_column) {
516-
// return;
517-
// }
518-
// if (p_setup.token_end->end_column < previous.end_column) {
519-
// return;
520-
// }
521-
// }
522-
523-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::CHECK_FOR_CURSOR)) {
524-
// if (previous.cursor_place == GDScriptTokenizerText::CURSOR_NONE && current.cursor_place == GDScriptTokenizerText::CURSOR_NONE) {
525-
// return;
526-
// }
527-
// }
528-
529-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::CHECK_SPECIFICITY)) {
530-
// if (refactor_rename_context.node) {
531-
// }
532-
// }
533-
534-
// RefactorRenameContext context;
535-
// context.type = p_setup.type;
536-
// context.current_class = current_class;
537-
// context.current_function = current_function;
538-
// context.current_suite = current_suite;
539-
// context.current_line = tokenizer->get_cursor_line();
540-
// context.parser = this;
541-
542-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::APPLY_OVERRIDEN_NODE)) {
543-
// context.node = p_setup.node;
544-
// }
545-
546-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::APPLY_CURRENT_ARGUMENT)) {
547-
// context.current_argument = p_setup.current_argument;
548-
// }
549-
550-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::APPLY_BUILTIN_TYPE)) {
551-
// context.builtin_type = p_setup.builtin_type;
552-
// }
553-
554-
// if (p_setup.flags.has_flag(RefactorRenameContextSetup::APPLY_TOKEN)) {
555-
// ERR_FAIL_NULL(p_setup.token);
556-
// context.token = *p_setup.token;
557-
// }
558-
559-
// refactor_rename_context = context;
560-
// }
561-
562452
Error GDScriptParser::parse(const String &p_source_code, const String &p_script_path, ParsingType p_type, bool p_parse_body) {
563453
clear();
564454

@@ -687,6 +577,8 @@ GDScriptTokenizer::Token GDScriptParser::advance() {
687577
update_extents(n);
688578
}
689579
}
580+
581+
tokens.push_back(previous);
690582
return previous;
691583
}
692584

@@ -3663,10 +3555,6 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_attribute(ExpressionNode *
36633555
Variant::Type builtin_type = get_builtin_type(id->name);
36643556
if (builtin_type < Variant::VARIANT_MAX) {
36653557
make_completion_context(COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, builtin_type);
3666-
if (refactor_rename_was_cursor_just_parsed()) {
3667-
refactor_rename_set_context(REFACTOR_RENAME_TYPE_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD);
3668-
refactor_rename_context.builtin_type = builtin_type;
3669-
}
36703558
is_builtin = true;
36713559
}
36723560
}

0 commit comments

Comments
 (0)