Replace HasSource usages with Semantics equivalent

This commit is contained in:
Lukas Wirth 2022-01-14 10:59:27 +01:00
parent c3148cb396
commit 2f8dd64830
4 changed files with 17 additions and 17 deletions

View file

@ -16,6 +16,9 @@ use crate::{
pub trait HasSource { pub trait HasSource {
type Ast; type Ast;
/// Fetches the definition's source node.
/// Using [`crate::Semantics::source`] is preferred when working with [`crate::Semantics`],
/// as that caches the parsed file in the semantics' cache.
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>; fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>>;
} }

View file

@ -443,8 +443,7 @@ impl<'db> SemanticsImpl<'db> {
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> { fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
let sa = self.analyze_no_infer(macro_call.syntax()); let sa = self.analyze_no_infer(macro_call.syntax());
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?; let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
let node = self.db.parse_or_expand(file_id)?; let node = self.parse_or_expand(file_id)?;
self.cache(node.clone(), file_id);
Some(node) Some(node)
} }
@ -452,8 +451,7 @@ impl<'db> SemanticsImpl<'db> {
let src = self.find_file(item.syntax()).with_value(item.clone()); let src = self.find_file(item.syntax()).with_value(item.clone());
let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src))?; let macro_call_id = self.with_ctx(|ctx| ctx.item_to_macro_call(src))?;
let file_id = macro_call_id.as_file(); let file_id = macro_call_id.as_file();
let node = self.db.parse_or_expand(file_id)?; let node = self.parse_or_expand(file_id)?;
self.cache(node.clone(), file_id);
Some(node) Some(node)
} }
@ -750,9 +748,8 @@ impl<'db> SemanticsImpl<'db> {
} }
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange { fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
let root = self.db.parse_or_expand(src.file_id).unwrap(); let root = self.parse_or_expand(src.file_id).unwrap();
let node = src.value.to_node(&root); let node = src.value.to_node(&root);
self.cache(root, src.file_id);
src.with_value(&node).original_file_range(self.db.upcast()) src.with_value(&node).original_file_range(self.db.upcast())
} }

View file

@ -31,7 +31,7 @@
//! } //! }
//! ``` //! ```
use hir::{self, HasAttrs, HasSource}; use hir::{self, HasAttrs};
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind}; use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
use syntax::{ use syntax::{
ast::{self, edit_in_place::AttrsOwnerEdit}, ast::{self, edit_in_place::AttrsOwnerEdit},
@ -151,7 +151,7 @@ fn add_function_impl(
let range = replacement_range(ctx, fn_def_node); let range = replacement_range(ctx, fn_def_node);
if let Some(source) = func.source(ctx.db) { if let Some(source) = ctx.sema.source(func) {
let assoc_item = ast::AssocItem::Fn(source.value); let assoc_item = ast::AssocItem::Fn(source.value);
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) { if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
let transformed_fn = match transformed_item { let transformed_fn = match transformed_item {
@ -189,7 +189,7 @@ fn get_transformed_assoc_item(
target_scope, target_scope,
source_scope, source_scope,
trait_, trait_,
impl_def.source(ctx.db)?.value, ctx.sema.source(impl_def)?.value,
); );
transform.apply(assoc_item.syntax()); transform.apply(assoc_item.syntax());
@ -227,7 +227,7 @@ fn add_const_impl(
let const_name = const_.name(ctx.db).map(|n| n.to_smol_str()); let const_name = const_.name(ctx.db).map(|n| n.to_smol_str());
if let Some(const_name) = const_name { if let Some(const_name) = const_name {
if let Some(source) = const_.source(ctx.db) { if let Some(source) = ctx.sema.source(const_) {
let assoc_item = ast::AssocItem::Const(source.value); let assoc_item = ast::AssocItem::Const(source.value);
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) { if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
let transformed_const = match transformed_item { let transformed_const = match transformed_item {

View file

@ -1,8 +1,8 @@
//! Renderer for macro invocations. //! Renderer for macro invocations.
use either::Either; use either::Either;
use hir::{db::HirDatabase, Documentation, HasSource}; use hir::{Documentation, HasSource, InFile, Semantics};
use ide_db::SymbolKind; use ide_db::{RootDatabase, SymbolKind};
use syntax::{ use syntax::{
display::{fn_as_proc_macro_label, macro_label}, display::{fn_as_proc_macro_label, macro_label},
SmolStr, SmolStr,
@ -30,8 +30,6 @@ fn render(
macro_: hir::MacroDef, macro_: hir::MacroDef,
import_to_add: Option<ImportEdit>, import_to_add: Option<ImportEdit>,
) -> CompletionItem { ) -> CompletionItem {
let db = completion.db;
let source_range = if completion.is_immediately_after_macro_bang() { let source_range = if completion.is_immediately_after_macro_bang() {
cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token); cov_mark::hit!(completes_macro_call_if_cursor_at_bang_token);
completion.token.parent().map_or_else(|| ctx.source_range(), |it| it.text_range()) completion.token.parent().map_or_else(|| ctx.source_range(), |it| it.text_range())
@ -54,7 +52,7 @@ fn render(
label(&ctx, needs_bang, bra, ket, &name), label(&ctx, needs_bang, bra, ket, &name),
); );
item.set_deprecated(ctx.is_deprecated(macro_)) item.set_deprecated(ctx.is_deprecated(macro_))
.set_detail(detail(db, macro_)) .set_detail(detail(&completion.sema, macro_))
.set_documentation(docs); .set_documentation(docs);
if let Some(import_to_add) = import_to_add { if let Some(import_to_add) = import_to_add {
@ -104,9 +102,11 @@ fn banged_name(name: &str) -> SmolStr {
SmolStr::from_iter([name, "!"]) SmolStr::from_iter([name, "!"])
} }
fn detail(db: &dyn HirDatabase, macro_: hir::MacroDef) -> Option<String> { fn detail(sema: &Semantics<RootDatabase>, macro_: hir::MacroDef) -> Option<String> {
// FIXME: This is parsing the file! // FIXME: This is parsing the file!
let detail = match macro_.source(db)?.value { let InFile { file_id, value } = macro_.source(sema.db)?;
let _ = sema.parse_or_expand(file_id);
let detail = match value {
Either::Left(node) => macro_label(&node), Either::Left(node) => macro_label(&node),
Either::Right(node) => fn_as_proc_macro_label(&node), Either::Right(node) => fn_as_proc_macro_label(&node),
}; };