Skip to content

Commit

Permalink
#25: Add option to disable Indent Rainbow on big files
Browse files Browse the repository at this point in the history
  • Loading branch information
dima74 committed Nov 11, 2021
1 parent 1166fbb commit 31c1bd3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/kotlin/indent/rainbow/annotators/IrAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiLanguageInjectionHost
import com.intellij.util.PatternUtil
import indent.rainbow.settings.IrConfig
import indent.rainbow.settings.document

enum class IrAnnotatorType {
SIMPLE,
Expand All @@ -17,6 +18,12 @@ fun IrConfig.isAnnotatorEnabled(file: PsiFile): Boolean =
enabled
&& matchesFileMask(fileMasks, file.name)
&& (file.isWritable || isEnabledForReadOnlyFiles)
&& !(disableOnBigFiles && file.hasMoreLinesThan(bigFilesLineThreshold))

private fun PsiFile.hasMoreLinesThan(count: Int): Boolean {
val document = document ?: return false
return document.lineCount > count
}

private fun matchesFileMask(fileMasks: String, fileName: String): Boolean {
val masks = fileMasks.trim()
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/indent/rainbow/settings/IrConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ data class IrConfig(

var useSimpleHighlighter: Boolean = false,
var simpleHighlighterFileMasks: String = "*",

var disableOnBigFiles: Boolean = true,
var bigFilesLineThreshold: Int = 1000,
) : PersistentStateComponent<IrConfig> {

override fun getState(): IrConfig = this
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/indent/rainbow/settings/IrConfigurable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class IrConfigurable : BoundConfigurable("Indent Rainbow") {
val radioButton = this@row.radioButton("Custom with", IrColorsPaletteType.CUSTOM)
intTextField(
prop = config::customPaletteNumberColors,
columns = 1,
columns = 3,
range = 1..99
).enableIf(radioButton.selected)
label("colors")
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/indent/rainbow/settings/IrConfigurableAdvanced.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import kotlin.reflect.KMutableProperty0
class IrConfigurableAdvanced : BoundConfigurable("Advanced Settings") {

override fun createPanel(): DialogPanel = panel {
row {
cell {
createDisableOnBigFilesCheckBox()
}
}
row {
checkBox("Enable in read only files", config::isEnabledForReadOnlyFiles)
}
Expand All @@ -22,6 +27,16 @@ class IrConfigurableAdvanced : BoundConfigurable("Advanced Settings") {
}
}

private fun InnerCell.createDisableOnBigFilesCheckBox() {
val checkbox = checkBox("Disable on files with more than", config::disableOnBigFiles)
intTextField(
prop = config::bigFilesLineThreshold,
columns = 5,
range = 1..Int.MAX_VALUE
).enableIf(checkbox.selected)
label("lines")
}

private fun Row.createSimpleHighlighterInFiles() {
val checkbox = checkBox("Use simple highlighter in these files:", config::useSimpleHighlighter)
createFilesMask(config::simpleHighlighterFileMasks)
Expand Down

0 comments on commit 31c1bd3

Please sign in to comment.