1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-04 21:32:52 +01:00

Added unit test-friendly version of VimExtensionFacade.getKeyStroke()

This commit is contained in:
Andrey Vlasovskikh 2016-01-26 18:14:14 +03:00
parent 2d86054fe8
commit 1a96533a3c
4 changed files with 102 additions and 29 deletions
src/com/maddyhome/idea/vim
test/org/jetbrains/plugins/ideavim

View File

@ -19,11 +19,13 @@
package com.maddyhome.idea.vim.extension;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Ref;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.MappingMode;
import com.maddyhome.idea.vim.helper.TestInputModel;
import com.maddyhome.idea.vim.key.OperatorFunction;
import com.maddyhome.idea.vim.ui.ModalEntryDialog;
import org.jetbrains.annotations.NotNull;
@ -85,17 +87,23 @@ public class VimExtensionFacade {
*/
@NotNull
public static KeyStroke getKeyStroke(@NotNull Editor editor) {
final Ref<KeyStroke> ref = Ref.create();
final ModalEntryDialog dialog = new ModalEntryDialog(editor, "");
dialog.setEntryKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
ref.set(KeyStroke.getKeyStrokeForEvent(e));
dialog.dispose();
}
});
dialog.setVisible(true);
final KeyStroke key = ref.get();
final KeyStroke key;
if (ApplicationManager.getApplication().isUnitTestMode()) {
key = TestInputModel.getInstance(editor).nextKeyStroke();
}
else {
final Ref<KeyStroke> ref = Ref.create();
final ModalEntryDialog dialog = new ModalEntryDialog(editor, "");
dialog.setEntryKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
ref.set(KeyStroke.getKeyStrokeForEvent(e));
dialog.dispose();
}
});
dialog.setVisible(true);
key = ref.get();
}
return key != null ? key : KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
}
}

View File

@ -267,6 +267,7 @@ public class EditorData {
public static final Key<Boolean> LINE_NUMBERS_SHOWN = new Key<Boolean>("lineNumbersShown");
private static final Key<ExOutputPanel> MORE_PANEL = new Key<ExOutputPanel>("IdeaVim.morePanel");
private static final Key<ExOutputModel> EX_OUTPUT_MODEL = new Key<ExOutputModel>("IdeaVim.exOutputModel");
private static final Key<TestInputModel> TEST_INPUT_MODEL = new Key<TestInputModel>("IdeaVim.testInputModel");
private static Key CONSOLE_VIEW_IN_EDITOR_VIEW = Key.create("CONSOLE_VIEW_IN_EDITOR_VIEW");
@ -309,4 +310,13 @@ public class EditorData {
final VirtualFile virtualFile = EditorData.getVirtualFile(editor);
return virtualFile != null && !(virtualFile instanceof LightVirtualFile);
}
@Nullable
public static TestInputModel getTestInputModel(@NotNull Editor editor) {
return editor.getUserData(TEST_INPUT_MODEL);
}
public static void setTestInputModel(@NotNull Editor editor, @NotNull TestInputModel model) {
editor.putUserData(TEST_INPUT_MODEL, model);
}
}

View File

@ -0,0 +1,58 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2016 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.helper;
import com.google.common.collect.Lists;
import com.intellij.openapi.editor.Editor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.List;
/**
* @author vlan
*/
public class TestInputModel {
@NotNull private final List<KeyStroke> myKeyStrokes = Lists.newArrayList();
private TestInputModel() {}
public static TestInputModel getInstance(@NotNull Editor editor) {
TestInputModel model = EditorData.getTestInputModel(editor);
if (model == null) {
model = new TestInputModel();
EditorData.setTestInputModel(editor, model);
}
return model;
}
public void setKeyStrokes(@NotNull List<KeyStroke> keyStrokes) {
myKeyStrokes.clear();
myKeyStrokes.addAll(keyStrokes);
}
@Nullable
public KeyStroke nextKeyStroke() {
if (!myKeyStrokes.isEmpty()) {
return myKeyStrokes.remove(0);
}
return null;
}
}

View File

@ -19,10 +19,9 @@ import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.CommandState;
import com.maddyhome.idea.vim.ex.ExOutputModel;
import com.maddyhome.idea.vim.ex.vimscript.VimScriptGlobalEnvironment;
import com.maddyhome.idea.vim.helper.EditorDataContext;
import com.maddyhome.idea.vim.helper.RunnableHelper;
import com.maddyhome.idea.vim.helper.StringHelper;
import com.maddyhome.idea.vim.helper.*;
import com.maddyhome.idea.vim.option.Options;
import com.maddyhome.idea.vim.option.ToggleOption;
import com.maddyhome.idea.vim.ui.ExEntryPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -66,6 +65,13 @@ public abstract class VimTestCase extends UsefulTestCase {
super.tearDown();
}
protected void enableExtensions(@NotNull String... extensionNames) {
for (String name : extensionNames) {
ToggleOption option = (ToggleOption)Options.getInstance().getOption(name);
option.set();
}
}
@NotNull
protected Editor typeTextInFile(@NotNull final List<KeyStroke> keys, @NotNull String fileContents) {
configureByText(fileContents);
@ -91,15 +97,17 @@ public abstract class VimTestCase extends UsefulTestCase {
}
@NotNull
protected Editor typeText(@NotNull final List<KeyStroke> keys) {
protected Editor typeText(@NotNull List<KeyStroke> keys) {
final Editor editor = myFixture.getEditor();
final KeyHandler keyHandler = KeyHandler.getInstance();
final EditorDataContext dataContext = new EditorDataContext(editor);
final Project project = myFixture.getProject();
TestInputModel.getInstance(editor).setKeyStrokes(keys);
RunnableHelper.runWriteCommand(project, new Runnable() {
@Override
public void run() {
for (KeyStroke key : keys) {
final TestInputModel inputModel = TestInputModel.getInstance(editor);
for (KeyStroke key = inputModel.nextKeyStroke(); key != null; key = inputModel.nextKeyStroke()) {
final ExEntryPanel exEntryPanel = ExEntryPanel.getInstance();
if (exEntryPanel.isActive()) {
exEntryPanel.handleKey(key);
@ -151,19 +159,8 @@ public abstract class VimTestCase extends UsefulTestCase {
}
public void doTest(final List<KeyStroke> keys, String before, String after) {
myFixture.configureByText(PlainTextFileType.INSTANCE, before);
final Editor editor = myFixture.getEditor();
final KeyHandler keyHandler = KeyHandler.getInstance();
final EditorDataContext dataContext = new EditorDataContext(editor);
final Project project = myFixture.getProject();
RunnableHelper.runWriteCommand(project, new Runnable() {
@Override
public void run() {
for (KeyStroke key : keys) {
keyHandler.handleKey(editor, key, dataContext);
}
}
}, null, null);
configureByText(before);
typeText(keys);
myFixture.checkResult(after);
}
}