Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/EditorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ define(function (require, exports, module) {
}

if (indentAuto) {
var currentWS = line.search(/\S/);
var currentLength = line.length;
CodeMirror.commands.indentAuto(instance);
// If the amount of whitespace didn't change, insert another tab
if (instance.getLine(from.line).search(/\S/) === currentWS) {
if (instance.getLine(from.line).length === currentLength) {
insertTab = true;
to.ch = 0;
}
Expand All @@ -81,7 +81,7 @@ define(function (require, exports, module) {
} else {
var i, ins = "", numSpaces = instance.getOption("tabSize");
numSpaces -= to.ch % numSpaces;
for (i = 0; i < numSpaces + 1; i++) {
for (i = 0; i < numSpaces; i++) {
ins += " ";
}
instance.replaceSelection(ins, "end");
Expand Down Expand Up @@ -160,6 +160,7 @@ define(function (require, exports, module) {
// NOTE: CodeMirror doesn't actually require calling 'new',
// but jslint does require it because of the capital 'C'
var editor = new CodeMirror(_editorHolder.get(0), {
electricChars: false,
indentUnit : 4,
lineNumbers: true,
extraKeys: {
Expand Down Expand Up @@ -188,6 +189,28 @@ define(function (require, exports, module) {
"Shift-F3": "findPrev",
"Ctrl-H": "replace",
"Shift-Delete": "cut"
},
onKeyEvent: function (instance, event) {
if (event.type === "keypress") {
var keyStr = String.fromCharCode(event.which || event.keyCode);
if (/[\]\}\)]/.test(keyStr)) {
// If the whole line is whitespace, auto-indent it
var lineNum = instance.getCursor().line;
var lineStr = instance.getLine(lineNum);

if (!/\S/.test(lineStr)) {
// 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.
setTimeout(function () {
instance.indentLine(lineNum);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CodeMirror electric-char handling passes "smart" here--the docs don't mention what this does and it's not clear to me what all the various options are. Should we be passing "smart" as well?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, CodeMirror wraps the indentation in an operation()--do we need to do that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"smart" is the default, but we should probably pass it anyway to be safe. Code updated.

I decided not to wrap it in an operation since the re-indenting should not be a separate undo step. In other words, if you type:

foo() {<return>}

and press undo, the closing curly should be undone. If we wrap an operation, then the re-indent would be undone first, and then the closing curly would be removed. (the actual granularity is subject to the timing of undo in CodeMirror, which may end up batching both into a single undo anyway)

}, 75);
}
}
}

return false;
}
});

Expand Down