mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-CR-56782: CPP-18466: Do not remove emty or new lines when formatting completions
Code completions are often not complete code chunks which requires to format them considering the need to keep empty and new lines. GitOrigin-RevId: 8b6d1a23aed3e5bf86416b2055101af4d1b414e2
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d7477fa5a6
commit
bc2adf7359
@@ -84,6 +84,10 @@ public abstract class CodeStyleManager {
|
||||
*/
|
||||
@NotNull public abstract PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException;
|
||||
|
||||
@NotNull
|
||||
public abstract PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly, boolean keepLineBreaks)
|
||||
throws IncorrectOperationException;
|
||||
|
||||
/**
|
||||
* Reformats part of the contents of the specified PSI element, enforces braces
|
||||
* and splits import statements according to the user's code style.
|
||||
@@ -117,6 +121,12 @@ public abstract class CodeStyleManager {
|
||||
int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException;
|
||||
|
||||
public abstract PsiElement reformatRange(@NotNull PsiElement element,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) throws IncorrectOperationException;
|
||||
|
||||
/**
|
||||
* Delegates to the {@link #reformatText(PsiFile, Collection)} with the single range defined by the given offsets.
|
||||
*
|
||||
|
||||
@@ -35,7 +35,7 @@ public interface ExternalFormatProcessor {
|
||||
* @return the range after formatting or null, if external format procedure cannot be applied to the source
|
||||
*/
|
||||
@Nullable
|
||||
TextRange format(@NotNull PsiFile source, @NotNull TextRange range, boolean canChangeWhiteSpacesOnly);
|
||||
TextRange format(@NotNull PsiFile source, @NotNull TextRange range, boolean canChangeWhiteSpacesOnly, boolean keepLineBreaks);
|
||||
|
||||
/**
|
||||
* Indents the line.
|
||||
@@ -96,9 +96,12 @@ public interface ExternalFormatProcessor {
|
||||
* @return the range after formatting or null, if external format procedure was not found or inactive (disabled)
|
||||
*/
|
||||
@Nullable
|
||||
static TextRange formatRangeInFile(@NotNull PsiFile source, @NotNull TextRange range, boolean canChangeWhiteSpacesOnly) {
|
||||
static TextRange formatRangeInFile(@NotNull PsiFile source,
|
||||
@NotNull TextRange range,
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) {
|
||||
ExternalFormatProcessor efp = activeExternalFormatProcessor(source);
|
||||
return efp != null ? efp.format(source, range, canChangeWhiteSpacesOnly) : null;
|
||||
return efp != null ? efp.format(source, range, canChangeWhiteSpacesOnly, keepLineBreaks) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,11 +113,12 @@ public interface ExternalFormatProcessor {
|
||||
@NotNull
|
||||
static PsiElement formatElement(@NotNull PsiElement elementToFormat,
|
||||
@NotNull TextRange range,
|
||||
boolean canChangeWhiteSpacesOnly) {
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) {
|
||||
final PsiFile file = elementToFormat.getContainingFile();
|
||||
final Document document = file.getViewProvider().getDocument();
|
||||
if (document != null) {
|
||||
final TextRange rangeAfterFormat = formatRangeInFile(file, range, canChangeWhiteSpacesOnly);
|
||||
final TextRange rangeAfterFormat = formatRangeInFile(file, range, canChangeWhiteSpacesOnly, keepLineBreaks);
|
||||
if (rangeAfterFormat != null) {
|
||||
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
|
||||
if (!elementToFormat.isValid()) {
|
||||
|
||||
+22
-5
@@ -73,6 +73,13 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting
|
||||
@Override
|
||||
@NotNull
|
||||
public PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException {
|
||||
return reformat(element, canChangeWhiteSpacesOnly, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly, boolean keepLineBreaks)
|
||||
throws IncorrectOperationException {
|
||||
CheckUtil.checkWritable(element);
|
||||
if( !SourceTreeToPsiMap.hasTreeElement( element ) )
|
||||
{
|
||||
@@ -82,7 +89,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting
|
||||
ASTNode treeElement = element.getNode();
|
||||
final PsiFile file = element.getContainingFile();
|
||||
if (ExternalFormatProcessor.useExternalFormatter(file)) {
|
||||
return ExternalFormatProcessor.formatElement(element, element.getTextRange(), canChangeWhiteSpacesOnly);
|
||||
return ExternalFormatProcessor.formatElement(element, element.getTextRange(), canChangeWhiteSpacesOnly, keepLineBreaks);
|
||||
}
|
||||
|
||||
final PsiElement formatted =
|
||||
@@ -128,18 +135,27 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement reformatRange(@NotNull PsiElement element,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) throws IncorrectOperationException {
|
||||
return reformatRangeImpl(element, startOffset, endOffset, canChangeWhiteSpacesOnly, keepLineBreaks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement reformatRange(@NotNull PsiElement element,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException {
|
||||
return reformatRangeImpl(element, startOffset, endOffset, canChangeWhiteSpacesOnly);
|
||||
return reformatRangeImpl(element, startOffset, endOffset, canChangeWhiteSpacesOnly, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset)
|
||||
throws IncorrectOperationException {
|
||||
return reformatRangeImpl(element, startOffset, endOffset, false);
|
||||
return reformatRangeImpl(element, startOffset, endOffset, false, false);
|
||||
|
||||
}
|
||||
|
||||
@@ -284,7 +300,8 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting
|
||||
private static PsiElement reformatRangeImpl(final @NotNull PsiElement element,
|
||||
final int startOffset,
|
||||
final int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException {
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) throws IncorrectOperationException {
|
||||
LOG.assertTrue(element.isValid());
|
||||
CheckUtil.checkWritable(element);
|
||||
if( !SourceTreeToPsiMap.hasTreeElement( element ) )
|
||||
@@ -295,7 +312,7 @@ public class CodeStyleManagerImpl extends CodeStyleManager implements Formatting
|
||||
ASTNode treeElement = element.getNode();
|
||||
final PsiFile file = element.getContainingFile();
|
||||
if (ExternalFormatProcessor.useExternalFormatter(file)) {
|
||||
return ExternalFormatProcessor.formatElement(element, TextRange.create(startOffset, endOffset), canChangeWhiteSpacesOnly);
|
||||
return ExternalFormatProcessor.formatElement(element, TextRange.create(startOffset, endOffset), canChangeWhiteSpacesOnly, keepLineBreaks);
|
||||
}
|
||||
|
||||
final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), element.getLanguage());
|
||||
|
||||
+17
@@ -86,6 +86,13 @@ public class MockCodeStyleManager extends CodeStyleManager {
|
||||
return reformat(element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElement reformat(@NotNull PsiElement element, boolean canChangeWhiteSpacesOnly, boolean keepLineBreaks)
|
||||
throws IncorrectOperationException {
|
||||
return reformat(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement reformatRange(@NotNull PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException {
|
||||
reformatText(element.getContainingFile(), startOffset, endOffset);
|
||||
@@ -99,6 +106,16 @@ public class MockCodeStyleManager extends CodeStyleManager {
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement reformatRange(@NotNull PsiElement element,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
boolean canChangeWhiteSpacesOnly,
|
||||
boolean keepLineBreaks) throws IncorrectOperationException {
|
||||
reformatText(element.getContainingFile(), startOffset, endOffset);
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException {
|
||||
reformatText(file, Collections.singletonList(new TextRange(startOffset, endOffset)));
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ShExternalFormatter implements ExternalFormatProcessor {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TextRange format(@NotNull PsiFile source, @NotNull TextRange range, boolean canChangeWhiteSpacesOnly) {
|
||||
public TextRange format(@NotNull PsiFile source, @NotNull TextRange range, boolean canChangeWhiteSpacesOnly, boolean keepLineBreaks) {
|
||||
doFormat(source.getProject(), source.getVirtualFile());
|
||||
return range;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ final class ShPostFormatProcessor implements PostFormatProcessor {
|
||||
@Override
|
||||
public TextRange processText(@NotNull PsiFile source, @NotNull TextRange rangeToReformat, @NotNull CodeStyleSettings settings) {
|
||||
if (!(source instanceof ShFile)) return rangeToReformat;
|
||||
TextRange range = ExternalFormatProcessor.formatRangeInFile(source, rangeToReformat, false);
|
||||
TextRange range = ExternalFormatProcessor.formatRangeInFile(source, rangeToReformat, false, false);
|
||||
return range != null ? range : rangeToReformat;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user