diff --git a/json/src/com/intellij/json/intentions/JsonSortPropertiesIntention.java b/json/src/com/intellij/json/intentions/JsonSortPropertiesIntention.java index 5d99b5ae8bcc..67471dcb914d 100644 --- a/json/src/com/intellij/json/intentions/JsonSortPropertiesIntention.java +++ b/json/src/com/intellij/json/intentions/JsonSortPropertiesIntention.java @@ -8,9 +8,7 @@ import com.intellij.json.psi.JsonObject; import com.intellij.json.psi.JsonProperty; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; +import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.util.CommonRefactoringUtil; @@ -20,6 +18,7 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -68,9 +67,12 @@ public class JsonSortPropertiesIntention implements IntentionAction, LowPriority assert parentObject instanceof JsonObject; // cycle-sort performs the minimal amount of modifications, and we want to patch the tree as little as possible cycleSortProperties(((JsonObject)parentObject).getPropertyList()); + SmartPsiElementPointer pointer = SmartPointerManager.createPointer(parentObject); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); + PsiElement element = pointer.getElement(); + if (element == null) return; CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); - codeStyleManager.reformat(parentObject); + codeStyleManager.reformatText(element.getContainingFile(), Collections.singleton(element.getTextRange())); } @Override diff --git a/json/tests/test/com/intellij/json/JsonIntentionActionTest.java b/json/tests/test/com/intellij/json/JsonIntentionActionTest.java index 1aeef2cbdf1a..5f70591d5e83 100644 --- a/json/tests/test/com/intellij/json/JsonIntentionActionTest.java +++ b/json/tests/test/com/intellij/json/JsonIntentionActionTest.java @@ -16,4 +16,8 @@ public class JsonIntentionActionTest extends JsonTestCase { public void testSortMalformedJson() { doTest("json.intention.sort.properties"); } + + public void testSortPropertiesShouldReformat() { + doTest("json.intention.sort.properties"); + } } diff --git a/json/tests/testData/intention/SortMalformedJson_after.json b/json/tests/testData/intention/SortMalformedJson_after.json index 9b980b39c1b6..e57729f6b24e 100644 --- a/json/tests/testData/intention/SortMalformedJson_after.json +++ b/json/tests/testData/intention/SortMalformedJson_after.json @@ -32,40 +32,40 @@ ] }, { - ",, - ".*credentials.*", - ": ", - Keys - Keys - be - be - can - "defaultValue": [ - , - "description": "" - ends - expressions. - "key" - "name": "endpoints.env.keys-to-sanitize" - or - property - ption - regex - sanitized. - "secret" - should - simple - "sourceType": "org.springframework.boot.actuate.endpoint.EnvironmentEndpoint" - strings - that - that - the, - "token", - "type": "java.lang.String[]", - "vcap_services", - with -] -}, + ",, + ".*credentials.*", + ": ", + Keys + Keys + be + be + can + "defaultValue": [ + , + "description": "" + ends + expressions. + "key" + "name": "endpoints.env.keys-to-sanitize" + or + property + ption + regex + sanitized. + "secret" + should + simple + "sourceType": "org.springframework.boot.actuate.endpoint.EnvironmentEndpoint" + strings + that + that + the, + "token", + "type": "java.lang.String[]", + "vcap_services", + with + ] + }, { "name": "endpoints.metrics.filter.enabled", "type": "java.lang.Boolean", diff --git a/json/tests/testData/intention/SortPropertiesShouldReformat.json b/json/tests/testData/intention/SortPropertiesShouldReformat.json new file mode 100644 index 000000000000..f0f9f8afe99e --- /dev/null +++ b/json/tests/testData/intention/SortPropertiesShouldReformat.json @@ -0,0 +1,16 @@ +{ + "description": "The Laravel Framework.", + "keywords": [ + "framework", + "laravel" + ], + "license": "MIT", + "name": "laravel/laravel", + "require": { + "php": ">=5.6.4", + "twbs/bootstrap": "^3.3", + "laravel/framework": "5.4.", + "laravel/tinker": "~1.0", + "laravelcollective/html": "^5.4" + } +} diff --git a/json/tests/testData/intention/SortPropertiesShouldReformat_after.json b/json/tests/testData/intention/SortPropertiesShouldReformat_after.json new file mode 100644 index 000000000000..b1ba21d52c68 --- /dev/null +++ b/json/tests/testData/intention/SortPropertiesShouldReformat_after.json @@ -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" + } +}