aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/libeditor/src/code_actions.rs40
-rw-r--r--crates/libsyntax2/src/yellow/syntax_text.rs3
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 @@
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::*;
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> {
31 Some(&text[range]) 31 Some(&text[range])
32 }) 32 })
33 } 33 }
34 pub fn push_to(&self, buf: &mut String) {
35 self.chunks().for_each(|it| buf.push_str(it));
36 }
34 pub fn to_string(&self) -> String { 37 pub fn to_string(&self) -> String {
35 self.chunks().collect() 38 self.chunks().collect()
36 } 39 }