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

Fix concurrency issues for Swift 6 #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.4
// swift-tools-version: 6.0

/**
* Splash
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Grammar/Grammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
/// Protocol used to define the grammar of a language to use for
/// syntax highlighting. See `SwiftGrammar` for a default implementation
/// of the Swift language grammar.
public protocol Grammar {
public protocol Grammar: Sendable {
/// The set of characters that make up the delimiters that separates
/// tokens within the language, such as punctuation characters. You
/// can control whether delimiters should be merged when forming
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Output/AttributedStringOutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public extension AttributedStringOutputFormat {
}

private extension NSMutableAttributedString {
func append(_ string: String, font: Font.Loaded, color: Color) {
func append(_ string: String, font: Font.LoadedFont, color: Color) {
let attributedString = NSAttributedString(string: string, attributes: [
.foregroundColor: color,
.font: font
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Output/OutputFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/// NSAttributedString outputs, and custom ones can be defined by
/// conforming to this protocol and passing the implementation to a
/// syntax highlighter when it's created.
public protocol OutputFormat {
public protocol OutputFormat: Sendable {
/// The type of builder that this output format uses. The builder's
/// `Output` type determines the output type of the format.
associatedtype Builder: OutputBuilder
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Syntax/SyntaxHighlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
/// To initialize this class, pass the desired output format, such as
/// `AttributedStringOutputFormat` or `HTMLOutputFormat`, or a custom
/// implementation. One syntax highlighter may be reused multiple times.
public struct SyntaxHighlighter<Format: OutputFormat> {
public struct SyntaxHighlighter<Format: OutputFormat>: Sendable {
private let format: Format
private let grammar: Grammar
private let tokenizer = Tokenizer()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Syntax/SyntaxRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
/// evaluated, is asked to check whether it matches a given segment
/// of code. If the rule matches then the rule's token type will be
/// associated with the given segment's current token.
public protocol SyntaxRule {
public protocol SyntaxRule: Sendable {
/// The token type that this syntax rule represents
var tokenType: TokenType { get }

Expand Down
48 changes: 29 additions & 19 deletions Sources/Splash/Theming/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import Foundation
/// Since Splash aims to be cross-platform, it uses this
/// simplified font representation rather than `NSFont`
/// or `UIFont`.
public struct Font {
public struct Font: Sendable {
/// The underlying resource used to load the font
public var resource: Resource
/// The size (in points) of the font
public var size: Double

/// Initialize an instance with a path to a font file
Expand All @@ -34,42 +33,53 @@ public struct Font {
}

public extension Font {
/// Sendable representation of a font
struct FontRepresentation: Sendable {
let name: String
let size: Double

init(name: String, size: Double) {
self.name = name
self.size = size
}
}

/// Enum describing how to load the underlying resource for a font
enum Resource {
enum Resource: Sendable {
/// Use an appropriate system font
case system
/// Use a pre-loaded font
case preloaded(Loaded)
case preloaded(FontRepresentation)
/// Load a font file from a given file system path
case path(String)
}
}

internal extension Font {
func load() -> Loaded {
func load() -> LoadedFont {
switch resource {
case .system:
return loadDefaultFont()
case .preloaded(let font):
return font
case .path(let path):
return load(fromPath: path) ?? loadDefaultFont()
case .system:
return loadDefaultFont()
case .preloaded(let fontRepresentation):
return LoadedFont(name: fontRepresentation.name, size: CGFloat(fontRepresentation.size)) ?? loadDefaultFont()
case .path(let path):
return load(fromPath: path) ?? loadDefaultFont()
}
}

private func loadDefaultFont() -> Loaded {
let font: Loaded?
private func loadDefaultFont() -> LoadedFont {
let font: LoadedFont?

#if os(iOS)
#if os(iOS)
font = UIFont(name: "Menlo-Regular", size: CGFloat(size))
#else
#else
font = load(fromPath: "/Library/Fonts/Courier New.ttf")
#endif
#endif

return font ?? .systemFont(ofSize: CGFloat(size))
}

private func load(fromPath path: String) -> Loaded? {
private func load(fromPath path: String) -> LoadedFont? {
guard
let url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path as CFString, .cfurlposixPathStyle, false),
let provider = CGDataProvider(url: url),
Expand All @@ -89,15 +99,15 @@ internal extension Font {
import UIKit

public extension Font {
typealias Loaded = UIFont
typealias LoadedFont = UIFont
}

#elseif os(macOS)

import Cocoa

public extension Font {
typealias Loaded = NSFont
typealias LoadedFont = NSFont
}

#endif
9 changes: 6 additions & 3 deletions Sources/Splash/Theming/Theme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
* MIT license - see LICENSE.md
*/

import Foundation

#if !os(Linux)
#if os(iOS)
import UIKit
#elseif os(macOS)
import Cocoa
#endif

/// A theme describes what fonts and colors to use when rendering
/// certain output formats - such as `NSAttributedString`. Several
/// default implementations are provided - see Theme+Defaults.swift.
public struct Theme {
public struct Theme: Sendable {
/// What font to use to render the highlighted text
public var font: Font
/// What color to use for plain text (no highlighting)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Splash/Tokenizing/TokenType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Foundation

/// Enum defining the possible types of tokens that can be highlighted
public enum TokenType: Hashable {
public enum TokenType: Hashable, Sendable {
/// A keyword, such as `if`, `class`, `let` or attributes such as @available
case keyword
/// A token that is part of a string literal
Expand Down