Ignore single space before star in comments (IJPL-178777)

GitOrigin-RevId: 1cddf384644a076d0a71f1a6487e72a78ed25e54
This commit is contained in:
Bas Leijdekkers
2025-02-20 18:48:19 +00:00
committed by intellij-monorepo-bot
parent b57e6e098b
commit 10011c9b79
2 changed files with 27 additions and 21 deletions
@@ -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;
@@ -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)) {