-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Generate enum variant assist #12334
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
Generate enum variant assist #12334
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2347da8
Generate enum variant assist
fasterthanlime 7d716cb
Simplify with adt.source()
fasterthanlime 0ed85be
Don't suggest enum variant if name_ref start with ASCII lowercase letter
fasterthanlime 707a568
Still suggest generating enum methods if the name ref starts with a l…
fasterthanlime 796c4d8
Better lowercase/uppercase checks
fasterthanlime ae2c0db
Pull text creation into the closure
fasterthanlime 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
195 changes: 195 additions & 0 deletions
195
crates/ide-assists/src/handlers/generate_enum_variant.rs
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,195 @@ | ||
use hir::HasSource; | ||
use ide_db::assists::{AssistId, AssistKind}; | ||
use syntax::{ | ||
ast::{self, edit::IndentLevel}, | ||
AstNode, TextSize, | ||
}; | ||
|
||
use crate::assist_context::{AssistContext, Assists}; | ||
|
||
// Assist: generate_enum_variant | ||
// | ||
// Adds a variant to an enum. | ||
// | ||
// ``` | ||
// enum Countries { | ||
// Ghana, | ||
// } | ||
// | ||
// fn main() { | ||
// let country = Countries::Lesotho$0; | ||
// } | ||
// ``` | ||
// -> | ||
// ``` | ||
// enum Countries { | ||
// Ghana, | ||
// Lesotho, | ||
// } | ||
// | ||
// fn main() { | ||
// let country = Countries::Lesotho; | ||
// } | ||
// ``` | ||
pub(crate) fn generate_enum_variant(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; | ||
let path = path_expr.path()?; | ||
|
||
if ctx.sema.resolve_path(&path).is_some() { | ||
// No need to generate anything if the path resolves | ||
return None; | ||
} | ||
|
||
let name_ref = path.segment()?.name_ref()?; | ||
if name_ref.text().as_str().chars().next()?.is_ascii_lowercase() { | ||
// Don't suggest generating variant if the name starts with a lowercase letter | ||
return None; | ||
} | ||
|
||
if let Some(hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e)))) = | ||
ctx.sema.resolve_path(&path.qualifier()?) | ||
{ | ||
let target = path.syntax().text_range(); | ||
return add_variant_to_accumulator(acc, ctx, target, e, &name_ref); | ||
} | ||
|
||
None | ||
} | ||
|
||
fn add_variant_to_accumulator( | ||
acc: &mut Assists, | ||
ctx: &AssistContext, | ||
target: syntax::TextRange, | ||
adt: hir::Enum, | ||
name_ref: &ast::NameRef, | ||
) -> Option<()> { | ||
let adt_ast = adt.source(ctx.db())?.original_ast_node(ctx.db())?.value; | ||
|
||
let enum_indent_level = IndentLevel::from_node(&adt_ast.syntax()); | ||
|
||
let offset = adt_ast.variant_list()?.syntax().text_range().end() - TextSize::of('}'); | ||
|
||
let prefix = if adt_ast.variant_list()?.variants().next().is_none() { | ||
format!("\n{}", IndentLevel(1)) | ||
} else { | ||
format!("{}", IndentLevel(1)) | ||
}; | ||
let text = format!("{}{},\n{}", prefix, name_ref, enum_indent_level); | ||
|
||
acc.add( | ||
AssistId("generate_enum_variant", AssistKind::Generate), | ||
"Generate variant", | ||
target, | ||
|builder| builder.insert(offset, text), | ||
) | ||
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. Ah, missed this. Let's pull the 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. Done in ae2c0db |
||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::{check_assist, check_assist_not_applicable}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn generate_basic_enum_variant_in_empty_enum() { | ||
check_assist( | ||
generate_enum_variant, | ||
r" | ||
enum Foo {} | ||
fn main() { | ||
Foo::Bar$0 | ||
} | ||
", | ||
r" | ||
enum Foo { | ||
Bar, | ||
} | ||
fn main() { | ||
Foo::Bar | ||
} | ||
", | ||
) | ||
} | ||
|
||
#[test] | ||
fn generate_basic_enum_variant_in_non_empty_enum() { | ||
check_assist( | ||
generate_enum_variant, | ||
r" | ||
enum Foo { | ||
Bar, | ||
} | ||
fn main() { | ||
Foo::Baz$0 | ||
} | ||
", | ||
r" | ||
enum Foo { | ||
Bar, | ||
Baz, | ||
} | ||
fn main() { | ||
Foo::Baz | ||
} | ||
", | ||
) | ||
} | ||
|
||
#[test] | ||
fn not_applicable_for_existing_variant() { | ||
check_assist_not_applicable( | ||
generate_enum_variant, | ||
r" | ||
enum Foo { | ||
Bar, | ||
} | ||
fn main() { | ||
Foo::Bar$0 | ||
} | ||
", | ||
) | ||
} | ||
|
||
#[test] | ||
fn not_applicable_for_lowercase() { | ||
check_assist_not_applicable( | ||
generate_enum_variant, | ||
r" | ||
enum Foo { | ||
Bar, | ||
} | ||
fn main() { | ||
Foo::new$0 | ||
} | ||
", | ||
) | ||
} | ||
|
||
#[test] | ||
fn indentation_level_is_correct() { | ||
check_assist( | ||
generate_enum_variant, | ||
r" | ||
mod m { | ||
enum Foo { | ||
Bar, | ||
} | ||
} | ||
fn main() { | ||
m::Foo::Baz$0 | ||
} | ||
", | ||
r" | ||
mod m { | ||
enum Foo { | ||
Bar, | ||
Baz, | ||
} | ||
} | ||
fn main() { | ||
m::Foo::Baz | ||
} | ||
", | ||
) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar can be done for the uppercase check in
generate_function
. Going with the non-ascii version probably makes more more sense here (though I don't really know of anyone NOT using the latin alphabet set for identifiers so it really shouldn't matter)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 796c4d8