ChimeHQ / Neon

A Swift library for efficient, flexible content-based text styling
BSD 3-Clause "New" or "Revised" License
335 stars 18 forks source link

TextStorageSystemInterface does not style textedits that are not at end of string #43

Open Wouter01 opened 8 months ago

Wouter01 commented 8 months ago

When using TextStorageSystemInterface, initial highlighting works fine. Adding text to the end of the file works fine too. Adding text somewhere else doesn't seem to highlight it.

I did some digging and couldn't find an immediate reason why it's happening. In the beginValidation function (RangeValidator.swift), the following line will return .none, which is probably unexpected.

guard let neededRange = nextNeededRange(in: set, prioritizing: range) else { return .none }

Reproducing the issue: open the sample project, in TextViewSystemInterface comment out the first two interfaces in effectiveInterface, so the TextStorageSystemInterface is used. Then, try editing the text somewhere in the middle.

mattmassicotte commented 8 months ago

Ok reproduced. Also not sure what is wrong. But I suspect it is either timing, or bad interaction between the text view typing attributes and the storage. I don't use storage-based attributes ever myself so I'm not surprised this is problematic.

mattmassicotte commented 8 months ago

Made little process here. Even you force TextViewSystemInterface.effectiveInterface to always return a TextStorageSystemInterface, NSTextView.visibleTextRange will still make use of TextKit2 to calculate the visible ranges. Those calculations are wrong, but I'm not sure yet whether I'm doing it incorrectly or TextKit 2 is just returning in correct results.

But I bet it is TextKit 2 returning stale information because of its async layout calculations.