-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Implement assist "Reorder field names" #3925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
730a927
Implement assist "Reorder field names"
geoffreycopin 21443f1
Remove Option unwraping
geoffreycopin 270bcfd
Avoid adding a RecordFieldPat variant to the Pat enum
geoffreycopin 25a0ce2
Add documentation comment
geoffreycopin d362fcf
Fix doc tests
geoffreycopin d908924
Generate doc
geoffreycopin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use itertools::Itertools; | ||
|
|
||
| use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; | ||
| use ra_ide_db::RootDatabase; | ||
| use ra_syntax::{ | ||
| algo, ast, | ||
| ast::{Name, Path, RecordLit, RecordPat}, | ||
| AstNode, SyntaxKind, SyntaxNode, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| assist_ctx::{Assist, AssistCtx}, | ||
| AssistId, | ||
| }; | ||
| use ra_syntax::ast::{Expr, NameRef}; | ||
|
|
||
| // Assist: reorder_fields | ||
| // | ||
| // Reorder the fields of record literals and record patterns in the same order as in | ||
| // the definition. | ||
| // | ||
| // ``` | ||
| // struct Foo {foo: i32, bar: i32}; | ||
| // const test: Foo = <|>Foo {bar: 0, foo: 1} | ||
| // ``` | ||
| // -> | ||
| // ``` | ||
| // struct Foo {foo: i32, bar: i32}; | ||
| // const test: Foo = Foo {foo: 1, bar: 0} | ||
| // ``` | ||
| // | ||
| pub(crate) fn reorder_fields(ctx: AssistCtx) -> Option<Assist> { | ||
| reorder::<RecordLit>(ctx.clone()).or_else(|| reorder::<RecordPat>(ctx)) | ||
| } | ||
|
|
||
| fn reorder<R: AstNode>(ctx: AssistCtx) -> Option<Assist> { | ||
| let record = ctx.find_node_at_offset::<R>()?; | ||
| let path = record.syntax().children().find_map(Path::cast)?; | ||
|
|
||
| let ranks = compute_fields_ranks(&path, &ctx)?; | ||
|
|
||
| let fields = get_fields(&record.syntax()); | ||
| let sorted_fields = sorted_by_rank(&fields, |node| { | ||
| *ranks.get(&get_field_name(node)).unwrap_or(&usize::max_value()) | ||
| }); | ||
|
|
||
| if sorted_fields == fields { | ||
| return None; | ||
| } | ||
|
|
||
| ctx.add_assist(AssistId("reorder_fields"), "Reorder record fields", |edit| { | ||
| for (old, new) in fields.iter().zip(&sorted_fields) { | ||
| algo::diff(old, new).into_text_edit(edit.text_edit_builder()); | ||
| } | ||
| edit.target(record.syntax().text_range()) | ||
| }) | ||
| } | ||
|
|
||
| fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> { | ||
| use SyntaxKind::*; | ||
| match node.kind() { | ||
| RECORD_LIT => vec![RECORD_FIELD], | ||
| RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT], | ||
| _ => vec![], | ||
| } | ||
| } | ||
|
|
||
| fn get_field_name(node: &SyntaxNode) -> String { | ||
| use SyntaxKind::*; | ||
| match node.kind() { | ||
| RECORD_FIELD => { | ||
| if let Some(name) = node.children().find_map(NameRef::cast) { | ||
| return name.to_string(); | ||
| } | ||
| node.children().find_map(Expr::cast).map(|expr| expr.to_string()).unwrap_or_default() | ||
| } | ||
| BIND_PAT | RECORD_FIELD_PAT => { | ||
| node.children().find_map(Name::cast).map(|n| n.to_string()).unwrap_or_default() | ||
| } | ||
| _ => String::new(), | ||
| } | ||
| } | ||
|
|
||
| fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> { | ||
| let kinds = get_fields_kind(record); | ||
| record.children().flat_map(|n| n.children()).filter(|n| kinds.contains(&n.kind())).collect() | ||
| } | ||
|
|
||
| fn sorted_by_rank( | ||
| fields: &[SyntaxNode], | ||
| get_rank: impl Fn(&SyntaxNode) -> usize, | ||
| ) -> Vec<SyntaxNode> { | ||
| fields.iter().cloned().sorted_by_key(get_rank).collect() | ||
| } | ||
|
|
||
| fn struct_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option<Struct> { | ||
| match sema.resolve_path(path) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this should use |
||
| Some(PathResolution::Def(ModuleDef::Adt(Adt::Struct(s)))) => Some(s), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn compute_fields_ranks(path: &Path, ctx: &AssistCtx) -> Option<HashMap<String, usize>> { | ||
| Some( | ||
| struct_definition(path, ctx.sema)? | ||
| .fields(ctx.db) | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(idx, field)| (field.name(ctx.db).to_string(), idx)) | ||
| .collect(), | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::helpers::{check_assist, check_assist_not_applicable}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn not_applicable_if_sorted() { | ||
| check_assist_not_applicable( | ||
| reorder_fields, | ||
| r#" | ||
| struct Foo { | ||
| foo: i32, | ||
| bar: i32, | ||
| } | ||
|
|
||
| const test: Foo = <|>Foo { foo: 0, bar: 0 }; | ||
| "#, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn trivial_empty_fields() { | ||
| check_assist_not_applicable( | ||
| reorder_fields, | ||
| r#" | ||
| struct Foo {}; | ||
| const test: Foo = <|>Foo {} | ||
| "#, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn reorder_struct_fields() { | ||
| check_assist( | ||
| reorder_fields, | ||
| r#" | ||
| struct Foo {foo: i32, bar: i32}; | ||
| const test: Foo = <|>Foo {bar: 0, foo: 1} | ||
| "#, | ||
| r#" | ||
| struct Foo {foo: i32, bar: i32}; | ||
| const test: Foo = <|>Foo {foo: 1, bar: 0} | ||
| "#, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn reorder_struct_pattern() { | ||
| check_assist( | ||
| reorder_fields, | ||
| r#" | ||
| struct Foo { foo: i64, bar: i64, baz: i64 } | ||
|
|
||
| fn f(f: Foo) -> { | ||
| match f { | ||
| <|>Foo { baz: 0, ref mut bar, .. } => (), | ||
| _ => () | ||
| } | ||
| } | ||
| "#, | ||
| r#" | ||
| struct Foo { foo: i64, bar: i64, baz: i64 } | ||
|
|
||
| fn f(f: Foo) -> { | ||
| match f { | ||
| <|>Foo { ref mut bar, baz: 0, .. } => (), | ||
| _ => () | ||
| } | ||
| } | ||
| "#, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn reorder_with_extra_field() { | ||
| check_assist( | ||
| reorder_fields, | ||
| r#" | ||
| struct Foo { | ||
| foo: String, | ||
| bar: String, | ||
| } | ||
|
|
||
| impl Foo { | ||
| fn new() -> Foo { | ||
| let foo = String::new(); | ||
| <|>Foo { | ||
| bar: foo.clone(), | ||
| extra: "Extra field", | ||
| foo, | ||
| } | ||
| } | ||
| } | ||
| "#, | ||
| r#" | ||
| struct Foo { | ||
| foo: String, | ||
| bar: String, | ||
| } | ||
|
|
||
| impl Foo { | ||
| fn new() -> Foo { | ||
| let foo = String::new(); | ||
| <|>Foo { | ||
| foo, | ||
| bar: foo.clone(), | ||
| extra: "Extra field", | ||
| } | ||
| } | ||
| } | ||
| "#, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.