separete structure from symbols

This commit is contained in:
Aleksey Kladov 2018-08-14 11:20:09 +03:00
parent 49ab441024
commit 2b828c68e8
9 changed files with 131 additions and 39 deletions

View file

@ -19,7 +19,7 @@ pub use libsyntax2::{File, TextRange, TextUnit};
pub use self::{
line_index::{LineIndex, LineCol},
extend_selection::extend_selection,
symbols::{FileSymbol, file_symbols},
symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
edit::{EditBuilder, Edit, AtomEdit},
code_actions::{flip_comma},
};

View file

@ -4,22 +4,58 @@ use libsyntax2::{
ast::{self, NameOwner},
algo::{
visit::{visitor, Visitor},
walk::{walk, WalkEvent},
walk::{walk, WalkEvent, preorder},
},
SyntaxKind::*,
};
use TextRange;
#[derive(Debug)]
pub struct FileSymbol {
pub struct StructureNode {
pub parent: Option<usize>,
pub name: SmolStr,
pub name_range: TextRange,
pub label: String,
pub navigation_range: TextRange,
pub node_range: TextRange,
pub kind: SyntaxKind,
}
#[derive(Debug)]
pub struct FileSymbol {
pub name: SmolStr,
pub node_range: TextRange,
pub kind: SyntaxKind,
}
pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
let syntax = file.syntax();
preorder(syntax.as_ref())
.filter_map(to_symbol)
.collect()
}
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> {
let name = node.name()?;
Some(FileSymbol {
name: name.text(),
node_range: node.syntax().range(),
kind: node.syntax().kind(),
})
}
visitor()
.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<_>>)
.accept(node)?
}
pub fn file_structure(file: &ast::File) -> Vec<StructureNode> {
let mut res = Vec::new();
let mut stack = Vec::new();
let syntax = file.syntax();
@ -27,7 +63,7 @@ pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
for event in walk(syntax.as_ref()) {
match event {
WalkEvent::Enter(node) => {
match to_symbol(node) {
match structure_node(node) {
Some(mut symbol) => {
symbol.parent = stack.last().map(|&n| n);
stack.push(res.len());
@ -37,7 +73,7 @@ pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
}
}
WalkEvent::Exit(node) => {
if to_symbol(node).is_some() {
if structure_node(node).is_some() {
stack.pop().unwrap();
}
}
@ -46,13 +82,13 @@ pub fn file_symbols(file: &ast::File) -> Vec<FileSymbol> {
res
}
fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<FileSymbol> {
fn structure_node(node: SyntaxNodeRef) -> Option<StructureNode> {
fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option<StructureNode> {
let name = node.name()?;
Some(FileSymbol {
Some(StructureNode {
parent: None,
name: name.text(),
name_range: name.syntax().range(),
label: name.text().to_string(),
navigation_range: name.syntax().range(),
node_range: node.syntax().range(),
kind: node.syntax().kind(),
})
@ -67,5 +103,29 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
.visit(decl::<ast::TypeDef<_>>)
.visit(decl::<ast::ConstDef<_>>)
.visit(decl::<ast::StaticDef<_>>)
.visit(|im: ast::ImplItem<_>| {
let mut label = String::new();
let brace = im.syntax().children()
.find(|it| {
let stop = it.kind() == L_CURLY;
if !stop {
label.push_str(&it.text());
}
stop
})?;
let navigation_range = TextRange::from_to(
im.syntax().range().start(),
brace.range().start(),
);
let node = StructureNode {
parent: None,
label,
navigation_range,
node_range: im.syntax().range(),
kind: im.syntax().kind(),
};
Some(node)
})
.accept(node)?
}

View file

@ -9,7 +9,7 @@ use itertools::Itertools;
use libsyntax2::AstNode;
use libeditor::{
File, TextUnit, TextRange,
highlight, runnables, extend_selection, file_symbols, flip_comma,
highlight, runnables, extend_selection, file_structure, flip_comma,
};
#[test]
@ -66,7 +66,7 @@ fn test_foo() {}
}
#[test]
fn symbols() {
fn test_structure() {
let file = file(r#"
struct Foo {
x: i32
@ -80,16 +80,22 @@ 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_symbols(&file);
let symbols = file_structure(&file);
dbg_eq(
r#"[FileSymbol { parent: None, name: "Foo", name_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
FileSymbol { parent: None, name: "m", name_range: [32; 33), node_range: [28; 53), kind: MODULE },
FileSymbol { parent: Some(1), name: "bar", name_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
FileSymbol { parent: None, name: "E", name_range: [60; 61), node_range: [55; 75), kind: ENUM_DEF },
FileSymbol { parent: None, name: "T", name_range: [81; 82), node_range: [76; 88), kind: TYPE_DEF },
FileSymbol { parent: None, name: "S", name_range: [96; 97), node_range: [89; 108), kind: STATIC_DEF },
FileSymbol { parent: None, name: "C", name_range: [115; 116), node_range: [109; 127), kind: CONST_DEF }]"#,
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
StructureNode { parent: Some(1), 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: [129; 136), node_range: [129; 138), kind: IMPL_ITEM },
StructureNode { parent: None, label: "impl fmt::Debug for E ", navigation_range: [140; 162), node_range: [140; 164), kind: IMPL_ITEM }]"#,
&symbols,
)
}