Skip to content

Commit

Permalink
Improve detection of read-only files
Browse files Browse the repository at this point in the history
Fixes #491.
  • Loading branch information
FWDekker committed Nov 12, 2023
1 parent 64378e7 commit 27c9dec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/fwdekker/randomness/InsertAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/fwdekker/randomness/PopupAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/test/kotlin/com/fwdekker/randomness/InsertActionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 27c9dec

Please sign in to comment.