Skip to content

Commit 66ec3a9

Browse files
authored
Pretty-print JSON cells by default without marking them changed (#1412)
* feat(datagrid): pretty-print JSON cells by default without marking them changed * fix(datagrid): keep JSON viewer popover on screen and restore syntax font * refactor(datagrid): render JSON with shared tree-sitter editor; vendor tree-sitter-json grammar * fix(datagrid): seed JSON editor text at init so content shows on open * fix(datagrid): cap JSON parser recursion depth to prevent stack overflow on deeply nested input * refactor(datagrid): open JSON cells in the resizable window instead of a popover * Revert "refactor(datagrid): open JSON cells in the resizable window instead of a popover" This reverts commit 2b25cb6.
1 parent b01ed37 commit 66ec3a9

30 files changed

Lines changed: 1885 additions & 870 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Click the page indicator in the pagination bar to jump to a specific page. (#1364)
2121
- Pagination now appears for filtered tables whose total row count is unknown, so you can page through them instead of seeing only the first page. (#1364)
2222
- First Page and Last Page keyboard actions, unbound by default and assignable in Settings > Keyboard. (#1364)
23+
- JSON and JSONB cells now display pretty-printed by default, keeping your original key order and exact numbers. Viewing or reformatting a value no longer marks the row as changed, and saving no longer reorders keys or rounds large integers.
2324

2425
### Fixed
2526

LocalPackages/CodeEditLanguages/Sources/CodeEditLanguages/CodeLanguage+Definitions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public extension CodeLanguage {
1313
static let allLanguages: [CodeLanguage] = [
1414
.bash,
1515
.javascript,
16+
.json,
1617
.jsx,
1718
.sql
1819
]
@@ -67,6 +68,15 @@ public extension CodeLanguage {
6768
highlights: ["highlights-jsx", "injections"]
6869
)
6970

71+
/// A language structure for `JSON`
72+
static let json: CodeLanguage = .init(
73+
id: .json,
74+
tsName: "json",
75+
extensions: ["json"],
76+
lineCommentString: "",
77+
rangeCommentStrings: ("", "")
78+
)
79+
7080
/// A language structure for `SQL`
7181
static let sql: CodeLanguage = .init(
7282
id: .sql,

LocalPackages/CodeEditLanguages/Sources/CodeEditLanguages/CodeLanguage.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ public struct CodeLanguage {
8282
.appendingPathComponent("Resources/tree-sitter-\(tsName)/\(highlights).scm")
8383
}
8484

85-
/// Gets the TSLanguage from `tree-sitter` — only SQL, Bash, and JavaScript are supported
85+
/// Gets the TSLanguage from `tree-sitter`. Only SQL, Bash, JavaScript, and JSON are supported
8686
private var tsLanguage: OpaquePointer? {
8787
switch id {
8888
case .bash:
8989
return tree_sitter_bash()
9090
case .javascript, .jsx:
9191
return tree_sitter_javascript()
92+
case .json:
93+
return tree_sitter_json()
9294
case .sql:
9395
return tree_sitter_sql()
9496
default:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(pair
2+
key: (_) @string.special.key)
3+
4+
(string) @string
5+
6+
(number) @number
7+
8+
[
9+
(null)
10+
(true)
11+
(false)
12+
] @constant.builtin
13+
14+
(escape_sequence) @escape
15+
16+
(comment) @comment

LocalPackages/CodeEditLanguages/Sources/CodeEditLanguages/TreeSitterLanguage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum TreeSitterLanguage: String {
1313
case html
1414
case javascript
1515
case jsdoc
16+
case json
1617
case jsx
1718
case python
1819
case ruby

LocalPackages/CodeEditLanguages/Sources/CodeEditLanguages/TreeSitterModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class TreeSitterModel {
2525
return javascriptQuery
2626
case .jsx:
2727
return jsxQuery
28+
case .json:
29+
return jsonQuery
2830
case .sql:
2931
return sqlQuery
3032
default:
@@ -47,6 +49,11 @@ public class TreeSitterModel {
4749
return queryFor(.jsx)
4850
}()
4951

52+
/// Query for `JSON` files.
53+
public private(set) lazy var jsonQuery: Query? = {
54+
return queryFor(.json)
55+
}()
56+
5057
/// Query for `SQL` files.
5158
public private(set) lazy var sqlQuery: Query? = {
5259
return queryFor(.sql)

LocalPackages/CodeEditLanguages/Sources/TreeSitterGrammars/include/TreeSitterGrammars.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ typedef struct TSLanguage TSLanguage;
66
const TSLanguage *tree_sitter_sql(void);
77
const TSLanguage *tree_sitter_bash(void);
88
const TSLanguage *tree_sitter_javascript(void);
9+
const TSLanguage *tree_sitter_json(void);
910

1011
#endif /* TreeSitterGrammars_h */

0 commit comments

Comments
 (0)