[occur] entering and exiting occur: position is translated
This commit is contained in:
parent
1483264033
commit
204e6b5ab0
3 changed files with 76 additions and 0 deletions
|
|
@ -38,12 +38,15 @@ var occurStartCommands = [{
|
||||||
name: "occur",
|
name: "occur",
|
||||||
exec: function(editor, options) {
|
exec: function(editor, options) {
|
||||||
if (!options.needle) return;
|
if (!options.needle) return;
|
||||||
|
var pos = editor.getCursorPosition();
|
||||||
var handler = new OccurKeyboardHandler();
|
var handler = new OccurKeyboardHandler();
|
||||||
editor.keyBinding.addKeyboardHandler(handler);
|
editor.keyBinding.addKeyboardHandler(handler);
|
||||||
editor.commands.addCommands(occurCommands);
|
editor.commands.addCommands(occurCommands);
|
||||||
editor.setReadOnly(true);
|
editor.setReadOnly(true);
|
||||||
var occur = new Occur();
|
var occur = new Occur();
|
||||||
occur.display(editor.session, options);
|
occur.display(editor.session, options);
|
||||||
|
pos = occur.originalToOccurPosition(editor.session, pos);
|
||||||
|
editor.moveCursorToPosition(pos);
|
||||||
},
|
},
|
||||||
readOnly: true
|
readOnly: true
|
||||||
}];
|
}];
|
||||||
|
|
@ -61,6 +64,19 @@ var occurCommands = [{
|
||||||
if (handler.isOccurHandler) editor.keyBinding.removeKeyboardHandler(handler);
|
if (handler.isOccurHandler) editor.keyBinding.removeKeyboardHandler(handler);
|
||||||
},
|
},
|
||||||
readOnly: true
|
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;
|
var HashHandler = require("../keyboard/hash_handler").HashHandler;
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,7 @@ oop.inherits(Occur, Search);
|
||||||
lines = found.map(function(foundLine) { return foundLine.content; }),
|
lines = found.map(function(foundLine) { return foundLine.content; }),
|
||||||
occurDoc = new Document(lines);
|
occurDoc = new Document(lines);
|
||||||
occurDoc.$occur = this;
|
occurDoc.$occur = this;
|
||||||
|
occurDoc.$occurMatchingLines = found;
|
||||||
session.setDocument(occurDoc);
|
session.setDocument(occurDoc);
|
||||||
session.highlight(null);
|
session.highlight(null);
|
||||||
session.highlight(options.re);
|
session.highlight(options.re);
|
||||||
|
|
@ -79,6 +80,38 @@ oop.inherits(Occur, Search);
|
||||||
session._emit('changeBackMarker');
|
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) {
|
this.matchingLines = function(session, options) {
|
||||||
options = oop.mixin({}, options);
|
options = oop.mixin({}, options);
|
||||||
if (!session || !options.needle) return [];
|
if (!session || !options.needle) return [];
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,14 @@ module.exports = {
|
||||||
assert.equal(session.getValue(), lines.join('\n') + '\n');
|
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() {
|
"test: occur command" : function() {
|
||||||
// setup
|
// setup
|
||||||
var lines = ['hel', 'lo', '', 'wo', 'rld'];
|
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.getReadOnly(), 'original doc is marked as read only');
|
||||||
assert.ok(!editor.getKeyboardHandler().isOccurHandler, 'occur handler installed after detach');
|
assert.ok(!editor.getKeyboardHandler().isOccurHandler, 'occur handler installed after detach');
|
||||||
assert.ok(!editor.commands.byName.occurexit, 'exitoccur installed after exiting occur');
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue