Skip to content

Commit

Permalink
vim-1062
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalPlacek authored and AlexPl292 committed Jul 1, 2021
1 parent af8edae commit ddb159e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
5 changes: 3 additions & 2 deletions resources/META-INF/includes/VimExCommands.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.MarkHandler" names="ma[rk],k"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.MarksHandler" names="marks"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.MoveTextHandler" names="m[ove]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.NextFileHandler" names="n[ext]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.NextFileHandler" names="n[ext],bn"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.NoHLSearchHandler" names="noh[lsearch]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.OnlyHandler" names="on[ly]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PreviousFileHandler" names="N[ext],prev[ious]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PreviousFileHandler" names="N[ext],prev[ious],bp"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PromptFindHandler" names="pro[mptfind]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PrintHandler" names="p[rint],P[rint]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PromptReplaceHandler" names="promptr[epl]"/>
Expand Down Expand Up @@ -71,5 +71,6 @@
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.BufferListHandler" names="buffers,ls,files"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.BufferHandler" names="b[uffer]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.PlugHandler" names="Plug[in]"/>
<vimExCommand implementation="com.maddyhome.idea.vim.ex.handler.BufferCloseHandler" names="bd"/>
</extensions>
</idea-plugin>
40 changes: 40 additions & 0 deletions src/com/maddyhome/idea/vim/ex/handler/BufferCloseHandler.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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()
if (arg.isNotEmpty() && arg.matches(Regex("^\\d+$"))) {
val bufNum = arg.toInt() - 1
VimPlugin.getFile().closeFile(bufNum, editor, context)
} else {
VimPlugin.getFile().closeFile(editor, context)
}
return true
}
}
29 changes: 23 additions & 6 deletions src/com/maddyhome/idea/vim/group/FileGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -161,6 +160,24 @@ public void closeFile(@NotNull Editor editor, @NotNull DataContext context) {
}
}

/**
* Closes editor.
*/
public void closeFile(int number, @NotNull Editor editor, @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 == 99) {
number = editors.length - 1;
}
if (number >= 0 && number < editors.length) {
fileEditorManager.closeFile(editors[number], window);
}
if (number < 0 || number >= editors.length) {
}
}

/**
* Saves specific file in the project.
*/
Expand Down Expand Up @@ -250,13 +267,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;
}
Expand Down Expand Up @@ -314,7 +330,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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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)
}
}

0 comments on commit ddb159e

Please sign in to comment.