mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Java. Code Formatting] IDEA-388356 Restore old behaviour for tabs when doing alignment
(cherry picked from commit 20a632fbf2c6281ebf957a7909ac655215334bc9) IJ-CR-212269 GitOrigin-RevId: c9f45723c2229016952141387a2a251317618b0e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b859639a82
commit
7c7f313161
+3
-3
@@ -1,8 +1,8 @@
|
||||
public class Formatter {
|
||||
void foo() {
|
||||
String sss = """
|
||||
|
||||
|
||||
foo""";
|
||||
|
||||
|
||||
foo""";
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
class A {
|
||||
int a;
|
||||
|
||||
void foo(int a,
|
||||
int b) {
|
||||
int b) {
|
||||
boolean b;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
class A {
|
||||
int a;
|
||||
|
||||
void foo(int a,
|
||||
int b) {
|
||||
int b) {
|
||||
boolean b;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
class A {
|
||||
int a;
|
||||
|
||||
void foo(int a,
|
||||
int b) {
|
||||
int b) {
|
||||
boolean b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
public class A {
|
||||
public void foo1(int i1, int i2,
|
||||
int i3, int i4,
|
||||
int i5, int i6,
|
||||
int i7) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
public class A {
|
||||
public void foo1(int i1, int i2,
|
||||
int i3, int i4,
|
||||
int i5, int i6,
|
||||
int i7) {
|
||||
|
||||
}
|
||||
}
|
||||
+12
-15
@@ -1,22 +1,9 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.psi.formatter.java;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ex.PathManagerEx;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
@@ -25,6 +12,7 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.testFramework.LightIdeaTestCase;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
|
||||
@@ -94,6 +82,15 @@ public class TabIndentingTest extends LightIdeaTestCase {
|
||||
doTest("moreTabsInComments.java", "moreTabsInComments_after.java");
|
||||
}
|
||||
|
||||
public void testAlignmentWithTabs() throws Exception {
|
||||
CodeStyleSettings settings = CodeStyle.getSettings(getProject());
|
||||
settings.getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true;
|
||||
settings.getIndentOptions(JavaFileType.INSTANCE).TAB_SIZE = 4;
|
||||
settings.getCommonSettings(JavaLanguage.INSTANCE).METHOD_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
|
||||
settings.getCommonSettings(JavaLanguage.INSTANCE).ALIGN_MULTILINE_PARAMETERS = true;
|
||||
doTest("methodAlignment.java", "methodAlignment_after.java");
|
||||
}
|
||||
|
||||
private void doTest(String fileNameBefore, String fileNameAfter) throws Exception {
|
||||
String text = loadFile(fileNameBefore);
|
||||
final PsiFile file = createFile(fileNameBefore, text);
|
||||
|
||||
@@ -43,6 +43,10 @@
|
||||
<with attribute="implementationClass" implements="com.intellij.openapi.editor.IndentStrategy"/>
|
||||
</extensionPoint>
|
||||
|
||||
<extensionPoint name="lang.formatter.alignmentWhitespacePolicy" beanClass="com.intellij.lang.LanguageExtensionPoint" dynamic="true">
|
||||
<with attribute="implementationClass" implements="com.intellij.formatting.AlignmentWhitespacePolicy"/>
|
||||
</extensionPoint>
|
||||
|
||||
<extensionPoint name="fileIndentOptionsProvider" interface="com.intellij.psi.codeStyle.FileIndentOptionsProvider" dynamic="true"/>
|
||||
<extensionPoint name="fileCodeStyleProvider" interface="com.intellij.psi.codeStyle.FileCodeStyleProvider" dynamic="true"/>
|
||||
<extensionPoint name="codeFormattingDataPreparer" interface="com.intellij.psi.impl.source.codeStyle.CodeFormattingDataPreparer" dynamic="true"/>
|
||||
|
||||
+6
-3
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.formatting;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -70,8 +70,11 @@ public abstract class AbstractBlockAlignmentProcessor implements BlockAlignmentP
|
||||
|
||||
WhiteSpace previousWhiteSpace = offsetResponsibleBlock.getWhiteSpace();
|
||||
previousWhiteSpace.setSpaces(previousWhiteSpace.getSpaces() - diff, previousWhiteSpace.getIndentOffset());
|
||||
// Backward shift introduces alignment spaces. Keep those exempt from tab conversion.
|
||||
previousWhiteSpace.setForceSkipTabulationsUsage(true);
|
||||
if (!previousWhiteSpace.containsLineFeeds() ||
|
||||
LanguageAlignmentWhitespacePolicy.useSpacesForAlignment(offsetResponsibleBlock)) {
|
||||
// Avoid tabulations usage for aligning blocks that are not the first blocks on a line.
|
||||
previousWhiteSpace.setForceSkipTabulationsUsage(true);
|
||||
}
|
||||
|
||||
return Result.BACKWARD_BLOCK_ALIGNED;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.formatting;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
/**
|
||||
* This policy allows enforcing using spaces for alignment that is necessary for some languages even if USE_TAB_CHARACTER option is enabled.
|
||||
* @see WhiteSpace#setForceSkipTabulationsUsage(boolean)
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
public interface AlignmentWhitespacePolicy {
|
||||
boolean useSpacesForAlignment();
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.formatting;
|
||||
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.lang.LanguageExtension;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public final class LanguageAlignmentWhitespacePolicy extends LanguageExtension<AlignmentWhitespacePolicy> {
|
||||
private static final LanguageAlignmentWhitespacePolicy INSTANCE = new LanguageAlignmentWhitespacePolicy();
|
||||
|
||||
private LanguageAlignmentWhitespacePolicy() {
|
||||
super("com.intellij.lang.formatter.alignmentWhitespacePolicy");
|
||||
}
|
||||
|
||||
static boolean useSpacesForAlignment(@Nullable AbstractBlockWrapper block) {
|
||||
if (block == null) return false;
|
||||
Language language = block.getLanguage();
|
||||
if (language == null) return false;
|
||||
AlignmentWhitespacePolicy policy = INSTANCE.forLanguage(language);
|
||||
return policy != null && policy.useSpacesForAlignment();
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.formatting;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -44,7 +44,8 @@ public final class LeftEdgeAlignmentProcessor extends AbstractBlockAlignmentProc
|
||||
protected boolean applyIndentToTheFirstBlockOnLine(@NotNull IndentData alignmentAnchorIndent, @NotNull Context context) {
|
||||
WhiteSpace whiteSpace = context.targetBlock().getWhiteSpace();
|
||||
whiteSpace.setSpaces(alignmentAnchorIndent.getSpaces(), alignmentAnchorIndent.getIndentSpaces());
|
||||
if (alignmentAnchorIndent.getSpaces() > 0) {
|
||||
if (alignmentAnchorIndent.getSpaces() > 0 &&
|
||||
LanguageAlignmentWhitespacePolicy.useSpacesForAlignment(context.targetBlock())) {
|
||||
// Alignment spaces should not be converted into tabs
|
||||
whiteSpace.setForceSkipTabulationsUsage(true);
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.formatting;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
@@ -48,7 +48,7 @@ public final class RightEdgeAlignmentProcessor extends AbstractBlockAlignmentPro
|
||||
|
||||
if (indentSpaces >= 0) {
|
||||
whiteSpace.setSpaces(spaces, indentSpaces);
|
||||
if (spaces > 0) {
|
||||
if (spaces > 0 && LanguageAlignmentWhitespacePolicy.useSpacesForAlignment(context.targetBlock())) {
|
||||
// Alignment spaces should not be converted into tabs
|
||||
whiteSpace.setForceSkipTabulationsUsage(true);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public final class RightEdgeAlignmentProcessor extends AbstractBlockAlignmentPro
|
||||
}
|
||||
}
|
||||
whiteSpace.setSpaces(spaces, indentSpaces);
|
||||
if (spaces > 0) {
|
||||
if (spaces > 0 && LanguageAlignmentWhitespacePolicy.useSpacesForAlignment(context.targetBlock())) {
|
||||
// Alignment spaces should not be converted into tabs
|
||||
whiteSpace.setForceSkipTabulationsUsage(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-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
|
||||
package com.intellij.formatting;
|
||||
|
||||
@@ -444,9 +444,7 @@ public final class WhiteSpace {
|
||||
* will be used for tab representation if there are non-white space symbols before it (IJ editor may use different number of columns
|
||||
* for single tabulation symbol representation).
|
||||
* <p/>
|
||||
* Hence, we can ask current white space object to avoid using tabulation symbols for alignment.
|
||||
* Tab usage is not necessarily suppressed entirely: if this whitespace starts a new line, tabs may still be
|
||||
* used for indentation while alignment remains spaces.
|
||||
* Some languages may apply this option to disable the usage of tabulation during alignment. See {@link AlignmentWhitespacePolicy}.
|
||||
*
|
||||
* @param skip indicates if tabulation symbols usage should be suppressed for alignment
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user