Skip to content

Commit

Permalink
feat: allow moving over right scope parenthesis with tab
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd committed Nov 26, 2019
1 parent b7afc47 commit 004cbd8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/kotlin/com/github/lppedd/cc/action/CommitTabAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.lppedd.cc.action

import com.intellij.openapi.editor.actions.TabAction

/**
* @author Edoardo Luppi
*/
internal class CommitTabAction : TabAction() {
init {
setupHandler(CommitTabHandler())
}
}
50 changes: 50 additions & 0 deletions src/main/kotlin/com/github/lppedd/cc/action/CommitTabHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.lppedd.cc.action

import com.github.lppedd.cc.CCEditorUtils
import com.github.lppedd.cc.CCParser
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorModificationUtil
import com.intellij.openapi.editor.actions.TabAction.Handler
import com.intellij.openapi.util.Key
import com.intellij.openapi.vcs.ui.CommitMessage

/**
* @author Edoardo Luppi
*/
internal class CommitTabHandler : Handler() {
companion object {
private val MOVE_CARET = Key.create<Unit>("Vcs.CommitMessage.moveCaret")
}

override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext) {
val document = editor.document

if (document.getUserData(MOVE_CARET) != null) {
EditorModificationUtil.moveCaretRelatively(editor, 1)
document.putUserData(MOVE_CARET, null)
} else {
super.executeWriteAction(editor, caret, dataContext)
}
}

override fun isEnabled(editor: Editor, dataContext: DataContext): Boolean {
val document = editor.document

if (document.getUserData(CommitMessage.DATA_KEY) != null) {
val lineRange = CCEditorUtils.getCurrentLineRange(editor)
val lineText = document.text.substring(lineRange.first, lineRange.last)
val lineCaretOffset = editor.caretModel.offset - lineRange.first
val scope = CCParser.parseText(lineText).scope

if (scope.isValid && lineCaretOffset == scope.range.last) {
document.putUserData(MOVE_CARET, Unit)
return true
}
}

@Suppress("DEPRECATION")
return super.isEnabled(editor, dataContext)
}
}
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

<resource-bundle>messages.ConventionalCommitBundle</resource-bundle>

<actions>
<action
id="EditorTab"
overrides="true"
class="com.github.lppedd.cc.action.CommitTabAction"
/>
</actions>

<extensions defaultExtensionNs="com.intellij">
<iconProvider implementation="com.github.lppedd.cc.icon.CCConfigFileIconProvider" />

Expand Down

0 comments on commit 004cbd8

Please sign in to comment.