aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-16 17:08:03 +0000
committerGitHub <[email protected]>2020-12-16 17:08:03 +0000
commit067067a6c11bb5afda98f5af14bfdec4744e7812 (patch)
tree1c0b6c4c78ee040ebdf818dada804fce311382a6 /crates/assists
parent63bbdb31e5148c804bbf940963c9c8f3481ad258 (diff)
parentdd496223f50232fe98312ee8edc89eb4b5ee3d85 (diff)
Merge #6896
6896: Node-ify lifetimes r=jonas-schievink a=Veykril Let's see if this passes the tests 🤞 Depends on https://github.com/rust-analyzer/ungrammar/pull/15 Co-authored-by: Jonas Schievink <[email protected]> Co-authored-by: Jonas Schievink <[email protected]> Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/generate_impl.rs2
-rw-r--r--crates/assists/src/handlers/generate_new.rs2
-rw-r--r--crates/assists/src/handlers/introduce_named_lifetime.rs21
3 files changed, 11 insertions, 14 deletions
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<()
53 if let Some(type_params) = type_params { 53 if let Some(type_params) = type_params {
54 let lifetime_params = type_params 54 let lifetime_params = type_params
55 .lifetime_params() 55 .lifetime_params()
56 .filter_map(|it| it.lifetime_token()) 56 .filter_map(|it| it.lifetime())
57 .map(|it| it.text().clone()); 57 .map(|it| it.text().clone());
58 let type_params = type_params 58 let type_params = type_params
59 .type_params() 59 .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 {
99 if let Some(type_params) = type_params { 99 if let Some(type_params) = type_params {
100 let lifetime_params = type_params 100 let lifetime_params = type_params
101 .lifetime_params() 101 .lifetime_params()
102 .filter_map(|it| it.lifetime_token()) 102 .filter_map(|it| it.lifetime())
103 .map(|it| it.text().clone()); 103 .map(|it| it.text().clone());
104 let type_params = 104 let type_params =
105 type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); 105 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 @@
1use rustc_hash::FxHashSet; 1use rustc_hash::FxHashSet;
2use syntax::{ 2use syntax::{
3 ast::{self, GenericParamsOwner, NameOwner}, 3 ast::{self, GenericParamsOwner, NameOwner},
4 AstNode, SyntaxKind, TextRange, TextSize, 4 AstNode, TextRange, TextSize,
5}; 5};
6 6
7use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists}; 7use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
@@ -35,13 +35,12 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
35// FIXME: How can we handle renaming any one of multiple anonymous lifetimes? 35// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
36// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo 36// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
37pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 37pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let lifetime_token = ctx 38 let lifetime =
39 .find_token_syntax_at_offset(SyntaxKind::LIFETIME) 39 ctx.find_node_at_offset::<ast::Lifetime>().filter(|lifetime| lifetime.text() == "'_")?;
40 .filter(|lifetime| lifetime.text() == "'_")?; 40 if let Some(fn_def) = lifetime.syntax().ancestors().find_map(ast::Fn::cast) {
41 if let Some(fn_def) = lifetime_token.ancestors().find_map(ast::Fn::cast) { 41 generate_fn_def_assist(acc, &fn_def, lifetime.lifetime_ident_token()?.text_range())
42 generate_fn_def_assist(acc, &fn_def, lifetime_token.text_range()) 42 } else if let Some(impl_def) = lifetime.syntax().ancestors().find_map(ast::Impl::cast) {
43 } else if let Some(impl_def) = lifetime_token.ancestors().find_map(ast::Impl::cast) { 43 generate_impl_def_assist(acc, &impl_def, lifetime.lifetime_ident_token()?.text_range())
44 generate_impl_def_assist(acc, &impl_def, lifetime_token.text_range())
45 } else { 44 } else {
46 None 45 None
47 } 46 }
@@ -58,7 +57,7 @@ fn generate_fn_def_assist(
58 let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end(); 57 let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end();
59 let self_param = 58 let self_param =
60 // use the self if it's a reference and has no explicit lifetime 59 // use the self if it's a reference and has no explicit lifetime
61 param_list.self_param().filter(|p| p.lifetime_token().is_none() && p.amp_token().is_some()); 60 param_list.self_param().filter(|p| p.lifetime().is_none() && p.amp_token().is_some());
62 // compute the location which implicitly has the same lifetime as the anonymous lifetime 61 // compute the location which implicitly has the same lifetime as the anonymous lifetime
63 let loc_needing_lifetime = if let Some(self_param) = self_param { 62 let loc_needing_lifetime = if let Some(self_param) = self_param {
64 // if we have a self reference, use that 63 // if we have a self reference, use that
@@ -68,9 +67,7 @@ fn generate_fn_def_assist(
68 let fn_params_without_lifetime: Vec<_> = param_list 67 let fn_params_without_lifetime: Vec<_> = param_list
69 .params() 68 .params()
70 .filter_map(|param| match param.ty() { 69 .filter_map(|param| match param.ty() {
71 Some(ast::Type::RefType(ascribed_type)) 70 Some(ast::Type::RefType(ascribed_type)) if ascribed_type.lifetime().is_none() => {
72 if ascribed_type.lifetime_token() == None =>
73 {
74 Some(ascribed_type.amp_token()?.text_range().end()) 71 Some(ascribed_type.amp_token()?.text_range().end())
75 } 72 }
76 _ => None, 73 _ => None,