TOML highlighting !52

merged merged by cmc on 2026-10-05 21:03 UTC · krz/orgstar:toml-highlighting into main

8 files changed, +64 −2

Layout: unified · split

Package.resolved +10 −1
@@ -1,5 +1,5 @@
11{
2 "originHash" : "03b8c85ddc87e40eb8da5020efd09eeaa81aa04770e7ba4cb19fce3b7e5bcf77",
2 "originHash" : "99f611db4aa216eecc3254d58a7ad6ee060533c7f899b1a0d0f72c21a75f90a9",
33 "pins" : [
44 {
55 "identity" : "grdb.swift",
@@ -154,6 +154,15 @@
154154 "version" : "0.24.7-1"
155155 }
156156 },
157 {
158 "identity" : "tree-sitter-toml",
159 "kind" : "remoteSourceControl",
160 "location" : "https://github.com/tree-sitter-grammars/tree-sitter-toml",
161 "state" : {
162 "revision" : "64b56832c2cffe41758f28e05c756a3a98d16f41",
163 "version" : "0.7.0"
164 }
165 },
157166 {
158167 "identity" : "tree-sitter-yaml",
159168 "kind" : "remoteSourceControl",
Package.swift +2
@@ -33,6 +33,7 @@ let package = Package(
3333 .package(url: "https://github.com/tree-sitter/tree-sitter-ruby", exact: "0.23.1"),
3434 .package(url: "https://github.com/tree-sitter/tree-sitter-rust", exact: "0.24.2"),
3535 .package(url: "https://github.com/6cdh/tree-sitter-scheme", exact: "0.24.7-1"),
36 .package(url: "https://github.com/tree-sitter-grammars/tree-sitter-toml", exact: "0.7.0"),
3637 .package(url: "https://github.com/tree-sitter-grammars/tree-sitter-yaml", exact: "0.7.2")
3738 ],
3839 targets: [
@@ -67,6 +68,7 @@ let package = Package(
6768 .product(name: "TreeSitterRuby", package: "tree-sitter-ruby"),
6869 .product(name: "TreeSitterRust", package: "tree-sitter-rust"),
6970 .product(name: "TreeSitterScheme", package: "tree-sitter-scheme"),
71 .product(name: "TreeSitterTOML", package: "tree-sitter-toml"),
7072 .product(name: "TreeSitterYAML", package: "tree-sitter-yaml")
7173 ]
7274 ),
Sources/OrgHighlight/Queries.swift +17
@@ -2295,6 +2295,23 @@ enum HighlightQueries {
22952295 (block_comment)
22962296 (directive)] @comment
22972297 """####,
2298 "toml": ####"""
2299 ; TOML highlights for Orgstar: table names as types, keys as properties. The grammar's own
2300 ; query captures whole pairs as properties, which colors values too.
2301 (comment) @comment
2302 (string) @string
2303 (quoted_key) @string
2304 [(integer) (float)] @number
2305 (boolean) @constant
2306 [(offset_date_time) (local_date_time) (local_date) (local_time)] @string.special
2307 (table (bare_key) @type)
2308 (table (dotted_key (bare_key) @type))
2309 (table_array_element (bare_key) @type)
2310 (table_array_element (dotted_key (bare_key) @type))
2311 (pair (bare_key) @property)
2312 (pair (dotted_key (bare_key) @property))
2313 "=" @operator
2314 """####,
22982315 "yaml": ####"""
22992316 (boolean_scalar) @boolean
23002317
Sources/OrgHighlight/TreeSitterHighlighter.swift +3
@@ -17,6 +17,7 @@ import TreeSitterR
1717import TreeSitterRuby
1818import TreeSitterRust
1919import TreeSitterScheme
20import TreeSitterTOML
2021import TreeSitterYAML
2122
2223/// Src block highlighting with tree-sitter grammars and their own highlight queries.
@@ -51,6 +52,7 @@ public final class TreeSitterHighlighter: CodeHighlighter, @unchecked Sendable {
5152 case "ruby": "ruby"
5253 case "json": "json"
5354 case "yaml", "yml": "yaml"
55 case "toml", "conf-toml": "toml"
5456 case "lua": "lua"
5557 default: nil
5658 }
@@ -74,6 +76,7 @@ public final class TreeSitterHighlighter: CodeHighlighter, @unchecked Sendable {
7476 case "ruby": tree_sitter_ruby()
7577 case "rust": tree_sitter_rust()
7678 case "scheme": tree_sitter_scheme()
79 case "toml": tree_sitter_toml()
7780 case "yaml": tree_sitter_yaml()
7881 default: nil
7982 }
Sources/Orgstar/FileTypes.swift +1
@@ -44,6 +44,7 @@ enum FileTypes {
4444 case "rb": return "ruby"
4545 case "json": return "json"
4646 case "yaml", "yml": return "yaml"
47 case "toml": return "toml"
4748 case "lua": return "lua"
4849 default:
4950 return ["zshrc", ".zshrc", "bashrc", ".bashrc", ".profile"].contains(url.lastPathComponent) ? "sh" : nil
Tests/OrgHighlightTests/HighlightTests.swift +11 −1
@@ -17,7 +17,7 @@ struct HighlightTests {
1717 }
1818
1919 @Test func everyGrammarLoads() {
20 for language in ["sh", "python", "emacs-lisp", "c", "cpp", "R", "js", "java", "scheme", "clojure", "haskell", "rust", "go", "ruby", "json", "yaml", "lua"] {
20 for language in ["sh", "python", "emacs-lisp", "c", "cpp", "R", "js", "java", "scheme", "clojure", "haskell", "rust", "go", "ruby", "json", "yaml", "lua", "toml"] {
2121 #expect(!highlighter.highlights(language: language, code: "x").isEmpty || true, "\(language)")
2222 let code = language == "json" ? "{\"a\": 1}" : "# comment\n"
2323 _ = highlighter.highlights(language: language, code: code)
@@ -39,6 +39,16 @@ struct HighlightTests {
3939 #expect(lisp[";; c"] == .comment)
4040 }
4141
42 @Test func toml() {
43 let found = categories("toml", "# settings\n[theme]\nfont-size = 13\nshow = true\nname = \"x\"\n")
44 #expect(found["# settings"] == .comment)
45 #expect(found["theme"] == .type)
46 #expect(found["font-size"] == .property)
47 #expect(found["13"] == .number)
48 #expect(found["true"] == .constant)
49 #expect(found["\"x\""] == .string)
50 }
51
4252 @Test func unknownLanguagesGetNothing() {
4353 #expect(highlighter.highlights(language: "cobol", code: "MOVE A TO B").isEmpty)
4454 }
scripts/queries/toml.scm added +15
@@ -0,0 +1,15 @@
1; TOML highlights for Orgstar: table names as types, keys as properties. The grammar's own
2; query captures whole pairs as properties, which colors values too.
3(comment) @comment
4(string) @string
5(quoted_key) @string
6[(integer) (float)] @number
7(boolean) @constant
8[(offset_date_time) (local_date_time) (local_date) (local_time)] @string.special
9(table (bare_key) @type)
10(table (dotted_key (bare_key) @type))
11(table_array_element (bare_key) @type)
12(table_array_element (dotted_key (bare_key) @type))
13(pair (bare_key) @property)
14(pair (dotted_key (bare_key) @property))
15"=" @operator
scripts/update-highlight-queries.sh +5
@@ -25,6 +25,8 @@ QUERIES = {
2525 "ruby": [("tree-sitter/tree-sitter-ruby", "v0.23.1", "queries/highlights.scm")],
2626 "rust": [("tree-sitter/tree-sitter-rust", "v0.24.2", "queries/highlights.scm")],
2727 "scheme": [("6cdh/tree-sitter-scheme", "v0.24.7-1", "queries/highlights.scm")],
28 # Orgstar's own query, in scripts/queries.
29 "toml": [("local", None, "scripts/queries/toml.scm")],
2830 "yaml": [("tree-sitter-grammars/tree-sitter-yaml", "v0.7.2", "queries/highlights.scm")],
2931}
3032
@@ -34,6 +36,9 @@ out = ["// Generated by scripts/update-highlight-queries.sh from the grammars' o
3436for language, files in sorted(QUERIES.items()):
3537 text = ""
3638 for repo, ref, path in files:
39 if repo == "local":
40 text += open(path, encoding="utf-8").read() + "\n"
41 continue
3742 url = f"https://raw.githubusercontent.com/{repo}/{ref}/{path}"
3843 text += urllib.request.urlopen(url).read().decode("utf-8") + "\n"
3944 assert '"""####' not in text