[formatter] refactor WhiteSpaceFormattingStrategy and co.

GitOrigin-RevId: b41ffe09ea82225661a435e047be29e3e94c0b7d
This commit is contained in:
Vojtech Balik
2022-07-01 14:10:27 +00:00
committed by intellij-monorepo-bot
parent 4be13e8463
commit 11b9c74949
5 changed files with 29 additions and 39 deletions
@@ -21,7 +21,6 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.impl.source.tree.LeafElement;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -34,15 +33,17 @@ import java.util.List;
*/
public class CompositeWhiteSpaceFormattingStrategy implements WhiteSpaceFormattingStrategy {
private final List<WhiteSpaceFormattingStrategy> myStrategies = new ArrayList<>();
private boolean myReplaceDefaultStrategy;
private final List<WhiteSpaceFormattingStrategy> myStrategies;
private final boolean myReplaceDefaultStrategy;
public CompositeWhiteSpaceFormattingStrategy(@NotNull Collection<? extends WhiteSpaceFormattingStrategy> strategies)
throws IllegalArgumentException
{
for (WhiteSpaceFormattingStrategy strategy : strategies) {
addStrategy(strategy);
}
public CompositeWhiteSpaceFormattingStrategy(boolean replaceDefaultStrategy,
@NotNull Collection<? extends WhiteSpaceFormattingStrategy> strategies) {
myStrategies = List.copyOf(strategies);
myReplaceDefaultStrategy = replaceDefaultStrategy;
}
public CompositeWhiteSpaceFormattingStrategy(@NotNull Collection<? extends WhiteSpaceFormattingStrategy> strategies) {
this(false, strategies);
}
@Override
@@ -68,17 +69,6 @@ public class CompositeWhiteSpaceFormattingStrategy implements WhiteSpaceFormatti
return myReplaceDefaultStrategy;
}
public void addStrategy(@NotNull WhiteSpaceFormattingStrategy strategy) throws IllegalArgumentException {
if (myReplaceDefaultStrategy && strategy.replaceDefaultStrategy()) {
throw new IllegalArgumentException(String.format(
"Can't combine strategy '%s' with already registered strategies (%s). Reason: given strategy is marked to replace "
+ "all existing strategies but strategy with such characteristics is already registered", strategy, myStrategies
));
}
myStrategies.add(strategy);
myReplaceDefaultStrategy |= strategy.replaceDefaultStrategy();
}
@NotNull
@Override
public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText,
@@ -160,12 +160,6 @@ public final class FormattingDocumentModelImpl implements FormattingDocumentMode
}
}
//@Override
//public boolean isWhiteSpaceSymbol(char symbol) {
// myBuffer.put(0, symbol);
// return myWhiteSpaceStrategy.check(myBuffer, 0, 1) > 0;
//}
public static boolean canUseDocumentModel(@NotNull Document document,@NotNull PsiFile file) {
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(file.getProject());
return !psiDocumentManager.isUncommited(document) &&
@@ -29,6 +29,7 @@ import org.jetbrains.annotations.NotNull;
* 'white space symbols' if necessary.
*
* @author Denis Zhdanov
* @see WhiteSpaceFormattingStrategyFactory
*/
public interface WhiteSpaceFormattingStrategy {
@@ -58,6 +59,7 @@ public interface WhiteSpaceFormattingStrategy {
* {@code false} to indicate that current strategy should be used in composition with default strategy
* if any, i.e. particular symbols sequence should be considered as white spaces if any of composed
* strategies defines so
* @see WhiteSpaceFormattingStrategyFactory#DEFAULT_STRATEGY
*/
boolean replaceDefaultStrategy();
@@ -26,7 +26,7 @@ import org.jetbrains.annotations.NotNull;
*/
public class WhiteSpaceFormattingStrategyAdapter implements WhiteSpaceFormattingStrategy {
private final WhiteSpaceFormattingStrategy DELEGATE = new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n');
private final WhiteSpaceFormattingStrategy DELEGATE = WhiteSpaceFormattingStrategyFactory.DEFAULT_STRATEGY;
@Override
public int check(@NotNull CharSequence text, int start, int end) {
@@ -19,9 +19,8 @@ import java.util.concurrent.atomic.AtomicReference;
* @author Denis Zhdanov
*/
public final class WhiteSpaceFormattingStrategyFactory {
private static final List<WhiteSpaceFormattingStrategy> SHARED_STRATEGIES = Collections.singletonList(
new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n')
);
public static final WhiteSpaceFormattingStrategy DEFAULT_STRATEGY =
new StaticSymbolWhiteSpaceDefinitionStrategy(' ', '\t', '\n');
private static final AtomicReference<WeakReference<Collection<WhiteSpaceFormattingStrategy>>> myCachedStrategies
= new AtomicReference<>();
@@ -33,7 +32,7 @@ public final class WhiteSpaceFormattingStrategyFactory {
* @return default language-agnostic white space strategy
*/
public static WhiteSpaceFormattingStrategy getStrategy() {
return new CompositeWhiteSpaceFormattingStrategy(SHARED_STRATEGIES);
return DEFAULT_STRATEGY;
}
/**
@@ -41,16 +40,21 @@ public final class WhiteSpaceFormattingStrategyFactory {
*
* @param language target language
* @return white space strategy to use for the given language
* @throws IllegalStateException if white space strategies configuration is invalid
*/
@NotNull
public static WhiteSpaceFormattingStrategy getStrategy(@NotNull Language language) throws IllegalStateException {
CompositeWhiteSpaceFormattingStrategy result = new CompositeWhiteSpaceFormattingStrategy(SHARED_STRATEGIES);
public static WhiteSpaceFormattingStrategy getStrategy(@NotNull Language language) {
WhiteSpaceFormattingStrategy strategy = LanguageWhiteSpaceFormattingStrategy.INSTANCE.forLanguage(language);
if (strategy != null) {
result.addStrategy(strategy);
if (strategy.replaceDefaultStrategy()) {
return strategy;
}
else {
return new CompositeWhiteSpaceFormattingStrategy(List.of(DEFAULT_STRATEGY, strategy));
}
}
else {
return getStrategy();
}
return result;
}
/**
@@ -65,7 +69,8 @@ public final class WhiteSpaceFormattingStrategyFactory {
}
final Collection<Language> languages = Language.getRegisteredLanguages();
Set<WhiteSpaceFormattingStrategy> result = new HashSet<>(SHARED_STRATEGIES);
Set<WhiteSpaceFormattingStrategy> result = new HashSet<>();
result.add(DEFAULT_STRATEGY);
final LanguageWhiteSpaceFormattingStrategy languageStrategy = LanguageWhiteSpaceFormattingStrategy.INSTANCE;
for (Language language : languages) {
final WhiteSpaceFormattingStrategy strategy = languageStrategy.forLanguage(language);
@@ -82,9 +87,8 @@ public final class WhiteSpaceFormattingStrategyFactory {
*
* @param editor editor that manages target document
* @return white space strategy for the document managed by the given editor
* @throws IllegalStateException if white space strategies configuration is invalid
*/
public static WhiteSpaceFormattingStrategy getStrategy(@NotNull Editor editor) throws IllegalStateException {
public static WhiteSpaceFormattingStrategy getStrategy(@NotNull Editor editor) {
Project project = editor.getProject();
if (project != null) {
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());