From 204e6b5ab003cf35bbfafc737671f57772f7dfeb Mon Sep 17 00:00:00 2001 From: Robert Krahn Date: Sun, 17 Mar 2013 04:11:34 -0700 Subject: [PATCH] [occur] entering and exiting occur: position is translated --- lib/ace/commands/occur_commands.js | 16 +++++++++++++++ lib/ace/occur.js | 33 ++++++++++++++++++++++++++++++ lib/ace/occur_test.js | 27 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/lib/ace/commands/occur_commands.js b/lib/ace/commands/occur_commands.js index a0106f08..b8669203 100644 --- a/lib/ace/commands/occur_commands.js +++ b/lib/ace/commands/occur_commands.js @@ -38,12 +38,15 @@ var occurStartCommands = [{ name: "occur", exec: function(editor, options) { if (!options.needle) return; + var pos = editor.getCursorPosition(); var handler = new OccurKeyboardHandler(); editor.keyBinding.addKeyboardHandler(handler); editor.commands.addCommands(occurCommands); editor.setReadOnly(true); var occur = new Occur(); occur.display(editor.session, options); + pos = occur.originalToOccurPosition(editor.session, pos); + editor.moveCursorToPosition(pos); }, readOnly: true }]; @@ -61,6 +64,19 @@ var occurCommands = [{ if (handler.isOccurHandler) editor.keyBinding.removeKeyboardHandler(handler); }, readOnly: true +}, { + name: "occuraccept", + bindKey: 'enter', + exec: function(editor) { + var occur = editor.session.doc.$occur, + pos = editor.getCursorPosition(); + if (occur) { + pos = occur.occurToOriginalPosition(editor.session, pos); + } + editor.commands.byName.occurexit.exec(editor); + editor.moveCursorToPosition(pos); + }, + readOnly: true }]; var HashHandler = require("../keyboard/hash_handler").HashHandler; diff --git a/lib/ace/occur.js b/lib/ace/occur.js index fd4fdbd9..6ab695dd 100644 --- a/lib/ace/occur.js +++ b/lib/ace/occur.js @@ -67,6 +67,7 @@ oop.inherits(Occur, Search); lines = found.map(function(foundLine) { return foundLine.content; }), occurDoc = new Document(lines); occurDoc.$occur = this; + occurDoc.$occurMatchingLines = found; session.setDocument(occurDoc); session.highlight(null); session.highlight(options.re); @@ -79,6 +80,38 @@ oop.inherits(Occur, Search); session._emit('changeBackMarker'); } + /** + * Translates the position from the original document to the occur lines in + * the document or the beginning if the doc {row: 0, column: 0} if not + * found. + * @param {EditSession} session The occur session + * @param {Object} pos The position in the original document + * @return {Object} position in occur doc + **/ + this.originalToOccurPosition = function(session, pos) { + var lines = session.getDocument().$occurMatchingLines, + nullPos = {row: 0, column: 0}; + if (!lines) return nullPos; + for (var i = 0; i < lines.length; i++) { + if (lines[i].row === pos.row) return {row: i, column: pos.column} + } + return nullPos; + } + + /** + * Translates the position from the occur document to the original document + * or `pos` if not found. + * @param {EditSession} session The occur session + * @param {Object} pos The position in the occur session document + * @return {Object} position + **/ + this.occurToOriginalPosition = function(session, pos) { + var lines = session.getDocument().$occurMatchingLines; + if (!lines || !lines[pos.row]) return pos; + debugger + return {row: lines[pos.row].row, column: pos.column}; + } + this.matchingLines = function(session, options) { options = oop.mixin({}, options); if (!session || !options.needle) return []; diff --git a/lib/ace/occur_test.js b/lib/ace/occur_test.js index faadf976..73d8d831 100644 --- a/lib/ace/occur_test.js +++ b/lib/ace/occur_test.js @@ -70,6 +70,14 @@ module.exports = { assert.equal(session.getValue(), lines.join('\n') + '\n'); }, + "test: original position from occur doc" : function() { + session.doc.insertLines(0, ['abc', 'def', 'xyz', 'bcx']); + occur.display(session, {needle: 'bc'}); + assert.equal(session.getValue(), 'abc\nbcx'); + var pos = occur.occurToOriginalPosition(session, {row: 1, column: 2}); + assert.position(pos, 3, 2); + }, + "test: occur command" : function() { // setup var lines = ['hel', 'lo', '', 'wo', 'rld']; @@ -93,6 +101,25 @@ module.exports = { assert.ok(!editor.getReadOnly(), 'original doc is marked as read only'); assert.ok(!editor.getKeyboardHandler().isOccurHandler, 'occur handler installed after detach'); assert.ok(!editor.commands.byName.occurexit, 'exitoccur installed after exiting occur'); + }, + + "test: occur navigation" : function() { + // setup + var lines = ['hel', 'lo', '', 'wo', 'rld']; + session.doc.insertLines(0, lines); + editor.commands.addCommands(occurCommands); + editor.moveCursorToPosition({row: 1, column: 1}); + + // run occur for lines including 'o' + editor.execCommand('occur', {needle: 'o'}); + assert.equal(session.getValue(), 'lo\nwo'); + assert.position(editor.getCursorPosition(), 0, 1, 'original -> occur pos'); + + // move to second line and accept + editor.moveCursorToPosition({row: 1, column: 1}); + editor.execCommand('occuraccept'); + + assert.position(editor.getCursorPosition(), 3, 1, 'occur -> original pos'); } };