generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CopyCommitReferenceAnnotationAction: new action
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
Showing
6 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...kotlin/dev/andrybak/intellij/copy_commit_reference/CopyCommitReferenceAnnotationAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/dev/andrybak/intellij/copy_commit_reference/CopyCommitReferenceBundle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters