| 2287 | |
| 2288 | // Make sure a position will be valid after the given change. |
| 2289 | function clipPostChange(doc, change, pos) { |
| 2290 | if (!posLess(change.from, pos)) return clipPos(doc, pos); |
| 2291 | var diff = (change.text.length - 1) - (change.to.line - change.from.line); |
| 2292 | if (pos.line > change.to.line + diff) { |
| 2293 | var preLine = pos.line - diff, lastLine = doc.first + doc.size - 1; |
| 2294 | if (preLine > lastLine) return Pos(lastLine, getLine(doc, lastLine).text.length); |
| 2295 | return clipToLen(pos, getLine(doc, preLine).text.length); |
| 2296 | } |
| 2297 | if (pos.line == change.to.line + diff) |
| 2298 | return clipToLen(pos, lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0) + |
| 2299 | getLine(doc, change.to.line).text.length - change.to.ch); |
| 2300 | var inside = pos.line - change.from.line; |
| 2301 | return clipToLen(pos, change.text[inside].length + (inside ? 0 : change.from.ch)); |
| 2302 | } |
| 2303 | |
| 2304 | // Hint can be null|"end"|"start"|"around"|{anchor,head} |
| 2305 | function computeSelAfterChange(doc, change, hint) { |