package org.jetbrains.plugins.ideavim.extension.commentary;

import org.jetbrains.plugins.ideavim.JavaVimTestCase;

import static com.maddyhome.idea.vim.command.CommandState.Mode.COMMAND;
import static com.maddyhome.idea.vim.helper.StringHelper.parseKeys;

/**
 * @author dhleong
 */
public class CommentaryExtensionTest extends JavaVimTestCase {

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    enableExtensions("commentary");
  }

  // |gc| |l|
  public void testBlockCommentSingle() {
    doTest(parseKeys("gcll"),
           "<caret>if (condition) {\n" + "}\n",
           "/<caret>*i*/f (condition) {\n" + "}\n");
    assertMode(COMMAND);
    assertSelection(null);
  }


  // |gc| |iw|
  public void testBlockCommentInnerWord() {
    doTest(parseKeys("gciw"),
           "<caret>if (condition) {\n" + "}\n",
           "<caret>/*if*/ (condition) {\n" + "}\n");
    assertMode(COMMAND);
    assertSelection(null);
  }

  // |gc| |iw|
  public void testBlockCommentTillForward() {
    doTest(parseKeys("gct{"),
           "<caret>if (condition) {\n" + "}\n",
           "<caret>/*if (condition) */{\n" + "}\n");
  }

  // |gc| |ab|
  public void testBlockCommentOuterParens() {
    doTest(parseKeys("gcab"),
           "if (<caret>condition) {\n" + "}\n",
           "if /*(condition)*/ {\n" + "}\n");
  }

  /*
   * NB: linewise motions become linewise comments;
   *  otherwise, they are incredibly difficult to undo
   */

  // |gc| |j|
  public void testLineCommentDown() {
    doTest(parseKeys("gcj"),
           "<caret>if (condition) {\n" + "}\n",
           "//if (condition) {\n" +
           "//}\n");
  }

  // |gc| |ip|
  public void testLineCommentInnerParagraph() {
    doTest(parseKeys("gcip"),
           "<caret>if (condition) {\n" + "}\n",
           "//if (condition) {\n" +
           "//}\n");
  }

  // |gc| |ip|
  public void testLineCommentSingleLineInnerParagraph() {
    doTest(parseKeys("gcip"),
           "<caret>if (condition) {}",
           "//if (condition) {}");
  }

  /* Ensure uncommenting works as well */

  // |gc| |ip|
  public void testLineUncommentInnerParagraph() {
    doTest(parseKeys("gcip"),
           "<caret>//if (condition) {\n" + "//}\n",
           "if (condition) {\n" +
           "}\n");
    assertMode(COMMAND);
    assertSelection(null);
  }

  // |gc| |ip|
  public void testLineUncommentSingleLineInnerParagraph() {
    doTest(parseKeys("gcip"),
           "<caret>//if (condition) {}",
           "if (condition) {}");
  }

  /* Visual mode */

  // |gc| |ip|
  public void testLineCommentVisualInnerParagraph() {
    doTest(parseKeys("vipgc"),
           "<caret>if (condition) {\n" + "}\n",
           "//if (condition) {\n" +
           "//}\n");
  }

  // |gc| |ip|
  public void testLineUncommentVisualInnerParagraph() {
    doTest(parseKeys("vipgc"),
           "<caret>//if (condition) {\n" + "//}\n",
           "if (condition) {\n" +
           "}\n");
  }




  /* Special shortcut gcc is always linewise */

  // |gcc|
  public void testLineCommentShortcut() {
    doTest(parseKeys("gccj"),
           "<caret>if (condition) {\n" + "}\n",
           "//if (condition) {\n" +
           "<caret>}\n");
    assertMode(COMMAND);
    assertSelection(null);
  }

  // |gcc|
  public void testLineUncommentShortcut() {
    doTest(parseKeys("gcc"),
           "<caret>//if (condition) {\n" + "}\n",
           "<caret>if (condition) {\n" +
           "}\n");
    assertMode(COMMAND);
    assertSelection(null);
  }
}

@@ -54,16 +54,66 @@
 ntary;%0A%0A
+import com.intellij.ide.highlighter.HtmlFileType;%0A
 import o
@@ -3558,11 +3558,263 @@
 ll);%0A  %7D
+%0A%0A  // %7Cgcc%7C%0A  public void testHTMLCommentShortcut() %7B%0A    myFixture.configureByText(HtmlFileType.INSTANCE, %22%3Cdiv /%3E%22);%0A    typeText(parseKeys(%22gcc%22));%0A    myFixture.checkResult(%22%3C!--%3Cdiv /%3E--%3E%22);%0A    assertMode(COMMAND);%0A    assertSelection(null);%0A  %7D
 %0A%7D%0A

Add an HTML filetype test