From 46b4f89c920c314caf1a8af2abdb09732d100d67 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Jan 2021 01:56:11 +0300 Subject: . --- crates/assists/src/handlers/generate_impl.rs | 11 +++++++---- crates/assists/src/handlers/generate_new.rs | 8 ++++---- crates/assists/src/handlers/raw_string.rs | 2 +- .../src/handlers/replace_derive_with_manual_impl.rs | 19 ++++++++++--------- 4 files changed, 22 insertions(+), 18 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 9af45192b..827477272 100644 --- a/crates/assists/src/handlers/generate_impl.rs +++ b/crates/assists/src/handlers/generate_impl.rs @@ -1,6 +1,9 @@ use itertools::Itertools; use stdx::format_to; -use syntax::ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}; +use syntax::{ + ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}, + SmolStr, +}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -49,16 +52,16 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); - buf.push_str(name.text().as_str()); + buf.push_str(name.text()); if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() .filter_map(|it| it.lifetime()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let type_params = type_params .type_params() .filter_map(|it| it.name()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let generic_params = lifetime_params.chain(type_params).format(", "); format_to!(buf, "<{}>", generic_params) diff --git a/crates/assists/src/handlers/generate_new.rs b/crates/assists/src/handlers/generate_new.rs index 5c52b2bc8..b7390855a 100644 --- a/crates/assists/src/handlers/generate_new.rs +++ b/crates/assists/src/handlers/generate_new.rs @@ -3,7 +3,7 @@ use itertools::Itertools; use stdx::format_to; use syntax::{ ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner}, - T, + SmolStr, T, }; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -95,14 +95,14 @@ fn generate_impl_text(strukt: &ast::Struct, code: &str) -> String { format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); - buf.push_str(strukt.name().unwrap().text().as_str()); + buf.push_str(strukt.name().unwrap().text()); if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() .filter_map(|it| it.lifetime()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let type_params = - type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); + type_params.type_params().filter_map(|it| it.name()).map(|it| SmolStr::from(it.text())); format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", ")) } diff --git a/crates/assists/src/handlers/raw_string.rs b/crates/assists/src/handlers/raw_string.rs index be963f162..d95267607 100644 --- a/crates/assists/src/handlers/raw_string.rs +++ b/crates/assists/src/handlers/raw_string.rs @@ -138,7 +138,7 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> return None; } - let text = token.text().as_str(); + let text = token.text(); if !text.starts_with("r#") && text.ends_with('#') { return None; } diff --git a/crates/assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/assists/src/handlers/replace_derive_with_manual_impl.rs index bd4c1c806..6aa9d2f2c 100644 --- a/crates/assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/assists/src/handlers/replace_derive_with_manual_impl.rs @@ -3,7 +3,7 @@ use ide_db::imports_locator; use itertools::Itertools; use syntax::{ ast::{self, make, AstNode}, - Direction, SmolStr, + Direction, SyntaxKind::{IDENT, WHITESPACE}, TextSize, }; @@ -43,17 +43,18 @@ pub(crate) fn replace_derive_with_manual_impl( ) -> Option<()> { let attr = ctx.find_node_at_offset::()?; - let attr_name = attr + let has_derive = attr .syntax() .descendants_with_tokens() .filter(|t| t.kind() == IDENT) .find_map(syntax::NodeOrToken::into_token) - .filter(|t| t.text() == "derive")? - .text() - .clone(); + .filter(|t| t.text() == "derive") + .is_some(); + if !has_derive { + return None; + } - let trait_token = - ctx.token_at_offset().find(|t| t.kind() == IDENT && *t.text() != attr_name)?; + let trait_token = ctx.token_at_offset().find(|t| t.kind() == IDENT && t.text() != "derive")?; let trait_path = make::path_unqualified(make::path_segment(make::name_ref(trait_token.text()))); let annotated_name = attr.syntax().siblings(Direction::Next).find_map(ast::Name::cast)?; @@ -176,9 +177,9 @@ fn update_attribute( .syntax() .descendants_with_tokens() .filter(|t| t.kind() == IDENT) - .filter_map(|t| t.into_token().map(|t| t.text().clone())) + .filter_map(|t| t.into_token().map(|t| t.text().to_string())) .filter(|t| t != trait_name.text()) - .collect::>(); + .collect::>(); let has_more_derives = !new_attr_input.is_empty(); if has_more_derives { -- cgit v1.2.3