Skip to content

Commit

Permalink
Merge #9146
Browse files Browse the repository at this point in the history
9146: FIX: Do not allow empty member selection in Extract struct fields and Generate getter/setter dialog r=dima74 a=Kobzol

This PR makes two small fixes to the `Extract struct fields` refactoring dialog, and one larger fix amongst all member-based refactorings. These refactorings enabled the "allow empty selection" option of the member selection dialog, which made it possible to run `Extract Fields`, `Generate getter`, `Generate setter` and `Generate constructor` refactorings with no members selected. This doesn't really make sense though, the only refactoring where it might be remotely useful is `Generate constructor`.

I have thus disabled empty selection for the mentioned refactorings, with the exception of `Generate constructor`.

Fixes: #9130

changelog: Do not enable struct field extraction or getter/setter generation of no fields are selected.

Co-authored-by: Jakub Beránek <berykubik@gmail.com>
  • Loading branch information
bors[bot] and Kobzol committed Aug 2, 2022
2 parents 4bf850b + 1c97efe commit b68ecfe
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Expand Up @@ -42,7 +42,8 @@ class RsExtractStructFieldsAction : RsBaseEditorRefactoringAction() {
project,
struct,
fields,
RsBundle.message("action.Rust.RsExtractStructFields.choose.fields.title")
RsBundle.message("action.Rust.RsExtractStructFields.choose.fields.title"),
allowEmptySelection = false
) ?: return
if (chosenFields.isEmpty()) return

Expand Down
Expand Up @@ -52,7 +52,7 @@ private class ExtractFieldsDialog(project: Project) : DialogWrapper(project, fal

override fun doValidate(): ValidationInfo? {
if (!isValidRustVariableIdentifier(input.text)) {
ValidationInfo(RsBundle.message("action.Rust.RsExtractStructFields.choose.name.dialog.invalid.name"), input)
return ValidationInfo(RsBundle.message("action.Rust.RsExtractStructFields.choose.name.dialog.invalid.name"), input)
}
return null
}
Expand Down
Expand Up @@ -45,6 +45,8 @@ abstract class BaseGenerateHandler : LanguageCodeInsightActionHandler {

override fun startInWriteAction() = false

open val allowEmptySelection: Boolean = false

private fun getContext(editor: Editor, file: PsiFile): Context? {
val element = file.findElementAt(editor.caretModel.offset) ?: return null
val struct = element.ancestorOrSelf<RsStructItem>()
Expand Down Expand Up @@ -80,7 +82,8 @@ abstract class BaseGenerateHandler : LanguageCodeInsightActionHandler {
context.struct.project,
context.struct,
context.fields,
dialogTitle
dialogTitle,
allowEmptySelection
) ?: return
runWriteAction {
performRefactoring(context.struct, context.implBlock, chosenFields, context.substitution, editor)
Expand Down
Expand Up @@ -27,6 +27,8 @@ class GenerateConstructorAction : BaseGenerateAction() {
class GenerateConstructorHandler : BaseGenerateHandler() {
override val dialogTitle: String = "Select constructor parameters"

override val allowEmptySelection: Boolean = true

override fun isImplBlockValid(impl: RsImplItem): Boolean = super.isImplBlockValid(impl) &&
impl.isSuitableForConstructor

Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/org/rust/ide/refactoring/generate/ui.kt
Expand Up @@ -22,12 +22,13 @@ fun showStructMemberChooserDialog(
project: Project,
structItem: RsStructItem,
fields: List<StructMember>,
@Suppress("UnstableApiUsage") @DialogTitle title: String
@Suppress("UnstableApiUsage") @DialogTitle title: String,
allowEmptySelection: Boolean
): List<StructMember>? {
val chooser = if (isUnitTestMode) {
MOCK ?: error("You should set mock ui via `withMockStructMemberChooserUi`")
} else {
DialogStructMemberChooserUi(title)
DialogStructMemberChooserUi(title, allowEmptySelection = allowEmptySelection)
}
val base = MemberChooserObjectBase(structItem.name, structItem.getIcon(0))
val arguments = fields.map { RsStructMemberChooserObject(base, it) }
Expand Down Expand Up @@ -60,11 +61,15 @@ interface StructMemberChooserUi {
}

private class DialogStructMemberChooserUi(
@Suppress("UnstableApiUsage") @DialogTitle private val title: String
@Suppress("UnstableApiUsage") @DialogTitle private val title: String,
private val allowEmptySelection: Boolean
) : StructMemberChooserUi {
override fun selectMembers(project: Project, all: List<RsStructMemberChooserObject>): List<RsStructMemberChooserObject>? {
override fun selectMembers(
project: Project,
all: List<RsStructMemberChooserObject>
): List<RsStructMemberChooserObject>? {
val dialogTitle = title
val chooser = MemberChooser(all.toTypedArray(), true, true, project).apply {
val chooser = MemberChooser(all.toTypedArray(), allowEmptySelection, true, project).apply {
title = dialogTitle
selectElements(all.toTypedArray())
setCopyJavadocVisible(false)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages/RsBundle.properties
Expand Up @@ -47,7 +47,7 @@ action.Rust.ShareInPlayground.notification.title=Share in Rust Playground
action.Rust.ShareInPlayground.progress.title=Posting code to Rust Playground
action.Rust.ShareInPlayground.text=Share in Playground
action.Rust.RsExtractStructFields.description=Extract selected struct fields into a separate struct
action.Rust.RsExtractStructFields.text=Extract Struct Fields
action.Rust.RsExtractStructFields.text=Extract Struct Fields...
action.Rust.RsExtractStructFields.intention.text=Extract struct fields
action.Rust.RsExtractStructFields.choose.fields.title=Choose Fields to Extract
action.Rust.RsExtractStructFields.choose.name.dialog.title=Enter Name For the New Struct
Expand Down

0 comments on commit b68ecfe

Please sign in to comment.