mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-09 20:06:28 +00:00
make Parse generic
This commit is contained in:
parent
abe72424a6
commit
a6224f3620
5 changed files with 44 additions and 23 deletions
|
@ -5,7 +5,7 @@ mod input;
|
||||||
use std::{panic, sync::Arc};
|
use std::{panic, sync::Arc};
|
||||||
|
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{Parse, SourceFile, TextRange, TextUnit};
|
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
@ -74,7 +74,7 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug {
|
||||||
fn file_text(&self, file_id: FileId) -> Arc<String>;
|
fn file_text(&self, file_id: FileId) -> Arc<String>;
|
||||||
// Parses the file into the syntax tree.
|
// Parses the file into the syntax tree.
|
||||||
#[salsa::invoke(parse_query)]
|
#[salsa::invoke(parse_query)]
|
||||||
fn parse(&self, file_id: FileId) -> Parse;
|
fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;
|
||||||
/// Path to a file, relative to the root of its source root.
|
/// Path to a file, relative to the root of its source root.
|
||||||
#[salsa::input]
|
#[salsa::input]
|
||||||
fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
|
fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
|
||||||
|
@ -98,7 +98,7 @@ fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<Cra
|
||||||
Arc::new(res)
|
Arc::new(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse {
|
fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
|
||||||
let _p = profile("parse_query");
|
let _p = profile("parse_query");
|
||||||
let text = db.file_text(file_id);
|
let text = db.file_text(file_id);
|
||||||
SourceFile::parse(&*text)
|
SourceFile::parse(&*text)
|
||||||
|
|
|
@ -43,7 +43,7 @@ pub(crate) struct CompletionContext<'a> {
|
||||||
impl<'a> CompletionContext<'a> {
|
impl<'a> CompletionContext<'a> {
|
||||||
pub(super) fn new(
|
pub(super) fn new(
|
||||||
db: &'a db::RootDatabase,
|
db: &'a db::RootDatabase,
|
||||||
original_parse: &'a Parse,
|
original_parse: &'a Parse<ast::SourceFile>,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> Option<CompletionContext<'a>> {
|
) -> Option<CompletionContext<'a>> {
|
||||||
let module = source_binder::module_from_position(db, position);
|
let module = source_binder::module_from_position(db, position);
|
||||||
|
@ -83,7 +83,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill(&mut self, original_parse: &'a Parse, offset: TextUnit) {
|
fn fill(&mut self, original_parse: &'a Parse<ast::SourceFile>, offset: TextUnit) {
|
||||||
// Insert a fake ident to get a valid parse tree. We will use this file
|
// Insert a fake ident to get a valid parse tree. We will use this file
|
||||||
// to determine context, though the original_file will be used for
|
// to determine context, though the original_file will be used for
|
||||||
// actual completion.
|
// actual completion.
|
||||||
|
|
|
@ -9,7 +9,7 @@ use ra_db::{
|
||||||
FileTextQuery, SourceRootId,
|
FileTextQuery, SourceRootId,
|
||||||
};
|
};
|
||||||
use ra_prof::{memory_usage, Bytes};
|
use ra_prof::{memory_usage, Bytes};
|
||||||
use ra_syntax::{AstNode, Parse, SyntaxNode, TreeArc};
|
use ra_syntax::{ast, AstNode, Parse, SyntaxNode, TreeArc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
|
@ -79,10 +79,10 @@ impl fmt::Display for SyntaxTreeStats {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromIterator<TableEntry<FileId, Parse>> for SyntaxTreeStats {
|
impl FromIterator<TableEntry<FileId, Parse<ast::SourceFile>>> for SyntaxTreeStats {
|
||||||
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
||||||
where
|
where
|
||||||
T: IntoIterator<Item = TableEntry<FileId, Parse>>,
|
T: IntoIterator<Item = TableEntry<FileId, Parse<ast::SourceFile>>>,
|
||||||
{
|
{
|
||||||
let mut res = SyntaxTreeStats::default();
|
let mut res = SyntaxTreeStats::default();
|
||||||
for entry in iter {
|
for entry in iter {
|
||||||
|
|
|
@ -169,7 +169,9 @@ impl SymbolIndex {
|
||||||
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
|
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn for_files(files: impl ParallelIterator<Item = (FileId, Parse)>) -> SymbolIndex {
|
pub(crate) fn for_files(
|
||||||
|
files: impl ParallelIterator<Item = (FileId, Parse<ast::SourceFile>)>,
|
||||||
|
) -> SymbolIndex {
|
||||||
let symbols = files
|
let symbols = files
|
||||||
.flat_map(|(file_id, file)| source_file_to_file_symbols(file.tree(), file_id))
|
.flat_map(|(file_id, file)| source_file_to_file_symbols(file.tree(), file_id))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
|
@ -35,7 +35,7 @@ use std::{fmt::Write, sync::Arc};
|
||||||
|
|
||||||
use ra_text_edit::AtomTextEdit;
|
use ra_text_edit::AtomTextEdit;
|
||||||
|
|
||||||
use crate::syntax_node::GreenNode;
|
use crate::syntax_node::{GreenNode, SyntaxNodeWrapper};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
ast::{AstNode, AstToken},
|
ast::{AstNode, AstToken},
|
||||||
|
@ -57,14 +57,24 @@ pub use rowan::{SmolStr, TextRange, TextUnit};
|
||||||
///
|
///
|
||||||
/// Note that we always produce a syntax tree, even for completely invalid
|
/// Note that we always produce a syntax tree, even for completely invalid
|
||||||
/// files.
|
/// files.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Parse {
|
pub struct Parse<T: SyntaxNodeWrapper> {
|
||||||
tree: TreeArc<SourceFile>,
|
tree: TreeArc<T>,
|
||||||
errors: Arc<Vec<SyntaxError>>,
|
errors: Arc<Vec<SyntaxError>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse {
|
impl<T: SyntaxNodeWrapper> Clone for Parse<T> {
|
||||||
pub fn tree(&self) -> &SourceFile {
|
fn clone(&self) -> Parse<T> {
|
||||||
|
Parse { tree: self.tree.clone(), errors: self.errors.clone() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: SyntaxNodeWrapper> Parse<T> {
|
||||||
|
fn new(tree: TreeArc<T>, errors: Vec<SyntaxError>) -> Parse<T> {
|
||||||
|
Parse { tree, errors: Arc::new(errors) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tree(&self) -> &T {
|
||||||
&*self.tree
|
&*self.tree
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,18 +82,16 @@ impl Parse {
|
||||||
&*self.errors
|
&*self.errors
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ok(self) -> Result<TreeArc<SourceFile>, Arc<Vec<SyntaxError>>> {
|
pub fn ok(self) -> Result<TreeArc<T>, Arc<Vec<SyntaxError>>> {
|
||||||
if self.errors.is_empty() {
|
if self.errors.is_empty() {
|
||||||
Ok(self.tree)
|
Ok(self.tree)
|
||||||
} else {
|
} else {
|
||||||
Err(self.errors)
|
Err(self.errors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reparse(&self, edit: &AtomTextEdit) -> Parse {
|
impl Parse<SourceFile> {
|
||||||
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn debug_dump(&self) -> String {
|
pub fn debug_dump(&self) -> String {
|
||||||
let mut buf = self.tree.syntax().debug_dump();
|
let mut buf = self.tree.syntax().debug_dump();
|
||||||
for err in self.errors.iter() {
|
for err in self.errors.iter() {
|
||||||
|
@ -92,7 +100,11 @@ impl Parse {
|
||||||
buf
|
buf
|
||||||
}
|
}
|
||||||
|
|
||||||
fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<Parse> {
|
pub fn reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> {
|
||||||
|
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<Parse<SourceFile>> {
|
||||||
// FIXME: validation errors are not handled here
|
// FIXME: validation errors are not handled here
|
||||||
parsing::incremental_reparse(self.tree.syntax(), edit, self.errors.to_vec()).map(
|
parsing::incremental_reparse(self.tree.syntax(), edit, self.errors.to_vec()).map(
|
||||||
|(green_node, errors, _reparsed_range)| Parse {
|
|(green_node, errors, _reparsed_range)| Parse {
|
||||||
|
@ -102,12 +114,19 @@ impl Parse {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full_reparse(&self, edit: &AtomTextEdit) -> Parse {
|
fn full_reparse(&self, edit: &AtomTextEdit) -> Parse<SourceFile> {
|
||||||
let text = edit.apply(self.tree.syntax().text().to_string());
|
let text = edit.apply(self.tree.syntax().text().to_string());
|
||||||
SourceFile::parse(&text)
|
SourceFile::parse(&text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Parse<SyntaxNode> {
|
||||||
|
pub fn cast<T: AstNode>(self) -> Option<Parse<T>> {
|
||||||
|
let node = T::cast(&self.tree)?;
|
||||||
|
Some(Parse { tree: node.to_owned(), errors: self.errors })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// `SourceFile` represents a parse tree for a single Rust file.
|
/// `SourceFile` represents a parse tree for a single Rust file.
|
||||||
pub use crate::ast::SourceFile;
|
pub use crate::ast::SourceFile;
|
||||||
|
|
||||||
|
@ -121,7 +140,7 @@ impl SourceFile {
|
||||||
TreeArc::cast(root)
|
TreeArc::cast(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(text: &str) -> Parse {
|
pub fn parse(text: &str) -> Parse<SourceFile> {
|
||||||
let (green, mut errors) = parsing::parse_text(text);
|
let (green, mut errors) = parsing::parse_text(text);
|
||||||
let tree = SourceFile::new(green);
|
let tree = SourceFile::new(green);
|
||||||
errors.extend(validation::validate(&tree));
|
errors.extend(validation::validate(&tree));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue