#581 add pure contract and fix possible NPEs in getParentMapping usages

This commit is contained in:
Valentin Fondaratov
2018-07-23 16:06:47 +03:00
parent 92e09b2b89
commit 098a2ec4b2
4 changed files with 22 additions and 13 deletions
@@ -59,6 +59,7 @@ public abstract class YamlKeyCompletionInsertHandler<T extends LookupElement> im
final YAMLKeyValue keyValue = PsiTreeUtil.getParentOfType(elementAtCaret, YAMLKeyValue.class);
assert keyValue != null;
assert keyValue.getParentMapping() != null;
context.commitDocument();
if (keyValue.getValue() != null) {
@@ -41,6 +41,8 @@ public class YAMLDuplicatedKeysInspection extends LocalInspectionTool {
if (entry.getValue().size() > 1) {
entry.getValue().forEach((duplicatedKey) -> {
assert duplicatedKey.getKey() != null;
assert duplicatedKey.getParentMapping() != null : "This key is get from mapping";
holder.registerProblem(duplicatedKey.getKey(),
YAMLBundle.message("YAMLDuplicatedKeysInspection.duplicated.key", entry.getKey()),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new RemoveDuplicatedKeyQuickFix(duplicatedKey));
@@ -68,7 +70,7 @@ public class YAMLDuplicatedKeysInspection extends LocalInspectionTool {
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
YAMLKeyValue keyVal = myKeyValueHolder.getElement();
if (keyVal == null) {
if (keyVal == null || keyVal.getParentMapping() == null) {
return;
}
@@ -7,17 +7,18 @@ import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiRecursiveElementVisitor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLBundle;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YamlRecursivePsiElementVisitor;
import java.util.ArrayList;
import java.util.Objects;
@ApiStatus.Experimental
public abstract class YamlNonEditableKeysInspectionBase extends YamlMetaTypeInspectionBase {
@@ -70,22 +71,26 @@ public abstract class YamlNonEditableKeysInspectionBase extends YamlMetaTypeInsp
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final ArrayList<YAMLKeyValue> keysToDelete = new ArrayList<>();
descriptor.getPsiElement().getContainingFile().accept(new PsiRecursiveElementVisitor() {
descriptor.getPsiElement().getContainingFile().accept(new YamlRecursivePsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof YAMLKeyValue) {
final YamlMetaTypeProvider.MetaTypeProxy meta = myMetaTypeProvider.getKeyValueMetaType((YAMLKeyValue)element);
if (meta != null && !meta.getField().isEditable()) {
keysToDelete.add((YAMLKeyValue)element);
return;
public void visitKeyValue(@NotNull YAMLKeyValue keyValue) {
final YamlMetaTypeProvider.MetaTypeProxy meta = myMetaTypeProvider.getKeyValueMetaType(keyValue);
if (meta != null && !meta.getField().isEditable()) {
if (keyValue.getParentMapping() != null) {
keysToDelete.add(keyValue);
}
else {
Logger.getInstance(YamlNonEditableKeysInspectionBase.class)
.warn("Wanted to remove KV, but it does not have a parent mapping");
}
return;
}
super.visitElement(element);
super.visitKeyValue(keyValue);
}
});
for (YAMLKeyValue keyValue : keysToDelete) {
keyValue.getParentMapping().deleteKeyValue(keyValue);
Objects.requireNonNull(keyValue.getParentMapping()).deleteKeyValue(keyValue);
}
}
}
@@ -26,7 +26,8 @@ public interface YAMLKeyValue extends YAMLPsiElement, PsiNamedElement, PomTarget
@Contract(pure = true)
@NotNull
String getValueText();
@Contract(pure = true)
@Nullable
YAMLMapping getParentMapping();