[occur] cleanup

This commit is contained in:
Robert Krahn 2013-03-17 04:39:45 -07:00
commit 313c4f77f7
2 changed files with 67 additions and 32 deletions

View file

@ -37,16 +37,8 @@ var config = require("../config"),
var occurStartCommands = [{ var occurStartCommands = [{
name: "occur", name: "occur",
exec: function(editor, options) { exec: function(editor, options) {
if (!options.needle) return; var occurSessionActive = new Occur().enter(editor, options);
var pos = editor.getCursorPosition(); if (occurSessionActive) OccurKeyboardHandler.installIn(editor);
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 readOnly: true
}]; }];
@ -55,26 +47,20 @@ var occurCommands = [{
name: "occurexit", name: "occurexit",
bindKey: 'esc|Ctrl-G', bindKey: 'esc|Ctrl-G',
exec: function(editor) { exec: function(editor) {
// if (!options.needle) return; var occur = editor.session.getDocument().$occur;
var session = editor.session, occur = session.doc.$occur; if (!occur) return;
if (occur) occur.displayOriginal(session); occur.exit(editor, {});
editor.commands.removeCommands(occurCommands); OccurKeyboardHandler.uninstallFrom(editor);
editor.setReadOnly(false);
var handler = editor.getKeyboardHandler();
if (handler.isOccurHandler) editor.keyBinding.removeKeyboardHandler(handler);
}, },
readOnly: true readOnly: true
}, { }, {
name: "occuraccept", name: "occuraccept",
bindKey: 'enter', bindKey: 'enter',
exec: function(editor) { exec: function(editor) {
var occur = editor.session.doc.$occur, var occur = editor.session.getDocument().$occur;
pos = editor.getCursorPosition(); if (!occur) return;
if (occur) { occur.exit(editor, {translatePosition: true});
pos = occur.occurToOriginalPosition(editor.session, pos); OccurKeyboardHandler.uninstallFrom(editor);
}
editor.commands.byName.occurexit.exec(editor);
editor.moveCursorToPosition(pos);
}, },
readOnly: true readOnly: true
}]; }];
@ -82,6 +68,7 @@ var occurCommands = [{
var HashHandler = require("../keyboard/hash_handler").HashHandler; var HashHandler = require("../keyboard/hash_handler").HashHandler;
var oop = require("../lib/oop"); var oop = require("../lib/oop");
function OccurKeyboardHandler() {} function OccurKeyboardHandler() {}
oop.inherits(OccurKeyboardHandler, HashHandler); oop.inherits(OccurKeyboardHandler, HashHandler);
@ -95,8 +82,6 @@ oop.inherits(OccurKeyboardHandler, HashHandler);
this.$editor = editor; this.$editor = editor;
} }
this.detach = function() {}
var handleKeyboard$super = this.handleKeyboard; var handleKeyboard$super = this.handleKeyboard;
this.handleKeyboard = function(data, hashId, key, keyCode) { this.handleKeyboard = function(data, hashId, key, keyCode) {
var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode); var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode);
@ -105,6 +90,18 @@ oop.inherits(OccurKeyboardHandler, HashHandler);
}).call(OccurKeyboardHandler.prototype); }).call(OccurKeyboardHandler.prototype);
OccurKeyboardHandler.installIn = function(editor) {
var handler = new this();
editor.keyBinding.addKeyboardHandler(handler);
editor.commands.addCommands(occurCommands);
}
OccurKeyboardHandler.uninstallFrom = function(editor) {
editor.commands.removeCommands(occurCommands);
var handler = editor.getKeyboardHandler();
if (handler.isOccurHandler) editor.keyBinding.removeKeyboardHandler(handler);
}
exports.commands = occurStartCommands; exports.commands = occurStartCommands;
}); });

View file

@ -39,8 +39,9 @@ var Document = require("./document").Document;
/** /**
* @class Occur * @class Occur
* *
* Finds all lines matching a search term in the current docuement and * Finds all lines matching a search term in the current [[Document
* displays them instead of the original document. * `Document`]] and displays them instead of the original `Document`. Keeps
* track of the mapping between the occur doc and the original doc.
* *
**/ **/
@ -52,15 +53,52 @@ var Document = require("./document").Document;
* *
* @constructor * @constructor
**/ **/
function Occur() { function Occur() {}
// this.$options = {wrap: false, skipCurrent: false};
// this.$keyboardHandler = new ISearchKbd(this);
}
oop.inherits(Occur, Search); oop.inherits(Occur, Search);
(function() { (function() {
/**
* Enables occur mode. expects that `options.needle` is a search term.
* This search term is used to filter out all the lines that include it
* and these are then used as the content of a new [[Document
* `Document`]]. The current cursor position of editor will be translated
* so that the cursor is on the matching row/column as it was before.
* @param {Editor} editor
* @param {Object} options options.needle should be a String
* @return {Boolean} Whether occur activation was successful
*
**/
this.enter = function(editor, options) {
if (!options.needle) return false;
var pos = editor.getCursorPosition();
editor.setReadOnly(true);
this.display(editor.session, options);
var translatedPos = this.originalToOccurPosition(editor.session, pos);
editor.moveCursorToPosition(translatedPos);
return true;
}
/**
* Disables occur mode. Resets the [[Sessions `EditSession`]] [[Document
* `Document`]] back to the original doc. If options.translatePosition is
* truthy also maps the [[Editors `Editor`]] cursor position accordingly.
* @param {Editor} editor
* @param {Object} options options.translatePosition
* @return {Boolean} Whether occur deactivation was successful
*
**/
this.exit = function(editor, options) {
var session = editor.session,
pos = options.translatePosition && editor.getCursorPosition(),
translatedPos = pos && this.occurToOriginalPosition(editor.session, pos);
this.displayOriginal(session);
editor.setReadOnly(false);
if (translatedPos) editor.moveCursorToPosition(translatedPos);
return true;
}
this.display = function(session, options) { this.display = function(session, options) {
this.$originalDoc = session.doc; this.$originalDoc = session.doc;
var found = this.matchingLines(session, options), var found = this.matchingLines(session, options),