diff options
Diffstat (limited to 'crates/libeditor/src/code_actions.rs')
-rw-r--r-- | crates/libeditor/src/code_actions.rs | 18 |
1 files changed, 4 insertions, 14 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index b3305be2a..f53a8f9c6 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs | |||
@@ -3,7 +3,7 @@ use std::{ | |||
3 | }; | 3 | }; |
4 | 4 | ||
5 | use libsyntax2::{ | 5 | use libsyntax2::{ |
6 | File, | 6 | File, TextUnit, |
7 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, | 7 | ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, |
8 | SyntaxKind::COMMA, | 8 | SyntaxKind::COMMA, |
9 | SyntaxNodeRef, | 9 | SyntaxNodeRef, |
@@ -13,7 +13,7 @@ use libsyntax2::{ | |||
13 | }, | 13 | }, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | use {TextUnit, EditBuilder, Edit}; | 16 | use {EditBuilder, Edit, find_node_at_offset}; |
17 | 17 | ||
18 | #[derive(Debug)] | 18 | #[derive(Debug)] |
19 | pub struct ActionResult { | 19 | pub struct ActionResult { |
@@ -39,7 +39,7 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() | |||
39 | } | 39 | } |
40 | 40 | ||
41 | pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | 41 | pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { |
42 | let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?; | 42 | let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?; |
43 | Some(move || { | 43 | Some(move || { |
44 | let derive_attr = nominal | 44 | let derive_attr = nominal |
45 | .attrs() | 45 | .attrs() |
@@ -66,7 +66,7 @@ pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() | |||
66 | } | 66 | } |
67 | 67 | ||
68 | pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { | 68 | pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> ActionResult + 'a> { |
69 | let nominal = find_node::<ast::NominalDef>(file.syntax(), offset)?; | 69 | let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?; |
70 | let name = nominal.name()?; | 70 | let name = nominal.name()?; |
71 | 71 | ||
72 | Some(move || { | 72 | Some(move || { |
@@ -105,16 +105,6 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta | |||
105 | .find(|node| !node.kind().is_trivia()) | 105 | .find(|node| !node.kind().is_trivia()) |
106 | } | 106 | } |
107 | 107 | ||
108 | pub fn find_node<'a, N: AstNode<'a>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option<N> { | ||
109 | let leaves = find_leaf_at_offset(syntax, offset); | ||
110 | let leaf = leaves.clone() | ||
111 | .find(|leaf| !leaf.kind().is_trivia()) | ||
112 | .or_else(|| leaves.right_biased())?; | ||
113 | ancestors(leaf) | ||
114 | .filter_map(N::cast) | ||
115 | .next() | ||
116 | } | ||
117 | |||
118 | fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator<Item=impl fmt::Display>) { | 108 | fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator<Item=impl fmt::Display>) { |
119 | buf.push_str(bra); | 109 | buf.push_str(bra); |
120 | let mut first = true; | 110 | let mut first = true; |