Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Use CMs electric char handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Gerber committed Apr 8, 2015
1 parent 240e572 commit 531c8d0
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ define(function (require, exports, module) {
coverGutterNextToScrollbar : true,
cursorScrollMargin : 3,
dragDrop : currentOptions[DRAG_DROP],
electricChars : false, // we use our own impl of this to avoid CodeMirror bugs; see _checkElectricChars()
electricChars : true,
extraKeys : codeMirrorKeyMap,
highlightSelectionMatches : currentOptions[HIGHLIGHT_MATCHES],
indentUnit : currentOptions[USE_TAB_CHAR] ? currentOptions[TAB_SIZE] : currentOptions[SPACE_UNITS],
Expand Down Expand Up @@ -420,39 +420,6 @@ define(function (require, exports, module) {
});
};

/**
* @private
* Checks if the user just typed a closing brace/bracket/paren, and considers automatically
* back-indenting it if so.
*/
Editor.prototype._checkElectricChars = function (event) {
var instance = this._codeMirror,
keyStr = String.fromCharCode(event.which || event.keyCode);

if (/[\]\{\}\)]/.test(keyStr)) {
// If all text before the cursor is whitespace, auto-indent it
var cursor = this.getCursorPos();
var lineStr = instance.getLine(cursor.line);
var nonWS = lineStr.search(/\S/);

if (nonWS === -1 || nonWS >= cursor.ch) {
if (nonWS === -1) {
// if the line is all whitespace, move the cursor to the end of the line
// before indenting so that embedded whitespace such as indents are not
// orphaned to the right of the electric char being inserted
this.setCursorPos(cursor.line, this.document.getLine(cursor.line).length);
}
// Need to do the auto-indent on a timeout to ensure
// the keypress is handled before auto-indenting.
// This is the same timeout value used by the
// electricChars feature in CodeMirror.
window.setTimeout(function () {
instance.indentLine(cursor.line);
}, 75);
}
}
};

/**
* @private
* Handle any cursor movement in editor, including selecting and unselecting text.
Expand All @@ -461,14 +428,42 @@ define(function (require, exports, module) {
Editor.prototype._handleCursorActivity = function (event) {
this._updateStyleActiveLine();
};


/**
* @private
* Removes any whitespace after one of ]{}) to prevent trailing whitespace when auto-indenting
*/
Editor.prototype._handleWhitespaceForElectricChars = function () {
var self = this,
instance = this._codeMirror,
selections,
lineStr;

selections = this.getSelections().map(function (sel) {
lineStr = instance.getLine(sel.end.line);

if (lineStr && !/\S/.test(lineStr)) {
// if the line is all whitespace, move the cursor to the end of the line
// before indenting so that embedded whitespace such as indents are not
// orphaned to the right of the electric char being inserted
sel.end.ch = self.document.getLine(sel.end.line).length;
}
return sel;
});
this.setSelections(selections);
};

/**
* @private
* Handle CodeMirror key events.
* @param {!Event} event
*/
Editor.prototype._handleKeypressEvents = function (event) {
this._checkElectricChars(event);
var keyStr = String.fromCharCode(event.which || event.keyCode);

if (/[\]\{\}\)]/.test(keyStr)) {
this._handleWhitespaceForElectricChars();
}
};

/**
Expand Down

0 comments on commit 531c8d0

Please sign in to comment.