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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions 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.join('\n') + '\n' + content;
Copy link
Member

@johnfn johnfn Sep 8, 2016

Choose a reason for hiding this comment

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

Careful. The reason that existingText can be a string[] is because of yanking in visual block mode - you copy an entire block, and then paste the whole block later. (Same thing with content[].) I'm don't think that flattening down that array is the right thing to do here, because then you lose that block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I was unsure about this. The approach I took preserves the type of the new content, so string + string[] -> string[], string[] + string -> string.

Are you saying that if either existingText or content is string[] then the result should be string[]?

}
} 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 @@ -63,12 +84,17 @@ export class Register {
* register ".
*/
public static async get(vimState: VimState): Promise<IRegisterContent> {
const register = vimState.recordedState.registerName;
let register = vimState.recordedState.registerName;

if (!Register.isValidRegister(register)) {
throw new Error(`Invalid register ${register}`);
}

// Register names are case-insensitive.
Copy link
Member

Choose a reason for hiding this comment

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

This comment is a little bit misleading. Probably you want to say something like 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"],
});

});