Skip to content

Commit 2525935

Browse files
committed
Fix line break mode in suggestion to be by char with a dirty solution
1 parent 42380a8 commit 2525935

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ struct CodeBlock: View {
107107
ForEach(0..<viewModel.suggestion.count, id: \.self) { index in
108108
Text("\(index + viewModel.startLineIndex + 1)")
109109
.foregroundColor(Color.white.opacity(0.6))
110-
Text(AttributedString(viewModel.suggestion[index]))
110+
BreakByCharText(viewModel.suggestion[index])
111111
.frame(maxWidth: .infinity, alignment: .leading)
112-
.multilineTextAlignment(.leading)
113-
.lineSpacing(4)
114112
}
115113
}
116114
.foregroundColor(.white)
@@ -234,3 +232,38 @@ struct SuggestionPanelView_Preview: PreviewProvider {
234232
}
235233
}
236234
}
235+
236+
// Super dirty hack to workaround that AttributedText doesn't support paragraphStyle.lineBreakMode
237+
struct BreakByCharText: View {
238+
let attributedString: NSAttributedString
239+
let attributedChars: [NSAttributedString]
240+
241+
init(_ attributedString: NSAttributedString) {
242+
self.attributedString = attributedString
243+
attributedChars = splitAttributedString(attributedString)
244+
}
245+
246+
var body: some View {
247+
LazyVGrid(
248+
columns: [.init(.adaptive(minimum: 8), spacing: 0, alignment: .leading)],
249+
alignment: .leading,
250+
spacing: 4
251+
) {
252+
ForEach(0..<attributedChars.endIndex, id: \.self) { index in
253+
Text(AttributedString(attributedChars[index]))
254+
}
255+
}
256+
}
257+
}
258+
259+
private func splitAttributedString(_ inputString: NSAttributedString) -> [NSAttributedString] {
260+
var output = [NSAttributedString]()
261+
var start = 0
262+
for char in inputString.string {
263+
let range = NSMakeRange(start, char.utf16.count)
264+
let attributedString = inputString.attributedSubstring(from: range)
265+
output.append(attributedString)
266+
start += range.length
267+
}
268+
return output
269+
}

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func highlighted(code: String, language: String) -> [NSAttributedString] {
376376
}
377377
}
378378

379-
func splitAttributedString(_ inputString: NSAttributedString) -> [NSAttributedString] {
379+
private func splitAttributedString(_ inputString: NSAttributedString) -> [NSAttributedString] {
380380
let input = inputString.string
381381
let separatedInput = input.components(separatedBy: "\n")
382382
var output = [NSAttributedString]()

0 commit comments

Comments
 (0)