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

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
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-write</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CodeEditSourceEditorExampleApp.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//

import SwiftUI

@main
struct CodeEditSourceEditorExampleApp: App {
var body: some Scene {
DocumentGroup(newDocument: CodeEditSourceEditorExampleDocument()) { file in
ContentView(document: file.$document, fileURL: file.fileURL)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CodeEditSourceEditorExampleDocument.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//

import SwiftUI
import UniformTypeIdentifiers

struct CodeEditSourceEditorExampleDocument: FileDocument {
var text: String

init(text: String = "") {
self.text = text
}

static var readableContentTypes: [UTType] {
[
.sourceCode,
.plainText,
.delimitedText,
.script
]
}

init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}

func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = text.data(using: .utf8)!
return .init(regularFileWithContents: data)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// EditorTheme+Default.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//

import Foundation
import AppKit
import CodeEditSourceEditor

extension EditorTheme {
static var standard: EditorTheme {
EditorTheme(
text: .init(hex: "000000"),
insertionPoint: .init(hex: "000000"),
invisibles: .init(hex: "D6D6D6"),
background: .init(hex: "FFFFFF"),
lineHighlight: .init(hex: "ECF5FF"),
selection: .init(hex: "B2D7FF"),
keywords: .init(hex: "9B2393"),
commands: .init(hex: "326D74"),
types: .init(hex: "0B4F79"),
attributes: .init(hex: "815F03"),
variables: .init(hex: "0F68A0"),
values: .init(hex: "6C36A9"),
numbers: .init(hex: "1C00CF"),
strings: .init(hex: "C41A16"),
characters: .init(hex: "1C00CF"),
comments: .init(hex: "267507")
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// NSColor+Hex.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//

import AppKit

extension NSColor {

/// Initializes a `NSColor` from a HEX String (e.g.: `#1D2E3F`) and an optional alpha value.
/// - Parameters:
/// - hex: A String of a HEX representation of a color (format: `#1D2E3F`)
/// - alpha: A Double indicating the alpha value from `0.0` to `1.0`
convenience init(hex: String, alpha: Double = 1.0) {
let hex = hex.trimmingCharacters(in: .alphanumerics.inverted)
var int: UInt64 = 0
Scanner(string: hex).scanHexInt64(&int)
self.init(hex: Int(int), alpha: alpha)
}

/// Initializes a `NSColor` from an Int (e.g.: `0x1D2E3F`)and an optional alpha value.
/// - Parameters:
/// - hex: An Int of a HEX representation of a color (format: `0x1D2E3F`)
/// - alpha: A Double indicating the alpha value from `0.0` to `1.0`
convenience init(hex: Int, alpha: Double = 1.0) {
let red = (hex >> 16) & 0xFF
let green = (hex >> 8) & 0xFF
let blue = hex & 0xFF
self.init(srgbRed: Double(red) / 255, green: Double(green) / 255, blue: Double(blue) / 255, alpha: alpha)
}

/// Returns an Int representing the `NSColor` in hex format (e.g.: 0x112233)
var hex: Int {
guard let components = cgColor.components, components.count >= 3 else { return 0 }

let red = lround((Double(components[0]) * 255.0)) << 16
let green = lround((Double(components[1]) * 255.0)) << 8
let blue = lround((Double(components[2]) * 255.0))

return red | green | blue
}

/// Returns a HEX String representing the `NSColor` (e.g.: #112233)
var hexString: String {
let color = self.hex

return "#" + String(format: "%06x", color)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// String+Lines.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 2/24/24.
//

import Foundation

extension String {
/// Calculates the first `n` lines and returns them as a new string.
/// - Parameters:
/// - lines: The number of lines to return.
/// - maxLength: The maximum number of characters to copy.
/// - Returns: A new string containing the lines.
func getFirstLines(_ lines: Int = 1, maxLength: Int = 512) -> String {
var string = ""
var foundLines = 0
var totalLength = 0
for char in self.lazy {
if char.isNewline {
foundLines += 1
}
totalLength += 1
if foundLines >= lines || totalLength >= maxLength {
break
}
string.append(char)
}
return string
}

/// Calculates the last `n` lines and returns them as a new string.
/// - Parameters:
/// - lines: The number of lines to return.
/// - maxLength: The maximum number of characters to copy.
/// - Returns: A new string containing the lines.
func getLastLines(_ lines: Int = 1, maxLength: Int = 512) -> String {
var string = ""
var foundLines = 0
var totalLength = 0
for char in self.lazy.reversed() {
if char.isNewline {
foundLines += 1
}
totalLength += 1
if foundLines >= lines || totalLength >= maxLength {
break
}
string = String(char) + string
}
return string
}
}
Loading