-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
General.kt
96 lines (81 loc) · 2.77 KB
/
General.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package nl.hannahsten.texifyidea.util
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiFile
import java.util.regex.Pattern
/**
* Returns `1` when `true`, returns `0` when `false`.
*/
val Boolean.int: Int
get() = if (this) 1 else 0
// Copied from grazie utils
fun Boolean?.orTrue() = this ?: true
fun Boolean?.orFalse() = this ?: false
/**
* Creates a pair of two objects, analogous to [to].
*/
infix fun <T1, T2> T1.and(other: T2) = Pair(this, other)
/**
* Executes the given run write action.
*/
fun runWriteAction(writeAction: () -> Unit) {
ApplicationManager.getApplication().runWriteAction(writeAction)
}
fun runWriteCommandAction(project: Project, writeCommandAction: () -> Unit) {
WriteCommandAction.runWriteCommandAction(project, writeCommandAction)
}
fun <T> runWriteCommandAction(
project: Project,
commandName: String,
vararg files: PsiFile,
writeCommandAction: () -> T
): T {
return WriteCommandAction.writeCommandAction(project, *files).withName(commandName)
.compute<T, RuntimeException>(writeCommandAction)
}
/**
* Converts an [IntRange] to [TextRange].
*/
fun IntRange.toTextRange() = TextRange(this.first, this.last + 1)
/**
* Get the length of an [IntRange].
*/
val IntRange.length: Int
get() = endInclusive - start
/**
* Converts the range to a range representation with the given seperator.
* When the range has size 0, it will only print the single number.
*/
fun IntRange.toRangeString(separator: String = "-") =
if (start == endInclusive) start else "$start$separator$endInclusive"
/**
* Shift the range to the right by the number of places given.
*/
fun IntRange.shiftRight(displacement: Int): IntRange {
return (this.first + displacement)..(this.last + displacement)
}
/**
* Converts a [TextRange] to [IntRange].
*/
fun TextRange.toIntRange() = startOffset until endOffset
/**
* Easy access to [java.util.regex.Matcher.matches].
*/
fun Pattern.matches(sequence: CharSequence?) = if (sequence != null) matcher(sequence).matches() else false
fun runInBackground(project: Project?, description: String, function: (indicator: ProgressIndicator) -> Unit) {
ProgressManager.getInstance().run(object : Backgroundable(project, description) {
override fun run(indicator: ProgressIndicator) {
function(indicator)
}
})
}
fun runInBackgroundWithoutProgress(function: () -> Unit) {
ApplicationManager.getApplication().invokeLater {
function()
}
}