aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs44
-rw-r--r--crates/ra_syntax/src/ast/make.rs25
2 files changed, 46 insertions, 23 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 24a1e1d91..29eb3fcb9 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -1,7 +1,10 @@
1//! This module contains functions for editing syntax trees. As the trees are 1//! This module contains functions for editing syntax trees. As the trees are
2//! immutable, all function here return a fresh copy of the tree, instead of 2//! immutable, all function here return a fresh copy of the tree, instead of
3//! doing an in-place modification. 3//! doing an in-place modification.
4use std::{iter, ops::RangeInclusive}; 4use std::{
5 fmt, iter,
6 ops::{self, RangeInclusive},
7};
5 8
6use arrayvec::ArrayVec; 9use arrayvec::ArrayVec;
7 10
@@ -437,6 +440,28 @@ impl From<u8> for IndentLevel {
437 } 440 }
438} 441}
439 442
443impl fmt::Display for IndentLevel {
444 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445 let spaces = " ";
446 let buf;
447 let len = self.0 as usize * 4;
448 let indent = if len <= spaces.len() {
449 &spaces[..len]
450 } else {
451 buf = iter::repeat(' ').take(len).collect::<String>();
452 &buf
453 };
454 fmt::Display::fmt(indent, f)
455 }
456}
457
458impl ops::Add<u8> for IndentLevel {
459 type Output = IndentLevel;
460 fn add(self, rhs: u8) -> IndentLevel {
461 IndentLevel(self.0 + rhs)
462 }
463}
464
440impl IndentLevel { 465impl IndentLevel {
441 pub fn from_node(node: &SyntaxNode) -> IndentLevel { 466 pub fn from_node(node: &SyntaxNode) -> IndentLevel {
442 let first_token = match node.first_token() { 467 let first_token = match node.first_token() {
@@ -453,6 +478,14 @@ impl IndentLevel {
453 IndentLevel(0) 478 IndentLevel(0)
454 } 479 }
455 480
481 /// XXX: this intentionally doesn't change the indent of the very first token.
482 /// Ie, in something like
483 /// ```
484 /// fn foo() {
485 /// 92
486 /// }
487 /// ```
488 /// if you indent the block, the `{` token would stay put.
456 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode { 489 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
457 let mut rewriter = SyntaxRewriter::default(); 490 let mut rewriter = SyntaxRewriter::default();
458 node.descendants_with_tokens() 491 node.descendants_with_tokens()
@@ -463,12 +496,7 @@ impl IndentLevel {
463 text.contains('\n') 496 text.contains('\n')
464 }) 497 })
465 .for_each(|ws| { 498 .for_each(|ws| {
466 let new_ws = make::tokens::whitespace(&format!( 499 let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,));
467 "{}{:width$}",
468 ws.syntax().text(),
469 "",
470 width = self.0 as usize * 4
471 ));
472 rewriter.replace(ws.syntax(), &new_ws) 500 rewriter.replace(ws.syntax(), &new_ws)
473 }); 501 });
474 rewriter.rewrite(&node) 502 rewriter.rewrite(&node)
@@ -485,7 +513,7 @@ impl IndentLevel {
485 }) 513 })
486 .for_each(|ws| { 514 .for_each(|ws| {
487 let new_ws = make::tokens::whitespace( 515 let new_ws = make::tokens::whitespace(
488 &ws.syntax().text().replace(&format!("\n{:1$}", "", self.0 as usize * 4), "\n"), 516 &ws.syntax().text().replace(&format!("\n{}", self), "\n"),
489 ); 517 );
490 rewriter.replace(ws.syntax(), &new_ws) 518 rewriter.replace(ws.syntax(), &new_ws)
491 }); 519 });
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index 2db017038..da0eb0926 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -280,7 +280,12 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
280 ast_from_text(&format!("fn f({}) {{ }}", args)) 280 ast_from_text(&format!("fn f({}) {{ }}", args))
281} 281}
282 282
283pub fn visibility_pub_crate() -> ast::Visibility {
284 ast_from_text("pub(crate) struct S")
285}
286
283pub fn fn_def( 287pub fn fn_def(
288 visibility: Option<ast::Visibility>,
284 fn_name: ast::Name, 289 fn_name: ast::Name,
285 type_params: Option<ast::TypeParamList>, 290 type_params: Option<ast::TypeParamList>,
286 params: ast::ParamList, 291 params: ast::ParamList,
@@ -288,21 +293,11 @@ pub fn fn_def(
288) -> ast::FnDef { 293) -> ast::FnDef {
289 let type_params = 294 let type_params =
290 if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; 295 if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
291 ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body)) 296 let visibility = match visibility {
292} 297 None => String::new(),
293 298 Some(it) => format!("{} ", it),
294pub fn add_leading_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile { 299 };
295 let newlines = "\n".repeat(amount_of_newlines); 300 ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body))
296 ast_from_text(&format!("{}{}", newlines, t.syntax()))
297}
298
299pub fn add_trailing_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile {
300 let newlines = "\n".repeat(amount_of_newlines);
301 ast_from_text(&format!("{}{}", t.syntax(), newlines))
302}
303
304pub fn add_pub_crate_modifier(fn_def: ast::FnDef) -> ast::FnDef {
305 ast_from_text(&format!("pub(crate) {}", fn_def))
306} 301}
307 302
308fn ast_from_text<N: AstNode>(text: &str) -> N { 303fn ast_from_text<N: AstNode>(text: &str) -> N {