@bigpixelrocket/uiok 0.3.0 → 0.5.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.
- package/CHANGELOG.md +50 -1
- package/dist/button/TextLink.d.ts +19 -3
- package/dist/button/TextLink.js +4 -3
- package/dist/dialog/ConfirmDialog.d.ts +7 -1
- package/dist/dialog/ConfirmDialog.js +2 -2
- package/dist/dialog/ModalDialog.js +8 -2
- package/dist/dialog/ProgressDialog.d.ts +7 -1
- package/dist/dialog/ProgressDialog.js +2 -2
- package/dist/dialog/TypedNameDialog.d.ts +8 -0
- package/dist/field/Check.d.ts +9 -2
- package/dist/field/Check.js +5 -4
- package/dist/field/Controls.d.ts +198 -0
- package/dist/field/Controls.js +94 -0
- package/dist/field/FieldLayout.d.ts +126 -0
- package/dist/field/FieldLayout.js +89 -0
- package/dist/field/index.d.ts +9 -6
- package/dist/field/index.js +9 -6
- package/dist/frame/Dock.js +1 -1
- package/dist/frame/SidebarResizer.js +1 -1
- package/dist/frame/Tree.d.ts +17 -4
- package/dist/frame/Tree.js +34 -9
- package/dist/frame/WindowFrame.js +2 -1
- package/dist/frame/frame.css +26 -4
- package/dist/icon/ProductMark.d.ts +57 -0
- package/dist/icon/ProductMark.js +26 -0
- package/dist/icon/icon.css +53 -0
- package/dist/icon/index.d.ts +3 -1
- package/dist/icon/index.js +3 -1
- package/dist/labels/UiokLabels.d.ts +3 -0
- package/dist/labels/UiokLabels.js +3 -1
- package/dist/labels/index.d.ts +1 -0
- package/dist/labels/index.js +1 -0
- package/dist/labels/settings.d.ts +6 -0
- package/dist/labels/settings.js +1 -0
- package/dist/labels/viewer.d.ts +193 -0
- package/dist/labels/viewer.js +124 -0
- package/dist/listing/Listing.d.ts +60 -4
- package/dist/listing/Listing.js +39 -10
- package/dist/listing/ListingScope.d.ts +7 -0
- package/dist/listing/ListingScope.js +8 -0
- package/dist/listing/PaneMessage.d.ts +28 -0
- package/dist/listing/PaneMessage.js +12 -0
- package/dist/listing/Timeline.d.ts +51 -0
- package/dist/listing/Timeline.js +28 -0
- package/dist/listing/index.d.ts +7 -3
- package/dist/listing/index.js +6 -2
- package/dist/listing/listing.css +185 -22
- package/dist/log/Ansi.logic.d.ts +27 -0
- package/dist/log/Ansi.logic.js +203 -0
- package/dist/log/LogWell.d.ts +11 -2
- package/dist/log/LogWell.js +15 -3
- package/dist/log/index.d.ts +1 -0
- package/dist/log/index.js +1 -0
- package/dist/log/log.css +80 -0
- package/dist/menu/Menu.d.ts +89 -0
- package/dist/menu/Menu.js +143 -0
- package/dist/menu/Menu.logic.d.ts +56 -0
- package/dist/menu/Menu.logic.js +64 -0
- package/dist/menu/index.d.ts +5 -1
- package/dist/menu/index.js +5 -1
- package/dist/menu/menu.css +51 -0
- package/dist/palette/Palette.d.ts +3 -2
- package/dist/palette/Palette.js +8 -4
- package/dist/palette/PaletteRows.d.ts +129 -0
- package/dist/palette/PaletteRows.js +74 -0
- package/dist/palette/index.d.ts +5 -2
- package/dist/palette/index.js +5 -2
- package/dist/pane/PaneFrame.d.ts +22 -1
- package/dist/pane/PaneFrame.js +42 -5
- package/dist/pane/index.d.ts +1 -1
- package/dist/shell/SettingsDialog.d.ts +20 -2
- package/dist/shell/SettingsDialog.js +13 -3
- package/dist/shell/index.d.ts +1 -1
- package/dist/styles.css +4 -0
- package/dist/surface/Card.d.ts +18 -0
- package/dist/surface/Card.js +10 -0
- package/dist/surface/FactTable.d.ts +44 -0
- package/dist/surface/FactTable.js +33 -0
- package/dist/surface/Notice.d.ts +45 -0
- package/dist/surface/Notice.js +27 -0
- package/dist/surface/index.d.ts +7 -3
- package/dist/surface/index.js +7 -3
- package/dist/surface/surface.css +64 -0
- package/dist/tabs/PageTabs.d.ts +6 -0
- package/dist/tabs/PageTabs.js +1 -1
- package/dist/tabs/tabs.css +14 -0
- package/dist/terminal/TerminalBox.d.ts +30 -0
- package/dist/terminal/TerminalBox.js +17 -0
- package/dist/terminal/index.d.ts +3 -1
- package/dist/terminal/index.js +3 -1
- package/dist/terminal/terminal.css +29 -0
- package/dist/themes/themes.generated.css +21 -0
- package/dist/themes/tokens.d.ts +1 -0
- package/dist/themes/tokens.js +16 -1
- package/dist/viewer/AudioView.d.ts +13 -0
- package/dist/viewer/AudioView.js +25 -0
- package/dist/viewer/HexJump.logic.d.ts +16 -0
- package/dist/viewer/HexJump.logic.js +27 -0
- package/dist/viewer/HexRows.logic.d.ts +55 -0
- package/dist/viewer/HexRows.logic.js +84 -0
- package/dist/viewer/HexSelection.logic.d.ts +57 -0
- package/dist/viewer/HexSelection.logic.js +68 -0
- package/dist/viewer/HexView.d.ts +10 -0
- package/dist/viewer/HexView.js +291 -0
- package/dist/viewer/HexViewport.logic.d.ts +61 -0
- package/dist/viewer/HexViewport.logic.js +79 -0
- package/dist/viewer/ImageView.d.ts +19 -0
- package/dist/viewer/ImageView.js +230 -0
- package/dist/viewer/ImageZoom.logic.d.ts +65 -0
- package/dist/viewer/ImageZoom.logic.js +119 -0
- package/dist/viewer/LatestRead.logic.d.ts +27 -0
- package/dist/viewer/LatestRead.logic.js +55 -0
- package/dist/viewer/MediaControls.d.ts +25 -0
- package/dist/viewer/MediaControls.js +183 -0
- package/dist/viewer/MediaPlayer.logic.d.ts +53 -0
- package/dist/viewer/MediaPlayer.logic.js +68 -0
- package/dist/viewer/PdfView.d.ts +23 -0
- package/dist/viewer/PdfView.js +25 -0
- package/dist/viewer/QuietFocus.d.ts +19 -0
- package/dist/viewer/QuietFocus.js +27 -0
- package/dist/viewer/ScaledScroll.logic.d.ts +30 -0
- package/dist/viewer/ScaledScroll.logic.js +28 -0
- package/dist/viewer/ScaledScrollbar.d.ts +19 -0
- package/dist/viewer/ScaledScrollbar.js +71 -0
- package/dist/viewer/SyntaxHighlighter.d.ts +29 -0
- package/dist/viewer/SyntaxHighlighter.js +144 -0
- package/dist/viewer/SyntaxLanguage.logic.d.ts +110 -0
- package/dist/viewer/SyntaxLanguage.logic.js +283 -0
- package/dist/viewer/SyntaxService.d.ts +75 -0
- package/dist/viewer/SyntaxService.js +49 -0
- package/dist/viewer/SyntaxSession.logic.d.ts +78 -0
- package/dist/viewer/SyntaxSession.logic.js +221 -0
- package/dist/viewer/SyntaxWorker.d.ts +6 -0
- package/dist/viewer/SyntaxWorker.js +8 -0
- package/dist/viewer/TextEncoding.logic.d.ts +35 -0
- package/dist/viewer/TextEncoding.logic.js +58 -0
- package/dist/viewer/TextRows.logic.d.ts +86 -0
- package/dist/viewer/TextRows.logic.js +241 -0
- package/dist/viewer/TextSelection.logic.d.ts +97 -0
- package/dist/viewer/TextSelection.logic.js +139 -0
- package/dist/viewer/TextView.d.ts +13 -0
- package/dist/viewer/TextView.js +448 -0
- package/dist/viewer/TextViewport.logic.d.ts +70 -0
- package/dist/viewer/TextViewport.logic.js +116 -0
- package/dist/viewer/VideoView.d.ts +18 -0
- package/dist/viewer/VideoView.js +53 -0
- package/dist/viewer/ViewerClipboard.d.ts +30 -0
- package/dist/viewer/ViewerClipboard.js +70 -0
- package/dist/viewer/ViewerFrame.logic.d.ts +61 -0
- package/dist/viewer/ViewerFrame.logic.js +130 -0
- package/dist/viewer/ViewerKeys.d.ts +16 -0
- package/dist/viewer/ViewerKeys.js +14 -0
- package/dist/viewer/ViewerScope.d.ts +12 -0
- package/dist/viewer/ViewerScope.js +22 -0
- package/dist/viewer/ViewerViews.d.ts +28 -0
- package/dist/viewer/ViewerViews.js +22 -0
- package/dist/viewer/ViewerWindow.d.ts +53 -0
- package/dist/viewer/ViewerWindow.js +219 -0
- package/dist/viewer/index.d.ts +12 -0
- package/dist/viewer/index.js +12 -0
- package/dist/viewer/types.d.ts +151 -0
- package/dist/viewer/types.js +26 -0
- package/dist/viewer/useElementHeight.d.ts +6 -0
- package/dist/viewer/useElementHeight.js +22 -0
- package/dist/viewer/useElementSize.d.ts +7 -0
- package/dist/viewer/useElementSize.js +24 -0
- package/dist/viewer/useEscapeToClear.d.ts +8 -0
- package/dist/viewer/useEscapeToClear.js +26 -0
- package/dist/viewer/useHexDocument.d.ts +41 -0
- package/dist/viewer/useHexDocument.js +144 -0
- package/dist/viewer/useMediaElement.d.ts +10 -0
- package/dist/viewer/useMediaElement.js +24 -0
- package/dist/viewer/useSyntaxHighlight.d.ts +24 -0
- package/dist/viewer/useSyntaxHighlight.js +133 -0
- package/dist/viewer/useTextDocument.d.ts +66 -0
- package/dist/viewer/useTextDocument.js +245 -0
- package/dist/viewer/viewer.css +645 -0
- package/package.json +7 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Highlighting a screen of rows without the whole file. A TextMate grammar
|
|
3
|
+
* tokenizes a line from the state the line before it left, so a row in the
|
|
4
|
+
* middle of a file needs the state at its line's start. The session keeps
|
|
5
|
+
* that state, keyed by the line's starting byte, for every line it has
|
|
6
|
+
* tokenized recently, plus a checkpoint every `CHECKPOINT_LINES` lines that
|
|
7
|
+
* it never drops. A screen's first line resumes, in this order:
|
|
8
|
+
*
|
|
9
|
+
* 1. from its own state, when the file's start leads to it ("exact");
|
|
10
|
+
* 2. by tokenizing forward from the nearest exact state or checkpoint at
|
|
11
|
+
* most `MAX_CATCHUP_BYTES` back, within the bytes held;
|
|
12
|
+
* 3. from its own state, when only an approximate one is known;
|
|
13
|
+
* 4. by tokenizing forward from the nearest approximate state in reach;
|
|
14
|
+
* 5. from the grammar's initial state, as if the file began there.
|
|
15
|
+
*
|
|
16
|
+
* A jump lands on an arbitrary byte, so step 5 is the usual first answer
|
|
17
|
+
* there: the landed line starts fresh and is marked approximate, and the
|
|
18
|
+
* file is never tokenized from its first line to reach a jump. Scrolling on
|
|
19
|
+
* from any line carries its state forward line by line, so scrolling back
|
|
20
|
+
* over tokenized lines costs nothing, and a later pass that reaches them
|
|
21
|
+
* from an exact state replaces the approximate ones.
|
|
22
|
+
*
|
|
23
|
+
* A line longer than `maxLineBytes` is not tokenized: its rows show plain,
|
|
24
|
+
* and the state before it carries on to the line after it unchanged. That
|
|
25
|
+
* is the state every run from the file's start reaches, so it stays exact
|
|
26
|
+
* when it was, and the lines after a long one are not tokenized again for
|
|
27
|
+
* want of a better state. Nothing here touches the DOM or Shiki; the
|
|
28
|
+
* tokenizer is passed in, so the worker binds Shiki and tests bind a fake.
|
|
29
|
+
*/
|
|
30
|
+
import type { TextLayout } from './TextEncoding.logic.js';
|
|
31
|
+
import { type RowStart, type TextChunk } from './TextRows.logic.js';
|
|
32
|
+
/** Lines between two checkpoints along a run of tokenized lines. */
|
|
33
|
+
export declare const CHECKPOINT_LINES = 2000;
|
|
34
|
+
/** How far back a screen may tokenize forward from a known state before it starts fresh. */
|
|
35
|
+
export declare const MAX_CATCHUP_BYTES: number;
|
|
36
|
+
/** How a token is drawn: a CSS color (null keeps the text's own) and its font style. */
|
|
37
|
+
export interface SyntaxStyle {
|
|
38
|
+
readonly color: string | null;
|
|
39
|
+
readonly italic: boolean;
|
|
40
|
+
readonly bold: boolean;
|
|
41
|
+
readonly strikethrough: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** Characters [start, end) of a line or row, drawn in style `style` of the tokenizer's styles. */
|
|
44
|
+
export interface SyntaxToken {
|
|
45
|
+
readonly start: number;
|
|
46
|
+
readonly end: number;
|
|
47
|
+
readonly style: number;
|
|
48
|
+
}
|
|
49
|
+
/** A grammar bound to a theme: tokenizes one line from the state the line before it left. */
|
|
50
|
+
export interface LineTokenizer<State> {
|
|
51
|
+
readonly initial: State;
|
|
52
|
+
readonly styles: readonly SyntaxStyle[];
|
|
53
|
+
readonly tokenizeLine: (text: string, state: State) => {
|
|
54
|
+
readonly tokens: readonly SyntaxToken[];
|
|
55
|
+
readonly state: State;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Why a row shows plain: its line is past the length limit, or the held bytes do not reach its line's ends. */
|
|
59
|
+
type PlainReason = 'long' | 'unknown';
|
|
60
|
+
/** One row's highlighting, in the row's own characters. */
|
|
61
|
+
export interface RowHighlight {
|
|
62
|
+
readonly start: number;
|
|
63
|
+
/** The row's tokens; null when it shows plain. */
|
|
64
|
+
readonly tokens: readonly SyntaxToken[] | null;
|
|
65
|
+
readonly plain: PlainReason | null;
|
|
66
|
+
/** Whether its line was tokenized from the state the file's start leads to. */
|
|
67
|
+
readonly exact: boolean;
|
|
68
|
+
}
|
|
69
|
+
/** A session over one file: the bytes it may read, and the rows to highlight. */
|
|
70
|
+
export interface SyntaxSession {
|
|
71
|
+
/** The bytes the view holds now; states found from earlier chunks stay. */
|
|
72
|
+
readonly setChunk: (chunk: TextChunk, layout: TextLayout) => void;
|
|
73
|
+
/** Highlights the rows of one screen, in order. */
|
|
74
|
+
readonly highlight: (rows: readonly RowStart[]) => readonly RowHighlight[];
|
|
75
|
+
}
|
|
76
|
+
/** A session that tokenizes with `tokenizer`, leaving lines over `maxLineBytes` plain. */
|
|
77
|
+
export declare function createSyntaxSession<State>(tokenizer: LineTokenizer<State>, maxLineBytes: number): SyntaxSession;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Highlighting a screen of rows without the whole file. A TextMate grammar
|
|
3
|
+
* tokenizes a line from the state the line before it left, so a row in the
|
|
4
|
+
* middle of a file needs the state at its line's start. The session keeps
|
|
5
|
+
* that state, keyed by the line's starting byte, for every line it has
|
|
6
|
+
* tokenized recently, plus a checkpoint every `CHECKPOINT_LINES` lines that
|
|
7
|
+
* it never drops. A screen's first line resumes, in this order:
|
|
8
|
+
*
|
|
9
|
+
* 1. from its own state, when the file's start leads to it ("exact");
|
|
10
|
+
* 2. by tokenizing forward from the nearest exact state or checkpoint at
|
|
11
|
+
* most `MAX_CATCHUP_BYTES` back, within the bytes held;
|
|
12
|
+
* 3. from its own state, when only an approximate one is known;
|
|
13
|
+
* 4. by tokenizing forward from the nearest approximate state in reach;
|
|
14
|
+
* 5. from the grammar's initial state, as if the file began there.
|
|
15
|
+
*
|
|
16
|
+
* A jump lands on an arbitrary byte, so step 5 is the usual first answer
|
|
17
|
+
* there: the landed line starts fresh and is marked approximate, and the
|
|
18
|
+
* file is never tokenized from its first line to reach a jump. Scrolling on
|
|
19
|
+
* from any line carries its state forward line by line, so scrolling back
|
|
20
|
+
* over tokenized lines costs nothing, and a later pass that reaches them
|
|
21
|
+
* from an exact state replaces the approximate ones.
|
|
22
|
+
*
|
|
23
|
+
* A line longer than `maxLineBytes` is not tokenized: its rows show plain,
|
|
24
|
+
* and the state before it carries on to the line after it unchanged. That
|
|
25
|
+
* is the state every run from the file's start reaches, so it stays exact
|
|
26
|
+
* when it was, and the lines after a long one are not tokenized again for
|
|
27
|
+
* want of a better state. Nothing here touches the DOM or Shiki; the
|
|
28
|
+
* tokenizer is passed in, so the worker binds Shiki and tests bind a fake.
|
|
29
|
+
*/
|
|
30
|
+
import { textDecoder } from './TextEncoding.logic.js';
|
|
31
|
+
import { lineStartBefore, rowAt } from './TextRows.logic.js';
|
|
32
|
+
/** Lines between two checkpoints along a run of tokenized lines. */
|
|
33
|
+
export const CHECKPOINT_LINES = 2000;
|
|
34
|
+
/** How far back a screen may tokenize forward from a known state before it starts fresh. */
|
|
35
|
+
export const MAX_CATCHUP_BYTES = 256 * 1024;
|
|
36
|
+
/** Recent line states kept besides the checkpoints; the oldest go first. */
|
|
37
|
+
const MAX_LINE_STATES = 20_000;
|
|
38
|
+
/** Clips line tokens to characters [from, to) and shifts them to start at 0. */
|
|
39
|
+
function clipTokens(tokens, from, to) {
|
|
40
|
+
const clipped = [];
|
|
41
|
+
for (const token of tokens) {
|
|
42
|
+
const start = Math.max(token.start, from);
|
|
43
|
+
const end = Math.min(token.end, to);
|
|
44
|
+
if (end > start)
|
|
45
|
+
clipped.push({ start: start - from, end: end - from, style: token.style });
|
|
46
|
+
}
|
|
47
|
+
return clipped;
|
|
48
|
+
}
|
|
49
|
+
/** A session that tokenizes with `tokenizer`, leaving lines over `maxLineBytes` plain. */
|
|
50
|
+
export function createSyntaxSession(tokenizer, maxLineBytes) {
|
|
51
|
+
const states = new Map();
|
|
52
|
+
const checkpoints = new Map();
|
|
53
|
+
let chunk = null;
|
|
54
|
+
let layout = null;
|
|
55
|
+
let decoder = null;
|
|
56
|
+
const decode = (from, to) => chunk === null || decoder === null
|
|
57
|
+
? ''
|
|
58
|
+
: decoder.decode(chunk.bytes.subarray(from - chunk.offset, to - chunk.offset));
|
|
59
|
+
/** Keeps `entry` for the line at `start`, never replacing an exact state with an approximate one. */
|
|
60
|
+
const remember = (start, entry) => {
|
|
61
|
+
const known = states.get(start);
|
|
62
|
+
if (known?.exact && !entry.exact)
|
|
63
|
+
return;
|
|
64
|
+
states.delete(start);
|
|
65
|
+
states.set(start, entry);
|
|
66
|
+
if (states.size > MAX_LINE_STATES) {
|
|
67
|
+
const oldest = states.keys().next().value;
|
|
68
|
+
if (oldest !== undefined)
|
|
69
|
+
states.delete(oldest);
|
|
70
|
+
}
|
|
71
|
+
if (entry.since === 0 && !checkpoints.get(start)?.exact)
|
|
72
|
+
checkpoints.set(start, entry);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The line starting at `start`, or 'unknown' where the held bytes end
|
|
76
|
+
* before it does. A long line whose end the bytes do not reach is still
|
|
77
|
+
* long, with no next line.
|
|
78
|
+
*/
|
|
79
|
+
const lineAt = (start) => {
|
|
80
|
+
if (chunk === null || layout === null || start < chunk.offset)
|
|
81
|
+
return 'unknown';
|
|
82
|
+
const rows = [];
|
|
83
|
+
let row = { start, kind: 'line' };
|
|
84
|
+
let long = false;
|
|
85
|
+
for (;;) {
|
|
86
|
+
const bounds = rowAt(chunk, layout, row);
|
|
87
|
+
if (bounds === null)
|
|
88
|
+
return long ? { start, rows: [], long, next: null } : 'unknown';
|
|
89
|
+
long ||= bounds.textEnd - start > maxLineBytes;
|
|
90
|
+
if (!long)
|
|
91
|
+
rows.push(bounds);
|
|
92
|
+
if (bounds.next === null || bounds.next.kind === 'line') {
|
|
93
|
+
return { start, rows: long ? [] : rows, long, next: bounds.next?.start ?? null };
|
|
94
|
+
}
|
|
95
|
+
row = bounds.next;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Tokenizes `line` from `entry`, remembers the state its next line starts
|
|
100
|
+
* in, and returns the tokens. A long line is skipped: its state carries
|
|
101
|
+
* over unchanged.
|
|
102
|
+
*/
|
|
103
|
+
const tokenize = (line, entry) => {
|
|
104
|
+
const texts = line.rows.map((row) => decode(row.start, row.textEnd));
|
|
105
|
+
const result = line.long
|
|
106
|
+
? { tokens: [], state: entry.state }
|
|
107
|
+
: tokenizer.tokenizeLine(texts.join(''), entry.state);
|
|
108
|
+
const since = entry.since + 1 >= CHECKPOINT_LINES ? 0 : entry.since + 1;
|
|
109
|
+
const after = {
|
|
110
|
+
state: result.state,
|
|
111
|
+
exact: entry.exact,
|
|
112
|
+
since
|
|
113
|
+
};
|
|
114
|
+
if (line.next !== null)
|
|
115
|
+
remember(line.next, after);
|
|
116
|
+
return { texts, tokens: result.tokens, after };
|
|
117
|
+
};
|
|
118
|
+
/** The state at `target` reached by tokenizing forward from `from`; null when a line on the way is not whole. */
|
|
119
|
+
const walk = (from, entry, target) => {
|
|
120
|
+
let start = from;
|
|
121
|
+
let state = entry;
|
|
122
|
+
while (start < target) {
|
|
123
|
+
const line = lineAt(start);
|
|
124
|
+
if (line === 'unknown' || line.next === null)
|
|
125
|
+
return null;
|
|
126
|
+
state = tokenize(line, state).after;
|
|
127
|
+
start = line.next;
|
|
128
|
+
}
|
|
129
|
+
return start === target ? state : null;
|
|
130
|
+
};
|
|
131
|
+
/** The nearest known line start before `target` in reach, exact ones only when `exact`. */
|
|
132
|
+
const nearest = (target, exact) => {
|
|
133
|
+
if (chunk === null || layout === null)
|
|
134
|
+
return null;
|
|
135
|
+
const floor = Math.max(chunk.offset, target - MAX_CATCHUP_BYTES);
|
|
136
|
+
let best = layout.contentStart >= floor && layout.contentStart < target
|
|
137
|
+
? [layout.contentStart, { state: tokenizer.initial, exact: true, since: 0 }]
|
|
138
|
+
: null;
|
|
139
|
+
for (const known of [checkpoints, states]) {
|
|
140
|
+
for (const [start, entry] of known) {
|
|
141
|
+
if (start < target && start >= floor && (entry.exact || !exact)) {
|
|
142
|
+
if (best === null || start > best[0])
|
|
143
|
+
best = [start, entry];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return best;
|
|
148
|
+
};
|
|
149
|
+
/** The state the line at `start` begins in, by the order in this module's description. */
|
|
150
|
+
const resume = (start) => {
|
|
151
|
+
if (layout !== null && start <= layout.contentStart) {
|
|
152
|
+
return { state: tokenizer.initial, exact: true, since: 0 };
|
|
153
|
+
}
|
|
154
|
+
const known = states.get(start) ?? checkpoints.get(start);
|
|
155
|
+
if (known?.exact)
|
|
156
|
+
return known;
|
|
157
|
+
for (const exact of [true, false]) {
|
|
158
|
+
if (!exact && known)
|
|
159
|
+
return known;
|
|
160
|
+
const from = nearest(start, exact);
|
|
161
|
+
const reached = from === null ? null : walk(from[0], from[1], start);
|
|
162
|
+
if (reached !== null)
|
|
163
|
+
return reached;
|
|
164
|
+
}
|
|
165
|
+
return { state: tokenizer.initial, exact: false, since: 0 };
|
|
166
|
+
};
|
|
167
|
+
const highlight = (rows) => {
|
|
168
|
+
if (chunk === null || layout === null)
|
|
169
|
+
return [];
|
|
170
|
+
const results = new Map();
|
|
171
|
+
const plain = (row, reason) => {
|
|
172
|
+
results.set(row.start, { start: row.start, tokens: null, plain: reason, exact: false });
|
|
173
|
+
};
|
|
174
|
+
let carried = null;
|
|
175
|
+
for (const row of rows) {
|
|
176
|
+
if (results.has(row.start))
|
|
177
|
+
continue;
|
|
178
|
+
const start = row.kind === 'line' ? row.start : lineStartBefore(chunk, layout, row.start, maxLineBytes);
|
|
179
|
+
if (start === null) {
|
|
180
|
+
const reachable = Math.max(layout.contentStart, row.start - maxLineBytes) >= chunk.offset;
|
|
181
|
+
plain(row, reachable ? 'long' : 'unknown');
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const line = lineAt(start);
|
|
185
|
+
if (line === 'unknown') {
|
|
186
|
+
plain(row, 'unknown');
|
|
187
|
+
carried = null;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const entry = carried?.next === start ? carried.entry : resume(start);
|
|
191
|
+
const { texts, tokens, after } = tokenize(line, entry);
|
|
192
|
+
carried = line.next === null ? null : { next: line.next, entry: after };
|
|
193
|
+
if (line.long) {
|
|
194
|
+
plain(row, 'long');
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
let offset = 0;
|
|
198
|
+
line.rows.forEach((bounds, index) => {
|
|
199
|
+
const length = texts[index]?.length ?? 0;
|
|
200
|
+
results.set(bounds.start, {
|
|
201
|
+
start: bounds.start,
|
|
202
|
+
tokens: clipTokens(tokens, offset, offset + length),
|
|
203
|
+
plain: null,
|
|
204
|
+
exact: entry.exact
|
|
205
|
+
});
|
|
206
|
+
offset += length;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return rows.flatMap((row) => results.get(row.start) ?? []);
|
|
210
|
+
};
|
|
211
|
+
return {
|
|
212
|
+
setChunk: (next, nextLayout) => {
|
|
213
|
+
if (layout === null || nextLayout.encoding !== layout.encoding) {
|
|
214
|
+
decoder = textDecoder(nextLayout.encoding);
|
|
215
|
+
}
|
|
216
|
+
chunk = next;
|
|
217
|
+
layout = nextLayout;
|
|
218
|
+
},
|
|
219
|
+
highlight
|
|
220
|
+
};
|
|
221
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The highlighting worker's entry: Shiki on its JavaScript regex engine,
|
|
3
|
+
* serving the text view's requests (`SyntaxService.ts`). Grammars and the
|
|
4
|
+
* theme load as chunks of their own when the page names a language.
|
|
5
|
+
*/
|
|
6
|
+
import { loadLineTokenizer } from './SyntaxHighlighter.js';
|
|
7
|
+
import { serveSyntax } from './SyntaxService.js';
|
|
8
|
+
serveSyntax(self, loadLineTokenizer);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text view's encoding detection: a byte order mark first, then whether
|
|
3
|
+
* the head of the file is valid UTF-8, else Latin-1. Detection reads only the
|
|
4
|
+
* head the view already read for its first screen; no decoder beyond the
|
|
5
|
+
* platform's `TextDecoder` is involved.
|
|
6
|
+
*/
|
|
7
|
+
import type { ViewerTextEncoding } from './types.js';
|
|
8
|
+
import type { ViewerLabels } from '../labels/viewer.js';
|
|
9
|
+
/** How much of the head the UTF-8 check decodes: enough to meet a stray Latin-1 byte early. */
|
|
10
|
+
export declare const ENCODING_SNIFF_BYTES: number;
|
|
11
|
+
/** A detected encoding and where the text begins, past any byte order mark. */
|
|
12
|
+
export interface TextLayout {
|
|
13
|
+
readonly encoding: ViewerTextEncoding;
|
|
14
|
+
/** Bytes of the byte order mark the file starts with; the text starts after them. */
|
|
15
|
+
readonly contentStart: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The encoding of a file from its first bytes. A UTF-8 mark gives UTF-8, a
|
|
19
|
+
* UTF-16 mark gives UTF-16 in its byte order; without a mark, a head that
|
|
20
|
+
* decodes as UTF-8 gives UTF-8 and anything else Latin-1. A character cut
|
|
21
|
+
* off where the sample stops is fine, unless `wholeFile` says the head runs
|
|
22
|
+
* to the end of the file and the sample holds all of it: then the cut-off
|
|
23
|
+
* bytes are the file's last, and not UTF-8. UTF-16 without a mark is not
|
|
24
|
+
* detected: it sniffs as binary before it reaches the text view.
|
|
25
|
+
*/
|
|
26
|
+
export declare function detectEncoding(head: Uint8Array, wholeFile?: boolean): TextLayout;
|
|
27
|
+
/** The encoding as the status bar names it. */
|
|
28
|
+
export declare function encodingLabel({ encoding, contentStart }: TextLayout, labels: ViewerLabels): string;
|
|
29
|
+
/**
|
|
30
|
+
* A decoder for the encoding that keeps a mark in the middle of the text as
|
|
31
|
+
* a character and turns an invalid sequence into U+FFFD instead of failing.
|
|
32
|
+
* The platform's `latin1` label decodes as Windows-1252, the superset every
|
|
33
|
+
* browser uses for it, so 80 to 9F show as printable characters.
|
|
34
|
+
*/
|
|
35
|
+
export declare function textDecoder(encoding: ViewerTextEncoding): TextDecoder;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text view's encoding detection: a byte order mark first, then whether
|
|
3
|
+
* the head of the file is valid UTF-8, else Latin-1. Detection reads only the
|
|
4
|
+
* head the view already read for its first screen; no decoder beyond the
|
|
5
|
+
* platform's `TextDecoder` is involved.
|
|
6
|
+
*/
|
|
7
|
+
/** How much of the head the UTF-8 check decodes: enough to meet a stray Latin-1 byte early. */
|
|
8
|
+
export const ENCODING_SNIFF_BYTES = 64 * 1024;
|
|
9
|
+
/**
|
|
10
|
+
* The encoding of a file from its first bytes. A UTF-8 mark gives UTF-8, a
|
|
11
|
+
* UTF-16 mark gives UTF-16 in its byte order; without a mark, a head that
|
|
12
|
+
* decodes as UTF-8 gives UTF-8 and anything else Latin-1. A character cut
|
|
13
|
+
* off where the sample stops is fine, unless `wholeFile` says the head runs
|
|
14
|
+
* to the end of the file and the sample holds all of it: then the cut-off
|
|
15
|
+
* bytes are the file's last, and not UTF-8. UTF-16 without a mark is not
|
|
16
|
+
* detected: it sniffs as binary before it reaches the text view.
|
|
17
|
+
*/
|
|
18
|
+
export function detectEncoding(head, wholeFile = false) {
|
|
19
|
+
if (head[0] === 0xef && head[1] === 0xbb && head[2] === 0xbf) {
|
|
20
|
+
return { encoding: 'utf-8', contentStart: 3 };
|
|
21
|
+
}
|
|
22
|
+
if (head[0] === 0xff && head[1] === 0xfe)
|
|
23
|
+
return { encoding: 'utf-16le', contentStart: 2 };
|
|
24
|
+
if (head[0] === 0xfe && head[1] === 0xff)
|
|
25
|
+
return { encoding: 'utf-16be', contentStart: 2 };
|
|
26
|
+
const sample = head.subarray(0, ENCODING_SNIFF_BYTES);
|
|
27
|
+
try {
|
|
28
|
+
new TextDecoder('utf-8', { fatal: true }).decode(sample, {
|
|
29
|
+
stream: !wholeFile || sample.length < head.length
|
|
30
|
+
});
|
|
31
|
+
return { encoding: 'utf-8', contentStart: 0 };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return { encoding: 'latin-1', contentStart: 0 };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The encoding as the status bar names it. */
|
|
38
|
+
export function encodingLabel({ encoding, contentStart }, labels) {
|
|
39
|
+
switch (encoding) {
|
|
40
|
+
case 'utf-8':
|
|
41
|
+
return contentStart > 0 ? labels.text.utf8Bom : 'UTF-8';
|
|
42
|
+
case 'utf-16le':
|
|
43
|
+
return 'UTF-16 LE';
|
|
44
|
+
case 'utf-16be':
|
|
45
|
+
return 'UTF-16 BE';
|
|
46
|
+
case 'latin-1':
|
|
47
|
+
return 'Latin-1';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A decoder for the encoding that keeps a mark in the middle of the text as
|
|
52
|
+
* a character and turns an invalid sequence into U+FFFD instead of failing.
|
|
53
|
+
* The platform's `latin1` label decodes as Windows-1252, the superset every
|
|
54
|
+
* browser uses for it, so 80 to 9F show as printable characters.
|
|
55
|
+
*/
|
|
56
|
+
export function textDecoder(encoding) {
|
|
57
|
+
return new TextDecoder(encoding === 'latin-1' ? 'latin1' : encoding, { ignoreBOM: true });
|
|
58
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text view's row model, the way `less` reads a file: from any byte, back
|
|
3
|
+
* up to the start of its row, then read forward one screenful. Nothing here
|
|
4
|
+
* needs the whole file, only a chunk of bytes around the place in view.
|
|
5
|
+
*
|
|
6
|
+
* A row starts at every line start. A line longer than a segment is split
|
|
7
|
+
* into display segments, so a gigabyte with no line break is still drawn one
|
|
8
|
+
* bounded row at a time. Where a line breaks is a pure function of the bytes
|
|
9
|
+
* near the break, never of where the line began: a line starting at `L`
|
|
10
|
+
* breaks at the first multiple of `SEGMENT_BYTES` at least one segment past
|
|
11
|
+
* `L`, then at every multiple after it, each moved forward to the next
|
|
12
|
+
* character boundary, and never right before the line's own line break. So a
|
|
13
|
+
* row is found the same way scrolling down to it, scrolling up to it, or
|
|
14
|
+
* jumping into the middle of a line whose start lies a gigabyte back, and
|
|
15
|
+
* the reader never has to look further back than `LOOKBEHIND_BYTES`.
|
|
16
|
+
*
|
|
17
|
+
* Rows carry byte offsets and their decoded text, so the plain view draws
|
|
18
|
+
* them and syntax highlighting can tokenize the same rows later.
|
|
19
|
+
*/
|
|
20
|
+
import type { TextLayout } from './TextEncoding.logic.js';
|
|
21
|
+
/** The longest stretch of one line drawn as one row before it is split. */
|
|
22
|
+
export declare const SEGMENT_BYTES = 1024;
|
|
23
|
+
/**
|
|
24
|
+
* The longest a row can be: the first row of a long line runs up to the
|
|
25
|
+
* second segment boundary past its start, plus a character's realignment and
|
|
26
|
+
* a line break kept on its row.
|
|
27
|
+
*/
|
|
28
|
+
export declare const MAX_ROW_BYTES: number;
|
|
29
|
+
/** How far back from a byte the reader may look to find the start of its row. */
|
|
30
|
+
export declare const LOOKBEHIND_BYTES: number;
|
|
31
|
+
/** A row begins a line, or continues one past a segment boundary. */
|
|
32
|
+
export type RowKind = 'line' | 'continuation';
|
|
33
|
+
/** Where a row starts. */
|
|
34
|
+
export interface RowStart {
|
|
35
|
+
readonly start: number;
|
|
36
|
+
readonly kind: RowKind;
|
|
37
|
+
}
|
|
38
|
+
/** One row: its bytes are [start, end); its text is [start, textEnd), without the line break. */
|
|
39
|
+
export interface RowBounds extends RowStart {
|
|
40
|
+
readonly end: number;
|
|
41
|
+
readonly textEnd: number;
|
|
42
|
+
/** The row after it, or null when this row ends the file. */
|
|
43
|
+
readonly next: RowStart | null;
|
|
44
|
+
}
|
|
45
|
+
/** Bytes read from the file at `offset`; `eof` says they run to the end of the file. */
|
|
46
|
+
export interface TextChunk {
|
|
47
|
+
readonly offset: number;
|
|
48
|
+
readonly bytes: Uint8Array;
|
|
49
|
+
readonly eof: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The first character boundary at or after `position`: past UTF-8
|
|
53
|
+
* continuation bytes, onto an even offset for UTF-16, and past the low half
|
|
54
|
+
* of a surrogate pair. Looks at no more than four bytes.
|
|
55
|
+
*/
|
|
56
|
+
export declare function alignToCharacter(chunk: TextChunk, layout: TextLayout, position: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* The row starting at `row`, or null when the chunk ends before its end can
|
|
59
|
+
* be known. `row` must be a row start: a line start or a segment break.
|
|
60
|
+
*/
|
|
61
|
+
export declare function rowAt(chunk: TextChunk, layout: TextLayout, row: RowStart): RowBounds | null;
|
|
62
|
+
/**
|
|
63
|
+
* The row holding byte `position`: back up to the last line start within
|
|
64
|
+
* `LOOKBEHIND_BYTES`, or, inside a longer line, to a segment break that
|
|
65
|
+
* line is sure to have, then walk forward to the row. Null for an empty file.
|
|
66
|
+
* A position past the end of the file finds the last row.
|
|
67
|
+
*/
|
|
68
|
+
export declare function rowContaining(chunk: TextChunk, layout: TextLayout, position: number): RowStart | null;
|
|
69
|
+
/**
|
|
70
|
+
* The start of the line holding byte `position`, looking back no further
|
|
71
|
+
* than `maxBytes`; null when no line starts that close in the chunk (the
|
|
72
|
+
* line is longer, or the chunk does not reach back far enough).
|
|
73
|
+
*/
|
|
74
|
+
export declare function lineStartBefore(chunk: TextChunk, layout: TextLayout, position: number, maxBytes: number): number | null;
|
|
75
|
+
/**
|
|
76
|
+
* Up to `count` rows from `row` onward, and whether they reach the end of
|
|
77
|
+
* the file. Stops early, with `end` false, where the chunk runs out.
|
|
78
|
+
*/
|
|
79
|
+
export declare function rowsFrom(chunk: TextChunk, layout: TextLayout, row: RowStart, count: number): {
|
|
80
|
+
readonly rows: readonly RowBounds[];
|
|
81
|
+
readonly end: boolean;
|
|
82
|
+
};
|
|
83
|
+
/** Up to `count` row starts before `row`, nearest last; fewer at the start of the file. */
|
|
84
|
+
export declare function rowsBefore(chunk: TextChunk, layout: TextLayout, row: RowStart, count: number): readonly RowStart[];
|
|
85
|
+
/** Line breaks starting in [from, to), as far as the chunk holds them. */
|
|
86
|
+
export declare function countLineBreaks(chunk: TextChunk, layout: TextLayout, from: number, to: number): number;
|