summaryrefslogtreecommitdiff
path: root/platform/platform-tests/testSrc/com/intellij/application/options/codeInsight/editor/quotes/SelectionQuotingTypedHandlerTest.java
blob: 0092d115c8c19f2495f89e2f5a16253dbf130d96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.application.options.codeInsight.editor.quotes;

import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;

/**
 * @author oleg
 */
public class SelectionQuotingTypedHandlerTest extends LightPlatformCodeInsightFixtureTestCase {

  private boolean myPrevValue;

 /**
   * Perfoms an action as write action
   *
   * @param project Project
   * @param action  Runnable to be executed
   */
  public static void performAction(final Project project, final Runnable action) {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        CommandProcessor.getInstance().executeCommand(project, action, "test command", null);
      }
    });
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    myPrevValue = CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED;
    CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = true;
  }

  @Override
  protected void tearDown() throws Exception {
    CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED = myPrevValue;
    super.tearDown();
  }

  public void testWOSelection() {
    doTest("\"", "aaa\nbbb\n\n", "\"aaa\nbbb\n\n");
  }

  public void testWithSelection() {
    doTest("\"", "<selection><caret>aaa\n</selection>bbb\n\n", "\"aaa\n\"bbb\n\n");
  }

  public void testWithSingleCharSelection() {
    doTest("\"", "<selection><caret>a</selection>aa\nbbb\n\n", "\"a\"aa\nbbb\n\n");
  }

  public void testWithBacktick() {
    doTest("`", "<selection><caret>a</selection>aa\nbbb\n\n", "`a`aa\nbbb\n\n");
  }

  public void testChangeQuotes() {
    doTest("\'", "<selection><caret>\"aaa\"</selection>\nbbb\n\n", "'aaa'\nbbb\n\n");
  }

  public void testChangeBrackets() {
    doTest("[", "<selection><caret>(aaa)</selection>\nbbb\n\n", "[aaa]\nbbb\n\n");
  }

  public void testDontChangeBrackets() {
    doTest("(", "aaa<selection>[foo][bar]<caret></selection>bbb", "aaa([foo][bar])bbb");
  }

  public void testDoubleBrackets() {
    doTest("[", "<selection><caret>[aaa]</selection>\nbbb\n\n", "[[aaa]]\nbbb\n\n");
  }

  public void testChangeNonSimilar() {
    doTest("[", "<selection><caret>\"aaa\"</selection>\nbbb\n\n", "[\"aaa\"]\nbbb\n\n");
  }

  public void testReplaceBracketAndText() {
    doTest("'a", "<selection><caret>\"b\"</selection>\nbbb\n\n", "'a<caret>'\nbbb\n\n");
  }

  public void testTripleEnquote() {
    doTest("\"\"\"", "<selection>text<caret></selection>\nbbb\n\n", "\"\"\"<selection>text</selection>\"\"\"\nbbb\n\n");
  }

  public void testMultipleCarets() {
    doTest("\"",
           "aa<caret>a <selection><caret>bbb</selection> c<selection>c<caret>c</selection>",
           "aa\"<caret>a \"<selection><caret>bbb</selection>\" c\"<selection><caret>cc</selection>\"");
  }

  private void doTest(@NotNull final String cs, @NotNull String before, @NotNull String expected) {
    myFixture.configureByText(FileTypes.PLAIN_TEXT, before);
    final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();

    performAction(myFixture.getProject(), new Runnable() {
      @Override
      public void run() {
        for (int i = 0, max = cs.length(); i < max; i++) {
          final char c = cs.charAt(i);
          typedAction.actionPerformed(myFixture.getEditor(), c, ((EditorEx)myFixture.getEditor()).getDataContext());
        }
      }
    });
    myFixture.checkResult(expected);
  }

  public void testRuby7852ErrantEditor() {
    myFixture.configureByText(FileTypes.PLAIN_TEXT, "\"aaa\"\nbbb\n\n");
    myFixture.getEditor().getCaretModel().moveToOffset(0);
    myFixture.getEditor().getSelectionModel().setSelection(0, 5);
    final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
    performAction(myFixture.getProject(), new Runnable() {
      @Override
      public void run() {
        typedAction.actionPerformed(myFixture.getEditor(), '\'', ((EditorEx)myFixture.getEditor()).getDataContext());
      }
    });
    myFixture.getEditor().getSelectionModel().removeSelection();
    myFixture.checkResult("'aaa'\nbbb\n\n");

    myFixture.getEditor().getCaretModel().moveToOffset(myFixture.getEditor().getDocument().getLineStartOffset(3));
    performAction(myFixture.getProject(), new Runnable() {
      @Override
      public void run() {
        typedAction.actionPerformed(myFixture.getEditor(), 'A', ((EditorEx)myFixture.getEditor()).getDataContext());
        typedAction.actionPerformed(myFixture.getEditor(), 'B', ((EditorEx)myFixture.getEditor()).getDataContext());
      }
    });
    myFixture.checkResult("'aaa'\nbbb\n\nAB");
  }
}