[occur] towards better occur session display

This commit is contained in:
Robert Krahn 2013-03-17 18:22:45 -07:00
commit 4b0b579bf4
2 changed files with 39 additions and 38 deletions

View file

@ -74,8 +74,7 @@ oop.inherits(Occur, Search);
this.enter = function(editor, options) {
if (!options.needle) return false;
var pos = editor.getCursorPosition();
editor.setReadOnly(true);
this.display(editor.session, options);
this.display(editor, options);
var translatedPos = this.originalToOccurPosition(editor.session, pos);
editor.moveCursorToPosition(translatedPos);
return true;
@ -94,8 +93,7 @@ oop.inherits(Occur, Search);
var session = editor.session,
pos = options.translatePosition && editor.getCursorPosition(),
translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
this.displayOriginal(session);
editor.setReadOnly(false);
this.displayOriginal(editor);
if (translatedPos) editor.moveCursorToPosition(translatedPos);
return true;
}
@ -107,22 +105,24 @@ oop.inherits(Occur, Search);
sess._emit("changeBackMarker"); // force highlight layer redraw
}
this.display = function(session, options) {
this.$originalDoc = session.doc;
var found = this.matchingLines(session, options),
this.display = function(editor, options) {
// this.setSession(session || new EditSession(""))
this.$originalDoc = editor.session.getDocument();
var found = this.matchingLines(editor.session, options),
lines = found.map(function(foundLine) { return foundLine.content; }),
occurDoc = new Document(lines);
occurDoc = new Document('');
occurDoc.$occur = this;
occurDoc.$occurMatchingLines = found;
session.setDocument(occurDoc);
this.highlight(session, options.re);
session._emit('changeBackMarker');
editor.session.setDocument(occurDoc);
occurDoc.insertLines(0, lines);
this.highlight(editor.session, options.re);
editor.session._emit('changeBackMarker');
}
this.displayOriginal = function(session) {
session.setDocument(this.$originalDoc);
this.highlight(session, null);
session._emit('changeBackMarker');
this.displayOriginal = function(editor) {
editor.session.setDocument(this.$originalDoc);
this.highlight(editor.session, null);
editor.session._emit('changeBackMarker');
}
/**

View file

@ -42,77 +42,78 @@ var Range = require("./range").Range;
var assert = require("./test/assertions");
var Occur = require("./occur").Occur;
var occurCommands = require("./commands/occur_commands").commands;
var editor, occur, session;
var editor, occur;
module.exports = {
name: "ACE occur.js",
setUp: function() {
session = new EditSession('');
var session = new EditSession('');
editor = new Editor(new MockRenderer(), session);
occur = new Occur();
},
"test: find lines matching" : function() {
session.doc.insertLines(0, ['abc', 'def', 'xyz', 'bcxbc']);
var result = occur.matchingLines(session, {needle: 'bc'}),
editor.session.insert({row: 0, column: 0}, 'abc\ndef\nxyz\nbcxbc');
var result = occur.matchingLines(editor.session, {needle: 'bc'}),
expected = [{row: 0, content: 'abc'}, {row: 3, content: 'bcxbc'}];
assert.deepEqual(result, expected);
},
"test: display occurrences" : function() {
var lines = ['abc', 'def', 'xyz', 'bcx'];
session.doc.insertLines(0, lines);
occur.display(session, {needle: 'bc'});
assert.equal(session.getValue(), 'abc\nbcx');
occur.displayOriginal(session);
assert.equal(session.getValue(), lines.join('\n') + '\n');
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);
assert.equal(editor.getValue(), text);
},
"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});
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');
var pos = occur.occurToOriginalPosition(editor.session, {row: 1, column: 2});
assert.position(pos, 3, 2);
},
"test: occur command" : function() {
// setup
var lines = ['hel', 'lo', '', 'wo', 'rld'];
session.doc.insertLines(0, lines);
var text = 'hel\nlo\n\nwo\nrld\n';
editor.session.insert({row: 0, column: 0}, text);
editor.commands.addCommands(occurCommands);
// run occur for lines including 'o'
editor.execCommand('occur', {needle: 'o'});
assert.equal(session.getValue(), 'lo\nwo');
assert.equal(editor.getValue(), 'lo\nwo\n');
// 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.commands.byName.occurexit, 'no exitoccur command installed');
// exit occur
editor.execCommand('occurexit');
assert.equal(session.getValue(), lines.join('\n') + '\n');
assert.equal(editor.getValue(), text);
// editor state cleaned up?
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.commands.byName.occurexit, 'exitoccur installed after exiting occur');
},
"test: occur navigation" : function() {
// setup
var lines = ['hel', 'lo', '', 'wo', 'rld'];
session.doc.insertLines(0, lines);
var text = 'hel\nlo\n\nwo\nrld\n';
editor.session.insert({row: 0, column: 0}, text);
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.equal(editor.getValue(), 'lo\nwo\n');
assert.position(editor.getCursorPosition(), 0, 1, 'original -> occur pos');
// move to second line and accept