Skip to main content

conceal_style_spans

Function conceal_style_spans 

Source
pub fn conceal_style_spans(
    rules: &[ConcealRule],
    line: &str,
) -> Vec<(u32, u32, Style)>
Expand description

The byte ranges rules hide in line — sorted and coalesced.

Rules are tried in declaration order and every match of every rule contributes; the result is the union of their hidden groups.

§Why the union is coalesced before it is returned

Two rules hiding overlapping spans must produce one hidden span. The consumer subtracts end - start per range to find a display column, so an un-merged overlap subtracts its shared width twice and every column past it on that line is wrong. Merging here rather than trusting callers means the invariant holds at the one place it can be established.

§Declaration order does not affect the result

Stated because the design originally claimed the opposite, and a test caught it. Every rule is tried at every position and the hidden spans are unioned, so no rule can consume text before another sees it — the output is the same under any permutation of the rule list.

The worry that motivated the retracted claim was that a described org link [[a][b]] would be matched as a bare one, hiding the outer brackets and leaving a][b on screen. It cannot happen, for a second and independent reason: the bare pattern’s [^]]+ stops at the first ], so it never reaches the closing ]] of a described link and does not match it at all. The two patterns are disjoint by construction, which is a property of how they are written rather than of the order they are declared in.

What a rule author must therefore get right is the pattern, not its position: a rule that matches more than it means to will hide more than it means to no matter where it sits.

Returns empty immediately when there are no rules, which is the path every buffer in the editor but one takes. OL.1: the styled runs rules paint in line — each match’s VISIBLE remainder, for rules that declared a style.

“What survives concealment” is the styling unit, and picking it rather than a named capture group is what makes one declaration serve both of org’s link forms without a second knob:

[[target][description]]   hides `[[target][` and `]]`  → styles `description`
[[target]]               hides `[[` and `]]`           → styles `target`

Semantically: what is left on screen after concealing IS the link, so the mechanism that decides where a link is also decides what to paint. Two mechanisms could disagree about that; one cannot.

Returns empty immediately when no rule carries a style — which is every language today but org, and every org rule but the two link ones.