[github] Ability to leave a comment in PR diff using keyboard

Fixes IDEA-247904

GitOrigin-RevId: 18d24b0d22ce0aaaa0d51da6afce3614eb10ea54
This commit is contained in:
Ivan Semenov
2020-08-17 10:48:58 +00:00
committed by intellij-monorepo-bot
parent 4992d8e15b
commit bfaa1fb147
3 changed files with 64 additions and 0 deletions
@@ -202,5 +202,11 @@
<separator/>
<action id="Github.Accounts.AddGHEAccount" class="org.jetbrains.plugins.github.authentication.ui.AddGHEAccountAction"/>
</group>
<action id="Github.PullRequest.Diff.Comment.Create"
class="org.jetbrains.plugins.github.pullrequest.comment.action.GHPRCreateDiffCommentAction">
<keyboard-shortcut first-keystroke="control shift X" keymap="$default"/>
<add-to-group group-id="Diff.EditorPopupMenu"/>
</action>
</actions>
</idea-plugin>
@@ -286,6 +286,7 @@ pull.request.diff.editor.add.single.comment=Add a single comment
pull.request.diff.editor.review.comment=Comment
pull.request.diff.editor.review.start=Start review
pull.request.diff.editor.review.with.comment=Start a review with a comment
action.Github.PullRequest.Diff.Comment.Create.text=Add Review Comment
pull.request.review.submit.request.changes=Request Changes
pull.request.review.submit.approve.button=Approve
pull.request.review.submit.comment.button=Comment
@@ -0,0 +1,57 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.pullrequest.comment.action
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.actionSystem.ex.ActionUtil
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.ex.RangeHighlighterEx
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.util.CommonProcessors
import org.jetbrains.plugins.github.pullrequest.comment.ui.GHPRAddCommentGutterIconRenderer
class GHPRCreateDiffCommentAction : DumbAwareAction() {
override fun update(e: AnActionEvent) {
e.presentation.isEnabledAndVisible = isEnabledAndVisible(e)
}
private fun isEnabledAndVisible(e: AnActionEvent): Boolean {
val editor = e.getData(CommonDataKeys.EDITOR) ?: return false
return findRendererActionUnderCaret(editor) != null
}
override fun actionPerformed(e: AnActionEvent) {
val editor = e.getRequiredData(CommonDataKeys.EDITOR)
val action = findRendererActionUnderCaret(editor) ?: return
val scrollingModel = editor.scrollingModel
scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE)
scrollingModel.runActionOnScrollingFinished {
if (action is ActionGroup) {
val point = editor.visualPositionToXY(editor.caretModel.visualPosition)
ActionManager.getInstance().createActionPopupMenu(ActionPlaces.EDITOR_GUTTER_POPUP, action).component
.show(editor.component, point.x, point.y - scrollingModel.verticalScrollOffset)
}
else ActionUtil.invokeAction(action, editor.component, ActionPlaces.EDITOR_GUTTER, e.inputEvent, null)
}
}
private fun findRendererActionUnderCaret(editor: Editor): AnAction? {
val markupModel = (editor as? EditorEx)?.markupModel ?: return null
val logicalPosition = editor.caretModel.logicalPosition
val line = logicalPosition.line
val offset = editor.logicalPositionToOffset(logicalPosition)
val findProcessor = object : CommonProcessors.FindProcessor<RangeHighlighterEx>() {
override fun accept(t: RangeHighlighterEx): Boolean {
val gutterIconRenderer = t.gutterIconRenderer
return gutterIconRenderer is GHPRAddCommentGutterIconRenderer && gutterIconRenderer.line == line
}
}
markupModel.processRangeHighlightersOverlappingWith(offset, offset, findProcessor)
val renderer = findProcessor.foundValue?.gutterIconRenderer ?: return null
return renderer.clickAction ?: renderer.popupMenuActions
}
}