From 9713316b499ff58fbee49d68b764991bf8308dc4 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 4 Apr 2017 12:08:12 +0300 Subject: [PATCH] IDEA-170355 LongLineInspection ignores package/import statements in java --- .../JavaLongLineInspectionPolicy.java | 30 ++++++++++ .../testData/inspection/longLine/Simple.java | 9 +++ .../JavaLongLineInspectionTest.kt | 33 +++++++++++ .../{ => longLine}/LongLineInspection.java | 57 +++++++++++++------ .../longLine/LongLineInspectionPolicy.java | 26 +++++++++ .../LongLineInspectionTest.java | 3 +- .../src/META-INF/LangExtensionPoints.xml | 2 + .../src/META-INF/LangExtensions.xml | 2 +- resources/src/META-INF/IdeaPlugin.xml | 2 + 9 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInspection/JavaLongLineInspectionPolicy.java create mode 100644 java/java-tests/testData/inspection/longLine/Simple.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInspection/JavaLongLineInspectionTest.kt rename platform/lang-impl/src/com/intellij/codeInspection/{ => longLine}/LongLineInspection.java (65%) create mode 100644 platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspectionPolicy.java diff --git a/java/java-impl/src/com/intellij/codeInspection/JavaLongLineInspectionPolicy.java b/java/java-impl/src/com/intellij/codeInspection/JavaLongLineInspectionPolicy.java new file mode 100644 index 000000000000..7d6bbc062cb6 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInspection/JavaLongLineInspectionPolicy.java @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2017 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.intellij.codeInspection; + +import com.intellij.codeInspection.longLine.LongLineInspectionPolicy; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiImportStatementBase; +import com.intellij.psi.PsiPackageStatement; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; + +public class JavaLongLineInspectionPolicy implements LongLineInspectionPolicy { + @Override + public boolean ignoreLongLineFor(@NotNull PsiElement element) { + return PsiTreeUtil.getNonStrictParentOfType(element, PsiImportStatementBase.class, PsiPackageStatement.class) != null; + } +} diff --git a/java/java-tests/testData/inspection/longLine/Simple.java b/java/java-tests/testData/inspection/longLine/Simple.java new file mode 100644 index 000000000000..887cd77dab60 --- /dev/null +++ b/java/java-tests/testData/inspection/longLine/Simple.java @@ -0,0 +1,9 @@ +package barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr; + +import foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo; + +class Test { + void m() { + String varrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr = "foo"; + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/JavaLongLineInspectionTest.kt b/java/java-tests/testSrc/com/intellij/codeInspection/JavaLongLineInspectionTest.kt new file mode 100644 index 000000000000..56018c6211bf --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInspection/JavaLongLineInspectionTest.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2017 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.intellij.codeInspection + +import com.intellij.JavaTestUtil +import com.intellij.codeInspection.longLine.LongLineInspection +import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase + +class JavaLongLineInspectionTest : JavaCodeInsightFixtureTestCase() { + override fun getBasePath() = JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/longLine" + + fun testSimple() { + doTest() + } + + private fun doTest() { + myFixture.enableInspections(LongLineInspection()) + myFixture.testHighlighting(true, false, false, getTestName(false) + ".java") + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInspection/LongLineInspection.java b/platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspection.java similarity index 65% rename from platform/lang-impl/src/com/intellij/codeInspection/LongLineInspection.java rename to platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspection.java index 4e4ebe4a40d0..0772196a2ddd 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/LongLineInspection.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -13,9 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.intellij.codeInspection; +package com.intellij.codeInspection.longLine; import com.intellij.application.options.CodeStyleSchemesConfigurable; +import com.intellij.codeInspection.InspectionManager; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.ide.DataManager; import com.intellij.injected.editor.VirtualFileWindow; import com.intellij.openapi.actionSystem.CommonDataKeys; @@ -27,8 +31,10 @@ import com.intellij.openapi.options.ex.Settings; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.*; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.ui.HyperlinkLabel; import com.intellij.util.Consumer; import com.intellij.util.SmartList; @@ -45,7 +51,6 @@ import java.util.List; * @author Dmitry Batkovich */ public class LongLineInspection extends LocalInspectionTool { - @Nullable @Override public JComponent createOptionsPanel() { @@ -53,18 +58,15 @@ public class LongLineInspection extends LocalInspectionTool { codeStyleHyperlink.addHyperlinkListener(new HyperlinkListener() { @Override public void hyperlinkUpdate(HyperlinkEvent e) { - DataManager.getInstance().getDataContextFromFocus().doWhenDone(new Consumer() { - @Override - public void consume(DataContext context) { - if (context != null) { - final Settings settings = Settings.KEY.getData(context); - if (settings != null) { - settings.select(settings.find(CodeStyleSchemesConfigurable.class)); - } - else { - ShowSettingsUtil.getInstance() - .showSettingsDialog(CommonDataKeys.PROJECT.getData(context), CodeStyleSchemesConfigurable.class); - } + DataManager.getInstance().getDataContextFromFocus().doWhenDone((Consumer)context -> { + if (context != null) { + final Settings settings = Settings.KEY.getData(context); + if (settings != null) { + settings.select(settings.find(CodeStyleSchemesConfigurable.class)); + } + else { + ShowSettingsUtil.getInstance() + .showSettingsDialog(CommonDataKeys.PROJECT.getData(context), CodeStyleSchemesConfigurable.class); } } }); @@ -93,7 +95,9 @@ public class LongLineInspection extends LocalInspectionTool { for (int idx = 0; idx < document.getLineCount(); idx++) { final int startOffset = document.getLineStartOffset(idx); final int endOffset = document.getLineEndOffset(idx); - if (endOffset - startOffset > codeStyleRightMargin) { + if (endOffset - startOffset > codeStyleRightMargin && !ignoreFor(findElementInRange(file, + startOffset + codeStyleRightMargin - 1, + endOffset - 1))) { final int maxOffset = startOffset + codeStyleRightMargin; descriptors.add( manager.createProblemDescriptor(file, new TextRange(maxOffset, endOffset), @@ -104,4 +108,23 @@ public class LongLineInspection extends LocalInspectionTool { } return descriptors.isEmpty() ? null : descriptors.toArray(new ProblemDescriptor[descriptors.size()]); } + + @Nullable + private static PsiElement findElementInRange(@NotNull PsiFile file, int leftOffset, int rightOffset) { + PsiElement leftElement = file.findElementAt(leftOffset); + if (leftElement == null) return null; + PsiElement rightElement = file.findElementAt(rightOffset); + if (rightElement == null) return null; + return PsiTreeUtil.findCommonParent(leftElement, rightElement); + } + + private static boolean ignoreFor(@Nullable PsiElement element) { + if (element == null) return false; + for (LongLineInspectionPolicy policy : LongLineInspectionPolicy.EP_NAME.getExtensions()) { + if (policy.ignoreLongLineFor(element)) { + return true; + } + } + return false; + } } diff --git a/platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspectionPolicy.java b/platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspectionPolicy.java new file mode 100644 index 000000000000..3c3fd262b47d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInspection/longLine/LongLineInspectionPolicy.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2017 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.intellij.codeInspection.longLine; + +import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; + +public interface LongLineInspectionPolicy { + ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.longLineInspectionPolicy"); + + boolean ignoreLongLineFor(@NotNull PsiElement element); +} diff --git a/platform/lang-impl/testSources/com/intellij/codeInspection/LongLineInspectionTest.java b/platform/lang-impl/testSources/com/intellij/codeInspection/LongLineInspectionTest.java index babd1fb44e43..3d597cf4da63 100644 --- a/platform/lang-impl/testSources/com/intellij/codeInspection/LongLineInspectionTest.java +++ b/platform/lang-impl/testSources/com/intellij/codeInspection/LongLineInspectionTest.java @@ -15,8 +15,7 @@ */ package com.intellij.codeInspection; -import com.intellij.codeInspection.ex.LocalInspectionToolWrapper; -import com.intellij.openapi.util.text.StringUtil; +import com.intellij.codeInspection.longLine.LongLineInspection; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index c16fc9edb85e..869ac784b8c4 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -902,6 +902,8 @@ + + diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index ba3cf23629dc..524a7765bf23 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -834,7 +834,7 @@ level="WARNING" implementationClass="com.intellij.codeInspection.TodoCommentInspection"/> + level="WARNING" implementationClass="com.intellij.codeInspection.longLine.LongLineInspection"/> diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 40e5f4e7805e..9c62fb1fcc57 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1918,6 +1918,8 @@ + +