aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/find_path.rs12
-rw-r--r--crates/ra_hir_def/src/path.rs11
-rw-r--r--crates/ra_hir_def/src/path/lower/lower_use.rs8
-rw-r--r--crates/ra_syntax/src/ast/edit.rs39
-rw-r--r--crates/ra_syntax/src/ast/make.rs26
5 files changed, 48 insertions, 48 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index f7dc8acb7..8cc2fb160 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -35,7 +35,7 @@ fn find_path_inner(
35 let def_map = db.crate_def_map(from.krate); 35 let def_map = db.crate_def_map(from.krate);
36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; 36 let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
37 if let Some((name, _)) = from_scope.name_of(item) { 37 if let Some((name, _)) = from_scope.name_of(item) {
38 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 38 return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
39 } 39 }
40 40
41 // - if the item is the crate root, return `crate` 41 // - if the item is the crate root, return `crate`
@@ -45,12 +45,12 @@ fn find_path_inner(
45 local_id: def_map.root, 45 local_id: def_map.root,
46 })) 46 }))
47 { 47 {
48 return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); 48 return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
49 } 49 }
50 50
51 // - if the item is the module we're in, use `self` 51 // - if the item is the module we're in, use `self`
52 if item == ItemInNs::Types(from.into()) { 52 if item == ItemInNs::Types(from.into()) {
53 return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new())); 53 return Some(ModPath::from_segments(PathKind::Super(0), Vec::new()));
54 } 54 }
55 55
56 // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) 56 // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
@@ -61,14 +61,14 @@ fn find_path_inner(
61 local_id: parent_id, 61 local_id: parent_id,
62 })) 62 }))
63 { 63 {
64 return Some(ModPath::from_simple_segments(PathKind::Super(1), Vec::new())); 64 return Some(ModPath::from_segments(PathKind::Super(1), Vec::new()));
65 } 65 }
66 } 66 }
67 67
68 // - if the item is the crate root of a dependency crate, return the name from the extern prelude 68 // - if the item is the crate root of a dependency crate, return the name from the extern prelude
69 for (name, def_id) in &def_map.extern_prelude { 69 for (name, def_id) in &def_map.extern_prelude {
70 if item == ItemInNs::Types(*def_id) { 70 if item == ItemInNs::Types(*def_id) {
71 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 71 return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
72 } 72 }
73 } 73 }
74 74
@@ -79,7 +79,7 @@ fn find_path_inner(
79 &prelude_def_map.modules[prelude_module.local_id].scope; 79 &prelude_def_map.modules[prelude_module.local_id].scope;
80 if let Some((name, vis)) = prelude_scope.name_of(item) { 80 if let Some((name, vis)) = prelude_scope.name_of(item) {
81 if vis.is_visible_from(db, from) { 81 if vis.is_visible_from(db, from) {
82 return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); 82 return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
83 } 83 }
84 } 84 }
85 } 85 }
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 9f93a5424..ab290e2c9 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -39,10 +39,7 @@ impl ModPath {
39 lower::lower_path(path, hygiene).map(|it| it.mod_path) 39 lower::lower_path(path, hygiene).map(|it| it.mod_path)
40 } 40 }
41 41
42 pub fn from_simple_segments( 42 pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
43 kind: PathKind,
44 segments: impl IntoIterator<Item = Name>,
45 ) -> ModPath {
46 let segments = segments.into_iter().collect::<Vec<_>>(); 43 let segments = segments.into_iter().collect::<Vec<_>>();
47 ModPath { kind, segments } 44 ModPath { kind, segments }
48 } 45 }
@@ -240,7 +237,7 @@ impl From<Name> for Path {
240 fn from(name: Name) -> Path { 237 fn from(name: Name) -> Path {
241 Path { 238 Path {
242 type_anchor: None, 239 type_anchor: None,
243 mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), 240 mod_path: ModPath::from_segments(PathKind::Plain, iter::once(name)),
244 generic_args: vec![None], 241 generic_args: vec![None],
245 } 242 }
246 } 243 }
@@ -248,7 +245,7 @@ impl From<Name> for Path {
248 245
249impl From<Name> for ModPath { 246impl From<Name> for ModPath {
250 fn from(name: Name) -> ModPath { 247 fn from(name: Name) -> ModPath {
251 ModPath::from_simple_segments(PathKind::Plain, iter::once(name)) 248 ModPath::from_segments(PathKind::Plain, iter::once(name))
252 } 249 }
253} 250}
254 251
@@ -311,7 +308,7 @@ macro_rules! __known_path {
311macro_rules! __path { 308macro_rules! __path {
312 ($start:ident $(:: $seg:ident)*) => ({ 309 ($start:ident $(:: $seg:ident)*) => ({
313 $crate::__known_path!($start $(:: $seg)*); 310 $crate::__known_path!($start $(:: $seg)*);
314 $crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec![ 311 $crate::path::ModPath::from_segments($crate::path::PathKind::Abs, vec![
315 $crate::path::__name![$start], $($crate::path::__name![$seg],)* 312 $crate::path::__name![$start], $($crate::path::__name![$seg],)*
316 ]) 313 ])
317 }); 314 });
diff --git a/crates/ra_hir_def/src/path/lower/lower_use.rs b/crates/ra_hir_def/src/path/lower/lower_use.rs
index 3218eaf0a..531878174 100644
--- a/crates/ra_hir_def/src/path/lower/lower_use.rs
+++ b/crates/ra_hir_def/src/path/lower/lower_use.rs
@@ -84,7 +84,7 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
84 res 84 res
85 } 85 }
86 Either::Right(crate_id) => { 86 Either::Right(crate_id) => {
87 return Some(ModPath::from_simple_segments( 87 return Some(ModPath::from_segments(
88 PathKind::DollarCrate(crate_id), 88 PathKind::DollarCrate(crate_id),
89 iter::empty(), 89 iter::empty(),
90 )) 90 ))
@@ -95,19 +95,19 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
95 if prefix.is_some() { 95 if prefix.is_some() {
96 return None; 96 return None;
97 } 97 }
98 ModPath::from_simple_segments(PathKind::Crate, iter::empty()) 98 ModPath::from_segments(PathKind::Crate, iter::empty())
99 } 99 }
100 ast::PathSegmentKind::SelfKw => { 100 ast::PathSegmentKind::SelfKw => {
101 if prefix.is_some() { 101 if prefix.is_some() {
102 return None; 102 return None;
103 } 103 }
104 ModPath::from_simple_segments(PathKind::Super(0), iter::empty()) 104 ModPath::from_segments(PathKind::Super(0), iter::empty())
105 } 105 }
106 ast::PathSegmentKind::SuperKw => { 106 ast::PathSegmentKind::SuperKw => {
107 if prefix.is_some() { 107 if prefix.is_some() {
108 return None; 108 return None;
109 } 109 }
110 ModPath::from_simple_segments(PathKind::Super(1), iter::empty()) 110 ModPath::from_segments(PathKind::Super(1), iter::empty())
111 } 111 }
112 ast::PathSegmentKind::Type { .. } => { 112 ast::PathSegmentKind::Type { .. } => {
113 // not allowed in imports 113 // not allowed in imports
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index b736098ac..d88a0cf4b 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -22,9 +22,8 @@ impl ast::BinExpr {
22 #[must_use] 22 #[must_use]
23 pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> { 23 pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
24 let op_node: SyntaxElement = self.op_details()?.0.into(); 24 let op_node: SyntaxElement = self.op_details()?.0.into();
25 let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into()); 25 let to_insert: Option<SyntaxElement> = Some(make::token(op).into());
26 let replace_range = RangeInclusive::new(op_node.clone(), op_node); 26 Some(replace_children(self, single_node(op_node), to_insert))
27 Some(replace_children(self, replace_range, to_insert.into_iter()))
28 } 27 }
29} 28}
30 29
@@ -40,11 +39,10 @@ impl ast::FnDef {
40 } else { 39 } else {
41 to_insert.push(make::tokens::single_space().into()); 40 to_insert.push(make::tokens::single_space().into());
42 to_insert.push(body.syntax().clone().into()); 41 to_insert.push(body.syntax().clone().into());
43 return insert_children(self, InsertPosition::Last, to_insert.into_iter()); 42 return insert_children(self, InsertPosition::Last, to_insert);
44 }; 43 };
45 to_insert.push(body.syntax().clone().into()); 44 to_insert.push(body.syntax().clone().into());
46 let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi); 45 replace_children(self, single_node(old_body_or_semi), to_insert)
47 replace_children(self, replace_range, to_insert.into_iter())
48 } 46 }
49} 47}
50 48
@@ -77,7 +75,7 @@ impl ast::ItemList {
77 let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); 75 let ws = tokens::WsBuilder::new(&format!("\n{}", indent));
78 let to_insert: ArrayVec<[SyntaxElement; 2]> = 76 let to_insert: ArrayVec<[SyntaxElement; 2]> =
79 [ws.ws().into(), item.syntax().clone().into()].into(); 77 [ws.ws().into(), item.syntax().clone().into()].into();
80 insert_children(self, position, to_insert.into_iter()) 78 insert_children(self, position, to_insert)
81 } 79 }
82 80
83 fn l_curly(&self) -> Option<SyntaxElement> { 81 fn l_curly(&self) -> Option<SyntaxElement> {
@@ -109,9 +107,7 @@ impl ast::ItemList {
109 let to_insert = iter::once(ws.ws().into()); 107 let to_insert = iter::once(ws.ws().into());
110 match existing_ws { 108 match existing_ws {
111 None => insert_children(self, InsertPosition::After(l_curly), to_insert), 109 None => insert_children(self, InsertPosition::After(l_curly), to_insert),
112 Some(ws) => { 110 Some(ws) => replace_children(self, single_node(ws), to_insert),
113 replace_children(self, RangeInclusive::new(ws.clone().into(), ws.into()), to_insert)
114 }
115 } 111 }
116 } 112 }
117} 113}
@@ -188,7 +184,7 @@ impl ast::RecordFieldList {
188 InsertPosition::After(anchor) => after_field!(anchor), 184 InsertPosition::After(anchor) => after_field!(anchor),
189 }; 185 };
190 186
191 insert_children(self, position, to_insert.iter().cloned()) 187 insert_children(self, position, to_insert)
192 } 188 }
193 189
194 fn l_curly(&self) -> Option<SyntaxElement> { 190 fn l_curly(&self) -> Option<SyntaxElement> {
@@ -207,7 +203,7 @@ impl ast::TypeParam {
207 Some(it) => it.syntax().clone().into(), 203 Some(it) => it.syntax().clone().into(),
208 None => colon.clone().into(), 204 None => colon.clone().into(),
209 }; 205 };
210 replace_children(self, RangeInclusive::new(colon.into(), end), iter::empty()) 206 replace_children(self, colon.into()..=end, iter::empty())
211 } 207 }
212} 208}
213 209
@@ -224,7 +220,7 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
224 Some(el) if el.kind() == WHITESPACE => el.clone(), 220 Some(el) if el.kind() == WHITESPACE => el.clone(),
225 Some(_) | None => start.clone(), 221 Some(_) | None => start.clone(),
226 }; 222 };
227 node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty()); 223 node = algo::replace_children(&node, start..=end, &mut iter::empty());
228 } 224 }
229 node 225 node
230} 226}
@@ -232,9 +228,10 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
232#[must_use] 228#[must_use]
233pub fn replace_descendants<N: AstNode, D: AstNode>( 229pub fn replace_descendants<N: AstNode, D: AstNode>(
234 parent: &N, 230 parent: &N,
235 replacement_map: impl Iterator<Item = (D, D)>, 231 replacement_map: impl IntoIterator<Item = (D, D)>,
236) -> N { 232) -> N {
237 let map = replacement_map 233 let map = replacement_map
234 .into_iter()
238 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into())) 235 .map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
239 .collect::<FxHashMap<SyntaxElement, _>>(); 236 .collect::<FxHashMap<SyntaxElement, _>>();
240 let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned()); 237 let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned());
@@ -348,19 +345,25 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
348fn insert_children<N: AstNode>( 345fn insert_children<N: AstNode>(
349 parent: &N, 346 parent: &N,
350 position: InsertPosition<SyntaxElement>, 347 position: InsertPosition<SyntaxElement>,
351 mut to_insert: impl Iterator<Item = SyntaxElement>, 348 to_insert: impl IntoIterator<Item = SyntaxElement>,
352) -> N { 349) -> N {
353 let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert); 350 let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter());
354 N::cast(new_syntax).unwrap() 351 N::cast(new_syntax).unwrap()
355} 352}
356 353
354fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> {
355 let element = element.into();
356 element.clone()..=element
357}
358
357#[must_use] 359#[must_use]
358fn replace_children<N: AstNode>( 360fn replace_children<N: AstNode>(
359 parent: &N, 361 parent: &N,
360 to_replace: RangeInclusive<SyntaxElement>, 362 to_replace: RangeInclusive<SyntaxElement>,
361 mut to_insert: impl Iterator<Item = SyntaxElement>, 363 to_insert: impl IntoIterator<Item = SyntaxElement>,
362) -> N { 364) -> N {
363 let new_syntax = algo::replace_children(parent.syntax(), to_replace, &mut to_insert); 365 let new_syntax =
366 algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter());
364 N::cast(new_syntax).unwrap() 367 N::cast(new_syntax).unwrap()
365} 368}
366 369
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index eef45090d..4a79d0dec 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -2,7 +2,7 @@
2//! of smaller pieces. 2//! of smaller pieces.
3use itertools::Itertools; 3use itertools::Itertools;
4 4
5use crate::{algo, ast, AstNode, SourceFile}; 5use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
6 6
7pub fn name(text: &str) -> ast::Name { 7pub fn name(text: &str) -> ast::Name {
8 ast_from_text(&format!("mod {};", text)) 8 ast_from_text(&format!("mod {};", text))
@@ -181,28 +181,28 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
181 ast_from_text(&format!("fn f() {{ {} }}", text)) 181 ast_from_text(&format!("fn f() {{ {} }}", text))
182} 182}
183 183
184pub fn token(kind: SyntaxKind) -> SyntaxToken {
185 tokens::SOURCE_FILE
186 .tree()
187 .syntax()
188 .descendants_with_tokens()
189 .filter_map(|it| it.into_token())
190 .find(|it| it.kind() == kind)
191 .unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
192}
193
184fn ast_from_text<N: AstNode>(text: &str) -> N { 194fn ast_from_text<N: AstNode>(text: &str) -> N {
185 let parse = SourceFile::parse(text); 195 let parse = SourceFile::parse(text);
186 parse.tree().syntax().descendants().find_map(N::cast).unwrap() 196 parse.tree().syntax().descendants().find_map(N::cast).unwrap()
187} 197}
188 198
189pub mod tokens { 199pub mod tokens {
190 use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T}; 200 use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
191 use once_cell::sync::Lazy; 201 use once_cell::sync::Lazy;
192 202
193 static SOURCE_FILE: Lazy<Parse<SourceFile>> = 203 pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
194 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;")); 204 Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
195 205
196 pub fn op(op: SyntaxKind) -> SyntaxToken {
197 SOURCE_FILE
198 .tree()
199 .syntax()
200 .descendants_with_tokens()
201 .filter_map(|it| it.into_token())
202 .find(|it| it.kind() == op)
203 .unwrap()
204 }
205
206 pub fn comma() -> SyntaxToken { 206 pub fn comma() -> SyntaxToken {
207 SOURCE_FILE 207 SOURCE_FILE
208 .tree() 208 .tree()