-
Notifications
You must be signed in to change notification settings - Fork 165
Create new target for common code #1331
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
Merged
d-ronnqvist
merged 13 commits into
swiftlang:main
from
d-ronnqvist:create-target-for-common-code
Nov 14, 2025
+44
−5
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
17a2037
Extract the SourceLanguage type to a new "Common" target
d-ronnqvist b413e1d
Extract the Lock type to the same "Common" target
d-ronnqvist 294c79d
Merge branch 'main' into create-target-for-common-code
d-ronnqvist 72254fa
Keep public type aliases to moved types
d-ronnqvist 50e20de
Merge branch 'main' into create-target-for-common-code
d-ronnqvist 04d9cc1
Move CMake file into target source directory
d-ronnqvist f40ce48
Move Synchronized type back into SwiftDocC for now
d-ronnqvist 04a7964
Use Swift 6 language mode in new target
d-ronnqvist 9dd4757
Merge branch 'main' into create-target-for-common-code
d-ronnqvist 6614f2f
Rename file that aliases types from the common target
d-ronnqvist 275bb42
Add back repeated whitespace in otherwise unmodified file
d-ronnqvist 20a6252
Add "DocC" prefix to new common target
d-ronnqvist 544f1d0
Merge branch 'main' into create-target-for-common-code
d-ronnqvist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| This source file is part of the Swift.org open source project | ||
|
|
||
| Copyright (c) 2021-2025 Apple Inc. and the Swift project authors | ||
| Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
|
||
| See https://swift.org/LICENSE.txt for license information | ||
| See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| */ | ||
|
|
||
| /// A programming language. | ||
| public struct SourceLanguage: Hashable, Codable, Comparable, Sendable { | ||
| /// The display name of the programming language. | ||
| public var name: String | ||
| /// A globally unique identifier for the language. | ||
| public var id: String | ||
| /// Aliases for the language's identifier. | ||
| public var idAliases: [String] = [] | ||
| /// The identifier to use for link disambiguation purposes. | ||
| public var linkDisambiguationID: String | ||
|
|
||
| /// Creates a new language with a given name and identifier. | ||
| /// - Parameters: | ||
| /// - name: The display name of the programming language. | ||
| /// - id: A globally unique identifier for the language. | ||
| /// - idAliases: Aliases for the language's identifier. | ||
| /// - linkDisambiguationID: The identifier to use for link disambiguation purposes. | ||
| public init(name: String, id: String, idAliases: [String] = [], linkDisambiguationID: String? = nil) { | ||
| self.name = name | ||
| self.id = id | ||
| self.idAliases = idAliases | ||
| self.linkDisambiguationID = linkDisambiguationID ?? id | ||
| } | ||
|
|
||
| /// Finds the programming language that matches a given identifier, or creates a new one if it finds no existing language. | ||
| /// - Parameter id: The identifier of the programming language. | ||
| public init(id: String) { | ||
| switch id { | ||
| case "swift": self = .swift | ||
| case "occ", "objc", "objective-c", "c": self = .objectiveC | ||
| // FIXME: DocC should display C++ and Objective-C++ as their own languages (https://github.com/swiftlang/swift-docc/issues/767) | ||
| case "occ++", "objc++", "objective-c++", "c++": self = .objectiveC | ||
| case "javascript": self = .javaScript | ||
| case "data": self = .data | ||
| case "metal": self = .metal | ||
| default: | ||
| self.name = id | ||
| self.id = id | ||
| self.linkDisambiguationID = id | ||
| } | ||
| } | ||
|
|
||
| /// Finds the programming language that matches a given display name, or creates a new one if it finds no existing language. | ||
| /// | ||
| /// - Parameter name: The display name of the programming language. | ||
| public init(name: String) { | ||
| if let knownLanguage = SourceLanguage.firstKnownLanguage(withName: name) { | ||
| self = knownLanguage | ||
| } else { | ||
| self.name = name | ||
|
|
||
| let id = name.lowercased() | ||
| self.id = id | ||
| self.linkDisambiguationID = id | ||
| } | ||
| } | ||
|
|
||
| /// Finds the programming language that matches a given display name. | ||
| /// | ||
| /// If the language name doesn't match any known language, this initializer returns `nil`. | ||
| /// | ||
| /// - Parameter knownLanguageName: The display name of the programming language. | ||
| public init?(knownLanguageName: String) { | ||
| if let knownLanguage = SourceLanguage.firstKnownLanguage(withName: knownLanguageName) { | ||
| self = knownLanguage | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| /// Finds the programming language that matches a given identifier. | ||
| /// | ||
| /// If the language identifier doesn't match any known language, this initializer returns `nil`. | ||
| /// | ||
| /// - Parameter knownLanguageIdentifier: The identifier name of the programming language. | ||
| public init?(knownLanguageIdentifier: String) { | ||
| if let knownLanguage = SourceLanguage.firstKnownLanguage(withIdentifier: knownLanguageIdentifier) { | ||
| self = knownLanguage | ||
| } else { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| private static func firstKnownLanguage(withName name: String) -> SourceLanguage? { | ||
| SourceLanguage.knownLanguages.first { $0.name.lowercased() == name.lowercased() } | ||
| } | ||
|
|
||
| private static func firstKnownLanguage(withIdentifier id: String) -> SourceLanguage? { | ||
| SourceLanguage.knownLanguages.first { knownLanguage in | ||
| ([knownLanguage.id] + knownLanguage.idAliases) | ||
| .map { $0.lowercased() } | ||
| .contains(id) | ||
| } | ||
| } | ||
|
|
||
| /// The Swift programming language. | ||
| public static let swift = SourceLanguage(name: "Swift", id: "swift") | ||
|
|
||
| /// The Objective-C programming language. | ||
| public static let objectiveC = SourceLanguage( | ||
| name: "Objective-C", | ||
| id: "occ", | ||
| idAliases: [ | ||
| "objective-c", | ||
| "objc", | ||
| "c", // FIXME: DocC should display C as its own language (github.com/swiftlang/swift-docc/issues/169). | ||
| "c++", // FIXME: DocC should display C++ and Objective-C++ as their own languages (https://github.com/swiftlang/swift-docc/issues/767) | ||
| "objective-c++", | ||
| "objc++", | ||
| "occ++", | ||
| ], | ||
| linkDisambiguationID: "c" | ||
| ) | ||
|
|
||
| /// The JavaScript programming language or another language that conforms to the ECMAScript specification. | ||
| public static let javaScript = SourceLanguage(name: "JavaScript", id: "javascript") | ||
| /// Miscellaneous data, that's not a programming language. | ||
| /// | ||
| /// For example, use this to represent JSON or XML content. | ||
| public static let data = SourceLanguage(name: "Data", id: "data") | ||
| /// The Metal programming language. | ||
| public static let metal = SourceLanguage(name: "Metal", id: "metal") | ||
|
|
||
| /// The list of programming languages that are known to DocC. | ||
| public static let knownLanguages: [SourceLanguage] = [.swift, .objectiveC, .javaScript, .data, .metal] | ||
|
|
||
| enum CodingKeys: CodingKey { | ||
| case name | ||
| case id | ||
| case idAliases | ||
| case linkDisambiguationID | ||
| } | ||
|
|
||
| public init(from decoder: any Decoder) throws { | ||
| let container = try decoder.container(keyedBy: SourceLanguage.CodingKeys.self) | ||
|
|
||
| let name = try container.decode(String.self, forKey: SourceLanguage.CodingKeys.name) | ||
| let id = try container.decode(String.self, forKey: SourceLanguage.CodingKeys.id) | ||
| let idAliases = try container.decodeIfPresent([String].self, forKey: SourceLanguage.CodingKeys.idAliases) ?? [] | ||
| let linkDisambiguationID = try container.decodeIfPresent(String.self, forKey: SourceLanguage.CodingKeys.linkDisambiguationID) | ||
|
|
||
| self.init(name: name, id: id, idAliases: idAliases, linkDisambiguationID: linkDisambiguationID) | ||
| } | ||
|
|
||
| public func encode(to encoder: any Encoder) throws { | ||
| var container = encoder.container(keyedBy: SourceLanguage.CodingKeys.self) | ||
|
|
||
| try container.encode(self.name, forKey: SourceLanguage.CodingKeys.name) | ||
| try container.encode(self.id, forKey: SourceLanguage.CodingKeys.id) | ||
| if !self.idAliases.isEmpty { | ||
| try container.encode(self.idAliases, forKey: SourceLanguage.CodingKeys.idAliases) | ||
| } | ||
| try container.encode(self.linkDisambiguationID, forKey: SourceLanguage.CodingKeys.linkDisambiguationID) | ||
| } | ||
|
|
||
| public static func < (lhs: SourceLanguage, rhs: SourceLanguage) -> Bool { | ||
| // Sort Swift before other languages. | ||
| if lhs == .swift { | ||
| return true | ||
| } else if rhs == .swift { | ||
| return false | ||
| } | ||
| // Otherwise, sort by ID for a stable order. | ||
| return lhs.id < rhs.id | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.