Skip to content

Commit

Permalink
Error thrown when inserting overlapping isolate markers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis DOUALLE committed Jun 21, 2018
1 parent 5eb74de commit 3b6c52c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/line/saw_special_spans.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Optimize some code when these features are not used.
export let sawReadOnlySpans = false, sawCollapsedSpans = false
export let sawReadOnlySpans = false, sawCollapsedSpans = false, sawIsolateSpans = false

export function seeReadOnlySpans() {
sawReadOnlySpans = true
Expand All @@ -8,3 +8,7 @@ export function seeReadOnlySpans() {
export function seeCollapsedSpans() {
sawCollapsedSpans = true
}

export function seeIsolateSpans() {
sawIsolateSpans = true
}
17 changes: 16 additions & 1 deletion src/line/spans.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { indexOf, lst } from "../util/misc.js"

import { cmp } from "./pos.js"
import { sawCollapsedSpans } from "./saw_special_spans.js"
import { sawCollapsedSpans, sawIsolateSpans } from "./saw_special_spans.js"
import { getLine, isLine, lineNo } from "./utils_line.js"

// TEXTMARKER SPANS
Expand Down Expand Up @@ -247,6 +247,21 @@ export function conflictingCollapsedRange(doc, lineNo, from, to, marker) {
}
}

// Test whether two isolate ranges overlap, which is not allowed
export function conflictingIsolateRange(doc, lineNo$$2, from, to, marker) {
let line = getLine(doc, lineNo$$2)
let sis = sawIsolateSpans && line.markedSpans
if (sis) { for (let i = 0; i < sis.length; ++i) {
let si = sis[i]
if (!si.marker.isolate) { continue }
let found = si.marker.find(0)
let fromCmp = cmp(found.from, from) || extraLeft(si.marker) - extraLeft(marker)
if (fromCmp <= 0 && (si.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.to, from) >= 0 : cmp(found.to, from) > 0) ||
fromCmp >= 0 && (si.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.from, to) <= 0 : cmp(found.from, to) < 0))
{ return true }
} }
}

// A visual line is a line as drawn on the screen. Folding, for
// example, can cause multiple logical lines to appear on the same
// visual line. This finds the start of the visual line that the
Expand Down
10 changes: 8 additions & 2 deletions src/model/mark_text.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { endOperation, operation, runInOp, startOperation } from "../display/ope
import { clipPos, cmp, Pos } from "../line/pos.js"
import { lineNo, updateLineHeight } from "../line/utils_line.js"
import { clearLineMeasurementCacheFor, findViewForLine, textHeight } from "../measurement/position_measurement.js"
import { seeReadOnlySpans, seeCollapsedSpans } from "../line/saw_special_spans.js"
import { addMarkedSpan, conflictingCollapsedRange, getMarkedSpanFor, lineIsHidden, lineLength, MarkedSpan, removeMarkedSpan, visualLine } from "../line/spans.js"
import { seeReadOnlySpans, seeCollapsedSpans, seeIsolateSpans } from "../line/saw_special_spans.js"
import { addMarkedSpan, conflictingCollapsedRange, conflictingIsolateRange, getMarkedSpanFor, lineIsHidden, lineLength, MarkedSpan, removeMarkedSpan, visualLine } from "../line/spans.js"
import { copyObj, indexOf, lst } from "../util/misc.js"
import { signalLater } from "../util/operation_group.js"
import { widgetHeight } from "../measurement/widgets.js"
Expand Down Expand Up @@ -177,6 +177,12 @@ export function markText(doc, from, to, options, type) {
throw new Error("Inserting collapsed marker partially overlapping an existing one")
seeCollapsedSpans()
}
if (marker.isolate) {
if (conflictingIsolateRange(doc, from.line, from, to, marker) ||
from.line != to.line && conflictingIsolateRange(doc, to.line, from, to, marker))
{ throw new Error("Inserting isolate marker partially overlapping an existing one") }
seeIsolateSpans()
}

if (marker.addToHistory)
addChangeToHistory(doc, {from: from, to: to, origin: "markText"}, doc.sel, NaN)
Expand Down

0 comments on commit 3b6c52c

Please sign in to comment.