mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
simplify
This commit is contained in:
parent
69eeae0c99
commit
ba02a55330
2 changed files with 8 additions and 35 deletions
|
@ -1,7 +1,3 @@
|
||||||
use std::{
|
|
||||||
fmt::{self, Write},
|
|
||||||
};
|
|
||||||
|
|
||||||
use join_to_string::join;
|
use join_to_string::join;
|
||||||
|
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
|
@ -78,17 +74,15 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
buf.push_str("\n\nimpl");
|
buf.push_str("\n\nimpl");
|
||||||
if let Some(type_params) = type_params {
|
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(" ");
|
||||||
buf.push_str(name.text().as_str());
|
buf.push_str(name.text().as_str());
|
||||||
if let Some(type_params) = type_params {
|
if let Some(type_params) = type_params {
|
||||||
comma_list(
|
join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
|
||||||
&mut buf, "<", ">",
|
.surround_with("<", ">")
|
||||||
type_params.type_params()
|
.to_buf(&mut buf);
|
||||||
.filter_map(|it| it.name())
|
|
||||||
.map(|it| it.text())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
buf.push_str(" {\n");
|
buf.push_str(" {\n");
|
||||||
let offset = start_offset + TextUnit::of_str(&buf);
|
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())
|
.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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -31,6 +31,9 @@ impl<'a> SyntaxText<'a> {
|
||||||
Some(&text[range])
|
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 {
|
pub fn to_string(&self) -> String {
|
||||||
self.chunks().collect()
|
self.chunks().collect()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue