rename struct -> record, pos -> tuple

This commit is contained in:
Aleksey Kladov 2019-08-23 15:55:21 +03:00
parent c12dce0073
commit 5b18a4eef9
78 changed files with 640 additions and 634 deletions

View file

@ -3,8 +3,8 @@ mod completion_context;
mod presentation;
mod complete_dot;
mod complete_struct_literal;
mod complete_struct_pattern;
mod complete_record_literal;
mod complete_record_pattern;
mod complete_pattern;
mod complete_fn_param;
mod complete_keyword;
@ -65,8 +65,8 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
complete_path::complete_path(&mut acc, &ctx);
complete_scope::complete_scope(&mut acc, &ctx);
complete_dot::complete_dot(&mut acc, &ctx);
complete_struct_literal::complete_struct_literal(&mut acc, &ctx);
complete_struct_pattern::complete_struct_pattern(&mut acc, &ctx);
complete_record_literal::complete_record_literal(&mut acc, &ctx);
complete_record_pattern::complete_record_pattern(&mut acc, &ctx);
complete_pattern::complete_pattern(&mut acc, &ctx);
complete_postfix::complete_postfix(&mut acc, &ctx);
Some(acc)

View file

@ -45,7 +45,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
// FIXME unions
TypeCtor::Tuple { .. } => {
for (i, ty) in a_ty.parameters.iter().enumerate() {
acc.add_pos_field(ctx, i, ty);
acc.add_tuple_field(ctx, i, ty);
}
}
_ => {}

View file

