-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
3dd95fa
commit 46778a7
Showing
12 changed files
with
623 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../java/org/springframework/ide/vscode/boot/java/cron/CronExpressionCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.springframework.ide.vscode.boot.java.cron; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider; | ||
import org.springframework.ide.vscode.commons.java.IJavaProject; | ||
|
||
public class CronExpressionCompletionProvider implements AnnotationAttributeCompletionProvider { | ||
|
||
private static final Map<String, String> CRON_EXPRESSIONS_MAP = new LinkedHashMap<>(); | ||
|
||
static { | ||
CRON_EXPRESSIONS_MAP.put("0 0 * * * 1-5", "every hour every day between Monday and Friday"); | ||
CRON_EXPRESSIONS_MAP.put("0 */5 * * * *", "every 5 minutes"); | ||
CRON_EXPRESSIONS_MAP.put("0 * * * * *", "every minute"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 */6 * * *", "every 6 hours at minute 0"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 * * * *", "every hour"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 * * * SUN", "every hour at Sunday day"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 * * *", "at 00:00"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 * * SAT,SUN", "at 00:00 on Saturday and Sunday"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 * * 6,0", "at 00:00 at Saturday and Sunday days"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 1-7 * SUN", "at 00:00 every day between 1 and 7 at Sunday day"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 1 * *", "at 00:00 at 1 day"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 1 1 *", "at 00:00 at 1 day at January month"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 8-18 * * *", "every hour between 8 and 18"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 9 * * MON", "at 09:00 at Monday day"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 10 * * *", "at 10:00"); | ||
CRON_EXPRESSIONS_MAP.put("0 30 9 * JAN MON", "at 09:30 at January month at Monday day"); | ||
CRON_EXPRESSIONS_MAP.put("10 * * * * *", "every minute at second 10"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 8-10 * * *", "every hour between 8 and 10"); | ||
CRON_EXPRESSIONS_MAP.put("0 0/30 8-10 * * *", "every 30 minutes every hour between 8 and 10"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 L * *", " at 00:00 last day of month"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 1W * *", "at 00:00 the nearest weekday to the 1 of the month"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 * * THUL", "at 00:00 last Thursday of every month"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 ? * 5#2", "at 00:00 Friday 2 of every month"); | ||
CRON_EXPRESSIONS_MAP.put("0 0 0 ? * MON#1", "at 00:00 Monday 1 of every month"); | ||
} | ||
|
||
|
||
@Override | ||
public Map<String, String> getCompletionCandidatesWithLabels(IJavaProject project) { | ||
return CRON_EXPRESSIONS_MAP; | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
...java/org/springframework/ide/vscode/boot/java/cron/CronExpressionsInlayHintsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package org.springframework.ide.vscode.boot.java.cron; | ||
|
||
import java.util.Locale; | ||
|
||
import org.eclipse.jdt.core.dom.ASTVisitor; | ||
import org.eclipse.jdt.core.dom.Annotation; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.Expression; | ||
import org.eclipse.jdt.core.dom.MemberValuePair; | ||
import org.eclipse.jdt.core.dom.NormalAnnotation; | ||
import org.eclipse.jdt.core.dom.SingleMemberAnnotation; | ||
import org.eclipse.jdt.core.dom.StringLiteral; | ||
import org.eclipse.jdt.core.dom.TextBlock; | ||
import org.eclipse.lsp4j.InlayHint; | ||
import org.eclipse.lsp4j.InlayHintKind; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ide.vscode.boot.java.Annotations; | ||
import org.springframework.ide.vscode.boot.java.JdtInlayHintsProvider; | ||
import org.springframework.ide.vscode.commons.java.IJavaProject; | ||
import org.springframework.ide.vscode.commons.util.Collector; | ||
import org.springframework.ide.vscode.commons.util.text.TextDocument; | ||
import org.springframework.scheduling.support.CronExpression; | ||
|
||
import com.cronutils.descriptor.CronDescriptor; | ||
import com.cronutils.model.definition.CronDefinition; | ||
import com.cronutils.model.definition.CronDefinitionBuilder; | ||
import com.cronutils.parser.CronParser; | ||
|
||
import static com.cronutils.model.CronType.SPRING; | ||
|
||
public class CronExpressionsInlayHintsProvider implements JdtInlayHintsProvider { | ||
|
||
protected static Logger logger = LoggerFactory.getLogger(CronExpressionsInlayHintsProvider.class); | ||
|
||
private static final String SCHEDULED = "Scheduled"; | ||
|
||
public record EmbeddedCronExpression(Expression expression, String text, int offset) { | ||
}; | ||
|
||
@Override | ||
public boolean isApplicable(IJavaProject project) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public ASTVisitor getInlayHintsComputer(IJavaProject project, TextDocument doc, CompilationUnit cu, | ||
Collector<InlayHint> collector) { | ||
return new ASTVisitor() { | ||
|
||
@Override | ||
public boolean visit(NormalAnnotation node) { | ||
EmbeddedCronExpression cron = extractCronExpression(node); | ||
if (cron != null) { | ||
processCron(project, doc, collector, cron, node); | ||
} | ||
return super.visit(node); | ||
} | ||
|
||
@Override | ||
public boolean visit(SingleMemberAnnotation node) { | ||
EmbeddedCronExpression cron = extractCronExpression(node); | ||
if (cron != null) { | ||
processCron(project, doc, collector, cron, node); | ||
} | ||
return super.visit(node); | ||
} | ||
|
||
}; | ||
} | ||
|
||
private void processCron(IJavaProject project, TextDocument doc, Collector<InlayHint> collector, | ||
EmbeddedCronExpression cronExp, Annotation node) { | ||
boolean isValidExpression = CronExpression.isValidExpression(cronExp.text()); | ||
|
||
try { | ||
if (isValidExpression) { | ||
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(SPRING); | ||
CronParser parser = new CronParser(cronDefinition); | ||
CronDescriptor descriptor = CronDescriptor.instance(Locale.US); | ||
String cronDescription = descriptor.describe(parser.parse(cronExp.text().toUpperCase())); | ||
|
||
InlayHint hint = new InlayHint(); | ||
hint.setKind(InlayHintKind.Type); | ||
hint.setLabel(Either.forLeft(cronDescription)); | ||
hint.setTooltip(cronDescription); | ||
hint.setPaddingLeft(true); | ||
hint.setPaddingRight(true); | ||
hint.setPosition(doc.toPosition(node.getStartPosition() + node.getLength())); | ||
collector.accept(hint); | ||
} | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
|
||
public static EmbeddedCronExpression extractCronExpression(SingleMemberAnnotation a) { | ||
if (isScheduledAnnotation(a)) { | ||
EmbeddedCronExpression expression = extractEmbeddedExpression(a.getValue(), a); | ||
return expression == null ? null | ||
: new EmbeddedCronExpression(expression.expression(), expression.text(), expression.offset()); | ||
} | ||
return null; | ||
} | ||
|
||
public static EmbeddedCronExpression extractCronExpression(NormalAnnotation a) { | ||
Expression cronExpression = null; | ||
if (isScheduledAnnotation(a)) { | ||
for (Object value : a.values()) { | ||
if (value instanceof MemberValuePair) { | ||
MemberValuePair pair = (MemberValuePair) value; | ||
String name = pair.getName().getFullyQualifiedName(); | ||
if ("cron".equals(name)) { | ||
cronExpression = pair.getValue(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (cronExpression != null) { | ||
EmbeddedCronExpression e = extractEmbeddedExpression(cronExpression, a); | ||
if (e != null) { | ||
return new EmbeddedCronExpression(e.expression(), e.text(), e.offset()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static EmbeddedCronExpression extractEmbeddedExpression(Expression valueExp, Annotation node) { | ||
String text = null; | ||
int offset = 0; | ||
if (valueExp instanceof StringLiteral sl) { | ||
text = sl.getEscapedValue(); | ||
text = text.substring(1, text.length() - 1); | ||
offset = sl.getStartPosition() + 1; // +1 to skip over opening " | ||
} else if (valueExp instanceof TextBlock tb) { | ||
text = tb.getEscapedValue(); | ||
text = text.substring(3, text.length() - 3).trim(); | ||
offset = tb.getStartPosition() + 3; // +3 to skip over opening """ | ||
} | ||
return text == null ? null : new EmbeddedCronExpression(valueExp, text, offset); | ||
} | ||
|
||
static boolean isScheduledAnnotation(Annotation a) { | ||
return Annotations.SCHEDULED.equals(a.getTypeName().getFullyQualifiedName()) | ||
|| SCHEDULED.equals(a.getTypeName().getFullyQualifiedName()); | ||
} | ||
} |
Oops, something went wrong.