internal: port rust-analyzer to new Salsa

This commit is contained in:
David Barsky 2024-11-05 12:24:41 -05:00
parent 394374e769
commit 74620e64ec
161 changed files with 3075 additions and 2331 deletions

View file

@ -1,7 +1,10 @@
//! This module allows building an SSR MatchFinder by parsing the SSR rule
//! from a comment.
use ide_db::{base_db::SourceDatabase, EditionedFileId, FilePosition, FileRange, RootDatabase};
use ide_db::{
base_db::{salsa::AsDynDatabase, RootQueryDb},
EditionedFileId, FilePosition, FileRange, RootDatabase,
};
use syntax::{
ast::{self, AstNode, AstToken},
TextRange,
@ -17,7 +20,11 @@ pub fn ssr_from_comment(
frange: FileRange,
) -> Option<(MatchFinder<'_>, TextRange)> {
let comment = {
let file = db.parse(EditionedFileId::current_edition(frange.file_id));
let editioned_file_id = EditionedFileId::current_edition(frange.file_id);
let file_id =
ide_db::base_db::EditionedFileId::new(db.as_dyn_database(), editioned_file_id);
let file = db.parse(file_id);
file.tree().syntax().token_at_offset(frange.range.start()).find_map(ast::Comment::cast)
}?;
let comment_text_without_prefix = comment.text().strip_prefix(comment.prefix()).unwrap();

View file

@ -80,8 +80,12 @@ pub use crate::{errors::SsrError, from_comment::ssr_from_comment, matching::Matc
use crate::{errors::bail, matching::MatchFailureReason};
use hir::{FileRange, Semantics};
use ide_db::symbol_index::SymbolsDatabase;
use ide_db::text_edit::TextEdit;
use ide_db::{base_db::SourceDatabase, EditionedFileId, FileId, FxHashMap, RootDatabase};
use ide_db::{
base_db::{salsa::AsDynDatabase, SourceDatabase},
EditionedFileId, FileId, FxHashMap, RootDatabase,
};
use resolving::ResolvedRule;
use syntax::{ast, AstNode, SyntaxNode, TextRange};
@ -137,10 +141,11 @@ impl<'db> MatchFinder<'db> {
/// Constructs an instance using the start of the first file in `db` as the lookup context.
pub fn at_first_file(db: &'db ide_db::RootDatabase) -> Result<MatchFinder<'db>, SsrError> {
use ide_db::base_db::SourceRootDatabase;
use ide_db::symbol_index::SymbolsDatabase;
if let Some(first_file_id) =
db.local_roots().iter().next().and_then(|root| db.source_root(*root).iter().next())
if let Some(first_file_id) = db
.local_roots()
.iter()
.next()
.and_then(|root| db.source_root(*root).source_root(db).iter().next())
{
MatchFinder::in_context(
db,
@ -184,7 +189,7 @@ impl<'db> MatchFinder<'db> {
replacing::matches_to_edit(
self.sema.db,
&matches,
&self.sema.db.file_text(file_id),
&self.sema.db.file_text(file_id).text(self.sema.db),
&self.rules,
),
)
@ -223,9 +228,12 @@ impl<'db> MatchFinder<'db> {
file_id: EditionedFileId,
snippet: &str,
) -> Vec<MatchDebugInfo> {
let file = self.sema.parse(file_id);
let editioned_file_id_wrapper =
ide_db::base_db::EditionedFileId::new(self.sema.db.as_dyn_database(), file_id);
let file = self.sema.parse(editioned_file_id_wrapper);
let mut res = Vec::new();
let file_text = self.sema.db.file_text(file_id.into());
let file_text = self.sema.db.file_text(file_id.into()).text(self.sema.db);
let mut remaining_text = &*file_text;
let mut base = 0;
let len = snippet.len() as u32;

View file

@ -7,7 +7,7 @@ use crate::{
SsrMatches,
};
use hir::{FileRange, ImportPathConfig, Semantics};
use ide_db::{base_db::SourceDatabase, FxHashMap};
use ide_db::{base_db::RootQueryDb, FxHashMap};
use std::{cell::Cell, iter::Peekable};
use syntax::{
ast::{self, AstNode, AstToken, HasGenericArgs},

View file

@ -1,7 +1,7 @@
//! This module is responsible for resolving paths within rules.
use hir::AsAssocItem;
use ide_db::FxHashMap;
use ide_db::{base_db::salsa::AsDynDatabase, FxHashMap};
use parsing::Placeholder;
use syntax::{
ast::{self, HasGenericArgs},
@ -198,7 +198,12 @@ impl<'db> ResolutionScope<'db> {
resolve_context: hir::FilePosition,
) -> Option<ResolutionScope<'db>> {
use syntax::ast::AstNode;
let file = sema.parse(resolve_context.file_id);
let editioned_file_id_wrapper = ide_db::base_db::EditionedFileId::new(
sema.db.as_dyn_database(),
resolve_context.file_id,
);
let file = sema.parse(editioned_file_id_wrapper);
// Find a node at the requested position, falling back to the whole file.
let node = file
.syntax()

View file

@ -7,6 +7,7 @@ use crate::{
};
use hir::FileRange;
use ide_db::{
base_db::salsa::AsDynDatabase,
defs::Definition,
search::{SearchScope, UsageSearchResult},
EditionedFileId, FileId, FxHashSet,
@ -74,7 +75,12 @@ impl MatchFinder<'_> {
resolved_path: &ResolvedPath,
file_range: FileRange,
) -> Vec<SyntaxNode> {
let file = self.sema.parse(file_range.file_id);
let editioned_file_id_wrapper = ide_db::base_db::EditionedFileId::new(
self.sema.db.as_dyn_database(),
file_range.file_id,
);
let file = self.sema.parse(editioned_file_id_wrapper);
let depth = resolved_path.depth as usize;
let offset = file_range.range.start();
@ -156,10 +162,10 @@ impl MatchFinder<'_> {
fn search_files_do(&self, mut callback: impl FnMut(FileId)) {
if self.restrict_ranges.is_empty() {
// Unrestricted search.
use ide_db::base_db::SourceRootDatabase;
use ide_db::base_db::SourceDatabase;
use ide_db::symbol_index::SymbolsDatabase;
for &root in self.sema.db.local_roots().iter() {
let sr = self.sema.db.source_root(root);
let sr = self.sema.db.source_root(root).source_root(self.sema.db);
for file_id in sr.iter() {
callback(file_id);
}

View file

@ -1,7 +1,7 @@
use expect_test::{expect, Expect};
use hir::{FilePosition, FileRange};
use ide_db::{
base_db::{ra_salsa::Durability, SourceDatabase},
base_db::{salsa::Durability, SourceDatabase},
EditionedFileId, FxHashSet,
};
use test_utils::RangeOrOffset;
@ -114,7 +114,7 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
}
// Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters
// stuff.
let mut actual = db.file_text(position.file_id.into()).to_string();
let mut actual = db.file_text(position.file_id.into()).text(&db).to_string();
edits[&position.file_id.into()].apply(&mut actual);
expected.assert_eq(&actual);
}