rust/crates/ra_ide_api/src/display/structure.rs

210 lines
5.8 KiB
Rust
Raw Normal View History

2019-01-02 07:29:08 -06:00
use crate::TextRange;
use ra_syntax::{
algo::visit::{visitor, Visitor},
ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner},
2019-01-07 07:53:24 -06:00
AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
2019-01-02 07:29:08 -06:00
};
#[derive(Debug, Clone)]
pub struct StructureNode {
pub parent: Option<usize>,
pub label: String,
pub navigation_range: TextRange,
pub node_range: TextRange,
pub kind: SyntaxKind,
2019-01-24 11:21:17 -06:00
pub detail: Option<String>,
2019-02-05 16:05:46 -06:00
pub deprecated: bool,
2019-01-02 07:29:08 -06:00
}
2019-01-07 07:53:24 -06:00
pub fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
2019-01-02 07:29:08 -06:00
let mut res = Vec::new();
let mut stack = Vec::new();
for event in file.syntax().preorder() {
match event {
WalkEvent::Enter(node) => {
2019-07-19 04:56:47 -05:00
if let Some(mut symbol) = structure_node(&node) {
2019-07-04 12:26:44 -05:00
symbol.parent = stack.last().copied();
2019-01-02 07:29:08 -06:00
stack.push(res.len());
res.push(symbol);
}
}
WalkEvent::Leave(node) => {
2019-07-19 04:56:47 -05:00
if structure_node(&node).is_some() {
2019-01-02 07:29:08 -06:00
stack.pop().unwrap();
}
}
}
}
res
}
2019-01-07 07:53:24 -06:00
fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
2019-07-19 04:56:47 -05:00
fn decl<N: NameOwner + AttrsOwner>(node: N) -> Option<StructureNode> {
2019-01-24 11:21:17 -06:00
decl_with_detail(node, None)
}
fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>(
2019-07-19 04:56:47 -05:00
node: N,
) -> Option<StructureNode> {
2019-07-19 04:56:47 -05:00
let ty = node.ascribed_type();
decl_with_type_ref(node, ty)
}
2019-02-05 16:05:46 -06:00
fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
2019-07-19 04:56:47 -05:00
node: N,
type_ref: Option<ast::TypeRef>,
2019-01-24 11:21:17 -06:00
) -> Option<StructureNode> {
let detail = type_ref.map(|type_ref| {
let mut detail = String::new();
collapse_ws(type_ref.syntax(), &mut detail);
detail
});
decl_with_detail(node, detail)
}
2019-02-05 16:05:46 -06:00
fn decl_with_detail<N: NameOwner + AttrsOwner>(
2019-07-19 04:56:47 -05:00
node: N,
2019-02-05 16:05:46 -06:00
detail: Option<String>,
) -> Option<StructureNode> {
2019-01-02 07:29:08 -06:00
let name = node.name()?;
2019-02-06 06:22:16 -06:00
2019-01-02 07:29:08 -06:00
Some(StructureNode {
parent: None,
label: name.text().to_string(),
navigation_range: name.syntax().range(),
node_range: node.syntax().range(),
kind: node.syntax().kind(),
2019-01-24 11:21:17 -06:00
detail,
2019-02-08 05:49:43 -06:00
deprecated: node.attrs().filter_map(|x| x.as_named()).any(|x| x == "deprecated"),
2019-01-02 07:29:08 -06:00
})
}
2019-01-24 11:21:17 -06:00
fn collapse_ws(node: &SyntaxNode, output: &mut String) {
let mut can_insert_ws = false;
node.text().for_each_chunk(|chunk| {
2019-07-19 04:56:47 -05:00
for line in chunk.lines() {
let line = line.trim();
if line.is_empty() {
if can_insert_ws {
output.push(' ');
2019-07-19 04:56:47 -05:00
can_insert_ws = false;
}
} else {
output.push_str(line);
can_insert_ws = true;
2019-01-24 11:21:17 -06:00
}
}
})
2019-01-24 11:21:17 -06:00
}
2019-01-02 07:29:08 -06:00
visitor()
2019-07-19 04:56:47 -05:00
.visit(|fn_def: ast::FnDef| {
2019-01-24 11:21:17 -06:00
let mut detail = String::from("fn");
if let Some(type_param_list) = fn_def.type_param_list() {
collapse_ws(type_param_list.syntax(), &mut detail);
}
if let Some(param_list) = fn_def.param_list() {
collapse_ws(param_list.syntax(), &mut detail);
}
if let Some(ret_type) = fn_def.ret_type() {
detail.push_str(" ");
collapse_ws(ret_type.syntax(), &mut detail);
}
decl_with_detail(fn_def, Some(detail))
})
2019-01-02 07:29:08 -06:00
.visit(decl::<ast::StructDef>)
.visit(decl::<ast::EnumDef>)
2019-01-26 01:12:00 -06:00
.visit(decl::<ast::EnumVariant>)
2019-01-02 07:29:08 -06:00
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
2019-07-19 04:56:47 -05:00
.visit(|td: ast::TypeAliasDef| {
let ty = td.type_ref();
decl_with_type_ref(td, ty)
})
.visit(decl_with_ascription::<ast::NamedFieldDef>)
.visit(decl_with_ascription::<ast::ConstDef>)
.visit(decl_with_ascription::<ast::StaticDef>)
2019-07-19 04:56:47 -05:00
.visit(|im: ast::ImplBlock| {
2019-01-02 07:29:08 -06:00
let target_type = im.target_type()?;
let target_trait = im.target_trait();
let label = match target_trait {
None => format!("impl {}", target_type.syntax().text()),
2019-02-08 05:49:43 -06:00
Some(t) => {
format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),)
}
2019-01-02 07:29:08 -06:00
};
let node = StructureNode {
parent: None,
label,
navigation_range: target_type.syntax().range(),
node_range: im.syntax().range(),
kind: im.syntax().kind(),
2019-01-24 11:21:17 -06:00
detail: None,
2019-02-05 16:05:46 -06:00
deprecated: false,
2019-01-02 07:29:08 -06:00
};
Some(node)
})
2019-07-19 04:56:47 -05:00
.visit(|mc: ast::MacroCall| {
2019-06-28 16:08:02 -05:00
let first_token = mc.syntax().first_token().unwrap();
if first_token.text().as_str() != "macro_rules" {
return None;
}
decl(mc)
})
2019-07-19 04:56:47 -05:00
.accept(&node)?
2019-01-02 07:29:08 -06:00
}
#[cfg(test)]
mod tests {
use super::*;
2019-01-14 07:27:08 -06:00
use insta::assert_debug_snapshot_matches;
2019-01-02 07:29:08 -06:00
#[test]
fn test_file_structure() {
2019-01-07 07:53:24 -06:00
let file = SourceFile::parse(
2019-01-02 07:29:08 -06:00
r#"
struct Foo {
x: i32
}
mod m {
2019-01-24 11:21:17 -06:00
fn bar1() {}
fn bar2<T>(t: T) -> T {}
fn bar3<A,
B>(a: A,
b: B) -> Vec<
u32
> {}
2019-01-02 07:29:08 -06:00
}
enum E { X, Y(i32) }
type T = ();
static S: i32 = 92;
const C: i32 = 92;
impl E {}
impl fmt::Debug for E {}
2019-02-05 16:05:46 -06:00
2019-06-28 16:08:02 -05:00
macro_rules! mc {
() => {}
}
2019-02-05 16:05:46 -06:00
#[deprecated]
fn obsolete() {}
#[deprecated(note = "for awhile")]
fn very_obsolete() {}
2019-01-02 07:29:08 -06:00
"#,
2019-05-28 10:46:11 -05:00
)
.ok()
.unwrap();
2019-01-02 07:29:08 -06:00
let structure = file_structure(&file);
2019-01-14 07:27:08 -06:00
assert_debug_snapshot_matches!("file_structure", structure);
2019-01-02 07:29:08 -06:00
}
}