Skip to content

Commit

Permalink
Disallow generation of getters, setters and extraction of fields with…
Browse files Browse the repository at this point in the history
… no fields selected
  • Loading branch information
Kobzol committed Aug 1, 2022
1 parent b576897 commit 1c97efe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 1c97efe

Please sign in to comment.