CleanCocoa / CleanCocoa/DeclarativeTextKit
Skip evaluation of a Modifying block iff it doesn't do anything
- Lingua principale
- Swift
- Stelle
- 50
- Fork
- 0
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
The `Modifying() { }` construct always evaluates even if the block is empty.
With TextKit integration, this means
1. an undo group is being started (and ended)
2. `NSTextView.shouldChangeText(in:replacementString:)` and `didChangeText()` are being run to guard against unwanted changes
With syntax highlighting in the text storage, you may end up processing the text for what's essentially a no-op.
## How to test
To get an empty block, use a for-loop to trigger the `buildArray` path of the result builder, but without any actual iterations:
```swift
Modifying(selectedRange) { _ in
for _ in 0 ..< 0 {
Insert(0) { "loop never runs" }
}
}
```
I'm not sure whether we can figure out at all whether a result builder produces nothing (i.e. empty array).
## Complete test case
This test fails with a thrown error at `try buffer.evaluate` because the text view doesn't permit changes in the range.
This should not be a problem, because the range is not actually changed.
```swift
func testModifying_EmptyLoopBlock_SkipsEvaluation() throws {
class TextViewSpy: NSTextView {
var didCallShouldChangeText = false
var didCallDidChangeText = false
override func shouldChangeText(in affectedCharRange: NSRange, replacementString: String?) -> Bool {
didCallShouldChangeText = true
return false // Would abort modification an error
}
override func didChangeText() {
didCallDidChangeText = true
}
}
let textViewSpy = TextViewSpy()
textViewSpy.string = "Lorem ipsum."
let buffer = NSTextViewBuffer(textView: textViewSpy)
let selectedRange: SelectedRange = .init(location: 6, length: 5)
assertBufferState(buffer, "Lorem ipsum.ˇ")
try buffer.evaluate {
Modifying(selectedRange) { _ in
for _ in 0 ..< 0 {
Insert(0) { "loop never runs" }
}
}
}
XCTAssertFalse(textViewSpy.didCallShouldChangeText)
XCTAssertFalse(textViewSpy.didCallDidChangeText)
}
```
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia dal costrutto Modifying e dal punto di ingresso buffer.evaluate, quindi esamina come viene rappresentato un blocco vuoto di result-builder per il percorso buildArray. Riproduci il problema con testModifying_EmptyLoopBlock_SkipsEvaluation e il TextViewSpy fornito. Il lavoro è completato quando il blocco di ciclo vuoto salta la valutazione e nessuno dei due callback di modifica del testo viene chiamato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- swift
- Ambito
- desktop
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100