From 72070938f0d3e92778367f40c2bc82776f7c35ba Mon Sep 17 00:00:00 2001
From: Jason Killian <jason.killian@trialspark.com>
Date: Tue, 2 Oct 2018 15:57:58 -0400
Subject: [PATCH] multiline yank writes to 0 register; fixes #1214

---
 src/register/register.ts       |  4 +---
 src/state/recordedState.ts     |  2 +-
 test/register/register.test.ts | 43 ++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/register/register.ts b/src/register/register.ts
index 1deb0500e28..62b2590ea32 100644
--- a/src/register/register.ts
+++ b/src/register/register.ts
@@ -302,9 +302,7 @@ export class Register {
    */
   private static processNumberedRegister(content: RegisterContent, vimState: VimState): void {
     // Find the BaseOperator of the current actions
-    const baseOperator = vimState.recordedState.actionsRun.find(value => {
-      return value instanceof BaseOperator || value instanceof BaseCommand;
-    });
+    const baseOperator = vimState.recordedState.operator || vimState.recordedState.command;
 
     if (baseOperator instanceof YankOperator || baseOperator instanceof CommandYankFullLine) {
       // 'yank' to 0 only if no register was specified
diff --git a/src/state/recordedState.ts b/src/state/recordedState.ts
index 7575f2e329d..5a0812b97a3 100644
--- a/src/state/recordedState.ts
+++ b/src/state/recordedState.ts
@@ -126,7 +126,7 @@ export class RecordedState {
    * The command (e.g. i, ., R, /) the user wants to run, if there is one.
    */
   public get command(): BaseCommand {
-    const list = _.filter(this.actionsRun, a => a instanceof BaseCommand);
+    const list = _.filter(this.actionsRun, a => a instanceof BaseCommand).reverse();
 
     // TODO - disregard <Esc>, then assert this is of length 1.
 
diff --git a/test/register/register.test.ts b/test/register/register.test.ts
index 4caf803adbf..a2e01284c71 100644
--- a/test/register/register.test.ts
+++ b/test/register/register.test.ts
@@ -105,6 +105,49 @@ suite('register', () => {
     assertEqualLines(['test2', 'test2', 'test3']);
   });
 
+  test("Multiline yank (`[count]yy`) stores text in Register '0'", async () => {
+    modeHandler.vimState.editor = vscode.window.activeTextEditor!;
+
+    await modeHandler.handleMultipleKeyEvents('itest1\ntest2\ntest3'.split(''));
+
+    await modeHandler.handleMultipleKeyEvents([
+      '<Esc>',
+      'g',
+      'g',
+      '2',
+      'y',
+      'y',
+      'd',
+      'd',
+      '"',
+      '0',
+      'P',
+    ]);
+
+    assertEqualLines(['test1', 'test2', 'test2', 'test3']);
+  });
+
+  test("Multiline yank (`[count]Y`) stores text in Register '0'", async () => {
+    modeHandler.vimState.editor = vscode.window.activeTextEditor!;
+
+    await modeHandler.handleMultipleKeyEvents('itest1\ntest2\ntest3'.split(''));
+
+    await modeHandler.handleMultipleKeyEvents([
+      '<Esc>',
+      'g',
+      'g',
+      '2',
+      'Y',
+      'd',
+      'd',
+      '"',
+      '0',
+      'P',
+    ]);
+
+    assertEqualLines(['test1', 'test2', 'test2', 'test3']);
+  });
+
   test("Register '1'-'9' stores delete content", async () => {
     modeHandler.vimState.editor = vscode.window.activeTextEditor!;