Permalink
Browse files

Support backspace in insert mode.

  • Loading branch information...
1 parent eb8beb6 commit 540e3ab1c295e67da581fe05ba765f5aad7aba1c @georgebrock georgebrock committed Dec 7, 2012
Showing with 75 additions and 13 deletions.
  1. +4 −0 css/base.css
  2. +36 −8 js/base.js
  3. +1 −1 js/insert_mode.js
  4. +3 −2 js/utils.js
  5. +23 −0 tests/acceptance/insertion.js
  6. +5 −0 tests/acceptance/repeat.js
  7. +1 −0 tests/helpers.js
  8. +2 −2 tests/unit/insert_mode.js
View
@@ -3,6 +3,10 @@
margin: 20px auto;
font-family: 'Arial', sans-serif;
}
+.vimulator input {
+ position: absolute;
+ left: -9999px;
+}
.vimulator pre {
position: relative;
z-index: 2;
View
@@ -36,9 +36,15 @@
Vimulator.Base.prototype.bindKeyListeners = function () {
var vim = this;
+ this.input = $('<input type="text">').appendTo(this.container)
+ .focus()
+ .blur(function () {
+ $(this).focus();
+ });
+
// Use keyup for special characters like escape
$(window).keyup(function (e) {
- if (e.keyCode === 27) {
+ if (e.keyCode < 32) {
vim.keyPress(e.keyCode);
return false;
}
@@ -136,13 +142,35 @@
return this.lines[this.cursor.row];
};
- Vimulator.Base.prototype.appendText = function (text) {
- line = this.currentLine();
- this.lines[this.cursor.row] =
- line.substr(0, this.cursor.col) +
- text +
- line.substr(this.cursor.col);
- this.cursor.col += text.length;
+ Vimulator.Base.prototype.appendChr = function (chr) {
+ var line;
+
+ if (chr === Vimulator.Utils.Keys.BACKSPACE) {
+ this.removeChr();
+ } else {
+ line = this.currentLine();
+ this.lines[this.cursor.row] =
+ line.substr(0, this.cursor.col) +
+ chr +
+ line.substr(this.cursor.col);
+ this.cursor.col += 1;
+ }
+ };
+
+ Vimulator.Base.prototype.removeChr = function () {
+ var line = this.currentLine();
+
+ if (this.cursor.col === 0 && this.cursor.row > 0) {
+ this.moveCursorRelative(-1, '$');
+ this.cursor.col += 1; //FIXME
+ this.lines[this.cursor.row] += line;
+ this.removeRows(this.cursor.row + 1, this.cursor.row + 2);
+ } else if (this.cursor.col > 0) {
+ this.lines[this.cursor.row] =
+ line.substr(0, this.cursor.col - 1) +
+ line.substr(this.cursor.col);
+ this.cursor.col -= 1;
+ }
};
Vimulator.Base.prototype.insertRowBelow = function (text, index) {
View
@@ -28,7 +28,7 @@
return op;
} else {
this.vim.registers["."] += key;
- this.vim.appendText(key);
+ this.vim.appendChr(key);
}
};
}());
View
@@ -4,8 +4,9 @@
Vimulator.Utils = {};
Vimulator.Utils.Keys = K = {
- ESC: '\u001B',
- RETURN: '\u000D'
+ BACKSPACE: '\u0008',
+ RETURN: '\u000D',
+ ESC: '\u001B'
};
Vimulator.Utils.pluralize = function (count, word) {
@@ -95,3 +95,26 @@ describe("Substituting lines with S", function () {
expect(currentText()).toBe("1 and 2\nThird\nFourth");
});
});
+
+describe("Deleting characters with backspace", function () {
+ it("deletes a character before the cursor", function () {
+ reset("Hello world!!1");
+ pressKeys("A" + BACKSPACE + BACKSPACE + ESC);
+ expect(currentText()).toBe("Hello world!");
+ expect(cursorPosition()).toEqual({row: 0, col: 11});
+ });
+
+ it("deletes line breaks", function () {
+ reset("First line\nSecond line\nThird line");
+ pressKeys("ja" + BACKSPACE + BACKSPACE + ESC);
+ expect(currentText()).toBe("First lineecond line\nThird line");
+ expect(cursorPosition()).toEqual({row: 0, col: 9});
+ });
+
+ it("does not delete beyond the end of the document", function () {
+ reset("Hello world");
+ pressKeys("gI" + BACKSPACE + BACKSPACE + ESC);
+ expect(currentText()).toBe("Hello world");
+ expect(cursorPosition()).toEqual({row: 0, col: 0});
+ });
+});
@@ -23,6 +23,11 @@ describe("Repeating the last command with .", function () {
expect(currentText()).toBe("Here ore some words, they ore all here");
});
+ it("repeats backspaces", function () {
+ pressKeys("ea" + BACKSPACE + ESC + "e.e.");
+ expect(currentText()).toBe("Her ar som words, they are all here");
+ });
+
it("does not repeat motions", function () {
pressKeys("3l");
expect(cursorPosition()).toEqual({row: 0, col: 3});
View
@@ -1,5 +1,6 @@
ESC = '\u001B';
RETURN = '\u000D';
+BACKSPACE = '\u0008';
function pressKeys(keys) {
jQuery.each(keys.split(''), function (i, key) {
@@ -13,7 +13,7 @@ describe("InsertMode", function () {
vim = jasmine.createSpyObj("vim", [
"moveCursorRelative",
"setMode",
- "appendText"
+ "appendChr"
]);
vim.registers = {};
im = new Vimulator.InsertMode(vim);
@@ -33,7 +33,7 @@ describe("InsertMode", function () {
for (i = 0; i < chars.length; i +=1) {
chr = chars[i];
im.keyPress(chr);
- expect(vim.appendText).toHaveBeenCalledWith(chr);
+ expect(vim.appendChr).toHaveBeenCalledWith(chr);
}
});

0 comments on commit 540e3ab

Please sign in to comment.