mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +00:00
align SyntaxText API with upstream
This commit is contained in:
parent
6b352ffeb3
commit
f6bcc2d745
9 changed files with 18 additions and 24 deletions
|
@ -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<impl HirDatabase>) -> Option<Assist> {
|
|||
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());
|
||||
|
|
|
@ -95,7 +95,7 @@ impl AstEditor<ast::NamedFieldList> {
|
|||
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!(
|
||||
|
|
|
@ -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<impl HirDatabase>) -> 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())
|
||||
|
|
|
@ -36,7 +36,7 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
|
|||
|
||||
pub fn extract_trivial_expression(block: &ast::Block) -> Option<ast::Expr> {
|
||||
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() {
|
||||
|
|
|
@ -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<T>(node: &T, label: &str) -> Option<String>
|
|||
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)
|
||||
|
|
|
@ -31,7 +31,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
|
|||
// 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 {
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -75,7 +75,7 @@ pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> {
|
|||
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 {
|
||||
|
|
|
@ -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<TextUnit> {
|
||||
pub fn find_char(&self, c: char) -> Option<TextUnit> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue