@astryxdesign/richtext 0.7.6 → 0.7.7-canary.6442361

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.
@@ -0,0 +1,188 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file codeLineTokens.ts
5
+ * @input Core CodeBlock's tokenizer (`tokenize`) and token line type.
6
+ * @output Exports CodeLineTokens, which keeps core CodeBlock's tokens for a
7
+ * block of code line by line and re-tokenizes only the lines an edit can
8
+ * change.
9
+ * @position Used by CodeSyntaxPlugin. Its tokens always equal what `tokenize`
10
+ * returns for the whole code. An edit re-tokenizes only its own line, and
11
+ * at most the line above it, for the built-in TypeScript, JavaScript,
12
+ * Python, and shell grammars while every line passes `standsAlone`;
13
+ * anything else re-tokenizes all of it. The checks follow those grammars'
14
+ * patterns in core CodeBlock's tokenizer.ts, and codeLineTokens.test.ts
15
+ * compares edits against a full `tokenize`, so a grammar change that
16
+ * breaks them fails there.
17
+ */
18
+
19
+ import { tokenize } from '@astryxdesign/core/CodeBlock';
20
+ const FAMILIES = new Map([['typescript', 'javascript'], ['javascript', 'javascript'], ['tsx', 'javascript'], ['jsx', 'javascript'], ['ts', 'javascript'], ['js', 'javascript'], ['python', 'python'], ['py', 'python'], ['bash', 'shell'], ['sh', 'shell'], ['zsh', 'shell'], ['shell', 'shell']]);
21
+
22
+ /** What can open a string or comment that runs on past its line. */
23
+ const OPENERS = {
24
+ javascript: /['"`]|\/\*/,
25
+ python: /['"]/,
26
+ shell: /['"`]|\$\{|\$\(|<</
27
+ };
28
+
29
+ /** A line whose last visible character can end a function name. */
30
+ const NAME_END = /[\w$]\s*$/;
31
+
32
+ /**
33
+ * Whether the name that ends a line can change color with the code after
34
+ * it: its last visible character is a name character outside every string,
35
+ * comment, number, keyword, and constant, whose patterns come before
36
+ * function names.
37
+ */
38
+ function endsInOpenName(text, tokens) {
39
+ const last = text.trimEnd().length - 1;
40
+ return NAME_END.test(text) && !tokens.some(token => token.start <= last && token.end > last && token.type !== 'function' && token.type !== 'type');
41
+ }
42
+
43
+ /** A line's first visible character, or '' when it has none. */
44
+ function firstVisible(line) {
45
+ return /\S/.exec(line)?.[0] ?? '';
46
+ }
47
+
48
+ /**
49
+ * Whether a line's strings and comments all close on it. Their patterns
50
+ * match line breaks, so an opener left open can run on to a later line.
51
+ * Only a string or comment the tokenizer emitted shields an opener: one that
52
+ * another token swallowed, such as `/*` in the operator `+/*`, still counts
53
+ * as open. Python tries triple quotes first, so `"""` can pair with another
54
+ * line even where its quotes close as single strings on this one.
55
+ */
56
+ function standsAlone(text, tokens, family) {
57
+ if (family === 'python' && /'''|"""/.test(text)) {
58
+ return false;
59
+ }
60
+ const opener = OPENERS[family];
61
+ let position = 0;
62
+ for (const token of tokens) {
63
+ if (token.end > text.length) {
64
+ return false;
65
+ }
66
+ if (token.type === 'string' || token.type === 'comment') {
67
+ if (opener.test(text.slice(position, token.start))) {
68
+ return false;
69
+ }
70
+ position = token.end;
71
+ }
72
+ }
73
+ return !opener.test(text.slice(position));
74
+ }
75
+
76
+ /**
77
+ * Core CodeBlock's tokens for a block of code, kept line by line with
78
+ * line-relative offsets, exactly as `tokenize` returns them for the whole
79
+ * code.
80
+ */
81
+ export class CodeLineTokens {
82
+ /** Where each line starts in the code. */
83
+ starts = [];
84
+ /** Each line's tokens. */
85
+ tokens = [];
86
+ lines = [];
87
+ /** Whether every line stands alone, so an edit re-tokenizes lines alone. */
88
+ linesStandAlone = false;
89
+ constructor(text, language) {
90
+ this.language = language;
91
+ this.family = typeof language === 'string' ? FAMILIES.get(language) ?? null : null;
92
+ this.tokenizeAll(text);
93
+ }
94
+
95
+ /** The code. */
96
+ get text() {
97
+ return this.lines.join('\n');
98
+ }
99
+
100
+ /** The line that holds `offset` in the code. */
101
+ lineAt(offset) {
102
+ let low = 0;
103
+ let high = this.starts.length - 1;
104
+ while (low < high) {
105
+ const mid = low + high + 1 >>> 1;
106
+ if (this.starts[mid] <= offset) {
107
+ low = mid;
108
+ } else {
109
+ high = mid - 1;
110
+ }
111
+ }
112
+ return low;
113
+ }
114
+
115
+ /**
116
+ * Replaces `removed` characters at `offset` in the code with `inserted`.
117
+ * Returns the lines it re-tokenized when the edit stays inside one line
118
+ * and every line still stands alone; otherwise it re-tokenizes all the
119
+ * code and returns null.
120
+ */
121
+ replace(offset, removed, inserted) {
122
+ const line = this.lineAt(offset);
123
+ const before = this.lines[line];
124
+ const column = offset - this.starts[line];
125
+ if (this.family == null || !this.linesStandAlone || column + removed > before.length || inserted.includes('\n')) {
126
+ const text = this.text;
127
+ this.tokenizeAll(text.slice(0, offset) + inserted + text.slice(offset + removed));
128
+ return null;
129
+ }
130
+ const after = before.slice(0, column) + inserted + before.slice(column + removed);
131
+ this.lines[line] = after;
132
+ const tokens = this.tokenizeLine(line);
133
+ if (!standsAlone(after, tokens, this.family)) {
134
+ this.tokenizeAll(this.text);
135
+ return null;
136
+ }
137
+ this.tokens[line] = tokens;
138
+ for (let i = line + 1; i < this.starts.length; i++) {
139
+ this.starts[i] += inserted.length - removed;
140
+ }
141
+ const changed = [line];
142
+ // A name that ends an earlier line looks past blank lines for `(`, so a
143
+ // new first visible character here can change that name's color.
144
+ if (firstVisible(before) !== firstVisible(after)) {
145
+ let above = line - 1;
146
+ while (above >= 0 && firstVisible(this.lines[above]) === '') {
147
+ above--;
148
+ }
149
+ if (above >= 0 && endsInOpenName(this.lines[above], this.tokens[above] ?? [])) {
150
+ this.tokens[above] = this.tokenizeLine(above);
151
+ changed.unshift(above);
152
+ }
153
+ }
154
+ return changed;
155
+ }
156
+
157
+ /**
158
+ * A line's tokens as `tokenize` finds them in the whole code. A function
159
+ * name is a name followed by `(` past any whitespace, line breaks
160
+ * included, so when the line ends in a name, the code up to the next
161
+ * visible character is tokenized with it.
162
+ */
163
+ tokenizeLine(line) {
164
+ let text = this.lines[line];
165
+ if (NAME_END.test(text)) {
166
+ for (let next = line + 1; next < this.lines.length; next++) {
167
+ const visible = /\S/.exec(this.lines[next]);
168
+ text += '\n' + (visible == null ? this.lines[next] : this.lines[next].slice(0, visible.index + 1));
169
+ if (visible != null) {
170
+ break;
171
+ }
172
+ }
173
+ }
174
+ return tokenize(text, this.language)[0] ?? [];
175
+ }
176
+ tokenizeAll(text) {
177
+ this.lines = text.split('\n');
178
+ let start = 0;
179
+ this.starts = this.lines.map(line => {
180
+ const lineStart = start;
181
+ start += line.length + 1;
182
+ return lineStart;
183
+ });
184
+ this.tokens = tokenize(text, this.language);
185
+ const family = this.family;
186
+ this.linesStandAlone = family != null && this.lines.every((line, i) => standsAlone(line, this.tokens[i] ?? [], family));
187
+ }
188
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@astryxdesign/richtext",
3
3
  "displayName": "Rich Text",
4
- "version": "0.7.6",
4
+ "version": "0.7.7-canary.6442361",
5
5
  "description": "Astryx rich text — a Lexical-based rich text editor and viewer, styled with Astryx design tokens.",
6
6
  "author": "Meta Open Source",
7
7
  "license": "MIT",
@@ -66,7 +66,7 @@
66
66
  },
67
67
  "peerDependencies": {
68
68
  "@stylexjs/stylex": ">=0.10.0",
69
- "@astryxdesign/core": "0.7.6",
69
+ "@astryxdesign/core": "0.7.7-canary.6442361",
70
70
  "react": ">=19.0.0",
71
71
  "react-dom": ">=19.0.0",
72
72
  "lexical": "^0.52.0",
@@ -125,6 +125,8 @@
125
125
  }
126
126
  },
127
127
  "devDependencies": {
128
+ "@astryxdesign/a11y-spec": "0.7.7-canary.6442361",
129
+ "@astryxdesign/cli": "0.7.7-canary.6442361",
128
130
  "@babel/cli": "^7.29.7",
129
131
  "@babel/core": "^7.29.7",
130
132
  "@babel/preset-react": "^7.29.7",
@@ -143,9 +145,7 @@
143
145
  "@lexical/utils": "^0.52.0",
144
146
  "@stylexjs/babel-plugin": "^0.19.0",
145
147
  "@testing-library/react": "^16.3.2",
146
- "lexical": "^0.52.0",
147
- "@astryxdesign/a11y-spec": "0.0.0",
148
- "@astryxdesign/cli": "0.7.6"
148
+ "lexical": "^0.52.0"
149
149
  },
150
150
  "scripts": {
151
151
  "api:report": "node ../../scripts/api-report/cli.mjs --package .",