From 10011c9b792456809cdf314b1ba1c2e19084ddfe Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 20 Feb 2025 16:08:47 +0100 Subject: [PATCH] Ignore single space before star in comments (IJPL-178777) GitOrigin-RevId: 1cddf384644a076d0a71f1a6487e72a78ed25e54 --- .../ProblematicWhitespaceInspectionTest.java | 33 ++++++++++--------- .../ProblematicWhitespaceInspection.java | 15 +++++---- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/java/java-tests/testSrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java index ef9a80e69e41..9f0d6b50323f 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/style/ProblematicWhitespaceInspectionTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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. - */ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.siyeh.ig.style; import com.intellij.application.options.CodeStyle; @@ -60,6 +46,23 @@ public class ProblematicWhitespaceInspectionTest extends LightJavaInspectionTest """); } + public void testDocComments() { + final CodeStyleSettings settings = CodeStyle.getSettings(getProject()); + settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true; + myFixture.configureByText("X.java", """ + /** + * This is class X. + */ + class X { + \t/** + \t * This is field s. + \t */ + \tString s; + } + """); + myFixture.testHighlighting(true, false, false); + } + public void testSpacesInFile() { final CodeStyleSettings settings = CodeStyle.getSettings(getProject()); settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ProblematicWhitespaceInspection.java b/platform/lang-impl/src/com/intellij/codeInspection/ProblematicWhitespaceInspection.java index e02c806a0ab9..4b698eaf433f 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ProblematicWhitespaceInspection.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ProblematicWhitespaceInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection; import com.intellij.application.options.CodeStyle; @@ -12,13 +12,11 @@ import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiElementVisitor; -import com.intellij.psi.PsiFile; +import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; /** @@ -133,7 +131,7 @@ public final class ProblematicWhitespaceInspection extends LocalInspectionTool { else if (c == ' ') { if (useTabs) { if (!smartTabs) { - if (registerError(file, startOffset, true)) { + if (!isSpaceBeforeCommentStar(file, j, line) && registerError(file, startOffset, true)) { return; } } @@ -158,6 +156,11 @@ public final class ProblematicWhitespaceInspection extends LocalInspectionTool { } } + private static boolean isSpaceBeforeCommentStar(@NotNull PsiFile file, int j, String line) { + return j + 1 < line.length() && line.charAt(j + 1) == '*' + && PsiTreeUtil.getParentOfType(file.findElementAt(j), PsiComment.class, false) != null; + } + private boolean registerError(PsiFile file, int startOffset, boolean tab) { final PsiElement element = file.findElementAt(startOffset); if (element != null && isSuppressedFor(element)) {