mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
ra_syntax::File is just RootNode
This commit is contained in:
parent
7196286ec5
commit
8eaf7952ae
4 changed files with 969 additions and 206 deletions
File diff suppressed because it is too large
Load diff
|
@ -11,6 +11,8 @@ the below applies to the result of this template
|
|||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{
|
||||
ast,
|
||||
SyntaxNode, SyntaxNodeRef, AstNode,
|
||||
|
@ -21,7 +23,7 @@ use crate::{
|
|||
// {{ node }}
|
||||
|
||||
{%- if methods.enum %}
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum {{ node }}<'a> {
|
||||
{%- for kind in methods.enum %}
|
||||
{{ kind }}({{ kind }}<'a>),
|
||||
|
@ -46,12 +48,20 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
|
|||
}
|
||||
}
|
||||
{% else %}
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy,)]
|
||||
pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> {
|
||||
syntax: SyntaxNode<R>,
|
||||
pub(crate) syntax: SyntaxNode<R>,
|
||||
}
|
||||
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>;
|
||||
|
||||
impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<{{node}}Node<R1>> for {{node}}Node<R2> {
|
||||
fn eq(&self, other: &{{node}}Node<R1>) -> bool { self.syntax == other.syntax }
|
||||
}
|
||||
impl<R: TreeRoot<RaTypes>> Eq for {{node}}Node<R> {}
|
||||
impl<R: TreeRoot<RaTypes>> Hash for {{node}}Node<R> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
|
||||
}
|
||||
|
||||
impl<'a> AstNode<'a> for {{ node }}<'a> {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
|
||||
match syntax.kind() {
|
||||
|
|
|
@ -60,11 +60,9 @@ pub use crate::{
|
|||
|
||||
use crate::yellow::GreenNode;
|
||||
|
||||
// TODO: pick a single name for everything. SourceFile maybe?
|
||||
/// File represents a parse tree for a single Rust file.
|
||||
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
||||
pub struct File {
|
||||
root: SyntaxNode,
|
||||
}
|
||||
pub type File = ast::RootNode;
|
||||
|
||||
impl File {
|
||||
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File {
|
||||
|
@ -72,7 +70,8 @@ impl File {
|
|||
if cfg!(debug_assertions) {
|
||||
utils::validate_block_structure(root.borrowed());
|
||||
}
|
||||
File { root }
|
||||
assert_eq!(root.kind(), SyntaxKind::ROOT);
|
||||
ast::RootNode { syntax: root }
|
||||
}
|
||||
pub fn parse(text: &str) -> File {
|
||||
let tokens = tokenize(&text);
|
||||
|
@ -95,14 +94,14 @@ impl File {
|
|||
}
|
||||
/// Typed AST representation of the parse tree.
|
||||
pub fn ast(&self) -> ast::Root {
|
||||
ast::Root::cast(self.syntax()).unwrap()
|
||||
self.borrowed()
|
||||
}
|
||||
/// Untyped homogeneous representation of the parse tree.
|
||||
pub fn syntax(&self) -> SyntaxNodeRef {
|
||||
self.root.borrowed()
|
||||
self.syntax.borrowed()
|
||||
}
|
||||
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||
let mut errors = self.root.root_data().clone();
|
||||
let mut errors = self.syntax.root_data().clone();
|
||||
errors.extend(validation::validate(self));
|
||||
errors
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
|
||||
pub(crate) fn validate(file: &File) -> Vec<SyntaxError> {
|
||||
let mut errors = Vec::new();
|
||||
for node in file.root.borrowed().descendants() {
|
||||
for node in file.syntax().descendants() {
|
||||
let _ = visitor_ctx(&mut errors)
|
||||
.visit::<ast::Char, _>(validate_char)
|
||||
.accept(node);
|
||||
|
|
Loading…
Reference in a new issue