[occur] proper session replacement
This commit is contained in:
parent
4b0b579bf4
commit
96bca7ed95
3 changed files with 27 additions and 31 deletions
|
|
@ -47,7 +47,7 @@ var occurCommands = [{
|
|||
name: "occurexit",
|
||||
bindKey: 'esc|Ctrl-G',
|
||||
exec: function(editor) {
|
||||
var occur = editor.session.getDocument().$occur;
|
||||
var occur = editor.session.$occur;
|
||||
if (!occur) return;
|
||||
occur.exit(editor, {});
|
||||
OccurKeyboardHandler.uninstallFrom(editor);
|
||||
|
|
@ -57,7 +57,7 @@ var occurCommands = [{
|
|||
name: "occuraccept",
|
||||
bindKey: 'enter',
|
||||
exec: function(editor) {
|
||||
var occur = editor.session.getDocument().$occur;
|
||||
var occur = editor.session.$occur;
|
||||
if (!occur) return;
|
||||
occur.exit(editor, {translatePosition: true});
|
||||
OccurKeyboardHandler.uninstallFrom(editor);
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ define(function(require, exports, module) {
|
|||
var oop = require("./lib/oop");
|
||||
var Range = require("./range").Range;
|
||||
var Search = require("./search").Search;
|
||||
var Document = require("./document").Document;
|
||||
var EditSession = require("./edit_session").EditSession;
|
||||
var SearchHighlight = require("./search_highlight").SearchHighlight;
|
||||
|
||||
/**
|
||||
|
|
@ -74,7 +74,7 @@ oop.inherits(Occur, Search);
|
|||
this.enter = function(editor, options) {
|
||||
if (!options.needle) return false;
|
||||
var pos = editor.getCursorPosition();
|
||||
this.display(editor, options);
|
||||
this.displayOccurContent(editor, options);
|
||||
var translatedPos = this.originalToOccurPosition(editor.session, pos);
|
||||
editor.moveCursorToPosition(translatedPos);
|
||||
return true;
|
||||
|
|
@ -90,10 +90,9 @@ oop.inherits(Occur, Search);
|
|||
*
|
||||
**/
|
||||
this.exit = function(editor, options) {
|
||||
var session = editor.session,
|
||||
pos = options.translatePosition && editor.getCursorPosition(),
|
||||
var pos = options.translatePosition && editor.getCursorPosition(),
|
||||
translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
|
||||
this.displayOriginal(editor);
|
||||
this.displayOriginalContent(editor);
|
||||
if (translatedPos) editor.moveCursorToPosition(translatedPos);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -105,24 +104,23 @@ oop.inherits(Occur, Search);
|
|||
sess._emit("changeBackMarker"); // force highlight layer redraw
|
||||
}
|
||||
|
||||
this.display = function(editor, options) {
|
||||
this.displayOccurContent = function(editor, options) {
|
||||
// this.setSession(session || new EditSession(""))
|
||||
this.$originalDoc = editor.session.getDocument();
|
||||
this.$originalSession = editor.session;
|
||||
var found = this.matchingLines(editor.session, options),
|
||||
lines = found.map(function(foundLine) { return foundLine.content; }),
|
||||
occurDoc = new Document('');
|
||||
occurDoc.$occur = this;
|
||||
occurDoc.$occurMatchingLines = found;
|
||||
editor.session.setDocument(occurDoc);
|
||||
occurDoc.insertLines(0, lines);
|
||||
this.highlight(editor.session, options.re);
|
||||
editor.session._emit('changeBackMarker');
|
||||
occurSession = new EditSession(lines.join('\n'));
|
||||
occurSession.$occur = this;
|
||||
occurSession.$occurMatchingLines = found;
|
||||
editor.setSession(occurSession);
|
||||
this.highlight(occurSession, options.re);
|
||||
occurSession._emit('changeBackMarker');
|
||||
}
|
||||
|
||||
this.displayOriginal = function(editor) {
|
||||
editor.session.setDocument(this.$originalDoc);
|
||||
this.highlight(editor.session, null);
|
||||
editor.session._emit('changeBackMarker');
|
||||
this.displayOriginalContent = function(editor) {
|
||||
editor.setSession(this.$originalSession);
|
||||
this.highlight(this.$originalSession, null);
|
||||
this.$originalSession._emit('changeBackMarker');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -134,7 +132,7 @@ oop.inherits(Occur, Search);
|
|||
* @return {Object} position in occur doc
|
||||
**/
|
||||
this.originalToOccurPosition = function(session, pos) {
|
||||
var lines = session.getDocument().$occurMatchingLines,
|
||||
var lines = session.$occurMatchingLines,
|
||||
nullPos = {row: 0, column: 0};
|
||||
if (!lines) return nullPos;
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
|
|
@ -151,9 +149,8 @@ oop.inherits(Occur, Search);
|
|||
* @return {Object} position
|
||||
**/
|
||||
this.occurToOriginalPosition = function(session, pos) {
|
||||
var lines = session.getDocument().$occurMatchingLines;
|
||||
var lines = session.$occurMatchingLines;
|
||||
if (!lines || !lines[pos.row]) return pos;
|
||||
debugger
|
||||
return {row: lines[pos.row].row, column: pos.column};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -64,17 +64,17 @@ module.exports = {
|
|||
"test: display occurrences" : function() {
|
||||
var text = 'abc\ndef\nxyz\nbcx\n';
|
||||
editor.session.insert({row: 0, column: 0}, text);
|
||||
occur.display(editor, {needle: 'bc'});
|
||||
assert.equal(editor.getValue(), 'abc\nbcx\n');
|
||||
occur.displayOriginal(editor);
|
||||
occur.displayOccurContent(editor, {needle: 'bc'});
|
||||
assert.equal(editor.getValue(), 'abc\nbcx');
|
||||
occur.displayOriginalContent(editor);
|
||||
assert.equal(editor.getValue(), text);
|
||||
},
|
||||
|
||||
"test: original position from occur doc" : function() {
|
||||
var text = 'abc\ndef\nxyz\nbcx\n';
|
||||
editor.session.insert({row: 0, column: 0}, text);
|
||||
occur.display(editor, {needle: 'bc'});
|
||||
assert.equal(editor.getValue(), 'abc\nbcx\n');
|
||||
occur.displayOccurContent(editor, {needle: 'bc'});
|
||||
assert.equal(editor.getValue(), 'abc\nbcx');
|
||||
var pos = occur.occurToOriginalPosition(editor.session, {row: 1, column: 2});
|
||||
assert.position(pos, 3, 2);
|
||||
},
|
||||
|
|
@ -87,8 +87,7 @@ module.exports = {
|
|||
|
||||
// run occur for lines including 'o'
|
||||
editor.execCommand('occur', {needle: 'o'});
|
||||
assert.equal(editor.getValue(), 'lo\nwo\n');
|
||||
|
||||
assert.equal(editor.getValue(), 'lo\nwo');
|
||||
// command install OK?
|
||||
// assert.ok(editor.getReadOnly(), 'occur doc not marked as read only');
|
||||
assert.ok(editor.getKeyboardHandler().isOccurHandler, 'no occur handler installed');
|
||||
|
|
@ -113,7 +112,7 @@ module.exports = {
|
|||
|
||||
// run occur for lines including 'o'
|
||||
editor.execCommand('occur', {needle: 'o'});
|
||||
assert.equal(editor.getValue(), 'lo\nwo\n');
|
||||
assert.equal(editor.getValue(), 'lo\nwo');
|
||||
assert.position(editor.getCursorPosition(), 0, 1, 'original -> occur pos');
|
||||
|
||||
// move to second line and accept
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue