diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs index 9a0cfb4e7f..59ca88468b 100644 --- a/crates/ra_assists/src/add_impl.rs +++ b/crates/ra_assists/src/add_impl.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use hir::db::HirDatabase; use join_to_string::join; use ra_syntax::{ @@ -17,7 +19,7 @@ pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { let mut buf = String::new(); buf.push_str("\n\nimpl"); if let Some(type_params) = &type_params { - type_params.syntax().text().push_to(&mut buf); + write!(buf, "{}", type_params.syntax()).unwrap(); } buf.push_str(" "); buf.push_str(name.text().as_str()); diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 36235eb137..ab6c347ad9 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -95,7 +95,7 @@ impl AstEditor { position: InsertPosition<&'_ ast::NamedField>, field: &ast::NamedField, ) { - let is_multiline = self.ast().syntax().text().contains('\n'); + let is_multiline = self.ast().syntax().text().contains_char('\n'); let ws; let space = if is_multiline { ws = tokens::WsBuilder::new(&format!( diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index df6c589899..911de2d48c 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, @@ -35,8 +37,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option buf.push_str("let var_name = "); TextUnit::of_str("let ") }; - - expr.syntax().text().push_to(&mut buf); + write!(buf, "{}", expr.syntax()).unwrap(); let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); let is_full_stmt = if let Some(expr_stmt) = &full_stmt { Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs index d6e8957293..b09478d7a3 100644 --- a/crates/ra_fmt/src/lib.rs +++ b/crates/ra_fmt/src/lib.rs @@ -36,7 +36,7 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator { pub fn extract_trivial_expression(block: &ast::Block) -> Option { let expr = block.expr()?; - if expr.syntax().text().contains('\n') { + if expr.syntax().text().contains_char('\n') { return None; } let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { diff --git a/crates/ra_ide_api/src/display/short_label.rs b/crates/ra_ide_api/src/display/short_label.rs index f926f631fc..be499e485b 100644 --- a/crates/ra_ide_api/src/display/short_label.rs +++ b/crates/ra_ide_api/src/display/short_label.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use ra_syntax::ast::{self, AstNode, NameOwner, TypeAscriptionOwner, VisibilityOwner}; pub(crate) trait ShortLabel { @@ -71,8 +73,7 @@ where let mut buf = short_label_from_node(node, prefix)?; if let Some(type_ref) = node.ascribed_type() { - buf.push_str(": "); - type_ref.syntax().text().push_to(&mut buf); + write!(buf, ": {}", type_ref.syntax()).unwrap(); } Some(buf) @@ -82,7 +83,7 @@ fn short_label_from_node(node: &T, label: &str) -> Option where T: NameOwner + VisibilityOwner, { - let mut buf = node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); + let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default(); buf.push_str(label); buf.push_str(node.name()?.text().as_str()); Some(buf) diff --git a/crates/ra_ide_api/src/folding_ranges.rs b/crates/ra_ide_api/src/folding_ranges.rs index 9699000dbb..571d1c5958 100644 --- a/crates/ra_ide_api/src/folding_ranges.rs +++ b/crates/ra_ide_api/src/folding_ranges.rs @@ -31,7 +31,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec { // Fold items that span multiple lines if let Some(kind) = fold_kind(element.kind()) { let is_multiline = match &element { - SyntaxElement::Node(node) => node.text().contains('\n'), + SyntaxElement::Node(node) => node.text().contains_char('\n'), SyntaxElement::Token(token) => token.text().contains('\n'), }; if is_multiline { diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs index fa998ebe1e..7f25f21081 100644 --- a/crates/ra_ide_api/src/join_lines.rs +++ b/crates/ra_ide_api/src/join_lines.rs @@ -13,7 +13,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { let range = if range.is_empty() { let syntax = file.syntax(); let text = syntax.text().slice(range.start()..); - let pos = match text.find('\n') { + let pos = match text.find_char('\n') { None => return TextEditBuilder::default().finish(), Some(pos) => pos, }; diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index 1747a529ec..5a1cbcc498 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -75,7 +75,7 @@ pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option { if expr_range.contains(eq_offset) && eq_offset != expr_range.start() { return None; } - if file.syntax().text().slice(eq_offset..expr_range.start()).contains('\n') { + if file.syntax().text().slice(eq_offset..expr_range.start()).contains_char('\n') { return None; } } else { diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index f8ddff48e9..652cb7a1e0 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs @@ -57,25 +57,15 @@ impl SyntaxText { } } - pub fn push_to(&self, buf: &mut String) { - self.for_each_chunk(|chunk| buf.push_str(chunk)) - } - - pub fn to_string(&self) -> String { - let mut buf = String::new(); - self.push_to(&mut buf); - buf - } - pub fn to_smol_string(&self) -> SmolStr { self.to_string().into() } - pub fn contains(&self, c: char) -> bool { + pub fn contains_char(&self, c: char) -> bool { self.try_for_each_chunk(|chunk| if chunk.contains(c) { Err(()) } else { Ok(()) }).is_err() } - pub fn find(&self, c: char) -> Option { + pub fn find_char(&self, c: char) -> Option { let mut acc: TextUnit = 0.into(); let res = self.try_for_each_chunk(|chunk| { if let Some(pos) = chunk.find(c) { @@ -158,7 +148,7 @@ impl fmt::Debug for SyntaxText { impl fmt::Display for SyntaxText { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(&self.to_string(), f) + self.try_for_each_chunk(|chunk| fmt::Display::fmt(chunk, f)) } }