Use find_node_at_range

This commit is contained in:
Jonas Schievink 2021-03-29 13:17:49 +02:00
parent e6580aa380
commit 8c1092455e
2 changed files with 7 additions and 26 deletions

View File

@ -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<N: AstNode>(&self) -> Option<N> {
find_node_at_offset(self.source_file.syntax(), self.offset())
}
pub(crate) fn find_node_at_range<N: AstNode>(&self) -> Option<N> {
find_node_at_range(self.source_file.syntax(), self.frange.range)
}
pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> {
self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset())
}

View File

@ -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::<ast::Type>()?;
let insert = ctx.find_node_at_offset::<ast::Item>()?.syntax().text_range().start();
let target = node.text_range();
let target = node.syntax().text_range();
acc.add(
AssistId("extract_type_alias", AssistKind::RefactorExtract),