-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to use code from ObjC #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,13 +34,13 @@ open class CodeAttributedString : NSTextStorage | |
{ | ||
/// Internal Storage | ||
let stringStorage = NSMutableAttributedString(string: "") | ||
|
||
/// Highlightr instace used internally for highlighting. Use this for configuring the theme. | ||
open let highlightr = Highlightr()! | ||
|
||
/// This object will be notified before and after the highlighting. | ||
open var highlightDelegate : HighlightDelegate? | ||
|
||
/// Initialize the CodeAttributedString | ||
public override init() | ||
{ | ||
|
@@ -66,7 +66,7 @@ open class CodeAttributedString : NSTextStorage | |
|
||
/// Language syntax to use for highlighting. Providing nil will disable highlighting. | ||
open var language : String? | ||
{ | ||
{ | ||
didSet | ||
{ | ||
highlight(NSMakeRange(0, stringStorage.length)) | ||
|
@@ -75,7 +75,7 @@ open class CodeAttributedString : NSTextStorage | |
|
||
/// Returns a standard String based on the current one. | ||
open override var string: String | ||
{ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the indentation style |
||
get | ||
{ | ||
return stringStorage.string | ||
|
@@ -132,7 +132,7 @@ open class CodeAttributedString : NSTextStorage | |
} | ||
} | ||
} | ||
|
||
func highlight(_ range: NSRange) | ||
{ | ||
if(language == nil) | ||
|
@@ -148,39 +148,39 @@ open class CodeAttributedString : NSTextStorage | |
return; | ||
} | ||
} | ||
|
||
|
||
let string = (self.string as NSString) | ||
let line = string.substring(with: range) | ||
DispatchQueue.global().async | ||
{ | ||
let tmpStrg = self.highlightr.highlight(line, as: self.language!) | ||
DispatchQueue.main.async(execute: { | ||
//Checks to see if this highlighting is still valid. | ||
if((range.location + range.length) > self.stringStorage.length) | ||
{ | ||
self.highlightDelegate?.didHighlight?(range, success: false) | ||
return; | ||
} | ||
|
||
if(tmpStrg?.string != self.stringStorage.attributedSubstring(from: range).string) | ||
{ | ||
self.highlightDelegate?.didHighlight?(range, success: false) | ||
return; | ||
} | ||
|
||
self.beginEditing() | ||
tmpStrg?.enumerateAttributes(in: NSMakeRange(0, (tmpStrg?.length)!), options: [], using: { (attrs, locRange, stop) in | ||
var fixedRange = NSMakeRange(range.location+locRange.location, locRange.length) | ||
fixedRange.length = (fixedRange.location + fixedRange.length < string.length) ? fixedRange.length : string.length-fixedRange.location | ||
fixedRange.length = (fixedRange.length >= 0) ? fixedRange.length : 0 | ||
self.stringStorage.setAttributes(attrs, range: fixedRange) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the indentation style |
||
let tmpStrg = self.highlightr.highlight(line, as: self.language!) | ||
DispatchQueue.main.async(execute: { | ||
//Checks to see if this highlighting is still valid. | ||
if((range.location + range.length) > self.stringStorage.length) | ||
{ | ||
self.highlightDelegate?.didHighlight?(range, success: false) | ||
return; | ||
} | ||
|
||
if(tmpStrg?.string != self.stringStorage.attributedSubstring(from: range).string) | ||
{ | ||
self.highlightDelegate?.didHighlight?(range, success: false) | ||
return; | ||
} | ||
|
||
self.beginEditing() | ||
tmpStrg?.enumerateAttributes(in: NSMakeRange(0, (tmpStrg?.length)!), options: [], using: { (attrs, locRange, stop) in | ||
var fixedRange = NSMakeRange(range.location+locRange.location, locRange.length) | ||
fixedRange.length = (fixedRange.location + fixedRange.length < string.length) ? fixedRange.length : string.length-fixedRange.location | ||
fixedRange.length = (fixedRange.length >= 0) ? fixedRange.length : 0 | ||
self.stringStorage.setAttributes(attrs, range: fixedRange) | ||
}) | ||
self.endEditing() | ||
self.edited(NSTextStorageEditActions.editedAttributes, range: range, changeInLength: 0) | ||
self.highlightDelegate?.didHighlight?(range, success: true) | ||
}) | ||
self.endEditing() | ||
self.edited(NSTextStorageEditActions.editedAttributes, range: range, changeInLength: 0) | ||
self.highlightDelegate?.didHighlight?(range, success: true) | ||
}) | ||
|
||
|
||
} | ||
|
||
} | ||
|
@@ -189,9 +189,27 @@ open class CodeAttributedString : NSTextStorage | |
{ | ||
highlightr.themeChanged = | ||
{ _ in | ||
self.highlight(NSMakeRange(0, self.stringStorage.length)) | ||
} | ||
self.highlight(NSMakeRange(0, self.stringStorage.length)) | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
extension CodeAttributedString { | ||
open func setTheme(to theme: String) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow Allman indentation style. |
||
self.highlightr.setTheme(to: theme) | ||
} | ||
open func supportedLanguages() -> [String] { | ||
return highlightr.supportedLanguages() | ||
} | ||
open func availableThemes() -> [String] { | ||
return highlightr.availableThemes() | ||
} | ||
open var theme: Theme { | ||
return highlightr.theme | ||
} | ||
open func update() { | ||
self.highlight(NSMakeRange(0, self.stringStorage.length)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep the indentation style