Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 53 additions & 35 deletions Pod/Classes/CodeAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -66,7 +66,7 @@ open class CodeAttributedString : NSTextStorage

/// Language syntax to use for highlighting. Providing nil will disable highlighting.
open var language : String?
{
{
Copy link
Owner

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

didSet
{
highlight(NSMakeRange(0, stringStorage.length))
Expand All @@ -75,7 +75,7 @@ open class CodeAttributedString : NSTextStorage

/// Returns a standard String based on the current one.
open override var string: String
{
{
Copy link
Owner

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

get
{
return stringStorage.string
Expand Down Expand Up @@ -132,7 +132,7 @@ open class CodeAttributedString : NSTextStorage
}
}
}

func highlight(_ range: NSRange)
{
if(language == nil)
Expand All @@ -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)
{
Copy link
Owner

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

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)
})


}

}
Expand All @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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))
}
}
4 changes: 3 additions & 1 deletion Pod/Classes/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private typealias RPThemeDict = [String:[String:AnyObject]]
private typealias RPThemeStringDict = [String:[String:String]]

/// Theme parser, can be used to configure the theme parameters.
open class Theme {
open class Theme: NSObject {
internal let theme : String
internal var lightTheme : String!

Expand All @@ -52,6 +52,7 @@ open class Theme {
init(themeString: String)
{
theme = themeString
super.init()
setCodeFont(RPFont(name: "Courier", size: 14)!)
strippedTheme = stripTheme(themeString)
lightTheme = strippedThemeToString(strippedTheme)
Expand Down Expand Up @@ -86,6 +87,7 @@ open class Theme {

- parameter font: UIFont (iOS or tvOS) or NSFont (OSX)
*/
@nonobjc
open func setCodeFont(_ font: RPFont)
{
codeFont = font
Expand Down