From 1c359ab634edb81b51e3c7eadfb83d46c926e890 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 15:04:06 +0200 Subject: Replace SepBy with Itertools --- crates/ra_assists/src/handlers/add_custom_impl.rs | 4 +- crates/ra_assists/src/handlers/generate_impl.rs | 5 +- crates/ra_assists/src/handlers/generate_new.rs | 9 +-- crates/ra_ide/src/completion/presentation.rs | 31 +++++------ crates/ra_ide/src/syntax_highlighting/injection.rs | 5 +- crates/ra_syntax/src/ast/traits.rs | 5 +- crates/stdx/src/lib.rs | 65 +--------------------- 7 files changed, 30 insertions(+), 94 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_custom_impl.rs b/crates/ra_assists/src/handlers/add_custom_impl.rs index b67438b6b..ebdf00e67 100644 --- a/crates/ra_assists/src/handlers/add_custom_impl.rs +++ b/crates/ra_assists/src/handlers/add_custom_impl.rs @@ -1,10 +1,10 @@ +use itertools::Itertools; use ra_syntax::{ ast::{self, AstNode}, Direction, SmolStr, SyntaxKind::{IDENT, WHITESPACE}, TextRange, TextSize, }; -use stdx::SepBy; use crate::{ assist_context::{AssistContext, Assists}, @@ -61,9 +61,9 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option< .filter(|t| t != trait_token.text()) .collect::>(); let has_more_derives = !new_attr_input.is_empty(); - let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string(); if has_more_derives { + let new_attr_input = format!("({})", new_attr_input.iter().format(", ")); builder.replace(input.syntax().text_range(), new_attr_input); } else { let attr_range = attr.syntax().text_range(); diff --git a/crates/ra_assists/src/handlers/generate_impl.rs b/crates/ra_assists/src/handlers/generate_impl.rs index d9b87c9c0..7162dc184 100644 --- a/crates/ra_assists/src/handlers/generate_impl.rs +++ b/crates/ra_assists/src/handlers/generate_impl.rs @@ -1,5 +1,6 @@ +use itertools::Itertools; use ra_syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner}; -use stdx::{format_to, SepBy}; +use stdx::format_to; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -50,7 +51,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() .filter_map(|it| it.name()) .map(|it| it.text().clone()); - let generic_params = lifetime_params.chain(type_params).sep_by(", "); + let generic_params = lifetime_params.chain(type_params).format(", "); format_to!(buf, "<{}>", generic_params) } match ctx.config.snippet_cap { diff --git a/crates/ra_assists/src/handlers/generate_new.rs b/crates/ra_assists/src/handlers/generate_new.rs index b84aa24b6..32dfed274 100644 --- a/crates/ra_assists/src/handlers/generate_new.rs +++ b/crates/ra_assists/src/handlers/generate_new.rs @@ -1,9 +1,10 @@ use hir::Adt; +use itertools::Itertools; use ra_syntax::{ ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner}, T, }; -use stdx::{format_to, SepBy}; +use stdx::format_to; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -52,8 +53,8 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let params = field_list .fields() .filter_map(|f| Some(format!("{}: {}", f.name()?.syntax(), f.ty()?.syntax()))) - .sep_by(", "); - let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", "); + .format(", "); + let fields = field_list.fields().filter_map(|f| f.name()).format(", "); format_to!(buf, " {}fn new({}) -> Self {{ Self {{ {} }} }}", vis, params, fields); @@ -102,7 +103,7 @@ fn generate_impl_text(strukt: &ast::Struct, 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()); - format_to!(buf, "<{}>", lifetime_params.chain(type_params).sep_by(", ")) + format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", ")) } format_to!(buf, " {{\n{}\n}}\n", code); diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 9a94ff476..59f1b1424 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -2,8 +2,8 @@ //! It also handles scoring (sorting) completions. use hir::{Docs, HasAttrs, HasSource, HirDisplay, ModPath, ScopeDef, StructKind, Type}; +use itertools::Itertools; use ra_syntax::ast::NameOwner; -use stdx::SepBy; use test_utils::mark; use crate::{ @@ -289,16 +289,16 @@ impl Completions { .map(|field| (field.name(ctx.db), field.signature_ty(ctx.db))); let variant_kind = variant.kind(ctx.db); let detail = match variant_kind { - StructKind::Tuple | StructKind::Unit => detail_types - .map(|(_, t)| t.display(ctx.db).to_string()) - .sep_by(", ") - .surround_with("(", ")") - .to_string(), - StructKind::Record => detail_types - .map(|(n, t)| format!("{}: {}", n, t.display(ctx.db).to_string())) - .sep_by(", ") - .surround_with("{ ", " }") - .to_string(), + StructKind::Tuple | StructKind::Unit => format!( + "({})", + detail_types.map(|(_, t)| t.display(ctx.db).to_string()).format(", ") + ), + StructKind::Record => format!( + "{{ {} }}", + detail_types + .map(|(n, t)| format!("{}: {}", n, t.display(ctx.db).to_string())) + .format(", ") + ), }; let mut res = CompletionItem::new( CompletionKind::Reference, @@ -412,11 +412,10 @@ impl Builder { self = self.trigger_call_info(); let snippet = match (ctx.config.add_call_argument_snippets, params) { (true, Params::Named(params)) => { - let function_params_snippet = params - .iter() - .enumerate() - .map(|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name)) - .sep_by(", "); + let function_params_snippet = + params.iter().enumerate().format_with(", ", |(index, param_name), f| { + f(&format_args!("${{{}:{}}}", index + 1, param_name)) + }); format!("{}({})$0", name, function_params_snippet) } _ => { diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 8665b480f..6046643ef 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs @@ -4,8 +4,8 @@ use std::{collections::BTreeMap, convert::TryFrom}; use ast::{HasQuotes, HasStringValue}; use hir::Semantics; +use itertools::Itertools; use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; -use stdx::SepBy; use crate::{ call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, @@ -129,8 +129,7 @@ pub(super) fn extract_doc_comments( line[pos..].to_owned() }) - .sep_by("\n") - .to_string(); + .join("\n"); if doctest.is_empty() { return None; diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 3a56b1674..0bdc22d95 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -1,7 +1,7 @@ //! Various traits that are implemented by ast nodes. //! //! The implementations are usually trivial, and live in generated.rs -use stdx::SepBy; +use itertools::Itertools; use crate::{ ast::{self, support, AstChildren, AstNode, AstToken}, @@ -119,8 +119,7 @@ impl CommentIter { // of a line in markdown. line[pos..end].to_owned() }) - .sep_by("\n") - .to_string(); + .join("\n"); if has_comments { Some(docs) diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 00bfcd29e..3c5027fe5 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -1,5 +1,5 @@ //! Missing batteries for standard libraries. -use std::{cell::Cell, fmt, time::Instant}; +use std::time::Instant; mod macros; @@ -8,69 +8,6 @@ pub fn is_ci() -> bool { option_env!("CI").is_some() } -pub trait SepBy: Sized { - /// Returns an `impl fmt::Display`, which joins elements via a separator. - fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self>; -} - -impl SepBy for I -where - I: Iterator, - I::Item: fmt::Display, -{ - fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self> { - SepByBuilder::new(sep, self) - } -} - -pub struct SepByBuilder<'a, I> { - sep: &'a str, - prefix: &'a str, - suffix: &'a str, - iter: Cell>, -} - -impl<'a, I> SepByBuilder<'a, I> { - fn new(sep: &'a str, iter: I) -> SepByBuilder<'a, I> { - SepByBuilder { sep, prefix: "", suffix: "", iter: Cell::new(Some(iter)) } - } - - pub fn prefix(mut self, prefix: &'a str) -> Self { - self.prefix = prefix; - self - } - - pub fn suffix(mut self, suffix: &'a str) -> Self { - self.suffix = suffix; - self - } - - /// Set both suffix and prefix. - pub fn surround_with(self, prefix: &'a str, suffix: &'a str) -> Self { - self.prefix(prefix).suffix(suffix) - } -} - -impl fmt::Display for SepByBuilder<'_, I> -where - I: Iterator, - I::Item: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.prefix)?; - let mut first = true; - for item in self.iter.take().unwrap() { - if !first { - f.write_str(self.sep)?; - } - first = false; - fmt::Display::fmt(&item, f)?; - } - f.write_str(self.suffix)?; - Ok(()) - } -} - #[must_use] pub fn timeit(label: &'static str) -> impl Drop { struct Guard { -- cgit v1.2.3