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