@tooee/renderers 0.11.0 → 0.13.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/dist/chooser/choose-filter.d.ts.map +1 -1
- package/dist/chooser/choose-filter.js.map +1 -1
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
- package/dist/chooser/choose-highlighted-text.js.map +1 -1
- package/dist/chooser/choose-list.d.ts.map +1 -1
- package/dist/chooser/choose-list.js.map +1 -1
- package/dist/chooser/choose-store.d.ts +3 -3
- package/dist/chooser/choose-store.d.ts.map +1 -1
- package/dist/chooser/choose-store.js.map +1 -1
- package/dist/chooser/chooser.d.ts.map +1 -1
- package/dist/chooser/chooser.js.map +1 -1
- package/dist/chooser/source.d.ts.map +1 -1
- package/dist/chooser/source.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.js.map +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts.map +1 -1
- package/dist/markdown/blocks/heading.js.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
- package/dist/markdown/blocks/paragraph.js.map +1 -1
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts.map +1 -1
- package/dist/markdown/links.js.map +1 -1
- package/dist/markdown/plain-text.d.ts +1 -1
- package/dist/markdown/plain-text.d.ts.map +1 -1
- package/dist/markdown/plain-text.js +1 -1
- package/dist/markdown/plain-text.js.map +1 -1
- package/dist/markdown/table-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js.map +1 -1
- package/dist/markdown/tokens.d.ts.map +1 -1
- package/dist/markdown/tokens.js.map +1 -1
- package/dist/markdown/visit-inline.d.ts.map +1 -1
- package/dist/markdown/visit-inline.js.map +1 -1
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts +6 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js +2 -2
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +8 -4
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js.map +1 -1
- package/dist/row-document-renderable.d.ts +11 -1
- package/dist/row-document-renderable.d.ts.map +1 -1
- package/dist/row-document-renderable.js +16 -7
- package/dist/row-document-renderable.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js.map +1 -1
- package/dist/text-table-internals.d.ts.map +1 -1
- package/dist/text-table-internals.js.map +1 -1
- package/package.json +6 -6
- package/src/chooser/choose-filter.tsx +1 -0
- package/src/chooser/choose-highlighted-text.tsx +2 -0
- package/src/chooser/choose-list.tsx +6 -0
- package/src/chooser/choose-store.ts +20 -3
- package/src/chooser/chooser.tsx +2 -0
- package/src/chooser/source.ts +5 -0
- package/src/chooser/use-choose.ts +44 -0
- package/src/code-blocks.tsx +14 -0
- package/src/command-palette.tsx +4 -0
- package/src/context-menu.tsx +13 -0
- package/src/index.ts +44 -0
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +13 -0
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +3 -0
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +7 -0
- package/src/markdown/links.ts +26 -0
- package/src/markdown/plain-text.ts +6 -1
- package/src/markdown/table-links.ts +12 -0
- package/src/markdown/tokens.ts +2 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +31 -0
- package/src/markdown-images.ts +21 -0
- package/src/markdown-view.tsx +11 -1
- package/src/mermaid.ts +21 -4
- package/src/parsers.ts +39 -0
- package/src/row-document-renderable.ts +72 -7
- package/src/source.ts +18 -0
- package/src/table.tsx +32 -0
- package/src/text-table-internals.ts +12 -0
|
@@ -27,6 +27,9 @@ export interface VisibleRowRange {
|
|
|
27
27
|
lastRow: number;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** Where `scrollToRow` puts a row in the viewport. */
|
|
31
|
+
export type RowScrollAlign = "nearest" | "start" | "center" | "end";
|
|
32
|
+
|
|
30
33
|
export interface RowDocumentOptions extends ScrollBoxOptions {
|
|
31
34
|
mode?: "auto" | "multi" | "provider";
|
|
32
35
|
|
|
@@ -68,10 +71,12 @@ const colorCache = new Map<string, RGBA>();
|
|
|
68
71
|
|
|
69
72
|
const cachedColor = function cachedColor(hex: string): RGBA {
|
|
70
73
|
let c = colorCache.get(hex);
|
|
74
|
+
|
|
71
75
|
if (!c) {
|
|
72
76
|
c = RGBA.fromHex(hex);
|
|
73
77
|
colorCache.set(hex, c);
|
|
74
78
|
}
|
|
79
|
+
|
|
75
80
|
return c;
|
|
76
81
|
};
|
|
77
82
|
|
|
@@ -90,6 +95,7 @@ const normalizeDecorationLayers = function normalizeDecorationLayers(
|
|
|
90
95
|
if (!layers || layers.length === 0) {
|
|
91
96
|
return [];
|
|
92
97
|
}
|
|
98
|
+
|
|
93
99
|
return [...layers].toSorted((a, b) => a.priority - b.priority);
|
|
94
100
|
};
|
|
95
101
|
|
|
@@ -102,6 +108,7 @@ const maxGutterNumber = function maxGutterNumber(opts: {
|
|
|
102
108
|
if (opts.rowNumbers && opts.rowNumbers.length > 0) {
|
|
103
109
|
return Math.max(...opts.rowNumbers);
|
|
104
110
|
}
|
|
111
|
+
|
|
105
112
|
return (opts.lineNumberStart ?? 1) + opts.rowCount - 1;
|
|
106
113
|
};
|
|
107
114
|
|
|
@@ -114,11 +121,14 @@ export const computeRowDocumentGutterWidth = function computeRowDocumentGutterWi
|
|
|
114
121
|
gutterPaddingRight?: number;
|
|
115
122
|
}): number {
|
|
116
123
|
let width = 0;
|
|
124
|
+
|
|
117
125
|
if (opts.showLineNumbers) {
|
|
118
126
|
width += Math.max(String(maxGutterNumber(opts)).length, 1);
|
|
119
127
|
}
|
|
128
|
+
|
|
120
129
|
width += opts.signColumnWidth ?? 0;
|
|
121
130
|
width += opts.gutterPaddingRight ?? 1;
|
|
131
|
+
|
|
122
132
|
return width;
|
|
123
133
|
};
|
|
124
134
|
|
|
@@ -245,6 +255,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
245
255
|
if (row < 0 || row >= this._rowCount) {
|
|
246
256
|
return null;
|
|
247
257
|
}
|
|
258
|
+
|
|
248
259
|
return {
|
|
249
260
|
row,
|
|
250
261
|
virtualHeight: this._rowVirtualHeights[row],
|
|
@@ -255,16 +266,20 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
255
266
|
getRowAtVirtualY(y: number): number {
|
|
256
267
|
const clamped = Math.max(0, Math.min(y, this._virtualRowToRow.length - 1));
|
|
257
268
|
const row = this._virtualRowToRow[clamped];
|
|
269
|
+
|
|
258
270
|
if (row >= 0) {
|
|
259
271
|
return row;
|
|
260
272
|
}
|
|
273
|
+
|
|
261
274
|
// Gap row — search backward for nearest valid row
|
|
262
275
|
for (let i = clamped - 1; i >= 0; i -= 1) {
|
|
263
276
|
const r = this._virtualRowToRow[i];
|
|
277
|
+
|
|
264
278
|
if (r >= 0) {
|
|
265
279
|
return r;
|
|
266
280
|
}
|
|
267
281
|
}
|
|
282
|
+
|
|
268
283
|
return 0;
|
|
269
284
|
}
|
|
270
285
|
|
|
@@ -282,22 +297,28 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
282
297
|
*/
|
|
283
298
|
getRowAtScreenY(screenY: number): number | null {
|
|
284
299
|
const localY = screenY - this.viewport.y;
|
|
300
|
+
|
|
285
301
|
if (localY < 0 || localY >= this.viewport.height) {
|
|
286
302
|
return null;
|
|
287
303
|
}
|
|
304
|
+
|
|
288
305
|
const virtualY = Math.floor(this.scrollTop) + localY;
|
|
306
|
+
|
|
289
307
|
if (virtualY < 0 || virtualY >= this._virtualRowToRow.length) {
|
|
290
308
|
return null;
|
|
291
309
|
}
|
|
310
|
+
|
|
292
311
|
if (virtualY >= this._contentVirtualEnd) {
|
|
293
312
|
return null;
|
|
294
313
|
}
|
|
314
|
+
|
|
295
315
|
return this.getRowAtVirtualY(virtualY);
|
|
296
316
|
}
|
|
297
317
|
|
|
298
318
|
getVisibleRange(): VisibleRowRange {
|
|
299
319
|
const top = Math.floor(this.scrollTop);
|
|
300
320
|
const bottom = top + this.viewport.height;
|
|
321
|
+
|
|
301
322
|
return {
|
|
302
323
|
firstRow: this.getRowAtVirtualY(top),
|
|
303
324
|
lastRow: this.getRowAtVirtualY(bottom - 1),
|
|
@@ -318,38 +339,53 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
318
339
|
// scrollToRow
|
|
319
340
|
// -----------------------------------------------------------------------
|
|
320
341
|
|
|
321
|
-
|
|
342
|
+
/**
|
|
343
|
+
* Scroll so `row` is in view at `align`. `margin` keeps that many lines
|
|
344
|
+
* between the row and the viewport edge it aligns to: `start` puts the row
|
|
345
|
+
* `margin` lines below the top, `end` puts it `margin` lines above the
|
|
346
|
+
* bottom, and `nearest` scrolls only when the row is inside the margin. The
|
|
347
|
+
* margin shrinks so the row and both margins fit the viewport. `center`
|
|
348
|
+
* ignores it.
|
|
349
|
+
*/
|
|
350
|
+
scrollToRow(row: number, align: RowScrollAlign = "nearest", margin = 0): void {
|
|
322
351
|
const metrics = this.getRowMetrics(row);
|
|
352
|
+
|
|
323
353
|
if (!metrics) {
|
|
324
354
|
return;
|
|
325
355
|
}
|
|
326
356
|
|
|
327
357
|
const vpHeight = this.viewport.height;
|
|
328
358
|
const { virtualTop, virtualHeight } = metrics;
|
|
359
|
+
const gap = Math.max(0, Math.min(margin, Math.floor((vpHeight - virtualHeight) / 2)));
|
|
329
360
|
|
|
330
361
|
let target = this.scrollTop;
|
|
331
362
|
|
|
332
363
|
switch (align) {
|
|
333
364
|
case "start": {
|
|
334
|
-
target = virtualTop;
|
|
365
|
+
target = virtualTop - gap;
|
|
335
366
|
break;
|
|
336
367
|
}
|
|
368
|
+
|
|
337
369
|
case "center": {
|
|
338
370
|
target = virtualTop - (vpHeight - virtualHeight) / 2;
|
|
339
371
|
break;
|
|
340
372
|
}
|
|
373
|
+
|
|
341
374
|
case "end": {
|
|
342
|
-
target = virtualTop + virtualHeight - vpHeight;
|
|
375
|
+
target = virtualTop + virtualHeight - vpHeight + gap;
|
|
343
376
|
break;
|
|
344
377
|
}
|
|
378
|
+
|
|
345
379
|
case "nearest": {
|
|
346
|
-
if (virtualTop < this.scrollTop) {
|
|
347
|
-
target = virtualTop;
|
|
348
|
-
} else if (virtualTop + virtualHeight > this.scrollTop + vpHeight) {
|
|
349
|
-
target = virtualTop + virtualHeight - vpHeight;
|
|
380
|
+
if (virtualTop - gap < this.scrollTop) {
|
|
381
|
+
target = virtualTop - gap;
|
|
382
|
+
} else if (virtualTop + virtualHeight + gap > this.scrollTop + vpHeight) {
|
|
383
|
+
target = virtualTop + virtualHeight - vpHeight + gap;
|
|
350
384
|
}
|
|
385
|
+
|
|
351
386
|
break;
|
|
352
387
|
}
|
|
388
|
+
|
|
353
389
|
default: {
|
|
354
390
|
break;
|
|
355
391
|
}
|
|
@@ -364,8 +400,10 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
364
400
|
// extend the scroll to include it
|
|
365
401
|
if (row + 1 === this._rowCount - 1) {
|
|
366
402
|
const next = this.getRowMetrics(row + 1);
|
|
403
|
+
|
|
367
404
|
if (next) {
|
|
368
405
|
const bottomEdge = next.virtualTop + next.virtualHeight;
|
|
406
|
+
|
|
369
407
|
if (bottomEdge > target + vpHeight) {
|
|
370
408
|
target = bottomEdge - vpHeight;
|
|
371
409
|
}
|
|
@@ -391,6 +429,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
391
429
|
|
|
392
430
|
// Paint decorations and gutter on top
|
|
393
431
|
this._paintDecorations(buffer);
|
|
432
|
+
|
|
394
433
|
if (this._showGutter) {
|
|
395
434
|
this._paintGutter(buffer);
|
|
396
435
|
}
|
|
@@ -404,20 +443,24 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
404
443
|
if (this._mode === "multi") {
|
|
405
444
|
return "multi";
|
|
406
445
|
}
|
|
446
|
+
|
|
407
447
|
if (this._mode === "provider") {
|
|
408
448
|
return "provider";
|
|
409
449
|
}
|
|
410
450
|
|
|
411
451
|
// Auto-detect: single child with LineInfoProvider → provider mode
|
|
412
452
|
const children = this.getChildren();
|
|
453
|
+
|
|
413
454
|
if (children.length === 1 && isRowContentProvider(children[0])) {
|
|
414
455
|
return "provider";
|
|
415
456
|
}
|
|
457
|
+
|
|
416
458
|
return "multi";
|
|
417
459
|
}
|
|
418
460
|
|
|
419
461
|
private _computeGeometry(): void {
|
|
420
462
|
const mode = this._detectMode();
|
|
463
|
+
|
|
421
464
|
if (mode === "provider") {
|
|
422
465
|
this._computeFromProvider();
|
|
423
466
|
} else {
|
|
@@ -433,6 +476,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
433
476
|
|
|
434
477
|
if (totalHeight === 0 || children.length <= offset) {
|
|
435
478
|
this._finishGeometry([], [], [], [], 0);
|
|
479
|
+
|
|
436
480
|
return;
|
|
437
481
|
}
|
|
438
482
|
|
|
@@ -453,6 +497,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
453
497
|
|
|
454
498
|
for (let r = 0; r < h; r += 1) {
|
|
455
499
|
const vRow = childStart + r;
|
|
500
|
+
|
|
456
501
|
if (vRow >= 0 && vRow < totalHeight) {
|
|
457
502
|
virtualRowToRow[vRow] = row;
|
|
458
503
|
virtualRowWraps[vRow] = r === 0 ? 0 : 1;
|
|
@@ -471,11 +516,13 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
471
516
|
|
|
472
517
|
private _computeFromProvider(): void {
|
|
473
518
|
const children = this.getChildren();
|
|
519
|
+
|
|
474
520
|
if (children.length === 0) {
|
|
475
521
|
return;
|
|
476
522
|
}
|
|
477
523
|
|
|
478
524
|
const [provider] = children;
|
|
525
|
+
|
|
479
526
|
if (!isRowContentProvider(provider)) {
|
|
480
527
|
return;
|
|
481
528
|
}
|
|
@@ -501,6 +548,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
501
548
|
if (currentRow >= 0) {
|
|
502
549
|
rowVirtualHeights[currentRow] = v - rowVirtualStarts[currentRow];
|
|
503
550
|
}
|
|
551
|
+
|
|
504
552
|
rowVirtualStarts[row] = v;
|
|
505
553
|
currentRow = row;
|
|
506
554
|
rowCount = Math.max(rowCount, row + 1);
|
|
@@ -529,16 +577,20 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
529
577
|
rowCount: number
|
|
530
578
|
): void {
|
|
531
579
|
let contentVirtualEnd = 0;
|
|
580
|
+
|
|
532
581
|
for (let row = 0; row < rowCount; row += 1) {
|
|
533
582
|
const end = rowVirtualStarts[row] + rowVirtualHeights[row];
|
|
583
|
+
|
|
534
584
|
if (end > contentVirtualEnd) {
|
|
535
585
|
contentVirtualEnd = end;
|
|
536
586
|
}
|
|
537
587
|
}
|
|
588
|
+
|
|
538
589
|
this._contentVirtualEnd = contentVirtualEnd;
|
|
539
590
|
|
|
540
591
|
// Change detection via hash
|
|
541
592
|
const hash = rowVirtualStarts.join(",");
|
|
593
|
+
|
|
542
594
|
if (hash !== this._lastGeometryHash) {
|
|
543
595
|
this._rowVirtualStarts = rowVirtualStarts;
|
|
544
596
|
this._rowVirtualHeights = rowVirtualHeights;
|
|
@@ -602,9 +654,11 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
602
654
|
if (deco.background !== undefined && deco.background !== "") {
|
|
603
655
|
rowBgs.set(deco.row, deco.background);
|
|
604
656
|
}
|
|
657
|
+
|
|
605
658
|
if (deco.gutterBackground !== undefined && deco.gutterBackground !== "") {
|
|
606
659
|
rowGutterBgs.set(deco.row, deco.gutterBackground);
|
|
607
660
|
}
|
|
661
|
+
|
|
608
662
|
if (deco.sign) {
|
|
609
663
|
rowSigns.set(deco.row, deco.sign);
|
|
610
664
|
}
|
|
@@ -613,18 +667,23 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
613
667
|
|
|
614
668
|
for (let screenY = 0; screenY < vpHeight; screenY += 1) {
|
|
615
669
|
const vRow = top + screenY;
|
|
670
|
+
|
|
616
671
|
if (vRow >= this._virtualRowToRow.length) {
|
|
617
672
|
break;
|
|
618
673
|
}
|
|
619
674
|
|
|
620
675
|
const row = this._virtualRowToRow[vRow];
|
|
676
|
+
|
|
621
677
|
if (row < 0) {
|
|
622
678
|
continue;
|
|
623
679
|
}
|
|
680
|
+
|
|
624
681
|
const bg = rowBgs.get(row);
|
|
682
|
+
|
|
625
683
|
if (bg === undefined || bg === "") {
|
|
626
684
|
continue;
|
|
627
685
|
}
|
|
686
|
+
|
|
628
687
|
buffer.fillRect(vpX, vpY + screenY, vpWidth, 1, cachedColor(bg));
|
|
629
688
|
}
|
|
630
689
|
|
|
@@ -638,6 +697,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
638
697
|
|
|
639
698
|
private _paintGutter(buffer: OptimizedBuffer): void {
|
|
640
699
|
const gutterWidth = this._computeGutterWidth();
|
|
700
|
+
|
|
641
701
|
if (gutterWidth === 0) {
|
|
642
702
|
return;
|
|
643
703
|
}
|
|
@@ -668,14 +728,17 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
668
728
|
|
|
669
729
|
for (let screenY = 0; screenY < vpHeight; screenY += 1) {
|
|
670
730
|
const vRow = top + screenY;
|
|
731
|
+
|
|
671
732
|
if (vRow >= this._virtualRowToRow.length) {
|
|
672
733
|
break;
|
|
673
734
|
}
|
|
674
735
|
|
|
675
736
|
const row = this._virtualRowToRow[vRow];
|
|
737
|
+
|
|
676
738
|
if (row < 0) {
|
|
677
739
|
continue;
|
|
678
740
|
}
|
|
741
|
+
|
|
679
742
|
const isFirstLine = this._virtualRowWraps[vRow] === 0;
|
|
680
743
|
|
|
681
744
|
if (!isFirstLine) {
|
|
@@ -686,8 +749,10 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
686
749
|
let col = 0;
|
|
687
750
|
|
|
688
751
|
const rowGutterHex = this._layerGutterBgs.get(row);
|
|
752
|
+
|
|
689
753
|
const effectiveGutterBg =
|
|
690
754
|
rowGutterHex !== undefined && rowGutterHex !== "" ? cachedColor(rowGutterHex) : gutterBg;
|
|
755
|
+
|
|
691
756
|
if (rowGutterHex !== undefined && rowGutterHex !== "") {
|
|
692
757
|
buffer.fillRect(drawX, vpY + screenY, gutterWidth, 1, effectiveGutterBg);
|
|
693
758
|
}
|
package/src/source.ts
CHANGED
|
@@ -56,16 +56,19 @@ export interface DocumentRowAnchor<T> {
|
|
|
56
56
|
// ---------------------------------------------------------------------------
|
|
57
57
|
|
|
58
58
|
const LINE_FEED_CODE = 10;
|
|
59
|
+
|
|
59
60
|
const CARRIAGE_RETURN_CODE = 13;
|
|
60
61
|
|
|
61
62
|
/** Offsets of the first character of every physical line. */
|
|
62
63
|
const buildLineStarts = function buildLineStarts(text: string): number[] {
|
|
63
64
|
const starts = [0];
|
|
65
|
+
|
|
64
66
|
for (let i = 0; i < text.length; i += 1) {
|
|
65
67
|
if (text.codePointAt(i) === LINE_FEED_CODE) {
|
|
66
68
|
starts.push(i + 1);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
71
|
+
|
|
69
72
|
return starts;
|
|
70
73
|
};
|
|
71
74
|
|
|
@@ -90,19 +93,23 @@ export class SourceIndex {
|
|
|
90
93
|
const starts = this.lineStarts;
|
|
91
94
|
let lo = 0;
|
|
92
95
|
let hi = starts.length - 1;
|
|
96
|
+
|
|
93
97
|
while (lo < hi) {
|
|
94
98
|
const mid = Math.floor((lo + hi + 1) / 2);
|
|
99
|
+
|
|
95
100
|
if (starts[mid] <= offset) {
|
|
96
101
|
lo = mid;
|
|
97
102
|
} else {
|
|
98
103
|
hi = mid - 1;
|
|
99
104
|
}
|
|
100
105
|
}
|
|
106
|
+
|
|
101
107
|
return lo;
|
|
102
108
|
}
|
|
103
109
|
|
|
104
110
|
point(offset: number): SourcePoint {
|
|
105
111
|
const line = this.lineAt(offset);
|
|
112
|
+
|
|
106
113
|
return { column: offset - this.lineStarts[line], line, offset };
|
|
107
114
|
}
|
|
108
115
|
|
|
@@ -111,12 +118,15 @@ export class SourceIndex {
|
|
|
111
118
|
const starts = this.lineStarts;
|
|
112
119
|
const nextStart = line + 1 < starts.length ? starts[line + 1] : this.text.length;
|
|
113
120
|
let end = nextStart;
|
|
121
|
+
|
|
114
122
|
if (end > starts[line] && this.text.codePointAt(end - 1) === LINE_FEED_CODE) {
|
|
115
123
|
end -= 1;
|
|
124
|
+
|
|
116
125
|
if (end > starts[line] && this.text.codePointAt(end - 1) === CARRIAGE_RETURN_CODE) {
|
|
117
126
|
end -= 1;
|
|
118
127
|
}
|
|
119
128
|
}
|
|
129
|
+
|
|
120
130
|
return end;
|
|
121
131
|
}
|
|
122
132
|
|
|
@@ -128,14 +138,17 @@ export class SourceIndex {
|
|
|
128
138
|
*/
|
|
129
139
|
span(rawStart: number, rawEnd: number, trimTrailingNewlines = true): SourceSpan {
|
|
130
140
|
let end = rawEnd;
|
|
141
|
+
|
|
131
142
|
if (trimTrailingNewlines) {
|
|
132
143
|
while (end > rawStart && this.text.codePointAt(end - 1) === 10) {
|
|
133
144
|
end -= 1;
|
|
145
|
+
|
|
134
146
|
if (end > rawStart && this.text.codePointAt(end - 1) === 13) {
|
|
135
147
|
end -= 1;
|
|
136
148
|
}
|
|
137
149
|
}
|
|
138
150
|
}
|
|
151
|
+
|
|
139
152
|
if (end < rawStart) {
|
|
140
153
|
end = rawStart;
|
|
141
154
|
}
|
|
@@ -143,6 +156,7 @@ export class SourceIndex {
|
|
|
143
156
|
const start = this.point(rawStart);
|
|
144
157
|
const endPoint = this.point(end);
|
|
145
158
|
const lastLine = end > rawStart ? this.lineAt(end - 1) : start.line;
|
|
159
|
+
|
|
146
160
|
const span: SourceSpan = {
|
|
147
161
|
end: endPoint,
|
|
148
162
|
lastLine,
|
|
@@ -150,9 +164,11 @@ export class SourceIndex {
|
|
|
150
164
|
start,
|
|
151
165
|
text: this.text.slice(rawStart, end),
|
|
152
166
|
};
|
|
167
|
+
|
|
153
168
|
if (this.sourceId !== undefined) {
|
|
154
169
|
span.sourceId = this.sourceId;
|
|
155
170
|
}
|
|
171
|
+
|
|
156
172
|
return span;
|
|
157
173
|
}
|
|
158
174
|
}
|
|
@@ -179,6 +195,7 @@ export const sourceLines = function sourceLines(
|
|
|
179
195
|
): SourceLineRow[] {
|
|
180
196
|
const index = new SourceIndex(source, options?.sourceId);
|
|
181
197
|
const rows: SourceLineRow[] = [];
|
|
198
|
+
|
|
182
199
|
for (let line = 0; line < index.lineStarts.length; line += 1) {
|
|
183
200
|
const start = index.lineStarts[line];
|
|
184
201
|
const end = index.lineContentEnd(line);
|
|
@@ -187,6 +204,7 @@ export const sourceLines = function sourceLines(
|
|
|
187
204
|
text: source.slice(start, end),
|
|
188
205
|
});
|
|
189
206
|
}
|
|
207
|
+
|
|
190
208
|
return rows;
|
|
191
209
|
};
|
|
192
210
|
|
package/src/table.tsx
CHANGED
|
@@ -38,9 +38,13 @@ export interface TableProps {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
const PADDING = 1;
|
|
41
|
+
|
|
41
42
|
const TABLE_MARGIN = 1;
|
|
43
|
+
|
|
42
44
|
const DEFAULT_MIN_COL_WIDTH = 4;
|
|
45
|
+
|
|
43
46
|
const DEFAULT_MAX_COL_WIDTH = 80;
|
|
47
|
+
|
|
44
48
|
const DEFAULT_SAMPLE_SIZE = 100;
|
|
45
49
|
|
|
46
50
|
const isNumeric = function isNumeric(value: string): boolean {
|
|
@@ -58,12 +62,15 @@ const sampleRows = function sampleRows(rows: string[][], sampleSize: number): st
|
|
|
58
62
|
if (rows.length <= sampleSize) {
|
|
59
63
|
return rows;
|
|
60
64
|
}
|
|
65
|
+
|
|
61
66
|
// Sample evenly distributed rows for representative widths
|
|
62
67
|
const step = rows.length / sampleSize;
|
|
63
68
|
const sampled: string[][] = [];
|
|
69
|
+
|
|
64
70
|
for (let i = 0; i < sampleSize; i += 1) {
|
|
65
71
|
sampled.push(rows[Math.floor(i * step)]);
|
|
66
72
|
}
|
|
73
|
+
|
|
67
74
|
return sampled;
|
|
68
75
|
};
|
|
69
76
|
|
|
@@ -85,15 +92,18 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
85
92
|
const headerLen = Bun.stringWidth(header);
|
|
86
93
|
let maxRowLen = 0;
|
|
87
94
|
const sampledRowCount = sampledRows.length;
|
|
95
|
+
|
|
88
96
|
for (let rowIndex = 0; rowIndex < sampledRowCount; rowIndex += 1) {
|
|
89
97
|
if (rowIndex in sampledRows) {
|
|
90
98
|
const row = sampledRows[rowIndex];
|
|
91
99
|
maxRowLen = Math.max(maxRowLen, Bun.stringWidth(row[col] ?? ""));
|
|
92
100
|
}
|
|
93
101
|
}
|
|
102
|
+
|
|
94
103
|
const contentWidth = Math.max(headerLen, maxRowLen);
|
|
95
104
|
// Apply min/max constraints before adding padding
|
|
96
105
|
const constrainedWidth = Math.min(maxColumnWidth, Math.max(minColumnWidth, contentWidth));
|
|
106
|
+
|
|
97
107
|
return constrainedWidth + PADDING * 2;
|
|
98
108
|
});
|
|
99
109
|
|
|
@@ -106,8 +116,10 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
106
116
|
const extra = maxWidth - totalNatural;
|
|
107
117
|
const perCol = Math.floor(extra / colCount);
|
|
108
118
|
const remainder = extra - perCol * colCount;
|
|
119
|
+
|
|
109
120
|
return naturalWidths.map((w, i) => w + perCol + (i < remainder ? 1 : 0));
|
|
110
121
|
}
|
|
122
|
+
|
|
111
123
|
return naturalWidths;
|
|
112
124
|
}
|
|
113
125
|
|
|
@@ -130,6 +142,7 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
130
142
|
if (longTotal === 0 || remaining <= 0) {
|
|
131
143
|
// All compact or no space left -- distribute evenly
|
|
132
144
|
const total = naturalWidths.reduce((a, b) => a + b, 0);
|
|
145
|
+
|
|
133
146
|
return naturalWidths.map((w) =>
|
|
134
147
|
Math.max(minColWidthWithPadding, Math.floor((w / total) * available))
|
|
135
148
|
);
|
|
@@ -139,6 +152,7 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
139
152
|
if (compact[i]) {
|
|
140
153
|
return w;
|
|
141
154
|
}
|
|
155
|
+
|
|
142
156
|
return Math.max(minColWidthWithPadding, Math.floor((w / longTotal) * remaining));
|
|
143
157
|
});
|
|
144
158
|
};
|
|
@@ -148,36 +162,46 @@ export const formatTableCell = function formatTableCell(value: TableRow[string])
|
|
|
148
162
|
if (value === null || value === undefined) {
|
|
149
163
|
return "";
|
|
150
164
|
}
|
|
165
|
+
|
|
151
166
|
if (value instanceof Date) {
|
|
152
167
|
return value.toISOString();
|
|
153
168
|
}
|
|
169
|
+
|
|
154
170
|
const valueTag = Object.prototype.toString.call(value);
|
|
171
|
+
|
|
155
172
|
switch (valueTag) {
|
|
156
173
|
case "[object String]": {
|
|
157
174
|
return String.prototype.toString.call(value);
|
|
158
175
|
}
|
|
176
|
+
|
|
159
177
|
case "[object Number]": {
|
|
160
178
|
return Number.prototype.toString.call(value);
|
|
161
179
|
}
|
|
180
|
+
|
|
162
181
|
case "[object BigInt]": {
|
|
163
182
|
return BigInt.prototype.toString.call(value);
|
|
164
183
|
}
|
|
184
|
+
|
|
165
185
|
case "[object Boolean]": {
|
|
166
186
|
return Boolean.prototype.toString.call(value);
|
|
167
187
|
}
|
|
188
|
+
|
|
168
189
|
case "[object Symbol]": {
|
|
169
190
|
return Symbol.prototype.toString.call(value);
|
|
170
191
|
}
|
|
192
|
+
|
|
171
193
|
case "[object Function]":
|
|
172
194
|
case "[object AsyncFunction]":
|
|
173
195
|
case "[object GeneratorFunction]":
|
|
174
196
|
case "[object AsyncGeneratorFunction]": {
|
|
175
197
|
return Function.prototype.toString.call(value);
|
|
176
198
|
}
|
|
199
|
+
|
|
177
200
|
default: {
|
|
178
201
|
break;
|
|
179
202
|
}
|
|
180
203
|
}
|
|
204
|
+
|
|
181
205
|
try {
|
|
182
206
|
return JSON.stringify(value);
|
|
183
207
|
} catch {
|
|
@@ -212,12 +236,14 @@ export const Table = function Table({
|
|
|
212
236
|
}),
|
|
213
237
|
[showLineNumbers, rows.length]
|
|
214
238
|
);
|
|
239
|
+
|
|
215
240
|
const effectiveMaxWidth = Math.max(
|
|
216
241
|
0,
|
|
217
242
|
(maxWidth ?? terminalWidth) - TABLE_MARGIN * 2 - gutterWidth
|
|
218
243
|
);
|
|
219
244
|
|
|
220
245
|
const headers = useMemo(() => columns.map((column) => column.header ?? column.key), [columns]);
|
|
246
|
+
|
|
221
247
|
const normalizedRows = useMemo(
|
|
222
248
|
() => rows.map((row) => columns.map((column) => formatTableCell(row[column.key]))),
|
|
223
249
|
[columns, rows]
|
|
@@ -249,11 +275,14 @@ export const Table = function Table({
|
|
|
249
275
|
if (column.align === "right") {
|
|
250
276
|
return true;
|
|
251
277
|
}
|
|
278
|
+
|
|
252
279
|
if (column.align === "left") {
|
|
253
280
|
return false;
|
|
254
281
|
}
|
|
282
|
+
|
|
255
283
|
const sampleValues = normalizedRows.slice(0, 10).map((row) => row[colIdx] ?? "");
|
|
256
284
|
const numericCount = sampleValues.filter(isNumeric).length;
|
|
285
|
+
|
|
257
286
|
return numericCount > sampleValues.length / 2;
|
|
258
287
|
}),
|
|
259
288
|
[columns, normalizedRows]
|
|
@@ -266,10 +295,12 @@ export const Table = function Table({
|
|
|
266
295
|
{row.map((cell, j): ReactNode => {
|
|
267
296
|
const contentWidth = colWidths[j] - PADDING * 2;
|
|
268
297
|
const cellWidth = Bun.stringWidth(cell);
|
|
298
|
+
|
|
269
299
|
const displayCell =
|
|
270
300
|
alignments[j] && cellWidth <= contentWidth
|
|
271
301
|
? " ".repeat(contentWidth - cellWidth) + cell
|
|
272
302
|
: cell;
|
|
303
|
+
|
|
273
304
|
return (
|
|
274
305
|
<text
|
|
275
306
|
key={j}
|
|
@@ -344,4 +375,5 @@ export const Table = function Table({
|
|
|
344
375
|
|
|
345
376
|
// Exported for testing and MarkdownView
|
|
346
377
|
export { computeColumnWidths, isNumeric, sampleRows };
|
|
378
|
+
|
|
347
379
|
export type { ColumnWidthOptions };
|
|
@@ -33,7 +33,9 @@ export const hasTextTableInternals = function hasTextTableInternals(
|
|
|
33
33
|
) {
|
|
34
34
|
return false;
|
|
35
35
|
}
|
|
36
|
+
|
|
36
37
|
const { _layout: layout } = table;
|
|
38
|
+
|
|
37
39
|
return (
|
|
38
40
|
Array.isArray(table._cells) &&
|
|
39
41
|
Number.isInteger(table.cellPaddingX) &&
|
|
@@ -68,10 +70,12 @@ const spanAt = function spanAt(
|
|
|
68
70
|
): Span | null {
|
|
69
71
|
for (const [index, size] of sizes.entries()) {
|
|
70
72
|
const inside = local - (offsets[index] ?? 0) - 1;
|
|
73
|
+
|
|
71
74
|
if (inside >= 0 && inside < size) {
|
|
72
75
|
return { index, inside };
|
|
73
76
|
}
|
|
74
77
|
}
|
|
78
|
+
|
|
75
79
|
return null;
|
|
76
80
|
};
|
|
77
81
|
|
|
@@ -98,27 +102,35 @@ export const textTableCellAt = function textTableCellAt(
|
|
|
98
102
|
if (!hasTextTableInternals(table)) {
|
|
99
103
|
return null;
|
|
100
104
|
}
|
|
105
|
+
|
|
101
106
|
const { columnOffsets, columnWidths, rowHeights, rowOffsets } = table._layout;
|
|
102
107
|
const row = spanAt(rowOffsets, rowHeights, y - table.y);
|
|
103
108
|
const column = spanAt(columnOffsets, columnWidths, x - table.x);
|
|
109
|
+
|
|
104
110
|
if (row === null || column === null) {
|
|
105
111
|
return null;
|
|
106
112
|
}
|
|
113
|
+
|
|
107
114
|
const view = table._cells[row.index]?.[column.index]?.textBufferView;
|
|
115
|
+
|
|
108
116
|
if (!(view instanceof TextBufferView)) {
|
|
109
117
|
return null;
|
|
110
118
|
}
|
|
119
|
+
|
|
111
120
|
const cellX = column.inside - table.cellPaddingX;
|
|
112
121
|
const visualLine = row.inside - table.cellPaddingY;
|
|
113
122
|
const { lineSources, lineStartCols, lineWidthCols } = view.lineInfo;
|
|
114
123
|
const line = lineSources[visualLine];
|
|
115
124
|
const start = lineStartCols[visualLine];
|
|
116
125
|
const width = lineWidthCols[visualLine];
|
|
126
|
+
|
|
117
127
|
if (line === undefined || start === undefined || width === undefined) {
|
|
118
128
|
return null;
|
|
119
129
|
}
|
|
130
|
+
|
|
120
131
|
if (cellX < 0 || cellX >= width) {
|
|
121
132
|
return null;
|
|
122
133
|
}
|
|
134
|
+
|
|
123
135
|
return { column: column.index, line, offset: start + cellX, row: row.index };
|
|
124
136
|
};
|