Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active December 14, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/5004622 to your computer and use it in GitHub Desktop.
Save dkandalov/5004622 to your computer and use it in GitHub Desktop.
Watch change list size micro-plugin
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diff.impl.ComparisonPolicy
import com.intellij.openapi.diff.impl.fragments.LineFragment
import com.intellij.openapi.diff.impl.processing.DiffPolicy
import com.intellij.openapi.diff.impl.processing.TextCompareProcessor
import com.intellij.openapi.diff.impl.util.TextDiffTypeEnum
import com.intellij.openapi.vcs.changes.Change
import com.intellij.openapi.vcs.changes.ChangeList
import com.intellij.openapi.vcs.changes.ChangeListManager
import org.jetbrains.annotations.Nullable
import static com.intellij.openapi.diff.impl.util.TextDiffTypeEnum.CHANGED
import static com.intellij.openapi.diff.impl.util.TextDiffTypeEnum.CONFLICT
import static com.intellij.openapi.diff.impl.util.TextDiffTypeEnum.DELETED
import static com.intellij.openapi.diff.impl.util.TextDiffTypeEnum.INSERT
import static liveplugin.PluginUtil.*
// This is micro-plugin for IntelliJ to watch the size of current change list and notify if it exceeds limit
// (Note that this code can only be executed within this plugin https://github.com/dkandalov/live-plugin)
int changedLinesLimit = 20
int delayBetweenNotificationsInSeconds = 60
boolean isWatching = false
registerAction("WatchChangeListSize", "alt shift W") { AnActionEvent event ->
def project = event.project
def notificationDelay = 0
def checkChangeListSize = {
int linesChanged = amountOfChangedLinesIn(ChangeListManager.getInstance(project).defaultChangeList)
if (linesChanged > changedLinesLimit) {
if (notificationDelay <= 0) {
def title = "Change Limit Exceeded"
def message = "Lines changed: ${linesChanged}; Limit: ${changedLinesLimit}<br/>Please consider commiting or reverting changes"
show(message, title, NotificationType.WARNING)
notificationDelay = delayBetweenNotificationsInSeconds
} else {
notificationDelay--
}
} else {
notificationDelay = 0
}
}
if (!isWatching) {
isWatching = true
new Thread({
show("Started watching change list size")
while (isWatching) {
sleep(1000)
checkChangeListSize()
}
show("Stopped watching change list size")
}).start()
} else {
isWatching = false
}
}
show("Use 'alt shift W' to start/stop watching default change list size")
int amountOfChangedLinesIn(ChangeList changeList) {
def compareProcessor = new TextCompareProcessor(ComparisonPolicy.IGNORE_SPACE)
changeList.changes.sum(0) { change ->
if (change.beforeRevision == null) return change.afterRevision.content.split("\n").size()
if (change.afterRevision == null) return change.beforeRevision.content.split("\n").size()
def lineFragments = compareProcessor.process(change.beforeRevision.content, change.afterRevision.content)
lineFragments.sum { LineFragment fragment ->
fragment.with {
if (type == DELETED) modifiedLines1
else if (type in [CHANGED, INSERT, CONFLICT]) modifiedLines2
else 0
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment