Make KeySequence class
This commit is contained in:
parent
62a86c5253
commit
b496cea582
5 changed files with 63 additions and 74 deletions
|
@ -1,14 +1,9 @@
|
||||||
import Key from './Key';
|
import Key from './Key';
|
||||||
|
|
||||||
export default class KeySequence {
|
export default class KeySequence {
|
||||||
private keys: Key[];
|
constructor(
|
||||||
|
public readonly keys: Key[],
|
||||||
private constructor(keys: Key[]) {
|
) {
|
||||||
this.keys = keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
static from(keys: Key[]): KeySequence {
|
|
||||||
return new KeySequence(keys);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
push(key: Key): number {
|
push(key: Key): number {
|
||||||
|
@ -31,34 +26,29 @@ export default class KeySequence {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
getKeyArray(): Key[] {
|
static fromMapKeys(keys: string): KeySequence {
|
||||||
return this.keys;
|
const fromMapKeysRecursive = (
|
||||||
|
remaining: string, mappedKeys: Key[],
|
||||||
|
): Key[] => {
|
||||||
|
if (remaining.length === 0) {
|
||||||
|
return mappedKeys;
|
||||||
|
}
|
||||||
|
|
||||||
|
let nextPos = 1;
|
||||||
|
if (remaining.startsWith('<')) {
|
||||||
|
let ltPos = remaining.indexOf('>');
|
||||||
|
if (ltPos > 0) {
|
||||||
|
nextPos = ltPos + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fromMapKeysRecursive(
|
||||||
|
remaining.slice(nextPos),
|
||||||
|
mappedKeys.concat([Key.fromMapKey(remaining.slice(0, nextPos))])
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
let data = fromMapKeysRecursive(keys, []);
|
||||||
|
return new KeySequence(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fromMapKeys = (keys: string): KeySequence => {
|
|
||||||
const fromMapKeysRecursive = (
|
|
||||||
remainings: string, mappedKeys: Key[],
|
|
||||||
): Key[] => {
|
|
||||||
if (remainings.length === 0) {
|
|
||||||
return mappedKeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
let nextPos = 1;
|
|
||||||
if (remainings.startsWith('<')) {
|
|
||||||
let ltPos = remainings.indexOf('>');
|
|
||||||
if (ltPos > 0) {
|
|
||||||
nextPos = ltPos + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fromMapKeysRecursive(
|
|
||||||
remainings.slice(nextPos),
|
|
||||||
mappedKeys.concat([Key.fromMapKey(remainings.slice(0, nextPos))])
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
let data = fromMapKeysRecursive(keys, []);
|
|
||||||
return KeySequence.from(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ export default interface KeymapRepository {
|
||||||
clear(): void;
|
clear(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
let current: KeySequence = KeySequence.from([]);
|
let current: KeySequence = new KeySequence([]);
|
||||||
|
|
||||||
export class KeymapRepositoryImpl {
|
export class KeymapRepositoryImpl {
|
||||||
|
|
||||||
|
@ -17,6 +17,6 @@ export class KeymapRepositoryImpl {
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(): void {
|
clear(): void {
|
||||||
current = KeySequence.from([]);
|
current = new KeySequence([]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
|
||||||
import * as operations from '../../shared/operations';
|
import * as operations from '../../shared/operations';
|
||||||
import { Keymaps } from '../../shared/Settings';
|
import { Keymaps } from '../../shared/Settings';
|
||||||
import Key from '../domains/Key';
|
import Key from '../domains/Key';
|
||||||
import KeySequence, * as keySequenceUtils from '../domains/KeySequence';
|
import KeySequence from '../domains/KeySequence';
|
||||||
|
|
||||||
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
|
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ export default class KeymapUseCase {
|
||||||
};
|
};
|
||||||
let entries = Object.entries(keymaps).map((entry) => {
|
let entries = Object.entries(keymaps).map((entry) => {
|
||||||
return [
|
return [
|
||||||
keySequenceUtils.fromMapKeys(entry[0]),
|
KeySequence.fromMapKeys(entry[0]),
|
||||||
entry[1],
|
entry[1],
|
||||||
];
|
];
|
||||||
}) as [KeySequence, operations.Operation][];
|
}) as [KeySequence, operations.Operation][];
|
||||||
|
|
|
@ -1,49 +1,48 @@
|
||||||
import KeySequence, * as utils from '../../../src/content/domains/KeySequence';
|
import KeySequence from '../../../src/content/domains/KeySequence';
|
||||||
import Key from '../../../src/content/domains/Key';
|
import Key from '../../../src/content/domains/Key';
|
||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
|
|
||||||
describe("KeySequence", () => {
|
describe("KeySequence", () => {
|
||||||
describe('#push', () => {
|
describe('#push', () => {
|
||||||
it('append a key to the sequence', () => {
|
it('append a key to the sequence', () => {
|
||||||
let seq = KeySequence.from([]);
|
let seq = new KeySequence([]);
|
||||||
seq.push(Key.fromMapKey('g'));
|
seq.push(Key.fromMapKey('g'));
|
||||||
seq.push(Key.fromMapKey('<S-U>'));
|
seq.push(Key.fromMapKey('<S-U>'));
|
||||||
|
|
||||||
let array = seq.getKeyArray();
|
expect(seq.keys[0].key).to.equal('g');
|
||||||
expect(array[0].key).to.equal('g');
|
expect(seq.keys[1].key).to.equal('U');
|
||||||
expect(array[1].key).to.equal('U');
|
expect(seq.keys[1].shift).to.be.true;
|
||||||
expect(array[1].shift).to.be.true;
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#startsWith', () => {
|
describe('#startsWith', () => {
|
||||||
it('returns true if the key sequence starts with param', () => {
|
it('returns true if the key sequence starts with param', () => {
|
||||||
let seq = KeySequence.from([
|
let seq = new KeySequence([
|
||||||
Key.fromMapKey('g'),
|
Key.fromMapKey('g'),
|
||||||
Key.fromMapKey('<S-U>'),
|
Key.fromMapKey('<S-U>'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
]))).to.be.true;
|
]))).to.be.true;
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
Key.fromMapKey('g'),
|
Key.fromMapKey('g'),
|
||||||
]))).to.be.true;
|
]))).to.be.true;
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
Key.fromMapKey('g'), Key.fromMapKey('<S-U>'),
|
Key.fromMapKey('g'), Key.fromMapKey('<S-U>'),
|
||||||
]))).to.be.true;
|
]))).to.be.true;
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
Key.fromMapKey('g'), Key.fromMapKey('<S-U>'), Key.fromMapKey('x'),
|
Key.fromMapKey('g'), Key.fromMapKey('<S-U>'), Key.fromMapKey('x'),
|
||||||
]))).to.be.false;
|
]))).to.be.false;
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
Key.fromMapKey('h'),
|
Key.fromMapKey('h'),
|
||||||
]))).to.be.false;
|
]))).to.be.false;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns true if the empty sequence starts with an empty sequence', () => {
|
it('returns true if the empty sequence starts with an empty sequence', () => {
|
||||||
let seq = KeySequence.from([]);
|
let seq = new KeySequence([]);
|
||||||
|
|
||||||
expect(seq.startsWith(KeySequence.from([]))).to.be.true;
|
expect(seq.startsWith(new KeySequence([]))).to.be.true;
|
||||||
expect(seq.startsWith(KeySequence.from([
|
expect(seq.startsWith(new KeySequence([
|
||||||
Key.fromMapKey('h'),
|
Key.fromMapKey('h'),
|
||||||
]))).to.be.false;
|
]))).to.be.false;
|
||||||
})
|
})
|
||||||
|
@ -51,23 +50,23 @@ describe("KeySequence", () => {
|
||||||
|
|
||||||
describe('#fromMapKeys', () => {
|
describe('#fromMapKeys', () => {
|
||||||
it('returns mapped keys for Shift+Esc', () => {
|
it('returns mapped keys for Shift+Esc', () => {
|
||||||
let keyArray = utils.fromMapKeys('<S-Esc>').getKeyArray();
|
let keys = KeySequence.fromMapKeys('<S-Esc>').keys;
|
||||||
expect(keyArray).to.have.lengthOf(1);
|
expect(keys).to.have.lengthOf(1);
|
||||||
expect(keyArray[0].key).to.equal('Esc');
|
expect(keys[0].key).to.equal('Esc');
|
||||||
expect(keyArray[0].shift).to.be.true;
|
expect(keys[0].shift).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
|
it('returns mapped keys for a<C-B><A-C>d<M-e>', () => {
|
||||||
let keyArray = utils.fromMapKeys('a<C-B><A-C>d<M-e>').getKeyArray();
|
let keys = KeySequence.fromMapKeys('a<C-B><A-C>d<M-e>').keys;
|
||||||
expect(keyArray).to.have.lengthOf(5);
|
expect(keys).to.have.lengthOf(5);
|
||||||
expect(keyArray[0].key).to.equal('a');
|
expect(keys[0].key).to.equal('a');
|
||||||
expect(keyArray[1].ctrl).to.be.true;
|
expect(keys[1].ctrl).to.be.true;
|
||||||
expect(keyArray[1].key).to.equal('b');
|
expect(keys[1].key).to.equal('b');
|
||||||
expect(keyArray[2].alt).to.be.true;
|
expect(keys[2].alt).to.be.true;
|
||||||
expect(keyArray[2].key).to.equal('c');
|
expect(keys[2].key).to.equal('c');
|
||||||
expect(keyArray[3].key).to.equal('d');
|
expect(keys[3].key).to.equal('d');
|
||||||
expect(keyArray[4].meta).to.be.true;
|
expect(keys[4].meta).to.be.true;
|
||||||
expect(keyArray[4].key).to.equal('e');
|
expect(keys[4].key).to.equal('e');
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,7 +16,7 @@ describe('KeymapRepositoryImpl', () => {
|
||||||
sut.enqueueKey(Key.fromMapKey('b');
|
sut.enqueueKey(Key.fromMapKey('b');
|
||||||
let sequence = sut.enqueueKey(Key.fromMapKey('c'));
|
let sequence = sut.enqueueKey(Key.fromMapKey('c'));
|
||||||
|
|
||||||
let keys = sequence.getKeyArray();
|
let keys = sequence.keys;
|
||||||
expect(keys[0].equals(Key.fromMapKey('a'))).to.be.true;
|
expect(keys[0].equals(Key.fromMapKey('a'))).to.be.true;
|
||||||
expect(keys[1].equals(Key.fromMapKey('b'))).to.be.true;
|
expect(keys[1].equals(Key.fromMapKey('b'))).to.be.true;
|
||||||
expect(keys[2].equals(Key.fromMapKey('c'))).to.be.true;
|
expect(keys[2].equals(Key.fromMapKey('c'))).to.be.true;
|
||||||
|
@ -25,9 +25,9 @@ describe('KeymapRepositoryImpl', () => {
|
||||||
|
|
||||||
describe('#clear()', () => {
|
describe('#clear()', () => {
|
||||||
it('clears keys', () => {
|
it('clears keys', () => {
|
||||||
sut.enqueueKey(Key.fromMapKey('a');
|
sut.enqueueKey(Key.fromMapKey('a'));
|
||||||
sut.enqueueKey(Key.fromMapKey('b');
|
sut.enqueueKey(Key.fromMapKey('b'));
|
||||||
sut.enqueueKey(Key.fromMapKey('c');
|
sut.enqueueKey(Key.fromMapKey('c'));
|
||||||
sut.clear();
|
sut.clear();
|
||||||
|
|
||||||
let sequence = sut.enqueueKey(Key.fromMapKey('a'));
|
let sequence = sut.enqueueKey(Key.fromMapKey('a'));
|
||||||
|
|
Reference in a new issue