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

Add support for appending to named registers #731

Closed
wants to merge 5 commits into from
Closed
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
28 changes: 27 additions & 1 deletion src/register/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Register {
* register ".
*/
public static put(content: string | string[], vimState: VimState): void {
const register = vimState.recordedState.registerName;
let register = vimState.recordedState.registerName;

if (!Register.isValidRegister(register)) {
throw new Error(`Invalid register ${register}`);
Expand All @@ -52,6 +52,27 @@ export class Register {
clipboard.copy(content);
}

// Upper-case register name denotes append.
if (/^[A-Z]$/.test(register)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this into it's own separate static function (isNamedRegister), similar to isValidRegister?

register = register.toLowerCase();
let existingText = Register.registers[register] && Register.registers[register].text;
if (existingText) {
if (typeof content === 'string') {
if (typeof existingText === 'string') {
content = existingText + (existingText.endsWith('\n') ? '' : '\n') + content;
} else {
content = existingText.concat(content.split('\n'));
}
} else {
if (typeof existingText === 'string') {
content = existingText.split('\n').concat(content);
} else {
content = existingText.concat(content);
}
}
}
}

Register.registers[register] = {
text : content,
registerMode: vimState.effectiveRegisterMode(),
Expand All @@ -72,6 +93,11 @@ export class Register {
throw new Error(`Invalid register ${register}`);
}

// Retrieving register content is case-insensitive
if (/^[A-Z]$/.test(register)) {
register = register.toLowerCase();
}

if (!Register.registers[register]) {
Register.registers[register] = { text: "", registerMode: RegisterMode.CharacterWise };
}
Expand Down
14 changes: 14 additions & 0 deletions test/register/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ suite("register", () => {
end: ["one", "two", "one", "|two"],
});

newTest({
title: "Can append content to register",
start: ['|one', "two"],
keysPressed: '"ayyj"Ayy$"ap',
end: ["one", "two", "|one", "two"],
});

newTest({
title: "Can replace register contents after append",
start: ['|one', "two", "three"],
keysPressed: '"ayyj"Ayyj"ayy$"ap',
end: ["one", "two", "three", "|three"],
});

});