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

Replace old actions with new actions that have configurable key bindings #4914

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -81,9 +81,31 @@
</group>

<action class="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererRecommendationAction"
text="Show Code Suggestions">
text="Invoke Amazon Q Inline Suggestions">
<keyboard-shortcut keymap="$default" first-keystroke="alt C"/>
</action>
<action id="codewhisperer.inline.navigate.previous"
class="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererNavigatePrevAction"
text="Navigate to Previous Inline Suggestion" description="Navigate to previous inline suggestion">
<keyboard-shortcut keymap="$default" first-keystroke="alt OPEN_BRACKET"/>
</action>
<action id="codewhisperer.inline.navigate.next"
class="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererNavigateNextAction"
text="Navigate to Next Inline Suggestion" description="Navigate to next inline suggestion">
<keyboard-shortcut keymap="$default" first-keystroke="alt CLOSE_BRACKET"/>
</action>
<action id="codewhisperer.inline.accept"
class="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererAcceptAction"
text="Accept the Current Inline Suggestion" description="Accept the current inline suggestions">
<keyboard-shortcut keymap="$default" first-keystroke="TAB"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm a bit worried about the interaction between this and the IDE

Copy link
Contributor Author

@andrewyuq andrewyuq Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic will be what the promoter does -- when the CW is showing:
CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()
it will be promoted to CW accept.
Otherwise it will be IDE tab action.

</action>
<action id="codewhisperer.inline.force.accept"
class="software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererForceAcceptAction"
text="Force Accept the Current Inline Suggestion" description="Force accept the current inline suggestion">
<keyboard-shortcut keymap="$default" first-keystroke="alt TAB"/>
<keyboard-shortcut keymap="$default" first-keystroke="alt ENTER"/>
</action>

<group id="aws.toolkit.codewhisperer.toolbar.security">
<action
id="codewhisperer.toolbar.security.scan"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.codewhisperer.actions

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAware
import software.aws.toolkits.jetbrains.services.codewhisperer.model.SessionContext
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.CodeWhispererPopupManager
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererService
import software.aws.toolkits.resources.message

open class CodeWhispererAcceptAction(title: String = message("codewhisperer.inline.accept")) : AnAction(title), DumbAware {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sealed and move the force action here since there is no difference except the key binding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like I can't make CodeWhispererAcceptAction as sealed class because I will be getting this error:

Caused by: java.lang.NullPointerException: null cannot be cast to non-null type software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererAcceptAction

override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.project != null && e.getData(CommonDataKeys.EDITOR) != null &&
CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()
}

override fun actionPerformed(e: AnActionEvent) {
val sessionContext = e.project?.getUserData(CodeWhispererService.KEY_SESSION_CONTEXT) ?: return
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()) return
ApplicationManager.getApplication().messageBus.syncPublisher(
CodeWhispererPopupManager.CODEWHISPERER_USER_ACTION_PERFORMED
).beforeAccept(sessionContext)
}
}

// A same accept action but different key shortcut and different promoter logic
class CodeWhispererForceAcceptAction(title: String = message("codewhisperer.inline.force.accept")) : CodeWhispererAcceptAction(title)
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,50 @@

package software.aws.toolkits.jetbrains.services.codewhisperer.actions

import com.intellij.codeInsight.lookup.impl.actions.ChooseItemAction
import com.intellij.openapi.actionSystem.ActionPromoter
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.actionSystem.EditorAction
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.handlers.CodeWhispererPopupLeftArrowHandler
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.handlers.CodeWhispererPopupRightArrowHandler
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.handlers.CodeWhispererPopupTabHandler
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus

