From 4012da07fd22223660a21c65d54d10a9a632eda0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 19 Nov 2019 22:56:48 +0800 Subject: [PATCH] Change return type of expand_macro --- crates/ra_ide_api/src/expand_macro.rs | 14 ++++++++++---- crates/ra_ide_api/src/lib.rs | 3 ++- crates/ra_lsp_server/src/main_loop/handlers.rs | 7 +++++-- crates/ra_lsp_server/src/req.rs | 9 ++++++++- editors/code/src/commands/expand_macro.ts | 13 ++++++++----- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index bd557d45505..44e77ba5028 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -11,7 +11,12 @@ 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 @@ 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())); + 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 d1b73ef6fa1..57ed9714706 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -66,6 +66,7 @@ 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 @@ pub fn syntax_tree( 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)) } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 783b0a82733..0461bf38542 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) - pub fn handle_expand_macro( world: WorldSnapshot, params: req::ExpandMacroParams, -) -> Result> { +) -> Result> { let _p = profile("handle_expand_macro"); let file_id = params.text_document.try_conv_with(&world)?; let line_index = world.analysis().file_line_index(file_id)?; @@ -58,7 +58,10 @@ pub fn handle_expand_macro( match offset { None => Ok(None), - Some(offset) => Ok(world.analysis().expand_macro(FilePosition { file_id, offset })?), + Some(offset) => { + let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; + Ok(res.map(|it| req::ExpandedMacro { name: it.name, expansion: it.expansion })) + } } } diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index dbc0e9624cb..39361b7e8fd 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -45,11 +45,18 @@ pub struct SyntaxTreeParams { pub range: Option, } +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct ExpandedMacro { + pub name: String, + pub expansion: String, +} + pub enum ExpandMacro {} impl Request for ExpandMacro { type Params = ExpandMacroParams; - type Result = Option<(String, String)>; + type Result = Option; const METHOD: &'static str = "rust-analyzer/expandMacro"; } diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index d024a70e0ee..3fc3e039172 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -2,13 +2,16 @@ import * as vscode from 'vscode'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; -type ExpandMacroResult = [string, string]; +interface ExpandedMacro { + name: string, + expansion: string, +} -function code_format([name, text]: [string, string]): vscode.MarkdownString { +function code_format(expanded: ExpandedMacro): vscode.MarkdownString { const markdown = new vscode.MarkdownString( - `#### Recursive expansion of ${name}! macro` + `#### Recursive expansion of ${expanded.name}! macro` ); - markdown.appendCodeblock(text, 'rust'); + markdown.appendCodeblock(expanded.expansion, 'rust'); return markdown; } @@ -23,7 +26,7 @@ export class ExpandMacroHoverProvider implements vscode.HoverProvider { textDocument: { uri: document.uri.toString() }, position }; - const result = await Server.client.sendRequest( + const result = await Server.client.sendRequest( 'rust-analyzer/expandMacro', request ); -- 2.44.0