aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/expand_macro.rs14
-rw-r--r--crates/ra_ide_api/src/lib.rs3
-rw-r--r--crates/ra_lsp_server/src/main_loop/handlers.rs7
-rw-r--r--crates/ra_lsp_server/src/req.rs9
4 files changed, 25 insertions, 8 deletions
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::{
11 AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, 11 AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent,
12}; 12};
13 13
14pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<(String, String)> { 14pub struct ExpandedMacro {
15 pub name: String,
16 pub expansion: String,
17}
18
19pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
15 let parse = db.parse(position.file_id); 20 let parse = db.parse(position.file_id);
16 let file = parse.tree(); 21 let file = parse.tree();
17 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,8 +28,8 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
23 // FIXME: 28 // FIXME:
24 // macro expansion may lose all white space information 29 // macro expansion may lose all white space information
25 // But we hope someday we can use ra_fmt for that 30 // But we hope someday we can use ra_fmt for that
26 let res = insert_whitespaces(expanded); 31 let expansion = insert_whitespaces(expanded);
27 Some((name_ref.text().to_string(), res)) 32 Some(ExpandedMacro { name: name_ref.text().to_string(), expansion })
28} 33}
29 34
30fn expand_macro_recur( 35fn expand_macro_recur(
@@ -87,7 +92,8 @@ mod tests {
87 let (analysis, pos) = analysis_and_position(fixture); 92 let (analysis, pos) = analysis_and_position(fixture);
88 93
89 let result = analysis.expand_macro(pos).unwrap().unwrap(); 94 let result = analysis.expand_macro(pos).unwrap().unwrap();
90 assert_eq!(result, (expected.0.to_string(), expected.1.to_string())); 95 assert_eq!(result.name, expected.0.to_string());
96 assert_eq!(result.expansion, expected.1.to_string());
91 } 97 }
92 98
93 #[test] 99 #[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::{
66 completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, 66 completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
67 diagnostics::Severity, 67 diagnostics::Severity,
68 display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, 68 display::{file_structure, FunctionSignature, NavigationTarget, StructureNode},
69 expand_macro::ExpandedMacro,
69 feature_flags::FeatureFlags, 70 feature_flags::FeatureFlags,
70 folding_ranges::{Fold, FoldKind}, 71 folding_ranges::{Fold, FoldKind},
71 hover::HoverResult, 72 hover::HoverResult,
@@ -297,7 +298,7 @@ impl Analysis {
297 self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) 298 self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
298 } 299 }
299 300
300 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<(String, String)>> { 301 pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
301 self.with_db(|db| expand_macro::expand_macro(db, position)) 302 self.with_db(|db| expand_macro::expand_macro(db, position))
302 } 303 }
303 304
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs
index 783b0a827..0461bf385 100644
--- a/crates/ra_lsp_server/src/main_loop/handlers.rs
+++ b/crates/ra_lsp_server/src/main_loop/handlers.rs
@@ -50,7 +50,7 @@ pub fn handle_syntax_tree(world: WorldSnapshot, params: req::SyntaxTreeParams) -
50pub fn handle_expand_macro( 50pub fn handle_expand_macro(
51 world: WorldSnapshot, 51 world: WorldSnapshot,
52 params: req::ExpandMacroParams, 52 params: req::ExpandMacroParams,
53) -> Result<Option<(String, String)>> { 53) -> Result<Option<req::ExpandedMacro>> {
54 let _p = profile("handle_expand_macro"); 54 let _p = profile("handle_expand_macro");
55 let file_id = params.text_document.try_conv_with(&world)?; 55 let file_id = params.text_document.try_conv_with(&world)?;
56 let line_index = world.analysis().file_line_index(file_id)?; 56 let line_index = world.analysis().file_line_index(file_id)?;
@@ -58,7 +58,10 @@ pub fn handle_expand_macro(
58 58
59 match offset { 59 match offset {
60 None => Ok(None), 60 None => Ok(None),
61 Some(offset) => Ok(world.analysis().expand_macro(FilePosition { file_id, offset })?), 61 Some(offset) => {
62 let res = world.analysis().expand_macro(FilePosition { file_id, offset })?;
63 Ok(res.map(|it| req::ExpandedMacro { name: it.name, expansion: it.expansion }))
64 }
62 } 65 }
63} 66}
64 67
diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs
index dbc0e9624..39361b7e8 100644
--- a/crates/ra_lsp_server/src/req.rs
+++ b/crates/ra_lsp_server/src/req.rs
@@ -45,11 +45,18 @@ pub struct SyntaxTreeParams {
45 pub range: Option<Range>, 45 pub range: Option<Range>,
46} 46}
47 47
48#[derive(Serialize, Debug)]
49#[serde(rename_all = "camelCase")]
50pub struct ExpandedMacro {
51 pub name: String,
52 pub expansion: String,
53}
54
48pub enum ExpandMacro {} 55pub enum ExpandMacro {}
49 56
50impl Request for ExpandMacro { 57impl Request for ExpandMacro {
51 type Params = ExpandMacroParams; 58 type Params = ExpandMacroParams;
52 type Result = Option<(String, String)>; 59 type Result = Option<ExpandedMacro>;
53 const METHOD: &'static str = "rust-analyzer/expandMacro"; 60 const METHOD: &'static str = "rust-analyzer/expandMacro";
54} 61}
55 62