package com.intellij.json;

import com.intellij.json.formatter.JsonCodeStyleSettings;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import org.jetbrains.annotations.NotNull;

/**
 * @author Mikhail Golubev
 */
public class JsonEditingTest extends JsonTestCase {

  private void doTest(@NotNull final String characters) {
    final String testName = "editing/" + getTestName(false);
    myFixture.configureByFile(testName + ".json");
    final int offset = myFixture.getEditor().getCaretModel().getOffset();
    WriteCommandAction.runWriteCommandAction(null, (Computable<PsiFile>)() -> {
      myFixture.getEditor().getCaretModel().moveToOffset(offset);
      myFixture.type(characters);
      return myFixture.getFile();
    });
    myFixture.checkResultByFile(testName + ".after.json");
  }

  public void testContinuationIndentAfterPropertyKey() {
    doTest("\n");
  }

  public void testContinuationIndentAfterColon() {
    doTest("\n");
  }

  // IDEA-130594
  public void testNormalIndentAfterPropertyWithoutComma() {
    doTest("\n");
  }

  // WEB-13675
  public void testIndentWithTabsWhenSmartTabEnabled() {
    final CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions();
    final CommonCodeStyleSettings.IndentOptions oldSettings = (CommonCodeStyleSettings.IndentOptions)indentOptions.clone();
    indentOptions.TAB_SIZE = 4;
    indentOptions.INDENT_SIZE = 4;
    indentOptions.USE_TAB_CHARACTER = true;
    indentOptions.SMART_TABS = true;
    try {
      doTest("\n\"baz\"");
    }
    finally {
      indentOptions.copyFrom(oldSettings);
    }
  }

  // Moved from JavaScript

  // WEB-11600
  public void testEnterWhenPropertiesAlignedOnValue() {
    doEnterTestForWeb11600();
  }

  // WEB-11600
  public void testEnterWhenPropertiesAlignedOnValue1() {
    doEnterTestForWeb11600();
  }

  private void doEnterTestForWeb11600() {
    final JsonCodeStyleSettings settings = getCustomCodeStyleSettings();
    final CommonCodeStyleSettings.IndentOptions indentOptions = getIndentOptions();

    final int oldPropertyAlignment = settings.PROPERTY_ALIGNMENT;
    final int oldIndentSize = indentOptions.INDENT_SIZE;
    settings.PROPERTY_ALIGNMENT = JsonCodeStyleSettings.ALIGN_PROPERTY_ON_VALUE;
    indentOptions.INDENT_SIZE = 4;
    try {
      doTest("\n");
    }
    finally {
      indentOptions.INDENT_SIZE = oldIndentSize;
      settings.PROPERTY_ALIGNMENT = oldPropertyAlignment;
    }
  }
}

@@ -83,142 +83,8 @@
 gs;%0A
-import com.intellij.openapi.command.WriteCommandAction;%0Aimport com.intellij.openapi.util.Computable;%0Aimport com.intellij.psi.PsiFile;%0A
 impo
@@ -134,24 +134,24 @@
 leSettings;%0A
+
 import org.j
@@ -447,297 +447,33 @@
     
-final int offset = myFixture.getEditor().getCaretModel().getOffset();%0A    WriteCommandAction.runWriteCommandAction(null, (Computable%3CPsiFile%3E)() -%3E %7B%0A      myFixture.getEditor().getCaretModel().moveToOffset(offset);%0A      myFixture.type(characters);%0A      return myFixture.getFile();%0A    %7D
+myFixture.type(characters
 );%0A 

remove unnecessary write action in some tests