mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
json formatter low-hanging optimization: get settings only once
This commit is contained in:
@@ -39,7 +39,7 @@ public class JsonBlock implements ASTBlock {
|
||||
private final Alignment myAlignment;
|
||||
private final Indent myIndent;
|
||||
private final Wrap myWrap;
|
||||
private final CodeStyleSettings mySettings;
|
||||
private final JsonCodeStyleSettings myCustomSettings;
|
||||
private final SpacingBuilder mySpacingBuilder;
|
||||
// lazy initialized on first call to #getSubBlocks()
|
||||
private List<Block> mySubBlocks = null;
|
||||
@@ -47,27 +47,43 @@ public class JsonBlock implements ASTBlock {
|
||||
private final Alignment myPropertyValueAlignment;
|
||||
private final Wrap myChildWrap;
|
||||
|
||||
/**
|
||||
* @deprecated Please use overload with settings JsonCodeStyleSettings and spacingBuilder.
|
||||
* Getting settings should be done only for the root block.
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("unused") //used externally
|
||||
public JsonBlock(@Nullable JsonBlock parent,
|
||||
@NotNull ASTNode node,
|
||||
@NotNull CodeStyleSettings settings,
|
||||
@Nullable Alignment alignment,
|
||||
@NotNull Indent indent,
|
||||
@Nullable Wrap wrap) {
|
||||
this(parent, node, settings.getCustomSettings(JsonCodeStyleSettings.class), alignment, indent, wrap,
|
||||
JsonFormattingBuilderModel.createSpacingBuilder(settings));
|
||||
}
|
||||
|
||||
public JsonBlock(@Nullable JsonBlock parent,
|
||||
@NotNull ASTNode node,
|
||||
@NotNull JsonCodeStyleSettings customSettings,
|
||||
@Nullable Alignment alignment,
|
||||
@NotNull Indent indent,
|
||||
@Nullable Wrap wrap,
|
||||
@NotNull SpacingBuilder spacingBuilder) {
|
||||
myParent = parent;
|
||||
myNode = node;
|
||||
myPsiElement = node.getPsi();
|
||||
myAlignment = alignment;
|
||||
myIndent = indent;
|
||||
myWrap = wrap;
|
||||
mySettings = settings;
|
||||
|
||||
mySpacingBuilder = JsonFormattingBuilderModel.createSpacingBuilder(settings);
|
||||
mySpacingBuilder = spacingBuilder;
|
||||
myCustomSettings = customSettings;
|
||||
|
||||
if (myPsiElement instanceof JsonObject) {
|
||||
myChildWrap = Wrap.createWrap(getCustomSettings().OBJECT_WRAPPING, true);
|
||||
myChildWrap = Wrap.createWrap(myCustomSettings.OBJECT_WRAPPING, true);
|
||||
}
|
||||
else if (myPsiElement instanceof JsonArray) {
|
||||
myChildWrap = Wrap.createWrap(getCustomSettings().ARRAY_WRAPPING, true);
|
||||
myChildWrap = Wrap.createWrap(myCustomSettings.ARRAY_WRAPPING, true);
|
||||
}
|
||||
else {
|
||||
myChildWrap = null;
|
||||
@@ -91,7 +107,7 @@ public class JsonBlock implements ASTBlock {
|
||||
@Override
|
||||
public List<Block> getSubBlocks() {
|
||||
if (mySubBlocks == null) {
|
||||
int propertyAlignment = getCustomSettings().PROPERTY_ALIGNMENT;
|
||||
int propertyAlignment = myCustomSettings.PROPERTY_ALIGNMENT;
|
||||
ASTNode[] children = myNode.getChildren(null);
|
||||
mySubBlocks = ContainerUtil.newArrayListWithCapacity(children.length);
|
||||
for (ASTNode child: children) {
|
||||
@@ -136,7 +152,7 @@ public class JsonBlock implements ASTBlock {
|
||||
}
|
||||
}
|
||||
}
|
||||
return new JsonBlock(this, childNode, mySettings, alignment, indent, wrap);
|
||||
return new JsonBlock(this, childNode, myCustomSettings, alignment, indent, wrap, mySpacingBuilder);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -202,8 +218,4 @@ public class JsonBlock implements ASTBlock {
|
||||
private static boolean isWhitespaceOrEmpty(ASTNode node) {
|
||||
return node.getElementType() == TokenType.WHITE_SPACE || node.getTextLength() == 0;
|
||||
}
|
||||
|
||||
private JsonCodeStyleSettings getCustomSettings() {
|
||||
return mySettings.getCustomSettings(JsonCodeStyleSettings.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,10 @@ package com.intellij.json.formatter;
|
||||
|
||||
import com.intellij.formatting.*;
|
||||
import com.intellij.json.JsonLanguage;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static com.intellij.json.JsonElementTypes.*;
|
||||
|
||||
@@ -20,10 +16,13 @@ public class JsonFormattingBuilderModel implements FormattingModelBuilder {
|
||||
@NotNull
|
||||
@Override
|
||||
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
|
||||
final JsonBlock block = new JsonBlock(null, element.getNode(), settings, null, Indent.getNoneIndent(), null);
|
||||
JsonCodeStyleSettings customSettings = settings.getCustomSettings(JsonCodeStyleSettings.class);
|
||||
SpacingBuilder spacingBuilder = createSpacingBuilder(settings);
|
||||
final JsonBlock block = new JsonBlock(null, element.getNode(), customSettings, null, Indent.getNoneIndent(), null, spacingBuilder);
|
||||
return FormattingModelProvider.createFormattingModelForPsiFile(element.getContainingFile(), block, settings);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
|
||||
final JsonCodeStyleSettings jsonSettings = settings.getCustomSettings(JsonCodeStyleSettings.class);
|
||||
final CommonCodeStyleSettings commonSettings = settings.getCommonSettings(JsonLanguage.INSTANCE);
|
||||
|
||||
Reference in New Issue
Block a user