Expose HasSource::source through Semantics with caching behaviour

This commit is contained in:
Lukas Wirth 2021-10-07 14:46:49 +02:00
parent fb27c58a04
commit 12465a8a3c
4 changed files with 24 additions and 7 deletions

View file

@ -25,9 +25,9 @@ use crate::{
db::HirDatabase, db::HirDatabase,
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
source_analyzer::{resolve_hir_path, SourceAnalyzer}, source_analyzer::{resolve_hir_path, SourceAnalyzer},
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile, Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HasSource, HirFileId, Impl,
Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type, InFile, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait,
TypeAlias, TypeParam, VariantDef, Type, TypeAlias, TypeParam, VariantDef,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -190,6 +190,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.descend_node_into_attributes(node) self.imp.descend_node_into_attributes(node)
} }
/// Search for a definition's source and cache its syntax tree
pub fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
where
Def::Ast: AstNode,
{
self.imp.source(def)
}
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId { pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
self.imp.find_file(syntax_node.clone()).file_id self.imp.find_file(syntax_node.clone()).file_id
} }
@ -845,6 +853,15 @@ impl<'db> SemanticsImpl<'db> {
SemanticsScope { db: self.db, file_id, resolver } SemanticsScope { db: self.db, file_id, resolver }
} }
fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
where
Def::Ast: AstNode,
{
let res = def.source(self.db)?;
self.cache(find_root(res.value.syntax()), res.file_id);
Some(res)
}
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer { fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
self.analyze_impl(node, None) self.analyze_impl(node, None)
} }

View file

@ -1,6 +1,6 @@
use ast::make; use ast::make;
use either::Either; use either::Either;
use hir::{db::HirDatabase, HasSource, PathResolution, Semantics, TypeInfo}; use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
use ide_db::{ use ide_db::{
base_db::{FileId, FileRange}, base_db::{FileId, FileRange},
defs::Definition, defs::Definition,
@ -194,7 +194,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
} }
}; };
let fn_source = function.source(ctx.db())?; let fn_source = ctx.sema.source(function)?;
let fn_body = fn_source.value.body()?; let fn_body = fn_source.value.body()?;
let param_list = fn_source.value.param_list()?; let param_list = fn_source.value.param_list()?;

View file

@ -38,7 +38,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// ``` // ```
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let let_kw = ctx.find_token_syntax_at_offset(T![let])?; let let_kw = ctx.find_token_syntax_at_offset(T![let])?;
let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?; let let_stmt = let_kw.parent().and_then(ast::LetStmt::cast)?;
let init = let_stmt.initializer()?; let init = let_stmt.initializer()?;
let original_pat = let_stmt.pat()?; let original_pat = let_stmt.pat()?;

View file

@ -185,7 +185,7 @@ fn get_transformed_assoc_item(
let assoc_item = assoc_item.clone_for_update(); let assoc_item = assoc_item.clone_for_update();
let trait_ = impl_def.trait_(ctx.db)?; let trait_ = impl_def.trait_(ctx.db)?;
let source_scope = &ctx.sema.scope_for_def(trait_); let source_scope = &ctx.sema.scope_for_def(trait_);
let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value); let target_scope = &ctx.sema.scope(ctx.sema.source(impl_def)?.syntax().value);
let transform = PathTransform::trait_impl( let transform = PathTransform::trait_impl(
target_scope, target_scope,
source_scope, source_scope,