Skip to content

Commit

Permalink
Moved More Logic to Syntaxr
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethshawfriedman committed May 8, 2017
1 parent c74a9da commit 5881d74
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
Binary file not shown.
18 changes: 17 additions & 1 deletion Schemer-MacOS/Schemer/Syntaxr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Cocoa
class Syntaxr {

//for a given line (as a string), return a string formatter with proper highlighting
static func highlight(line:String) -> NSAttributedString {
static func highlightLine(_ line:String) -> NSAttributedString {
let result:NSMutableAttributedString = NSMutableAttributedString()
let semiColonLoc = line.range(of: ";")
if let scLoc = semiColonLoc {
Expand All @@ -30,4 +30,20 @@ class Syntaxr {
return result
}


//take all text from textview, seperate by line, and syntax highlight at the per line level
static func highlightAllText(_ text:String) -> NSAttributedString {
let lines = text.components(separatedBy: "\n")
let formattedText = NSMutableAttributedString()
for i in 0..<lines.count {
let line = lines[i]
let formattedLine = Syntaxr.highlightLine(line)
formattedText.append(formattedLine)
if (i != lines.count-1) { //append new line char, unless it's the last line
let aNewLine = NSAttributedString(string: "\n", attributes: CodeField.standardAtrributes())
formattedText.append(aNewLine)
}
}
return formattedText
}
}
16 changes: 3 additions & 13 deletions Schemer-MacOS/Schemer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,9 @@ class ViewController: NSViewController {

override func textStorageDidProcessEditing(_ notification: Notification) {
let textStorage = notification.object as! NSTextStorage
print(textStorage.string)
let lines = textStorage.string.components(separatedBy: "\n")
let allText = NSMutableAttributedString()
for i in 0..<lines.count {
let line = lines[i]
let formattedLine = Syntaxr.highlight(line:line)
allText.append(formattedLine)
if (i != lines.count-1) { //append new line char, unless it's the last line
let aNewLine = NSAttributedString(string: "\n", attributes: CodeField.standardAtrributes())
allText.append(aNewLine)
}
}
textStorage.setAttributedString(allText)
let allText = textStorage.string
let formattedText = Syntaxr.highlightAllText(allText)
textStorage.setAttributedString(formattedText)
}

}
Expand Down

0 comments on commit 5881d74

Please sign in to comment.