IDEA-170355 LongLineInspection ignores package/import statements in java

This commit is contained in:
Dmitry Batkovich
2017-04-04 12:10:44 +03:00
parent 174129afe2
commit 9713316b49
9 changed files with 144 additions and 20 deletions
@@ -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;
}
}
@@ -0,0 +1,9 @@
package barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr;
import <error descr="Cannot resolve symbol 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'">foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo</error>;
class Test {
void m() {
String varrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr<warning descr="Line is longer than allowed by code style (> 120 columns)">rrrrrrrrrrrrrrrrrr = "foo";</warning>
}
}
@@ -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")
}
}
@@ -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<DataContext>() {
@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<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);
}
}
});
@@ -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;
}
}
@@ -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<LongLineInspectionPolicy> EP_NAME = ExtensionPointName.create("com.intellij.longLineInspectionPolicy");
boolean ignoreLongLineFor(@NotNull PsiElement element);
}
@@ -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;
@@ -902,6 +902,8 @@
<extensionPoint name="sdkEditorAdditionalOptionsProvider" interface="com.intellij.openapi.SdkEditorAdditionalOptionsProvider"/>
<extensionPoint name="metaLanguage" interface="com.intellij.lang.MetaLanguage"/>
<extensionPoint name="longLineInspectionPolicy" interface="com.intellij.codeInspection.longLine.LongLineInspectionPolicy"/>
</extensionPoints>
</idea-plugin>
@@ -834,7 +834,7 @@
level="WARNING" implementationClass="com.intellij.codeInspection.TodoCommentInspection"/>
<localInspection shortName="LongLine" bundle="messages.InspectionsBundle" key="long.line.display.name"
groupKey="inspection.general.tools.group.name" enabledByDefault="false"
level="WARNING" implementationClass="com.intellij.codeInspection.LongLineInspection"/>
level="WARNING" implementationClass="com.intellij.codeInspection.longLine.LongLineInspection"/>
<annotator language="TEXT" implementationClass="com.intellij.codeInsight.highlighting.LargeFilesAnnotator"/>
+2
View File
@@ -1918,6 +1918,8 @@
<library.dependencyScopeSuggester implementation="com.intellij.jarRepository.RepositoryLibraryDependencyScopeSuggester"/>
<applicationService serviceImplementation="com.intellij.jarRepository.services.MavenRepositoryServicesManager"/>
<postStartupActivity implementation="com.intellij.jarRepository.RepositoryLibrarySynchronizer"/>
<longLineInspectionPolicy implementation="com.intellij.codeInspection.JavaLongLineInspectionPolicy"/>
</extensions>
<actions>