(cm, n, how, aggressive)
| 3596 | // lines are not indented, and places where the mode returns Pass |
| 3597 | // are left alone. |
| 3598 | function indentLine(cm, n, how, aggressive) { |
| 3599 | var doc = cm.doc, state; |
| 3600 | if (how == null) how = "add"; |
| 3601 | if (how == "smart") { |
| 3602 | // Fall back to "prev" when the mode doesn't have an indentation |
| 3603 | // method. |
| 3604 | if (!cm.doc.mode.indent) how = "prev"; |
| 3605 | else state = getStateBefore(cm, n); |
| 3606 | } |
| 3607 | |
| 3608 | var tabSize = cm.options.tabSize; |
| 3609 | var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize); |
| 3610 | if (line.stateAfter) line.stateAfter = null; |
| 3611 | var curSpaceString = line.text.match(/^\s*/)[0], indentation; |
| 3612 | if (!aggressive && !/\S/.test(line.text)) { |
| 3613 | indentation = 0; |
| 3614 | how = "not"; |
| 3615 | } else if (how == "smart") { |
| 3616 | indentation = cm.doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text); |
| 3617 | if (indentation == Pass) { |
| 3618 | if (!aggressive) return; |
| 3619 | how = "prev"; |
| 3620 | } |
| 3621 | } |
| 3622 | if (how == "prev") { |
| 3623 | if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize); |
| 3624 | else indentation = 0; |
| 3625 | } else if (how == "add") { |
| 3626 | indentation = curSpace + cm.options.indentUnit; |
| 3627 | } else if (how == "subtract") { |
| 3628 | indentation = curSpace - cm.options.indentUnit; |
| 3629 | } else if (typeof how == "number") { |
| 3630 | indentation = curSpace + how; |
| 3631 | } |
| 3632 | indentation = Math.max(0, indentation); |
| 3633 | |
| 3634 | var indentString = "", pos = 0; |
| 3635 | if (cm.options.indentWithTabs) |
| 3636 | for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";} |
| 3637 | if (pos < indentation) indentString += spaceStr(indentation - pos); |
| 3638 | |
| 3639 | if (indentString != curSpaceString) { |
| 3640 | replaceRange(cm.doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input"); |
| 3641 | } else { |
| 3642 | // Ensure that, if the cursor was in the whitespace at the start |
| 3643 | // of the line, it is moved to the end of that space. |
| 3644 | for (var i = 0; i < doc.sel.ranges.length; i++) { |
| 3645 | var range = doc.sel.ranges[i]; |
| 3646 | if (range.head.line == n && range.head.ch < curSpaceString.length) { |
| 3647 | var pos = Pos(n, curSpaceString.length); |
| 3648 | replaceOneSelection(doc, i, new Range(pos, pos)); |
| 3649 | break; |
| 3650 | } |
| 3651 | } |
| 3652 | } |
| 3653 | line.stateAfter = null; |
| 3654 | } |
| 3655 |
no test coverage detected