aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/expand_macro.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
committerSeivan Heidari <[email protected]>2019-12-23 14:35:31 +0000
commitb21d9337d9200e2cfdc90b386591c72c302dc03e (patch)
treef81f5c08f821115cee26fa4d3ceaae88c7807fd5 /crates/ra_ide/src/expand_macro.rs
parent18a0937585b836ec5ed054b9ae48e0156ab6d9ef (diff)
parentce07a2daa9e53aa86a769f8641b14c2878444fbc (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_ide/src/expand_macro.rs')
-rw-r--r--crates/ra_ide/src/expand_macro.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/crates/ra_ide/src/expand_macro.rs b/crates/ra_ide/src/expand_macro.rs
index abc602244..bdbc31704 100644
--- a/crates/ra_ide/src/expand_macro.rs
+++ b/crates/ra_ide/src/expand_macro.rs
@@ -22,7 +22,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
22 let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)?; 22 let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)?;
23 let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?; 23 let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?;
24 24
25 let source = hir::Source::new(position.file_id.into(), mac.syntax()); 25 let source = hir::InFile::new(position.file_id.into(), mac.syntax());
26 let expanded = expand_macro_recur(db, source, source.with_value(&mac))?; 26 let expanded = expand_macro_recur(db, source, source.with_value(&mac))?;
27 27
28 // FIXME: 28 // FIXME:
@@ -34,8 +34,8 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
34 34
35fn expand_macro_recur( 35fn expand_macro_recur(
36 db: &RootDatabase, 36 db: &RootDatabase,
37 source: hir::Source<&SyntaxNode>, 37 source: hir::InFile<&SyntaxNode>,
38 macro_call: hir::Source<&ast::MacroCall>, 38 macro_call: hir::InFile<&ast::MacroCall>,
39) -> Option<SyntaxNode> { 39) -> Option<SyntaxNode> {
40 let analyzer = hir::SourceAnalyzer::new(db, source, None); 40 let analyzer = hir::SourceAnalyzer::new(db, source, None);
41 let expansion = analyzer.expand(db, macro_call)?; 41 let expansion = analyzer.expand(db, macro_call)?;
@@ -46,7 +46,7 @@ fn expand_macro_recur(
46 let mut replaces = FxHashMap::default(); 46 let mut replaces = FxHashMap::default();
47 47
48 for child in children.into_iter() { 48 for child in children.into_iter() {
49 let node = hir::Source::new(macro_file_id, &child); 49 let node = hir::InFile::new(macro_file_id, &child);
50 if let Some(new_node) = expand_macro_recur(db, source, node) { 50 if let Some(new_node) = expand_macro_recur(db, source, node) {
51 // Replace the whole node if it is root 51 // Replace the whole node if it is root
52 // `replace_descendants` will not replace the parent node 52 // `replace_descendants` will not replace the parent node
@@ -86,21 +86,18 @@ fn insert_whitespaces(syn: SyntaxNode) -> String {
86 let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool { 86 let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool {
87 token_iter.peek().map(|it| f(it.kind())).unwrap_or(default) 87 token_iter.peek().map(|it| f(it.kind())).unwrap_or(default)
88 }; 88 };
89 let is_last = |f: fn(SyntaxKind) -> bool, default| -> bool { 89 let is_last =
90 last.map(|it| f(it)).unwrap_or(default) 90 |f: fn(SyntaxKind) -> bool, default| -> bool { last.map(f).unwrap_or(default) };
91 };
92 91
93 res += &match token.kind() { 92 res += &match token.kind() {
94 k @ _ if is_text(k) && is_next(|it| !it.is_punct(), true) => { 93 k if is_text(k) && is_next(|it| !it.is_punct(), true) => token.text().to_string() + " ",
95 token.text().to_string() + " "
96 }
97 L_CURLY if is_next(|it| it != R_CURLY, true) => { 94 L_CURLY if is_next(|it| it != R_CURLY, true) => {
98 indent += 1; 95 indent += 1;
99 let leading_space = if is_last(|it| is_text(it), false) { " " } else { "" }; 96 let leading_space = if is_last(is_text, false) { " " } else { "" };
100 format!("{}{{\n{}", leading_space, " ".repeat(indent)) 97 format!("{}{{\n{}", leading_space, " ".repeat(indent))
101 } 98 }
102 R_CURLY if is_last(|it| it != L_CURLY, true) => { 99 R_CURLY if is_last(|it| it != L_CURLY, true) => {
103 indent = indent.checked_sub(1).unwrap_or(0); 100 indent = indent.saturating_sub(1);
104 format!("\n{}}}", " ".repeat(indent)) 101 format!("\n{}}}", " ".repeat(indent))
105 } 102 }
106 R_CURLY => format!("}}\n{}", " ".repeat(indent)), 103 R_CURLY => format!("}}\n{}", " ".repeat(indent)),