MCPcopy
hub / github.com/witheve/Eve / applyTextInput

Function applyTextInput

src/codemirror.js:1104–1149  ·  view source on GitHub ↗
(cm, inserted, deleted, sel, origin)

Source from the content-addressed store, hash-verified

1102 var lastCopied = null;
1103
1104 function applyTextInput(cm, inserted, deleted, sel, origin) {
1105 var doc = cm.doc;
1106 cm.display.shift = false;
1107 if (!sel) sel = doc.sel;
1108
1109 var paste = cm.state.pasteIncoming || origin == "paste";
1110 var textLines = doc.splitLines(inserted), multiPaste = null
1111 // When pasing N lines into N selections, insert one line per selection
1112 if (paste && sel.ranges.length > 1) {
1113 if (lastCopied && lastCopied.text.join("\n") == inserted) {
1114 if (sel.ranges.length % lastCopied.text.length == 0) {
1115 multiPaste = [];
1116 for (var i = 0; i < lastCopied.text.length; i++)
1117 multiPaste.push(doc.splitLines(lastCopied.text[i]));
1118 }
1119 } else if (textLines.length == sel.ranges.length) {
1120 multiPaste = map(textLines, function(l) { return [l]; });
1121 }
1122 }
1123
1124 // Normal behavior is to insert the new text into every selection
1125 for (var i = sel.ranges.length - 1; i >= 0; i--) {
1126 var range = sel.ranges[i];
1127 var from = range.from(), to = range.to();
1128 if (range.empty()) {
1129 if (deleted && deleted > 0) // Handle deletion
1130 from = Pos(from.line, from.ch - deleted);
1131 else if (cm.state.overwrite && !paste) // Handle overwrite
1132 to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length));
1133 else if (lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == inserted)
1134 from = to = Pos(from.line, 0)
1135 }
1136 var updateInput = cm.curOp.updateInput;
1137 var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i % multiPaste.length] : textLines,
1138 origin: origin || (paste ? "paste" : cm.state.cutIncoming ? "cut" : "+input")};
1139 makeChange(cm.doc, changeEvent);
1140 signalLater(cm, "inputRead", cm, changeEvent);
1141 }
1142 if (inserted && !paste)
1143 triggerElectric(cm, inserted);
1144
1145 ensureCursorVisible(cm);
1146 cm.curOp.updateInput = updateInput;
1147 cm.curOp.typing = true;
1148 cm.state.pasteIncoming = cm.state.cutIncoming = false;
1149 }
1150
1151 function handlePaste(e, cm) {
1152 var pasted = e.clipboardData && e.clipboardData.getData("text/plain");

Callers 2

handlePasteFunction · 0.85
codemirror.jsFile · 0.85

Calls 9

mapFunction · 0.85
getLineFunction · 0.85
lstFunction · 0.85
makeChangeFunction · 0.85
signalLaterFunction · 0.85
triggerElectricFunction · 0.85
ensureCursorVisibleFunction · 0.85
fromMethod · 0.80
toMethod · 0.80

Tested by

no test coverage detected