@@ -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