IDEA-226522 Sort properties alphabetically breaks indentation in JSON

GitOrigin-RevId: 2f7299bfb9ccfd2f14e9b6ec15078c8b41c38dba
This commit is contained in:
Anton Lobov
2019-12-10 18:49:18 +01:00
committed by intellij-monorepo-bot
parent a9285da09f
commit d8df42bfc8
5 changed files with 76 additions and 38 deletions

View File

@@ -8,9 +8,7 @@ import com.intellij.json.psi.JsonObject;
import com.intellij.json.psi.JsonProperty; import com.intellij.json.psi.JsonProperty;
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.CommonRefactoringUtil; import com.intellij.refactoring.util.CommonRefactoringUtil;
@@ -20,6 +18,7 @@ import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@@ -68,9 +67,12 @@ public class JsonSortPropertiesIntention implements IntentionAction, LowPriority
assert parentObject instanceof JsonObject; assert parentObject instanceof JsonObject;
// cycle-sort performs the minimal amount of modifications, and we want to patch the tree as little as possible // cycle-sort performs the minimal amount of modifications, and we want to patch the tree as little as possible
cycleSortProperties(((JsonObject)parentObject).getPropertyList()); cycleSortProperties(((JsonObject)parentObject).getPropertyList());
SmartPsiElementPointer<PsiElement> pointer = SmartPointerManager.createPointer(parentObject);
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
PsiElement element = pointer.getElement();
if (element == null) return;
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(parentObject); codeStyleManager.reformatText(element.getContainingFile(), Collections.singleton(element.getTextRange()));
} }
@Override @Override

View File

@@ -16,4 +16,8 @@ public class JsonIntentionActionTest extends JsonTestCase {
public void testSortMalformedJson() { public void testSortMalformedJson() {
doTest("json.intention.sort.properties"); doTest("json.intention.sort.properties");
} }
public void testSortPropertiesShouldReformat() {
doTest("json.intention.sort.properties");
}
} }

View File

@@ -32,40 +32,40 @@
] ]
}, },
{ {
",<caret>, ",<caret>,
".*credentials.*", ".*credentials.*",
": ", ": ",
Keys Keys
Keys Keys
be be
be be
can can
"defaultValue": [ "defaultValue": [
, ,
"description": "" "description": ""
ends ends
expressions. expressions.
"key" "key"
"name": "endpoints.env.keys-to-sanitize" "name": "endpoints.env.keys-to-sanitize"
or or
property property
ption ption
regex regex
sanitized. sanitized.
"secret" "secret"
should should
simple simple
"sourceType": "org.springframework.boot.actuate.endpoint.EnvironmentEndpoint" "sourceType": "org.springframework.boot.actuate.endpoint.EnvironmentEndpoint"
strings strings
that that
that that
the, the,
"token", "token",
"type": "java.lang.String[]", "type": "java.lang.String[]",
"vcap_services", "vcap_services",
with with
] ]
}, },
{ {
"name": "endpoints.metrics.filter.enabled", "name": "endpoints.metrics.filter.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",

View File

@@ -0,0 +1,16 @@
{
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"name": "laravel/laravel",
"require": {
"p<caret>hp": ">=5.6.4",
"twbs/bootstrap": "^3.3",
"laravel/framework": "5.4.",
"laravel/tinker": "~1.0",
"laravelcollective/html": "^5.4"
}
}

View File

@@ -0,0 +1,16 @@
{
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"name": "laravel/laravel",
"require": {
"laravel/framework": "5.4.",
"laravel/tinker": "~1.0",
"laravelcollective/html": "^5.4",
"php": ">=5.6.4",
"twbs/bootstrap": "^3.3"
}
}