@innovatorved/code-lab 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/ARCHITECTURE.md +8 -4
  2. package/CHANGELOG.md +45 -0
  3. package/README.md +70 -158
  4. package/dist/apps/web/src/index.d.ts +34 -0
  5. package/dist/code-lab-sql.css +482 -0
  6. package/dist/code-lab-sql.d.ts +179 -0
  7. package/dist/code-lab-sql.esm.js +542 -0
  8. package/dist/code-lab-sql.umd.js +531 -0
  9. package/dist/code-lab.css +298 -2
  10. package/dist/code-lab.d.ts +110 -15
  11. package/dist/code-lab.esm.js +35284 -18253
  12. package/dist/code-lab.js +40434 -23431
  13. package/dist/code-lab.umd.js +65502 -0
  14. package/dist/diagnostics.d.ts +5 -0
  15. package/dist/extensions/cpp.esm.js +19532 -3772
  16. package/dist/extensions/cpp.js +19529 -3769
  17. package/dist/extensions/guided-lab.esm.js +19401 -3725
  18. package/dist/extensions/guided-lab.js +19400 -3724
  19. package/dist/extensions/python.d.ts +1 -0
  20. package/dist/extensions/python.esm.js +19666 -3843
  21. package/dist/extensions/python.js +19668 -3845
  22. package/dist/extensions/umbrella.d.ts +5 -0
  23. package/dist/index.d.ts +1 -1
  24. package/dist/modular-kernel.d.ts +11 -0
  25. package/dist/packages/commands/src/index.d.ts +96 -0
  26. package/dist/packages/core/src/di.d.ts +36 -0
  27. package/dist/packages/core/src/disposable.d.ts +27 -0
  28. package/dist/packages/core/src/errors.d.ts +69 -0
  29. package/dist/packages/core/src/events.d.ts +38 -0
  30. package/dist/packages/core/src/index.d.ts +6 -0
  31. package/dist/packages/core/src/path.d.ts +14 -0
  32. package/dist/packages/core/src/registry.d.ts +23 -0
  33. package/dist/packages/editor/src/index.d.ts +118 -0
  34. package/dist/packages/fs/src/backends.d.ts +37 -0
  35. package/dist/packages/fs/src/index.d.ts +3 -0
  36. package/dist/packages/fs/src/types.d.ts +40 -0
  37. package/dist/packages/fs/src/vfs-service.d.ts +33 -0
  38. package/dist/packages/preview/src/index.d.ts +34 -0
  39. package/dist/packages/runtime/src/index.d.ts +47 -0
  40. package/dist/packages/settings/src/builtin-schemas.d.ts +15 -0
  41. package/dist/packages/settings/src/configuration-service.d.ts +100 -0
  42. package/dist/packages/settings/src/index.d.ts +4 -0
  43. package/dist/packages/settings/src/jsonc.d.ts +20 -0
  44. package/dist/packages/settings/src/schema.d.ts +28 -0
  45. package/dist/packages/sql/src/completions.d.ts +8 -0
  46. package/dist/packages/sql/src/diagnostics.d.ts +2 -0
  47. package/dist/packages/sql/src/formatter.d.ts +2 -0
  48. package/dist/packages/sql/src/index.d.ts +7 -0
  49. package/dist/packages/sql/src/sql-editor.d.ts +2 -0
  50. package/dist/packages/sql/src/styles.d.ts +2 -0
  51. package/dist/packages/sql/src/tokenizer.d.ts +13 -0
  52. package/dist/packages/sql/src/types.d.ts +130 -0
  53. package/dist/packages/terminal/src/index.d.ts +54 -0
  54. package/dist/packages/ui/src/index.d.ts +4 -0
  55. package/dist/packages/ui/src/observability.d.ts +22 -0
  56. package/dist/packages/ui/src/plugin-registry.d.ts +89 -0
  57. package/dist/packages/ui/src/settings-dialog.d.ts +25 -0
  58. package/dist/packages/ui/src/themes.d.ts +35 -0
  59. package/dist/preview-sw.js +82 -24
  60. package/dist/runtime.d.ts +3 -3
  61. package/dist/runtime.esm.js +503 -73
  62. package/dist/runtime.js +501 -73
  63. package/dist/schemas.d.ts +234 -814
  64. package/dist/schemas.esm.js +19401 -3725
  65. package/dist/schemas.js +19404 -3728
  66. package/dist/scm.d.ts +13 -2
  67. package/dist/search.d.ts +5 -1
  68. package/dist/shell.d.ts +1 -1
  69. package/dist/shell.esm.js +435 -54
  70. package/dist/shell.js +435 -54
  71. package/dist/terminal.esm.js +242 -54
  72. package/dist/terminal.js +242 -54
  73. package/dist/workbench/diff-editor.d.ts +1 -0
  74. package/dist/workbench/index.d.ts +1 -1
  75. package/dist/workbench/styles.d.ts +1 -1
  76. package/dist/zip.d.ts +4 -3
  77. package/package.json +37 -8
  78. package/packages/commands/README.md +7 -0
  79. package/packages/commands/package.json +16 -0
  80. package/packages/commands/src/index.ts +467 -0
  81. package/packages/commands/tsconfig.json +22 -0
  82. package/packages/core/README.md +12 -0
  83. package/packages/core/package.json +13 -0
  84. package/packages/core/src/di.ts +147 -0
  85. package/packages/core/src/disposable.ts +112 -0
  86. package/packages/core/src/errors.ts +205 -0
  87. package/packages/core/src/events.ts +157 -0
  88. package/packages/core/src/index.ts +49 -0
  89. package/packages/core/src/path.ts +118 -0
  90. package/packages/core/src/registry.ts +61 -0
  91. package/packages/core/tsconfig.json +19 -0
  92. package/packages/editor/README.md +9 -0
  93. package/packages/editor/package.json +18 -0
  94. package/packages/editor/src/index.ts +344 -0
  95. package/packages/editor/tsconfig.json +24 -0
  96. package/packages/fs/README.md +7 -0
  97. package/packages/fs/package.json +16 -0
  98. package/packages/fs/src/backends.ts +301 -0
  99. package/packages/fs/src/index.ts +18 -0
  100. package/packages/fs/src/types.ts +44 -0
  101. package/packages/fs/src/vfs-service.ts +257 -0
  102. package/packages/fs/tsconfig.json +22 -0
  103. package/packages/preview/README.md +3 -0
  104. package/packages/preview/package.json +16 -0
  105. package/packages/preview/src/index.ts +125 -0
  106. package/packages/preview/tsconfig.json +22 -0
  107. package/packages/runtime/README.md +3 -0
  108. package/packages/runtime/package.json +17 -0
  109. package/packages/runtime/src/index.ts +134 -0
  110. package/packages/runtime/tsconfig.json +23 -0
  111. package/packages/settings/README.md +7 -0
  112. package/packages/settings/package.json +16 -0
  113. package/packages/settings/src/builtin-schemas.ts +235 -0
  114. package/packages/settings/src/configuration-service.ts +543 -0
  115. package/packages/settings/src/index.ts +37 -0
  116. package/packages/settings/src/jsonc.ts +189 -0
  117. package/packages/settings/src/schema.ts +164 -0
  118. package/packages/settings/tsconfig.json +22 -0
  119. package/packages/sql/README.md +12 -0
  120. package/packages/sql/package.json +13 -0
  121. package/packages/sql/src/completions.ts +359 -0
  122. package/packages/sql/src/diagnostics.ts +200 -0
  123. package/packages/sql/src/formatter.ts +271 -0
  124. package/packages/sql/src/index.ts +54 -0
  125. package/packages/sql/src/sql-editor.ts +802 -0
  126. package/packages/sql/src/styles.ts +501 -0
  127. package/packages/sql/src/tokenizer.ts +457 -0
  128. package/packages/sql/src/types.ts +148 -0
  129. package/packages/sql/tsconfig.json +19 -0
  130. package/packages/terminal/README.md +3 -0
  131. package/packages/terminal/package.json +16 -0
  132. package/packages/terminal/src/index.ts +183 -0
  133. package/packages/terminal/tsconfig.json +22 -0
  134. package/packages/ui/README.md +8 -0
  135. package/packages/ui/package.json +18 -0
  136. package/packages/ui/src/index.ts +36 -0
  137. package/packages/ui/src/observability.ts +175 -0
  138. package/packages/ui/src/plugin-registry.ts +274 -0
  139. package/packages/ui/src/settings-dialog.ts +293 -0
  140. package/packages/ui/src/themes.ts +174 -0
  141. package/packages/ui/tsconfig.json +24 -0
  142. package/src/code-lab.css +298 -2
  143. package/src/csp.ts +7 -10
  144. package/src/diagnostics.ts +42 -3
  145. package/src/extensions/cpp.ts +105 -24
  146. package/src/extensions/python.ts +260 -116
  147. package/src/extensions/umbrella.ts +5 -0
  148. package/src/index.ts +1 -1
  149. package/src/modular-kernel.ts +11 -0
  150. package/src/preview-sw.js +73 -12
  151. package/src/runtime.ts +501 -74
  152. package/src/schemas.ts +62 -62
  153. package/src/scm.ts +106 -19
  154. package/src/search.ts +71 -27
  155. package/src/shell.ts +433 -53
  156. package/src/terminal.ts +239 -52
  157. package/src/workbench/diff-editor.ts +106 -18
  158. package/src/workbench/drawer.ts +17 -4
  159. package/src/workbench/guided-pane.ts +12 -2
  160. package/src/workbench/icons.ts +5 -2
  161. package/src/workbench/share.ts +37 -8
  162. package/src/workbench/styles.ts +2 -1
  163. package/src/workbench/toolbar.ts +15 -4
  164. package/src/workbench.ts +2877 -284
  165. package/src/zip.ts +29 -7
  166. package/dist/runtime/index.d.ts +0 -4
  167. package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
  168. package/dist/shell/index.d.ts +0 -4
  169. package/src/runtime/index.ts +0 -5
  170. package/src/shell/coreutils/compilers-cli.ts +0 -103
  171. package/src/shell/index.ts +0 -5
