HasSource::source -> HasSource::source_old

To start migrating HasSource::source to return an Option.
This commit is contained in:
Nick Spain 2021-01-01 13:05:28 +11:00
parent aa3ce16f26
commit 27cadcd531
15 changed files with 55 additions and 55 deletions

View file

@ -196,7 +196,7 @@ fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::Variant) -> Optio
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
let pat: ast::Pat = match var.source(db).value.kind() {
let pat: ast::Pat = match var.source_old(db).value.kind() {
ast::StructKind::Tuple(field_list) => {
let pats = iter::repeat(make::wildcard_pat().into()).take(field_list.fields().count());
make::tuple_struct_pat(path, pats).into()

View file

@ -97,7 +97,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
let parent_name = parent.name(ctx.db());
let target_module = parent.module(ctx.db());
let in_file_source = record_field_def.source(ctx.db());
let in_file_source = record_field_def.source_old(ctx.db());
let (offset, current_visibility, target) = match in_file_source.value {
hir::FieldSource::Named(it) => {
let s = it.syntax();
@ -150,7 +150,7 @@ fn target_data_for_def(
S: HasSource<Ast = Ast>,
Ast: AstNode + ast::VisibilityOwner,
{
let source = x.source(db);
let source = x.source_old(db);
let in_file_syntax = source.syntax();
let file_id = in_file_syntax.file_id;
let syntax = in_file_syntax.value;

View file

@ -99,9 +99,9 @@ pub fn filter_assoc_items(
items
.iter()
.map(|i| match i {
hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(db).value),
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(db).value),
hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(db).value),
hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source_old(db).value),
hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source_old(db).value),
hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source_old(db).value),
})
.filter(has_def_name)
.filter(|it| match it {

View file

@ -156,7 +156,7 @@ fn add_function_impl(
};
let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end());
let function_decl = function_declaration(&func.source(ctx.db).value);
let function_decl = function_declaration(&func.source_old(ctx.db).value);
match ctx.config.snippet_cap {
Some(cap) => {
let snippet = format!("{} {{\n $0\n}}", function_decl);
@ -200,7 +200,7 @@ fn add_const_impl(
let const_name = const_.name(ctx.db).map(|n| n.to_string());
if let Some(const_name) = const_name {
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
let snippet = make_const_compl_syntax(&const_.source_old(ctx.db).value);
let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end());

View file

@ -27,7 +27,7 @@ struct ConstRender<'a> {
impl<'a> ConstRender<'a> {
fn new(ctx: RenderContext<'a>, const_: hir::Const) -> ConstRender<'a> {
let ast_node = const_.source(ctx.db()).value;
let ast_node = const_.source_old(ctx.db()).value;
ConstRender { ctx, const_, ast_node }
}

View file

@ -34,7 +34,7 @@ impl<'a> FunctionRender<'a> {
fn_: hir::Function,
) -> FunctionRender<'a> {
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
let ast_node = fn_.source(ctx.db()).value;
let ast_node = fn_.source_old(ctx.db()).value;
FunctionRender { ctx, name, func: fn_, ast_node }
}

View file

@ -96,7 +96,7 @@ impl<'a> MacroRender<'a> {
}
fn detail(&self) -> String {
let ast_node = self.macro_.source(self.ctx.db()).value;
let ast_node = self.macro_.source_old(self.ctx.db()).value;
macro_label(&ast_node)
}
}

View file

@ -27,7 +27,7 @@ struct TypeAliasRender<'a> {
impl<'a> TypeAliasRender<'a> {
fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> {
let ast_node = type_alias.source(ctx.db()).value;
let ast_node = type_alias.source_old(ctx.db()).value;
TypeAliasRender { ctx, type_alias, ast_node }
}

View file

@ -989,7 +989,7 @@ impl MacroDef {
if self.is_proc_macro() {
return None;
}
self.source(db).value.name().map(|it| it.as_name())
self.source_old(db).value.name().map(|it| it.as_name())
}
/// Indicate it is a proc-macro
@ -1378,7 +1378,7 @@ impl Impl {
}
pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
let src = self.source(db);
let src = self.source_old(db);
let item = src.file_id.is_builtin_derive(db.upcast())?;
let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id);

View file

@ -16,7 +16,7 @@ use crate::{
pub trait HasSource {
type Ast;
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast>;
fn source_old(self, db: &dyn HirDatabase) -> InFile<Self::Ast>;
}
/// NB: Module is !HasSource, because it has two source nodes at the same time:
@ -46,7 +46,7 @@ impl Module {
impl HasSource for Field {
type Ast = FieldSource;
fn source(self, db: &dyn HirDatabase) -> InFile<FieldSource> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<FieldSource> {
let var = VariantId::from(self.parent);
let src = var.child_source(db.upcast());
src.map(|it| match it[self.id].clone() {
@ -57,61 +57,61 @@ impl HasSource for Field {
}
impl HasSource for Struct {
type Ast = ast::Struct;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Struct> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Struct> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Union {
type Ast = ast::Union;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Union> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Union> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Enum {
type Ast = ast::Enum;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Enum> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Enum> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Variant {
type Ast = ast::Variant;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Variant> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Variant> {
self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())
}
}
impl HasSource for Function {
type Ast = ast::Fn;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Fn> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Fn> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Const {
type Ast = ast::Const;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Const> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Const> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Static {
type Ast = ast::Static;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Static> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Static> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for Trait {
type Ast = ast::Trait;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Trait> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Trait> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for TypeAlias {
type Ast = ast::TypeAlias;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::TypeAlias> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::TypeAlias> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for MacroDef {
type Ast = ast::Macro;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Macro> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Macro> {
InFile {
file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id,
value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()),
@ -120,14 +120,14 @@ impl HasSource for MacroDef {
}
impl HasSource for Impl {
type Ast = ast::Impl;
fn source(self, db: &dyn HirDatabase) -> InFile<ast::Impl> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<ast::Impl> {
self.id.lookup(db.upcast()).source(db.upcast())
}
}
impl HasSource for TypeParam {
type Ast = Either<ast::Trait, ast::TypeParam>;
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
let child_source = self.id.parent.child_source(db.upcast());
child_source.map(|it| it[self.id.local_id].clone())
}
@ -135,7 +135,7 @@ impl HasSource for TypeParam {
impl HasSource for LifetimeParam {
type Ast = ast::LifetimeParam;
fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
fn source_old(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
let child_source = self.id.parent.child_source(db.upcast());
child_source.map(|it| it[self.id.local_id].clone())
}

View file

@ -156,20 +156,20 @@ fn missing_record_expr_field_fix(
let record_fields = match VariantDef::from(def_id) {
VariantDef::Struct(s) => {
module = s.module(sema.db);
let source = s.source(sema.db);
let source = s.source_old(sema.db);
def_file_id = source.file_id;
let fields = source.value.field_list()?;
record_field_list(fields)?
}
VariantDef::Union(u) => {
module = u.module(sema.db);
let source = u.source(sema.db);
let source = u.source_old(sema.db);
def_file_id = source.file_id;
source.value.record_field_list()?
}
VariantDef::Variant(e) => {
module = e.module(sema.db);
let source = e.source(sema.db);
let source = e.source_old(sema.db);
def_file_id = source.file_id;
let fields = source.value.field_list()?;
record_field_list(fields)?

View file

@ -285,7 +285,7 @@ where
D::Ast: ast::NameOwner + ShortLabel,
{
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
let mut res = NavigationTarget::from_named(
db,
src.as_ref().map(|it| it as &dyn ast::NameOwner),
@ -314,7 +314,7 @@ impl ToNav for hir::Module {
impl ToNav for hir::Impl {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
let derive_attr = self.is_builtin_derive(db);
let frange = if let Some(item) = &derive_attr {
item.syntax().original_file_range(db)
@ -339,7 +339,7 @@ impl ToNav for hir::Impl {
impl ToNav for hir::Field {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
match &src.value {
FieldSource::Named(it) => {
@ -365,7 +365,7 @@ impl ToNav for hir::Field {
impl ToNav for hir::MacroDef {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
log::debug!("nav target {:#?}", src.value.syntax());
let mut res = NavigationTarget::from_named(
db,
@ -448,7 +448,7 @@ impl ToNav for hir::Label {
impl ToNav for hir::TypeParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
let full_range = match &src.value {
Either::Left(it) => it.syntax().text_range(),
Either::Right(it) => it.syntax().text_range(),
@ -472,7 +472,7 @@ impl ToNav for hir::TypeParam {
impl ToNav for hir::LifetimeParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let src = self.source_old(db);
let full_range = src.value.syntax().text_range();
NavigationTarget {
file_id: src.file_id.original_file(db),

View file

@ -206,7 +206,7 @@ fn runnable_action(
_ => None,
},
ModuleDef::Function(it) => {
let src = it.source(sema.db);
let src = it.source_old(sema.db);
if src.file_id != file_id.into() {
mark::hit!(hover_macro_generated_struct_fn_doc_comment);
mark::hit!(hover_macro_generated_struct_fn_doc_attr);
@ -332,11 +332,11 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
if it.is_proc_macro() {
return None;
}
let label = macro_label(&it.source(db).value);
let label = macro_label(&it.source_old(db).value);
from_def_source_labeled(db, it, Some(label), mod_path)
}
Definition::Field(def) => {
let src = def.source(db).value;
let src = def.source_old(db).value;
if let FieldSource::Named(it) = src {
from_def_source_labeled(db, def, it.short_label(), mod_path)
} else {
@ -385,7 +385,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
D: HasSource<Ast = A> + HasAttrs + Copy,
A: ShortLabel,
{
let short_label = def.source(db).value.short_label();
let short_label = def.source_old(db).value.short_label();
from_def_source_labeled(db, def, short_label, mod_path)
}

View file

@ -121,9 +121,9 @@ impl Definition {
if let Definition::Local(var) = self {
let range = match var.parent(db) {
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
DefWithBody::Static(s) => s.source(db).value.syntax().text_range(),
DefWithBody::Function(f) => f.source_old(db).value.syntax().text_range(),
DefWithBody::Const(c) => c.source_old(db).value.syntax().text_range(),
DefWithBody::Static(s) => s.source_old(db).value.syntax().text_range(),
};
let mut res = FxHashMap::default();
res.insert(file_id, Some(range));
@ -132,17 +132,17 @@ impl Definition {
if let Definition::LifetimeParam(param) = self {
let range = match param.parent(db) {
hir::GenericDef::Function(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::Function(it) => it.source_old(db).value.syntax().text_range(),
hir::GenericDef::Adt(it) => match it {
hir::Adt::Struct(it) => it.source(db).value.syntax().text_range(),
hir::Adt::Union(it) => it.source(db).value.syntax().text_range(),
hir::Adt::Enum(it) => it.source(db).value.syntax().text_range(),
hir::Adt::Struct(it) => it.source_old(db).value.syntax().text_range(),
hir::Adt::Union(it) => it.source_old(db).value.syntax().text_range(),
hir::Adt::Enum(it) => it.source_old(db).value.syntax().text_range(),
},
hir::GenericDef::Trait(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::TypeAlias(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::Impl(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::Variant(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::Const(it) => it.source(db).value.syntax().text_range(),
hir::GenericDef::Trait(it) => it.source_old(db).value.syntax().text_range(),
hir::GenericDef::TypeAlias(it) => it.source_old(db).value.syntax().text_range(),
hir::GenericDef::Impl(it) => it.source_old(db).value.syntax().text_range(),
hir::GenericDef::Variant(it) => it.source_old(db).value.syntax().text_range(),
hir::GenericDef::Const(it) => it.source_old(db).value.syntax().text_range(),
};
let mut res = FxHashMap::default();
res.insert(file_id, Some(range));

View file

@ -161,7 +161,7 @@ impl AnalysisStatsCmd {
}
let mut msg = format!("processing: {}", full_name);
if verbosity.is_verbose() {
let src = f.source(db);
let src = f.source_old(db);
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.syntax().text_range();