rust/crates/ra_ide_api/src/syntax_tree.rs

351 lines
8.7 KiB
Rust
Raw Normal View History

use crate::db::RootDatabase;
use ra_db::SourceDatabase;
use ra_syntax::{
2019-07-20 12:04:34 -05:00
algo, AstNode, NodeOrToken, SourceFile,
SyntaxKind::{RAW_STRING, STRING},
SyntaxToken, TextRange,
};
pub use ra_db::FileId;
pub(crate) fn syntax_tree(
db: &RootDatabase,
file_id: FileId,
text_range: Option<TextRange>,
) -> String {
let parse = db.parse(file_id);
if let Some(text_range) = text_range {
let node = match algo::find_covering_element(parse.tree().syntax(), text_range) {
2019-07-20 12:04:34 -05:00
NodeOrToken::Node(node) => node,
NodeOrToken::Token(token) => {
2019-07-19 04:56:47 -05:00
if let Some(tree) = syntax_tree_for_string(&token, text_range) {
2019-03-30 05:25:53 -05:00
return tree;
}
token.parent()
}
};
2019-07-20 04:48:24 -05:00
format!("{:#?}", node)
} else {
2019-07-20 04:48:24 -05:00
format!("{:#?}", parse.tree().syntax())
}
}
/// Attempts parsing the selected contents of a string literal
/// as rust syntax and returns its syntax tree
2019-07-19 04:56:47 -05:00
fn syntax_tree_for_string(token: &SyntaxToken, text_range: TextRange) -> Option<String> {
// When the range is inside a string
// we'll attempt parsing it as rust syntax
// to provide the syntax tree of the contents of the string
2019-03-30 05:25:53 -05:00
match token.kind() {
STRING | RAW_STRING => syntax_tree_for_token(token, text_range),
_ => None,
}
}
2019-07-19 04:56:47 -05:00
fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<String> {
// Range of the full node
2019-07-20 04:58:27 -05:00
let node_range = node.text_range();
let text = node.text().to_string();
// We start at some point inside the node
// Either we have selected the whole string
// or our selection is inside it
let start = text_range.start() - node_range.start();
// how many characters we have selected
let len = text_range.len().to_usize();
let node_len = node_range.len().to_usize();
let start = start.to_usize();
// We want to cap our length
let len = len.min(node_len);
// Ensure our slice is inside the actual string
let end = if start + len < text.len() { start + len } else { text.len() - start };
let text = &text[start..end];
// Remove possible extra string quotes from the start
// and the end of the string
let text = text
.trim_start_matches('r')
.trim_start_matches('#')
.trim_start_matches('"')
.trim_end_matches('#')
.trim_end_matches('"')
.trim()
// Remove custom markers
.replace("<|>", "");
let parsed = SourceFile::parse(&text);
// If the "file" parsed without errors,
// return its syntax
if parsed.errors().is_empty() {
2019-07-20 04:48:24 -05:00
return Some(format!("{:#?}", parsed.tree().syntax()));
}
None
}
2019-03-25 15:03:32 -05:00
#[cfg(test)]
mod tests {
2019-04-02 06:04:23 -05:00
use test_utils::assert_eq_text;
2019-03-25 15:03:32 -05:00
use crate::mock_analysis::{single_file, single_file_with_range};
#[test]
fn test_syntax_tree_without_range() {
// Basic syntax
let (analysis, file_id) = single_file(r#"fn foo() {}"#);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(file_id, None).unwrap();
2019-03-25 15:03:32 -05:00
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
SOURCE_FILE@[0; 11)
FN_DEF@[0; 11)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
2019-04-02 06:04:23 -05:00
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
2019-03-25 15:03:32 -05:00
BLOCK@[9; 11)
2019-04-02 06:04:23 -05:00
L_CURLY@[9; 10) "{"
R_CURLY@[10; 11) "}"
"#
2019-03-25 15:03:32 -05:00
.trim()
);
let (analysis, file_id) = single_file(
r#"
fn test() {
assert!("
fn foo() {
}
", "");
}"#
.trim(),
);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(file_id, None).unwrap();
2019-03-25 15:03:32 -05:00
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
SOURCE_FILE@[0; 60)
FN_DEF@[0; 60)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 7)
IDENT@[3; 7) "test"
PARAM_LIST@[7; 9)
2019-04-02 06:04:23 -05:00
L_PAREN@[7; 8) "("
R_PAREN@[8; 9) ")"
WHITESPACE@[9; 10) " "
2019-03-25 15:03:32 -05:00
BLOCK@[10; 60)
2019-04-02 06:04:23 -05:00
L_CURLY@[10; 11) "{"
WHITESPACE@[11; 16) "\n "
2019-03-25 15:03:32 -05:00
EXPR_STMT@[16; 58)
MACRO_CALL@[16; 57)
PATH@[16; 22)
PATH_SEGMENT@[16; 22)
NAME_REF@[16; 22)
IDENT@[16; 22) "assert"
2019-04-02 06:04:23 -05:00
EXCL@[22; 23) "!"
2019-03-25 15:03:32 -05:00
TOKEN_TREE@[23; 57)
2019-04-02 06:04:23 -05:00
L_PAREN@[23; 24) "("
STRING@[24; 52) "\"\n fn foo() {\n ..."
COMMA@[52; 53) ","
WHITESPACE@[53; 54) " "
STRING@[54; 56) "\"\""
R_PAREN@[56; 57) ")"
SEMI@[57; 58) ";"
WHITESPACE@[58; 59) "\n"
R_CURLY@[59; 60) "}"
"#
2019-03-25 15:03:32 -05:00
.trim()
);
}
#[test]
fn test_syntax_tree_with_range() {
let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim());
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
2019-03-25 15:03:32 -05:00
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
FN_DEF@[0; 11)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
2019-04-02 06:04:23 -05:00
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
2019-03-25 15:03:32 -05:00
BLOCK@[9; 11)
2019-04-02 06:04:23 -05:00
L_CURLY@[9; 10) "{"
R_CURLY@[10; 11) "}"
"#
2019-03-25 15:03:32 -05:00
.trim()
);
let (analysis, range) = single_file_with_range(
r#"fn test() {
<|>assert!("
fn foo() {
}
", "");<|>
}"#
.trim(),
);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
2019-03-25 15:03:32 -05:00
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
EXPR_STMT@[16; 58)
MACRO_CALL@[16; 57)
PATH@[16; 22)
PATH_SEGMENT@[16; 22)
NAME_REF@[16; 22)
IDENT@[16; 22) "assert"
2019-04-02 06:04:23 -05:00
EXCL@[22; 23) "!"
2019-03-25 15:03:32 -05:00
TOKEN_TREE@[23; 57)
2019-04-02 06:04:23 -05:00
L_PAREN@[23; 24) "("
STRING@[24; 52) "\"\n fn foo() {\n ..."
COMMA@[52; 53) ","
WHITESPACE@[53; 54) " "
STRING@[54; 56) "\"\""
R_PAREN@[56; 57) ")"
SEMI@[57; 58) ";"
"#
2019-03-25 15:03:32 -05:00
.trim()
);
}
#[test]
fn test_syntax_tree_inside_string() {
let (analysis, range) = single_file_with_range(
r#"fn test() {
assert!("
<|>fn foo() {
}<|>
fn bar() {
}
", "");
}"#
.trim(),
);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
SOURCE_FILE@[0; 12)
FN_DEF@[0; 12)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
2019-04-02 06:04:23 -05:00
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
2019-03-25 15:03:32 -05:00
BLOCK@[9; 12)
2019-04-02 06:04:23 -05:00
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
R_CURLY@[11; 12) "}"
2019-03-25 15:03:32 -05:00
"#
.trim()
);
// With a raw string
let (analysis, range) = single_file_with_range(
r###"fn test() {
assert!(r#"
<|>fn foo() {
}<|>
fn bar() {
}
"#, "");
}"###
.trim(),
);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
SOURCE_FILE@[0; 12)
FN_DEF@[0; 12)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
2019-04-02 06:04:23 -05:00
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
2019-03-25 15:03:32 -05:00
BLOCK@[9; 12)
2019-04-02 06:04:23 -05:00
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
R_CURLY@[11; 12) "}"
2019-03-25 15:03:32 -05:00
"#
.trim()
);
// With a raw string
let (analysis, range) = single_file_with_range(
r###"fn test() {
assert!(r<|>#"
fn foo() {
}
fn bar() {
}"<|>#, "");
}"###
.trim(),
);
2019-07-25 12:22:41 -05:00
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
2019-04-02 06:04:23 -05:00
assert_eq_text!(
2019-03-25 15:03:32 -05:00
syn.trim(),
r#"
SOURCE_FILE@[0; 25)
FN_DEF@[0; 12)
2019-04-02 06:04:23 -05:00
FN_KW@[0; 2) "fn"
WHITESPACE@[2; 3) " "
2019-03-25 15:03:32 -05:00
NAME@[3; 6)
IDENT@[3; 6) "foo"
PARAM_LIST@[6; 8)
2019-04-02 06:04:23 -05:00
L_PAREN@[6; 7) "("
R_PAREN@[7; 8) ")"
WHITESPACE@[8; 9) " "
2019-03-25 15:03:32 -05:00
BLOCK@[9; 12)
2019-04-02 06:04:23 -05:00
L_CURLY@[9; 10) "{"
WHITESPACE@[10; 11) "\n"
R_CURLY@[11; 12) "}"
WHITESPACE@[12; 13) "\n"
2019-03-25 15:03:32 -05:00
FN_DEF@[13; 25)
2019-04-02 06:04:23 -05:00
FN_KW@[13; 15) "fn"
WHITESPACE@[15; 16) " "
2019-03-25 15:03:32 -05:00
NAME@[16; 19)
IDENT@[16; 19) "bar"
PARAM_LIST@[19; 21)
2019-04-02 06:04:23 -05:00
L_PAREN@[19; 20) "("
R_PAREN@[20; 21) ")"
WHITESPACE@[21; 22) " "
2019-03-25 15:03:32 -05:00
BLOCK@[22; 25)
2019-04-02 06:04:23 -05:00
L_CURLY@[22; 23) "{"
WHITESPACE@[23; 24) "\n"
R_CURLY@[24; 25) "}"
2019-03-25 15:03:32 -05:00
"#
.trim()
);
}
}