From ba02a55330d2ec9a0ea2c5cd457b82782ae299e9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 22:58:02 +0300 Subject: simplify --- crates/libeditor/src/code_actions.rs | 40 ++++------------------------- crates/libsyntax2/src/yellow/syntax_text.rs | 3 +++ 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index cef3a12f9..684b8243a 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs @@ -1,7 +1,3 @@ -use std::{ - fmt::{self, Write}, -}; - use join_to_string::join; use libsyntax2::{ @@ -78,17 +74,15 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option let mut buf = String::new(); buf.push_str("\n\nimpl"); if let Some(type_params) = type_params { - buf.push_display(&type_params.syntax().text()); + type_params.syntax().text() + .push_to(&mut buf); } buf.push_str(" "); buf.push_str(name.text().as_str()); if let Some(type_params) = type_params { - comma_list( - &mut buf, "<", ">", - type_params.type_params() - .filter_map(|it| it.name()) - .map(|it| it.text()) - ); + join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) + .surround_with("<", ">") + .to_buf(&mut buf); } buf.push_str(" {\n"); let offset = start_offset + TextUnit::of_str(&buf); @@ -107,30 +101,6 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option) { - buf.push_str(bra); - let mut first = true; - for item in items { - if !first { - buf.push_str(", "); - } - first = false; - write!(buf, "{}", item).unwrap(); - } - buf.push_str(ket); -} - -trait PushDisplay { - fn push_display(&mut self, item: &T); -} - -impl PushDisplay for String { - fn push_display(&mut self, item: &T) { - use std::fmt::Write; - write!(self, "{}", item).unwrap() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/libsyntax2/src/yellow/syntax_text.rs b/crates/libsyntax2/src/yellow/syntax_text.rs index d299fd700..b855687c4 100644 --- a/crates/libsyntax2/src/yellow/syntax_text.rs +++ b/crates/libsyntax2/src/yellow/syntax_text.rs @@ -31,6 +31,9 @@ impl<'a> SyntaxText<'a> { Some(&text[range]) }) } + pub fn push_to(&self, buf: &mut String) { + self.chunks().for_each(|it| buf.push_str(it)); + } pub fn to_string(&self) -> String { self.chunks().collect() } -- cgit v1.2.3