Skip to content

Commit 3a762c6

Browse files
andrewyuqrli
andauthored
Fix Enter action will add incorrect number of newlines when triggered inside braces (#3278)
1. When enter action is triggered inside braces, an EnterBetweenBracesFinalHandler will be invoked which is an EnterHandlerDelegate, this delegate will call originalHandler.execute() to add an extra newline to correctly put the closing brace in another new line. Inside CodeWhispererEnterHandler we need to call originalHandler.execute() instead of super.executeWriteAction() to avoid adding another(3rd) newline character in the document. 2. Since the originalHandler in EnterHandler is private not protected, need to make the originalHander in CodeWhispererEnterHandler a private field. Co-authored-by: Richard Li <[email protected]> Co-authored-by: Richard Li <[email protected]>
1 parent bcaac64 commit 3a762c6

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Fix hitting enter inside braces will produce an extra newline (#3270)"
4+
}

jetbrains-core/resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ with what features/services are supported.
320320
<registryKey key="aws.toolkit.developerMode" description="Enables features to facilitate development of the toolkit" restartRequired="false" defaultValue="false"/>
321321
<typedHandler implementation="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererTypedHandler"/>
322322
<editorFactoryListener implementation="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorListener"/>
323-
<editorActionHandler action="EditorEnter" implementationClass="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEnterHandler"/>
323+
<editorActionHandler action="EditorEnter" implementationClass="software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEnterHandler" order="first, before editorEnter"/>
324324
<actionPromoter order="last" implementation="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererActionPromoter"/>
325325
</extensions>
326326

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEnterHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispe
1212
import software.aws.toolkits.telemetry.CodewhispererAutomatedTriggerType
1313
import software.aws.toolkits.telemetry.CodewhispererTriggerType
1414

15-
class CodeWhispererEnterHandler(originalHandler: EditorActionHandler) :
15+
class CodeWhispererEnterHandler(private val originalHandler: EditorActionHandler) :
1616
EnterHandler(originalHandler),
1717
CodeWhispererAutoTriggerHandler {
1818
override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext?) {
19-
super.executeWriteAction(editor, caret, dataContext)
19+
originalHandler.execute(editor, caret, dataContext)
2020
if (!CodeWhispererService.getInstance().canDoInvocation(editor, CodewhispererTriggerType.AutoTrigger)) {
2121
return
2222
}

jetbrains-core/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestBase.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ open class CodeWhispererTestBase {
122122
editorManager = CodeWhispererEditorManager.getInstance()
123123
settingsManager = CodeWhispererSettings.getInstance()
124124

125-
setPrompt(pythonFileName, pythonTestLeftContext)
125+
setFileContext(pythonFileName, pythonTestLeftContext, "")
126126

127127
originalExplorerActionState = stateManager.state
128128
originalSettings = settingsManager.state
@@ -178,10 +178,10 @@ open class CodeWhispererTestBase {
178178
assertThat(actual).isEqualTo(expected)
179179
}
180180

181-
fun setPrompt(filename: String, prompt: String) {
182-
projectRule.fixture.configureByText(filename, prompt)
181+
fun setFileContext(filename: String, leftContext: String, rightContext: String) {
182+
projectRule.fixture.configureByText(filename, leftContext + rightContext)
183183
runInEdtAndWait {
184-
projectRule.fixture.editor.caretModel.primaryCaret.moveToOffset(projectRule.fixture.editor.document.textLength)
184+
projectRule.fixture.editor.caretModel.primaryCaret.moveToOffset(leftContext.length)
185185
}
186186
}
187187
}

jetbrains-core/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererUserActionsTest.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_TEXT_START_WIT
1515
import com.intellij.openapi.command.WriteCommandAction
1616
import com.intellij.openapi.ui.popup.JBPopup
1717
import com.intellij.testFramework.runInEdtAndWait
18+
import org.assertj.core.api.Assertions.assertThat
1819
import org.junit.Before
1920
import org.junit.Test
2021
import org.mockito.kotlin.any
2122
import org.mockito.kotlin.argumentCaptor
2223
import org.mockito.kotlin.timeout
23-
import org.mockito.kotlin.times
2424
import org.mockito.kotlin.verify
25+
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.javaFileName
2526
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonFileName
2627
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonTestLeftContext
2728
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
@@ -104,9 +105,26 @@ class CodeWhispererUserActionsTest : CodeWhispererTestBase() {
104105
testHittingEnterAfterWhitespaceCharsShouldTriggerCodeWhisperer("$pythonTestLeftContext\n", 3)
105106
}
106107

108+
@Test
109+
fun `test hitting enter inside braces in Java file should auto-trigger CodeWhisperer and keep the formatting correct`() {
110+
val testLeftContext = "public class Test {\n public static void main() {"
111+
val testRightContext = "}\n}"
112+
setFileContext(javaFileName, testLeftContext, testRightContext)
113+
CodeWhispererExplorerActionManager.getInstance().setAutoEnabled(true)
114+
projectRule.fixture.type('\n')
115+
val expectedFileContext = "$testLeftContext\n \n $testRightContext"
116+
assertThat(projectRule.fixture.editor.document.text).isEqualTo(expectedFileContext)
117+
val popupCaptor = argumentCaptor<JBPopup>()
118+
verify(popupManagerSpy, timeout(5000))
119+
.showPopup(any(), any(), any(), any(), any(), popupCaptor.capture(), any(), any())
120+
runInEdtAndWait {
121+
popupManagerSpy.closePopup(popupCaptor.lastValue)
122+
}
123+
}
124+
107125
private fun testHittingEnterAfterWhitespaceCharsShouldTriggerCodeWhisperer(prompt: String, times: Int) {
108126
CodeWhispererExplorerActionManager.getInstance().setAutoEnabled(true)
109-
setPrompt(pythonFileName, prompt)
127+
setFileContext(pythonFileName, prompt, "")
110128
projectRule.fixture.type('\n')
111129
val popupCaptor = argumentCaptor<JBPopup>()
112130
verify(popupManagerSpy, timeout(5000).atLeast(times))

0 commit comments

Comments
 (0)