From 7afd84febc76a75a3ed1be75c57ff35d7b8b3de6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 12:28:59 +0300 Subject: visitor --- crates/libeditor/src/extend_selection.rs | 7 ++- crates/libeditor/src/lib.rs | 14 ++--- crates/libeditor/src/symbols.rs | 67 ++++++++++++++++++++++++ crates/libeditor/tests/test.rs | 25 ++++++++- crates/libsyntax2/src/algo/mod.rs | 1 + crates/libsyntax2/src/algo/visit.rs | 52 +++++++++++++++++++ crates/libsyntax2/src/ast/generated.rs | 80 +++++++++-------------------- crates/libsyntax2/src/ast/generated.rs.tera | 8 +++ crates/libsyntax2/src/ast/mod.rs | 9 ++++ crates/libsyntax2/src/grammar.ron | 28 +++------- crates/libsyntax2/src/yellow/syntax.rs | 6 +++ 11 files changed, 211 insertions(+), 86 deletions(-) create mode 100644 crates/libeditor/src/symbols.rs create mode 100644 crates/libsyntax2/src/algo/visit.rs diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs index 16d4bc084..ed7d9b3f7 100644 --- a/crates/libeditor/src/extend_selection.rs +++ b/crates/libeditor/src/extend_selection.rs @@ -1,11 +1,16 @@ use libsyntax2::{ + ast, AstNode, TextRange, SyntaxNodeRef, SyntaxKind::WHITESPACE, algo::{find_leaf_at_offset, find_covering_node, ancestors}, }; +pub fn extend_selection(file: &ast::File, range: TextRange) -> Option { + let syntax = file.syntax(); + extend(syntax.as_ref(), range) +} -pub(crate) fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option { +pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option { if range.is_empty() { let offset = range.start(); let mut leaves = find_leaf_at_offset(root, offset); diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 293fafae7..4ea344b17 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs @@ -2,16 +2,21 @@ extern crate libsyntax2; extern crate superslice; mod extend_selection; +mod symbols; mod line_index; use libsyntax2::{ - ast, + ast::{self, NameOwner}, SyntaxNodeRef, AstNode, algo::walk, SyntaxKind::*, }; pub use libsyntax2::{File, TextRange, TextUnit}; -pub use self::line_index::{LineIndex, LineCol}; +pub use self::{ + line_index::{LineIndex, LineCol}, + extend_selection::extend_selection, + symbols::{FileSymbol, file_symbols} +}; #[derive(Debug)] pub struct HighlightedRange { @@ -108,11 +113,6 @@ pub fn symbols(file: &ast::File) -> Vec { res // NLL :-( } -pub fn extend_selection(file: &ast::File, range: TextRange) -> Option { - let syntax = file.syntax(); - extend_selection::extend_selection(syntax.as_ref(), range) -} - pub fn runnables(file: &ast::File) -> Vec { file .functions() diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs new file mode 100644 index 000000000..3faf96868 --- /dev/null +++ b/crates/libeditor/src/symbols.rs @@ -0,0 +1,67 @@ +use libsyntax2::{ + SyntaxKind, SyntaxNodeRef, SyntaxRoot, AstNode, + ast::{self, NameOwner}, + algo::{ + visit::{visitor, Visitor}, + walk::{walk, WalkEvent}, + }, +}; +use TextRange; + +#[derive(Debug)] +pub struct FileSymbol { + pub parent: Option, + pub name: String, + pub name_range: TextRange, + pub node_range: TextRange, + pub kind: SyntaxKind, +} + + +pub fn file_symbols(file: &ast::File) -> Vec { + let mut res = Vec::new(); + let mut stack = Vec::new(); + let syntax = file.syntax(); + + for event in walk(syntax.as_ref()) { + match event { + WalkEvent::Enter(node) => { + match to_symbol(node) { + Some(mut symbol) => { + symbol.parent = stack.last().map(|&n| n); + stack.push(res.len()); + res.push(symbol); + } + None => (), + } + } + WalkEvent::Exit(node) => { + if to_symbol(node).is_some() { + stack.pop().unwrap(); + } + } + } + } + res +} + +fn to_symbol(node: SyntaxNodeRef) -> Option { + fn decl<'a, N: NameOwner<&'a SyntaxRoot>>(node: N) -> Option { + let name = node.name()?; + Some(FileSymbol { + parent: None, + name: name.text(), + name_range: name.syntax().range(), + node_range: node.syntax().range(), + kind: node.syntax().kind(), + }) + } + + visitor() + .visit(decl::>) + .visit(decl::>) + .visit(decl::>) + .visit(decl::>) + .visit(decl::>) + .accept(node)? +} diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs index d617f4b99..ba7181ab8 100644 --- a/crates/libeditor/tests/test.rs +++ b/crates/libeditor/tests/test.rs @@ -3,7 +3,7 @@ extern crate itertools; use std::fmt; use itertools::Itertools; -use libeditor::{File, highlight, runnables, extend_selection, TextRange}; +use libeditor::{File, highlight, runnables, extend_selection, TextRange, file_symbols}; #[test] fn test_extend_selection() { @@ -58,6 +58,29 @@ fn test_foo() {} ) } +#[test] +fn symbols() { + let file = file(r#" +struct Foo { + x: i32 +} + +mod m { + fn bar() {} +} + +enum E { X, Y(i32) } +"#); + let symbols = file_symbols(&file); + dbg_eq( + &symbols, + r#"[FileSymbol { parent: None, name: "Foo", name_range: [8; 11), node_range: [1; 26), kind: STRUCT }, + 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: FUNCTION }, + FileSymbol { parent: None, name: "E", name_range: [60; 61), node_range: [55; 75), kind: ENUM }]"#, + ) +} + fn file(text: &str) -> File { File::parse(text) } diff --git a/crates/libsyntax2/src/algo/mod.rs b/crates/libsyntax2/src/algo/mod.rs index d2de70fd4..263b58d97 100644 --- a/crates/libsyntax2/src/algo/mod.rs +++ b/crates/libsyntax2/src/algo/mod.rs @@ -1,4 +1,5 @@ pub mod walk; +pub mod visit; use {SyntaxNodeRef, TextUnit, TextRange}; diff --git a/crates/libsyntax2/src/algo/visit.rs b/crates/libsyntax2/src/algo/visit.rs new file mode 100644 index 000000000..dc5afa5a9 --- /dev/null +++ b/crates/libsyntax2/src/algo/visit.rs @@ -0,0 +1,52 @@ +use std::marker::PhantomData; +use {SyntaxNodeRef, AstNode, SyntaxRoot}; + + +pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { + EmptyVisitor { ph: PhantomData } +} + +pub trait Visitor<'a>: Sized { + type Output; + fn accept(self, node: SyntaxNodeRef<'a>) -> Option; + fn visit(self, f: F) -> Vis + where N: AstNode<&'a SyntaxRoot>, + F: FnOnce(N) -> Self::Output, + { + Vis { inner: self, f, ph: PhantomData } + } +} + +#[derive(Debug)] +struct EmptyVisitor { + ph: PhantomData T> +} + +impl<'a, T> Visitor<'a> for EmptyVisitor { + type Output = T; + + fn accept(self, _node: SyntaxNodeRef<'a>) -> Option { + None + } +} + +#[derive(Debug)] +pub struct Vis { + inner: V, + f: F, + ph: PhantomData, +} + +impl<'a, V, N, F> Visitor<'a> for Vis + where + V: Visitor<'a>, + N: AstNode<&'a SyntaxRoot>, + F: FnOnce(N) -> >::Output, +{ + type Output = >::Output; + + fn accept(self, node: SyntaxNodeRef<'a>) -> Option { + let Vis { inner, f, .. } = self; + inner.accept(node).or_else(|| N::cast(node).map(f)) + } +} diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 7a2a9c7d4..a4b116941 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -1,9 +1,11 @@ use std::sync::Arc; use { + ast, SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxKind::*, }; +// ConstItem #[derive(Debug, Clone, Copy)] pub struct ConstItem> { syntax: SyntaxNode, @@ -19,15 +21,10 @@ impl AstNode for ConstItem { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl ConstItem { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for ConstItem {} +impl ConstItem {} +// Enum #[derive(Debug, Clone, Copy)] pub struct Enum> { syntax: SyntaxNode, @@ -43,15 +40,10 @@ impl AstNode for Enum { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Enum { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Enum {} +impl Enum {} +// File #[derive(Debug, Clone, Copy)] pub struct File> { syntax: SyntaxNode, @@ -75,6 +67,7 @@ impl File { } } +// Function #[derive(Debug, Clone, Copy)] pub struct Function> { syntax: SyntaxNode, @@ -90,15 +83,10 @@ impl AstNode for Function { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Function { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Function {} +impl Function {} +// Module #[derive(Debug, Clone, Copy)] pub struct Module> { syntax: SyntaxNode, @@ -114,15 +102,10 @@ impl AstNode for Module { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Module { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Module {} +impl Module {} +// Name #[derive(Debug, Clone, Copy)] pub struct Name> { syntax: SyntaxNode, @@ -140,6 +123,7 @@ impl AstNode for Name { impl Name {} +// StaticItem #[derive(Debug, Clone, Copy)] pub struct StaticItem> { syntax: SyntaxNode, @@ -155,15 +139,10 @@ impl AstNode for StaticItem { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl StaticItem { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for StaticItem {} +impl StaticItem {} +// Struct #[derive(Debug, Clone, Copy)] pub struct Struct> { syntax: SyntaxNode, @@ -179,15 +158,10 @@ impl AstNode for Struct { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Struct { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Struct {} +impl Struct {} +// Trait #[derive(Debug, Clone, Copy)] pub struct Trait> { syntax: SyntaxNode, @@ -203,12 +177,6 @@ impl AstNode for Trait { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Trait { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Trait {} +impl Trait {} diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index 86b8b05d1..3d79b5543 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera @@ -1,9 +1,11 @@ use std::sync::Arc; use { + ast, SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxKind::*, }; {% for node, methods in ast %} +// {{ node }} #[derive(Debug, Clone, Copy)] pub struct {{ node }}> { syntax: SyntaxNode, @@ -19,6 +21,12 @@ impl AstNode for {{ node }} { fn syntax(&self) -> &SyntaxNode { &self.syntax } } +{% if methods.traits -%} +{%- for t in methods.traits -%} +impl ast::{{ t }} for {{ node }} {} +{% endfor -%} +{%- endif -%} + impl {{ node }} { {%- if methods.collections -%} {%- for m in methods.collections -%} diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 7d3cdb93d..56bc099fe 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -12,6 +12,15 @@ pub trait AstNode: Sized { fn syntax(&self) -> &SyntaxNode; } +pub trait NameOwner: AstNode { + fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + impl File> { pub fn parse(text: &str) -> Self { File::cast(::parse(text)).unwrap() diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index c9470d4fa..3fe8fdf0b 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -220,39 +220,25 @@ Grammar( ] ), "Function": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Struct": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Enum": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Trait": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Module": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "ConstItem": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "StaticItem": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Name": (), }, diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 6e33310f1..a22275ed9 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -28,6 +28,12 @@ impl Eq for SyntaxNode {} pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; +#[test] +fn syntax_node_ref_is_copy() { + fn assert_copy(){} + assert_copy::() +} + #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct SyntaxError { pub msg: String, -- cgit v1.2.3