From 27c9decb8117baa76979a33c127a0d37bff46659 Mon Sep 17 00:00:00 2001 From: "Florine W. Dekker" Date: Sun, 12 Nov 2023 16:44:21 +0100 Subject: [PATCH] Improve detection of read-only files Fixes #491. --- .../com/fwdekker/randomness/InsertAction.kt | 2 +- .../kotlin/com/fwdekker/randomness/PopupAction.kt | 2 +- .../com/fwdekker/randomness/InsertActionTest.kt | 15 +++++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/fwdekker/randomness/InsertAction.kt b/src/main/kotlin/com/fwdekker/randomness/InsertAction.kt index b5516b5a4..70e6b6aa5 100644 --- a/src/main/kotlin/com/fwdekker/randomness/InsertAction.kt +++ b/src/main/kotlin/com/fwdekker/randomness/InsertAction.kt @@ -52,7 +52,7 @@ abstract class InsertAction( val presentation = event.presentation val editor = event.getData(CommonDataKeys.EDITOR) - presentation.isEnabled = editor != null + presentation.isEnabled = editor?.document?.isWritable == true } /** diff --git a/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt b/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt index 2938349f8..b1dae4c11 100644 --- a/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt +++ b/src/main/kotlin/com/fwdekker/randomness/PopupAction.kt @@ -41,7 +41,7 @@ class PopupAction : AnAction(Icons.RANDOMNESS) { event.presentation.icon = Icons.RANDOMNESS // Running this in [actionPerformed] always sets it to `true` - isEditable = event.getData(CommonDataKeys.EDITOR)?.isViewer == false + isEditable = event.getData(CommonDataKeys.EDITOR)?.document?.isWritable == true } /** diff --git a/src/test/kotlin/com/fwdekker/randomness/InsertActionTest.kt b/src/test/kotlin/com/fwdekker/randomness/InsertActionTest.kt index 50452a69b..708b02ee1 100644 --- a/src/test/kotlin/com/fwdekker/randomness/InsertActionTest.kt +++ b/src/test/kotlin/com/fwdekker/randomness/InsertActionTest.kt @@ -181,22 +181,29 @@ class InsertActionTest : BasePlatformTestCase() { fun `test that it disables the presentation if the editor is null`() { val presentation = Presentation() - val event = AnActionEvent.createFromDataContext("", presentation) { null } - insertAction.update(event) + insertAction.update(AnActionEvent.createFromDataContext("", presentation) { null }) presentation.isEnabled shouldBe false } fun `test that it enables the presentation if the editor is not null`() { val presentation = Presentation() - val event = AnActionEvent.createFromDataContext("", presentation) { myFixture.editor } - insertAction.update(event) + insertAction.update(AnActionEvent.createFromDataContext("", presentation) { myFixture.editor }) presentation.isEnabled shouldBe true } + fun `test that it disables the presentation if the editor is read-only`() { + val presentation = Presentation() + + myFixture.editor.document.setReadOnly(true) + insertAction.update(AnActionEvent.createFromDataContext("", presentation) { myFixture.editor }) + + presentation.isEnabled shouldBe false + } + /** * Moves the primary caret to [offset] and returns the primary caret.