Skip to content

Commit 29cc036

Browse files
committed
add folding builder
1 parent 10bf67e commit 29cc036

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.xepozz.crontab.ide
2+
3+
object CronScheduleDescriber {
4+
fun asHumanReadable(cron: String): String {
5+
val fields = cron.trim().split("\\s+".toRegex())
6+
if (fields.size != 5) {
7+
throw IllegalArgumentException("Invalid cron expression. Expected 5 fields, got ${fields.size}")
8+
}
9+
10+
val (minute, hour, dayOfMonth, month, dayOfWeek) = fields
11+
12+
return buildString {
13+
append("Runs ")
14+
15+
// Minute
16+
append(
17+
when (minute) {
18+
"*" -> "every minute"
19+
else -> "at minute $minute"
20+
}
21+
)
22+
23+
// Hour
24+
append(
25+
when (hour) {
26+
"*" -> ""
27+
else -> " past hour $hour"
28+
}
29+
)
30+
31+
append(" ")
32+
33+
// Day of month
34+
append(
35+
when (dayOfMonth) {
36+
"*" -> "every day"
37+
else -> "on day $dayOfMonth of the month"
38+
}
39+
)
40+
41+
append(" ")
42+
43+
// Month
44+
append(
45+
when (month) {
46+
"*" -> "in every month"
47+
else -> "in $month"
48+
}
49+
)
50+
51+
append(" ")
52+
53+
// Day of week
54+
append(
55+
when (dayOfWeek) {
56+
"*" -> ""
57+
"0", "7" -> "on Sundays"
58+
"1" -> "on Mondays"
59+
"2" -> "on Tuesdays"
60+
"3" -> "on Wednesdays"
61+
"4" -> "on Thursdays"
62+
"5" -> "on Fridays"
63+
"6" -> "on Saturdays"
64+
else -> "on specific days ($dayOfWeek)"
65+
}
66+
)
67+
}
68+
}
69+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.github.xepozz.crontab.ide
2+
3+
import com.github.xepozz.crontab.language.CrontabFile
4+
import com.github.xepozz.crontab.language.psi.CrontabSchedule
5+
import com.intellij.lang.ASTNode
6+
import com.intellij.lang.folding.CustomFoldingBuilder
7+
import com.intellij.lang.folding.FoldingDescriptor
8+
import com.intellij.openapi.editor.Document
9+
import com.intellij.openapi.editor.FoldingGroup
10+
import com.intellij.openapi.project.DumbService
11+
import com.intellij.openapi.util.TextRange
12+
import com.intellij.psi.PsiElement
13+
import com.intellij.psi.util.PsiTreeUtil
14+
15+
class CronScheduleFoldingBuilder : CustomFoldingBuilder() {
16+
override fun buildLanguageFoldRegions(
17+
resultSet: MutableList<FoldingDescriptor>,
18+
element: PsiElement,
19+
document: Document,
20+
p3: Boolean
21+
) {
22+
val element = element as? CrontabFile ?: return
23+
if (DumbService.isDumb(element.project)) return
24+
25+
PsiTreeUtil.findChildrenOfType(element, CrontabSchedule::class.java)
26+
.map {
27+
FoldingDescriptor(
28+
it.node,
29+
it.textRange,
30+
FoldingGroup.newGroup("Crontab Schedule Group"),
31+
)
32+
}
33+
.forEach { resultSet.add(it) }
34+
35+
}
36+
37+
override fun getLanguagePlaceholderText(node: ASTNode, textRange: TextRange): String? {
38+
val element = node.psi as? CrontabSchedule ?: return null
39+
40+
try {
41+
return CronScheduleDescriber.asHumanReadable(element.text)
42+
} catch (e: IllegalArgumentException) {
43+
}
44+
return null
45+
}
46+
47+
override fun isRegionCollapsedByDefault(node: ASTNode): Boolean = true
48+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
implementationClass="com.github.xepozz.crontab.language.CrontabAnnotator"/>
2828
<multiHostInjector
2929
implementation="com.github.xepozz.crontab.language.CrontabLanguageInjector"/>
30+
<lang.foldingBuilder
31+
language="Crontab"
32+
implementationClass="com.github.xepozz.crontab.ide.CronScheduleFoldingBuilder"/>
3033
</extensions>
3134
</idea-plugin>

0 commit comments

Comments
 (0)