Skip to content

Commit d1f4d98

Browse files
committed
implement crontab guru intention
1 parent ae1406b commit d1f4d98

File tree

9 files changed

+120
-47
lines changed

9 files changed

+120
-47
lines changed

src/main/kotlin/com/github/xepozz/crontab/CrontabIcons.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ package com.github.xepozz.crontab
33
import com.intellij.openapi.util.IconLoader
44

55
object CrontabIcons {
6-
val FILE = IconLoader.getIcon("/icons/cron/pluginIcon.svg", CrontabIcons::class.java);
6+
@JvmStatic
7+
val FILE = IconLoader.getIcon("/icons/cron/pluginIcon.svg", CrontabIcons::class.java)
8+
9+
@JvmStatic
10+
val GURU = IconLoader.getIcon("/icons/guru/icon.svg", CrontabIcons::class.java)
711
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.xepozz.crontab.ide
2+
3+
import com.github.xepozz.crontab.language.psi.CrontabSchedule
4+
import com.intellij.ide.BrowserUtil
5+
6+
object CrontabGuruUtils {
7+
fun openInBrowser(crontabSchedule: CrontabSchedule) {
8+
BrowserUtil.browse(
9+
"https://crontab.guru/#" + crontabSchedule.text.split(Regex("[\\s\\t]+")).joinToString("_")
10+
)
11+
}
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.xepozz.crontab.ide.actions
2+
3+
import com.github.xepozz.crontab.CrontabIcons
4+
import com.github.xepozz.crontab.ide.CrontabGuruUtils
5+
import com.github.xepozz.crontab.language.psi.CrontabPsiTreeUtils
6+
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction
7+
import com.intellij.openapi.editor.Editor
8+
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.util.Iconable
10+
import com.intellij.psi.PsiElement
11+
12+
class CrontabGuruIntention : PsiElementBaseIntentionAction(), Iconable {
13+
override fun invoke(
14+
project: Project,
15+
editor: Editor,
16+
element: PsiElement
17+
) {
18+
val crontabSchedule = CrontabPsiTreeUtils.findCrontabSchedule(element)
19+
20+
if (crontabSchedule != null) {
21+
CrontabGuruUtils.openInBrowser(crontabSchedule)
22+
}
23+
}
24+
25+
override fun isAvailable(project: Project, editor: Editor, element: PsiElement) = true
26+
27+
override fun getFamilyName() = "Crontab intentions"
28+
29+
override fun getText() = "Open in crontab.guru"
30+
31+
override fun getIcon(flags: Int) = CrontabIcons.GURU
32+
}

src/main/kotlin/com/github/xepozz/crontab/ide/actions/CrontabScheduleAction.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.github.xepozz.crontab.ide.actions
2+
3+
import com.github.xepozz.crontab.ide.CrontabGuruUtils
4+
import com.github.xepozz.crontab.language.CrontabFile
5+
import com.github.xepozz.crontab.language.psi.CrontabPsiTreeUtils
6+
import com.intellij.openapi.actionSystem.ActionUpdateThread
7+
import com.intellij.openapi.actionSystem.AnAction
8+
import com.intellij.openapi.actionSystem.AnActionEvent
9+
import com.intellij.openapi.actionSystem.CommonDataKeys
10+
11+
class OpenCrontabGuruAction : AnAction() {
12+
override fun actionPerformed(event: AnActionEvent) {
13+
val editor = event.getData(CommonDataKeys.EDITOR) ?: return
14+
val psiFile = event.getData(CommonDataKeys.PSI_FILE) as? CrontabFile ?: return
15+
val psiElement = psiFile.findElementAt(editor.caretModel.offset) ?: return
16+
17+
val crontabSchedule = CrontabPsiTreeUtils.findCrontabSchedule(psiElement)
18+
19+
if (crontabSchedule != null) {
20+
CrontabGuruUtils.openInBrowser(crontabSchedule)
21+
}
22+
}
23+
24+
override fun update(event: AnActionEvent) {
25+
println("call update $event")
26+
val editor = event.getData(CommonDataKeys.EDITOR) ?: return
27+
val psiFile = event.getData(CommonDataKeys.PSI_FILE) as? CrontabFile ?: return
28+
val psiElement = psiFile.findElementAt(editor.caretModel.offset) ?: return
29+
30+
val crontabSchedule = CrontabPsiTreeUtils.findCrontabSchedule(psiElement)
31+
32+
event.presentation.isEnabledAndVisible = crontabSchedule != null
33+
}
34+
35+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.xepozz.crontab.language.psi
2+
3+
import com.intellij.psi.PsiElement
4+
import com.intellij.psi.util.PsiTreeUtil
5+
6+
object CrontabPsiTreeUtils {
7+
fun findCrontabSchedule(psiElement: PsiElement): CrontabSchedule? =
8+
PsiTreeUtil.collectParents(psiElement, CrontabSchedule::class.java, true)
9+
{ it is CrontabCronExpression }
10+
.firstOrNull()
11+
12+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<lang.commenter
3737
language="Crontab"
3838
implementationClass="com.github.xepozz.crontab.ide.CrontabCommenter"/>
39+
40+
<intentionAction>
41+
<language>Crontab</language>
42+
<className>com.github.xepozz.crontab.ide.actions.CrontabGuruIntention</className>
43+
<category>Crontab intentions</category>
44+
</intentionAction>
3945
</extensions>
4046
<actions>
4147
<action id="Crontab.OpenCrontabGuruAction" popup="false"
@@ -44,6 +50,8 @@
4450
description="Opens the schedule in the crontab.guru service"
4551
icon="/icons/cron/pluginIcon.svg">
4652
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
53+
<add-to-group group-id="CodeMenu" anchor="last"/>
54+
<add-to-group group-id="Floating.CodeToolbar.Extract" anchor="last"/>
4755
</action>
4856
</actions>
4957
</idea-plugin>
Lines changed: 10 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Open the inspected cron schedule expression on the https://crontab.guru service.
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)