This commit is contained in:
Aleksey Kladov 2018-08-28 22:58:02 +03:00
parent 69eeae0c99
commit ba02a55330
2 changed files with 8 additions and 35 deletions

View File

@ -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<impl FnOnce() ->
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<Synta
.find(|node| !node.kind().is_trivia())
}
fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator<Item=impl fmt::Display>) {
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<T: fmt::Display>(&mut self, item: &T);
}
impl PushDisplay for String {
fn push_display<T: fmt::Display>(&mut self, item: &T) {
use std::fmt::Write;
write!(self, "{}", item).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -31,6 +31,9 @@ pub fn chunks(&self) -> impl Iterator<Item=&'a str> {
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()
}