From dd496223f50232fe98312ee8edc89eb4b5ee3d85 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 15 Dec 2020 19:23:51 +0100 Subject: Node-ify lifetimes --- crates/assists/src/handlers/generate_impl.rs | 2 +- crates/assists/src/handlers/generate_new.rs | 2 +- .../src/handlers/introduce_named_lifetime.rs | 21 +++++++++------------ 3 files changed, 11 insertions(+), 14 deletions(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/generate_impl.rs b/crates/assists/src/handlers/generate_impl.rs index 114974465..960af5ab3 100644 --- a/crates/assists/src/handlers/generate_impl.rs +++ b/crates/assists/src/handlers/generate_impl.rs @@ -53,7 +53,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() - .filter_map(|it| it.lifetime_token()) + .filter_map(|it| it.lifetime()) .map(|it| it.text().clone()); let type_params = type_params .type_params() diff --git a/crates/assists/src/handlers/generate_new.rs b/crates/assists/src/handlers/generate_new.rs index 7db10f276..c5fec4e0a 100644 --- a/crates/assists/src/handlers/generate_new.rs +++ b/crates/assists/src/handlers/generate_new.rs @@ -99,7 +99,7 @@ fn generate_impl_text(strukt: &ast::Struct, code: &str) -> String { if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() - .filter_map(|it| it.lifetime_token()) + .filter_map(|it| it.lifetime()) .map(|it| it.text().clone()); let type_params = type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); diff --git a/crates/assists/src/handlers/introduce_named_lifetime.rs b/crates/assists/src/handlers/introduce_named_lifetime.rs index 4cc8dae65..ab8fe3ea9 100644 --- a/crates/assists/src/handlers/introduce_named_lifetime.rs +++ b/crates/assists/src/handlers/introduce_named_lifetime.rs @@ -1,7 +1,7 @@ use rustc_hash::FxHashSet; use syntax::{ ast::{self, GenericParamsOwner, NameOwner}, - AstNode, SyntaxKind, TextRange, TextSize, + AstNode, TextRange, TextSize, }; use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists}; @@ -35,13 +35,12 @@ static ASSIST_LABEL: &str = "Introduce named lifetime"; // FIXME: How can we handle renaming any one of multiple anonymous lifetimes? // FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let lifetime_token = ctx - .find_token_syntax_at_offset(SyntaxKind::LIFETIME) - .filter(|lifetime| lifetime.text() == "'_")?; - if let Some(fn_def) = lifetime_token.ancestors().find_map(ast::Fn::cast) { - generate_fn_def_assist(acc, &fn_def, lifetime_token.text_range()) - } else if let Some(impl_def) = lifetime_token.ancestors().find_map(ast::Impl::cast) { - generate_impl_def_assist(acc, &impl_def, lifetime_token.text_range()) + let lifetime = + ctx.find_node_at_offset::().filter(|lifetime| lifetime.text() == "'_")?; + if let Some(fn_def) = lifetime.syntax().ancestors().find_map(ast::Fn::cast) { + generate_fn_def_assist(acc, &fn_def, lifetime.lifetime_ident_token()?.text_range()) + } else if let Some(impl_def) = lifetime.syntax().ancestors().find_map(ast::Impl::cast) { + generate_impl_def_assist(acc, &impl_def, lifetime.lifetime_ident_token()?.text_range()) } else { None } @@ -58,7 +57,7 @@ fn generate_fn_def_assist( let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end(); let self_param = // use the self if it's a reference and has no explicit lifetime - param_list.self_param().filter(|p| p.lifetime_token().is_none() && p.amp_token().is_some()); + param_list.self_param().filter(|p| p.lifetime().is_none() && p.amp_token().is_some()); // compute the location which implicitly has the same lifetime as the anonymous lifetime let loc_needing_lifetime = if let Some(self_param) = self_param { // if we have a self reference, use that @@ -68,9 +67,7 @@ fn generate_fn_def_assist( let fn_params_without_lifetime: Vec<_> = param_list .params() .filter_map(|param| match param.ty() { - Some(ast::Type::RefType(ascribed_type)) - if ascribed_type.lifetime_token() == None => - { + Some(ast::Type::RefType(ascribed_type)) if ascribed_type.lifetime().is_none() => { Some(ascribed_type.amp_token()?.text_range().end()) } _ => None, -- cgit v1.2.3