From 37caefe8ac667969a1dd48dfe0e93622b9eb8e1d Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 6 Nov 2013 15:56:59 +0100 Subject: [PATCH] IDEA-72889 (CodeStyle inspection to check tabs instead of spaces in file) --- .../src/META-INF/InspectionGadgets.xml | 4 + .../siyeh/InspectionGadgetsBundle.properties | 4 + .../com/siyeh/ig/BaseInspectionVisitor.java | 11 +- .../ProblematicWhitespaceInspection.java | 165 ++++++++++++++++++ .../ProblematicWhitespace.html | 10 ++ .../ProblematicWhitespaceInspectionTest.java | 76 ++++++++ 6 files changed, 261 insertions(+), 9 deletions(-) create mode 100644 plugins/InspectionGadgets/src/com/siyeh/ig/style/ProblematicWhitespaceInspection.java create mode 100644 plugins/InspectionGadgets/src/inspectionDescriptions/ProblematicWhitespace.html create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 1ca82904785d..18654c807735 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -2388,6 +2388,10 @@ key="size.replaceable.by.isempty.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.style.SizeReplaceableByIsEmptyInspection"/> + + +Reports tabs used for indentation when the code style is configured to use only spaces. Reports spaces used for indentation when +the code style is configured to use only tabs. Reports spaces used for indentation and tabs used for alignment when the code +style is configured to use smart tabs. + +

+ + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java new file mode 100644 index 000000000000..c0e062c6d79b --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2013 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.style; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.intellij.ide.highlighter.JavaFileType; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class ProblematicWhitespaceInspectionTest extends LightInspectionTestCase { + + public void testTabsInFile() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = false; + doTest("/*File 'X.java' uses tabs for indentation*/class X {\n" + + "\tString s;\n" + + "}\n/**/"); + } + + public void testTabsInFile2() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true; + doTest("class X {\n" + + "\tString s;\n" + + "}\n"); + } + + public void testSpacesInFile() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true; + doTest("/*File 'X.java' uses spaces for indentation*/class X {\n" + + " String s;\n" + + "}\n/**/"); + } + + public void testSpacesInFile2() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = false; + doTest("class X {\n" + + " String s;\n" + + "}\n"); + } + + public void testSmartTabsInFile() { + final CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()); + final CommonCodeStyleSettings.IndentOptions options = settings.getIndentOptions(JavaFileType.INSTANCE); + options.USE_TAB_CHARACTER = true; + options.SMART_TABS = true; + doTest("/*File 'X.java' uses spaces for indentation*/class X {\n" + + " \tString s;\n" + + "}\n/**/"); + } + + @Override + protected InspectionProfileEntry getInspection() { + return new ProblematicWhitespaceInspection(); + } +}