Skip to content

Commit 1bd90e1

Browse files
committed
Fix a few bugs with :bd[elete]
- Implement `:bd {bufname}` - Fix with high index - Fix error handling
1 parent 9596b49 commit 1bd90e1

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

src/cmd_line/commands/bufferDelete.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { VimState } from '../../state/vimState';
66
import { StatusBar } from '../../statusBar';
77
import { ExCommand } from '../../vimscript/exCommand';
88
import { bangParser, fileNameParser, numberParser } from '../../vimscript/parserUtils';
9+
import { find } from 'lodash';
10+
import { findTabInActiveTabGroup } from '../../util/util';
911

1012
interface IBufferDeleteCommandArguments {
1113
bang: boolean;
@@ -33,28 +35,37 @@ export class BufferDeleteCommand extends ExCommand {
3335
throw VimError.NoWriteSinceLastChange();
3436
}
3537

38+
let deletedBuffers = 0;
39+
3640
if (this.arguments.buffers.length === 0) {
3741
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
3842
} else {
39-
for (const buffer of this.arguments.buffers) {
43+
for (let buffer of this.arguments.buffers) {
4044
if (typeof buffer === 'string') {
41-
// TODO
42-
StatusBar.setText(
43-
vimState,
44-
':bd[elete][!] {bufname} is not yet implemented (PRs are welcome!)',
45-
true,
46-
);
45+
const [idx, tab] = findTabInActiveTabGroup(buffer);
46+
buffer = idx + 1;
47+
}
48+
49+
if (buffer < 1) {
50+
throw VimError.PositiveCountRequired();
51+
}
52+
if (buffer > vscode.window.tabGroups.activeTabGroup.tabs.length) {
4753
continue;
4854
}
4955

5056
try {
51-
await vscode.commands.executeCommand(`workbench.action.openEditorAtIndex${buffer}`);
57+
await vscode.commands.executeCommand('workbench.action.openEditorAtIndex', buffer - 1);
5258
} catch (e) {
53-
throw VimError.NoBuffersDeleted();
59+
continue;
5460
}
5561

5662
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
63+
++deletedBuffers;
5764
}
5865
}
66+
67+
if (deletedBuffers === 0) {
68+
throw VimError.NoBuffersDeleted();
69+
}
5970
}
6071
}

src/cmd_line/commands/tab.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
numberParser,
1515
} from '../../vimscript/parserUtils';
1616
import { VimError } from '../../error';
17+
import { findTabInActiveTabGroup } from '../../util/util';
1718

1819
export enum TabCommandType {
1920
Next,
@@ -171,23 +172,7 @@ export class TabCommand extends ExCommand {
171172
this.arguments.buf - 1,
172173
);
173174
} else if (this.arguments.buf !== undefined && typeof this.arguments.buf === 'string') {
174-
const tabGroup = vscode.window.tabGroups.activeTabGroup;
175-
const searchTerm = this.arguments.buf;
176-
const foundBuffers = [];
177-
for (const t of tabGroup.tabs) {
178-
if (t.label.includes(searchTerm)) {
179-
foundBuffers.push(t);
180-
}
181-
if (foundBuffers.length > 1) {
182-
break;
183-
}
184-
}
185-
if (foundBuffers.length === 0) {
186-
throw VimError.NoMatchingBuffer(searchTerm);
187-
} else if (foundBuffers.length > 1) {
188-
throw VimError.MultipleMatches(searchTerm);
189-
}
190-
const tab = foundBuffers[0];
175+
const [idx, tab] = findTabInActiveTabGroup(this.arguments.buf);
191176
if ((tab.input as vscode.TextDocument).uri !== undefined) {
192177
const uri = (tab.input as vscode.TextDocument).uri;
193178
await vscode.commands.executeCommand('vscode.open', uri);

src/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export enum ErrorCode {
8585
MaxDepthMustBeANonNegativeNumber = 900,
8686
ExpectedADict = 922,
8787
SecondArgumentOfFunction = 923,
88+
PositiveCountRequired = 939,
8889
BlobLiteralShouldHaveAnEvenNumberOfHexCharacters = 973,
8990
UsingABlobAsANumber = 974,
9091
CanOnlyCompareBlobWithBlob = 977,
@@ -423,6 +424,9 @@ export class VimError extends Error {
423424
'Second argument of function() must be a list or a dict',
424425
);
425426
}
427+
static PositiveCountRequired(): VimError {
428+
return new VimError(ErrorCode.PositiveCountRequired, 'Positive count required');
429+
}
426430
static BlobLiteralShouldHaveAnEvenNumberOfHexCharacters(): VimError {
427431
return new VimError(
428432
ErrorCode.BlobLiteralShouldHaveAnEvenNumberOfHexCharacters,

src/util/util.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import { Cursor } from '../common/motion/cursor';
33
import { VimState } from '../state/vimState';
4+
import { VimError } from '../error';
45

56
/**
67
* We used to have an issue where we would do something like execute a VSCode
@@ -46,3 +47,21 @@ export function isHighSurrogate(charCode: number): boolean {
4647
export function isLowSurrogate(charCode: number): boolean {
4748
return 0xdc00 <= charCode && charCode <= 0xdfff;
4849
}
50+
51+
export function findTabInActiveTabGroup(name: string): [number, vscode.Tab] {
52+
const foundBuffers: Array<[number, vscode.Tab]> = [];
53+
const tabs = vscode.window.tabGroups.activeTabGroup.tabs;
54+
for (let t = 0; t < tabs.length; t++) {
55+
const tab = tabs[t];
56+
if (tab.label.includes(name)) {
57+
foundBuffers.push([t, tab]);
58+
if (foundBuffers.length > 1) {
59+
throw VimError.MultipleMatches(name);
60+
}
61+
}
62+
}
63+
if (foundBuffers.length === 0) {
64+
throw VimError.NoMatchingBuffer(name);
65+
}
66+
return foundBuffers[0];
67+
}

0 commit comments

Comments
 (0)