From 9086ec7a5d400f7d7a5afaebbcc13e0b6e24d40a Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 22 Oct 2016 12:56:31 +0200 Subject: [PATCH] IG: remove newline after modifier list if necessary (IDEA-162989) --- .../ig/junit/JUnitDatapointInspection.java | 15 ++++++ .../siyeh/ig/junit/MakePublicStaticFix.java | 48 +++++++++++++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitDatapointInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitDatapointInspection.java index fdca2818f665..7a600418e5c9 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitDatapointInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/JUnitDatapointInspection.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.siyeh.ig.junit; import com.intellij.codeInsight.AnnotationUtil; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/MakePublicStaticFix.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/MakePublicStaticFix.java index 0e54556e29ff..97bc74f7f62d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/MakePublicStaticFix.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/junit/MakePublicStaticFix.java @@ -1,10 +1,26 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.siyeh.ig.junit; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiMember; -import com.intellij.psi.PsiModifier; +import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; +import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade; import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.ig.InspectionGadgetsFix; @@ -15,6 +31,7 @@ import org.jetbrains.annotations.NotNull; * Date: 5/22/13 */ class MakePublicStaticFix extends InspectionGadgetsFix { + private final String myName; private final boolean myMakeStatic; @@ -24,14 +41,25 @@ class MakePublicStaticFix extends InspectionGadgetsFix { } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { + protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); - if (element != null) { - final PsiElement parent = element.getParent(); - if (parent instanceof PsiMember) { - PsiUtil.setModifierProperty((PsiMember)parent, PsiModifier.PUBLIC, true); - PsiUtil.setModifierProperty((PsiMember)parent, PsiModifier.STATIC, myMakeStatic); - } + if (element == null) { + return; + } + final PsiElement parent = element.getParent(); + if (!(parent instanceof PsiMember)) { + return; + } + final PsiMember member = (PsiMember)parent; + final PsiModifierList modifierList = member.getModifierList(); + if (modifierList == null) { + return; + } + modifierList.setModifierProperty(PsiModifier.PUBLIC, true); + modifierList.setModifierProperty(PsiModifier.STATIC, myMakeStatic); + final PsiElement sibling = modifierList.getNextSibling(); + if (sibling instanceof PsiWhiteSpace && sibling.getText().contains("\n")) { + sibling.replace(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText(" ")); } }