|
| 1 | +package dev.openfga.intellijplugin.language; |
| 2 | + |
| 3 | +import com.intellij.lang.annotation.AnnotationHolder; |
| 4 | +import com.intellij.lang.annotation.ExternalAnnotator; |
| 5 | +import com.intellij.lang.annotation.HighlightSeverity; |
| 6 | +import com.intellij.openapi.util.Pair; |
| 7 | +import com.intellij.openapi.util.TextRange; |
| 8 | +import com.intellij.psi.PsiElement; |
| 9 | +import com.intellij.psi.PsiFile; |
| 10 | +import dev.openfga.language.errors.DslErrorsException; |
| 11 | +import dev.openfga.language.errors.ParsingError; |
| 12 | +import dev.openfga.language.errors.StartEnd; |
| 13 | +import dev.openfga.language.validation.DslValidator; |
| 14 | +import org.apache.commons.lang3.ObjectUtils; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | +import org.jetbrains.yaml.YAMLUtil; |
| 18 | +import org.jetbrains.yaml.psi.YAMLFile; |
| 19 | +import org.jetbrains.yaml.psi.impl.YAMLScalarListImpl; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class OpenFGAStoreAnnotator extends ExternalAnnotator<String, List<? extends ParsingError>> { |
| 25 | + |
| 26 | + @Override |
| 27 | + public @Nullable String collectInformation(@NotNull final PsiFile file) { |
| 28 | + final Pair<PsiElement, String> modelField = getModelField(file); |
| 29 | + |
| 30 | + // If empty or not a scalar list, return |
| 31 | + if (!ObjectUtils.isNotEmpty(modelField) || !(modelField.getFirst() instanceof YAMLScalarListImpl)) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + // When the field is in scalar strip model `getSecond()` returns a trimmed version of the model |
| 36 | + // This skews all the original line numbers, so we instead get the unmodified value |
| 37 | + if (ObjectUtils.isNotEmpty(modelField) && ObjectUtils.isNotEmpty(modelField.getFirst())) { |
| 38 | + // Remove first line which is the scalar notation ('|', '|-', '|+') |
| 39 | + return modelField.getFirst().getText().split("\n", 2)[1]; |
| 40 | + } |
| 41 | + |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public @Nullable List<? extends ParsingError> doAnnotate(@NotNull final String collectedInfo) { |
| 47 | + if (collectedInfo.isEmpty()) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + DslValidator.validate(collectedInfo); |
| 53 | + } catch (IOException e) { |
| 54 | + throw new RuntimeException("Failure when attempting to validate model", e); |
| 55 | + } catch (DslErrorsException e) { |
| 56 | + return e.getErrors(); |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + // Parsing is difficult: both the trimmed string (model) and the string retaining whitespace (originalString) |
| 63 | + // The model is the clean string, whereas the originalString is that from the YAML with extra whitespace |
| 64 | + // First the clean string is validated, then the original string is used to determine correct offsets |
| 65 | + @Override |
| 66 | + public void apply(@NotNull final PsiFile file, |
| 67 | + final List<? extends ParsingError> annotationResult, |
| 68 | + @NotNull final AnnotationHolder holder) { |
| 69 | + final @Nullable Pair<PsiElement, String> fileContents = getModelField(file); |
| 70 | + |
| 71 | + if (ObjectUtils.isEmpty(fileContents)) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + final PsiElement key = fileContents.getFirst(); |
| 76 | + |
| 77 | + final String originalString = key.getText().split("\n", 2)[1]; |
| 78 | + // Key offset and newline |
| 79 | + int offset = key.getFirstChild().getTextRange().getEndOffset() + 1; |
| 80 | + |
| 81 | + for (ParsingError error : annotationResult) { |
| 82 | + final StartEnd startEndLine = error.getLine(); |
| 83 | + final StartEnd startEndColumn = error.getColumn(); |
| 84 | + |
| 85 | + int offsetStart = getOffsetFromRange( |
| 86 | + originalString, startEndLine.getStart(), startEndColumn.getStart(), offset); |
| 87 | + int offsetEnd = getOffsetFromRange( |
| 88 | + originalString, startEndLine.getEnd(), startEndColumn.getEnd(), offset); |
| 89 | + |
| 90 | + holder.newAnnotation(HighlightSeverity.ERROR, error.getMessage()) |
| 91 | + .range(new TextRange(offsetStart, offsetEnd)) |
| 92 | + .create(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static int getOffsetFromRange( |
| 97 | + @NotNull final String doc, int line, int character, int offset) { |
| 98 | + final String[] lines = doc.split("\n"); |
| 99 | + |
| 100 | + // Count offset |
| 101 | + for (int i = 0; i < line; i++) { |
| 102 | + // Strip leading spaces to normalize indentation, tabs are banned in YAML. |
| 103 | + // This assumes an indent of 1 || 2 spaces, doesn't support 4 as that would bite into indentation |
| 104 | + final String replacementLine = lines[i].replaceFirst("^ {1,2}", ""); |
| 105 | + offset += replacementLine.length() + (lines[i].length() - replacementLine.length()) + 1; |
| 106 | + } |
| 107 | + |
| 108 | + return offset + character; |
| 109 | + } |
| 110 | + |
| 111 | + private static @Nullable Pair<PsiElement, String> getModelField(@NotNull final PsiFile file) { |
| 112 | + final Pair<PsiElement, String> field = YAMLUtil.getValue((YAMLFile) file, "model"); |
| 113 | + |
| 114 | + if (ObjectUtils.isNotEmpty(field)) { |
| 115 | + return field; |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | +} |
0 commit comments