Skip to content

Commit 59aa89c

Browse files
committed
Fix updating of the messages binding
1 parent ca3a409 commit 59aa89c

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

Sources/CodeEditorView/CodeEditor.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public struct CodeEditor {
140140
public class _Coordinator {
141141
@Binding fileprivate var text: String
142142
@Binding fileprivate var position: Position
143+
@Binding fileprivate var messages: Set<TextLocated<Message>>
143144

144145
fileprivate var setActions: SetActions
145146
fileprivate var setInfo: SetInfo
@@ -168,11 +169,13 @@ public struct CodeEditor {
168169

169170
init(text: Binding<String>,
170171
position: Binding<Position>,
172+
messages: Binding<Set<TextLocated<Message>>>,
171173
setAction: SetActions,
172174
setInfo: SetInfo)
173175
{
174176
self._text = text
175177
self._position = position
178+
self._messages = messages
176179
self.setActions = setAction
177180
self.setInfo = setInfo
178181
}
@@ -182,11 +185,13 @@ public struct CodeEditor {
182185
///
183186
func updateBindings(text: Binding<String>,
184187
position: Binding<Position>,
188+
messages: Binding<Set<TextLocated<Message>>>,
185189
setAction: SetActions,
186190
setInfo: SetInfo)
187191
{
188192
self._text = text
189193
self._position = position
194+
self._messages = messages
190195
self.setActions = setAction
191196
self.setInfo = setInfo
192197
}
@@ -586,7 +591,7 @@ extension CodeEditor: UIViewRepresentable {
586591
indentation: indentationConfiguration,
587592
theme: context.environment.codeEditorTheme,
588593
setText: setText(_:),
589-
setMessages: { messages = $0 })
594+
setMessages: { context.coordinator.messages = $0 })
590595

591596
// NB: We are not setting `codeView.text` here. That will happen via `updateUIView(:)`.
592597
// This implies that we must take care to not report that initial updates as a change to any connected language
@@ -632,6 +637,7 @@ extension CodeEditor: UIViewRepresentable {
632637

633638
context.coordinator.updateBindings(text: $text,
634639
position: $position,
640+
messages: $messages,
635641
setAction: definitiveSetActions,
636642
setInfo: definitiveSetInfo)
637643
if codeView.lastMessages != messages { codeView.update(messages: messages) }
@@ -672,7 +678,11 @@ extension CodeEditor: UIViewRepresentable {
672678
}
673679

674680
public func makeCoordinator() -> Coordinator {
675-
return Coordinator(text: $text, position: $position, setAction: definitiveSetActions, setInfo: definitiveSetInfo)
681+
return Coordinator(text: $text,
682+
position: $position,
683+
messages: $messages,
684+
setAction: definitiveSetActions,
685+
setInfo: definitiveSetInfo)
676686
}
677687

678688
public final class Coordinator: _Coordinator {
@@ -729,13 +739,14 @@ extension CodeEditor: NSViewRepresentable {
729739
scrollView.autoresizingMask = [.width, .height]
730740

731741
// Set up text view with gutter
742+
// NB: Update with `setMessages` must go via the coordinator to work with view updates changing the binding.
732743
let codeView = CodeView(frame: CGRect(x: 0, y: 0, width: 100, height: 40),
733744
with: language,
734745
viewLayout: definitiveLayout,
735746
indentation: indentationConfiguration,
736747
theme: context.environment.codeEditorTheme,
737748
setText: setText(_:),
738-
setMessages: { messages = $0 })
749+
setMessages: { context.coordinator.messages = $0 })
739750
codeView.isVerticallyResizable = true
740751
codeView.isHorizontallyResizable = false
741752
codeView.autoresizingMask = .width
@@ -832,6 +843,7 @@ extension CodeEditor: NSViewRepresentable {
832843

833844
context.coordinator.updateBindings(text: $text,
834845
position: $position,
846+
messages: $messages,
835847
setAction: definitiveSetActions,
836848
setInfo: definitiveSetInfo)
837849
if codeView.lastMessages != messages { codeView.update(messages: messages) }
@@ -889,7 +901,11 @@ extension CodeEditor: NSViewRepresentable {
889901
}
890902

891903
public func makeCoordinator() -> Coordinator {
892-
return Coordinator(text: $text, position: $position, setAction: definitiveSetActions, setInfo: definitiveSetInfo)
904+
return Coordinator(text: $text,
905+
position: $position,
906+
messages: $messages,
907+
setAction: definitiveSetActions,
908+
setInfo: definitiveSetInfo)
893909
}
894910

895911
public final class Coordinator: _Coordinator {

0 commit comments

Comments
 (0)