2019-01-02 07:29:08 -06:00
|
|
|
use ra_syntax::{
|
2019-07-04 15:05:17 -05:00
|
|
|
ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner},
|
2020-05-31 02:59:38 -05:00
|
|
|
match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, 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,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
// |===
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-26 03:47:13 -06:00
|
|
|
fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>(
|
2019-07-19 04:56:47 -05:00
|
|
|
node: N,
|
2019-02-26 03:47:13 -06:00
|
|
|
) -> Option<StructureNode> {
|
2019-07-19 04:56:47 -05:00
|
|
|
let ty = node.ascribed_type();
|
|
|
|
decl_with_type_ref(node, ty)
|
2019-02-26 03:47:13 -06:00
|
|
|
}
|
|
|
|
|
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(),
|
2019-07-20 04:58:27 -05:00
|
|
|
navigation_range: name.syntax().text_range(),
|
|
|
|
node_range: node.syntax().text_range(),
|
2019-01-02 07:29:08 -06:00
|
|
|
kind: node.syntax().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;
|
2019-07-19 12:29:59 -05:00
|
|
|
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 {
|
2019-07-19 12:29:59 -05:00
|
|
|
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-07-19 12:29:59 -05:00
|
|
|
})
|
2019-01-24 11:21:17 -06:00
|
|
|
}
|
|
|
|
|
2019-10-05 09:03:03 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::FnDef(it) => {
|
|
|
|
let mut detail = String::from("fn");
|
|
|
|
if let Some(type_param_list) = it.type_param_list() {
|
|
|
|
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
|
|
|
|
2019-10-05 09:03:03 -05:00
|
|
|
decl_with_detail(it, Some(detail))
|
|
|
|
},
|
2020-04-06 09:21:33 -05:00
|
|
|
ast::StructDef(it) => decl(it),
|
|
|
|
ast::EnumDef(it) => decl(it),
|
|
|
|
ast::EnumVariant(it) => decl(it),
|
|
|
|
ast::TraitDef(it) => decl(it),
|
|
|
|
ast::Module(it) => decl(it),
|
2019-10-05 09:03:03 -05:00
|
|
|
ast::TypeAliasDef(it) => {
|
|
|
|
let ty = it.type_ref();
|
|
|
|
decl_with_type_ref(it, ty)
|
|
|
|
},
|
2020-04-06 09:21:33 -05:00
|
|
|
ast::RecordFieldDef(it) => decl_with_ascription(it),
|
|
|
|
ast::ConstDef(it) => decl_with_ascription(it),
|
|
|
|
ast::StaticDef(it) => decl_with_ascription(it),
|
2020-02-29 14:24:40 -06:00
|
|
|
ast::ImplDef(it) => {
|
2019-10-05 09:03:03 -05:00
|
|
|
let target_type = it.target_type()?;
|
|
|
|
let target_trait = it.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(),)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let node = StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label,
|
|
|
|
navigation_range: target_type.syntax().text_range(),
|
|
|
|
node_range: it.syntax().text_range(),
|
|
|
|
kind: it.syntax().kind(),
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
};
|
|
|
|
Some(node)
|
|
|
|
},
|
|
|
|
ast::MacroCall(it) => {
|
2020-01-12 08:23:04 -06:00
|
|
|
match it.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) {
|
|
|
|
Some(path_segment) if path_segment.text() == "macro_rules"
|
2020-01-10 17:06:31 -06:00
|
|
|
=> decl(it),
|
|
|
|
_ => None,
|
2019-02-08 05:49:43 -06:00
|
|
|
}
|
2019-10-05 09:03:03 -05:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 07:29:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-08-29 08:49:10 -05:00
|
|
|
use insta::assert_debug_snapshot;
|
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 {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:49:46 -06:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! mcexp {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:06:31 -06:00
|
|
|
/// Doc comment
|
|
|
|
macro_rules! mcexp {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
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-08-29 08:49:10 -05:00
|
|
|
assert_debug_snapshot!(structure,
|
2019-11-15 03:56:24 -06:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "Foo",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 8..11,
|
|
|
|
node_range: 1..26,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: STRUCT_DEF,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
label: "x",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 18..19,
|
|
|
|
node_range: 18..24,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: RECORD_FIELD_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"i32",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "m",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 32..33,
|
|
|
|
node_range: 28..158,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: MODULE,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
label: "bar1",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 43..47,
|
|
|
|
node_range: 40..52,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: FN_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"fn()",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
label: "bar2",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 60..64,
|
|
|
|
node_range: 57..81,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: FN_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"fn<T>(t: T) -> T",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
label: "bar3",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 89..93,
|
|
|
|
node_range: 86..156,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: FN_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"fn<A, B>(a: A, b: B) -> Vec< u32 >",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "E",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 165..166,
|
|
|
|
node_range: 160..180,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: ENUM_DEF,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
6,
|
|
|
|
),
|
|
|
|
label: "X",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 169..170,
|
|
|
|
node_range: 169..170,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: ENUM_VARIANT,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: Some(
|
|
|
|
6,
|
|
|
|
),
|
|
|
|
label: "Y",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 172..173,
|
|
|
|
node_range: 172..178,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: ENUM_VARIANT,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "T",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 186..187,
|
|
|
|
node_range: 181..193,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: TYPE_ALIAS_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"()",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "S",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 201..202,
|
|
|
|
node_range: 194..213,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: STATIC_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"i32",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "C",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 220..221,
|
|
|
|
node_range: 214..232,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: CONST_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"i32",
|
|
|
|
),
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "impl E",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 239..240,
|
|
|
|
node_range: 234..243,
|
2020-02-29 14:24:40 -06:00
|
|
|
kind: IMPL_DEF,
|
2019-11-15 03:56:24 -06:00
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "impl fmt::Debug for E",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 265..266,
|
|
|
|
node_range: 245..269,
|
2020-02-29 14:24:40 -06:00
|
|
|
kind: IMPL_DEF,
|
2019-11-15 03:56:24 -06:00
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "mc",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 284..286,
|
|
|
|
node_range: 271..303,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: MACRO_CALL,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
2020-01-10 14:49:46 -06:00
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "mcexp",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 334..339,
|
|
|
|
node_range: 305..356,
|
2020-01-10 14:49:46 -06:00
|
|
|
kind: MACRO_CALL,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
2020-01-10 17:06:31 -06:00
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "mcexp",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 387..392,
|
|
|
|
node_range: 358..409,
|
2020-01-10 17:06:31 -06:00
|
|
|
kind: MACRO_CALL,
|
|
|
|
detail: None,
|
|
|
|
deprecated: false,
|
|
|
|
},
|
2019-11-15 03:56:24 -06:00
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "obsolete",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 428..436,
|
|
|
|
node_range: 411..441,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: FN_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"fn()",
|
|
|
|
),
|
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
StructureNode {
|
|
|
|
parent: None,
|
|
|
|
label: "very_obsolete",
|
2020-04-24 16:51:02 -05:00
|
|
|
navigation_range: 481..494,
|
|
|
|
node_range: 443..499,
|
2019-11-15 03:56:24 -06:00
|
|
|
kind: FN_DEF,
|
|
|
|
detail: Some(
|
|
|
|
"fn()",
|
|
|
|
),
|
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 06:08:06 -05:00
|
|
|
);
|
2019-01-02 07:29:08 -06:00
|
|
|
}
|
|
|
|
}
|