Skip to content

Commit

Permalink
Add source code editor (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojteklu authored Apr 2, 2019
1 parent 226d27e commit 42c7b35
Show file tree
Hide file tree
Showing 15 changed files with 1,184 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Logo/IDE/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
32 changes: 32 additions & 0 deletions Logo/IDE/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Logo</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 wojteklu. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
43 changes: 43 additions & 0 deletions Logo/IDE/LogoSyntaxTextView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Cocoa

class LogoSyntaxTextView: NSTextView {

override func awakeFromNib() {
font = NSFont(name: "Menlo", size: 15)
}

override func didChangeText() {
guard let textStorage = textStorage else { return }

textStorage.beginEditing()
setTextColor(.white, range:NSRange(location: 0, length: string.count))

colorText(regexString: "\\d",
color: NSColor(hexString: "d19a66"))

colorText(regexString: "([a-zA-Z_{1}][a-zA-Z0-9_]+)(?=\\()",
color: NSColor(hexString: "61aeee"))

colorText(regexString: "\\b(if|else|end|to|repeat)\\b",
color: NSColor(hexString: "c678dd"))

colorText(regexString: "[:]\\w+",
color: NSColor(hexString: "e6c07b"))

textStorage.endEditing()
}

private func colorText(regexString: String, color: NSColor) {
guard let regex = try? NSRegularExpression(pattern: regexString, options: []) else {
return
}

let matches = regex.matches(in: string,
options: [],
range: NSRange(location: 0, length: string.count))

for match in matches {
setTextColor(color, range: match.range)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions Logo/IDE/Resources/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "play-button.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
748 changes: 748 additions & 0 deletions Logo/IDE/Resources/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Logo/IDE/Resources/IDE.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
24 changes: 24 additions & 0 deletions Logo/IDE/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Cocoa

extension NSColor {
convenience init(hexString: String) {
var hex = hexString.hasPrefix("#") ? String(hexString.dropFirst()) : hexString

guard hex.count == 3 || hex.count == 6 else {
self.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 1)
return
}

if hex.count == 3 {
for (index, char) in hex.enumerated() {
hex.insert(char, at: hex.index(hex.startIndex, offsetBy: index * 2))
}
}

let number = Int(hex, radix: 16)!
let red = CGFloat((number >> 16) & 0xFF) / 255.0
let green = CGFloat((number >> 8) & 0xFF) / 255.0
let blue = CGFloat(number & 0xFF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1)
}
}
18 changes: 18 additions & 0 deletions Logo/IDE/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Cocoa
import Logo

class ViewController: NSViewController {
@IBOutlet private var codeTextView: LogoSyntaxTextView!
@IBOutlet private var outputImageView: NSImageView!
private let interpreter = Interpreter()

func evaluate() {
let code = codeTextView.string
guard code.count > 0 else { return }

DispatchQueue.main.async {
self.outputImageView.image = self.interpreter.run(code: code,
canvasSize: self.outputImageView.bounds.size)
}
}
}
8 changes: 8 additions & 0 deletions Logo/IDE/WindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Cocoa

class WindowController: NSWindowController {

@IBAction private func play(_ sender: Any) {
(window?.contentViewController as? ViewController)?.evaluate()
}
}
Loading

0 comments on commit 42c7b35

Please sign in to comment.