Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Enter action will add incorrect number of newlines when triggered inside braces #3278

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Fix hitting enter inside braces will produce an extra newline (#3270)"
}
2 changes: 1 addition & 1 deletion jetbrains-core/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ with what features/services are supported.
<registryKey key="aws.toolkit.developerMode" description="Enables features to facilitate development of the toolkit" restartRequired="false" defaultValue="false"/>
<typedHandler implementation="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererTypedHandler"/>
<editorFactoryListener implementation="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorListener"/>
<editorActionHandler action="EditorEnter" implementationClass="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEnterHandler"/>
<editorActionHandler action="EditorEnter" implementationClass="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEnterHandler" order="first, before editorEnter"/>
<actionPromoter order="last" implementation="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererActionPromoter"/>
</extensions>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispe
import software.aws.toolkits.telemetry.CodewhispererAutomatedTriggerType
import software.aws.toolkits.telemetry.CodewhispererTriggerType

class CodeWhispererEnterHandler(originalHandler: EditorActionHandler) :
class CodeWhispererEnterHandler(private val originalHandler: EditorActionHandler) :
EnterHandler(originalHandler),
CodeWhispererAutoTriggerHandler {
override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext?) {
super.executeWriteAction(editor, caret, dataContext)
originalHandler.execute(editor, caret, dataContext)
if (!CodeWhispererService.getInstance().canDoInvocation(editor, CodewhispererTriggerType.AutoTrigger)) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ open class CodeWhispererTestBase {
editorManager = CodeWhispererEditorManager.getInstance()
settingsManager = CodeWhispererSettings.getInstance()

setPrompt(pythonFileName, pythonTestLeftContext)
setFileContext(pythonFileName, pythonTestLeftContext, "")

originalExplorerActionState = stateManager.state
originalSettings = settingsManager.state
Expand Down Expand Up @@ -178,10 +178,10 @@ open class CodeWhispererTestBase {
assertThat(actual).isEqualTo(expected)
}

fun setPrompt(filename: String, prompt: String) {
projectRule.fixture.configureByText(filename, prompt)
fun setFileContext(filename: String, leftContext: String, rightContext: String) {
projectRule.fixture.configureByText(filename, leftContext + rightContext)
runInEdtAndWait {
projectRule.fixture.editor.caretModel.primaryCaret.moveToOffset(projectRule.fixture.editor.document.textLength)
projectRule.fixture.editor.caretModel.primaryCaret.moveToOffset(leftContext.length)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_TEXT_START_WIT
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.ui.popup.JBPopup
import com.intellij.testFramework.runInEdtAndWait
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.timeout
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.javaFileName
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonFileName
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonTestLeftContext
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
Expand Down Expand Up @@ -104,9 +105,26 @@ class CodeWhispererUserActionsTest : CodeWhispererTestBase() {
testHittingEnterAfterWhitespaceCharsShouldTriggerCodeWhisperer("$pythonTestLeftContext\n", 3)
}

@Test
fun `test hitting enter inside braces in Java file should auto-trigger CodeWhisperer and keep the formatting correct`() {
val testLeftContext = "public class Test {\n public static void main() {"
val testRightContext = "}\n}"
setFileContext(javaFileName, testLeftContext, testRightContext)
CodeWhispererExplorerActionManager.getInstance().setAutoEnabled(true)
projectRule.fixture.type('\n')
val expectedFileContext = "$testLeftContext\n \n $testRightContext"
assertThat(projectRule.fixture.editor.document.text).isEqualTo(expectedFileContext)
val popupCaptor = argumentCaptor<JBPopup>()
verify(popupManagerSpy, timeout(5000))
.showPopup(any(), any(), any(), any(), any(), popupCaptor.capture(), any(), any())
runInEdtAndWait {
popupManagerSpy.closePopup(popupCaptor.lastValue)
}
}

private fun testHittingEnterAfterWhitespaceCharsShouldTriggerCodeWhisperer(prompt: String, times: Int) {
CodeWhispererExplorerActionManager.getInstance().setAutoEnabled(true)
setPrompt(pythonFileName, prompt)
setFileContext(pythonFileName, prompt, "")
projectRule.fixture.type('\n')
val popupCaptor = argumentCaptor<JBPopup>()
verify(popupManagerSpy, timeout(5000).atLeast(times))
Expand Down