1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-07-31 03:59:07 +02:00

Fix small code smells

This commit is contained in:
Alex Plate 2019-07-05 12:22:25 +03:00
parent fecd2d7111
commit 91de1004e6
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
7 changed files with 140 additions and 175 deletions

View File

@ -282,12 +282,8 @@ public class KeyHandler {
RegisterGroup register = VimPlugin.getRegister();
if (register.getCurrentRegister() == register.getDefaultRegister()) {
if (key.getKeyCode() == KeyEvent.VK_ESCAPE) {
CommandProcessor.getInstance().executeCommand(editor.getProject(), new Runnable() {
@Override
public void run() {
KeyHandler.executeAction("EditorEscape", context);
}
}, "", null);
CommandProcessor.getInstance().executeCommand(editor.getProject(),
() -> KeyHandler.executeAction("EditorEscape", context), "", null);
}
VimPlugin.indicateError();
}
@ -334,46 +330,43 @@ public class KeyHandler {
}
else if (mappingInfo != null) {
mappingKeys.clear();
final Runnable handleMappedKeys = new Runnable() {
@Override
public void run() {
if (editor.isDisposed()) {
return;
}
final List<KeyStroke> toKeys = mappingInfo.getToKeys();
final VimExtensionHandler extensionHandler = mappingInfo.getExtensionHandler();
final EditorDataContext currentContext = new EditorDataContext(editor);
if (toKeys != null) {
final boolean fromIsPrefix = isPrefix(mappingInfo.getFromKeys(), toKeys);
boolean first = true;
for (KeyStroke keyStroke : toKeys) {
final boolean recursive = mappingInfo.isRecursive() && !(first && fromIsPrefix);
handleKey(editor, keyStroke, currentContext, recursive);
first = false;
}
}
else if (extensionHandler != null) {
final CommandProcessor processor = CommandProcessor.getInstance();
processor.executeCommand(editor.getProject(), () -> extensionHandler.execute(editor, context),
"Vim " + extensionHandler.getClass().getSimpleName(), null);
final Runnable handleMappedKeys = () -> {
if (editor.isDisposed()) {
return;
}
final List<KeyStroke> toKeys = mappingInfo.getToKeys();
final VimExtensionHandler extensionHandler = mappingInfo.getExtensionHandler();
final EditorDataContext currentContext = new EditorDataContext(editor);
if (toKeys != null) {
final boolean fromIsPrefix = isPrefix(mappingInfo.getFromKeys(), toKeys);
boolean first = true;
for (KeyStroke keyStroke : toKeys) {
final boolean recursive = mappingInfo.isRecursive() && !(first && fromIsPrefix);
handleKey(editor, keyStroke, currentContext, recursive);
first = false;
}
}
else if (extensionHandler != null) {
final CommandProcessor processor = CommandProcessor.getInstance();
processor.executeCommand(editor.getProject(), () -> extensionHandler.execute(editor, context),
"Vim " + extensionHandler.getClass().getSimpleName(), null);
}
// NB: mappingInfo MUST be non-null here, so if equal
// then prevMappingInfo is also non-null; this also
// means that the prev mapping was a prefix, but the
// next key typed (`key`) was not part of that
if (prevMappingInfo == mappingInfo) {
// post to end of queue so it's handled AFTER
// an <Plug> mapping is invoked (since that
// will also get posted)
Runnable handleRemainingKey = () -> handleKey(editor, key, currentContext);
// NB: mappingInfo MUST be non-null here, so if equal
// then prevMappingInfo is also non-null; this also
// means that the prev mapping was a prefix, but the
// next key typed (`key`) was not part of that
if (prevMappingInfo == mappingInfo) {
// post to end of queue so it's handled AFTER
// an <Plug> mapping is invoked (since that
// will also get posted)
Runnable handleRemainingKey = () -> handleKey(editor, key, currentContext);
if (application.isUnitTestMode()) {
handleRemainingKey.run();
}
else {
application.invokeLater(handleRemainingKey);
}
if (application.isUnitTestMode()) {
handleRemainingKey.run();
}
else {
application.invokeLater(handleRemainingKey);
}
}
};

View File

@ -37,7 +37,7 @@ class VimLocalConfig : PersistentStateComponent<Element> {
val element = Element("ideavim-local")
val state = Element("state")
state.setAttribute("version", Integer.toString(STATE_VERSION))
state.setAttribute("version", STATE_VERSION.toString())
element.addContent(state)
VimPlugin.getMark().saveData(element)

View File

@ -201,31 +201,52 @@ public class VimPlugin implements BaseComponent, PersistentStateComponent<Elemen
return element;
}
@Override
public void loadState(@NotNull final Element element) {
LOG.debug("Loading state");
// Restore whether the plugin is enabled or not
Element state = element.getChild("state");
if (state != null) {
try {
previousStateVersion = Integer.valueOf(state.getAttributeValue("version"));
}
catch (NumberFormatException ignored) {
}
enabled = Boolean.valueOf(state.getAttributeValue("enabled"));
previousKeyMap = state.getAttributeValue("keymap");
/**
* Reports statistics about installed IdeaVim and enabled Vim emulation.
* <p>
* See https://github.com/go-lang-plugin-org/go-lang-idea-plugin/commit/5182ab4a1d01ad37f6786268a2fe5e908575a217
*/
public static void statisticReport() {
final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
final long lastUpdate = propertiesComponent.getOrInitLong(IDEAVIM_STATISTICS_TIMESTAMP_KEY, 0);
final boolean outOfDate =
lastUpdate == 0 || System.currentTimeMillis() - lastUpdate > TimeUnit.DAYS.toMillis(1);
if (outOfDate && isEnabled()) {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
try {
final String buildNumber = ApplicationInfo.getInstance().getBuild().asString();
final String version = URLEncoder.encode(getVersion(), CharsetToolkit.UTF8);
final String os =
URLEncoder.encode(SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION, CharsetToolkit.UTF8);
final String uid = PermanentInstallationID.get();
final String url = "https://plugins.jetbrains.com/plugins/list" +
"?pluginId=" + IDEAVIM_PLUGIN_ID +
"&build=" +
buildNumber +
"&pluginVersion=" +
version +
"&os=" +
os +
"&uuid=" +
uid;
PropertiesComponent.getInstance()
.setValue(IDEAVIM_STATISTICS_TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
HttpRequests.request(url).connect(request -> {
LOG.info("Sending statistics: " + url);
try {
JDOMUtil.load(request.getInputStream());
}
catch (JDOMException e) {
LOG.warn(e);
}
return null;
});
}
catch (IOException e) {
LOG.warn(e);
}
});
}
if (previousStateVersion > 0 && previousStateVersion < 5) {
// Migrate settings from 4 to 5 version
mark.readData(element);
register.readData(element);
search.readData(element);
history.readData(element);
}
key.readData(element);
editor.readData(element);
}
@NotNull
@ -467,59 +488,30 @@ public class VimPlugin implements BaseComponent, PersistentStateComponent<Elemen
}
}
/**
* Reports statistics about installed IdeaVim and enabled Vim emulation.
* <p>
* See https://github.com/go-lang-plugin-org/go-lang-idea-plugin/commit/5182ab4a1d01ad37f6786268a2fe5e908575a217
*/
public static void statisticReport() {
final PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
final long lastUpdate = propertiesComponent.getOrInitLong(IDEAVIM_STATISTICS_TIMESTAMP_KEY, 0);
final boolean outOfDate =
lastUpdate == 0 || System.currentTimeMillis() - lastUpdate > TimeUnit.DAYS.toMillis(1);
if (outOfDate && isEnabled()) {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
try {
final String buildNumber = ApplicationInfo.getInstance().getBuild().asString();
final String pluginId = IDEAVIM_PLUGIN_ID;
final String version = URLEncoder.encode(getVersion(), CharsetToolkit.UTF8);
final String os =
URLEncoder.encode(SystemInfo.OS_NAME + " " + SystemInfo.OS_VERSION, CharsetToolkit.UTF8);
final String uid = PermanentInstallationID.get();
final String url = "https://plugins.jetbrains.com/plugins/list" +
"?pluginId=" +
pluginId +
"&build=" +
buildNumber +
"&pluginVersion=" +
version +
"&os=" +
os +
"&uuid=" +
uid;
PropertiesComponent.getInstance()
.setValue(IDEAVIM_STATISTICS_TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
HttpRequests.request(url).connect(new HttpRequests.RequestProcessor<Object>() {
@Override
public Object process(@NotNull HttpRequests.Request request) throws IOException {
LOG.info("Sending statistics: " + url);
try {
JDOMUtil.load(request.getInputStream());
}
catch (JDOMException e) {
LOG.warn(e);
}
return null;
}
});
}
catch (IOException e) {
LOG.warn(e);
}
}
});
@Override
public void loadState(@NotNull final Element element) {
LOG.debug("Loading state");
// Restore whether the plugin is enabled or not
Element state = element.getChild("state");
if (state != null) {
try {
previousStateVersion = Integer.parseInt(state.getAttributeValue("version"));
}
catch (NumberFormatException ignored) {
}
enabled = Boolean.parseBoolean(state.getAttributeValue("enabled"));
previousKeyMap = state.getAttributeValue("keymap");
}
if (previousStateVersion > 0 && previousStateVersion < 5) {
// Migrate settings from 4 to 5 version
mark.readData(element);
register.readData(element);
search.readData(element);
history.readData(element);
}
key.readData(element);
editor.readData(element);
}
}

View File

@ -21,7 +21,6 @@ package com.maddyhome.idea.vim.extension;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Ref;
import com.intellij.util.Processor;
import com.maddyhome.idea.vim.KeyHandler;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.MappingMode;
@ -98,12 +97,9 @@ public class VimExtensionFacade {
}
else {
final Ref<KeyStroke> ref = Ref.create();
ModalEntry.activate(new Processor<KeyStroke>() {
@Override
public boolean process(KeyStroke stroke) {
ref.set(stroke);
return false;
}
ModalEntry.activate(stroke -> {
ref.set(stroke);
return false;
});
key = ref.get();
}
@ -133,21 +129,18 @@ public class VimExtensionFacade {
// XXX: The Ex entry panel is used only for UI here, its logic might be inappropriate for input()
final ExEntryPanel exEntryPanel = ExEntryPanel.getInstance();
exEntryPanel.activate(editor, new EditorDataContext(editor), prompt.isEmpty() ? " " : prompt, "", 1);
ModalEntry.activate(new Processor<KeyStroke>() {
@Override
public boolean process(KeyStroke key) {
if (StringHelper.isCloseKeyStroke(key)) {
exEntryPanel.deactivate(true);
return false;
}
else if (key.getKeyCode() == KeyEvent.VK_ENTER) {
text.set(exEntryPanel.getText());
exEntryPanel.deactivate(true);
return false;
} else {
exEntryPanel.handleKey(key);
return true;
}
ModalEntry.activate(key -> {
if (StringHelper.isCloseKeyStroke(key)) {
exEntryPanel.deactivate(true);
return false;
}
else if (key.getKeyCode() == KeyEvent.VK_ENTER) {
text.set(exEntryPanel.getText());
exEntryPanel.deactivate(true);
return false;
} else {
exEntryPanel.handleKey(key);
return true;
}
});
return text.get();
@ -170,6 +163,6 @@ public class VimExtensionFacade {
* Set the current contents of the given register
*/
public static void setRegister(char register, @Nullable List<KeyStroke> keys) {
VimPlugin.getRegister().setKeys(register, keys != null ? keys : Collections.<KeyStroke>emptyList());
VimPlugin.getRegister().setKeys(register, keys != null ? keys : Collections.emptyList());
}
}

View File

@ -32,8 +32,6 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.option.ListOption;
import com.maddyhome.idea.vim.option.OptionChangeEvent;
import com.maddyhome.idea.vim.option.OptionChangeListener;
import com.maddyhome.idea.vim.option.OptionsManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -2156,12 +2154,7 @@ public class SearchHelper {
ListOption lo = OptionsManager.INSTANCE.getMatchpairs();
pairsChars = parseOption(lo);
lo.addOptionChangeListener(new OptionChangeListener() {
@Override
public void valueChange(@NotNull OptionChangeEvent event) {
pairsChars = parseOption((ListOption)event.getOption());
}
});
lo.addOptionChangeListener(event -> pairsChars = parseOption((ListOption)event.getOption()));
}
return pairsChars;

View File

@ -297,19 +297,16 @@ public class ExOutputPanel extends JPanel implements LafManagerListener {
}
private void close(@Nullable final KeyEvent e) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
deactivate(true);
ApplicationManager.getApplication().invokeLater(() -> {
deactivate(true);
final Project project = myEditor.getProject();
final Project project = myEditor.getProject();
if (project != null && e != null && e.getKeyChar() != '\n') {
final KeyStroke key = KeyStroke.getKeyStrokeForEvent(e);
final List<KeyStroke> keys = new ArrayList<>(1);
keys.add(key);
VimPlugin.getMacro().playbackKeys(myEditor, new EditorDataContext(myEditor), project, keys, 0, 0, 1);
}
if (project != null && e != null && e.getKeyChar() != '\n') {
final KeyStroke key = KeyStroke.getKeyStrokeForEvent(e);
final List<KeyStroke> keys = new ArrayList<>(1);
keys.add(key);
VimPlugin.getMacro().playbackKeys(myEditor, new EditorDataContext(myEditor), project, keys, 0, 0, 1);
}
});
}

View File

@ -87,18 +87,15 @@ public abstract class JavaVimTestCase extends JavaCodeInsightFixtureTestCase {
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() {
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);
}
else {
keyHandler.handleKey(editor, key, dataContext);
}
RunnableHelper.runWriteCommand(project, () -> {
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);
}
else {
keyHandler.handleKey(editor, key, dataContext);
}
}
}, null, null);