Skip to content

Commit ae1406b

Browse files
committed
add crontab guru action
1 parent e680760 commit ae1406b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.xepozz.crontab.ide.actions
2+
3+
import com.github.xepozz.crontab.language.CrontabFile
4+
import com.github.xepozz.crontab.language.psi.CrontabCronExpression
5+
import com.github.xepozz.crontab.language.psi.CrontabSchedule
6+
import com.intellij.ide.BrowserUtil
7+
import com.intellij.openapi.actionSystem.ActionUpdateThread
8+
import com.intellij.openapi.actionSystem.AnAction
9+
import com.intellij.openapi.actionSystem.AnActionEvent
10+
import com.intellij.openapi.actionSystem.CommonDataKeys
11+
import com.intellij.psi.PsiElement
12+
import com.intellij.psi.util.PsiTreeUtil
13+
14+
class OpenCrontabGuruAction : AnAction() {
15+
override fun actionPerformed(e: AnActionEvent) {
16+
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
17+
val psiFile = e.getData(CommonDataKeys.PSI_FILE) as? CrontabFile ?: return
18+
val psiElement = psiFile.findElementAt(editor.caretModel.offset) ?: return
19+
20+
val crontabSchedule = findCrontabSchedule(psiElement)
21+
22+
if (crontabSchedule != null) {
23+
BrowserUtil.browse(
24+
"https://crontab.guru/#" + crontabSchedule.text.split(Regex("[\\s\\t]+")).joinToString("_")
25+
)
26+
}
27+
}
28+
29+
override fun update(e: AnActionEvent) {
30+
val editor = e.getData(CommonDataKeys.EDITOR) ?: return
31+
val psiFile = e.getData(CommonDataKeys.PSI_FILE) as? CrontabFile ?: return
32+
val psiElement = psiFile.findElementAt(editor.caretModel.offset) ?: return
33+
34+
val crontabSchedule = findCrontabSchedule(psiElement)
35+
36+
e.presentation.isEnabledAndVisible = crontabSchedule != null
37+
}
38+
39+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
40+
41+
private fun findCrontabSchedule(psiElement: PsiElement): CrontabSchedule? =
42+
PsiTreeUtil.collectParents(psiElement, CrontabSchedule::class.java, true)
43+
{ it is CrontabCronExpression }
44+
.firstOrNull()
45+
46+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@
3737
language="Crontab"
3838
implementationClass="com.github.xepozz.crontab.ide.CrontabCommenter"/>
3939
</extensions>
40+
<actions>
41+
<action id="Crontab.OpenCrontabGuruAction" popup="false"
42+
class="com.github.xepozz.crontab.ide.actions.OpenCrontabGuruAction"
43+
text="Open in Crontab Guru"
44+
description="Opens the schedule in the crontab.guru service"
45+
icon="/icons/cron/pluginIcon.svg">
46+
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
47+
</action>
48+
</actions>
4049
</idea-plugin>

0 commit comments

Comments
 (0)