Skip to content

Commit 12465a8

Browse files
committed
Expose HasSource::source through Semantics with caching behaviour
1 parent fb27c58 commit 12465a8

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

crates/hir/src/semantics.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use crate::{
2525
db::HirDatabase,
2626
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
2727
source_analyzer::{resolve_hir_path, SourceAnalyzer},
28-
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile,
29-
Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type,
30-
TypeAlias, TypeParam, VariantDef,
28+
Access, AssocItem, Callable, ConstParam, Crate, Field, Function, HasSource, HirFileId, Impl,
29+
InFile, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait,
30+
Type, TypeAlias, TypeParam, VariantDef,
3131
};
3232

3333
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -190,6 +190,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
190190
self.imp.descend_node_into_attributes(node)
191191
}
192192

193+
/// Search for a definition's source and cache its syntax tree
194+
pub fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
195+
where
196+
Def::Ast: AstNode,
197+
{
198+
self.imp.source(def)
199+
}
200+
193201
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
194202
self.imp.find_file(syntax_node.clone()).file_id
195203
}
@@ -845,6 +853,15 @@ impl<'db> SemanticsImpl<'db> {
845853
SemanticsScope { db: self.db, file_id, resolver }
846854
}
847855

856+
fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
857+
where
858+
Def::Ast: AstNode,
859+
{
860+
let res = def.source(self.db)?;
861+
self.cache(find_root(res.value.syntax()), res.file_id);
862+
Some(res)
863+
}
864+
848865
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
849866
self.analyze_impl(node, None)
850867
}

crates/ide_assists/src/handlers/inline_call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ast::make;
22
use either::Either;
3-
use hir::{db::HirDatabase, HasSource, PathResolution, Semantics, TypeInfo};
3+
use hir::{db::HirDatabase, PathResolution, Semantics, TypeInfo};
44
use ide_db::{
55
base_db::{FileId, FileRange},
66
defs::Definition,
@@ -194,7 +194,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
194194
}
195195
};
196196

197-
let fn_source = function.source(ctx.db())?;
197+
let fn_source = ctx.sema.source(function)?;
198198
let fn_body = fn_source.value.body()?;
199199
let param_list = fn_source.value.param_list()?;
200200

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

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

crates/ide_completion/src/completions/trait_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn get_transformed_assoc_item(
185185
let assoc_item = assoc_item.clone_for_update();
186186
let trait_ = impl_def.trait_(ctx.db)?;
187187
let source_scope = &ctx.sema.scope_for_def(trait_);
188-
let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value);
188+
let target_scope = &ctx.sema.scope(ctx.sema.source(impl_def)?.syntax().value);
189189
let transform = PathTransform::trait_impl(
190190
target_scope,
191191
source_scope,

0 commit comments

Comments
 (0)