rust/crates/ra_syntax/src/yellow.rs

224 lines
5.6 KiB
Rust
Raw Normal View History

2018-07-30 13:08:06 +02:00
mod builder;
pub mod syntax_error;
2018-08-28 13:06:30 +02:00
mod syntax_text;
2018-07-29 12:51:55 +02:00
use std::{fmt, borrow::Borrow};
use self::syntax_text::SyntaxText;
use crate::{SmolStr, SyntaxKind, TextRange};
2019-01-07 14:15:47 +01:00
use rowan::{Types, TransparentNewType};
2018-08-10 16:49:45 +02:00
2018-10-02 16:07:12 +02:00
pub(crate) use self::builder::GreenBuilder;
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
2019-01-07 14:15:47 +01:00
pub use rowan::WalkEvent;
2018-10-02 16:07:12 +02:00
#[derive(Debug, Clone, Copy)]
pub enum RaTypes {}
impl Types for RaTypes {
type Kind = SyntaxKind;
type RootData = Vec<SyntaxError>;
2018-08-10 16:49:45 +02:00
}
2019-01-07 14:42:10 +01:00
pub type GreenNode = rowan::GreenNode<RaTypes>;
2019-01-07 15:00:39 +01:00
#[derive(PartialEq, Eq, Hash)]
pub struct TreeArc<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>>(
pub(crate) rowan::TreeArc<RaTypes, T>,
2019-01-07 14:42:10 +01:00
);
impl<T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>> Borrow<T> for TreeArc<T> {
fn borrow(&self) -> &T {
&*self
}
}
impl<T> TreeArc<T>
2019-01-07 14:42:10 +01:00
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
{
pub(crate) fn cast<U>(this: TreeArc<T>) -> TreeArc<U>
2019-01-07 14:42:10 +01:00
where
U: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
{
TreeArc(rowan::TreeArc::cast(this.0))
2019-01-07 14:42:10 +01:00
}
}
impl<T> std::ops::Deref for TreeArc<T>
2019-01-07 14:42:10 +01:00
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
{
type Target = T;
fn deref(&self) -> &T {
self.0.deref()
}
}
impl<T> PartialEq<T> for TreeArc<T>
2019-01-08 09:28:42 +01:00
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
T: PartialEq<T>,
{
fn eq(&self, other: &T) -> bool {
let t: &T = self;
t == other
}
}
impl<T> Clone for TreeArc<T>
2019-01-07 15:00:39 +01:00
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
{
fn clone(&self) -> TreeArc<T> {
TreeArc(self.0.clone())
2019-01-07 15:00:39 +01:00
}
}
impl<T> fmt::Debug for TreeArc<T>
2019-01-07 14:42:10 +01:00
where
T: TransparentNewType<Repr = rowan::SyntaxNode<RaTypes>>,
T: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
2018-08-17 20:10:55 +02:00
2019-01-07 14:15:47 +01:00
#[derive(PartialEq, Eq, Hash)]
#[repr(transparent)]
2019-01-07 14:42:10 +01:00
pub struct SyntaxNode(pub(crate) rowan::SyntaxNode<RaTypes>);
2019-01-07 14:15:47 +01:00
unsafe impl TransparentNewType for SyntaxNode {
2019-01-07 14:42:10 +01:00
type Repr = rowan::SyntaxNode<RaTypes>;
2018-10-02 16:07:12 +02:00
}
2018-08-17 20:10:55 +02:00
2018-10-02 16:50:56 +02:00
impl SyntaxNode {
pub(crate) fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SyntaxNode> {
let ptr = TreeArc(rowan::SyntaxNode::new(green, errors));
TreeArc::cast(ptr)
2018-10-02 16:07:12 +02:00
}
}
2018-10-02 17:14:33 +02:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Next,
Prev,
}
2019-01-07 14:15:47 +01:00
impl SyntaxNode {
pub fn leaf_text(&self) -> Option<&SmolStr> {
2018-10-02 16:07:12 +02:00
self.0.leaf_text()
2018-08-17 20:10:55 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode> {
2018-10-15 18:55:32 +02:00
crate::algo::generate(Some(self), |&node| node.parent())
}
2019-01-07 14:15:47 +01:00
pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode> {
2018-10-17 18:52:25 +02:00
self.preorder().filter_map(|event| match event {
WalkEvent::Enter(node) => Some(node),
WalkEvent::Leave(_) => None,
})
}
2019-01-07 14:15:47 +01:00
pub fn siblings(&self, direction: Direction) -> impl Iterator<Item = &SyntaxNode> {
2018-10-15 18:55:32 +02:00
crate::algo::generate(Some(self), move |&node| match direction {
2018-10-02 17:14:33 +02:00
Direction::Next => node.next_sibling(),
Direction::Prev => node.prev_sibling(),
})
}
2019-01-07 14:15:47 +01:00
pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode>> {
2018-10-17 18:52:25 +02:00
self.0.preorder().map(|event| match event {
2019-01-07 14:15:47 +01:00
WalkEvent::Enter(n) => WalkEvent::Enter(SyntaxNode::from_repr(n)),
WalkEvent::Leave(n) => WalkEvent::Leave(SyntaxNode::from_repr(n)),
2018-10-17 18:52:25 +02:00
})
}
2018-08-17 20:10:55 +02:00
}
impl ToOwned for SyntaxNode {
type Owned = TreeArc<SyntaxNode>;
fn to_owned(&self) -> TreeArc<SyntaxNode> {
let ptr = TreeArc(self.0.to_owned());
TreeArc::cast(ptr)
}
}
2019-01-07 14:15:47 +01:00
impl SyntaxNode {
2018-10-02 16:07:12 +02:00
pub(crate) fn root_data(&self) -> &Vec<SyntaxError> {
self.0.root_data()
}
2018-10-02 16:07:12 +02:00
pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode {
2019-01-07 14:15:47 +01:00
self.0.replace_self(replacement)
2018-10-02 16:07:12 +02:00
}
2018-10-02 16:07:12 +02:00
pub fn kind(&self) -> SyntaxKind {
self.0.kind()
2018-08-17 20:10:55 +02:00
}
2018-10-02 16:07:12 +02:00
pub fn range(&self) -> TextRange {
self.0.range()
2018-08-17 20:10:55 +02:00
}
2018-10-02 16:07:12 +02:00
pub fn text(&self) -> SyntaxText {
2019-01-07 14:15:47 +01:00
SyntaxText::new(self)
2018-10-02 16:07:12 +02:00
}
2018-10-02 16:07:12 +02:00
pub fn is_leaf(&self) -> bool {
self.0.is_leaf()
}
2019-01-07 14:15:47 +01:00
pub fn parent(&self) -> Option<&SyntaxNode> {
self.0.parent().map(SyntaxNode::from_repr)
2018-10-02 16:07:12 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn first_child(&self) -> Option<&SyntaxNode> {
self.0.first_child().map(SyntaxNode::from_repr)
2018-10-02 16:07:12 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn last_child(&self) -> Option<&SyntaxNode> {
self.0.last_child().map(SyntaxNode::from_repr)
2018-10-02 16:07:12 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn next_sibling(&self) -> Option<&SyntaxNode> {
self.0.next_sibling().map(SyntaxNode::from_repr)
2018-10-02 16:07:12 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn prev_sibling(&self) -> Option<&SyntaxNode> {
self.0.prev_sibling().map(SyntaxNode::from_repr)
2018-10-02 16:07:12 +02:00
}
2019-01-07 14:15:47 +01:00
pub fn children(&self) -> SyntaxNodeChildren {
2018-10-02 16:07:12 +02:00
SyntaxNodeChildren(self.0.children())
2018-08-17 20:10:55 +02:00
}
2019-01-22 21:37:03 +01:00
pub fn memory_size_of_subtree(&self) -> usize {
self.0.memory_size_of_subtree()
}
2018-08-17 20:10:55 +02:00
}
2018-08-10 16:49:45 +02:00
2019-01-07 14:15:47 +01:00
impl fmt::Debug for SyntaxNode {
2018-10-02 16:07:12 +02:00
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}@{:?}", self.kind(), self.range())?;
if has_short_text(self.kind()) {
write!(fmt, " \"{}\"", self.text())?;
2018-08-10 16:49:45 +02:00
}
2018-10-02 16:07:12 +02:00
Ok(())
2018-08-10 16:49:45 +02:00
}
}
2018-10-02 16:07:12 +02:00
#[derive(Debug)]
2019-01-07 14:42:10 +01:00
pub struct SyntaxNodeChildren<'a>(rowan::SyntaxNodeChildren<'a, RaTypes>);
2018-08-10 16:49:45 +02:00
2019-01-07 14:15:47 +01:00
impl<'a> Iterator for SyntaxNodeChildren<'a> {
type Item = &'a SyntaxNode;
2018-08-10 16:49:45 +02:00
2019-01-07 14:15:47 +01:00
fn next(&mut self) -> Option<&'a SyntaxNode> {
self.0.next().map(SyntaxNode::from_repr)
2018-08-10 16:49:45 +02:00
}
}
2018-10-02 16:07:12 +02:00
fn has_short_text(kind: SyntaxKind) -> bool {
2018-10-15 18:55:32 +02:00
use crate::SyntaxKind::*;
2018-10-02 16:07:12 +02:00
match kind {
IDENT | LIFETIME | INT_NUMBER | FLOAT_NUMBER => true,
_ => false,
}
2018-08-10 16:49:45 +02:00
}