| 411 | |
| 412 | // The {line,ch} representation of positions makes this rather awkward. |
| 413 | function findContext(doc, data) { |
| 414 | var before = data.context.slice(0, data.contextOffset).split("\n"); |
| 415 | var startLine = data.start.line - (before.length - 1); |
| 416 | var start = Pos(startLine, (before.length == 1 ? data.start.ch : doc.getLine(startLine).length) - before[0].length); |
| 417 | |
| 418 | var text = doc.getLine(startLine).slice(start.ch); |
| 419 | for (var cur = startLine + 1; cur < doc.lineCount() && text.length < data.context.length; ++cur) |
| 420 | text += "\n" + doc.getLine(cur); |
| 421 | if (text.slice(0, data.context.length) == data.context) return data; |
| 422 | |
| 423 | var cursor = doc.getSearchCursor(data.context, 0, false); |
| 424 | var nearest, nearestDist = Infinity; |
| 425 | while (cursor.findNext()) { |
| 426 | var from = cursor.from(), dist = Math.abs(from.line - start.line) * 10000; |
| 427 | if (!dist) dist = Math.abs(from.ch - start.ch); |
| 428 | if (dist < nearestDist) { nearest = from; nearestDist = dist; } |
| 429 | } |
| 430 | if (!nearest) return null; |
| 431 | |
| 432 | if (before.length == 1) |
| 433 | nearest.ch += before[0].length; |
| 434 | else |
| 435 | nearest = Pos(nearest.line + (before.length - 1), before[before.length - 1].length); |
| 436 | if (data.start.line == data.end.line) |
| 437 | var end = Pos(nearest.line, nearest.ch + (data.end.ch - data.start.ch)); |
| 438 | else |
| 439 | var end = Pos(nearest.line + (data.end.line - data.start.line), data.end.ch); |
| 440 | return {start: nearest, end: end}; |
| 441 | } |
| 442 | |
| 443 | function atInterestingExpression(cm) { |
| 444 | var pos = cm.getCursor("end"), tok = cm.getTokenAt(pos); |