diff options
author | Jonas Schievink <[email protected]> | 2021-03-29 12:17:49 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-03-29 12:17:49 +0100 |
commit | 8c1092455e6b110a28ff27e76a7e21e48fd0a038 (patch) | |
tree | 66750f6cd984712aa59a3cb4fdd9347655e21d91 /crates/ide_assists/src | |
parent | e6580aa380ed68cbd3a8e05aa6944d759d3ea247 (diff) |
Use `find_node_at_range`
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r-- | crates/ide_assists/src/assist_context.rs | 5 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/extract_type_alias.rs | 28 |
2 files changed, 7 insertions, 26 deletions
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::{ | |||
13 | RootDatabase, | 13 | RootDatabase, |
14 | }; | 14 | }; |
15 | use syntax::{ | 15 | use syntax::{ |
16 | algo::{self, find_node_at_offset, SyntaxRewriter}, | 16 | algo::{self, find_node_at_offset, find_node_at_range, SyntaxRewriter}, |
17 | AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, | 17 | AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, |
18 | SyntaxToken, TextRange, TextSize, TokenAtOffset, | 18 | SyntaxToken, TextRange, TextSize, TokenAtOffset, |
19 | }; | 19 | }; |
@@ -89,6 +89,9 @@ impl<'a> AssistContext<'a> { | |||
89 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { | 89 | pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> { |
90 | find_node_at_offset(self.source_file.syntax(), self.offset()) | 90 | find_node_at_offset(self.source_file.syntax(), self.offset()) |
91 | } | 91 | } |
92 | pub(crate) fn find_node_at_range<N: AstNode>(&self) -> Option<N> { | ||
93 | find_node_at_range(self.source_file.syntax(), self.frange.range) | ||
94 | } | ||
92 | pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> { | 95 | pub(crate) fn find_node_at_offset_with_descend<N: AstNode>(&self) -> Option<N> { |
93 | self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) | 96 | self.sema.find_node_at_offset_with_descend(self.source_file.syntax(), self.offset()) |
94 | } | 97 | } |
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 @@ | |||
1 | use syntax::{ | 1 | use syntax::ast::{self, AstNode}; |
2 | ast::{self, AstNode}, | ||
3 | SyntaxKind, | ||
4 | }; | ||
5 | 2 | ||
6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
7 | 4 | ||
@@ -27,28 +24,9 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
27 | return None; | 24 | return None; |
28 | } | 25 | } |
29 | 26 | ||
30 | let node = match ctx.covering_element() { | 27 | let node = ctx.find_node_at_range::<ast::Type>()?; |
31 | syntax::NodeOrToken::Node(node) => node, | ||
32 | syntax::NodeOrToken::Token(tok) => tok.parent()?, | ||
33 | }; | ||
34 | let range = node.text_range(); | ||
35 | let mut type_like_node = None; | ||
36 | for node in node.ancestors() { | ||
37 | if node.text_range() != range { | ||
38 | break; | ||
39 | } | ||
40 | |||
41 | let kind = node.kind(); | ||
42 | if ast::Type::can_cast(kind) || kind == SyntaxKind::TYPE_ARG { | ||
43 | type_like_node = Some(node); | ||
44 | break; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | let node = type_like_node?; | ||
49 | |||
50 | let insert = ctx.find_node_at_offset::<ast::Item>()?.syntax().text_range().start(); | 28 | let insert = ctx.find_node_at_offset::<ast::Item>()?.syntax().text_range().start(); |
51 | let target = node.text_range(); | 29 | let target = node.syntax().text_range(); |
52 | 30 | ||
53 | acc.add( | 31 | acc.add( |
54 | AssistId("extract_type_alias", AssistKind::RefactorExtract), | 32 | AssistId("extract_type_alias", AssistKind::RefactorExtract), |