Skip to content

Commit

Permalink
Extend existing macro with uppercase register (like qX) (#6971)
Browse files Browse the repository at this point in the history
Fixes #6965
  • Loading branch information
DevinLeamy authored Aug 14, 2021
1 parent e1ce051 commit c2d7f84
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ class CommandRecordMacro extends BaseCommand {
const registerKey = this.keysPressed[1];
const register = registerKey.toLocaleLowerCase();
vimState.macro = new RecordedState();
vimState.macro.registerKey = registerKey;
vimState.macro.registerName = register;

if (!/^[A-Z]+$/.test(registerKey) || !Register.has(register)) {
// If register name is upper case, it means we are appending commands to existing register instead of overriding.
if (!Register.isValidUppercaseRegister(registerKey) || !Register.has(register)) {
// TODO: this seems suspect - why are we not putting `vimState.macro` in the register? Why are we setting `registerName`?
const newRegister = new RecordedState();
newRegister.registerName = register;
Expand All @@ -341,7 +341,11 @@ export class CommandQuitRecordMacro extends BaseCommand {

const existingMacro = (await Register.get(macro.registerName))?.text;
if (existingMacro instanceof RecordedState) {
existingMacro.actionsRun = existingMacro.actionsRun.concat(macro.actionsRun);
if (Register.isValidUppercaseRegister(macro.registerKey)) {
existingMacro.actionsRun = existingMacro.actionsRun.concat(macro.actionsRun);
} else {
existingMacro.actionsRun = macro.actionsRun;
}
}

vimState.macro = undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Register {
return /^[a-z]$/.test(register);
}

private static isValidUppercaseRegister(register: string): boolean {
public static isValidUppercaseRegister(register: string): boolean {
return /^[A-Z]$/.test(register);
}

Expand Down
7 changes: 7 additions & 0 deletions src/state/recordedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ export class RecordedState {
*/
public registerName: string;

/**
* The key used to access the register with `registerName`
* Example: if 'q5' then key=5 and name=5
* Or: if 'qA' then key=A and name=a
*/
public registerKey: string = '';

public clone(): RecordedState {
const res = new RecordedState();

Expand Down
25 changes: 25 additions & 0 deletions test/macro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ suite('Record and execute a macro', () => {
end: ['one two XXX three', 'one two XXX three', 'one two XX|X three'],
});

newTest({
title: 'q[A-Z] (action) Can record and append to a macro',
start: ['|'],
keysPressed:
'qb' +
'i' +
'one two ' +
'<Esc>q' +
'o<Esc>@b' +
'o<Esc>' +
'qB' +
'i' +
'three four' +
'<Esc>q' +
'o<Esc>@b',
end: ['one two ', 'one two ', 'three four', 'one twothree fou|r '],
});

newTest({
title: 'q[A-Z] (action) Creates new register, accessible by [a-z]',
start: ['|'],
keysPressed: 'qB' + 'i' + 'one two' + '<Esc>q' + 'o<Esc>@b',
end: ['one two', 'one tw|o'],
});

newTest({
title: 'Invalid register throws E354',
start: ['one t|wo three'],
Expand Down

0 comments on commit c2d7f84

Please sign in to comment.