Merge #1910
1910: Assists r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
bb467b857e
@ -4,7 +4,7 @@ use ra_syntax::{
|
||||
SmolStr,
|
||||
};
|
||||
|
||||
use crate::{ast_builder::AstBuilder, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum AddMissingImplMembersMode {
|
||||
@ -102,9 +102,7 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
|
||||
fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
|
||||
let mut ast_editor = AstEditor::new(fn_def.clone());
|
||||
if fn_def.body().is_none() {
|
||||
ast_editor.set_body(&AstBuilder::<ast::Block>::single_expr(
|
||||
&AstBuilder::<ast::Expr>::unimplemented(),
|
||||
));
|
||||
ast_editor.set_body(&Make::<ast::Block>::single_expr(Make::<ast::Expr>::unimplemented()));
|
||||
}
|
||||
ast_editor.ast().to_owned()
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use std::iter;
|
||||
use hir::{db::HirDatabase, Adt, HasSource};
|
||||
use ra_syntax::ast::{self, AstNode, NameOwner};
|
||||
|
||||
use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId};
|
||||
use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
|
||||
|
||||
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
|
||||
@ -29,13 +29,10 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
||||
|
||||
ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
|
||||
let variants = variant_list.variants();
|
||||
let arms = variants.filter_map(build_pat).map(|pat| {
|
||||
AstBuilder::<ast::MatchArm>::from_pieces(
|
||||
iter::once(pat),
|
||||
&AstBuilder::<ast::Expr>::unit(),
|
||||
)
|
||||
});
|
||||
let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms);
|
||||
let arms = variants
|
||||
.filter_map(build_pat)
|
||||
.map(|pat| Make::<ast::MatchArm>::from(iter::once(pat), Make::<ast::Expr>::unit()));
|
||||
let new_arm_list = Make::<ast::MatchArmList>::from_arms(arms);
|
||||
|
||||
edit.target(match_expr.syntax().text_range());
|
||||
edit.set_cursor(expr.syntax().text_range().start());
|
||||
@ -66,21 +63,21 @@ fn resolve_enum_def(
|
||||
}
|
||||
|
||||
fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
|
||||
let path = &AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?);
|
||||
let path = Make::<ast::Path>::from(var.parent_enum().name()?, var.name()?);
|
||||
|
||||
let pat: ast::Pat = match var.kind() {
|
||||
ast::StructKind::Tuple(field_list) => {
|
||||
let pats = iter::repeat(AstBuilder::<ast::PlaceholderPat>::placeholder().into())
|
||||
let pats = iter::repeat(Make::<ast::PlaceholderPat>::placeholder().into())
|
||||
.take(field_list.fields().count());
|
||||
AstBuilder::<ast::TupleStructPat>::from_pieces(path, pats).into()
|
||||
Make::<ast::TupleStructPat>::from(path, pats).into()
|
||||
}
|
||||
ast::StructKind::Named(field_list) => {
|
||||
let pats = field_list
|
||||
.fields()
|
||||
.map(|f| AstBuilder::<ast::BindPat>::from_name(&f.name().unwrap()).into());
|
||||
AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into()
|
||||
.map(|f| Make::<ast::BindPat>::from_name(f.name().unwrap()).into());
|
||||
Make::<ast::RecordPat>::from(path, pats).into()
|
||||
}
|
||||
ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(),
|
||||
ast::StructKind::Unit => Make::<ast::PathPat>::from_path(path).into(),
|
||||
};
|
||||
|
||||
Some(pat)
|
||||
|
@ -6,7 +6,7 @@ use ra_syntax::{
|
||||
TextRange,
|
||||
};
|
||||
|
||||
use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId};
|
||||
use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
|
||||
|
||||
pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||
let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
|
||||
@ -52,7 +52,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
|
||||
}
|
||||
|
||||
let predicates = type_params.iter().filter_map(build_predicate);
|
||||
let where_clause = AstBuilder::<ast::WhereClause>::from_predicates(predicates);
|
||||
let where_clause = Make::<ast::WhereClause>::from_predicates(predicates);
|
||||
|
||||
let to_insert = match anchor.prev_sibling_or_token() {
|
||||
Some(ref elem) if elem.kind() == WHITESPACE => {
|
||||
@ -69,9 +69,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
|
||||
}
|
||||
|
||||
fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> {
|
||||
let path = AstBuilder::<ast::Path>::from_name(param.name()?);
|
||||
let predicate =
|
||||
AstBuilder::<ast::WherePred>::from_pieces(path, param.type_bound_list()?.bounds());
|
||||
let path = Make::<ast::Path>::from_name(param.name()?);
|
||||
let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds());
|
||||
Some(predicate)
|
||||
}
|
||||
|
||||
|
@ -1,44 +1,35 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
use hir::Name;
|
||||
use ra_syntax::{ast, AstNode, SourceFile};
|
||||
|
||||
pub struct AstBuilder<N: AstNode> {
|
||||
pub struct Make<N: AstNode> {
|
||||
_phantom: std::marker::PhantomData<N>,
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::RecordField> {
|
||||
pub fn from_name(name: &Name) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
||||
}
|
||||
|
||||
pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> ast::RecordField {
|
||||
impl Make<ast::RecordField> {
|
||||
pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
|
||||
match expr {
|
||||
Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
|
||||
None => Self::from_text(&name.syntax().to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::RecordField {
|
||||
ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::Block> {
|
||||
impl Make<ast::Block> {
|
||||
pub fn single_expr(e: ast::Expr) -> ast::Block {
|
||||
Self::from_text(&format!("{{ {} }}", e.syntax()))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Block {
|
||||
ast_node_from_file_text(&format!("fn f() {}", text))
|
||||
}
|
||||
|
||||
pub fn single_expr(e: &ast::Expr) -> ast::Block {
|
||||
Self::from_text(&format!("{{ {} }}", e.syntax()))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::Expr> {
|
||||
fn from_text(text: &str) -> ast::Expr {
|
||||
ast_node_from_file_text(&format!("const C: () = {};", text))
|
||||
}
|
||||
|
||||
impl Make<ast::Expr> {
|
||||
pub fn unit() -> ast::Expr {
|
||||
Self::from_text("()")
|
||||
}
|
||||
@ -46,130 +37,128 @@ impl AstBuilder<ast::Expr> {
|
||||
pub fn unimplemented() -> ast::Expr {
|
||||
Self::from_text("unimplemented!()")
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Expr {
|
||||
ast_node_from_file_text(&format!("const C: () = {};", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::NameRef> {
|
||||
pub fn new(text: &str) -> ast::NameRef {
|
||||
impl Make<ast::NameRef> {
|
||||
pub fn from(text: &str) -> ast::NameRef {
|
||||
ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::Path> {
|
||||
fn from_text(text: &str) -> ast::Path {
|
||||
ast_node_from_file_text(text)
|
||||
}
|
||||
|
||||
impl Make<ast::Path> {
|
||||
pub fn from_name(name: ast::Name) -> ast::Path {
|
||||
let name = name.syntax().to_string();
|
||||
Self::from_text(name.as_str())
|
||||
}
|
||||
|
||||
pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
|
||||
pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
|
||||
Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::Path {
|
||||
ast_node_from_file_text(text)
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::BindPat> {
|
||||
impl Make<ast::BindPat> {
|
||||
pub fn from_name(name: ast::Name) -> ast::BindPat {
|
||||
Self::from_text(name.text())
|
||||
}
|
||||
|
||||
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<ast::PlaceholderPat> {
|
||||
fn from_text(text: &str) -> ast::PlaceholderPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
|
||||
impl Make<ast::PlaceholderPat> {
|
||||
pub fn placeholder() -> ast::PlaceholderPat {
|
||||
Self::from_text("_")
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::TupleStructPat> {
|
||||
fn from_text(text: &str) -> ast::TupleStructPat {
|
||||
fn from_text(text: &str) -> ast::PlaceholderPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_pieces(
|
||||
path: &ast::Path,
|
||||
pats: impl Iterator<Item = ast::Pat>,
|
||||
) -> ast::TupleStructPat {
|
||||
impl Make<ast::TupleStructPat> {
|
||||
pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(&format!("{}({})", path.syntax(), pats_str))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::RecordPat> {
|
||||
fn from_text(text: &str) -> ast::RecordPat {
|
||||
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<Item = ast::Pat>) -> ast::RecordPat {
|
||||
impl Make<ast::RecordPat> {
|
||||
pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
|
||||
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::PathPat> {
|
||||
fn from_text(text: &str) -> ast::PathPat {
|
||||
fn from_text(text: &str) -> ast::RecordPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_path(path: &ast::Path) -> ast::PathPat {
|
||||
impl Make<ast::PathPat> {
|
||||
pub fn from_path(path: ast::Path) -> ast::PathPat {
|
||||
let path_str = path.syntax().text().to_string();
|
||||
Self::from_text(path_str.as_str())
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::PathPat {
|
||||
ast_node_from_file_text(&format!("fn f({}: ())", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::MatchArm> {
|
||||
fn from_text(text: &str) -> ast::MatchArm {
|
||||
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
|
||||
pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm {
|
||||
impl Make<ast::MatchArm> {
|
||||
pub fn from(pats: impl Iterator<Item = ast::Pat>, 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<ast::MatchArmList> {
|
||||
fn from_text(text: &str) -> ast::MatchArmList {
|
||||
fn from_text(text: &str) -> ast::MatchArm {
|
||||
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::MatchArmList> {
|
||||
pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
|
||||
let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
|
||||
Self::from_text(&format!("{},\n", arms_str))
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::MatchArmList {
|
||||
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::WherePred> {
|
||||
fn from_text(text: &str) -> ast::WherePred {
|
||||
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
|
||||
pub fn from_pieces(
|
||||
path: ast::Path,
|
||||
bounds: impl Iterator<Item = ast::TypeBound>,
|
||||
) -> ast::WherePred {
|
||||
impl Make<ast::WherePred> {
|
||||
pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
|
||||
let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
|
||||
Self::from_text(&format!("{}: {}", path.syntax(), bounds))
|
||||
}
|
||||
}
|
||||
|
||||
impl AstBuilder<ast::WhereClause> {
|
||||
fn from_text(text: &str) -> ast::WhereClause {
|
||||
fn from_text(text: &str) -> ast::WherePred {
|
||||
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
impl Make<ast::WhereClause> {
|
||||
pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
|
||||
let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
|
||||
Self::from_text(preds.as_str())
|
||||
}
|
||||
|
||||
fn from_text(text: &str) -> ast::WhereClause {
|
||||
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
|
||||
}
|
||||
}
|
||||
|
||||
fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {
|
||||
|
@ -2,11 +2,11 @@ use std::cell::RefCell;
|
||||
|
||||
use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink};
|
||||
use itertools::Itertools;
|
||||
use ra_assists::{ast_builder::AstBuilder, ast_editor::AstEditor};
|
||||
use ra_assists::{ast_builder::Make, ast_editor::AstEditor};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_prof::profile;
|
||||
use ra_syntax::{
|
||||
ast::{self, AstNode, RecordField},
|
||||
ast::{self, AstNode},
|
||||
Location, SyntaxNode, TextRange, T,
|
||||
};
|
||||
use ra_text_edit::{TextEdit, TextEditBuilder};
|
||||
@ -59,7 +59,11 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
||||
let node = d.ast(db);
|
||||
let mut ast_editor = AstEditor::new(node);
|
||||
for f in d.missed_fields.iter() {
|
||||
ast_editor.append_field(&AstBuilder::<RecordField>::from_name(f));
|
||||
let field = Make::<ast::RecordField>::from(
|
||||
Make::<ast::NameRef>::from(&f.to_string()),
|
||||
Some(Make::<ast::Expr>::unit()),
|
||||
);
|
||||
ast_editor.append_field(&field);
|
||||
}
|
||||
|
||||
let mut builder = TextEditBuilder::default();
|
||||
|
Loading…
x
Reference in New Issue
Block a user