mirror of
https://github.com/TelegramMessenger/Telegram-iOS.git
synced 2026-07-06 03:33:41 +02:00
Code highlight improvements
This commit is contained in:
parent
6a7f071252
commit
82281662e1
10 changed files with 656 additions and 178 deletions
|
|
@ -538,3 +538,41 @@ public func asyncUpdateMessageSyntaxHighlight(engine: TelegramEngine, messageId:
|
|||
}
|
||||
|> runOn(messageSyntaxHighlightQueue)
|
||||
}
|
||||
|
||||
public func asyncStanaloneSyntaxHighlight(current: CachedMessageSyntaxHighlight?, specs: [CachedMessageSyntaxHighlight.Spec]) -> Signal<CachedMessageSyntaxHighlight, NoError> {
|
||||
if let current, !specs.contains(where: { current.values[$0] == nil }) {
|
||||
return .single(current)
|
||||
}
|
||||
|
||||
return Signal { subscriber in
|
||||
var updated: [CachedMessageSyntaxHighlight.Spec: MessageSyntaxHighlight] = [:]
|
||||
|
||||
let theme = SyntaxterTheme(dark: false, textColor: .black, textFont: internalFixedCodeFont, italicFont: internalFixedCodeFont, mediumFont: internalFixedCodeFont)
|
||||
|
||||
for spec in specs {
|
||||
if let value = current?.values[spec] {
|
||||
updated[spec] = value
|
||||
} else {
|
||||
var entities: [MessageSyntaxHighlight.Entity] = []
|
||||
|
||||
if let syntaxHighlighter {
|
||||
if let highlightedString = syntaxHighlighter.syntax(spec.text, language: spec.language, theme: theme) {
|
||||
highlightedString.enumerateAttribute(.foregroundColor, in: NSRange(location: 0, length: highlightedString.length), using: { value, subRange, _ in
|
||||
if let value = value as? UIColor, value != .black {
|
||||
entities.append(MessageSyntaxHighlight.Entity(color: Int32(bitPattern: value.rgb), range: subRange.lowerBound ..< subRange.upperBound))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
updated[spec] = MessageSyntaxHighlight(entities: entities)
|
||||
}
|
||||
}
|
||||
|
||||
subscriber.putNext(CachedMessageSyntaxHighlight(values: updated))
|
||||
subscriber.putCompletion()
|
||||
|
||||
return EmptyDisposable
|
||||
}
|
||||
|> runOn(messageSyntaxHighlightQueue)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue