Change return type of expand_macro

This commit is contained in:
Edwin Cheng 2019-11-19 22:56:48 +08:00
parent 94c63d2802
commit 4012da07fd
5 changed files with 33 additions and 13 deletions

View file

@ -11,7 +11,12 @@ use ra_syntax::{
AstNode, NodeOrToken, SyntaxKind, SyntaxNode, WalkEvent, 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<ExpandedMacro> {
let parse = db.parse(position.file_id); let parse = db.parse(position.file_id);
let file = parse.tree(); let file = parse.tree();
let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)?; 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<
// FIXME: // FIXME:
// macro expansion may lose all white space information // macro expansion may lose all white space information
// But we hope someday we can use ra_fmt for that // But we hope someday we can use ra_fmt for that
let res = insert_whitespaces(expanded); let expansion = insert_whitespaces(expanded);
Some((name_ref.text().to_string(), res)) Some(ExpandedMacro { name: name_ref.text().to_string(), expansion })
} }
fn expand_macro_recur( fn expand_macro_recur(
@ -87,7 +92,8 @@ mod tests {
let (analysis, pos) = analysis_and_position(fixture); let (analysis, pos) = analysis_and_position(fixture);
let result = analysis.expand_macro(pos).unwrap().unwrap(); 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] #[test]

View file

@ -66,6 +66,7 @@ pub use crate::{
completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
diagnostics::Severity, diagnostics::Severity,
display::{file_structure, FunctionSignature, NavigationTarget, StructureNode}, display::{file_structure, FunctionSignature, NavigationTarget, StructureNode},
expand_macro::ExpandedMacro,
feature_flags::FeatureFlags, feature_flags::FeatureFlags,
folding_ranges::{Fold, FoldKind}, folding_ranges::{Fold, FoldKind},
hover::HoverResult, hover::HoverResult,
@ -297,7 +298,7 @@ impl Analysis {
self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range)) self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
} }
pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<(String, String)>> { pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
self.with_db(|db| expand_macro::expand_macro(db, position)) self.with_db(|db| expand_macro::expand_macro(db, position))
} }

View file

@ -50,7 +50,7 @@ pub fn handle_syntax_tree(world: WorldSnapshot, params: req::SyntaxTreeParams) -
pub fn handle_expand_macro( pub fn handle_expand_macro(
world: WorldSnapshot, world: WorldSnapshot,
params: req::ExpandMacroParams, params: req::ExpandMacroParams,
) -> Result<Option<(String, String)>> { ) -> Result<Option<req::ExpandedMacro>> {
let _p = profile("handle_expand_macro"); let _p = profile("handle_expand_macro");
let file_id = params.text_document.try_conv_with(&world)?; let file_id = params.text_document.try_conv_with(&world)?;
let line_index = world.analysis().file_line_index(file_id)?; let line_index = world.analysis().file_line_index(file_id)?;
@ -58,7 +58,10 @@ pub fn handle_expand_macro(
match offset { match offset {
None => Ok(None), 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 }))
}
} }
} }

View file

@ -45,11 +45,18 @@ pub struct SyntaxTreeParams {
pub range: Option<Range>, pub range: Option<Range>,
} }
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ExpandedMacro {
pub name: String,
pub expansion: String,
}
pub enum ExpandMacro {} pub enum ExpandMacro {}
impl Request for ExpandMacro { impl Request for ExpandMacro {
type Params = ExpandMacroParams; type Params = ExpandMacroParams;
type Result = Option<(String, String)>; type Result = Option<ExpandedMacro>;
const METHOD: &'static str = "rust-analyzer/expandMacro"; const METHOD: &'static str = "rust-analyzer/expandMacro";
} }

View file

@ -2,13 +2,16 @@ import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server'; 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( 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; return markdown;
} }
@ -23,7 +26,7 @@ export class ExpandMacroHoverProvider implements vscode.HoverProvider {
textDocument: { uri: document.uri.toString() }, textDocument: { uri: document.uri.toString() },
position position
}; };
const result = await Server.client.sendRequest<ExpandMacroResult>( const result = await Server.client.sendRequest<ExpandedMacro>(
'rust-analyzer/expandMacro', 'rust-analyzer/expandMacro',
request request
); );