From a7ba9e0547c90fffda539c9f563633c74b37ec2b Mon Sep 17 00:00:00 2001 From: Matt <85322+mattmassicotte@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:43:20 -0500 Subject: [PATCH] TypeScript support --- Configurations/NonSwiftWorkaround.xcconfig | 1 + Dependencies/Package.swift | 2 ++ .../TreeSitterParsers/TreeSitterParsers.swift | 1 + .../xcshareddata/swiftpm/Package.resolved | 11 ++++++- .../LanguageProfile+Profiles.swift | 16 ++++++++-- Edit/Modules/SyntaxService/RootLanguage.swift | 6 ++++ .../Modules/SyntaxService/SyntaxService.swift | 1 + Edit/Resources/Info.plist | 30 +++++++++++++++++++ EditIntents/HighlightIntent.swift | 1 + QuickLookPreview/Info.plist | 1 + 10 files changed, 66 insertions(+), 4 deletions(-) diff --git a/Configurations/NonSwiftWorkaround.xcconfig b/Configurations/NonSwiftWorkaround.xcconfig index eb5edc7..5b73b45 100644 --- a/Configurations/NonSwiftWorkaround.xcconfig +++ b/Configurations/NonSwiftWorkaround.xcconfig @@ -25,4 +25,5 @@ OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DI OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterPython.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterRuby.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterRust.modulemap +OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterTypeScript.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterSwift.modulemap diff --git a/Dependencies/Package.swift b/Dependencies/Package.swift index 21b0e05..a097ed5 100644 --- a/Dependencies/Package.swift +++ b/Dependencies/Package.swift @@ -29,6 +29,7 @@ let package = Package( .package(url: "https://github.com/tree-sitter/tree-sitter-python", branch: "master"), .package(url: "https://github.com/tree-sitter/tree-sitter-ruby", branch: "master"), .package(url: "https://github.com/tree-sitter/tree-sitter-rust", branch: "master"), + .package(url: "https://github.com/tree-sitter/tree-sitter-typescript", branch: "master"), .package(url: "https://github.com/alex-pinkus/tree-sitter-swift", branch: "with-generated-files"), ], targets: [ @@ -52,6 +53,7 @@ let package = Package( .product(name: "TreeSitterPython", package: "tree-sitter-python"), .product(name: "TreeSitterRuby", package: "tree-sitter-ruby"), .product(name: "TreeSitterRust", package: "tree-sitter-rust"), + .product(name: "TreeSitterTypeScript", package: "tree-sitter-typescript"), .product(name: "TreeSitterSwift", package: "tree-sitter-swift"), ] ), diff --git a/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift b/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift index ea01607..2b4d60d 100644 --- a/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift +++ b/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift @@ -16,4 +16,5 @@ @_exported import TreeSitterPython @_exported import TreeSitterRuby @_exported import TreeSitterRust +@_exported import TreeSitterTypeScript @_exported import TreeSitterSwift diff --git a/Edit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Edit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index e4ea11f..7510340 100644 --- a/Edit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Edit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "2c533b6dafa208983e2c3996abe8ef2b3eb75356e0c996ea3d9ee4ef4da06719", + "originHash" : "a425ffff3c05722e5d50ef8ce30dc82d4392cf636f759db35d39aac4e657e178", "pins" : [ { "identity" : "asyncxpcconnection", @@ -411,6 +411,15 @@ "revision" : "eda05af7ac41adb4eb19c346883c0fa32fe3bdd8" } }, + { + "identity" : "tree-sitter-typescript", + "kind" : "remoteSourceControl", + "location" : "https://github.com/tree-sitter/tree-sitter-typescript", + "state" : { + "branch" : "master", + "revision" : "45af49560396d67a710b21a3ce315fcaa09006ff" + } + }, { "identity" : "windowtreatment", "kind" : "remoteSourceControl", diff --git a/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift b/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift index 4f1d9b6..c5b63d5 100644 --- a/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift +++ b/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift @@ -80,6 +80,11 @@ extension LanguageProfile { return LanguageProfile.swiftProfile } + // special-case this type to allow the same extension to be used by default + if utType.conforms(to: .typescriptSource) || utType.conforms(to: .mpeg2TransportStream) { + return LanguageProfile.typeScriptProfile + } + return LanguageProfile.genericProfile } } @@ -177,14 +182,19 @@ extension LanguageProfile { language: Language(tree_sitter_ruby()) ) + static let rustProfile = LanguageProfile( + RootLanguage.rust, + language: Language(tree_sitter_rust()) + ) + static let swiftProfile = LanguageProfile( RootLanguage.swift, language: Language(tree_sitter_swift()) ) - static let rustProfile = LanguageProfile( - RootLanguage.rust, - language: Language(tree_sitter_rust()) + static let typeScriptProfile = LanguageProfile( + RootLanguage.typeScript, + language: Language(tree_sitter_typescript()) ) static let genericProfile = LanguageProfile( diff --git a/Edit/Modules/SyntaxService/RootLanguage.swift b/Edit/Modules/SyntaxService/RootLanguage.swift index 4af54e5..5359a17 100644 --- a/Edit/Modules/SyntaxService/RootLanguage.swift +++ b/Edit/Modules/SyntaxService/RootLanguage.swift @@ -26,6 +26,7 @@ public enum RootLanguage: Hashable, CaseIterable, Sendable { case ruby case rust case swift + case typeScript var typeIdentifier: UTType { switch self { @@ -47,6 +48,7 @@ public enum RootLanguage: Hashable, CaseIterable, Sendable { case .python: .pythonScript case .ruby: .rubyScript case .rust: .rustSource + case .typeScript: .typescriptSource case .swift: .swiftSource } } @@ -97,6 +99,8 @@ extension RootLanguage: RawRepresentable { self = .rust case "swift": self = .swift + case "typescript": + self = .typeScript default: return nil } @@ -142,6 +146,8 @@ extension RootLanguage: RawRepresentable { "Rust" case .swift: "Swift" + case .typeScript: + "TypeScript" } } } diff --git a/Edit/Modules/SyntaxService/SyntaxService.swift b/Edit/Modules/SyntaxService/SyntaxService.swift index 3691402..3de7472 100644 --- a/Edit/Modules/SyntaxService/SyntaxService.swift +++ b/Edit/Modules/SyntaxService/SyntaxService.swift @@ -171,6 +171,7 @@ extension SyntaxService { let queryParams = try self.highlightsQueryParams(for: range) let namedRanges = try await client.highlightsProvider.async(queryParams) + print("names:", namedRanges) return TokenApplication(namedRanges: namedRanges, range: range) } catch { self.logger.warning("Failed to get highlighting: \(error)") diff --git a/Edit/Resources/Info.plist b/Edit/Resources/Info.plist index 075c62b..0b54eaa 100644 --- a/Edit/Resources/Info.plist +++ b/Edit/Resources/Info.plist @@ -292,6 +292,36 @@ + + UTTypeConformsTo + + public.source-code + + UTTypeIdentifier + org.typescriptlang.source + UTTypeTagSpecification + + public.filename-extension + + ts + + + + + UTTypeConformsTo + + public.source-code + + UTTypeIdentifier + org.typescriptlang.jsx-source + UTTypeTagSpecification + + public.filename-extension + + tsx + + + diff --git a/EditIntents/HighlightIntent.swift b/EditIntents/HighlightIntent.swift index 1153b12..046ca68 100644 --- a/EditIntents/HighlightIntent.swift +++ b/EditIntents/HighlightIntent.swift @@ -29,6 +29,7 @@ extension RootLanguage: AppEnum { .ruby: "Ruby", .rust: "Rust", .swift: "Swift", + .typeScript: "TypeScript", ] } } diff --git a/QuickLookPreview/Info.plist b/QuickLookPreview/Info.plist index 505b84d..6cbf879 100644 --- a/QuickLookPreview/Info.plist +++ b/QuickLookPreview/Info.plist @@ -32,6 +32,7 @@ public.shell-script public.swift-source com.netscape.javascript-source + org.typescriptlang.source QLSupportsSearchableItems