mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
move db basics to ra_db
This should allow to move hir to a separate crate
This commit is contained in:
parent
b2de95879a
commit
11168c464c
22 changed files with 352 additions and 293 deletions
69
crates/ra_db/src/lib.rs
Normal file
69
crates/ra_db/src/lib.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
//! ra_db defines basic database traits. Concrete DB is defined by ra_analysis.
|
||||
|
||||
extern crate ra_editor;
|
||||
extern crate ra_syntax;
|
||||
extern crate relative_path;
|
||||
extern crate rustc_hash;
|
||||
extern crate salsa;
|
||||
|
||||
mod syntax_ptr;
|
||||
mod file_resolver;
|
||||
mod input;
|
||||
mod loc2id;
|
||||
|
||||
use std::sync::Arc;
|
||||
use ra_editor::LineIndex;
|
||||
use ra_syntax::SourceFileNode;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct Canceled;
|
||||
|
||||
pub type Cancelable<T> = Result<T, Canceled>;
|
||||
|
||||
impl std::fmt::Display for Canceled {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
fmt.write_str("Canceled")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Canceled {}
|
||||
|
||||
pub use crate::{
|
||||
syntax_ptr::LocalSyntaxPtr,
|
||||
file_resolver::{FileResolver, FileResolverImp},
|
||||
input::{
|
||||
FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, WORKSPACE,
|
||||
FileTextQuery, FileSourceRootQuery, SourceRootQuery, LibrariesQuery, CrateGraphQuery,
|
||||
},
|
||||
loc2id::{LocationIntener, NumericId},
|
||||
};
|
||||
|
||||
pub trait BaseDatabase: salsa::Database {
|
||||
fn check_canceled(&self) -> Cancelable<()> {
|
||||
if self.salsa_runtime().is_current_revision_canceled() {
|
||||
Err(Canceled)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
salsa::query_group! {
|
||||
pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
|
||||
fn source_file(file_id: FileId) -> SourceFileNode {
|
||||
type SourceFileQuery;
|
||||
}
|
||||
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
|
||||
type FileLinesQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode {
|
||||
let text = db.file_text(file_id);
|
||||
SourceFileNode::parse(&*text)
|
||||
}
|
||||
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
|
||||
let text = db.file_text(file_id);
|
||||
Arc::new(LineIndex::new(&*text))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue