MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / indentLine

Function indentLine

lib/web/CodeMirror/src/input/indent.js:14–71  ·  view source on GitHub ↗
(cm, n, how, aggressive)

Source from the content-addressed store, hash-verified

12// lines are not indented, and places where the mode returns Pass
13// are left alone.
14export function indentLine(cm, n, how, aggressive) {
15 let doc = cm.doc, state
16 if (how == null) how = "add"
17 if (how == "smart") {
18 // Fall back to "prev" when the mode doesn't have an indentation
19 // method.
20 if (!doc.mode.indent) how = "prev"
21 else state = getContextBefore(cm, n).state
22 }
23
24 let tabSize = cm.options.tabSize
25 let line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize)
26 if (line.stateAfter) line.stateAfter = null
27 let curSpaceString = line.text.match(/^\s*/)[0], indentation
28 if (!aggressive && !/\S/.test(line.text)) {
29 indentation = 0
30 how = "not"
31 } else if (how == "smart") {
32 indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text)
33 if (indentation == Pass || indentation > 150) {
34 if (!aggressive) return
35 how = "prev"
36 }
37 }
38 if (how == "prev") {
39 if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize)
40 else indentation = 0
41 } else if (how == "add") {
42 indentation = curSpace + cm.options.indentUnit
43 } else if (how == "subtract") {
44 indentation = curSpace - cm.options.indentUnit
45 } else if (typeof how == "number") {
46 indentation = curSpace + how
47 }
48 indentation = Math.max(0, indentation)
49
50 let indentString = "", pos = 0
51 if (cm.options.indentWithTabs)
52 for (let i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t"}
53 if (pos < indentation) indentString += spaceStr(indentation - pos)
54
55 if (indentString != curSpaceString) {
56 replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input")
57 line.stateAfter = null
58 return true
59 } else {
60 // Ensure that, if the cursor was in the whitespace at the start
61 // of the line, it is moved to the end of that space.
62 for (let i = 0; i < doc.sel.ranges.length; i++) {
63 let range = doc.sel.ranges[i]
64 if (range.head.line == n && range.head.ch < curSpaceString.length) {
65 let pos = Pos(n, curSpaceString.length)
66 replaceOneSelection(doc, i, new Range(pos, pos))
67 break
68 }
69 }
70 }
71}

Callers 2

triggerElectricFunction · 0.90
methods.jsFile · 0.90

Calls 11

getContextBeforeFunction · 0.90
getLineFunction · 0.90
countColumnFunction · 0.90
spaceStrFunction · 0.90
replaceRangeFunction · 0.90
PosFunction · 0.90
replaceOneSelectionFunction · 0.90
matchMethod · 0.45
testMethod · 0.45
indentMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected