MCPcopy
hub / github.com/pandao/editor.md / indentLine

Function indentLine

lib/codemirror/lib/codemirror.js:4532–4588  ·  view source on GitHub ↗
(cm, n, how, aggressive)

Source from the content-addressed store, hash-verified

4530 // lines are not indented, and places where the mode returns Pass
4531 // are left alone.
4532 function indentLine(cm, n, how, aggressive) {
4533 var doc = cm.doc, state;
4534 if (how == null) how = "add";
4535 if (how == "smart") {
4536 // Fall back to "prev" when the mode doesn't have an indentation
4537 // method.
4538 if (!doc.mode.indent) how = "prev";
4539 else state = getStateBefore(cm, n);
4540 }
4541
4542 var tabSize = cm.options.tabSize;
4543 var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);
4544 if (line.stateAfter) line.stateAfter = null;
4545 var curSpaceString = line.text.match(/^\s*/)[0], indentation;
4546 if (!aggressive && !/\S/.test(line.text)) {
4547 indentation = 0;
4548 how = "not";
4549 } else if (how == "smart") {
4550 indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text);
4551 if (indentation == Pass || indentation > 150) {
4552 if (!aggressive) return;
4553 how = "prev";
4554 }
4555 }
4556 if (how == "prev") {
4557 if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize);
4558 else indentation = 0;
4559 } else if (how == "add") {
4560 indentation = curSpace + cm.options.indentUnit;
4561 } else if (how == "subtract") {
4562 indentation = curSpace - cm.options.indentUnit;
4563 } else if (typeof how == "number") {
4564 indentation = curSpace + how;
4565 }
4566 indentation = Math.max(0, indentation);
4567
4568 var indentString = "", pos = 0;
4569 if (cm.options.indentWithTabs)
4570 for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
4571 if (pos < indentation) indentString += spaceStr(indentation - pos);
4572
4573 if (indentString != curSpaceString) {
4574 replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input");
4575 } else {
4576 // Ensure that, if the cursor was in the whitespace at the start
4577 // of the line, it is moved to the end of that space.
4578 for (var i = 0; i < doc.sel.ranges.length; i++) {
4579 var range = doc.sel.ranges[i];
4580 if (range.head.line == n && range.head.ch < curSpaceString.length) {
4581 var pos = Pos(n, curSpaceString.length);
4582 replaceOneSelection(doc, i, new Range(pos, pos));
4583 break;
4584 }
4585 }
4586 }
4587 line.stateAfter = null;
4588 }
4589

Callers 2

applyTextInputFunction · 0.70
codemirror.jsFile · 0.70

Calls 5

getStateBeforeFunction · 0.70
getLineFunction · 0.70
spaceStrFunction · 0.70
replaceRangeFunction · 0.70
replaceOneSelectionFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…