Skip to content

Commit

Permalink
Merge pull request #368 from rebornix/Tabs
Browse files Browse the repository at this point in the history
Tabs
  • Loading branch information
johnfn authored Jun 28, 2016
2 parents 2f9d66a + 9e9c212 commit c7ba280
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,19 @@ Status | Command | Description
---|--------|------------------------------
| :r [file] | insert the contents of [file] below the cursor
| :r! {command} | insert the standard output of {command} below the cursor


## Tabs

Status | Command | Description
---|--------|------------------------------
:warning: | :1234: :tabe[dit] | Open a new tab page with an empty window, after the current tab page
:warning: | :1234: :tabnew | Open a new tab page with an empty window, after the current tab page
:warning: | :tabc[lose][!] :1234: | Close current tab page.
:warning: | :tabo[nly][!] | Close all other tab pages.
:white_check_mark: | :tabn[ext] :1234: | Go to tab page {count}. The first tab page has number one.
:white_check_mark: | :tabp[revious] :1234: | Go to the previous tab page. Wraps around from the first one to the last one.
:white_check_mark: | :tabfir[st] | Go to the first tab page.
:white_check_mark: | :tabl[ast] | Go to the last tab page.
:x: | :tabls | List the tab pages and the windows they contain.
:x: | :tabm[ove] [N] | Move the current tab page to after tab page N.
77 changes: 77 additions & 0 deletions src/cmd_line/commands/tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"use strict";

import * as vscode from "vscode";
import * as node from "../node";

export enum Tab {
Next,
Previous,
First,
Last,
New,
Close,
Only
}

export interface ITabCommandArguments extends node.ICommandArgs {
tab: Tab;
count?: number;
}

//
// Implements tab
// http://vimdoc.sourceforge.net/htmldoc/tabpage.html
//
export class TabCommand extends node.CommandBase {
protected _arguments : ITabCommandArguments;

constructor(args : ITabCommandArguments) {
super();
this._name = 'tab';
this._shortName = 'tab';
this._arguments = args;
}

get arguments() : ITabCommandArguments {
return this._arguments;
}

private executeCommandWithCount(count: number, command: string) {
if (!count) {
count = 1;
}

for (let i = 0; i < count; i++) {
vscode.commands.executeCommand(command);
}
}

execute() : void {
switch (this._arguments.tab) {
case Tab.Next:
this.executeCommandWithCount(this._arguments.count, "workbench.action.nextEditor");
break;
case Tab.Previous:
this.executeCommandWithCount(this._arguments.count, "workbench.action.previousEditor");
break;
case Tab.First:
this.executeCommandWithCount(this._arguments.count, "workbench.action.openEditorAtIndex1");
break;
case Tab.Last:
this.executeCommandWithCount(this._arguments.count, "workbench.action.openLastEditorInGroup");
break;
case Tab.New:
this.executeCommandWithCount(this._arguments.count, "workbench.action.files.newUntitledFile");
break;
case Tab.Close:
this.executeCommandWithCount(this._arguments.count, "workbench.action.closeActiveEditor");
break;
case Tab.Only:
this.executeCommandWithCount(this._arguments.count, "workbench.action.closeOtherEditors");
break;

default:
break;
}
}
}
25 changes: 24 additions & 1 deletion src/cmd_line/subparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

import {parseQuitCommandArgs} from './subparsers/quit';
import {parseWriteCommandArgs} from './subparsers/write';
import * as tabCmd from './subparsers/tab';

// maps command names to parsers for said commands.
export const commandParsers = {
w: parseWriteCommandArgs,
write: parseWriteCommandArgs,

quit: parseQuitCommandArgs,
q: parseQuitCommandArgs
q: parseQuitCommandArgs,

tabn: tabCmd.parseTabNCommandArgs,
tabnext: tabCmd.parseTabNCommandArgs,

tabp: tabCmd.parseTabPCommandArgs,
tabprevious: tabCmd.parseTabPCommandArgs,

tabfirst: tabCmd.parseTabFirstCommandArgs,
tabfir: tabCmd.parseTabFirstCommandArgs,

tablast: tabCmd.parseTabLastCommandArgs,
tabl: tabCmd.parseTabLastCommandArgs,

tabe: tabCmd.parseTabNewCommandArgs,
tabedit: tabCmd.parseTabNewCommandArgs,
tabnew: tabCmd.parseTabNewCommandArgs,

tabclose: tabCmd.parseTabCloseCommandArgs,
tabc: tabCmd.parseTabCloseCommandArgs,

tabo: tabCmd.parseTabOnlyCommandArgs,
tabonly: tabCmd.parseTabOnlyCommandArgs
};
78 changes: 78 additions & 0 deletions src/cmd_line/subparsers/tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";

import * as node from "../commands/tab";
import {Scanner} from '../scanner';

function parseCount(args: string): number {
if (!args) {
return 1;
}

let scanner = new Scanner(args);
scanner.skipWhiteSpace();

if (scanner.isAtEof) {
return 1;
}

let c = scanner.next();
let count = Number.parseInt(c);

if (Number.isInteger(count) && count > 0 ) {
if (count > 999) {
count = 999;
}

return count;
} else {
throw new Error(`Invalid tab number: ${c}!`);
}
}

export function parseTabNCommandArgs(args : string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.Next,
count: parseCount(args)
});
}

export function parseTabPCommandArgs(args : string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.Previous,
count: parseCount(args)
});
}

export function parseTabFirstCommandArgs(args : string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.First
});
}

export function parseTabLastCommandArgs(args : string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.Last
});
}

export function parseTabNewCommandArgs(args: string) : node.TabCommand {
// New Tab command should support `count`
// And the new created Tab's position depends on `count`
// For now VS Code only allows open tab next to current Tab
// So `count == 0` is not possible. But we can workaround this once we can move tabs through API.
return new node.TabCommand({
tab: node.Tab.New
});
}

export function parseTabCloseCommandArgs(args: string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.Close
});
}

export function parseTabOnlyCommandArgs(args: string) : node.TabCommand {
return new node.TabCommand({
tab: node.Tab.Only
});
}

0 comments on commit c7ba280

Please sign in to comment.