From 8288002f672064cafa3219396dffd83900ad6a7c Mon Sep 17 00:00:00 2001
From: blukat29 <yunjong@kaist.ac.kr>
Date: Mon, 6 Oct 2014 04:09:17 +0900
Subject: [PATCH] [vim] Fix error when dd the only line.

When the buffer has only one line, pressing 'dd' causes
CodeMirror to throw an error "There is no line -1".
---
 keymap/vim.js    | 13 +++++++++----
 test/vim_test.js |  9 +++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/keymap/vim.js b/keymap/vim.js
index 50b1c554f..a4cfd5796 100644
--- a/keymap/vim.js
+++ b/keymap/vim.js
@@ -1862,10 +1862,15 @@
         // including the trailing \n, include the \n before the starting line
         if (operatorArgs.linewise &&
             curEnd.line == cm.lastLine() && curStart.line == curEnd.line) {
-          var tmp = copyCursor(curEnd);
-          curStart.line--;
-          curStart.ch = lineLength(cm, curStart.line);
-          curEnd = tmp;
+          if (curEnd.line == 0) {
+            curStart.ch = 0;
+          }
+          else {
+            var tmp = copyCursor(curEnd);
+            curStart.line--;
+            curStart.ch = lineLength(cm, curStart.line);
+            curEnd = tmp;
+          }
           cm.replaceRange('', curStart, curEnd);
         } else {
           cm.replaceSelections(replacement);
diff --git a/test/vim_test.js b/test/vim_test.js
index 1a5d0f5d6..7d5e5a148 100644
--- a/test/vim_test.js
+++ b/test/vim_test.js
@@ -828,6 +828,15 @@ testVim('dd_lastline', function(cm, vim, helpers) {
   eq(expectedLineCount, cm.lineCount());
   helpers.assertCursorAt(cm.lineCount() - 1, 0);
 });
+testVim('dd_only_line', function(cm, vim, helpers) {
+  cm.setCursor(0, 0);
+  var expectedRegister = cm.getValue() + "\n";
+  helpers.doKeys('d','d');
+  eq(1, cm.lineCount());
+  eq('', cm.getValue());
+  var register = helpers.getRegisterController().getRegister();
+  eq(expectedRegister, register.toString());
+}, { value: "thisistheonlyline" });
 // Yank commands should behave the exact same as d commands, expect that nothing
 // gets deleted.
 testVim('yw_repeat', function(cm, vim, helpers) {
-- 
GitLab