Skip to content

Commit e055cfa

Browse files
bors[bot]viorina
andauthored
Merge #1724
1724: Refactor fill_match_arms assist to use AstBuilder facilities r=matklad a=viorina Co-authored-by: Ekaterina Babshukova <[email protected]>
2 parents 0c35d82 + e84f93c commit e055cfa

File tree

3 files changed

+170
-132
lines changed

3 files changed

+170
-132
lines changed

crates/ra_assists/src/add_missing_impl_members.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
use hir::{db::HirDatabase, HasSource};
2+
use ra_syntax::{
3+
ast::{self, AstNode, NameOwner},
4+
SmolStr,
5+
};
6+
17
use crate::{
28
ast_editor::{AstBuilder, AstEditor},
39
Assist, AssistCtx, AssistId,
410
};
511

6-
use hir::{db::HirDatabase, HasSource};
7-
use ra_db::FilePosition;
8-
use ra_syntax::ast::{self, AstNode, NameOwner};
9-
use ra_syntax::SmolStr;
10-
1112
#[derive(PartialEq)]
1213
enum AddMissingImplMembersMode {
1314
DefaultMethodsOnly,
@@ -43,8 +44,7 @@ fn add_missing_impl_members_inner(
4344

4445
let trait_def = {
4546
let file_id = ctx.frange.file_id;
46-
let position = FilePosition { file_id, offset: impl_node.syntax().text_range().start() };
47-
let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None);
47+
let analyzer = hir::SourceAnalyzer::new(ctx.db, file_id, impl_node.syntax(), None);
4848

4949
resolve_target_trait_def(ctx.db, &analyzer, &impl_node)?
5050
};

crates/ra_assists/src/ast_editor.rs

+91-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::{iter, ops::RangeInclusive};
22

33
use arrayvec::ArrayVec;
4+
use itertools::Itertools;
5+
46
use hir::Name;
57
use ra_fmt::leading_indent;
68
use ra_syntax::{
@@ -168,8 +170,7 @@ impl AstEditor<ast::NamedFieldList> {
168170

169171
impl AstEditor<ast::ItemList> {
170172
pub fn append_items(&mut self, items: impl Iterator<Item = ast::ImplItem>) {
171-
let n_existing_items = self.ast().impl_items().count();
172-
if n_existing_items == 0 {
173+
if !self.ast().syntax().text().contains_char('\n') {
173174
self.do_make_multiline();
174175
}
175176
items.for_each(|it| self.append_item(it));
@@ -288,6 +289,94 @@ impl AstBuilder<ast::NameRef> {
288289
}
289290
}
290291

292+
impl AstBuilder<ast::Path> {
293+
fn from_text(text: &str) -> ast::Path {
294+
ast_node_from_file_text(text)
295+
}
296+
297+
pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
298+
Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
299+
}
300+
}
301+
302+
impl AstBuilder<ast::BindPat> {
303+
fn from_text(text: &str) -> ast::BindPat {
304+
ast_node_from_file_text(&format!("fn f({}: ())", text))
305+
}
306+
307+
pub fn from_name(name: &ast::Name) -> ast::BindPat {
308+
Self::from_text(name.text())
309+
}
310+
}
311+
312+
impl AstBuilder<ast::PlaceholderPat> {
313+
fn from_text(text: &str) -> ast::PlaceholderPat {
314+
ast_node_from_file_text(&format!("fn f({}: ())", text))
315+
}
316+
317+
pub fn placeholder() -> ast::PlaceholderPat {
318+
Self::from_text("_")
319+
}
320+
}
321+
322+
impl AstBuilder<ast::TupleStructPat> {
323+
fn from_text(text: &str) -> ast::TupleStructPat {
324+
ast_node_from_file_text(&format!("fn f({}: ())", text))
325+
}
326+
327+
pub fn from_pieces(
328+
path: &ast::Path,
329+
pats: impl Iterator<Item = ast::Pat>,
330+
) -> ast::TupleStructPat {
331+
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
332+
Self::from_text(&format!("{}({})", path.syntax(), pats_str))
333+
}
334+
}
335+
336+
impl AstBuilder<ast::StructPat> {
337+
fn from_text(text: &str) -> ast::StructPat {
338+
ast_node_from_file_text(&format!("fn f({}: ())", text))
339+
}
340+
341+
pub fn from_pieces(path: &ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::StructPat {
342+
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
343+
Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
344+
}
345+
}
346+
347+
impl AstBuilder<ast::PathPat> {
348+
fn from_text(text: &str) -> ast::PathPat {
349+
ast_node_from_file_text(&format!("fn f({}: ())", text))
350+
}
351+
352+
pub fn from_path(path: &ast::Path) -> ast::PathPat {
353+
let path_str = path.syntax().text().to_string();
354+
Self::from_text(path_str.as_str())
355+
}
356+
}
357+
358+
impl AstBuilder<ast::MatchArm> {
359+
fn from_text(text: &str) -> ast::MatchArm {
360+
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
361+
}
362+
363+
pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm {
364+
let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
365+
Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
366+
}
367+
}
368+
369+
impl AstBuilder<ast::MatchArmList> {
370+
fn from_text(text: &str) -> ast::MatchArmList {
371+
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
372+
}
373+
374+
pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
375+
let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
376+
Self::from_text(&format!("{},\n", arms_str))
377+
}
378+
}
379+
291380
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
292381
let parse = SourceFile::parse(text);
293382
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned();

0 commit comments

Comments
 (0)