Skip to content

Commit

Permalink
Release 2.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jan 6, 2023
1 parent 00305e7 commit 4883cfd
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 62 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
id("org.jetbrains.intellij") version "1.11.0"
}

group = "dev.rollczi.litecommands.intellijplugin"
version = "1.0-SNAPSHOT"
group = "dev.rollczi"
version = "2.7.0-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "LiteCommandsIntelliJPlugin"
rootProject.name = "LiteCommands-IntelliJPlugin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.rollczi.litecommands.intellijplugin;

import com.intellij.codeInspection.LocalInspectionTool;

public final class InspectionToolUtil {

private static final String FILE_DESCRIPTION_EXCLUDE_END_NAME = "Inspection";
private static final String FILE_DESCRIPTION_EXTENSION = ".html";

private InspectionToolUtil() {
}

public static String getDescriptionFileName(Class<? extends LocalInspectionTool> inspectionToolClass) {
String className = inspectionToolClass.getSimpleName();

if (className.endsWith(FILE_DESCRIPTION_EXCLUDE_END_NAME)) {
className = className.substring(0, className.length() - FILE_DESCRIPTION_EXCLUDE_END_NAME.length());
}

return className + FILE_DESCRIPTION_EXTENSION;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dev.rollczi.litecommands.intellijplugin.legacy.annotation;

import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;

public final class AnnotationMapper {

private final String legacyName;
private final String modernName;
private final Set<Attribute> attributes;
private final List<Attribute> attributes;

private AnnotationMapper(String legacyName, String modernName, Set<Attribute> attributes) {
private AnnotationMapper(String legacyName, String modernName, List<Attribute> attributes) {
this.legacyName = legacyName;
this.modernName = modernName;
this.attributes = attributes;
Expand Down Expand Up @@ -43,7 +43,7 @@ private String toShortName(String name) {
return name.substring(name.lastIndexOf('.') + 1);
}

public Set<Attribute> getAttributes() {
public List<Attribute> getAttributes() {
return this.attributes;
}

Expand All @@ -55,7 +55,7 @@ public static class Builder {

private String legacyName;
private String modernName;
private final Set<Attribute> attributes = new HashSet<>();
private final List<Attribute> attributes = new ArrayList<>();

public Builder legacyName(String legacyName) {
this.legacyName = legacyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ protected LegacyAnnotationInspectionTool(AnnotationMapper mapper) {
return "LiteCommands";
}

@Override
public @NotNull String getShortName() {
return "Legacy" + this.mapper.legacyName() + "Annotation";
}

@Override
public boolean isEnabledByDefault() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public final class AnnotationValidator {

Expand Down Expand Up @@ -46,8 +47,8 @@ public Builder name(String name) {
return this;
}

public Builder attribute(String modern, AttributeType type, Predicate<String> validator) {
this.attributeValidators.add(new AttributeValidator(modern, type, validator));
public Builder attribute(String modern, AttributeType type, Predicate<String> validator, UnaryOperator<String> quickFix) {
this.attributeValidators.add(new AttributeValidator(modern, type, validator, quickFix));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import dev.rollczi.litecommands.intellijplugin.legacy.annotation.AttributeType;

import java.util.function.Predicate;
import java.util.function.UnaryOperator;

public class AttributeValidator {

private final String name;
private final AttributeType type;
private final Predicate<String> validator;
private final UnaryOperator<String> quickFix;

public AttributeValidator(String name, AttributeType type, Predicate<String> validator) {
public AttributeValidator(String name, AttributeType type, Predicate<String> validator, UnaryOperator<String> quickFix) {
this.name = name;
this.type = type;
this.validator = validator;
this.quickFix = quickFix;
}

public String name() {
Expand All @@ -28,4 +31,8 @@ public boolean validate(String value) {
return this.validator.test(value);
}

public String quickFix(String value) {
return this.quickFix.apply(value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.rollczi.litecommands.intellijplugin.validation.annoation;

import com.intellij.psi.PsiElement;

class AttributeValue {

private final String text;
private final String rawText;
private final PsiElement element;

AttributeValue(String text, String rawText, PsiElement element) {
this.text = text;
this.rawText = rawText;
this.element = element;
}

public String text() {
return this.text;
}

public String rawText() {
return this.rawText;
}

public PsiElement element() {
return this.element;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.rollczi.litecommands.intellijplugin.validation.annoation;

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.util.IntentionFamilyName;
import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

class ReplaceWithValidValueQuickFix implements LocalQuickFix {

private final PsiAnnotation patternAnnotation;
private final AttributeValidator validator;
private final AttributeValue value;

public ReplaceWithValidValueQuickFix(PsiAnnotation patternAnnotation, AttributeValidator validator, AttributeValue value) {
this.patternAnnotation = patternAnnotation;
this.validator = validator;
this.value = value;
}

@Override
public @IntentionFamilyName @NotNull String getFamilyName() {
String quickFix = this.validator.quickFix(this.value.rawText());

if (quickFix.startsWith("\"") && quickFix.endsWith("\"")) {
quickFix = quickFix.substring(1, quickFix.length() - 1);
}

return "Replace with '" + quickFix + "'";
}

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
String quickFix = this.validator.quickFix(this.value.rawText());
PsiElement old = this.value.element();
String annotationText = this.patternAnnotation.getText();

int start = old.getTextRange().getStartOffset() - this.patternAnnotation.getTextRange().getStartOffset();
int end = old.getTextRange().getEndOffset() - this.patternAnnotation.getTextRange().getStartOffset();

String newAnnotationText = annotationText.substring(0, start) + quickFix + annotationText.substring(end);

this.patternAnnotation.replace(JavaPsiFacade.getElementFactory(project).createAnnotationFromText(
newAnnotationText,
this.patternAnnotation
));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public abstract class ValidationAnnotationInspectionTool extends AbstractBaseJavaLocalInspectionTool {

private static final String DISPLAY_NAME = "Annotation @%s has invalid value";
Expand All @@ -28,11 +38,6 @@ protected ValidationAnnotationInspectionTool(AnnotationValidator annotationValid
return "LiteCommands";
}

@Override
public @NotNull String getShortName() {
return "Validation" + this.annotationValidator.name() + "Annotation";
}

@Override
public boolean isEnabledByDefault() {
return true;
Expand All @@ -54,14 +59,69 @@ public void visitAnnotation(PsiAnnotation annotation) {
continue;
}

String value = attribute.getText();
List<AttributeValue> values = ValidationAnnotationInspectionTool.this.getValues(attribute);

if (!validator.validate(value)) {
holder.registerProblem(attribute, String.format("%s attribute %s has invalid value '%s'", ValidationAnnotationInspectionTool.this.annotationValidator.annotation(), validator.name(), value));
for (AttributeValue value : values) {
if (!validator.validate(value.text())) {
holder.registerProblem(
value.element(),
String.format("Attribute '%s' has invalid value <code>%s</code>", validator.name(), value.text()),
new ReplaceWithValidValueQuickFix(annotation, validator, value)
);
}
}
}
}
};
}

private static final Set<IElementType> CHECKED_TOKENS = Set.of(
JavaTokenType.STRING_LITERAL,
JavaTokenType.CHARACTER_LITERAL,
JavaTokenType.INTEGER_LITERAL,
JavaTokenType.LONG_LITERAL,
JavaTokenType.FLOAT_LITERAL,
JavaTokenType.DOUBLE_LITERAL,
JavaTokenType.TRUE_KEYWORD,
JavaTokenType.FALSE_KEYWORD,
JavaTokenType.NULL_KEYWORD,
JavaTokenType.MINUS,
JavaTokenType.PLUS
);

private List<AttributeValue> getValues(PsiElement element) {
List<AttributeValue> values = new ArrayList<>();

for (PsiElement child : element.getChildren()) {
if (child instanceof PsiJavaToken) {
PsiJavaToken token = (PsiJavaToken) child;
IElementType tokenType = token.getTokenType();
String text = token.getText();

if (!CHECKED_TOKENS.contains(tokenType)) {
continue;
}

if (tokenType == JavaTokenType.STRING_LITERAL || tokenType == JavaTokenType.CHARACTER_LITERAL) {
values.add(new AttributeValue(text.substring(1, text.length() - 1), text, token));
continue;
}

if (tokenType == JavaTokenType.MINUS || tokenType == JavaTokenType.PLUS) {
return Collections.singletonList(new AttributeValue(element.getText(), element.getText(), element));
}
}

if (child instanceof PsiLiteralExpression) {
values.addAll(this.getValues(child));
}
}

if (values.isEmpty()) {
values.add(new AttributeValue(element.getText(), element.getText(), element));
}

return values;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,8 @@ final class CommandNameValidUtils {

private CommandNameValidUtils() {
}


static boolean validateArray(String value) {
if (value.startsWith("{") && value.endsWith("}")) {
value = value.substring(1, value.length() - 1);
}

value = value.trim();

if (value.isEmpty()) {
return true;
}

String[] values = value.split(",");

for (String val : values) {
if (!validateString(val)) {
return false;
}
}

return true;
}


static boolean validateString(String value) {
value = value.trim();

if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}

if (value.isEmpty()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

public class ExecuteValidationInspection extends ValidationAnnotationInspectionTool {

private static final String NUMBER_MORE_OR_EQ_MINUS_ONE_REGEX = "( *[0-9]+ *| *- *1 *)";

private static final AnnotationValidator ROUTE_VALIDATOR = AnnotationValidator.builder()
.name("dev.rollczi.litecommands.command.execute.Execute")
.attribute("route", AttributeType.STRING, CommandNameValidUtils::validateString)
.attribute("aliases", AttributeType.ARRAY, CommandNameValidUtils::validateArray)
.attribute("required", AttributeType.INT, number -> number.matches("[0-9]+|-1"))
.attribute("min", AttributeType.INT, number -> number.matches("[0-9]+|-1"))
.attribute("max", AttributeType.INT, number -> number.matches("[0-9]+|-1"))
.attribute("route", AttributeType.STRING, CommandNameValidUtils::validateString, new StringQuickFix())
.attribute("aliases", AttributeType.ARRAY, CommandNameValidUtils::validateString, new StringQuickFix())
.attribute("required", AttributeType.INT, number -> number.matches(NUMBER_MORE_OR_EQ_MINUS_ONE_REGEX), new MoreThanMinusOneQuickFix())
.attribute("min", AttributeType.INT, number -> number.matches(NUMBER_MORE_OR_EQ_MINUS_ONE_REGEX), new MoreThanMinusOneQuickFix())
.attribute("max", AttributeType.INT, number -> number.matches(NUMBER_MORE_OR_EQ_MINUS_ONE_REGEX), new MoreThanMinusOneQuickFix())
.build();

public ExecuteValidationInspection() {
Expand Down
Loading

0 comments on commit 4883cfd

Please sign in to comment.