Improve JSON editor for iOS

This commit is contained in:
世界
2025-11-26 22:25:52 +08:00
parent 264c37218c
commit 3f1c716a0a
18 changed files with 3129 additions and 17 deletions
+2
View File
@@ -0,0 +1,2 @@
.build/
.swiftpm/
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "Runestone",
"repositoryURL": "https://github.com/simonbs/Runestone",
"state": {
"branch": null,
"revision": "1fad339aab99cf2136ce6bf8c32da3265b2e85e5",
"version": "0.5.1"
}
},
{
"package": "TreeSitter",
"repositoryURL": "https://github.com/tree-sitter/tree-sitter",
"state": {
"branch": null,
"revision": "98be227227af10cc7a269cb3ffb23686c0610b17",
"version": "0.20.9"
}
}
]
},
"version": 1
}
+23
View File
@@ -0,0 +1,23 @@
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "TreeSitterJSON5",
platforms: [
.iOS(.v14), .macCatalyst(.v14),
],
products: [
.library(name: "TreeSitterJSON5", targets: ["TreeSitterJSON5"]),
.library(name: "TreeSitterJSON5Queries", targets: ["TreeSitterJSON5Queries"]),
.library(name: "TreeSitterJSON5Runestone", targets: ["TreeSitterJSON5Runestone"]),
],
dependencies: [
.package(url: "https://github.com/simonbs/Runestone", from: "0.3.0"),
],
targets: [
.target(name: "TreeSitterJSON5", cSettings: [.headerSearchPath("src")]),
.target(name: "TreeSitterJSON5Queries", resources: [.copy("highlights.scm"), .copy("injections.scm")]),
.target(name: "TreeSitterJSON5Runestone", dependencies: ["Runestone", "TreeSitterJSON5", "TreeSitterJSON5Queries"]),
]
)
+3
View File
@@ -0,0 +1,3 @@
# TreeSitterJSON5
Copied from https://github.com/Lakr233/FlowDown
@@ -0,0 +1,10 @@
#ifdef __cplusplus
extern "C" {
#endif
typedef struct TSLanguage TSLanguage;
const TSLanguage *tree_sitter_json5(void);
#ifdef __cplusplus
}
#endif
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,223 @@
#ifndef TREE_SITTER_PARSER_H_
#define TREE_SITTER_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define ts_builtin_sym_error ((TSSymbol)-1)
#define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage;
#endif
typedef struct {
TSFieldId field_id;
uint8_t child_index;
bool inherited;
} TSFieldMapEntry;
typedef struct {
uint16_t index;
uint16_t length;
} TSFieldMapSlice;
typedef struct {
bool visible;
bool named;
bool supertype;
} TSSymbolMetadata;
typedef struct TSLexer TSLexer;
struct TSLexer {
int32_t lookahead;
TSSymbol result_symbol;
void (*advance)(TSLexer *, bool);
void (*mark_end)(TSLexer *);
uint32_t (*get_column)(TSLexer *);
bool (*is_at_included_range_start)(const TSLexer *);
bool (*eof)(const TSLexer *);
};
typedef enum {
TSParseActionTypeShift,
TSParseActionTypeReduce,
TSParseActionTypeAccept,
TSParseActionTypeRecover,
} TSParseActionType;
typedef union {
struct {
uint8_t type;
TSStateId state;
bool extra;
bool repetition;
} shift;
struct {
uint8_t type;
uint8_t child_count;
TSSymbol symbol;
int16_t dynamic_precedence;
uint16_t production_id;
} reduce;
uint8_t type;
} TSParseAction;
typedef struct {
uint16_t lex_state;
uint16_t external_lex_state;
} TSLexMode;
typedef union {
TSParseAction action;
struct {
uint8_t count;
bool reusable;
} entry;
} TSParseActionEntry;
struct TSLanguage {
uint32_t version;
uint32_t symbol_count;
uint32_t alias_count;
uint32_t token_count;
uint32_t external_token_count;
uint32_t state_count;
uint32_t large_state_count;
uint32_t production_id_count;
uint32_t field_count;
uint16_t max_alias_sequence_length;
const uint16_t *parse_table;
const uint16_t *small_parse_table;
const uint32_t *small_parse_table_map;
const TSParseActionEntry *parse_actions;
const char * const *symbol_names;
const char * const *field_names;
const TSFieldMapSlice *field_map_slices;
const TSFieldMapEntry *field_map_entries;
const TSSymbolMetadata *symbol_metadata;
const TSSymbol *public_symbol_map;
const uint16_t *alias_map;
const TSSymbol *alias_sequences;
const TSLexMode *lex_modes;
bool (*lex_fn)(TSLexer *, TSStateId);
bool (*keyword_lex_fn)(TSLexer *, TSStateId);
TSSymbol keyword_capture_token;
struct {
const bool *states;
const TSSymbol *symbol_map;
void *(*create)(void);
void (*destroy)(void *);
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
unsigned (*serialize)(void *, char *);
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
};
/*
* Lexer Macros
*/
#define START_LEXER() \
bool result = false; \
bool skip = false; \
bool eof = false; \
int32_t lookahead; \
goto start; \
next_state: \
lexer->advance(lexer, skip); \
start: \
skip = false; \
lookahead = lexer->lookahead;
#define ADVANCE(state_value) \
{ \
state = state_value; \
goto next_state; \
}
#define SKIP(state_value) \
{ \
skip = true; \
state = state_value; \
goto next_state; \
}
#define ACCEPT_TOKEN(symbol_value) \
result = true; \
lexer->result_symbol = symbol_value; \
lexer->mark_end(lexer);
#define END_STATE() return result;
/*
* Parse Table Macros
*/
#define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id
#define ACTIONS(id) id
#define SHIFT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value \
} \
}}
#define SHIFT_REPEAT(state_value) \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.state = state_value, \
.repetition = true \
} \
}}
#define SHIFT_EXTRA() \
{{ \
.shift = { \
.type = TSParseActionTypeShift, \
.extra = true \
} \
}}
#define REDUCE(symbol_val, child_count_val, ...) \
{{ \
.reduce = { \
.type = TSParseActionTypeReduce, \
.symbol = symbol_val, \
.child_count = child_count_val, \
__VA_ARGS__ \
}, \
}}
#define RECOVER() \
{{ \
.type = TSParseActionTypeRecover \
}}
#define ACCEPT_INPUT() \
{{ \
.type = TSParseActionTypeAccept \
}}
#ifdef __cplusplus
}
#endif
#endif // TREE_SITTER_PARSER_H_
@@ -0,0 +1,17 @@
import Foundation
public enum Query {
public static var highlightsFileURL: URL {
url(named: "highlights")
}
public static var injectionsFileURL: URL {
url(named: "injections")
}
}
private extension Query {
static func url(named filename: String) -> URL {
Bundle.module.url(forResource: filename, withExtension: "scm")!
}
}
@@ -0,0 +1,15 @@
(number) @number
(comment) @comment
(member name: (_) @property)
(member value: (string) @string)
(member value: (number) @number)
(member value: ["true" "false" "null"] @constant.builtin)
(array ((string) @string))
(array (number) @number)
(array ["true" "false" "null"] @constant.builtin)
(ERROR) @error
@@ -0,0 +1 @@
(comment) @comment
@@ -0,0 +1,7 @@
import Runestone
public extension TreeSitterIndentationScopes {
static var json5: TreeSitterIndentationScopes {
TreeSitterIndentationScopes(indent: ["object", "array"], outdent: ["}", "]"])
}
}
@@ -0,0 +1,11 @@
import Runestone
import TreeSitterJSON5
import TreeSitterJSON5Queries
public extension TreeSitterLanguage {
static var json5: TreeSitterLanguage {
let highlightsQuery = TreeSitterLanguage.Query(contentsOf: TreeSitterJSON5Queries.Query.highlightsFileURL)
let injectionsQuery = TreeSitterLanguage.Query(contentsOf: TreeSitterJSON5Queries.Query.injectionsFileURL)
return TreeSitterLanguage(tree_sitter_json5(), highlightsQuery: highlightsQuery, injectionsQuery: injectionsQuery, indentationScopes: .json5)
}
}