From 8c1092455e6b110a28ff27e76a7e21e48fd0a038 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 29 Mar 2021 13:17:49 +0200 Subject: Use `find_node_at_range` --- crates/ide_assists/src/assist_context.rs | 5 +++- .../ide_assists/src/handlers/extract_type_alias.rs | 28 +++------------------- 2 files changed, 7 insertions(+), 26 deletions(-) (limited to 'crates/ide_assists/src') diff --git a/crates/ide_assists/src/assist_context.rs b/crates/ide_assists/src/assist_context.rs index 1482d37f8..8714e4978 100644 --- a/crates/ide_assists/src/assist_context.rs +++ b/crates/ide_assists/src/assist_context.rs @@ -13,7 +13,7 @@ use ide_db::{ RootDatabase, }; use syntax::{ - algo::{self, find_node_at_offset, SyntaxRewriter}, + algo::{self, find_node_at_offset, find_node_at_range, SyntaxRewriter}, AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize, TokenAtOffset, }; @@ -89,6 +89,9 @@ impl<'a> AssistContext<'a> { pub(crate) fn find_node_at_offset(&self) -> Option { find_node_at_offset(self.source_file.syntax(), self.offset()) } + pub(crate) fn find_node_at_range(&self) -> Option { + find_node_at_range(self.source_file.syntax(), self.frange.range) + } pub(crate) fn find_node_at_offset_with_descend(&self) -> Option { self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) } diff --git a/crates/ide_assists/src/handlers/extract_type_alias.rs b/crates/ide_assists/src/handlers/extract_type_alias.rs index 171b87918..f75d35462 100644 --- a/crates/ide_assists/src/handlers/extract_type_alias.rs +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs @@ -1,7 +1,4 @@ -use syntax::{ - ast::{self, AstNode}, - SyntaxKind, -}; +use syntax::ast::{self, AstNode}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -27,28 +24,9 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti return None; } - let node = match ctx.covering_element() { - syntax::NodeOrToken::Node(node) => node, - syntax::NodeOrToken::Token(tok) => tok.parent()?, - }; - let range = node.text_range(); - let mut type_like_node = None; - for node in node.ancestors() { - if node.text_range() != range { - break; - } - - let kind = node.kind(); - if ast::Type::can_cast(kind) || kind == SyntaxKind::TYPE_ARG { - type_like_node = Some(node); - break; - } - } - - let node = type_like_node?; - + let node = ctx.find_node_at_range::()?; let insert = ctx.find_node_at_offset::()?.syntax().text_range().start(); - let target = node.text_range(); + let target = node.syntax().text_range(); acc.add( AssistId("extract_type_alias", AssistKind::RefactorExtract), -- cgit v1.2.3