From 3ccd05fedc46796f793295901a8619492256468e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 18 Nov 2019 02:47:50 +0800 Subject: Add recursive expand in vscode --- crates/ra_ide_api/src/expand_macro.rs | 112 ++++++++++++++++++++++++++++++++++ crates/ra_ide_api/src/lib.rs | 5 ++ 2 files changed, 117 insertions(+) create mode 100644 crates/ra_ide_api/src/expand_macro.rs (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs new file mode 100644 index 000000000..48dc90932 --- /dev/null +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -0,0 +1,112 @@ +//! FIXME: write short doc here + +use crate::{db::RootDatabase, FilePosition}; +use ra_db::SourceDatabase; +use rustc_hash::FxHashMap; + +use hir::db::AstDatabase; +use ra_syntax::{ + algo::{find_node_at_offset, replace_descendants}, + ast::{self}, + AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, +}; + +fn insert_whitespaces(syn: SyntaxNode) -> String { + let mut res = String::new(); + + let mut token_iter = syn + .preorder_with_tokens() + .filter_map(|event| { + if let WalkEvent::Enter(NodeOrToken::Token(token)) = event { + Some(token) + } else { + None + } + }) + .peekable(); + + while let Some(token) = token_iter.next() { + res += &token.text().to_string(); + if token.kind().is_keyword() + || token.kind().is_literal() + || token.kind() == SyntaxKind::IDENT + { + if !token_iter.peek().map(|it| it.kind().is_punct()).unwrap_or(false) { + res += " "; + } + } + } + + res +} + +fn expand_macro_recur( + db: &RootDatabase, + source: hir::Source<&SyntaxNode>, + macro_call: &ast::MacroCall, +) -> Option { + let analyzer = hir::SourceAnalyzer::new(db, source, None); + let expansion = analyzer.expand(db, ¯o_call)?; + let expanded: SyntaxNode = db.parse_or_expand(expansion.file_id())?; + + let children = expanded.descendants().filter_map(ast::MacroCall::cast); + let mut replaces = FxHashMap::default(); + + for child in children.into_iter() { + let source = hir::Source::new(expansion.file_id(), source.ast); + let new_node = expand_macro_recur(db, source, &child)?; + + replaces.insert(child.syntax().clone().into(), new_node.into()); + } + + Some(replace_descendants(&expanded, &replaces)) +} + +pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<(String, String)> { + let parse = db.parse(position.file_id); + let file = parse.tree(); + let name_ref = find_node_at_offset::(file.syntax(), position.offset)?; + let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?; + + let source = hir::Source::new(position.file_id.into(), mac.syntax()); + + let expanded = expand_macro_recur(db, source, &mac)?; + + // FIXME: + // macro expansion may lose all white space information + // But we hope someday we can use ra_fmt for that + let res = insert_whitespaces(expanded); + Some((name_ref.text().to_string(), res)) +} + +#[cfg(test)] +mod tests { + use crate::mock_analysis::analysis_and_position; + + fn check_expand_macro(fixture: &str, expected: (&str, &str)) { + let (analysis, pos) = analysis_and_position(fixture); + + let result = analysis.expand_macro(pos).unwrap().unwrap(); + assert_eq!(result, (expected.0.to_string(), expected.1.to_string())); + } + + #[test] + fn macro_expand_recursive_expansion() { + check_expand_macro( + r#" + //- /lib.rs + macro_rules! bar { + () => { fn b() {} } + } + macro_rules! foo { + () => { bar!(); } + } + macro_rules! baz { + () => { foo!(); } + } + f<|>oo!(); + "#, + ("foo", "fn b(){}"), + ); + } +} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 110ddcd62..d1b73ef6f 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -42,6 +42,7 @@ mod display; mod inlay_hints; mod wasm_shims; mod expand; +mod expand_macro; #[cfg(test)] mod marks; @@ -296,6 +297,10 @@ impl Analysis { self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) } + pub fn expand_macro(&self, position: FilePosition) -> Cancelable> { + self.with_db(|db| expand_macro::expand_macro(db, position)) + } + /// Returns an edit to remove all newlines in the range, cleaning up minor /// stuff like trailing commas. pub fn join_lines(&self, frange: FileRange) -> Cancelable { -- cgit v1.2.3 From ae49a22b5cdd47add478fef6bb8ad3d75338e313 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 18 Nov 2019 03:35:46 +0800 Subject: Rebase --- crates/ra_ide_api/src/expand_macro.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 48dc90932..49c096ed5 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -1,10 +1,10 @@ //! FIXME: write short doc here use crate::{db::RootDatabase, FilePosition}; +use hir::db::AstDatabase; use ra_db::SourceDatabase; use rustc_hash::FxHashMap; -use hir::db::AstDatabase; use ra_syntax::{ algo::{find_node_at_offset, replace_descendants}, ast::{self}, @@ -47,13 +47,14 @@ fn expand_macro_recur( ) -> Option { let analyzer = hir::SourceAnalyzer::new(db, source, None); let expansion = analyzer.expand(db, ¯o_call)?; - let expanded: SyntaxNode = db.parse_or_expand(expansion.file_id())?; + let new_source = expansion.source(db); + let expanded: SyntaxNode = db.parse_or_expand(new_source.file_id)?; let children = expanded.descendants().filter_map(ast::MacroCall::cast); let mut replaces = FxHashMap::default(); for child in children.into_iter() { - let source = hir::Source::new(expansion.file_id(), source.ast); + let source = new_source.with_ast(source.ast); let new_node = expand_macro_recur(db, source, &child)?; replaces.insert(child.syntax().clone().into(), new_node.into()); -- cgit v1.2.3 From 94c63d280246971983cad4fa6ce2d333e3ba9f02 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 19 Nov 2019 22:56:28 +0800 Subject: Change to use Expansion::file_id and reordering --- crates/ra_ide_api/src/expand_macro.rs | 79 +++++++++++++++++------------------ 1 file changed, 39 insertions(+), 40 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 49c096ed5..bd557d455 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -11,33 +11,20 @@ use ra_syntax::{ AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, }; -fn insert_whitespaces(syn: SyntaxNode) -> String { - let mut res = String::new(); - - let mut token_iter = syn - .preorder_with_tokens() - .filter_map(|event| { - if let WalkEvent::Enter(NodeOrToken::Token(token)) = event { - Some(token) - } else { - None - } - }) - .peekable(); +pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<(String, String)> { + let parse = db.parse(position.file_id); + let file = parse.tree(); + let name_ref = find_node_at_offset::(file.syntax(), position.offset)?; + let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?; - while let Some(token) = token_iter.next() { - res += &token.text().to_string(); - if token.kind().is_keyword() - || token.kind().is_literal() - || token.kind() == SyntaxKind::IDENT - { - if !token_iter.peek().map(|it| it.kind().is_punct()).unwrap_or(false) { - res += " "; - } - } - } + let source = hir::Source::new(position.file_id.into(), mac.syntax()); + let expanded = expand_macro_recur(db, source, &mac)?; - res + // FIXME: + // macro expansion may lose all white space information + // But we hope someday we can use ra_fmt for that + let res = insert_whitespaces(expanded); + Some((name_ref.text().to_string(), res)) } fn expand_macro_recur( @@ -47,14 +34,14 @@ fn expand_macro_recur( ) -> Option { let analyzer = hir::SourceAnalyzer::new(db, source, None); let expansion = analyzer.expand(db, ¯o_call)?; - let new_source = expansion.source(db); - let expanded: SyntaxNode = db.parse_or_expand(new_source.file_id)?; + let macro_file_id = expansion.file_id(); + let expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?; let children = expanded.descendants().filter_map(ast::MacroCall::cast); let mut replaces = FxHashMap::default(); for child in children.into_iter() { - let source = new_source.with_ast(source.ast); + let source = hir::Source::new(macro_file_id, source.ast); let new_node = expand_macro_recur(db, source, &child)?; replaces.insert(child.syntax().clone().into(), new_node.into()); @@ -63,21 +50,33 @@ fn expand_macro_recur( Some(replace_descendants(&expanded, &replaces)) } -pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<(String, String)> { - let parse = db.parse(position.file_id); - let file = parse.tree(); - let name_ref = find_node_at_offset::(file.syntax(), position.offset)?; - let mac = name_ref.syntax().ancestors().find_map(ast::MacroCall::cast)?; +fn insert_whitespaces(syn: SyntaxNode) -> String { + let mut res = String::new(); - let source = hir::Source::new(position.file_id.into(), mac.syntax()); + let mut token_iter = syn + .preorder_with_tokens() + .filter_map(|event| { + if let WalkEvent::Enter(NodeOrToken::Token(token)) = event { + Some(token) + } else { + None + } + }) + .peekable(); - let expanded = expand_macro_recur(db, source, &mac)?; + while let Some(token) = token_iter.next() { + res += &token.text().to_string(); + if token.kind().is_keyword() + || token.kind().is_literal() + || token.kind() == SyntaxKind::IDENT + { + if !token_iter.peek().map(|it| it.kind().is_punct()).unwrap_or(false) { + res += " "; + } + } + } - // FIXME: - // macro expansion may lose all white space information - // But we hope someday we can use ra_fmt for that - let res = insert_whitespaces(expanded); - Some((name_ref.text().to_string(), res)) + res } #[cfg(test)] -- cgit v1.2.3 From 4012da07fd22223660a21c65d54d10a9a632eda0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 19 Nov 2019 22:56:48 +0800 Subject: Change return type of expand_macro --- crates/ra_ide_api/src/expand_macro.rs | 14 ++++++++++---- crates/ra_ide_api/src/lib.rs | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index bd557d455..44e77ba50 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -11,7 +11,12 @@ use ra_syntax::{ AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, }; -pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<(String, String)> { +pub struct ExpandedMacro { + pub name: String, + pub expansion: String, +} + +pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option { let parse = db.parse(position.file_id); let file = parse.tree(); let name_ref = find_node_at_offset::(file.syntax(), position.offset)?; @@ -23,8 +28,8 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< // FIXME: // macro expansion may lose all white space information // But we hope someday we can use ra_fmt for that - let res = insert_whitespaces(expanded); - Some((name_ref.text().to_string(), res)) + let expansion = insert_whitespaces(expanded); + Some(ExpandedMacro { name: name_ref.text().to_string(), expansion }) } fn expand_macro_recur( @@ -87,7 +92,8 @@ mod tests { let (analysis, pos) = analysis_and_position(fixture); let result = analysis.expand_macro(pos).unwrap().unwrap(); - assert_eq!(result, (expected.0.to_string(), expected.1.to_string())); + assert_eq!(result.name, expected.0.to_string()); + assert_eq!(result.expansion, expected.1.to_string()); } #[test] diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index d1b73ef6f..57ed97147 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -66,6 +66,7 @@ pub use crate::{ completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, diagnostics::Severity, display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, + expand_macro::ExpandedMacro, feature_flags::FeatureFlags, folding_ranges::{Fold, FoldKind}, hover::HoverResult, @@ -297,7 +298,7 @@ impl Analysis { self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) } - pub fn expand_macro(&self, position: FilePosition) -> Cancelable> { + pub fn expand_macro(&self, position: FilePosition) -> Cancelable> { self.with_db(|db| expand_macro::expand_macro(db, position)) } -- cgit v1.2.3 From 80fe467ce864de077d2f5a4e29e1a4d5fe535dc3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 20 Nov 2019 00:12:48 +0800 Subject: Improve insert_whitespaces --- crates/ra_ide_api/src/expand_macro.rs | 94 ++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 44e77ba50..07a7c738a 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; use ra_syntax::{ algo::{find_node_at_offset, replace_descendants}, ast::{self}, - AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, + AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, T, }; pub struct ExpandedMacro { @@ -55,9 +55,12 @@ fn expand_macro_recur( Some(replace_descendants(&expanded, &replaces)) } +// FIXME: It would also be cool to share logic here and in the mbe tests, +// which are pretty unreadable at the moment. fn insert_whitespaces(syn: SyntaxNode) -> String { - let mut res = String::new(); + use SyntaxKind::*; + let mut res = String::new(); let mut token_iter = syn .preorder_with_tokens() .filter_map(|event| { @@ -69,16 +72,44 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { }) .peekable(); + let mut indent = 0; + let mut last: Option = None; + while let Some(token) = token_iter.next() { - res += &token.text().to_string(); - if token.kind().is_keyword() - || token.kind().is_literal() - || token.kind() == SyntaxKind::IDENT - { - if !token_iter.peek().map(|it| it.kind().is_punct()).unwrap_or(false) { - res += " "; + let mut is_next = |f: fn(SyntaxKind) -> bool, default| -> bool { + token_iter.peek().map(|it| f(it.kind())).unwrap_or(default) + }; + let is_last = |f: fn(SyntaxKind) -> bool, default| -> bool { + last.map(|it| f(it)).unwrap_or(default) + }; + + res += &match token.kind() { + k @ _ + if (k.is_keyword() || k.is_literal() || k == IDENT) + && is_next(|it| !it.is_punct(), true) => + { + token.text().to_string() + " " } - } + L_CURLY if is_next(|it| it != R_CURLY, true) => { + indent += 1; + format!(" {{\n{}", " ".repeat(indent)) + } + R_CURLY if is_last(|it| it != L_CURLY, true) => { + indent = indent.checked_sub(1).unwrap_or(0); + format!("\n}}{}", " ".repeat(indent)) + } + R_CURLY => { + indent = indent.checked_sub(1).unwrap_or(0); + format!("}}\n{}", " ".repeat(indent)) + } + T![;] => format!(";\n{}", " ".repeat(indent)), + T![->] => " -> ".to_string(), + T![=] => " = ".to_string(), + T![=>] => " => ".to_string(), + _ => token.text().to_string(), + }; + + last = Some(token.kind()); } res @@ -86,19 +117,18 @@ fn insert_whitespaces(syn: SyntaxNode) -> String { #[cfg(test)] mod tests { + use super::*; use crate::mock_analysis::analysis_and_position; + use insta::assert_snapshot; - fn check_expand_macro(fixture: &str, expected: (&str, &str)) { + fn check_expand_macro(fixture: &str) -> ExpandedMacro { let (analysis, pos) = analysis_and_position(fixture); - - let result = analysis.expand_macro(pos).unwrap().unwrap(); - assert_eq!(result.name, expected.0.to_string()); - assert_eq!(result.expansion, expected.1.to_string()); + analysis.expand_macro(pos).unwrap().unwrap() } #[test] fn macro_expand_recursive_expansion() { - check_expand_macro( + let res = check_expand_macro( r#" //- /lib.rs macro_rules! bar { @@ -112,7 +142,37 @@ mod tests { } f<|>oo!(); "#, - ("foo", "fn b(){}"), ); + + assert_eq!(res.name, "foo"); + assert_snapshot!(res.expansion, @r###" +fn b(){} +"###); + } + + #[test] + fn macro_expand_multiple_lines() { + let res = check_expand_macro( + r#" + //- /lib.rs + macro_rules! foo { + () => { + fn some_thing() -> u32 { + let a = 0; + a + 10 + } + } + } + f<|>oo!(); + "#, + ); + + assert_eq!(res.name, "foo"); + assert_snapshot!(res.expansion, @r###" +fn some_thing() -> u32 { + let a = 0; + a+10 +} +"###); } } -- cgit v1.2.3 From e29017139708174e922af7af614b1eb390952ecf Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 20 Nov 2019 01:12:56 +0800 Subject: Add shot doc for expand_macro module --- crates/ra_ide_api/src/expand_macro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 07a7c738a..e9eb2a7fb 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -1,4 +1,4 @@ -//! FIXME: write short doc here +//! This modules implements "expand macro" functionality in the IDE use crate::{db::RootDatabase, FilePosition}; use hir::db::AstDatabase; -- cgit v1.2.3