class CodeWhispererActionPromoter : ActionPromoter {
override fun promote(actions: MutableList<out AnAction>, context: DataContext): MutableList<AnAction> {
val results = actions.toMutableList()
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()) return results

results.sortWith { a, b ->
if (isCodeWhispererPopupAction(a)) {
if (isCodeWhispererForceAction(a)) {
return@sortWith -1
} else if (isCodeWhispererForceAction(b)) {
return@sortWith 1
}

if (a is ChooseItemAction) {
return@sortWith -1
} else if (isCodeWhispererPopupAction(b)) {
} else if (b is ChooseItemAction) {
return@sortWith 1
} else {
0
}

if (isCodeWhispererAcceptAction(a)) {
return@sortWith -1
} else if (isCodeWhispererAcceptAction(b)) {
return@sortWith 1
}

0
}
return results
}

private fun isCodeWhispererAcceptAction(action: AnAction): Boolean =
action is EditorAction && action.handler is CodeWhispererPopupTabHandler
action is CodeWhispererAcceptAction

private fun isCodeWhispererForceAcceptAction(action: AnAction): Boolean =
action is CodeWhispererForceAcceptAction

private fun isCodeWhispererNavigateAction(action: AnAction): Boolean =
action is EditorAction && (
action.handler is CodeWhispererPopupRightArrowHandler ||
action.handler is CodeWhispererPopupLeftArrowHandler
)
action is CodeWhispererNavigateNextAction || action is CodeWhispererNavigatePrevAction

private fun isCodeWhispererPopupAction(action: AnAction): Boolean =
isCodeWhispererAcceptAction(action) || isCodeWhispererNavigateAction(action)
private fun isCodeWhispererForceAction(action: AnAction): Boolean =
isCodeWhispererForceAcceptAction(action) || isCodeWhispererNavigateAction(action)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.codewhisperer.actions

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAware
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.CodeWhispererPopupManager
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererService
import software.aws.toolkits.resources.message

class CodeWhispererNavigateNextAction : AnAction(message("codewhisperer.inline.navigate.next")), DumbAware {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.project != null &&
e.getData(CommonDataKeys.EDITOR) != null &&
CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()
}

override fun actionPerformed(e: AnActionEvent) {
val sessionContext = e.project?.getUserData(CodeWhispererService.KEY_SESSION_CONTEXT) ?: return
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()) return
ApplicationManager.getApplication().messageBus.syncPublisher(
CodeWhispererPopupManager.CODEWHISPERER_USER_ACTION_PERFORMED
).navigateNext(sessionContext)
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR tho, i feel we could make this messagebus a project level

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob doesn't matter since we aren't going to have multple display sessions across different projects

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.codewhisperer.actions

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAware
import software.aws.toolkits.jetbrains.services.codewhisperer.popup.CodeWhispererPopupManager
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererInvocationStatus
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererService
import software.aws.toolkits.resources.message

class CodeWhispererNavigatePrevAction : AnAction(message("codewhisperer.inline.navigate.previous")), DumbAware {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.EDT

override fun update(e: AnActionEvent) {
e.presentation.isEnabled = e.project != null &&
e.getData(CommonDataKeys.EDITOR) != null &&
CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()
}

override fun actionPerformed(e: AnActionEvent) {
val sessionContext = e.project?.getUserData(CodeWhispererService.KEY_SESSION_CONTEXT) ?: return
if (!CodeWhispererInvocationStatus.getInstance().isDisplaySessionActive()) return
ApplicationManager.getApplication().messageBus.syncPublisher(
CodeWhispererPopupManager.CODEWHISPERER_USER_ACTION_PERFORMED
).navigatePrevious(sessionContext)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ package software.aws.toolkits.jetbrains.services.codewhisperer.popup
import com.intellij.icons.AllIcons
import com.intellij.ide.BrowserUtil
import com.intellij.idea.AppMode
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.ActionToolbar
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.actionSystem.KeyboardShortcut
import com.intellij.openapi.actionSystem.Presentation
import com.intellij.openapi.actionSystem.impl.ActionButton
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.keymap.MacKeymapUtil
import com.intellij.openapi.util.SystemInfo
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.components.ActionLink
import com.intellij.util.ui.UIUtil
Expand Down Expand Up @@ -43,10 +48,33 @@ import javax.swing.JPanel

class CodeWhispererPopupComponents {
val prevButton = createNavigationButton(
message("codewhisperer.popup.button.prev", POPUP_DIM_HEX)
message(
"codewhisperer.popup.button.prev",
POPUP_DIM_HEX,
run {
// TODO: Doesn't reflect dynamically if users change but didn't restart IDE
val shortcut = ActionManager.getInstance().getAction("codewhisperer.inline.navigate.previous")
.shortcutSet.shortcuts.first()
val keyStroke = (shortcut as KeyboardShortcut).firstKeyStroke
if (SystemInfo.isMac) {
MacKeymapUtil.getKeyStrokeText(keyStroke, " ", true)
} else {
KeymapUtil.getKeystrokeText(keyStroke)
}
}
)
)
val nextButton = createNavigationButton(
message("codewhisperer.popup.button.next", POPUP_DIM_HEX)
message(
"codewhisperer.popup.button.next",
POPUP_DIM_HEX,
run {
// TODO: Doesn't reflect dynamically if users change but didn't restart IDE
KeymapUtil.getFirstKeyboardShortcutText(
ActionManager.getInstance().getAction("codewhisperer.inline.navigate.next")
)
}
)
).apply {
preferredSize = prevButton.preferredSize
}
Expand Down
Loading