-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve IDE settings change logic on users' behalf from codewhisperer…
… to ensure changes are reverted (#4582)
- Loading branch information
1 parent
a8d51e3
commit 9cfe9f4
Showing
5 changed files
with
257 additions
and
19 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
.changes/next-release/bugfix-a6c159d0-f2cb-469d-9c46-49a378eaaf60.json
This file contains 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,4 @@ | ||
{ | ||
"type" : "bugfix", | ||
"description" : "Fix IDE auto completion settings potentially overwritten by Q inline suggestion" | ||
} |
This file contains 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 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
98 changes: 98 additions & 0 deletions
98
...software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeInsightsSettingsFacade.kt
This file contains 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,98 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.jetbrains.services.codewhisperer.util | ||
|
||
import com.intellij.codeInsight.CodeInsightSettings | ||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.util.Disposer | ||
import com.intellij.openapi.util.SimpleModificationTracker | ||
import org.jetbrains.annotations.VisibleForTesting | ||
import software.aws.toolkits.core.utils.error | ||
import software.aws.toolkits.core.utils.getLogger | ||
import kotlin.reflect.KMutableProperty | ||
|
||
class CodeInsightsSettingsFacade : SimpleModificationTracker(), Disposable { | ||
private inner class ChangeAndRevert<T : Any>( | ||
val p: KMutableProperty<T>, | ||
val value: T, | ||
parentDisposable: Disposable | ||
) { | ||
val origin: T = p.getter.call() | ||
var isComplete: Boolean = false | ||
private set | ||
|
||
init { | ||
Disposer.register(parentDisposable) { | ||
revert() | ||
} | ||
} | ||
|
||
fun commit(): ChangeAndRevert<T> { | ||
p.setter.call(value) | ||
return this | ||
} | ||
|
||
fun revert() { | ||
if (isComplete) { | ||
return | ||
} | ||
|
||
p.setter.call(origin) | ||
isComplete = true | ||
} | ||
} | ||
|
||
private val settings by lazy { | ||
CodeInsightSettings.getInstance() | ||
} | ||
|
||
private var pendingReverts = listOf<ChangeAndRevert<*>>() | ||
val pendingRevertCounts: Int | ||
get() = pendingReverts.size | ||
|
||
@VisibleForTesting | ||
internal fun revertAll() { | ||
if (pendingReverts.count { !it.isComplete } == 0) { | ||
return | ||
} | ||
|
||
pendingReverts.forEach { | ||
it.revert() | ||
} | ||
|
||
pendingReverts = pendingReverts.filter { | ||
!it.isComplete | ||
}.toMutableList() | ||
} | ||
|
||
fun disableCodeInsightUntil(parentDisposable: Disposable) { | ||
revertAll() | ||
val toReverts = mutableListOf<ChangeAndRevert<*>>() | ||
|
||
ChangeAndRevert(settings::TAB_EXITS_BRACKETS_AND_QUOTES, false, parentDisposable).commit().also { | ||
toReverts.add(it) | ||
} | ||
|
||
ChangeAndRevert(settings::AUTO_POPUP_COMPLETION_LOOKUP, false, parentDisposable).commit().also { | ||
toReverts.add(it) | ||
} | ||
|
||
if (pendingReverts.count { !it.isComplete } != 0) { | ||
LOG.error { "trying to overwrite users' settings without reverting all previous overwrites" } | ||
} | ||
pendingReverts = toReverts | ||
|
||
Disposer.register(parentDisposable) { | ||
revertAll() | ||
} | ||
} | ||
|
||
override fun dispose() { | ||
revertAll() | ||
} | ||
|
||
companion object { | ||
val LOG = getLogger<CodeInsightsSettingsFacade>() | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
...ware/aws/toolkits/jetbrains/services/codewhisperer/util/CodeInsightsSettingsFacadeTest.kt
This file contains 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,137 @@ | ||
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package software.aws.toolkits.jetbrains.services.codewhisperer.util | ||
|
||
import com.intellij.codeInsight.CodeInsightSettings | ||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.util.Disposer | ||
import com.intellij.testFramework.ProjectExtension | ||
import com.intellij.testFramework.junit5.TestDisposable | ||
import com.intellij.testFramework.replaceService | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import org.mockito.kotlin.spy | ||
import org.mockito.kotlin.times | ||
import org.mockito.kotlin.verify | ||
|
||
class CodeInsightsSettingsFacadeTest { | ||
private lateinit var settings: CodeInsightSettings | ||
private lateinit var sut: CodeInsightsSettingsFacade | ||
|
||
@TestDisposable | ||
private lateinit var disposable: Disposable | ||
|
||
companion object { | ||
@JvmField | ||
@RegisterExtension | ||
val projectExtension = ProjectExtension() | ||
} | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
sut = spy(CodeInsightsSettingsFacade()) | ||
settings = spy { CodeInsightSettings() } | ||
|
||
ApplicationManager.getApplication().replaceService( | ||
CodeInsightSettings::class.java, | ||
settings, | ||
disposable | ||
) | ||
} | ||
|
||
@Test | ||
fun `disableCodeInsightUntil should revert when parent is disposed`() { | ||
val myFakePopup = Disposable {}.also { | ||
Disposer.register(disposable, it) | ||
} | ||
|
||
// assume users' enable the following two codeinsight functionalities | ||
settings.TAB_EXITS_BRACKETS_AND_QUOTES = true | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
settings.AUTOCOMPLETE_ON_CODE_COMPLETION = true | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
|
||
// codewhisperer disable them while popup is shown | ||
sut.disableCodeInsightUntil(myFakePopup) | ||
|
||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isFalse | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isFalse | ||
assertThat(sut.pendingRevertCounts).isEqualTo(2) | ||
|
||
// popup is closed and disposed | ||
Disposer.dispose(myFakePopup) | ||
|
||
// revert changes made by codewhisperer | ||
verify(sut, times(2)).revertAll() | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
} | ||
|
||
@Test | ||
fun `revertAll should revert back all changes made by codewhisperer`() { | ||
settings.TAB_EXITS_BRACKETS_AND_QUOTES = true | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
settings.AUTOCOMPLETE_ON_CODE_COMPLETION = true | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
|
||
sut.disableCodeInsightUntil(disposable) | ||
|
||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isFalse | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isFalse | ||
|
||
assertThat(sut.pendingRevertCounts).isEqualTo(2) | ||
|
||
sut.revertAll() | ||
assertThat(sut.pendingRevertCounts).isEqualTo(0) | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
} | ||
|
||
@Test | ||
fun `disableCodeInsightUntil should always flush pending reverts before making next changes`() { | ||
val myFakePopup = Disposable {}.also { | ||
Disposer.register(disposable, it) | ||
} | ||
val myAnotherFakePopup = Disposable {}.also { | ||
Disposer.register(disposable, it) | ||
} | ||
|
||
// assume users' enable the following two codeinsight functionalities | ||
settings.TAB_EXITS_BRACKETS_AND_QUOTES = true | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
settings.AUTOCOMPLETE_ON_CODE_COMPLETION = true | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
|
||
// codewhisperer disable them while popup_1 is shown | ||
sut.disableCodeInsightUntil(myFakePopup) | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isFalse | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isFalse | ||
assertThat(sut.pendingRevertCounts).isEqualTo(2) | ||
verify(sut, times(1)).revertAll() | ||
|
||
// unexpected issue happens and popup_1 is not disposed correctly and popup_2 is created | ||
sut.disableCodeInsightUntil(myAnotherFakePopup) | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isFalse | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isFalse | ||
// should still be 2 because previous ones should be reverted before preceding next changes | ||
assertThat(sut.pendingRevertCounts).isEqualTo(2) | ||
verify(sut, times(1 + 1)).revertAll() | ||
|
||
Disposer.dispose(myAnotherFakePopup) | ||
|
||
assertThat(sut.pendingRevertCounts).isEqualTo(0) | ||
verify(sut, times(1 + 1 + 1)).revertAll() | ||
assertThat(settings.TAB_EXITS_BRACKETS_AND_QUOTES).isTrue | ||
assertThat(settings.AUTO_POPUP_COMPLETION_LOOKUP).isTrue | ||
} | ||
|
||
@Test | ||
fun `dispose should call revertAll to revert all changes made by CodeWhisperer`() { | ||
sut.dispose() | ||
verify(sut).revertAll() | ||
} | ||
} |