Skip to content

Commit d777a44

Browse files
committed
Display spaces as dimmed middle dots
It also works as a workaround for linebreakMode not being supported.
1 parent 2525935 commit d777a44

2 files changed

Lines changed: 31 additions & 44 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ 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-
BreakByCharText(viewModel.suggestion[index])
110+
Text(AttributedString(viewModel.suggestion[index]))
111+
.foregroundColor(.white.opacity(0.1))
111112
.frame(maxWidth: .infinity, alignment: .leading)
113+
.multilineTextAlignment(.leading)
114+
.lineSpacing(4)
112115
}
113116
}
114117
.foregroundColor(.white)
@@ -232,38 +235,3 @@ struct SuggestionPanelView_Preview: PreviewProvider {
232235
}
233236
}
234237
}
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: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,48 @@ func highlighted(code: String, language: String) -> [NSAttributedString] {
362362
[.font: NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)],
363363
range: NSRange(location: 0, length: formatted.length)
364364
)
365-
return splitAttributedString(formatted)
365+
return convertToCodeLines(formatted)
366366
default:
367367
guard let highlighter = Highlightr() else {
368-
return splitAttributedString(NSAttributedString(string: code))
368+
return convertToCodeLines(NSAttributedString(string: code))
369369
}
370370
highlighter.setTheme(to: "atom-one-dark")
371371
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: 13, weight: .regular))
372372
guard let formatted = highlighter.highlight(code, as: "swift") else {
373-
return splitAttributedString(NSAttributedString(string: code))
373+
return convertToCodeLines(NSAttributedString(string: code))
374374
}
375-
return splitAttributedString(formatted)
375+
return convertToCodeLines(formatted)
376376
}
377377
}
378378

379-
private func splitAttributedString(_ inputString: NSAttributedString) -> [NSAttributedString] {
380-
let input = inputString.string
379+
private func convertToCodeLines(_ formatedCode: NSAttributedString) -> [NSAttributedString] {
380+
let input = formatedCode.string
381381
let separatedInput = input.components(separatedBy: "\n")
382382
var output = [NSAttributedString]()
383383
var start = 0
384384
for sub in separatedInput {
385385
let range = NSMakeRange(start, sub.utf16.count)
386-
let attributedString = inputString.attributedSubstring(from: range)
387-
output.append(attributedString)
386+
let attributedString = formatedCode.attributedSubstring(from: range)
387+
let mutable = NSMutableAttributedString(attributedString: attributedString)
388+
// use regex to replace all spaces to a middle dot
389+
do {
390+
let regex = try NSRegularExpression(pattern: "[ ]*", options: [])
391+
let result = regex.matches(
392+
in: mutable.string,
393+
range: NSRange(location: 0, length: mutable.mutableString.length)
394+
)
395+
for r in result {
396+
let range = r.range
397+
mutable.replaceCharacters(
398+
in: range,
399+
with: String(repeating: "·", count: range.length)
400+
)
401+
mutable.addAttributes([
402+
.foregroundColor: NSColor.white.withAlphaComponent(0.1),
403+
], range: range)
404+
}
405+
} catch {}
406+
output.append(mutable)
388407
start += range.length + 1
389408
}
390409
return output

0 commit comments

Comments
 (0)