mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	align SyntaxText API with upstream
This commit is contained in:
		
							parent
							
								
									6b352ffeb3
								
							
						
					
					
						commit
						f6bcc2d745
					
				@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					use std::fmt::Write;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use hir::db::HirDatabase;
 | 
					use hir::db::HirDatabase;
 | 
				
			||||||
use join_to_string::join;
 | 
					use join_to_string::join;
 | 
				
			||||||
use ra_syntax::{
 | 
					use ra_syntax::{
 | 
				
			||||||
@ -17,7 +19,7 @@ pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
 | 
				
			|||||||
        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 {
 | 
				
			||||||
            type_params.syntax().text().push_to(&mut buf);
 | 
					            write!(buf, "{}", type_params.syntax()).unwrap();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        buf.push_str(" ");
 | 
					        buf.push_str(" ");
 | 
				
			||||||
        buf.push_str(name.text().as_str());
 | 
					        buf.push_str(name.text().as_str());
 | 
				
			||||||
 | 
				
			|||||||
@ -95,7 +95,7 @@ impl AstEditor<ast::NamedFieldList> {
 | 
				
			|||||||
        position: InsertPosition<&'_ ast::NamedField>,
 | 
					        position: InsertPosition<&'_ ast::NamedField>,
 | 
				
			||||||
        field: &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 ws;
 | 
				
			||||||
        let space = if is_multiline {
 | 
					        let space = if is_multiline {
 | 
				
			||||||
            ws = tokens::WsBuilder::new(&format!(
 | 
					            ws = tokens::WsBuilder::new(&format!(
 | 
				
			||||||
 | 
				
			|||||||
@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					use std::fmt::Write;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use hir::db::HirDatabase;
 | 
					use hir::db::HirDatabase;
 | 
				
			||||||
use ra_syntax::{
 | 
					use ra_syntax::{
 | 
				
			||||||
    ast::{self, AstNode},
 | 
					    ast::{self, AstNode},
 | 
				
			||||||
@ -35,8 +37,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
 | 
				
			|||||||
            buf.push_str("let var_name = ");
 | 
					            buf.push_str("let var_name = ");
 | 
				
			||||||
            TextUnit::of_str("let ")
 | 
					            TextUnit::of_str("let ")
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					        write!(buf, "{}", expr.syntax()).unwrap();
 | 
				
			||||||
        expr.syntax().text().push_to(&mut buf);
 | 
					 | 
				
			||||||
        let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());
 | 
					        let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());
 | 
				
			||||||
        let is_full_stmt = if let Some(expr_stmt) = &full_stmt {
 | 
					        let is_full_stmt = if let Some(expr_stmt) = &full_stmt {
 | 
				
			||||||
            Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone())
 | 
					            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> {
 | 
					pub fn extract_trivial_expression(block: &ast::Block) -> Option<ast::Expr> {
 | 
				
			||||||
    let expr = block.expr()?;
 | 
					    let expr = block.expr()?;
 | 
				
			||||||
    if expr.syntax().text().contains('\n') {
 | 
					    if expr.syntax().text().contains_char('\n') {
 | 
				
			||||||
        return None;
 | 
					        return None;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
 | 
					    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};
 | 
					use ra_syntax::ast::{self, AstNode, NameOwner, TypeAscriptionOwner, VisibilityOwner};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub(crate) trait ShortLabel {
 | 
					pub(crate) trait ShortLabel {
 | 
				
			||||||
@ -71,8 +73,7 @@ where
 | 
				
			|||||||
    let mut buf = short_label_from_node(node, prefix)?;
 | 
					    let mut buf = short_label_from_node(node, prefix)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if let Some(type_ref) = node.ascribed_type() {
 | 
					    if let Some(type_ref) = node.ascribed_type() {
 | 
				
			||||||
        buf.push_str(": ");
 | 
					        write!(buf, ": {}", type_ref.syntax()).unwrap();
 | 
				
			||||||
        type_ref.syntax().text().push_to(&mut buf);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Some(buf)
 | 
					    Some(buf)
 | 
				
			||||||
@ -82,7 +83,7 @@ fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
 | 
				
			|||||||
where
 | 
					where
 | 
				
			||||||
    T: NameOwner + VisibilityOwner,
 | 
					    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(label);
 | 
				
			||||||
    buf.push_str(node.name()?.text().as_str());
 | 
					    buf.push_str(node.name()?.text().as_str());
 | 
				
			||||||
    Some(buf)
 | 
					    Some(buf)
 | 
				
			||||||
 | 
				
			|||||||
@ -31,7 +31,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
 | 
				
			|||||||
        // Fold items that span multiple lines
 | 
					        // Fold items that span multiple lines
 | 
				
			||||||
        if let Some(kind) = fold_kind(element.kind()) {
 | 
					        if let Some(kind) = fold_kind(element.kind()) {
 | 
				
			||||||
            let is_multiline = match &element {
 | 
					            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'),
 | 
					                SyntaxElement::Token(token) => token.text().contains('\n'),
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            if is_multiline {
 | 
					            if is_multiline {
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit {
 | 
				
			|||||||
    let range = if range.is_empty() {
 | 
					    let range = if range.is_empty() {
 | 
				
			||||||
        let syntax = file.syntax();
 | 
					        let syntax = file.syntax();
 | 
				
			||||||
        let text = syntax.text().slice(range.start()..);
 | 
					        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(),
 | 
					            None => return TextEditBuilder::default().finish(),
 | 
				
			||||||
            Some(pos) => pos,
 | 
					            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() {
 | 
					        if expr_range.contains(eq_offset) && eq_offset != expr_range.start() {
 | 
				
			||||||
            return None;
 | 
					            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;
 | 
					            return None;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } else {
 | 
					    } 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 {
 | 
					    pub fn to_smol_string(&self) -> SmolStr {
 | 
				
			||||||
        self.to_string().into()
 | 
					        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()
 | 
					        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 mut acc: TextUnit = 0.into();
 | 
				
			||||||
        let res = self.try_for_each_chunk(|chunk| {
 | 
					        let res = self.try_for_each_chunk(|chunk| {
 | 
				
			||||||
            if let Some(pos) = chunk.find(c) {
 | 
					            if let Some(pos) = chunk.find(c) {
 | 
				
			||||||
@ -158,7 +148,7 @@ impl fmt::Debug for SyntaxText {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
impl fmt::Display for SyntaxText {
 | 
					impl fmt::Display for SyntaxText {
 | 
				
			||||||
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
					    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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user