From b764c38436fcb9426eb7da3be4f5fbcd63b316f5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 28 Mar 2020 11:01:25 +0100 Subject: Start stdx This crate will hold everything to small to be worth publishing --- crates/ra_assists/Cargo.toml | 4 +-- crates/ra_assists/src/handlers/add_custom_impl.rs | 14 +++----- crates/ra_assists/src/handlers/add_impl.rs | 9 ++--- crates/ra_assists/src/handlers/add_new.rs | 39 ++++++++++------------ .../ra_assists/src/handlers/introduce_variable.rs | 4 +-- 5 files changed, 31 insertions(+), 39 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index a87f4052a..3bcf58ba4 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -8,12 +8,12 @@ authors = ["rust-analyzer developers"] doctest = false [dependencies] -format-buf = "1.0.0" -join_to_string = "0.1.3" rustc-hash = "1.1.0" itertools = "0.9.0" either = "1.5.3" +stdx = { path = "../stdx" } + ra_syntax = { path = "../ra_syntax" } ra_text_edit = { path = "../ra_text_edit" } ra_fmt = { path = "../ra_fmt" } diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs index dd2bed25a..15f9b216b 100644 --- a/crates/ra_assists/src/handlers/add_custom_impl.rs +++ b/crates/ra_assists/src/handlers/add_custom_impl.rs @@ -1,17 +1,13 @@ -//! FIXME: write short doc here - -use join_to_string::join; use ra_syntax::{ ast::{self, AstNode}, Direction, SmolStr, SyntaxKind::{IDENT, WHITESPACE}, TextRange, TextUnit, }; +use stdx::SepBy; use crate::{Assist, AssistCtx, AssistId}; -const DERIVE_TRAIT: &str = "derive"; - // Assist: add_custom_impl // // Adds impl block for derived trait. @@ -38,7 +34,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { .descendants_with_tokens() .filter(|t| t.kind() == IDENT) .find_map(|i| i.into_token()) - .filter(|t| *t.text() == DERIVE_TRAIT)? + .filter(|t| *t.text() == "derive")? .text() .clone(); @@ -63,8 +59,7 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { .filter(|t| t != trait_token.text()) .collect::>(); let has_more_derives = !new_attr_input.is_empty(); - let new_attr_input = - join(new_attr_input.iter()).separator(", ").surround_with("(", ")").to_string(); + let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string(); let new_attr_input_len = new_attr_input.len(); let mut buf = String::new(); @@ -100,9 +95,10 @@ pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { #[cfg(test)] mod tests { - use super::*; use crate::helpers::{check_assist, check_assist_not_applicable}; + use super::*; + #[test] fn add_custom_impl_for_unique_input() { check_assist( diff --git a/crates/ra_assists/src/handlers/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs index afae7d385..6622eadb2 100644 --- a/crates/ra_assists/src/handlers/add_impl.rs +++ b/crates/ra_assists/src/handlers/add_impl.rs @@ -1,9 +1,8 @@ -use format_buf::format; -use join_to_string::join; use ra_syntax::{ ast::{self, AstNode, NameOwner, TypeParamsOwner}, TextUnit, }; +use stdx::{format_to, SepBy}; use crate::{Assist, AssistCtx, AssistId}; @@ -36,7 +35,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option { let mut buf = String::new(); buf.push_str("\n\nimpl"); if let Some(type_params) = &type_params { - format!(buf, "{}", type_params.syntax()); + format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); buf.push_str(name.text().as_str()); @@ -47,7 +46,9 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option { .map(|it| it.text().clone()); let type_params = type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); - join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); + + let generic_params = lifetime_params.chain(type_params).sep_by(", "); + format_to!(buf, "<{}>", generic_params) } buf.push_str(" {\n"); edit.set_cursor(start_offset + TextUnit::of_str(&buf)); diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 729a223e0..240b19fa3 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs @@ -1,14 +1,11 @@ -use std::fmt::Write; - -use format_buf::format; use hir::Adt; -use join_to_string::join; use ra_syntax::{ ast::{ self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner, }, TextUnit, T, }; +use stdx::{format_to, SepBy}; use crate::{Assist, AssistCtx, AssistId}; @@ -53,24 +50,22 @@ pub(crate) fn add_new(ctx: AssistCtx) -> Option { buf.push('\n'); } - let vis = strukt.visibility().map(|v| format!("{} ", v.syntax())); + let vis = strukt.visibility().map(|v| format!("{} ", v)); let vis = vis.as_deref().unwrap_or(""); - write!(&mut buf, " {}fn new(", vis).unwrap(); - - join(field_list.fields().filter_map(|f| { - Some(format!("{}: {}", f.name()?.syntax().text(), f.ascribed_type()?.syntax().text())) - })) - .separator(", ") - .to_buf(&mut buf); - buf.push_str(") -> Self { Self {"); - - join(field_list.fields().filter_map(|f| Some(f.name()?.syntax().text()))) - .separator(", ") - .surround_with(" ", " ") - .to_buf(&mut buf); + let params = field_list + .fields() + .filter_map(|f| { + Some(format!( + "{}: {}", + f.name()?.syntax().text(), + f.ascribed_type()?.syntax().text() + )) + }) + .sep_by(", "); + let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", "); - buf.push_str("} }"); + format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); let (start_offset, end_offset) = impl_def .and_then(|impl_def| { @@ -103,7 +98,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { let mut buf = String::with_capacity(code.len()); buf.push_str("\n\nimpl"); if let Some(type_params) = &type_params { - format!(buf, "{}", type_params.syntax()); + format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); buf.push_str(strukt.name().unwrap().text().as_str()); @@ -114,10 +109,10 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { .map(|it| it.text().clone()); let type_params = type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); - join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); + format_to!(buf, "<{}>", lifetime_params.chain(type_params).sep_by(", ")) } - format!(&mut buf, " {{\n{}\n}}\n", code); + format_to!(buf, " {{\n{}\n}}\n", code); buf } diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index b453c51fb..1edbdc14c 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs @@ -1,4 +1,3 @@ -use format_buf::format; use ra_syntax::{ ast::{self, AstNode}, SyntaxKind::{ @@ -7,6 +6,7 @@ use ra_syntax::{ }, SyntaxNode, TextUnit, }; +use stdx::format_to; use test_utils::tested_by; use crate::{Assist, AssistCtx, AssistId}; @@ -52,7 +52,7 @@ pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option { buf.push_str("let var_name = "); TextUnit::of_str("let ") }; - format!(buf, "{}", expr.syntax()); + format_to!(buf, "{}", expr.syntax()); let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); let is_full_stmt = if let Some(expr_stmt) = &full_stmt { Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) -- cgit v1.2.3