From e84f93cb5b651696637d87b98653d7e8f9149086 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Thu, 22 Aug 2019 21:31:21 +0300 Subject: refactor fill_match_arms assist --- crates/ra_assists/src/ast_editor.rs | 93 ++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src/ast_editor.rs') diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 95b871b30..5b6952426 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -1,6 +1,8 @@ use std::{iter, ops::RangeInclusive}; use arrayvec::ArrayVec; +use itertools::Itertools; + use hir::Name; use ra_fmt::leading_indent; use ra_syntax::{ @@ -168,8 +170,7 @@ impl AstEditor { impl AstEditor { pub fn append_items(&mut self, items: impl Iterator) { - let n_existing_items = self.ast().impl_items().count(); - if n_existing_items == 0 { + if !self.ast().syntax().text().contains_char('\n') { self.do_make_multiline(); } items.for_each(|it| self.append_item(it)); @@ -288,6 +289,94 @@ impl AstBuilder { } } +impl AstBuilder { + fn from_text(text: &str) -> ast::Path { + ast_node_from_file_text(text) + } + + pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { + Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::BindPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } + + pub fn from_name(name: &ast::Name) -> ast::BindPat { + Self::from_text(name.text()) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::PlaceholderPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } + + pub fn placeholder() -> ast::PlaceholderPat { + Self::from_text("_") + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::TupleStructPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } + + pub fn from_pieces( + path: &ast::Path, + pats: impl Iterator, + ) -> ast::TupleStructPat { + let pats_str = pats.map(|p| p.syntax().to_string()).collect::>().join(", "); + Self::from_text(&format!("{}({})", path.syntax(), pats_str)) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::StructPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } + + pub fn from_pieces(path: &ast::Path, pats: impl Iterator) -> ast::StructPat { + let pats_str = pats.map(|p| p.syntax().to_string()).collect::>().join(", "); + Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::PathPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } + + pub fn from_path(path: &ast::Path) -> ast::PathPat { + let path_str = path.syntax().text().to_string(); + Self::from_text(path_str.as_str()) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::MatchArm { + ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) + } + + pub fn from_pieces(pats: impl Iterator, expr: &ast::Expr) -> ast::MatchArm { + let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); + Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) + } +} + +impl AstBuilder { + fn from_text(text: &str) -> ast::MatchArmList { + ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) + } + + pub fn from_arms(arms: impl Iterator) -> ast::MatchArmList { + let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); + Self::from_text(&format!("{},\n", arms_str)) + } +} + fn ast_node_from_file_text(text: &str) -> N { let parse = SourceFile::parse(text); let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned(); -- cgit v1.2.3