@@ -0,0 +1,457 @@
1
+ import type { SqlDiagnostic, SqlDialect } from './types';
2
+
3
+ export type SqlTokenType =
4
+ | 'keyword'
5
+ | 'function'
6
+ | 'type'
7
+ | 'string'
8
+ | 'number'
9
+ | 'operator'
10
+ | 'cast'
11
+ | 'param'
12
+ | 'identifier'
13
+ | 'quoted-ident'
14
+ | 'comment'
15
+ | 'punctuation'
16
+ | 'whitespace';
17
+
18
+ export interface SqlToken {
19
+ readonly type: SqlTokenType;
20
+ readonly value: string;
21
+ readonly start: number;
22
+ readonly end: number;
23
+ }
24
+
25
+ const COMMON_KEYWORDS = new Set([
26
+ 'SELECT', 'FROM', 'WHERE', 'AND', 'OR', 'NOT', 'IN', 'EXISTS', 'BETWEEN', 'LIKE', 'IS', 'NULL',
27
+ 'TRUE', 'FALSE', 'AS', 'JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'OUTER', 'CROSS', 'NATURAL',
28
+ 'ON', 'USING', 'GROUP', 'BY', 'HAVING', 'WINDOW', 'QUALIFY', 'ORDER', 'ASC', 'DESC', 'NULLS',
29
+ 'FIRST', 'LAST', 'LIMIT', 'OFFSET', 'FETCH', 'NEXT', 'ROWS', 'ONLY', 'WITH', 'RECURSIVE',
30
+ 'UNION', 'ALL', 'INTERSECT', 'EXCEPT', 'DISTINCT', 'CASE', 'WHEN', 'THEN', 'ELSE', 'END',
31
+ 'INSERT', 'INTO', 'VALUES', 'DEFAULT', 'UPDATE', 'SET', 'DELETE', 'MERGE', 'CREATE', 'ALTER',
32
+ 'DROP', 'TRUNCATE', 'TABLE', 'TEMP', 'TEMPORARY', 'IF', 'VIEW', 'MATERIALIZED', 'INDEX',
33
+ 'UNIQUE', 'PRIMARY', 'KEY', 'FOREIGN', 'REFERENCES', 'CHECK', 'CONSTRAINT', 'CASCADE',
34
+ 'RESTRICT', 'ADD', 'COLUMN', 'RENAME', 'TO', 'TRIGGER', 'BEFORE', 'AFTER', 'INSTEAD', 'OF',
35
+ 'EACH', 'ROW', 'FOR', 'BEGIN', 'COMMIT', 'ROLLBACK', 'SAVEPOINT', 'RELEASE', 'TRANSACTION',
36
+ 'EXPLAIN', 'QUERY', 'PLAN', 'OVER', 'PARTITION', 'RANGE', 'GROUPS', 'UNBOUNDED', 'PRECEDING',
37
+ 'FOLLOWING', 'CURRENT', 'EXCLUDE', 'TIES', 'OTHERS', 'NO', 'ACTION', 'CONFLICT', 'DO', 'NOTHING',
38
+ 'REPLACE', 'ABORT', 'FAIL', 'IGNORE', 'CAST', 'COLLATE', 'ESCAPE', 'HAVING', 'ANY', 'SOME'
39
+ ]);
40
+
41
+ const POSTGRES_KEYWORDS = new Set([
42
+ 'ILIKE', 'SIMILAR', 'LATERAL', 'RETURNING', 'FILTER', 'ANALYZE', 'BUFFERS', 'VERBOSE', 'COSTS',
43
+ 'SETTINGS', 'WAL', 'SUMMARY', 'TIMING', 'FORMAT', 'CONCURRENTLY', 'GENERATED', 'ALWAYS',
44
+ 'IDENTITY', 'OVERRIDING', 'SYSTEM', 'USER', 'VALUE', 'LOCK', 'SHARE', 'NOWAIT', 'SKIP', 'LOCKED',
45
+ 'EXTENSION', 'SCHEMA', 'SEQUENCE', 'DOMAIN', 'TYPE', 'ENUM', 'FUNCTION', 'PROCEDURE', 'LANGUAGE',
46
+ 'PLPGSQL', 'SQL', 'IMMUTABLE', 'STABLE', 'VOLATILE', 'PARALLEL', 'SAFE', 'TABLESAMPLE',
47
+ 'BERNOULLI', 'UNNEST', 'ORDINALITY', 'WITHIN', 'GROUPING', 'SETS', 'CUBE', 'ROLLUP'
48
+ ]);
49
+
50
+ const SQLITE_KEYWORDS = new Set([
51
+ 'PRAGMA', 'WITHOUT', 'ROWID', 'STRICT', 'GLOB', 'REGEXP', 'MATCH', 'AUTOINCREMENT', 'VACUUM',
52
+ 'ATTACH', 'DETACH', 'DATABASE', 'REINDEX', 'INDEXED', 'RETURNING', 'FILTER', 'GENERATED',
53
+ 'ALWAYS', 'STORED', 'VIRTUAL'
54
+ ]);
55
+
56
+ const SQL_TYPES = new Set([
57
+ 'INT', 'INTEGER', 'SMALLINT', 'BIGINT', 'SERIAL', 'SMALLSERIAL', 'BIGSERIAL', 'REAL', 'FLOAT',
58
+ 'DOUBLE', 'PRECISION', 'NUMERIC', 'DECIMAL', 'MONEY', 'TEXT', 'VARCHAR', 'CHAR', 'CHARACTER',
59
+ 'VARYING', 'BPCHAR', 'BYTEA', 'BLOB', 'BOOLEAN', 'BOOL', 'DATE', 'TIME', 'TIMETZ', 'TIMESTAMP',
60
+ 'TIMESTAMPTZ', 'INTERVAL', 'UUID', 'JSON', 'JSONB', 'XML', 'INET', 'CIDR', 'MACADDR', 'TSVECTOR',
61
+ 'TSQUERY', 'ARRAY', 'INT2', 'INT4', 'INT8', 'FLOAT4', 'FLOAT8', 'ANYARRAY'
62
+ ]);
63
+
64
+ const SQL_FUNCTIONS = new Set([
65
+ 'COUNT', 'SUM', 'AVG', 'MIN', 'MAX', 'COALESCE', 'NULLIF', 'GREATEST', 'LEAST', 'ABS', 'ROUND',
66
+ 'CEIL', 'CEILING', 'FLOOR', 'MOD', 'POWER', 'SQRT', 'RANDOM', 'LENGTH', 'CHAR_LENGTH', 'LOWER',
67
+ 'UPPER', 'TRIM', 'LTRIM', 'RTRIM', 'SUBSTR', 'SUBSTRING', 'POSITION', 'STRPOS', 'CONCAT',
68
+ 'CONCAT_WS', 'SPLIT_PART', 'REGEXP_REPLACE', 'REGEXP_MATCHES', 'NOW', 'CURRENT_DATE',
69
+ 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CLOCK_TIMESTAMP', 'DATE_TRUNC', 'DATE_PART', 'EXTRACT',
70
+ 'AGE', 'TO_CHAR', 'TO_DATE', 'TO_TIMESTAMP', 'MAKE_INTERVAL', 'STRFTIME', 'DATETIME', 'JULIANDAY',
71
+ 'UNIXEPOCH', 'ROW_NUMBER', 'RANK', 'DENSE_RANK', 'PERCENT_RANK', 'CUME_DIST', 'NTILE', 'LAG',
72
+ 'LEAD', 'FIRST_VALUE', 'LAST_VALUE', 'NTH_VALUE', 'ARRAY_AGG', 'STRING_AGG', 'GROUP_CONCAT',
73
+ 'BOOL_AND', 'BOOL_OR', 'JSON_AGG', 'JSONB_AGG', 'JSON_OBJECT', 'JSON_BUILD_OBJECT',
74
+ 'JSONB_BUILD_OBJECT', 'JSON_EXTRACT', 'JSONB_EXTRACT_PATH_TEXT', 'JSONB_SET', 'JSON_EACH',
75
+ 'JSONB_EACH', 'GENERATE_SERIES', 'PG_SLEEP', 'PG_SIZE_PRETTY', 'PG_TOTAL_RELATION_SIZE', 'IIF',
76
+ 'PRINTF', 'TYPEOF', 'TOTAL', 'CHANGES'
77
+ ]);
78
+
79
+ export function getDialectKeywords(dialect: SqlDialect): ReadonlySet<string> {
80
+ const out = new Set<string>(COMMON_KEYWORDS);
81
+ const extra = dialect === 'postgres' ? POSTGRES_KEYWORDS : SQLITE_KEYWORDS;
82
+ for (const k of extra) out.add(k);
83
+ return out;
84
+ }
85
+
86
+ export function getSqlBuiltinFunctions(): ReadonlySet<string> {
87
+ return SQL_FUNCTIONS;
88
+ }
89
+
90
+ function matchDollarQuoteTag(sql: string, pos: number): string | null {
91
+ if (sql[pos] !== '$') return null;
92
+ let end = pos + 1;
93
+ if (end < sql.length && sql[end] === '$') {
94
+ return '$$';
95
+ }
96
+ if (end < sql.length && /[A-Za-z_]/.test(sql[end])) {
97
+ end++;
98
+ while (end < sql.length && /[A-Za-z0-9_]/.test(sql[end])) {
99
+ end++;
100
+ }
101
+ if (end < sql.length && sql[end] === '$') {
102
+ return sql.slice(pos, end + 1);
103
+ }
104
+ }
105
+ return null;
106
+ }
107
+
108
+ export function tokenizeSql(sql: string, dialect: SqlDialect = 'postgres'): SqlToken[] {
109
+ const tokens: SqlToken[] = [];
110
+ const keywords = getDialectKeywords(dialect);
111
+ const len = sql.length;
112
+ let i = 0;
113
+
114
+ while (i < len) {
115
+ const ch = sql[i];
116
+ const next = sql[i + 1];
117
+
118
+ // 1. Whitespace
119
+ if (/\s/.test(ch)) {
120
+ const start = i;
121
+ while (i < len && /\s/.test(sql[i])) i++;
122
+ tokens.push({ type: 'whitespace', value: sql.slice(start, i), start, end: i });
123
+ continue;
124
+ }
125
+
126
+ // 2. Single-line comment `-- ...`
127
+ if (ch === '-' && next === '-') {
128
+ const start = i;
129
+ i += 2;
130
+ while (i < len && sql[i] !== '\n') i++;
131
+ tokens.push({ type: 'comment', value: sql.slice(start, i), start, end: i });
132
+ continue;
133
+ }
134
+
135
+ // 3. Block comment `/* ... */` (supports nested Postgres block comments)
136
+ if (ch === '/' && next === '*') {
137
+ const start = i;
138
+ i += 2;
139
+ let depth = 1;
140
+ while (i < len && depth > 0) {
141
+ if (sql[i] === '/' && sql[i + 1] === '*') {
142
+ depth++;
143
+ i += 2;
144
+ } else if (sql[i] === '*' && sql[i + 1] === '/') {
145
+ depth--;
146
+ i += 2;
147
+ } else {
148
+ i++;
149
+ }
150
+ }
151
+ tokens.push({ type: 'comment', value: sql.slice(start, i), start, end: i });
152
+ continue;
153
+ }
154
+
155
+ // 4. Postgres dollar-quoted strings `$$...$$` or `$tag$...$tag$`
156
+ if (dialect === 'postgres' && ch === '$') {
157
+ const tag = matchDollarQuoteTag(sql, i);
158
+ if (tag) {
159
+ const start = i;
160
+ i += tag.length;
161
+ const closeIdx = sql.indexOf(tag, i);
162
+ if (closeIdx === -1) {
163
+ i = len;
164
+ } else {
165
+ i = closeIdx + tag.length;
166
+ }
167
+ tokens.push({ type: 'string', value: sql.slice(start, i), start, end: i });
168
+ continue;
169
+ }
170
+ }
171
+
172
+ // 5. Positional/named parameters (`$1` in Postgres; `?1`, `:param`, `@param` in SQLite)
173
+ if (ch === '$' && /[0-9]/.test(next ?? '')) {
174
+ const start = i;
175
+ i++;
176
+ while (i < len && /[0-9]/.test(sql[i])) i++;
177
+ tokens.push({ type: 'param', value: sql.slice(start, i), start, end: i });
178
+ continue;
179
+ }
180
+ if (dialect === 'sqlite' && (ch === ':' || ch === '@') && /[A-Za-z_]/.test(next ?? '')) {
181
+ const start = i;
182
+ i++;
183
+ while (i < len && /[A-Za-z0-9_]/.test(sql[i])) i++;
184
+ tokens.push({ type: 'param', value: sql.slice(start, i), start, end: i });
185
+ continue;
186
+ }
187
+ if (dialect === 'sqlite' && ch === '?' && /[0-9]/.test(next ?? '')) {
188
+ const start = i;
189
+ i++;
190
+ while (i < len && /[0-9]/.test(sql[i])) i++;
191
+ tokens.push({ type: 'param', value: sql.slice(start, i), start, end: i });
192
+ continue;
193
+ }
194
+
195
+ // 6. Postgres `::type` cast
196
+ if (dialect === 'postgres' && ch === ':' && next === ':') {
197
+ const start = i;
198
+ i += 2;
199
+ while (i < len && /\s/.test(sql[i])) i++;
200
+ while (i < len && /[A-Za-z0-9_[\]]/.test(sql[i])) i++;
201
+ tokens.push({ type: 'cast', value: sql.slice(start, i), start, end: i });
202
+ continue;
203
+ }
204
+
205
+ // 7. Single-quoted string (or Postgres E'...' / X'...')
206
+ if (ch === '\'' || ((ch === 'E' || ch === 'e' || ch === 'X' || ch === 'x') && next === '\'')) {
207
+ const start = i;
208
+ const isEscaped = ch !== '\'';
209
+ i += isEscaped ? 2 : 1;
210
+ while (i < len) {
211
+ if (isEscaped && sql[i] === '\\' && i + 1 < len) {
212
+ i += 2;
213
+ continue;
214
+ }
215
+ if (sql[i] === '\'') {
216
+ if (sql[i + 1] === '\'') {
217
+ i += 2;
218
+ continue;
219
+ }
220
+ i++;
221
+ break;
222
+ }
223
+ i++;
224
+ }
225
+ tokens.push({ type: 'string', value: sql.slice(start, i), start, end: i });
226
+ continue;
227
+ }
228
+
229
+ // 8. Quoted identifiers (`"..."`, SQLite `` `...` `` or `[...]`)
230
+ if (ch === '"' || (dialect === 'sqlite' && (ch === '`' || ch === '['))) {
231
+ const start = i;
232
+ const closeChar = ch === '[' ? ']' : ch;
233
+ i++;
234
+ while (i < len) {
235
+ if (sql[i] === closeChar) {
236
+ if (closeChar !== ']' && sql[i + 1] === closeChar) {
237
+ i += 2;
238
+ continue;
239
+ }
240
+ i++;
241
+ break;
242
+ }
243
+ i++;
244
+ }
245
+ tokens.push({ type: 'quoted-ident', value: sql.slice(start, i), start, end: i });
246
+ continue;
247
+ }
248
+
249
+ // 9. Numeric literals
250
+ if (/[0-9]/.test(ch) || (ch === '.' && /[0-9]/.test(next ?? ''))) {
251
+ const start = i;
252
+ if (ch === '0' && (next === 'x' || next === 'X')) {
253
+ i += 2;
254
+ while (i < len && /[0-9a-fA-F_]/.test(sql[i])) i++;
255
+ } else {
256
+ while (i < len && /[0-9_]/.test(sql[i])) i++;
257
+ if (i < len && sql[i] === '.' && /[0-9]/.test(sql[i + 1] ?? '')) {
258
+ i++;
259
+ while (i < len && /[0-9_]/.test(sql[i])) i++;
260
+ }
261
+ if (i < len && (sql[i] === 'e' || sql[i] === 'E')) {
262
+ i++;
263
+ if (sql[i] === '+' || sql[i] === '-') i++;
264
+ while (i < len && /[0-9]/.test(sql[i])) i++;
265
+ }
266
+ }
267
+ tokens.push({ type: 'number', value: sql.slice(start, i), start, end: i });
268
+ continue;
269
+ }
270
+
271
+ // 10. Multi-char JSONB & relational operators (`->>`, `#>>`, `->`, `#>`, `@>`, `<@`, `?|`, `?&`, `||`, `<>`, `!=`, `<=`, `>=`, `!~*`, `~*`)
272
+ const three = sql.slice(i, i + 3);
273
+ if (three === '->>' || three === '#>>' || three === '!~*') {
274
+ tokens.push({ type: 'operator', value: three, start: i, end: i + 3 });
275
+ i += 3;
276
+ continue;
277
+ }
278
+ const two = sql.slice(i, i + 2);
279
+ if (
280
+ two === '->' || two === '#>' || two === '@>' || two === '<@' ||
281
+ two === '?|' || two === '?&' || two === '||' || two === '<>' ||
282
+ two === '!=' || two === '<=' || two === '>=' || two === '~*' ||
283
+ two === '!~' || two === '<<' || two === '>>'
284
+ ) {
285
+ tokens.push({ type: 'operator', value: two, start: i, end: i + 2 });
286
+ i += 2;
287
+ continue;
288
+ }
289
+
290
+ // 11. Single-char punctuation vs operators
291
+ if ('(),.;[]'.includes(ch)) {
292
+ tokens.push({ type: 'punctuation', value: ch, start: i, end: i + 1 });
293
+ i++;
294
+ continue;
295
+ }
296
+ if ('+-*/%=<>?&|~^!'.includes(ch)) {
297
+ tokens.push({ type: 'operator', value: ch, start: i, end: i + 1 });
298
+ i++;
299
+ continue;
300
+ }
301
+
302
+ // 12. Identifiers, Keywords, Types & Functions
303
+ if (/[A-Za-z_]/.test(ch)) {
304
+ const start = i;
305
+ i++;
306
+ while (i < len && /[A-Za-z0-9_$]/.test(sql[i])) i++;
307
+ const word = sql.slice(start, i);
308
+ const upper = word.toUpperCase();
309
+
310
+ let look = i;
311
+ while (look < len && /\s/.test(sql[look])) look++;
312
+ const followedByParen = sql[look] === '(';
313
+
314
+ // Check previous non-whitespace token: if `.` precede this word, it's a column/member identifier
315
+ let prevNonWs: SqlToken | undefined;
316
+ for (let k = tokens.length - 1; k >= 0; k--) {
317
+ if (tokens[k].type !== 'whitespace' && tokens[k].type !== 'comment') {
318
+ prevNonWs = tokens[k];
319
+ break;
320
+ }
321
+ }
322
+
323
+ if (prevNonWs && prevNonWs.value === '.') {
324
+ tokens.push({ type: 'identifier', value: word, start, end: i });
325
+ } else if (followedByParen && SQL_FUNCTIONS.has(upper)) {
326
+ tokens.push({ type: 'function', value: word, start, end: i });
327
+ } else if (keywords.has(upper)) {
328
+ tokens.push({ type: 'keyword', value: word, start, end: i });
329
+ } else if (SQL_TYPES.has(upper)) {
330
+ tokens.push({ type: 'type', value: word, start, end: i });
331
+ } else {
332
+ tokens.push({ type: 'identifier', value: word, start, end: i });
333
+ }
334
+ continue;
335
+ }
336
+
337
+ tokens.push({ type: 'punctuation', value: ch, start: i, end: i + 1 });
338
+ i++;
339
+ }
340
+
341
+ return tokens;
342
+ }
343
+
344
+ export function escapeSqlHtml(raw: string): string {
345
+ return raw
346
+ .replace(/&/g, '&amp;')
347
+ .replace(/</g, '&lt;')
348
+ .replace(/>/g, '&gt;')
349
+ .replace(/"/g, '&quot;');
350
+ }
351
+
352
+ function tokenCssClass(type: SqlTokenType): string | null {
353
+ switch (type) {
354
+ case 'keyword':
355
+ return 'cl-sql-tok-kw';
356
+ case 'function':
357
+ return 'cl-sql-tok-fn';
358
+ case 'type':
359
+ case 'cast':
360
+ return 'cl-sql-tok-type';
361
+ case 'string':
362
+ return 'cl-sql-tok-str';
363
+ case 'number':
364
+ return 'cl-sql-tok-num';
365
+ case 'operator':
366
+ return 'cl-sql-tok-op';
367
+ case 'param':
368
+ return 'cl-sql-tok-param';
369
+ case 'quoted-ident':
370
+ return 'cl-sql-tok-qident';
371
+ case 'comment':
372
+ return 'cl-sql-tok-comment';
373
+ default:
374
+ return null;
375
+ }
376
+ }
377
+
378
+ interface NormalizedDiagRange {
379
+ readonly from: number;
380
+ readonly to: number;
381
+ readonly severity: 'error' | 'warning' | 'info';
382
+ readonly message: string;
383
+ }
384
+
385
+ function resolveDiagRanges(sql: string, diagnostics: readonly SqlDiagnostic[]): NormalizedDiagRange[] {
386
+ if (diagnostics.length === 0) return [];
387
+ const lineStarts: number[] = [0];
388
+ for (let i = 0; i < sql.length; i++) {
389
+ if (sql[i] === '\n') lineStarts.push(i + 1);
390
+ }
391
+ const ranges: NormalizedDiagRange[] = [];
392
+ for (const d of diagnostics) {
393
+ let from = d.fromOffset;
394
+ let to = d.toOffset;
395
+ if (from === undefined && d.line !== undefined && d.line >= 1) {
396
+ const lineIdx = Math.min(d.line - 1, lineStarts.length - 1);
397
+ const lineStart = lineStarts[lineIdx];
398
+ const nextNewline = sql.indexOf('\n', lineStart);
399
+ const lineEnd = nextNewline === -1 ? sql.length : nextNewline;
400
+ if (d.column !== undefined && d.column >= 1) {
401
+ from = Math.min(lineStart + d.column - 1, lineEnd);
402
+ let tokEnd = from;
403
+ while (tokEnd < lineEnd && !/\s|[(),;]/.test(sql[tokEnd])) tokEnd++;
404
+ to = tokEnd > from ? tokEnd : Math.min(from + 1, lineEnd);
405
+ } else {
406
+ from = lineStart;
407
+ to = lineEnd;
408
+ }
409
+ }
410
+ if (from !== undefined) {
411
+ const safeFrom = Math.max(0, Math.min(from, sql.length));
412
+ const safeTo = Math.max(safeFrom + 1, Math.min(to ?? safeFrom + 1, sql.length));
413
+ if (safeFrom < sql.length) {
414
+ ranges.push({
415
+ from: safeFrom,
416
+ to: safeTo,
417
+ severity: d.severity,
418
+ message: d.message
419
+ });
420
+ }
421
+ }
422
+ }
423
+ return ranges;
424
+ }
425
+
426
+ export function highlightSql(
427
+ sql: string,
428
+ dialect: SqlDialect = 'postgres',
429
+ diagnostics: readonly SqlDiagnostic[] = []
430
+ ): string {
431
+ const tokens = tokenizeSql(sql, dialect);
432
+ const diagRanges = resolveDiagRanges(sql, diagnostics);
433
+ const parts: string[] = [];
434
+
435
+ for (const tok of tokens) {
436
+ const cls = tokenCssClass(tok.type);
437
+ const overlapping = diagRanges.find((r) => tok.start < r.to && tok.end > r.from);
438
+ const escaped = escapeSqlHtml(tok.value);
439
+ let rendered = cls ? `<span class="${cls}">${escaped}</span>` : escaped;
440
+ if (overlapping && tok.type !== 'whitespace') {
441
+ const diagCls =
442
+ overlapping.severity === 'error'
443
+ ? 'cl-sql-diag-error'
444
+ : overlapping.severity === 'warning'
445
+ ? 'cl-sql-diag-warning'
446
+ : 'cl-sql-diag-info';
447
+ rendered = `<span class="${diagCls}" title="${escapeSqlHtml(overlapping.message)}">${rendered}</span>`;
448
+ }
449
+ parts.push(rendered);
450
+ }
451
+
452
+ // Trailing newline keeps `<pre>` height matched with `<textarea>` when sql ends with `\n`
453
+ if (sql.endsWith('\n') || sql.length === 0) {
454
+ parts.push(' ');
455
+ }
456
+ return parts.join('');
457
+ }
@@ -0,0 +1,148 @@
1
+ export type SqlDialect = 'postgres' | 'sqlite';
2
+
3
+ export type SqlEditorMode = 'inline' | 'playground' | 'workbench';
4
+
5
+ export type SqlThemeMode = 'dark' | 'light' | 'auto';
6
+
7
+ export interface SqlColumnSchema {
8
+ readonly name: string;
9
+ readonly type: string;
10
+ readonly pk?: boolean;
11
+ readonly primaryKey?: boolean;
12
+ readonly fk?: string;
13
+ readonly references?: string;
14
+ readonly nullable?: boolean;
15
+ readonly comment?: string;
16
+ }
17
+
18
+ export interface SqlTableSchema {
19
+ readonly name: string;
20
+ readonly rowCount?: number;
21
+ readonly columns: readonly SqlColumnSchema[];
22
+ }
23
+
24
+ export interface SqlFunctionSchema {
25
+ readonly name: string;
26
+ readonly signature: string;
27
+ readonly description?: string;
28
+ }
29
+
30
+ export interface SqlSchema {
31
+ readonly dialect: SqlDialect;
32
+ readonly tables: readonly SqlTableSchema[];
33
+ readonly functions?: readonly SqlFunctionSchema[];
34
+ }
35
+
36
+ export interface SqlDiagnostic {
37
+ readonly severity: 'error' | 'warning' | 'info';
38
+ readonly message: string;
39
+ readonly hint?: string;
40
+ readonly line?: number;
41
+ readonly column?: number;
42
+ readonly fromOffset?: number;
43
+ readonly toOffset?: number;
44
+ }
45
+
46
+ export interface SqlRunPayload {
47
+ readonly sql: string;
48
+ readonly selectedSql: string;
49
+ readonly effectiveSql: string;
50
+ readonly dialect: SqlDialect;
51
+ }
52
+
53
+ export interface SqlFormatOptions {
54
+ readonly dialect?: SqlDialect;
55
+ readonly indent?: number;
56
+ readonly keywordCase?: 'upper' | 'lower' | 'preserve';
57
+ }
58
+
59
+ export type SqlCompletionKind = 'table' | 'column' | 'function' | 'keyword' | 'cte';
60
+
61
+ export interface SqlCompletionItem {
62
+ readonly label: string;
63
+ readonly insertText: string;
64
+ readonly kind: SqlCompletionKind;
65
+ readonly detail?: string;
66
+ readonly badge?: string;
67
+ readonly secondaryBadge?: string;
68
+ readonly description?: string;
69
+ readonly replaceFrom: number;
70
+ readonly replaceTo: number;
71
+ }
72
+
73
+ export interface SqlEditorOptions {
74
+ readonly value?: string;
75
+ readonly dialect?: SqlDialect;
76
+ readonly mode?: SqlEditorMode;
77
+ readonly schema?: SqlSchema | null;
78
+ readonly theme?: SqlThemeMode;
79
+ readonly readOnly?: boolean;
80
+ readonly ariaLabel?: string;
81
+ readonly placeholder?: string;
82
+ readonly minLines?: number;
83
+ readonly maxLines?: number;
84
+ readonly showToolbar?: boolean;
85
+ readonly onRun?: (payload: SqlRunPayload) => void | Promise<void>;
86
+ readonly onExplain?: (payload: SqlRunPayload) => void | Promise<void>;
87
+ readonly onChange?: (value: string) => void;
88
+ readonly onFormat?: (formatted: string) => void;
89
+ }
90
+
91
+ export interface SqlEditorInstance {
92
+ getValue(): string;
93
+ setValue(sql: string): void;
94
+ getSelection(): string;
95
+ insertAtCursor(text: string): void;
96
+ format(): string;
97
+ setDialect(dialect: SqlDialect): void;
98
+ getDialect(): SqlDialect;
99
+ setSchema(schema: SqlSchema | null): void;
100
+ getSchema(): SqlSchema | null;
101
+ setDiagnostics(diagnostics: readonly SqlDiagnostic[]): void;
102
+ clearDiagnostics(): void;
103
+ getDiagnostics(): readonly SqlDiagnostic[];
104
+ setTheme(theme: SqlThemeMode): void;
105
+ getTheme(): SqlThemeMode;
106
+ setReadOnly(readOnly: boolean): void;
107
+ focus(): void;
108
+ destroy(): void;
109
+ /** Playground slot elements when `mode === 'playground'` */
110
+ readonly slots?: {
111
+ readonly header: HTMLElement | null;
112
+ readonly footer: HTMLElement | null;
113
+ readonly results: HTMLElement | null;
114
+ };
115
+ }
116
+
117
+ export interface SqlRunnerStepInfo {
118
+ readonly index: number;
119
+ readonly id?: string;
120
+ readonly title?: string;
121
+ }
122
+
123
+ export interface SqlRunnerRequest {
124
+ readonly sql: string;
125
+ readonly selectedSql: string;
126
+ readonly effectiveSql: string;
127
+ readonly dialect: SqlDialect;
128
+ readonly step?: SqlRunnerStepInfo | null;
129
+ readonly mode: 'run' | 'verify' | 'explain';
130
+ }
131
+
132
+ export interface SqlRunnerVerification {
133
+ readonly passed: boolean;
134
+ readonly message?: string;
135
+ readonly details?: readonly string[];
136
+ }
137
+
138
+ export interface SqlRunnerResult {
139
+ readonly columns?: readonly string[];
140
+ readonly rows?: ReadonlyArray<ReadonlyArray<unknown> | Readonly<Record<string, unknown>>>;
141
+ readonly rowCount?: number;
142
+ readonly execMs?: number;
143
+ readonly plan?: string | Readonly<Record<string, unknown>> | ReadonlyArray<unknown>;
144
+ readonly verification?: SqlRunnerVerification;
145
+ readonly error?: unknown;
146
+ }
147
+
148
+ export type SqlRunnerFn = (request: SqlRunnerRequest) => Promise<SqlRunnerResult> | SqlRunnerResult;
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": ["ES2022", "DOM"],
7
+ "strict": true,
8
+ "noImplicitAny": true,
9
+ "strictNullChecks": true,
10
+ "noUnusedLocals": true,
11
+ "noUnusedParameters": true,
12
+ "noImplicitReturns": true,
13
+ "noFallthroughCasesInSwitch": true,
14
+ "noEmit": true,
15
+ "skipLibCheck": true,
16
+ "forceConsistentCasingInFileNames": true
17
+ },
18
+ "include": ["src/**/*.ts"]
19
+ }
@@ -0,0 +1,3 @@
1
+ # `@code-lab/terminal`
2
+
3
+ Layer 2 (`services`) multi-session terminal coordinator (`ITerminalService`, `TerminalService`) and ANSI SGR stream parser (`parseAnsiSegments`).
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@code-lab/terminal",
3
+ "version": "0.4.0",
4
+ "description": "Typed multi-session terminal service & ANSI stream parser for CodeLab.",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
7
+ "types": "./src/index.ts",
8
+ "exports": {
9
+ ".": "./src/index.ts"
10
+ },
11
+ "dependencies": {
12
+ "@code-lab/core": "workspace:*"
13
+ },
14
+ "sideEffects": false,
15
+ "license": "MIT"
16
+ }