(doc, from, to, options, type)
| 4957 | |
| 4958 | // Create a marker, wire it up to the right lines, and |
| 4959 | function markText(doc, from, to, options, type) { |
| 4960 | // Shared markers (across linked documents) are handled separately |
| 4961 | // (markTextShared will call out to this again, once per |
| 4962 | // document). |
| 4963 | if (options && options.shared) return markTextShared(doc, from, to, options, type); |
| 4964 | // Ensure we are in an operation. |
| 4965 | if (doc.cm && !doc.cm.curOp) return operation(doc.cm, markText)(doc, from, to, options, type); |
| 4966 | |
| 4967 | var marker = new TextMarker(doc, type), diff = cmp(from, to); |
| 4968 | if (options) copyObj(options, marker, false); |
| 4969 | // Don't connect empty markers unless clearWhenEmpty is false |
| 4970 | if (diff > 0 || diff == 0 && marker.clearWhenEmpty !== false) |
| 4971 | return marker; |
| 4972 | if (marker.replacedWith) { |
| 4973 | // Showing up as a widget implies collapsed (widget replaces text) |
| 4974 | marker.collapsed = true; |
| 4975 | marker.widgetNode = elt("span", [marker.replacedWith], "CodeMirror-widget"); |
| 4976 | if (!options.handleMouseEvents) marker.widgetNode.ignoreEvents = true; |
| 4977 | if (options.insertLeft) marker.widgetNode.insertLeft = true; |
| 4978 | } |
| 4979 | if (marker.collapsed) { |
| 4980 | if (conflictingCollapsedRange(doc, from.line, from, to, marker) || |
| 4981 | from.line != to.line && conflictingCollapsedRange(doc, to.line, from, to, marker)) |
| 4982 | throw new Error("Inserting collapsed marker partially overlapping an existing one"); |
| 4983 | sawCollapsedSpans = true; |
| 4984 | } |
| 4985 | |
| 4986 | if (marker.addToHistory) |
| 4987 | addChangeToHistory(doc, {from: from, to: to, origin: "markText"}, doc.sel, NaN); |
| 4988 | |
| 4989 | var curLine = from.line, cm = doc.cm, updateMaxLine; |
| 4990 | doc.iter(curLine, to.line + 1, function(line) { |
| 4991 | if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(line) == cm.display.maxLine) |
| 4992 | updateMaxLine = true; |
| 4993 | if (marker.collapsed && curLine != from.line) updateLineHeight(line, 0); |
| 4994 | addMarkedSpan(line, new MarkedSpan(marker, |
| 4995 | curLine == from.line ? from.ch : null, |
| 4996 | curLine == to.line ? to.ch : null)); |
| 4997 | ++curLine; |
| 4998 | }); |
| 4999 | // lineIsHidden depends on the presence of the spans, so needs a second pass |
| 5000 | if (marker.collapsed) doc.iter(from.line, to.line + 1, function(line) { |
| 5001 | if (lineIsHidden(doc, line)) updateLineHeight(line, 0); |
| 5002 | }); |
| 5003 | |
| 5004 | if (marker.clearOnEnter) on(marker, "beforeCursorEnter", function() { marker.clear(); }); |
| 5005 | |
| 5006 | if (marker.readOnly) { |
| 5007 | sawReadOnlySpans = true; |
| 5008 | if (doc.history.done.length || doc.history.undone.length) |
| 5009 | doc.clearHistory(); |
| 5010 | } |
| 5011 | if (marker.collapsed) { |
| 5012 | marker.id = ++nextMarkerId; |
| 5013 | marker.atomic = true; |
| 5014 | } |
| 5015 | if (cm) { |
| 5016 | // Sync editor state |
no test coverage detected