Skip to content

Commit

Permalink
CopyCommitReferenceAnnotationAction: new action
Browse files Browse the repository at this point in the history
Turns out that it is possible to add plugin's action to the context menu
of annotations in IntelliJ editor, and my assumptions about the
construction of that menu were incorrect.

Add CopyCommitReferenceGutterActionProvider to plugin.xml via the
<vcsAnnotationGutterActionProvider> extension.[1]  This class creates
instances of CopyCommitReferenceAnnotationAction.  Aside from the
obvious copying of the commit reference for the given annotation line,
this action also keeps track of the state of the annotations via
interface UpToDateLineNumberListener.[2]

Replace the incorrect assumption in README.md with a link to the TODO.md
file on the `todo` branch of the GitHub repository.

[1] Full list of extensions for plugins is available in JetBrains docs
    https://plugins.jetbrains.com/docs/intellij/plugin-extensions.html
    Extension <vcsAnnotationGutterActionProvider> is used by method
    com.intellij.openapi.vcs.actions.AnnotateToggleAction#addActionsFromExtensions
    I've originally found the extension in source code of plugin
    "Bitbucket Linky"
    https://bitbucket.org/atlassianlabs/intellij-bitbucket-references-plugin/src/0dbcdc83399faf933566ed1e238113f02d0ae45d/src/main/resources/META-INF/plugin.xml?at=master#lines-46:51
[2] The passing of up-to-date line numbers happens in method
    com.intellij.openapi.vcs.actions.AnnotationPresentation#getActions(int)
  • Loading branch information
rybak committed Jul 30, 2023
1 parent eba0a41 commit aeef267
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## [Unreleased]

### Added
- Added "Commit Commit Reference" action to the context menu of VCS annotations.

## [1.0.0] - 2023-07-10

### Added
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ and install it manually using
2. Launch run configuration "Run IDE for UI Tests"

## Plugin TODO list
- [ ] add action to the context menu of annotations, after action of class
`CopyRevisionNumberFromAnnotateAction`
- Not feasible as of IntelliJ 2023.1, because context menu of annotations
is custom-built. See [usage of class `CopyRevisionNumberFromAnnotateAction` in
`AnnotateToggleAction.java`][AnnotateToggleAction]
See file [TODO.md on branch `todo`][TODO]

---
Plugin based on the [IntelliJ Platform Plugin Template][template].
Expand All @@ -74,3 +70,4 @@ Plugin based on the [IntelliJ Platform Plugin Template][template].
[GitHubLatestRelease]: https://github.com/rybak/intellij-copy-commit-reference/releases/latest
[Marketplace]: https://plugins.jetbrains.com/plugin/22138-copy-commit-reference
[MarketplaceVersions]: https://plugins.jetbrains.com/plugin/22138-copy-commit-reference/versions
[TODO]: https://github.com/rybak/intellij-copy-commit-reference/blob/todo/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import com.intellij.openapi.vcs.history.VcsRevisionNumber
import com.intellij.vcs.log.VcsCommitMetadata

/**
* This context menu action provides easy access to the "reference" pretty format of Git.
* This context menu action provides easy access to the "reference" pretty format of Git in IntelliJ UI views that
* contain a Git log: tab "Log" of tool window "Git", table in the "History for Selection" dialog, etc.
* See [Git documentation of `git-log`](https://git-scm.com/docs/git-log#_pretty_formats) for details.
* It is similar to [com.intellij.openapi.vcs.history.actions.CopyRevisionNumberAction], but with more
* information: the reference format includes an abbreviated hash of the commit, subject line of the commit (first line
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dev.andrybak.intellij.copy_commit_reference

import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.UpdateInBackground
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.vcs.annotate.AnnotationGutterActionProvider
import com.intellij.openapi.vcs.annotate.FileAnnotation
import com.intellij.openapi.vcs.annotate.UpToDateLineNumberListener
import com.intellij.util.PlatformIcons

/**
* This context menu action provides easy access to the "reference" pretty format of Git in IntelliJ UI annotation
* gutter of the editor.
* See [Git documentation of `git-log`](https://git-scm.com/docs/git-log#_pretty_formats) for details.
* It is similar to [com.intellij.openapi.vcs.actions.CopyRevisionNumberFromAnnotateAction], but with more
* information: the reference format includes an abbreviated hash of the commit, subject line of the commit (first line
* of the commit message), and the date in ISO 8601 format.
*/
class CopyCommitReferenceAnnotationAction(private val annotation: FileAnnotation) : DumbAwareAction(
CopyCommitReferenceBundle.messagePointer("action.devAndrybakCopyCommitReferenceAction.text"),
CopyCommitReferenceBundle.messagePointer("action.devAndrybakCopyCommitReferenceAction.description"),
PlatformIcons.COPY_ICON
), UpToDateLineNumberListener, UpdateInBackground {

private var lineNumber = -1

override fun actionPerformed(e: AnActionEvent) {
if (lineNumber < 0) {
return
}
val revisionNumber = annotation.getLineRevisionNumber(lineNumber)
if (revisionNumber != null) {
// non-nullity is enforced by method `update()`
val project = e.project!!
val actionName = templateText ?: "Bundle is broken for CopyCommitReferenceAnnotationAction"
getCommitMetadata(project, actionName, listOf(revisionNumber)) { listOfMetadata ->
copyCommitReference(project, listOfMetadata)
}
}
}

override fun update(e: AnActionEvent) {
val enabled = e.project != null && lineNumber >= 0 && annotation.getLineRevisionNumber(lineNumber) != null
e.presentation.setEnabledAndVisible(enabled)
}

/**
* Consumer the line number given to [UpToDateLineNumberListener]s, same as
* [com.intellij.openapi.vcs.actions.CopyRevisionNumberFromAnnotateAction].
*/
override fun consume(upToDateLineNumber: Int) {
lineNumber = upToDateLineNumber
}
}

class CopyCommitReferenceGutterActionProvider : AnnotationGutterActionProvider {
override fun createAction(annotation: FileAnnotation): AnAction = CopyCommitReferenceAnnotationAction(annotation)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.andrybak.intellij.copy_commit_reference

import com.intellij.DynamicBundle
import org.jetbrains.annotations.PropertyKey
import java.util.function.Supplier

private const val COPY_COMMIT_REFERENCE_BUNDLE_PATH = "messages.CopyCommitReferenceBundle"

object CopyCommitReferenceBundle : DynamicBundle(COPY_COMMIT_REFERENCE_BUNDLE_PATH) {
fun messagePointer(@PropertyKey(resourceBundle = COPY_COMMIT_REFERENCE_BUNDLE_PATH) key: String): Supplier<String> {
return getLazyMessage(key)
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
/>
</action>
</actions>
<extensions defaultExtensionNs="com.intellij">
<vcsAnnotationGutterActionProvider
implementation="dev.andrybak.intellij.copy_commit_reference.CopyCommitReferenceGutterActionProvider"/>
</extensions>
</idea-plugin>

0 comments on commit aeef267

Please sign in to comment.