(cm, n, how, aggressive)
| 4732 | // lines are not indented, and places where the mode returns Pass |
| 4733 | // are left alone. |
| 4734 | function indentLine(cm, n, how, aggressive) { |
| 4735 | var doc = cm.doc, state; |
| 4736 | if (how == null) how = "add"; |
| 4737 | if (how == "smart") { |
| 4738 | // Fall back to "prev" when the mode doesn't have an indentation |
| 4739 | // method. |
| 4740 | if (!doc.mode.indent) how = "prev"; |
| 4741 | else state = getStateBefore(cm, n); |
| 4742 | } |
| 4743 | |
| 4744 | var tabSize = cm.options.tabSize; |
| 4745 | var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize); |
| 4746 | if (line.stateAfter) line.stateAfter = null; |
| 4747 | var curSpaceString = line.text.match(/^\s*/)[0], indentation; |
| 4748 | if (!aggressive && !/\S/.test(line.text)) { |
| 4749 | indentation = 0; |
| 4750 | how = "not"; |
| 4751 | } else if (how == "smart") { |
| 4752 | indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text); |
| 4753 | if (indentation == Pass || indentation > 150) { |
| 4754 | if (!aggressive) return; |
| 4755 | how = "prev"; |
| 4756 | } |
| 4757 | } |
| 4758 | if (how == "prev") { |
| 4759 | if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize); |
| 4760 | else indentation = 0; |
| 4761 | } else if (how == "add") { |
| 4762 | indentation = curSpace + cm.options.indentUnit; |
| 4763 | } else if (how == "subtract") { |
| 4764 | indentation = curSpace - cm.options.indentUnit; |
| 4765 | } else if (typeof how == "number") { |
| 4766 | indentation = curSpace + how; |
| 4767 | } |
| 4768 | indentation = Math.max(0, indentation); |
| 4769 | |
| 4770 | var indentString = "", pos = 0; |
| 4771 | if (cm.options.indentWithTabs) |
| 4772 | for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";} |
| 4773 | if (pos < indentation) indentString += spaceStr(indentation - pos); |
| 4774 | |
| 4775 | if (indentString != curSpaceString) { |
| 4776 | replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input"); |
| 4777 | line.stateAfter = null; |
| 4778 | return true; |
| 4779 | } else { |
| 4780 | // Ensure that, if the cursor was in the whitespace at the start |
| 4781 | // of the line, it is moved to the end of that space. |
| 4782 | for (var i = 0; i < doc.sel.ranges.length; i++) { |
| 4783 | var range = doc.sel.ranges[i]; |
| 4784 | if (range.head.line == n && range.head.ch < curSpaceString.length) { |
| 4785 | var pos = Pos(n, curSpaceString.length); |
| 4786 | replaceOneSelection(doc, i, new Range(pos, pos)); |
| 4787 | break; |
| 4788 | } |
| 4789 | } |
| 4790 | } |
| 4791 | } |
no test coverage detected