rust/crates/ra_editor/src/symbols.rs

168 lines
5.3 KiB
Rust
Raw Normal View History

2018-09-16 11:54:24 +02:00
use ra_syntax::{
2018-08-25 10:44:58 +02:00
SyntaxKind, SyntaxNodeRef, AstNode, File, SmolStr,
2018-08-11 11:28:59 +02:00
ast::{self, NameOwner},
algo::{
visit::{visitor, Visitor},
2018-08-14 10:20:09 +02:00
walk::{walk, WalkEvent, preorder},
2018-08-11 11:28:59 +02:00
},
};
use TextRange;
2018-08-17 14:37:17 +02:00
#[derive(Debug, Clone)]
2018-08-14 10:20:09 +02:00
pub struct StructureNode {
2018-08-11 11:28:59 +02:00
pub parent: Option<usize>,
2018-08-14 10:20:09 +02:00
pub label: String,
pub navigation_range: TextRange,
2018-08-11 11:28:59 +02:00
pub node_range: TextRange,
pub kind: SyntaxKind,
}
2018-09-15 22:42:01 +02:00
#[derive(Debug, Clone, Hash)]
2018-08-14 10:20:09 +02:00
pub struct FileSymbol {
pub name: SmolStr,
pub node_range: TextRange,
pub kind: SyntaxKind,
}
2018-08-11 11:28:59 +02:00
2018-08-25 10:44:58 +02:00
pub fn file_symbols(file: &File) -> Vec<FileSymbol> {
2018-08-17 21:00:13 +02:00
preorder(file.syntax())
2018-08-14 10:20:09 +02:00
.filter_map(to_symbol)
.collect()
}
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
2018-08-17 21:00:13 +02:00
fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<FileSymbol> {
2018-08-14 10:20:09 +02:00
let name = node.name()?;
Some(FileSymbol {
name: name.text(),
node_range: node.syntax().range(),
kind: node.syntax().kind(),
})
}
visitor()
2018-08-17 21:00:13 +02:00
.visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef>)
.visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef>)
2018-08-14 10:20:09 +02:00
.accept(node)?
}
2018-08-25 10:44:58 +02:00
pub fn file_structure(file: &File) -> Vec<StructureNode> {
2018-08-11 11:28:59 +02:00
let mut res = Vec::new();
let mut stack = Vec::new();
2018-08-17 21:00:13 +02:00
for event in walk(file.syntax()) {
2018-08-11 11:28:59 +02:00
match event {
WalkEvent::Enter(node) => {
2018-08-14 10:20:09 +02:00
match structure_node(node) {
2018-08-11 11:28:59 +02:00
Some(mut symbol) => {
symbol.parent = stack.last().map(|&n| n);
stack.push(res.len());
res.push(symbol);
}
None => (),
}
}
WalkEvent::Exit(node) => {
2018-08-14 10:20:09 +02:00
if structure_node(node).is_some() {
2018-08-11 11:28:59 +02:00
stack.pop().unwrap();
}
}
}
}
res
}
2018-08-14 10:20:09 +02:00
fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
2018-08-17 21:00:13 +02:00
fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<StructureNode> {
2018-08-11 11:28:59 +02:00
let name = node.name()?;
2018-08-14 10:20:09 +02:00
Some(StructureNode {
2018-08-11 11:28:59 +02:00
parent: None,
2018-08-14 10:20:09 +02:00
label: name.text().to_string(),
navigation_range: name.syntax().range(),
2018-08-11 11:28:59 +02:00
node_range: node.syntax().range(),
kind: node.syntax().kind(),
})
}
visitor()
2018-08-17 21:00:13 +02:00
.visit(decl::<ast::FnDef>)
.visit(decl::<ast::StructDef>)
2018-08-24 18:27:30 +02:00
.visit(decl::<ast::NamedFieldDef>)
2018-08-17 21:00:13 +02:00
.visit(decl::<ast::EnumDef>)
.visit(decl::<ast::TraitDef>)
.visit(decl::<ast::Module>)
.visit(decl::<ast::TypeDef>)
.visit(decl::<ast::ConstDef>)
.visit(decl::<ast::StaticDef>)
.visit(|im: ast::ImplItem| {
2018-08-14 11:38:20 +02: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()),
Some(t) => format!(
"impl {} for {}",
t.syntax().text(),
target_type.syntax().text(),
),
};
2018-08-14 10:20:09 +02:00
let node = StructureNode {
parent: None,
label,
2018-08-14 11:38:20 +02:00
navigation_range: target_type.syntax().range(),
2018-08-14 10:20:09 +02:00
node_range: im.syntax().range(),
kind: im.syntax().kind(),
};
Some(node)
})
2018-08-11 11:28:59 +02:00
.accept(node)?
}
2018-08-28 13:47:12 +02:00
#[cfg(test)]
mod tests {
use super::*;
use test_utils::assert_eq_dbg;
#[test]
fn test_file_structure() {
let file = File::parse(r#"
struct Foo {
x: i32
}
mod m {
fn bar() {}
}
enum E { X, Y(i32) }
type T = ();
static S: i32 = 92;
const C: i32 = 92;
impl E {}
impl fmt::Debug for E {}
"#);
let symbols = file_structure(&file);
assert_eq_dbg(
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD_DEF },
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
StructureNode { parent: Some(2), label: "bar", navigation_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
StructureNode { parent: None, label: "E", navigation_range: [60; 61), node_range: [55; 75), kind: ENUM_DEF },
StructureNode { parent: None, label: "T", navigation_range: [81; 82), node_range: [76; 88), kind: TYPE_DEF },
StructureNode { parent: None, label: "S", navigation_range: [96; 97), node_range: [89; 108), kind: STATIC_DEF },
StructureNode { parent: None, label: "C", navigation_range: [115; 116), node_range: [109; 127), kind: CONST_DEF },
StructureNode { parent: None, label: "impl E", navigation_range: [134; 135), node_range: [129; 138), kind: IMPL_ITEM },
StructureNode { parent: None, label: "impl fmt::Debug for E", navigation_range: [160; 161), node_range: [140; 164), kind: IMPL_ITEM }]"#,
&symbols,
)
}
}