Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize VSCode commands in .vimrc #4286

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Configuration implements IConfiguration {
}

if (this.vimrc.enable) {
vimrc.load(this);
await vimrc.load(this);
}

this.leader = Notation.NormalizeKey(this.leader, this.leaderDefault);
Expand Down
4 changes: 2 additions & 2 deletions src/configuration/vimrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VimrcImpl {
return this._vimrcPath;
}

public load(config: IConfiguration) {
public async load(config: IConfiguration) {
const _path = config.vimrc.path
? VimrcImpl.expandHome(config.vimrc.path)
: VimrcImpl.findDefaultVimrc();
Expand All @@ -26,7 +26,7 @@ class VimrcImpl {
// Add the new remappings
const lines = fs.readFileSync(config.vimrc.path, { encoding: 'utf8' }).split(/\r?\n/);
for (const line of lines) {
const remap = vimrcKeyRemappingBuilder.build(line);
const remap = await vimrcKeyRemappingBuilder.build(line);
if (remap) {
VimrcImpl.addRemapToConfig(config, remap);
}
Expand Down
8 changes: 5 additions & 3 deletions src/configuration/vimrcKeyRemappingBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as vscode from 'vscode';
import { IKeyRemapping, IVimrcKeyRemapping } from './iconfiguration';

class VimrcKeyRemappingBuilderImpl {
Expand All @@ -8,7 +9,7 @@ class VimrcKeyRemappingBuilderImpl {
/**
* @returns A remapping if the given `line` parses to one, and `undefined` otherwise.
*/
public build(line: string): IVimrcKeyRemapping | undefined {
public async build(line: string): Promise<IVimrcKeyRemapping | undefined> {
const matches = VimrcKeyRemappingBuilderImpl.KEY_REMAPPING_REG_EX.exec(line);
if (!matches || matches.length < 4) {
return undefined;
Expand All @@ -19,7 +20,8 @@ class VimrcKeyRemappingBuilderImpl {
const after = matches[3];

let mapping: IKeyRemapping;
if (VimrcKeyRemappingBuilderImpl.isCommand(after)) {
const vscodeCommands = await vscode.commands.getCommands();
if (vscodeCommands.includes(after) || VimrcKeyRemappingBuilderImpl.isVimCommand(after)) {
mapping = {
before: VimrcKeyRemappingBuilderImpl.buildKeyList(before),
commands: [after],
Expand All @@ -42,7 +44,7 @@ class VimrcKeyRemappingBuilderImpl {
/**
* @returns `true` if this remaps a key sequence to a `:` command
*/
private static isCommand(commandString: string): boolean {
private static isVimCommand(commandString: string): boolean {
const matches = VimrcKeyRemappingBuilderImpl.COMMAND_REG_EX.exec(commandString);
if (matches) {
return true;
Expand Down
19 changes: 14 additions & 5 deletions test/configuration/vimrcKeyRemappingBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IKeyRemapping, IVimrcKeyRemapping } from '../../src/configuration/iconf
import { vimrcKeyRemappingBuilder } from '../../src/configuration/vimrcKeyRemappingBuilder';

suite('VimrcKeyRemappingBuilder', () => {
test('Build IKeyRemapping objects from .vimrc lines', () => {
test('Build IKeyRemapping objects from .vimrc lines', async () => {
const testCases = [
{
vimrcLine: 'nnoremap <C-h> <<',
Expand Down Expand Up @@ -36,7 +36,7 @@ suite('VimrcKeyRemappingBuilder', () => {
expectNull: false,
},
{
// Mapping with a command
// Mapping with a vim command
vimrcLine: 'nnoremap <C-s> :w',
keyRemapping: {
before: ['<C-s>'],
Expand All @@ -46,6 +46,17 @@ suite('VimrcKeyRemappingBuilder', () => {
keyRemappingType: 'nnoremap',
expectNull: false,
},
{
// Mapping with a VSCode command
vimrcLine: 'nnoremap <C-t> workbench.action.files.newUntitledFile',
keyRemapping: {
before: ['<C-t>'],
commands: ['workbench.action.files.newUntitledFile'],
source: 'vimrc',
},
keyRemappingType: 'nnoremap',
expectNull: false,
},
{
// Ignore non-mapping lines
vimrcLine: 'set scrolloff=8',
Expand All @@ -54,9 +65,7 @@ suite('VimrcKeyRemappingBuilder', () => {
];

for (const testCase of testCases) {
const vimrcKeyRemapping: IVimrcKeyRemapping | undefined = vimrcKeyRemappingBuilder.build(
testCase.vimrcLine
);
const vimrcKeyRemapping = await vimrcKeyRemappingBuilder.build(testCase.vimrcLine);

if (testCase.expectNull) {
assert.strictEqual(vimrcKeyRemapping, undefined);
Expand Down