Skip to content

Commit

Permalink
New RootLanguage type to make this all slightly safer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 15, 2024
1 parent 913c69e commit 8171a41
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
6 changes: 6 additions & 0 deletions Edit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@
C9CDEB552B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CDEB542B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift */; };
C9CDEB8F2B56ACA3009E252A /* MainOffender in Frameworks */ = {isa = PBXBuildFile; productRef = C9CDEB8E2B56ACA3009E252A /* MainOffender */; };
C9CFCF752BF4CBE80053C9A6 /* TreeSitterOCaml in Frameworks */ = {isa = PBXBuildFile; productRef = C9CFCF742BF4CBE80053C9A6 /* TreeSitterOCaml */; };
C9CFCF7B2BF4D0890053C9A6 /* RootLanguage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFCF7A2BF4D0890053C9A6 /* RootLanguage.swift */; };
C9CFCF7C2BF4D19B0053C9A6 /* RootLanguage.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CFCF7A2BF4D0890053C9A6 /* RootLanguage.swift */; };
C9DCE1502BD7BFF30067D354 /* ThemeStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9DCE14F2BD7BFF30067D354 /* ThemeStore.swift */; };
C9DCE1562BD8049F0067D354 /* ObservableState.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9DCE1552BD8049F0067D354 /* ObservableState.swift */; };
C9E0B9F72BD1A3CE007AF034 /* DiagnosticsStatusBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9E0B9F62BD1A3CE007AF034 /* DiagnosticsStatusBarItem.swift */; };
Expand Down Expand Up @@ -1028,6 +1030,7 @@
C9C2133A2AD1FD6F00C60282 /* Framework.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Framework.xcconfig; sourceTree = "<group>"; };
C9CDEB542B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TextSystem+SwiftTreeSitterLayer.swift"; sourceTree = "<group>"; };
C9CDEB872B55B889009E252A /* LanguageData */ = {isa = PBXFileReference; lastKnownFileType = folder; path = LanguageData; sourceTree = "<group>"; };
C9CFCF7A2BF4D0890053C9A6 /* RootLanguage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootLanguage.swift; sourceTree = "<group>"; };
C9DCE14F2BD7BFF30067D354 /* ThemeStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeStore.swift; sourceTree = "<group>"; };
C9DCE1552BD8049F0067D354 /* ObservableState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableState.swift; sourceTree = "<group>"; };
C9E0B9F62BD1A3CE007AF034 /* DiagnosticsStatusBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiagnosticsStatusBarItem.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1587,6 +1590,7 @@
C9EA095C2B4D695400732203 /* LanguageDataStore.swift */,
C9EA09562B4D62B800732203 /* LanguageProfile.swift */,
C9EA095A2B4D645F00732203 /* LanguageProfile+Profiles.swift */,
C9CFCF7A2BF4D0890053C9A6 /* RootLanguage.swift */,
C9B8AA532B37057C00C79606 /* SyntaxService.swift */,
C9CDEB542B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift */,
);
Expand Down Expand Up @@ -3098,6 +3102,7 @@
files = (
C93F724B2BF26BDC0021ACF3 /* EditIntentsExtension.swift in Sources */,
C93F724D2BF26BDC0021ACF3 /* HighlightIntent.swift in Sources */,
C9CFCF7C2BF4D19B0053C9A6 /* RootLanguage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3200,6 +3205,7 @@
C9EA09572B4D62B800732203 /* LanguageProfile.swift in Sources */,
C9EA095D2B4D695400732203 /* LanguageDataStore.swift in Sources */,
C9CDEB552B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift in Sources */,
C9CFCF7B2BF4D0890053C9A6 /* RootLanguage.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 6 additions & 9 deletions Edit/Modules/SyntaxService/LanguageDataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ public final class LanguageDataStore {
}

extension LanguageDataStore {
private static func normalizeLanguageName(_ identifier: String) -> String {
identifier.lowercased().replacingOccurrences(of: "-", with: "_")
}

private static func languageDocumentType(from identifier: String) -> UTType {
let name = Self.normalizeLanguageName(identifier)
if let lang = RootLanguage(rawValue: identifier) {
return lang.typeIdentifier
}

// use the same normalization rules, but check for non-root languages
let name = RootLanguage.normalizeLanguageName(identifier)

switch name {
case "swift":
return .swiftSource
case "markdown":
return .markdown
case "markdown_inline":
return .markdownInline
default:
Expand Down
10 changes: 5 additions & 5 deletions Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ extension LanguageProfile {

extension LanguageProfile {
static let goProfile = LanguageProfile(
name: "Go",
RootLanguage.go,
language: Language(tree_sitter_go())
)

static let markdownProfile = LanguageProfile(
name: "Markdown",
RootLanguage.markdown,
language: Language(tree_sitter_markdown())
)

Expand All @@ -53,17 +53,17 @@ extension LanguageProfile {
)

static let ocamlProfile = LanguageProfile(
name: "OCaml",
RootLanguage.ocaml,
language: Language(tree_sitter_ocaml())
)

static let ocamlInterfaceProfile = LanguageProfile(
name: "OCaml",
RootLanguage.ocaml,
language: Language(tree_sitter_ocaml_interface())
)

static let swiftProfile = LanguageProfile(
name: "Swift",
RootLanguage.swift,
language: Language(tree_sitter_swift())
)

Expand Down
9 changes: 9 additions & 0 deletions Edit/Modules/SyntaxService/LanguageProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public struct LanguageProfile: Sendable {
self.language = language
self.bundleName = bundleName
}

public init(_ rootLanguage: RootLanguage, language: SwiftTreeSitter.Language?) {
let name = rootLanguage.rawValue
let bundleName = "TreeSitter\(name)_TreeSitter\(name)"

self.name = name
self.language = language
self.bundleName = bundleName
}
}

extension LanguageProfile {
Expand Down
55 changes: 55 additions & 0 deletions Edit/Modules/SyntaxService/RootLanguage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Foundation
import UniformTypeIdentifiers

/// Describes a document root language.
///
/// This type must also be compiled into EditIntents, because the AppIntent infrastructure depends on having the type visible within the module.
public enum RootLanguage: Hashable, CaseIterable, Sendable {
case go
case markdown
case ocaml
case swift

var typeIdentifier: UTType {
switch self {
case .go: .goSource
case .markdown: .markdown
case .ocaml: .ocamlSource
case .swift: .swiftSource
}
}
}

extension RootLanguage: RawRepresentable {
public static func normalizeLanguageName(_ identifier: String) -> String {
identifier.lowercased().replacingOccurrences(of: "-", with: "_")
}

public init?(rawValue: String) {
switch Self.normalizeLanguageName(rawValue) {
case "go":
self = .go
case "markdown":
self = .markdown
case "ocaml":
self = .ocaml
case "swift":
self = .swift
default:
return nil
}
}

public var rawValue: String {
switch self {
case .go:
"Go"
case .markdown:
"Markdown"
case .ocaml:
"OCaml"
case .swift:
"Swift"
}
}
}
19 changes: 0 additions & 19 deletions EditIntents/HighlightIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ import Theme
import TreeSitterClient
import UniformTypeIdentifiers

/// All types that are supported by EditIntents.
///
/// This must be defined within the same module as any AppIntent that uses it.
public enum RootLanguage: String, Hashable, CaseIterable, Sendable {
case go = "Go"
case markdown = "Markdown"
case ocaml = "OCaml"
case swift = "Swift"

var typeIdentifier: UTType {
switch self {
case .go: .goSource
case .markdown: .markdown
case .ocaml: .ocamlSource
case .swift: .swiftSource
}
}
}

extension RootLanguage: AppEnum {
public static var typeDisplayRepresentation: TypeDisplayRepresentation { "Language" }
public static var caseDisplayRepresentations: [RootLanguage: DisplayRepresentation] {
Expand Down

0 comments on commit 8171a41

Please sign in to comment.