blots-editor 0.0.15 → 0.0.16

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.
@@ -20,6 +20,23 @@ export declare class Document {
20
20
  private _wrapWidth;
21
21
  private _viewScrollTop;
22
22
  private _scrollRemainder;
23
+ /**
24
+ * Per-line soft-wrap segment cache. Segments are derived purely from
25
+ * `_lines` + `_wrapWidth`, but recomputing them is expensive (binary
26
+ * searches + per-character tab expansion), and the render path queries
27
+ * them O(viewport) times per frame — so identical inputs must not be
28
+ * recomputed. null while invalidated; rebuilt lazily on first use.
29
+ */
30
+ private _segmentCache;
31
+ /**
32
+ * Prefix sums of segment counts: `prefix[i]` = visual lines occupied by
33
+ * the first `i` logical lines (so `prefix[line]` is the global visual
34
+ * line where logical line `line` starts). Lets `visualLineAt` /
35
+ * `cursorVisualLine` / `visualLineCount` answer in O(log n)/O(1)
36
+ * instead of scanning every line. Invalidated together with
37
+ * `_segmentCache`.
38
+ */
39
+ private _segPrefix;
23
40
  readonly indentWidth: number;
24
41
  constructor(text: string, options?: DocumentOptions);
25
42
  get lineCount(): number;
@@ -45,14 +62,36 @@ export declare class Document {
45
62
  get wrapWidth(): number;
46
63
  /** Set the soft-wrap width (clamped to at least 1 cell). */
47
64
  setWrapWidth(width: number): void;
48
- /** The soft-wrap segments of one logical line (an empty line is one empty segment). */
65
+ /**
66
+ * The soft-wrap segments of one logical line (an empty line is one empty
67
+ * segment). Cached: the render path queries these O(viewport) times per
68
+ * frame, and every hit would otherwise re-run `segmentFrom` (binary
69
+ * search + per-character tab expansion) on unchanged input.
70
+ */
49
71
  private lineSegments;
72
+ /**
73
+ * Prefix sums of segment counts, built lazily from the segment cache.
74
+ * Both caches are invalidated together (see {@link invalidateIndex}), so
75
+ * they can never disagree once built.
76
+ */
77
+ private buildSegmentPrefix;
78
+ /**
79
+ * Drop the segment cache and the visual-line index. Every mutation entry
80
+ * point (`setText`, `setLine`, `insertLineAt`, `removeLineAt`, and a
81
+ * *changed* `setWrapWidth`) must call this, or queries would keep serving
82
+ * stale segments. Rebuilding is lazy — the next query pays O(total
83
+ * segments) once.
84
+ */
85
+ private invalidateIndex;
50
86
  /** Total number of visual (soft-wrapped) lines across the document. */
51
87
  get visualLineCount(): number;
52
88
  /**
53
89
  * Map a global visual line to its logical line and the segment's logical
54
90
  * span. `first` marks the segment that starts at the logical line start
55
- * (the one that carries the line number).
91
+ * (the one that carries the line number). Binary search over the segment
92
+ * prefix sums keeps this O(log n) — the render loop calls it once per
93
+ * visible line, so a linear scan here would be O(viewport × lines) every
94
+ * frame.
56
95
  */
57
96
  visualLineAt(vline: number): {
58
97
  line: number;
@@ -20,6 +20,23 @@ export class Document {
20
20
  _viewScrollTop = null;
21
21
  // Fractional view-scroll remainder, so e.g. 3.5 cells/notch scrolls 3,4,3,4…
22
22
  _scrollRemainder = 0;
23
+ /**
24
+ * Per-line soft-wrap segment cache. Segments are derived purely from
25
+ * `_lines` + `_wrapWidth`, but recomputing them is expensive (binary
26
+ * searches + per-character tab expansion), and the render path queries
27
+ * them O(viewport) times per frame — so identical inputs must not be
28
+ * recomputed. null while invalidated; rebuilt lazily on first use.
29
+ */
30
+ _segmentCache = null;
31
+ /**
32
+ * Prefix sums of segment counts: `prefix[i]` = visual lines occupied by
33
+ * the first `i` logical lines (so `prefix[line]` is the global visual
34
+ * line where logical line `line` starts). Lets `visualLineAt` /
35
+ * `cursorVisualLine` / `visualLineCount` answer in O(log n)/O(1)
36
+ * instead of scanning every line. Invalidated together with
37
+ * `_segmentCache`.
38
+ */
39
+ _segPrefix = null;
23
40
  indentWidth;
24
41
  constructor(text, options = {}) {
25
42
  this._lines = text.split(/\r?\n/).map((t) => new TextLine(t));
@@ -56,6 +73,7 @@ export class Document {
56
73
  this._scrollTop = 0;
57
74
  this._viewScrollTop = null;
58
75
  this._scrollRemainder = 0;
76
+ this.invalidateIndex();
59
77
  }
60
78
  get cursor() {
61
79
  const line = this._lines[this._cursor.line];
@@ -74,68 +92,123 @@ export class Document {
74
92
  }
75
93
  /** Set the soft-wrap width (clamped to at least 1 cell). */
76
94
  setWrapWidth(width) {
77
- this._wrapWidth = Math.max(1, width);
95
+ const w = Math.max(1, width);
96
+ // The view calls this on every render with the measured width; only a
97
+ // real change invalidates the cache, or it would be rebuilt (and
98
+ // discarded) every frame.
99
+ if (w !== this._wrapWidth) {
100
+ this._wrapWidth = w;
101
+ this.invalidateIndex();
102
+ }
78
103
  }
79
- /** The soft-wrap segments of one logical line (an empty line is one empty segment). */
104
+ /**
105
+ * The soft-wrap segments of one logical line (an empty line is one empty
106
+ * segment). Cached: the render path queries these O(viewport) times per
107
+ * frame, and every hit would otherwise re-run `segmentFrom` (binary
108
+ * search + per-character tab expansion) on unchanged input.
109
+ */
80
110
  lineSegments(line) {
111
+ const cached = this._segmentCache?.[line];
112
+ if (cached) {
113
+ return cached;
114
+ }
81
115
  const tl = this._lines[line];
116
+ const segs = [];
82
117
  if (tl.text.length === 0) {
83
- return [{ start: 0, end: 0, text: "" }];
118
+ segs.push({ start: 0, end: 0, text: "" });
84
119
  }
85
- const segs = [];
86
- let segVisual = 0;
87
- let segStart = 0;
88
- while (segStart < tl.text.length) {
89
- const seg = tl.segmentFrom(segVisual, this._wrapWidth);
90
- segs.push({ start: segStart, end: seg.endLogical, text: seg.text });
91
- if (seg.text === "") {
92
- break;
120
+ else {
121
+ let segVisual = 0;
122
+ let segStart = 0;
123
+ while (segStart < tl.text.length) {
124
+ const seg = tl.segmentFrom(segVisual, this._wrapWidth);
125
+ segs.push({ start: segStart, end: seg.endLogical, text: seg.text });
126
+ if (seg.text === "") {
127
+ break;
128
+ }
129
+ segStart = seg.endLogical;
130
+ segVisual = seg.endVisual;
93
131
  }
94
- segStart = seg.endLogical;
95
- segVisual = seg.endVisual;
96
132
  }
133
+ if (!this._segmentCache) {
134
+ this._segmentCache = [];
135
+ }
136
+ this._segmentCache[line] = segs;
97
137
  return segs;
98
138
  }
99
- /** Total number of visual (soft-wrapped) lines across the document. */
100
- get visualLineCount() {
139
+ /**
140
+ * Prefix sums of segment counts, built lazily from the segment cache.
141
+ * Both caches are invalidated together (see {@link invalidateIndex}), so
142
+ * they can never disagree once built.
143
+ */
144
+ buildSegmentPrefix() {
145
+ if (this._segPrefix) {
146
+ return this._segPrefix;
147
+ }
148
+ const prefix = [0];
101
149
  let total = 0;
102
150
  for (let i = 0; i < this._lines.length; i++) {
103
151
  total += this.lineSegments(i).length;
152
+ prefix.push(total);
104
153
  }
105
- return total;
154
+ this._segPrefix = prefix;
155
+ return prefix;
156
+ }
157
+ /**
158
+ * Drop the segment cache and the visual-line index. Every mutation entry
159
+ * point (`setText`, `setLine`, `insertLineAt`, `removeLineAt`, and a
160
+ * *changed* `setWrapWidth`) must call this, or queries would keep serving
161
+ * stale segments. Rebuilding is lazy — the next query pays O(total
162
+ * segments) once.
163
+ */
164
+ invalidateIndex() {
165
+ this._segmentCache = null;
166
+ this._segPrefix = null;
167
+ }
168
+ /** Total number of visual (soft-wrapped) lines across the document. */
169
+ get visualLineCount() {
170
+ return this.buildSegmentPrefix()[this._lines.length];
106
171
  }
107
172
  /**
108
173
  * Map a global visual line to its logical line and the segment's logical
109
174
  * span. `first` marks the segment that starts at the logical line start
110
- * (the one that carries the line number).
175
+ * (the one that carries the line number). Binary search over the segment
176
+ * prefix sums keeps this O(log n) — the render loop calls it once per
177
+ * visible line, so a linear scan here would be O(viewport × lines) every
178
+ * frame.
111
179
  */
112
180
  visualLineAt(vline) {
113
181
  if (vline < 0) {
114
182
  return null;
115
183
  }
116
- let remaining = vline;
117
- for (let line = 0; line < this._lines.length; line++) {
118
- const segs = this.lineSegments(line);
119
- if (remaining < segs.length) {
120
- return {
121
- line,
122
- start: segs[remaining].start,
123
- end: segs[remaining].end,
124
- first: remaining === 0,
125
- text: segs[remaining].text,
126
- };
184
+ const prefix = this.buildSegmentPrefix();
185
+ if (vline >= prefix[this._lines.length]) {
186
+ return null;
187
+ }
188
+ // Largest line index whose prefix start is <= vline.
189
+ let lo = 0;
190
+ let hi = this._lines.length - 1;
191
+ while (lo < hi) {
192
+ const mid = (lo + hi + 1) >> 1;
193
+ if (prefix[mid] <= vline) {
194
+ lo = mid;
195
+ }
196
+ else {
197
+ hi = mid - 1;
127
198
  }
128
- remaining -= segs.length;
129
199
  }
130
- return null;
200
+ const seg = this.lineSegments(lo)[vline - prefix[lo]];
201
+ return {
202
+ line: lo,
203
+ start: seg.start,
204
+ end: seg.end,
205
+ first: vline === prefix[lo],
206
+ text: seg.text,
207
+ };
131
208
  }
132
209
  /** Global visual line the cursor sits on. */
133
210
  get cursorVisualLine() {
134
211
  const { line, logical } = this._cursor;
135
- let v = 0;
136
- for (let i = 0; i < line; i++) {
137
- v += this.lineSegments(i).length;
138
- }
139
212
  const segs = this.lineSegments(line);
140
213
  let idx = 0;
141
214
  // A cursor exactly on a segment boundary belongs to the segment
@@ -144,7 +217,7 @@ export class Document {
144
217
  while (idx < segs.length - 1 && logical >= segs[idx + 1].start) {
145
218
  idx++;
146
219
  }
147
- return v + idx;
220
+ return this.buildSegmentPrefix()[line] + idx;
148
221
  }
149
222
  /** Visual offset of the cursor within its current soft-wrap segment. */
150
223
  get cursorSegmentVisual() {
@@ -175,14 +248,18 @@ export class Document {
175
248
  }
176
249
  setLine(i, text) {
177
250
  this._lines[i] = new TextLine(text);
251
+ this.invalidateIndex();
178
252
  }
179
253
  /** Insert a line at index `i` (0 <= i <= lineCount), shifting the rest down. */
180
254
  insertLineAt(i, text) {
181
255
  this._lines.splice(i, 0, new TextLine(text));
256
+ this.invalidateIndex();
182
257
  }
183
258
  /** Remove line `i` and return its text. */
184
259
  removeLineAt(i) {
185
- return this._lines.splice(i, 1)[0].text;
260
+ const removed = this._lines.splice(i, 1)[0].text;
261
+ this.invalidateIndex();
262
+ return removed;
186
263
  }
187
264
  /** Set the cursor, clamping out-of-range values (line to `[0, lineCount-1]`, column to end of line). */
188
265
  setCursor(line, logical) {
@@ -122,7 +122,7 @@ export function ToolBar({ currentMode, session, openFileTree, openSettings, file
122
122
  }),
123
123
  boundKeyboard(["return"], () => {
124
124
  items[index].onClick();
125
- }),
125
+ }, { mode: "normal" }),
126
126
  ];
127
127
  return () => {
128
128
  unbinds.forEach((unbind) => unbind());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blots-editor",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "A lightweight Markdown editor for the terminal, built on ink-cartridge",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",