aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-28 20:58:02 +0100
committerAleksey Kladov <[email protected]>2018-08-28 20:58:02 +0100
commitba02a55330d2ec9a0ea2c5cd457b82782ae299e9 (patch)
tree99f5fb731559f15428eb7ddc2273206a921e311f /crates/libeditor
parent69eeae0c9903931f13038f4e350fd53b1903530d (diff)
simplify
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/src/code_actions.rs40
1 files changed, 5 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 @@
1use std::{
2 fmt::{self, Write},
3};
4
5use join_to_string::join; 1use join_to_string::join;
6 2
7use libsyntax2::{ 3use libsyntax2::{
@@ -78,17 +74,15 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
78 let mut buf = String::new(); 74 let mut buf = String::new();
79 buf.push_str("\n\nimpl"); 75 buf.push_str("\n\nimpl");
80 if let Some(type_params) = type_params { 76 if let Some(type_params) = type_params {
81 buf.push_display(&type_params.syntax().text()); 77 type_params.syntax().text()
78 .push_to(&mut buf);
82 } 79 }
83 buf.push_str(" "); 80 buf.push_str(" ");
84 buf.push_str(name.text().as_str()); 81 buf.push_str(name.text().as_str());
85 if let Some(type_params) = type_params { 82 if let Some(type_params) = type_params {
86 comma_list( 83 join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()))
87 &mut buf, "<", ">", 84 .surround_with("<", ">")
88 type_params.type_params() 85 .to_buf(&mut buf);
89 .filter_map(|it| it.name())
90 .map(|it| it.text())
91 );
92 } 86 }
93 buf.push_str(" {\n"); 87 buf.push_str(" {\n");
94 let offset = start_offset + TextUnit::of_str(&buf); 88 let offset = start_offset + TextUnit::of_str(&buf);
@@ -107,30 +101,6 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<Synta
107 .find(|node| !node.kind().is_trivia()) 101 .find(|node| !node.kind().is_trivia())
108} 102}
109 103
110fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator<Item=impl fmt::Display>) {
111 buf.push_str(bra);
112 let mut first = true;
113 for item in items {
114 if !first {
115 buf.push_str(", ");
116 }
117 first = false;
118 write!(buf, "{}", item).unwrap();
119 }
120 buf.push_str(ket);
121}
122
123trait PushDisplay {
124 fn push_display<T: fmt::Display>(&mut self, item: &T);
125}
126
127impl PushDisplay for String {
128 fn push_display<T: fmt::Display>(&mut self, item: &T) {
129 use std::fmt::Write;
130 write!(self, "{}", item).unwrap()
131 }
132}
133
134#[cfg(test)] 104#[cfg(test)]
135mod tests { 105mod tests {
136 use super::*; 106 use super::*;