Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22307 from Shourya742/2026-05-07-remove-ted-based-indent-operation
Remove ted based indent operation
| -rw-r--r-- | crates/syntax/src/ast/edit.rs | 41 | ||||
| -rw-r--r-- | crates/syntax/src/ast/edit_in_place.rs | 53 |
2 files changed, 1 insertions, 93 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 1e898844ae..2e3a4016ee 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -14,7 +14,6 @@ use crate::{ SyntaxNode, SyntaxToken, ast::{self, AstNode, HasName, make}, syntax_editor::{Position, SyntaxEditor, SyntaxMappingBuilder}, - ted, }; use super::syntax_factory::SyntaxFactory; @@ -88,29 +87,6 @@ impl IndentLevel { IndentLevel(0) } - /// XXX: this intentionally doesn't change the indent of the very first token. - /// For example, in something like: - /// ``` - /// fn foo() -> i32 { - /// 92 - /// } - /// ``` - /// if you indent the block, the `{` token would stay put. - pub(super) fn increase_indent(self, node: &SyntaxNode) { - let tokens = node.preorder_with_tokens().filter_map(|event| match event { - rowan::WalkEvent::Leave(NodeOrToken::Token(it)) => Some(it), - _ => None, - }); - for token in tokens { - if let Some(ws) = ast::Whitespace::cast(token) - && ws.text().contains('\n') - { - let new_ws = make::tokens::whitespace(&format!("{}{self}", ws.syntax())); - ted::replace(ws.syntax(), &new_ws); - } - } - } - pub(super) fn clone_increase_indent(self, node: &SyntaxNode) -> SyntaxNode { let (editor, node) = SyntaxEditor::new(node.clone()); let tokens = node @@ -128,23 +104,6 @@ impl IndentLevel { editor.finish().new_root().clone() } - pub(super) fn decrease_indent(self, node: &SyntaxNode) { - let tokens = node.preorder_with_tokens().filter_map(|event| match event { - rowan::WalkEvent::Leave(NodeOrToken::Token(it)) => Some(it), - _ => None, - }); - for token in tokens { - if let Some(ws) = ast::Whitespace::cast(token) - && ws.text().contains('\n') - { - let new_ws = make::tokens::whitespace( - &ws.syntax().text().replace(&format!("\n{self}"), "\n"), - ); - ted::replace(ws.syntax(), &new_ws); - } - } - } - pub(super) fn clone_decrease_indent(self, node: &SyntaxNode) -> SyntaxNode { let (editor, node) = SyntaxEditor::new(node.clone()); let tokens = node diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 68e4f956c2..487557c0de 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -7,7 +7,7 @@ use parser::T; use crate::{ AstNode, AstToken, Direction, algo::{self, neighbor}, - ast::{self, edit::IndentLevel, make, syntax_factory::SyntaxFactory}, + ast::{self, make, syntax_factory::SyntaxFactory}, syntax_editor::SyntaxEditor, ted, }; @@ -262,54 +262,3 @@ impl ast::RecordExprField { } } } - -pub trait Indent: AstNode + Clone + Sized { - fn indent_level(&self) -> IndentLevel { - IndentLevel::from_node(self.syntax()) - } - fn indent(&self, by: IndentLevel) { - by.increase_indent(self.syntax()); - } - fn dedent(&self, by: IndentLevel) { - by.decrease_indent(self.syntax()); - } - fn reindent_to(&self, target_level: IndentLevel) { - let current_level = IndentLevel::from_node(self.syntax()); - self.dedent(current_level); - self.indent(target_level); - } -} - -impl<N: AstNode + Clone> Indent for N {} - -#[cfg(test)] -mod tests { - use parser::Edition; - - use crate::SourceFile; - - use super::*; - - fn ast_mut_from_text<N: AstNode>(text: &str) -> N { - let parse = SourceFile::parse(text, Edition::CURRENT); - parse.tree().syntax().descendants().find_map(N::cast).unwrap().clone_for_update() - } - - #[test] - fn test_increase_indent() { - let arm_list = ast_mut_from_text::<ast::Fn>( - "fn foo() { - ; - ; -}", - ); - arm_list.indent(IndentLevel(2)); - assert_eq!( - arm_list.to_string(), - "fn foo() { - ; - ; - }", - ); - } -} |