rust/crates/ide/src/file_structure.rs

577 lines
19 KiB
Rust
Raw Normal View History

use ide_db::SymbolKind;
2020-08-12 11:26:51 -05:00
use syntax::{
2020-07-30 13:51:43 -05:00
ast::{self, AttrsOwner, GenericParamsOwner, NameOwner},
2021-03-11 10:14:41 -06:00
match_ast, AstNode, AstToken, NodeOrToken, SourceFile, SyntaxNode, SyntaxToken, TextRange,
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,
2021-03-14 10:16:29 -05:00
pub kind: StructureNodeKind,
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
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum StructureNodeKind {
SymbolKind(SymbolKind),
Region,
}
2020-05-30 18:54:54 -05:00
// Feature: File Structure
//
// Provides a tree of the symbols defined in the file. Can be used to
//
// * fuzzy search symbol in a file (super useful)
// * draw breadcrumbs to describe the context around the cursor
// * draw outline of the file
//
// |===
// | Editor | Shortcut
//
// | VS Code | kbd:[Ctrl+Shift+O]
// |===
2020-11-02 09:31:38 -06:00
pub(crate) 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();
2021-03-11 10:14:41 -06:00
for event in file.syntax().preorder_with_tokens() {
2019-01-02 07:29:08 -06:00
match event {
2021-03-11 10:14:41 -06:00
WalkEvent::Enter(NodeOrToken::Node(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);
}
}
2021-03-11 10:14:41 -06:00
WalkEvent::Leave(NodeOrToken::Node(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();
}
}
2021-03-11 10:14:41 -06:00
WalkEvent::Enter(NodeOrToken::Token(token)) => {
if let Some(mut symbol) = structure_token(token) {
symbol.parent = stack.last().copied();
stack.push(res.len());
res.push(symbol);
}
}
WalkEvent::Leave(NodeOrToken::Token(token)) => {
if structure_token(token).is_some() {
stack.pop().unwrap();
}
}
2019-01-02 07:29:08 -06:00
}
}
res
}
2019-01-07 07:53:24 -06:00
fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
2021-03-14 10:16:29 -05:00
fn decl<N: NameOwner + AttrsOwner>(node: N, kind: StructureNodeKind) -> Option<StructureNode> {
decl_with_detail(&node, None, kind)
}
2019-02-05 16:05:46 -06:00
fn decl_with_type_ref<N: NameOwner + AttrsOwner>(
2020-07-30 13:51:43 -05:00
node: &N,
type_ref: Option<ast::Type>,
2021-03-14 10:16:29 -05:00
kind: StructureNodeKind,
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, kind)
2019-01-24 11:21:17 -06:00
}
2019-02-05 16:05:46 -06:00
fn decl_with_detail<N: NameOwner + AttrsOwner>(
2020-07-30 13:51:43 -05:00
node: &N,
2019-02-05 16:05:46 -06:00
detail: Option<String>,
2021-03-14 10:16:29 -05:00
kind: StructureNodeKind,
2019-02-05 16:05:46 -06:00
) -> 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(),
2019-07-20 04:58:27 -05:00
navigation_range: name.syntax().text_range(),
node_range: node.syntax().text_range(),
kind,
2019-01-24 11:21:17 -06:00
detail,
2019-09-29 16:15:03 -05:00
deprecated: node.attrs().filter_map(|x| x.simple_name()).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-10-05 09:03:03 -05:00
match_ast! {
match node {
2020-07-30 07:51:08 -05:00
ast::Fn(it) => {
2019-10-05 09:03:03 -05:00
let mut detail = String::from("fn");
if let Some(type_param_list) = it.generic_param_list() {
2019-10-05 09:03:03 -05:00
collapse_ws(type_param_list.syntax(), &mut detail);
}
if let Some(param_list) = it.param_list() {
collapse_ws(param_list.syntax(), &mut detail);
}
if let Some(ret_type) = it.ret_type() {
detail.push_str(" ");
collapse_ws(ret_type.syntax(), &mut detail);
}
2019-01-24 11:21:17 -06:00
2021-03-14 10:16:29 -05:00
decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function))
2019-10-05 09:03:03 -05:00
},
2021-03-14 10:16:29 -05:00
ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)),
ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)),
ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)),
ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)),
ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)),
ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)),
ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)),
ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)),
ast::Const(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Const)),
ast::Static(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Static)),
2020-07-30 11:28:28 -05:00
ast::Impl(it) => {
2020-07-31 13:23:52 -05:00
let target_type = it.self_ty()?;
let target_trait = it.trait_();
2019-10-05 09:03:03 -05:00
let label = match target_trait {
None => format!("impl {}", target_type.syntax().text()),
Some(t) => {
format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),)
}
};
let node = StructureNode {
parent: None,
label,
navigation_range: target_type.syntax().text_range(),
node_range: it.syntax().text_range(),
2021-03-14 10:16:29 -05:00
kind: StructureNodeKind::SymbolKind(SymbolKind::Impl),
2019-10-05 09:03:03 -05:00
detail: None,
deprecated: false,
};
Some(node)
},
ast::Macro(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
2019-10-05 09:03:03 -05:00
_ => None,
}
}
2019-01-02 07:29:08 -06:00
}
2021-03-11 10:14:41 -06:00
fn structure_token(token: SyntaxToken) -> Option<StructureNode> {
if let Some(comment) = ast::Comment::cast(token) {
let text = comment.text().trim();
2021-03-12 03:01:43 -06:00
if let Some(region_name) = text.strip_prefix("// region:").map(str::trim) {
2021-03-11 10:14:41 -06:00
return Some(StructureNode {
parent: None,
label: region_name.to_string(),
navigation_range: comment.syntax().text_range(),
node_range: comment.syntax().text_range(),
2021-03-14 10:16:29 -05:00
kind: StructureNodeKind::Region,
2021-03-11 10:14:41 -06:00
detail: None,
deprecated: false,
});
}
}
None
}
2019-01-02 07:29:08 -06:00
#[cfg(test)]
mod tests {
2020-08-21 06:19:31 -05:00
use expect_test::{expect, Expect};
2020-07-01 10:59:44 -05:00
2019-01-02 07:29:08 -06:00
use super::*;
2020-07-01 10:59:44 -05:00
fn check(ra_fixture: &str, expect: Expect) {
let file = SourceFile::parse(ra_fixture).ok().unwrap();
let structure = file_structure(&file);
expect.assert_debug_eq(&structure)
}
2019-01-02 07:29:08 -06:00
#[test]
fn test_file_structure() {
2020-07-01 10:59:44 -05:00
check(
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 {
() => {}
}
#[macro_export]
macro_rules! mcexp {
() => {}
}
/// Doc comment
macro_rules! mcexp {
() => {}
}
2019-02-05 16:05:46 -06:00
#[deprecated]
fn obsolete() {}
#[deprecated(note = "for awhile")]
fn very_obsolete() {}
2021-03-11 10:14:41 -06:00
// region: Some region name
// endregion
2021-03-14 05:52:04 -05:00
// region: dontpanic
mod m {
fn f() {}
// endregion
fn g() {}
}
2019-01-02 07:29:08 -06:00
"#,
2020-07-01 10:59:44 -05:00
expect![[r#"
[
StructureNode {
parent: None,
label: "Foo",
navigation_range: 8..11,
node_range: 1..26,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Struct,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
0,
),
label: "x",
navigation_range: 18..19,
node_range: 18..24,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Field,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"i32",
),
deprecated: false,
},
StructureNode {
parent: None,
label: "m",
navigation_range: 32..33,
node_range: 28..158,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Module,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
2,
),
label: "bar1",
navigation_range: 43..47,
node_range: 40..52,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"fn()",
),
deprecated: false,
},
StructureNode {
parent: Some(
2,
),
label: "bar2",
navigation_range: 60..64,
node_range: 57..81,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"fn<T>(t: T) -> T",
),
deprecated: false,
},
StructureNode {
parent: Some(
2,
),
label: "bar3",
navigation_range: 89..93,
node_range: 86..156,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"fn<A, B>(a: A, b: B) -> Vec< u32 >",
),
deprecated: false,
},
StructureNode {
parent: None,
label: "E",
navigation_range: 165..166,
node_range: 160..180,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Enum,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
6,
),
label: "X",
navigation_range: 169..170,
node_range: 169..170,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Variant,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
6,
),
label: "Y",
navigation_range: 172..173,
node_range: 172..178,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Variant,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "T",
navigation_range: 186..187,
node_range: 181..193,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
TypeAlias,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"()",
),
deprecated: false,
},
StructureNode {
parent: None,
label: "S",
navigation_range: 201..202,
node_range: 194..213,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Static,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"i32",
),
deprecated: false,
},
StructureNode {
parent: None,
label: "C",
navigation_range: 220..221,
node_range: 214..232,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Const,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"i32",
),
deprecated: false,
},
StructureNode {
parent: None,
label: "impl E",
navigation_range: 239..240,
node_range: 234..243,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Impl,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "impl fmt::Debug for E",
navigation_range: 265..266,
node_range: 245..269,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Impl,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "mc",
navigation_range: 284..286,
node_range: 271..303,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Macro,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "mcexp",
navigation_range: 334..339,
node_range: 305..356,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Macro,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "mcexp",
navigation_range: 387..392,
node_range: 358..409,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Macro,
),
2020-07-01 10:59:44 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: None,
label: "obsolete",
navigation_range: 428..436,
node_range: 411..441,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"fn()",
),
deprecated: true,
},
StructureNode {
parent: None,
label: "very_obsolete",
navigation_range: 481..494,
node_range: 443..499,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2020-07-01 10:59:44 -05:00
detail: Some(
"fn()",
),
deprecated: true,
},
2021-03-11 10:14:41 -06:00
StructureNode {
parent: None,
label: "Some region name",
navigation_range: 501..528,
node_range: 501..528,
kind: Region,
detail: None,
deprecated: false,
},
2021-03-14 05:52:04 -05:00
StructureNode {
parent: None,
label: "m",
navigation_range: 568..569,
node_range: 543..606,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Module,
),
2021-03-14 05:52:04 -05:00
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
20,
),
label: "dontpanic",
navigation_range: 543..563,
node_range: 543..563,
kind: Region,
detail: None,
deprecated: false,
},
StructureNode {
parent: Some(
20,
),
label: "f",
navigation_range: 575..576,
node_range: 572..581,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2021-03-14 05:52:04 -05:00
detail: Some(
"fn()",
),
deprecated: false,
},
StructureNode {
parent: Some(
20,
),
label: "g",
navigation_range: 598..599,
node_range: 582..604,
2021-03-14 10:16:29 -05:00
kind: SymbolKind(
Function,
),
2021-03-14 05:52:04 -05:00
detail: Some(
"fn()",
),
deprecated: false,
},
2020-07-01 10:59:44 -05:00
]
"#]],
);
2019-01-02 07:29:08 -06:00
}
}