MCPcopy Create free account
hub / github.com/TruthHun/BookStack / indentLine

Function indentLine

static/mergely/lib/codemirror.js:4711–4768  ·  view source on GitHub ↗
(cm, n, how, aggressive)

Source from the content-addressed store, hash-verified

4709 // lines are not indented, and places where the mode returns Pass
4710 // are left alone.
4711 function indentLine(cm, n, how, aggressive) {
4712 var doc = cm.doc, state;
4713 if (how == null) how = "add";
4714 if (how == "smart") {
4715 // Fall back to "prev" when the mode doesn't have an indentation
4716 // method.
4717 if (!doc.mode.indent) how = "prev";
4718 else state = getStateBefore(cm, n);
4719 }
4720
4721 var tabSize = cm.options.tabSize;
4722 var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);
4723 if (line.stateAfter) line.stateAfter = null;
4724 var curSpaceString = line.text.match(/^\s*/)[0], indentation;
4725 if (!aggressive && !/\S/.test(line.text)) {
4726 indentation = 0;
4727 how = "not";
4728 } else if (how == "smart") {
4729 indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text);
4730 if (indentation == Pass || indentation > 150) {
4731 if (!aggressive) return;
4732 how = "prev";
4733 }
4734 }
4735 if (how == "prev") {
4736 if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize);
4737 else indentation = 0;
4738 } else if (how == "add") {
4739 indentation = curSpace + cm.options.indentUnit;
4740 } else if (how == "subtract") {
4741 indentation = curSpace - cm.options.indentUnit;
4742 } else if (typeof how == "number") {
4743 indentation = curSpace + how;
4744 }
4745 indentation = Math.max(0, indentation);
4746
4747 var indentString = "", pos = 0;
4748 if (cm.options.indentWithTabs)
4749 for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}
4750 if (pos < indentation) indentString += spaceStr(indentation - pos);
4751
4752 if (indentString != curSpaceString) {
4753 replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input");
4754 line.stateAfter = null;
4755 return true;
4756 } else {
4757 // Ensure that, if the cursor was in the whitespace at the start
4758 // of the line, it is moved to the end of that space.
4759 for (var i = 0; i < doc.sel.ranges.length; i++) {
4760 var range = doc.sel.ranges[i];
4761 if (range.head.line == n && range.head.ch < curSpaceString.length) {
4762 var pos = Pos(n, curSpaceString.length);
4763 replaceOneSelection(doc, i, new Range(pos, pos));
4764 break;
4765 }
4766 }
4767 }
4768 }

Callers 2

triggerElectricFunction · 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