diff --git a/resources/META-INF/includes/VimExCommands.xml b/resources/META-INF/includes/VimExCommands.xml index d152e02436..b80183c51c 100644 --- a/resources/META-INF/includes/VimExCommands.xml +++ b/resources/META-INF/includes/VimExCommands.xml @@ -32,10 +32,10 @@ - + - + @@ -71,5 +71,6 @@ + diff --git a/src/com/maddyhome/idea/vim/ex/handler/BufferCloseHandler.kt b/src/com/maddyhome/idea/vim/ex/handler/BufferCloseHandler.kt new file mode 100644 index 0000000000..c12e61cc67 --- /dev/null +++ b/src/com/maddyhome/idea/vim/ex/handler/BufferCloseHandler.kt @@ -0,0 +1,40 @@ +/* + * IdeaVim - Vim emulator for IDEs based on the IntelliJ platform + * Copyright (C) 2003-2021 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 . + */ + +package com.maddyhome.idea.vim.ex.handler + +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.editor.Editor +import com.maddyhome.idea.vim.VimPlugin +import com.maddyhome.idea.vim.ex.CommandHandler +import com.maddyhome.idea.vim.ex.ExCommand +import com.maddyhome.idea.vim.ex.flags + +class BufferCloseHandler : CommandHandler.SingleExecution() { + override val argFlags = flags(RangeFlag.RANGE_OPTIONAL, ArgumentFlag.ARGUMENT_OPTIONAL, Access.READ_ONLY) + override fun execute(editor: Editor, context: DataContext, cmd: ExCommand): Boolean { + val arg = cmd.argument.trim() + val bufNum = arg.toIntOrNull(); + if (bufNum != null) { + VimPlugin.getFile().closeFile(bufNum - 1, context) + } else { + VimPlugin.getFile().closeFile(editor, context) + } + return true + } +} diff --git a/src/com/maddyhome/idea/vim/group/FileGroup.java b/src/com/maddyhome/idea/vim/group/FileGroup.java index 69df0a48f5..1ce0409a16 100644 --- a/src/com/maddyhome/idea/vim/group/FileGroup.java +++ b/src/com/maddyhome/idea/vim/group/FileGroup.java @@ -92,8 +92,7 @@ public boolean openFile(@NotNull String filename, @NotNull DataContext context) } } - @Nullable - VirtualFile findFile(@NotNull String filename, @NotNull Project project) { + @Nullable VirtualFile findFile(@NotNull String filename, @NotNull Project project) { VirtualFile found = null; if (filename.length() > 2 && filename.charAt(0) == '~' && filename.charAt(1) == File.separatorChar) { String homefile = filename.substring(2); @@ -161,6 +160,19 @@ public void closeFile(@NotNull Editor editor, @NotNull DataContext context) { } } + /** + * Closes editor. + */ + public void closeFile(int number, @NotNull DataContext context) { + final Project project = PlatformDataKeys.PROJECT.getData(context); + final FileEditorManagerEx fileEditorManager = FileEditorManagerEx.getInstanceEx(project); + final EditorWindow window = fileEditorManager.getCurrentWindow(); + VirtualFile[] editors = fileEditorManager.getOpenFiles(); + if (number >= 0 && number < editors.length) { + fileEditorManager.closeFile(editors[number], window); + } + } + /** * Saves specific file in the project. */ @@ -250,13 +262,12 @@ public void selectPreviousTab(@NotNull DataContext context) { return null; } - @Nullable - Editor selectEditor(Project project, @NotNull VirtualFile file) { + @Nullable Editor selectEditor(Project project, @NotNull VirtualFile file) { FileEditorManager fMgr = FileEditorManager.getInstance(project); FileEditor[] feditors = fMgr.openFile(file, true); if (feditors.length > 0) { if (feditors[0] instanceof TextEditor) { - Editor editor = ((TextEditor) feditors[0]).getEditor(); + Editor editor = ((TextEditor)feditors[0]).getEditor(); if (!editor.isDisposed()) { return editor; } @@ -314,7 +325,8 @@ public void displayLocationInfo(@NotNull Editor editor) { else { msg.append("Selected "); - TextRange vr = new TextRange(editor.getSelectionModel().getBlockSelectionStarts(), editor.getSelectionModel().getBlockSelectionEnds()); + TextRange vr = new TextRange(editor.getSelectionModel().getBlockSelectionStarts(), + editor.getSelectionModel().getBlockSelectionEnds()); vr.normalize(); int lines; diff --git a/test/org/jetbrains/plugins/ideavim/ex/handler/BufferCloseHandlerTest.kt b/test/org/jetbrains/plugins/ideavim/ex/handler/BufferCloseHandlerTest.kt new file mode 100644 index 0000000000..c1c71da97e --- /dev/null +++ b/test/org/jetbrains/plugins/ideavim/ex/handler/BufferCloseHandlerTest.kt @@ -0,0 +1,40 @@ +/* + * IdeaVim - Vim emulator for IDEs based on the IntelliJ platform + * Copyright (C) 2003-2021 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 . + */ + +package org.jetbrains.plugins.ideavim.ex.handler + +import org.jetbrains.plugins.ideavim.VimTestCase + +/** + * @author Michal Placek + */ +class BufferCloseHandlerTest : VimTestCase() { + fun `test close file by bd command`() { + + val psiFile1 = myFixture.configureByText("A_Discovery1", "I found it in a legendary land") + val psiFile2 = myFixture.configureByText("A_Discovery2", "all rocks and lavender and tufted grass,") + + fileManager.openFile(psiFile1.virtualFile, false) + fileManager.openFile(psiFile2.virtualFile, true) + assertPluginError(false) + + typeText(commandToKeys("bd")) + + assertPluginError(false) + } +}