From 2606ec38f447c0894a16a7b568ee9fc15a19ccac Mon Sep 17 00:00:00 2001 From: Thiago Holanda Date: Fri, 22 Nov 2024 17:04:59 +0100 Subject: [PATCH] Add support to HTML (#23) * Add TreeSitterHTML as dependency * Add HTML support * Make sure all NSTextStorage instances are plain text only * No need to import pre-defined UTIs --------- Co-authored-by: Matt <85322+mattmassicotte@users.noreply.github.com> --- Configurations/NonSwiftWorkaround.xcconfig | 1 + Dependencies/Package.swift | 2 ++ .../Sources/TreeSitterParsers/TreeSitterParsers.swift | 1 + .../Modules/SyntaxService/LanguageProfile+Profiles.swift | 9 +++++++++ Edit/Modules/SyntaxService/RootLanguage.swift | 6 ++++++ Edit/Modules/TextSystem/TextViewSystem.swift | 1 + EditIntents/HighlightIntent.swift | 1 + QuickLookPreview/Info.plist | 1 + 8 files changed, 22 insertions(+) diff --git a/Configurations/NonSwiftWorkaround.xcconfig b/Configurations/NonSwiftWorkaround.xcconfig index 6ee22ed..b921d47 100644 --- a/Configurations/NonSwiftWorkaround.xcconfig +++ b/Configurations/NonSwiftWorkaround.xcconfig @@ -15,6 +15,7 @@ OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DI OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterGoMod.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterGosum.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterGoWork.modulemap +OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterHTML.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterMarkdown.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterMarkdownInline.modulemap OTHER_SWIFT_FLAGS = $(inherited) -Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/TreeSitterOCaml.modulemap diff --git a/Dependencies/Package.swift b/Dependencies/Package.swift index 6f43b24..ca1521f 100644 --- a/Dependencies/Package.swift +++ b/Dependencies/Package.swift @@ -16,6 +16,7 @@ let package = Package( .package(url: "https://github.com/tree-sitter/tree-sitter-c", branch: "master"), .package(url: "https://github.com/mattmassicotte/tree-sitter-clojure", branch: "feature/spm"), .package(url: "https://github.com/elixir-lang/tree-sitter-elixir", branch: "main"), + .package(url: "https://github.com/tree-sitter/tree-sitter-html", branch: "master"), .package(url: "https://github.com/tree-sitter/tree-sitter-go", branch: "master"), .package(url: "https://github.com/camdencheek/tree-sitter-go-mod", branch: "main"), .package(url: "https://github.com/tree-sitter-grammars/tree-sitter-go-sum", branch: "master"), @@ -39,6 +40,7 @@ let package = Package( .product(name: "TreeSitterGoMod", package: "tree-sitter-go-mod"), .product(name: "TreeSitterGosum", package: "tree-sitter-go-sum"), .product(name: "TreeSitterGoWork", package: "tree-sitter-go-work"), + .product(name: "TreeSitterHTML", package: "tree-sitter-html"), .product(name: "TreeSitterMarkdown", package: "tree-sitter-markdown"), .product(name: "TreeSitterOCaml", package: "tree-sitter-ocaml"), .product(name: "TreeSitterPython", package: "tree-sitter-python"), diff --git a/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift b/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift index 07c80ea..91eb2ac 100644 --- a/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift +++ b/Dependencies/Sources/TreeSitterParsers/TreeSitterParsers.swift @@ -6,6 +6,7 @@ @_exported import TreeSitterGoMod @_exported import TreeSitterGosum @_exported import TreeSitterGoWork +@_exported import TreeSitterHTML @_exported import TreeSitterMarkdown @_exported import TreeSitterMarkdownInline @_exported import TreeSitterOCaml diff --git a/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift b/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift index f94ff92..b1fc508 100644 --- a/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift +++ b/Edit/Modules/SyntaxService/LanguageProfile+Profiles.swift @@ -32,6 +32,10 @@ extension LanguageProfile { return LanguageProfile.goSumProfile } + if utType.conforms(to: .html) { + return LanguageProfile.htmlProfile + } + if utType.conforms(to: .markdown) { return LanguageProfile.markdownProfile } @@ -109,6 +113,11 @@ extension LanguageProfile { language: Language(tree_sitter_gowork()) ) + static let htmlProfile = LanguageProfile( + RootLanguage.html, + language: Language(tree_sitter_html()) + ) + static let markdownProfile = LanguageProfile( RootLanguage.markdown, language: Language(tree_sitter_markdown()) diff --git a/Edit/Modules/SyntaxService/RootLanguage.swift b/Edit/Modules/SyntaxService/RootLanguage.swift index 021b410..2fd62e6 100644 --- a/Edit/Modules/SyntaxService/RootLanguage.swift +++ b/Edit/Modules/SyntaxService/RootLanguage.swift @@ -15,6 +15,7 @@ public enum RootLanguage: Hashable, CaseIterable, Sendable { case goMod case goSum case goWork + case html case markdown case ocaml case ocamlInterface @@ -33,6 +34,7 @@ public enum RootLanguage: Hashable, CaseIterable, Sendable { case .goMod: .goModFile case .goSum: .goSumFile case .goWork: .goWorkFile + case .html: .html case .markdown: .markdown case .ocaml: .ocamlSource case .ocamlInterface: .ocamlInterface @@ -67,6 +69,8 @@ extension RootLanguage: RawRepresentable { self = .goSum case "gowork": self = .goWork + case "html": + self = .html case "markdown": self = .markdown case "ocaml": @@ -104,6 +108,8 @@ extension RootLanguage: RawRepresentable { "Go Sum" case .goWork: "Go Work" + case .html: + "HTML" case .markdown: "Markdown" case .ocaml: diff --git a/Edit/Modules/TextSystem/TextViewSystem.swift b/Edit/Modules/TextSystem/TextViewSystem.swift index e6d72ef..087d23f 100644 --- a/Edit/Modules/TextSystem/TextViewSystem.swift +++ b/Edit/Modules/TextSystem/TextViewSystem.swift @@ -180,6 +180,7 @@ extension TextViewSystem { public func reload(from url: URL, attributes: [NSAttributedString.Key: Any]) throws { let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [ .defaultAttributes: attributes, + .documentType: NSAttributedString.DocumentType.plain, ] let storage = try TSYTextStorage(url: url, options: options, documentAttributes: nil) diff --git a/EditIntents/HighlightIntent.swift b/EditIntents/HighlightIntent.swift index 72fa512..91b00b3 100644 --- a/EditIntents/HighlightIntent.swift +++ b/EditIntents/HighlightIntent.swift @@ -18,6 +18,7 @@ extension RootLanguage: AppEnum { .goMod: "Go Mod", .goSum: "Go Sum", .goWork: "Go Work", + .html: "HTML", .markdown: "Markdown", .ocaml: "OCaml", .ocamlInterface: "OCaml Interface", diff --git a/QuickLookPreview/Info.plist b/QuickLookPreview/Info.plist index ec2d043..868ad95 100644 --- a/QuickLookPreview/Info.plist +++ b/QuickLookPreview/Info.plist @@ -20,6 +20,7 @@ dev.go.go-module dev.go.go-work dev.go.go-sum + public.html org.ocaml.ocaml org.ocaml.ocaml-interface public.python-script