1 // 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.
2 package git4idea.console
4 import com.intellij.execution.ConsoleFolding
5 import com.intellij.execution.ui.ConsoleView
6 import com.intellij.openapi.project.Project
7 import com.intellij.openapi.util.TextRange
8 import com.intellij.openapi.util.text.StringUtil
9 import com.intellij.vcs.console.VcsConsoleFolding
10 import com.intellij.vcs.console.VcsConsoleFolding.Placeholder
11 import com.intellij.vcs.console.VcsConsoleView
12 import git4idea.commands.GitImplBase
13 import java.util.regex.Matcher
14 import java.util.regex.Pattern
16 class GitConsoleFolding : VcsConsoleFolding {
17 override fun getFoldingsForLine(project: Project, line: String): List<Placeholder> {
18 if (!isGitCommandLine(line)) return emptyList()
20 val result = mutableListOf<Placeholder>()
21 val matcher: Matcher = CONFIG_OPTIONS_REGEX.matcher(line)
22 while (matcher.find()) {
23 var start = matcher.start()
24 val end = matcher.end()
25 if (start < end && StringUtil.isWhiteSpace(line[start])) start++
28 result.add(Placeholder("-c ...", TextRange(start, end)))
35 private fun isGitCommandLine(line: String): Boolean {
36 return GIT_LINE_REGEX.matcher(line).find()
40 private val CONFIG_OPTIONS_REGEX: Pattern = Pattern.compile("(\\s-c\\s[\\w.]+=[^ ]*)+")
41 private val GIT_LINE_REGEX: Pattern = Pattern.compile("\\[.*] git ")
45 class GitProgressOutputConsoleFolding : ConsoleFolding() {
46 override fun shouldBeAttachedToThePreviousLine(): Boolean = false
48 override fun getPlaceholderText(project: Project, lines: MutableList<String>): String? {
49 return lines.lastOrNull()
52 override fun shouldFoldLine(project: Project, line: String): Boolean {
53 return GitImplBase.looksLikeProgress(line)
56 override fun isEnabledForConsole(consoleView: ConsoleView): Boolean {
57 return consoleView is VcsConsoleView