mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
apply suggestions
This commit is contained in:
parent
f52f5fed11
commit
f780145c4a
3 changed files with 10 additions and 97 deletions
|
@ -1,4 +1,6 @@
|
||||||
use ide_db::imports::import_assets::item_for_path_search;
|
use ide_db::{
|
||||||
|
imports::import_assets::item_for_path_search, use_trivial_contructor::use_trivial_constructor,
|
||||||
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
use syntax::ast::{self, AstNode, HasName, HasVisibility, StructKind};
|
use syntax::ast::{self, AstNode, HasName, HasVisibility, StructKind};
|
||||||
|
@ -8,51 +10,6 @@ use crate::{
|
||||||
AssistContext, AssistId, AssistKind, Assists,
|
AssistContext, AssistId, AssistKind, Assists,
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: how to depupicate with `ide-diagnostics/mssing_fields`
|
|
||||||
fn use_trivial_constructor(
|
|
||||||
db: &ide_db::RootDatabase,
|
|
||||||
path: ast::Path,
|
|
||||||
ty: &hir::Type,
|
|
||||||
) -> Option<ast::Expr> {
|
|
||||||
match ty.as_adt() {
|
|
||||||
Some(hir::Adt::Enum(x)) => {
|
|
||||||
let variants = x.variants(db);
|
|
||||||
|
|
||||||
if variants.len() == 1 {
|
|
||||||
let variant = variants[0];
|
|
||||||
|
|
||||||
if variant.fields(db).is_empty() {
|
|
||||||
let path = ast::make::path_qualified(
|
|
||||||
path,
|
|
||||||
syntax::ast::make::path_segment(ast::make::name_ref(
|
|
||||||
&variant.name(db).to_smol_str(),
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
|
|
||||||
let is_record = variant.kind(db) == hir::StructKind::Record;
|
|
||||||
|
|
||||||
return Some(if is_record {
|
|
||||||
ast::Expr::RecordExpr(syntax::ast::make::record_expr(
|
|
||||||
path,
|
|
||||||
ast::make::record_expr_field_list(std::iter::empty()),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
syntax::ast::make::expr_path(path)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(hir::Adt::Struct(x)) => {
|
|
||||||
if x.fields(db).is_empty() {
|
|
||||||
return Some(syntax::ast::make::expr_path(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assist: generate_new
|
// Assist: generate_new
|
||||||
//
|
//
|
||||||
// Adds a new inherent impl for a type.
|
// Adds a new inherent impl for a type.
|
||||||
|
@ -84,6 +41,8 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
// Return early if we've found an existing new fn
|
// Return early if we've found an existing new fn
|
||||||
let impl_def = find_struct_impl(ctx, &ast::Adt::Struct(strukt.clone()), "new")?;
|
let impl_def = find_struct_impl(ctx, &ast::Adt::Struct(strukt.clone()), "new")?;
|
||||||
|
|
||||||
|
let current_module = ctx.sema.scope(strukt.syntax())?.module();
|
||||||
|
|
||||||
let target = strukt.syntax().text_range();
|
let target = strukt.syntax().text_range();
|
||||||
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| {
|
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| {
|
||||||
let mut buf = String::with_capacity(512);
|
let mut buf = String::with_capacity(512);
|
||||||
|
@ -94,8 +53,6 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
|
||||||
|
|
||||||
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
|
let vis = strukt.visibility().map_or(String::new(), |v| format!("{} ", v));
|
||||||
|
|
||||||
let current_module = ctx.sema.scope(strukt.syntax()).unwrap().module();
|
|
||||||
|
|
||||||
let trivial_constructors = field_list
|
let trivial_constructors = field_list
|
||||||
.fields()
|
.fields()
|
||||||
.map(|f| {
|
.map(|f| {
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub mod source_change;
|
||||||
pub mod symbol_index;
|
pub mod symbol_index;
|
||||||
pub mod traits;
|
pub mod traits;
|
||||||
pub mod ty_filter;
|
pub mod ty_filter;
|
||||||
|
pub mod use_trivial_contructor;
|
||||||
|
|
||||||
pub mod imports {
|
pub mod imports {
|
||||||
pub mod import_assets;
|
pub mod import_assets;
|
||||||
|
|
|
@ -5,7 +5,7 @@ use hir::{
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
assists::Assist, famous_defs::FamousDefs, imports::import_assets::item_for_path_search,
|
assists::Assist, famous_defs::FamousDefs, imports::import_assets::item_for_path_search,
|
||||||
source_change::SourceChange, FxHashMap,
|
source_change::SourceChange, use_trivial_contructor::use_trivial_constructor, FxHashMap,
|
||||||
};
|
};
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -17,51 +17,6 @@ use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{fix, Diagnostic, DiagnosticsContext};
|
use crate::{fix, Diagnostic, DiagnosticsContext};
|
||||||
|
|
||||||
// FIXME: how to depupicate with `ide-assists/generate_new`
|
|
||||||
fn use_trivial_constructor(
|
|
||||||
db: &ide_db::RootDatabase,
|
|
||||||
path: ast::Path,
|
|
||||||
ty: &hir::Type,
|
|
||||||
) -> Option<ast::Expr> {
|
|
||||||
match ty.as_adt() {
|
|
||||||
Some(hir::Adt::Enum(x)) => {
|
|
||||||
let variants = x.variants(db);
|
|
||||||
|
|
||||||
if variants.len() == 1 {
|
|
||||||
let variant = variants[0];
|
|
||||||
|
|
||||||
if variant.fields(db).is_empty() {
|
|
||||||
let path = ast::make::path_qualified(
|
|
||||||
path,
|
|
||||||
syntax::ast::make::path_segment(ast::make::name_ref(
|
|
||||||
&variant.name(db).to_smol_str(),
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
|
|
||||||
let is_record = variant.kind(db) == hir::StructKind::Record;
|
|
||||||
|
|
||||||
return Some(if is_record {
|
|
||||||
ast::Expr::RecordExpr(syntax::ast::make::record_expr(
|
|
||||||
path,
|
|
||||||
ast::make::record_expr_field_list(std::iter::empty()),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
syntax::ast::make::expr_path(path)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(hir::Adt::Struct(x)) => {
|
|
||||||
if x.fields(db).is_empty() {
|
|
||||||
return Some(syntax::ast::make::expr_path(path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
// Diagnostic: missing-fields
|
// Diagnostic: missing-fields
|
||||||
//
|
//
|
||||||
// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
|
// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
|
||||||
|
@ -104,8 +59,8 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
|
||||||
let root = ctx.sema.db.parse_or_expand(d.file)?;
|
let root = ctx.sema.db.parse_or_expand(d.file)?;
|
||||||
|
|
||||||
let current_module = match &d.field_list_parent {
|
let current_module = match &d.field_list_parent {
|
||||||
Either::Left(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).unwrap().module(),
|
Either::Left(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).map(|it| it.module()),
|
||||||
Either::Right(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).unwrap().module(),
|
Either::Right(ptr) => ctx.sema.scope(ptr.to_node(&root).syntax()).map(|it| it.module()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
|
let build_text_edit = |parent_syntax, new_syntax: &SyntaxNode, old_syntax| {
|
||||||
|
@ -166,7 +121,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
|
||||||
let expr = (|| -> Option<ast::Expr> {
|
let expr = (|| -> Option<ast::Expr> {
|
||||||
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
|
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
|
||||||
|
|
||||||
let type_path = current_module.find_use_path(
|
let type_path = current_module?.find_use_path(
|
||||||
ctx.sema.db,
|
ctx.sema.db,
|
||||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||||
)?;
|
)?;
|
||||||
|
|
Loading…
Reference in a new issue