@ -3,11 +3,11 @@ use hir::Substs;
use crate::completion::{CompletionContext, Completions};
/// Complete fields in fields literals.
pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) {
let (ty, variant) = match ctx.struct_lit_syntax.as_ref().and_then(|it| {
pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) {
let (ty, variant) = match ctx.record_lit_syntax.as_ref().and_then(|it| {
Some((
ctx.analyzer.type_of(ctx.db, &it.clone().into())?,
ctx.analyzer.resolve_struct_literal(it)?,
ctx.analyzer.resolve_record_literal(it)?,
))
}) {
Some(it) => it,
@ -30,7 +30,7 @@ mod tests {
}
#[test]
fn test_struct_literal_field() {
fn test_record_literal_field() {
let completions = complete(
r"
struct A { the_field: u32 }
@ -54,7 +54,7 @@ mod tests {
}
#[test]
fn test_struct_literal_enum_variant() {
fn test_record_literal_enum_variant() {
let completions = complete(
r"
enum E {
@ -80,7 +80,7 @@ mod tests {
}
#[test]
fn test_struct_literal_two_structs() {
fn test_record_literal_two_structs() {
let completions = complete(
r"
struct A { a: u32 }
@ -106,7 +106,7 @@ mod tests {
}
#[test]
fn test_struct_literal_generic_struct() {
fn test_record_literal_generic_struct() {
let completions = complete(
r"
struct A<T> { a: T }

View file

@ -2,11 +2,11 @@ use hir::Substs;
use crate::completion::{CompletionContext, Completions};
pub(super) fn complete_struct_pattern(acc: &mut Completions, ctx: &CompletionContext) {
let (ty, variant) = match ctx.struct_lit_pat.as_ref().and_then(|it| {
pub(super) fn complete_record_pattern(acc: &mut Completions, ctx: &CompletionContext) {
let (ty, variant) = match ctx.record_lit_pat.as_ref().and_then(|it| {
Some((
ctx.analyzer.type_of_pat(ctx.db, &it.clone().into())?,
ctx.analyzer.resolve_struct_pattern(it)?,
ctx.analyzer.resolve_record_pattern(it)?,
))
}) {
Some(it) => it,
@ -29,7 +29,7 @@ mod tests {
}
#[test]
fn test_struct_pattern_field() {
fn test_record_pattern_field() {
let completions = complete(
r"
struct S { foo: u32 }
@ -56,7 +56,7 @@ mod tests {
}
#[test]
fn test_struct_pattern_enum_variant() {
fn test_record_pattern_enum_variant() {
let completions = complete(
r"
enum E {

View file

@ -20,8 +20,8 @@ pub(crate) struct CompletionContext<'a> {
pub(super) module: Option<hir::Module>,
pub(super) function_syntax: Option<ast::FnDef>,
pub(super) use_item_syntax: Option<ast::UseItem>,
pub(super) struct_lit_syntax: Option<ast::StructLit>,
pub(super) struct_lit_pat: Option<ast::StructPat>,
pub(super) record_lit_syntax: Option<ast::RecordLit>,
pub(super) record_lit_pat: Option<ast::RecordPat>,
pub(super) is_param: bool,
/// If a name-binding or reference to a const in a pattern.
/// Irrefutable patterns (like let) are excluded.
@ -60,8 +60,8 @@ impl<'a> CompletionContext<'a> {
module,
function_syntax: None,
use_item_syntax: None,
struct_lit_syntax: None,
struct_lit_pat: None,
record_lit_syntax: None,
record_lit_pat: None,
is_param: false,
is_pat_binding: false,
is_trivial_path: false,
@ -120,8 +120,8 @@ impl<'a> CompletionContext<'a> {
self.is_param = true;
return;
}
if name.syntax().ancestors().find_map(ast::FieldPatList::cast).is_some() {
self.struct_lit_pat =
if name.syntax().ancestors().find_map(ast::RecordFieldPatList::cast).is_some() {
self.record_lit_pat =
find_node_at_offset(original_parse.tree().syntax(), self.offset);
}
}
@ -129,8 +129,8 @@ impl<'a> CompletionContext<'a> {
fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) {
let name_range = name_ref.syntax().text_range();
if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() {
self.struct_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() {
self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
}
let top_node = name_ref

View file

@ -28,7 +28,7 @@ impl Completions {
.add_to(self);
}
pub(crate) fn add_pos_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) {
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) {
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string())
.kind(CompletionItemKind::Field)
.detail(ty.display(ctx.db).to_string())

View file

@ -9,7 +9,7 @@ use ra_assists::ast_editor::{AstBuilder, AstEditor};
use ra_db::SourceDatabase;
use ra_prof::profile;
use ra_syntax::{
ast::{self, AstNode, NamedField},
ast::{self, AstNode, RecordField},
Location, SyntaxNode, TextRange, T,
};
use ra_text_edit::{TextEdit, TextEditBuilder};
@ -62,7 +62,7 @@ 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::<NamedField>::from_name(f));
ast_editor.append_field(&AstBuilder::<RecordField>::from_name(f));
}
let mut builder = TextEditBuilder::default();
@ -141,20 +141,20 @@ fn check_struct_shorthand_initialization(
file_id: FileId,
node: &SyntaxNode,
) -> Option<()> {
let struct_lit = ast::StructLit::cast(node.clone())?;
let named_field_list = struct_lit.named_field_list()?;
for named_field in named_field_list.fields() {
if let (Some(name_ref), Some(expr)) = (named_field.name_ref(), named_field.expr()) {
let record_lit = ast::RecordLit::cast(node.clone())?;
let record_field_list = record_lit.record_field_list()?;
for record_field in record_field_list.fields() {
if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) {
let field_name = name_ref.syntax().text().to_string();
let field_expr = expr.syntax().text().to_string();
if field_name == field_expr {
let mut edit_builder = TextEditBuilder::default();
edit_builder.delete(named_field.syntax().text_range());
edit_builder.insert(named_field.syntax().text_range().start(), field_name);
edit_builder.delete(record_field.syntax().text_range());
edit_builder.insert(record_field.syntax().text_range().start(), field_name);
let edit = edit_builder.finish();
acc.push(Diagnostic {
range: named_field.syntax().text_range(),
range: record_field.syntax().text_range(),
message: "Shorthand struct initialization".to_string(),
severity: Severity::WeakWarning,
fix: Some(SourceChange::source_file_edit(

View file

@ -314,7 +314,7 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option
.visit(|it: ast::TypeAliasDef| it.doc_comment_text())
.visit(|it: ast::ConstDef| it.doc_comment_text())
.visit(|it: ast::StaticDef| it.doc_comment_text())
.visit(|it: ast::NamedFieldDef| it.doc_comment_text())
.visit(|it: ast::RecordFieldDef| it.doc_comment_text())
.visit(|it: ast::EnumVariant| it.doc_comment_text())
.visit(|it: ast::MacroCall| it.doc_comment_text())
.accept(&node)?
@ -336,7 +336,7 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
.visit(|node: ast::TypeAliasDef| node.short_label())
.visit(|node: ast::ConstDef| node.short_label())
.visit(|node: ast::StaticDef| node.short_label())
.visit(|node: ast::NamedFieldDef| node.short_label())
.visit(|node: ast::RecordFieldDef| node.short_label())
.visit(|node: ast::EnumVariant| node.short_label())
.accept(&node)?
}

View file

@ -53,7 +53,7 @@ impl ShortLabel for ast::StaticDef {
}
}
impl ShortLabel for ast::NamedFieldDef {
impl ShortLabel for ast::RecordFieldDef {
fn short_label(&self) -> Option<String> {
short_label_from_ascribed_node(self, "")
}

View file

@ -124,7 +124,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
let ty = td.type_ref();
decl_with_type_ref(td, ty)
})
.visit(decl_with_ascription::<ast::NamedFieldDef>)
.visit(decl_with_ascription::<ast::RecordFieldDef>)
.visit(decl_with_ascription::<ast::ConstDef>)
.visit(decl_with_ascription::<ast::StaticDef>)
.visit(|im: ast::ImplBlock| {
@ -222,7 +222,7 @@ fn very_obsolete() {}
label: "x",
navigation_range: [18; 19),
node_range: [18; 24),
kind: NAMED_FIELD_DEF,
kind: RECORD_FIELD_DEF,
detail: Some(
"i32",
),

View file

@ -18,11 +18,11 @@ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRang
fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING];
let list_kinds = [
FIELD_PAT_LIST,
RECORD_FIELD_PAT_LIST,
MATCH_ARM_LIST,
NAMED_FIELD_DEF_LIST,
POS_FIELD_DEF_LIST,
NAMED_FIELD_LIST,
RECORD_FIELD_DEF_LIST,
TUPLE_FIELD_DEF_LIST,
RECORD_FIELD_LIST,
ENUM_VARIANT_LIST,
USE_TREE_LIST,
TYPE_PARAM_LIST,

View file

@ -81,8 +81,14 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> {
match kind {
COMMENT => Some(FoldKind::Comment),
USE_ITEM => Some(FoldKind::Imports),
NAMED_FIELD_DEF_LIST | FIELD_PAT_LIST | ITEM_LIST | EXTERN_ITEM_LIST | USE_TREE_LIST
| BLOCK | ENUM_VARIANT_LIST | TOKEN_TREE => Some(FoldKind::Block),
RECORD_FIELD_DEF_LIST
| RECORD_FIELD_PAT_LIST
| ITEM_LIST
| EXTERN_ITEM_LIST
| USE_TREE_LIST
| BLOCK
| ENUM_VARIANT_LIST
| TOKEN_TREE => Some(FoldKind::Block),
_ => None,
}
}

View file

@ -178,7 +178,7 @@ fn named_target(file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget>
node.short_label(),
)
})
.visit(|node: ast::NamedFieldDef| {
.visit(|node: ast::RecordFieldDef| {
NavigationTarget::from_named(
file_id,
&node,
@ -344,13 +344,13 @@ mod tests {
foo.spam<|>;
}
",
"spam NAMED_FIELD_DEF FileId(1) [17; 26) [17; 21)",
"spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)",
);
}
#[test]
fn goto_definition_works_for_named_fields() {
covers!(goto_definition_works_for_named_fields);
fn goto_definition_works_for_record_fields() {
covers!(goto_definition_works_for_record_fields);
check_goto(
"
//- /lib.rs
@ -364,7 +364,7 @@ mod tests {
}
}
",
"spam NAMED_FIELD_DEF FileId(1) [17; 26) [17; 21)",
"spam RECORD_FIELD_DEF FileId(1) [17; 26) [17; 21)",
);
}
#[test]
@ -473,7 +473,7 @@ mod tests {
field<|>: string,
}
"#,
"field NAMED_FIELD_DEF FileId(1) [17; 30) [17; 22)",
"field RECORD_FIELD_DEF FileId(1) [17; 30) [17; 22)",
);
check_goto(

View file

@ -197,7 +197,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
.visit(|node: ast::TraitDef| {
hover_text(node.doc_comment_text(), node.short_label())
})
.visit(|node: ast::NamedFieldDef| {
.visit(|node: ast::RecordFieldDef| {
hover_text(node.doc_comment_text(), node.short_label())
})
.visit(|node: ast::Module| hover_text(node.doc_comment_text(), node.short_label()))

View file

@ -125,13 +125,13 @@ fn get_leaf_pats(root_pat: ast::Pat) -> Vec<ast::Pat> {
pats_to_process.push_back(arg_pat);
}
}
ast::Pat::StructPat(struct_pat) => {
if let Some(pat_list) = struct_pat.field_pat_list() {
ast::Pat::RecordPat(record_pat) => {
if let Some(pat_list) = record_pat.record_field_pat_list() {
pats_to_process.extend(
pat_list
.field_pats()
.filter_map(|field_pat| {
field_pat
.record_field_pats()
.filter_map(|record_field_pat| {
record_field_pat
.pat()
.filter(|pat| pat.syntax().kind() != SyntaxKind::BIND_PAT)
})

View file

@ -3,7 +3,7 @@ test_utils::marks!(
goto_definition_works_for_macros
goto_definition_works_for_methods
goto_definition_works_for_fields
goto_definition_works_for_named_fields
goto_definition_works_for_record_fields
call_info_bad_offset
dont_complete_current_use
dont_complete_primitive_in_use

View file

@ -54,12 +54,12 @@ pub(crate) fn classify_name_ref(
}
// It could also be a named field
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::NamedField::cast) {
tested_by!(goto_definition_works_for_named_fields);
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) {
tested_by!(goto_definition_works_for_record_fields);
let struct_lit = field_expr.syntax().ancestors().find_map(ast::StructLit::cast);
let record_lit = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast);
if let Some(ty) = struct_lit.and_then(|lit| analyzer.type_of(db, &lit.into())) {
if let Some(ty) = record_lit.and_then(|lit| analyzer.type_of(db, &lit.into())) {
if let Some((hir::AdtDef::Struct(s), _)) = ty.as_adt() {
let hir_path = hir::Path::from_name_ref(name_ref);
let hir_name = hir_path.as_ident().unwrap();

View file

@ -165,7 +165,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => {
"type"
}
NAMED_FIELD_DEF => "field",
RECORD_FIELD_DEF => "field",
_ => "function",
})
.unwrap_or("function")