From a0b9140e18bfaf78359cb518ffc89d9c3974c4d1 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Sun, 13 Feb 2011 15:52:29 +0100 Subject: [PATCH] implement transpose letters command --- lib/ace/commands/default_commands.js | 4 +++ lib/ace/editor.js | 28 ++++++++++++++++ lib/ace/test/text_edit_test.js | 50 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index ff9ec121..aa243c5e 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -256,5 +256,9 @@ canon.addCommand({ env.editor.insert(lang.stringRepeat(args.text || "", args.times || 1)); } }); +canon.addCommand({ + name: "transposeletters", + exec: function(env, args, request) { env.editor.transposeLetters(); } +}); }); diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 9d785ca7..996efabe 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -565,6 +565,34 @@ var Editor =function(renderer, session) { this.clearSelection(); }; + + this.transposeLetters = function() { + if (this.$readOnly) + return; + + if (!this.selection.isEmpty()) { + //this.session.remove(this.getSelectionRange()); + //this.clearSelection(); + return; + } + + var cursor = this.getCursorPosition(); + var column = cursor.column; + if (column == 0) + return; + + var line = this.session.getLine(cursor.row); + if (column < line.length) { + var swap = line.charAt(column) + line.charAt(column-1); + var range = new Range(cursor.row, column-1, cursor.row, column+1) + } + else { + var swap = line.charAt(column-1) + line.charAt(column-2); + var range = new Range(cursor.row, column-2, cursor.row, column) + } + this.session.replace(range, swap); + }; + this.indent = function() { if (this.$readOnly) return; diff --git a/lib/ace/test/text_edit_test.js b/lib/ace/test/text_edit_test.js index 4537464a..5bdb49ce 100644 --- a/lib/ace/test/text_edit_test.js +++ b/lib/ace/test/text_edit_test.js @@ -434,6 +434,56 @@ var Test = { editor.moveCursorTo(1, 8); editor.removeLeft(); assert.equal(session.toString(), "123\n 456"); + }, + + "test: transpose at line start should be a noop": function() { + var session = new EditSession(["123", "4567", "89"]); + + var editor = new Editor(new MockRenderer(), session); + editor.moveCursorTo(1, 0); + editor.transposeLetters(); + + assert.equal(session.getValue(), ["123", "4567", "89"].join("\n")); + }, + + "test: transpose in line should swap the charaters before and after the cursor": function() { + var session = new EditSession(["123", "4567", "89"]); + + var editor = new Editor(new MockRenderer(), session); + editor.moveCursorTo(1, 2); + editor.transposeLetters(); + + assert.equal(session.getValue(), ["123", "4657", "89"].join("\n")); + }, + + "test: transpose at line end should swap the last two characters": function() { + var session = new EditSession(["123", "4567", "89"]); + + var editor = new Editor(new MockRenderer(), session); + editor.moveCursorTo(1, 4); + editor.transposeLetters(); + + assert.equal(session.getValue(), ["123", "4576", "89"].join("\n")); + }, + + "test: transpose with non empty selection should be a noop": function() { + var session = new EditSession(["123", "4567", "89"]); + + var editor = new Editor(new MockRenderer(), session); + editor.moveCursorTo(1, 1); + editor.getSelection().selectRight(); + editor.transposeLetters(); + + assert.equal(session.getValue(), ["123", "4567", "89"].join("\n")); + }, + + "test: transpose should move the cursor behind the last swapped character": function() { + var session = new EditSession(["123", "4567", "89"]); + + var editor = new Editor(new MockRenderer(), session); + editor.moveCursorTo(1, 2); + editor.transposeLetters(); + assert.position(editor.getCursorPosition(), 1, 3); } };