Skip to content

Instantly share code, notes, and snippets.

@njx
Created October 30, 2012 02:19
Show Gist options
  • Save njx/3977937 to your computer and use it in GitHub Desktop.
Save njx/3977937 to your computer and use it in GitHub Desktop.
CM v3 selection bug
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $, CodeMirror, window */
/**
* Editor is a 1-to-1 wrapper for a CodeMirror editor instance. It layers on Brackets-specific
* functionality and provides APIs that cleanly pass through the bits of CodeMirror that the rest
* of our codebase may want to interact with. An Editor is always backed by a Document, and stays
* in sync with its content; because Editor keeps the Document alive, it's important to always
* destroy() an Editor that's going away so it can release its Document ref.
*
* For now, there's a distinction between the "master" Editor for a Document - which secretly acts
* as the Document's internal model of the text state - and the multitude of "slave" secondary Editors
* which, via Document, sync their changes to and from that master.
*
* For now, direct access to the underlying CodeMirror object is still possible via _codeMirror --
* but this is considered deprecated and may go away.
*
* The Editor object dispatches the following events:
* - keyEvent -- When any key event happens in the editor (whether it changes the text or not).
* Event handlers are passed ({Editor}, {KeyboardEvent}). The 2nd arg is the raw DOM event.
* Note: most listeners will only want to respond when event.type === "keypress".
* - cursorActivity -- When the user moves the cursor or changes the selection, or an edit occurs.
* Note: do not listen to this in order to be generally informed of edits--listen to the
* "change" event on Document instead.
* - scroll -- When the editor is scrolled, either by user action or programmatically.
* - lostContent -- When the backing Document changes in such a way that this Editor is no longer
* able to display accurate text. This occurs if the Document's file is deleted, or in certain
* Document->editor syncing edge cases that we do not yet support (the latter cause will
* eventually go away).
*
* The Editor also dispatches "change" events internally, but you should listen for those on
* Documents, not Editors.
*
* These are jQuery events, so to listen for them you do something like this:
* $(editorInstance).on("eventname", handler);
*/
define(function (require, exports, module) {
/**
* Sets the current selection. Start is inclusive, end is exclusive. Places the cursor at the
* end of the selection range.
* @param {!{line:number, ch:number}} start
* @param {!{line:number, ch:number}} end
*/
Editor.prototype.setSelection = function (start, end) {
this._codeMirror.setSelection(start, end);
};
/**
* Selects word that the given pos lies within or adjacent to. If pos isn't touching a word
* (e.g. within a token like "//"), moves the cursor to pos without selecting a range.
* @param {!{line:number, ch:number}}
*/
Editor.prototype.selectWordAt = function (pos) {
this._codeMirror.selectWordAt(pos);
};
/**
* Gets the total number of lines in the the document (includes lines not visible in the viewport)
* @returns {!number}
*/
Editor.prototype.lineCount = function () {
return this._codeMirror.lineCount();
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment