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

Function handleChar

lib/web/CodeMirror/addon/edit/closebrackets.js:117–188  ·  view source on GitHub ↗
(cm, ch)

Source from the content-addressed store, hash-verified

115 }
116
117 function handleChar(cm, ch) {
118 var conf = getConfig(cm);
119 if (!conf || cm.getOption("disableInput")) return CodeMirror.Pass;
120
121 var pairs = getOption(conf, "pairs");
122 var pos = pairs.indexOf(ch);
123 if (pos == -1) return CodeMirror.Pass;
124
125 var closeBefore = getOption(conf,"closeBefore");
126
127 var triples = getOption(conf, "triples");
128
129 var identical = pairs.charAt(pos + 1) == ch;
130 var ranges = cm.listSelections();
131 var opening = pos % 2 == 0;
132
133 var type;
134 for (var i = 0; i < ranges.length; i++) {
135 var range = ranges[i], cur = range.head, curType;
136 var next = cm.getRange(cur, Pos(cur.line, cur.ch + 1));
137 if (opening && !range.empty()) {
138 curType = "surround";
139 } else if ((identical || !opening) && next == ch) {
140 if (identical && stringStartsAfter(cm, cur))
141 curType = "both";
142 else if (triples.indexOf(ch) >= 0 && cm.getRange(cur, Pos(cur.line, cur.ch + 3)) == ch + ch + ch)
143 curType = "skipThree";
144 else
145 curType = "skip";
146 } else if (identical && cur.ch > 1 && triples.indexOf(ch) >= 0 &&
147 cm.getRange(Pos(cur.line, cur.ch - 2), cur) == ch + ch) {
148 if (cur.ch > 2 && /\bstring/.test(cm.getTokenTypeAt(Pos(cur.line, cur.ch - 2)))) return CodeMirror.Pass;
149 curType = "addFour";
150 } else if (identical) {
151 var prev = cur.ch == 0 ? " " : cm.getRange(Pos(cur.line, cur.ch - 1), cur)
152 if (!CodeMirror.isWordChar(next) && prev != ch && !CodeMirror.isWordChar(prev)) curType = "both";
153 else return CodeMirror.Pass;
154 } else if (opening && (next.length === 0 || /\s/.test(next) || closeBefore.indexOf(next) > -1)) {
155 curType = "both";
156 } else {
157 return CodeMirror.Pass;
158 }
159 if (!type) type = curType;
160 else if (type != curType) return CodeMirror.Pass;
161 }
162
163 var left = pos % 2 ? pairs.charAt(pos - 1) : ch;
164 var right = pos % 2 ? ch : pairs.charAt(pos + 1);
165 cm.operation(function() {
166 if (type == "skip") {
167 moveSel(cm, 1)
168 } else if (type == "skipThree") {
169 moveSel(cm, 3)
170 } else if (type == "surround") {
171 var sels = cm.getSelections();
172 for (var i = 0; i < sels.length; i++)
173 sels[i] = left + sels[i] + right;
174 cm.replaceSelections(sels, "around");

Callers 1

handlerFunction · 0.85

Calls 10

getConfigFunction · 0.85
stringStartsAfterFunction · 0.85
moveSelFunction · 0.85
contractSelectionFunction · 0.85
indexOfMethod · 0.80
emptyMethod · 0.80
getOptionFunction · 0.70
PosFunction · 0.50
getRangeMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected