@malloy-publisher/sdk 0.4.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/README.md +19 -0
- package/dist/{DocumentStorageProvider-CyOJ0hqf.es.js → DocumentStorage-B04sZ5Ie.es.js} +2307 -2334
- package/dist/{DocumentStorageProvider-DGPQEd_l.cjs.js → DocumentStorage-B_MMJ0Ot.cjs.js} +20 -20
- package/dist/{RenderedResult-BnPzJ9Jf.es.js → RenderedResult-BB7eokcf.es.js} +100 -80
- package/dist/RenderedResult-DVuGhatg.cjs.js +135 -0
- package/dist/ServerProvider-D-jjzfE5.cjs.js +1 -0
- package/dist/{ServerProvider-BWOEKg0O.es.js → ServerProvider-Do-GBvpn.es.js} +492 -481
- package/dist/builder/index.cjs.js +57 -52
- package/dist/builder/index.es.js +4421 -3738
- package/dist/client/api.d.ts +14 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/TileCard.d.ts +7 -3
- package/dist/components/Dashboard/telemetry.d.ts +20 -8
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +16 -4
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +31 -8
- package/dist/components/DashboardBuilder/controls.d.ts +8 -6
- package/dist/components/DashboardBuilder/document.d.ts +19 -5
- package/dist/components/DashboardBuilder/malloyText.d.ts +0 -30
- package/dist/components/DashboardBuilder/malloyTree.d.ts +232 -0
- package/dist/components/DashboardBuilder/preview.d.ts +17 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +23 -28
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +12 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +69 -5
- package/dist/components/DocumentStorage/index.d.ts +1 -0
- package/dist/index.cjs.js +9 -9
- package/dist/index.es.js +508 -506
- package/dist/theme/resolveTheme.d.ts +1 -1
- package/dist/theme/types.d.ts +12 -2
- package/package.json +1 -1
- package/src/components/Dashboard/TileCard.tsx +8 -4
- package/src/components/Dashboard/telemetry.ts +20 -8
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +4 -5
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +21 -3
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +706 -0
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +356 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +468 -103
- package/src/components/DashboardBuilder/FilterDialog.tsx +19 -6
- package/src/components/DashboardBuilder/TileFrame.tsx +11 -2
- package/src/components/DashboardBuilder/TileMenu.tsx +6 -0
- package/src/components/DashboardBuilder/__test__/inventory.ts +367 -0
- package/src/components/DashboardBuilder/controls.spec.ts +53 -3
- package/src/components/DashboardBuilder/controls.ts +9 -7
- package/src/components/DashboardBuilder/document.ts +17 -5
- package/src/components/DashboardBuilder/invariants.spec.ts +297 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +27 -66
- package/src/components/DashboardBuilder/malloyText.ts +4 -109
- package/src/components/DashboardBuilder/malloyTree.spec.ts +362 -0
- package/src/components/DashboardBuilder/malloyTree.ts +954 -0
- package/src/components/DashboardBuilder/preview.spec.ts +18 -0
- package/src/components/DashboardBuilder/preview.ts +24 -6
- package/src/components/DashboardBuilder/readDocument.spec.ts +312 -21
- package/src/components/DashboardBuilder/readDocument.ts +144 -165
- package/src/components/DashboardBuilder/scannerShapes.spec.ts +550 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +955 -21
- package/src/components/DashboardBuilder/spliceDocument.ts +509 -119
- package/src/components/DashboardBuilder/statementExtent.spec.ts +606 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +7 -6
- package/src/components/DocumentStorage/DocumentStorage.ts +78 -5
- package/src/components/DocumentStorage/index.ts +1 -0
- package/src/components/Package/Package.tsx +12 -7
- package/src/components/RenderedResult/RenderedResult.tsx +21 -1
- package/src/components/given/GivensPanel.tsx +15 -3
- package/src/theme/buildTableCssVars.spec.ts +3 -0
- package/src/theme/buildTableCssVars.ts +6 -0
- package/src/theme/defaults.ts +16 -12
- package/src/theme/resolveTheme.spec.ts +14 -7
- package/src/theme/resolveTheme.ts +14 -8
- package/src/theme/types.ts +12 -2
- package/tsconfig.spec.json +5 -1
- package/dist/RenderedResult-Dp22kZsQ.cjs.js +0 -115
- package/dist/ServerProvider-Cc489A3f.cjs.js +0 -1
|
@@ -8,18 +8,37 @@ import type {
|
|
|
8
8
|
DashboardTile,
|
|
9
9
|
} from "./document";
|
|
10
10
|
import type { LocalGiven } from "./document";
|
|
11
|
+
import { artifactLine } from "./malloyText";
|
|
11
12
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
parseMalloy,
|
|
14
|
+
parseRefused,
|
|
15
|
+
translate,
|
|
16
|
+
type ParsedMalloy,
|
|
17
|
+
type Span,
|
|
18
|
+
type TreeGiven,
|
|
19
|
+
type TreeStage,
|
|
20
|
+
type TreeView,
|
|
21
|
+
} from "./malloyTree";
|
|
22
|
+
import { blockAbove, readDashboardDocument, readFailed } from "./readDocument";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The syntax errors Malloy's own parser reports for `text`, as a multiset of
|
|
26
|
+
* messages.
|
|
27
|
+
*
|
|
28
|
+
* The first `translate()` is enough and is all we want: a document with
|
|
29
|
+
* imports comes back NOT final, asking for the urls it needs, and by then the
|
|
30
|
+
* parse has already happened while nothing has been resolved. So this sees
|
|
31
|
+
* every syntax error and none of the semantic ones, which are not this
|
|
32
|
+
* writer's business -- a dashboard referring to a source in a file we decline
|
|
33
|
+
* to hand over is not damage we caused.
|
|
34
|
+
*/
|
|
35
|
+
export async function syntaxErrors(text: string): Promise<string[]> {
|
|
36
|
+
const { problems } = await translate(text, "file://splice-check.malloy");
|
|
37
|
+
return problems
|
|
38
|
+
.filter((p) => p.code === "syntax-error")
|
|
39
|
+
.map((p) => p.message ?? "")
|
|
40
|
+
.sort();
|
|
41
|
+
}
|
|
23
42
|
|
|
24
43
|
/**
|
|
25
44
|
* Write a change back into a `dashboards/*.malloy` file by PATCHING it.
|
|
@@ -106,7 +125,14 @@ function lineStarts(source: string): number[] {
|
|
|
106
125
|
|
|
107
126
|
function applyEdits(source: string, edits: Edit[]): string {
|
|
108
127
|
let out = source;
|
|
109
|
-
|
|
128
|
+
// Back to front, so an earlier edit's offsets still address the text it was
|
|
129
|
+
// planned against. Where two share a start — an inserted tag line and a
|
|
130
|
+
// rewrite of the declaration it sits above — the replacement has to go
|
|
131
|
+
// first, or the insertion shifts the text out from under its end offset and
|
|
132
|
+
// the rewrite lands in the middle of what was just inserted.
|
|
133
|
+
for (const edit of [...edits].sort(
|
|
134
|
+
(a, b) => b.start - a.start || b.end - a.end,
|
|
135
|
+
))
|
|
110
136
|
out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
111
137
|
return out;
|
|
112
138
|
}
|
|
@@ -200,33 +226,6 @@ function givenTagLine(given: LocalGiven): string | undefined {
|
|
|
200
226
|
const givenDeclaration = (given: LocalGiven) =>
|
|
201
227
|
`given: ${given.name} :: ${given.type} is ${given.default}`;
|
|
202
228
|
|
|
203
|
-
/**
|
|
204
|
-
* The last line of the declaration starting at `line`: the line itself for a
|
|
205
|
-
* one-line `view: x is y + { … }`, or the matching closing brace for a body
|
|
206
|
-
* (`view: x is {`, `source: s is b extend {`). Braces inside strings are not
|
|
207
|
-
* a concern Malloy dashboards have raised, so the count is plain.
|
|
208
|
-
*/
|
|
209
|
-
function declarationEnd(lines: string[], line: number): number {
|
|
210
|
-
let depth = 0;
|
|
211
|
-
let opened = false;
|
|
212
|
-
for (let i = line; i < lines.length; i++) {
|
|
213
|
-
for (const ch of lines[i]) {
|
|
214
|
-
if (ch === "{") {
|
|
215
|
-
depth++;
|
|
216
|
-
opened = true;
|
|
217
|
-
} else if (ch === "}") depth--;
|
|
218
|
-
}
|
|
219
|
-
if (opened && depth <= 0) return i;
|
|
220
|
-
if (!opened && i === line) return i;
|
|
221
|
-
}
|
|
222
|
-
return lines.length - 1;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/** A name as a regex literal: view and source names are identifiers, but the
|
|
226
|
-
* pattern is built from document data and should not be able to mean anything
|
|
227
|
-
* else. */
|
|
228
|
-
const literal = (name: string) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
229
|
-
|
|
230
229
|
/**
|
|
231
230
|
* The line where `<source>` declares `view: <view>`, or -1.
|
|
232
231
|
*
|
|
@@ -241,18 +240,14 @@ const literal = (name: string) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
|
241
240
|
* this far.
|
|
242
241
|
*/
|
|
243
242
|
function viewDeclarationLine(
|
|
244
|
-
|
|
243
|
+
parsed: ParsedMalloy,
|
|
245
244
|
sourceName: string,
|
|
246
245
|
viewName: string,
|
|
247
246
|
): number {
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const end = declarationEnd(lines, sourceLine);
|
|
253
|
-
const wanted = new RegExp(`\\bview:\\s*${literal(viewName)}\\s+is\\b`);
|
|
254
|
-
for (let i = sourceLine; i <= end; i++) if (wanted.test(lines[i])) return i;
|
|
255
|
-
return -1;
|
|
247
|
+
const view = parsed.sources
|
|
248
|
+
.find((source) => source.name === sourceName)
|
|
249
|
+
?.views.find((v) => v.name === viewName);
|
|
250
|
+
return view ? view.line : -1;
|
|
256
251
|
}
|
|
257
252
|
|
|
258
253
|
/** `# drill { to=… given=… }`, one line, as the reader spells it back. */
|
|
@@ -299,6 +294,8 @@ interface TileMembership {
|
|
|
299
294
|
/** Everything a planner reads, and the edits it adds to. */
|
|
300
295
|
interface SpliceContext extends TileMembership {
|
|
301
296
|
sourceText: string;
|
|
297
|
+
/** Where everything is, from Malloy's own parser. */
|
|
298
|
+
parsed: ParsedMalloy;
|
|
302
299
|
lines: string[];
|
|
303
300
|
starts: number[];
|
|
304
301
|
wholeLine: (line: number) => { start: number; end: number };
|
|
@@ -560,7 +557,7 @@ function planSettings(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
560
557
|
}
|
|
561
558
|
|
|
562
559
|
function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
563
|
-
const { lines, wholeLine, indentOf, current, next, edits } = ctx;
|
|
560
|
+
const { lines, wholeLine, indentOf, current, next, edits, parsed } = ctx;
|
|
564
561
|
// THE DASHBOARD'S OWN GIVENS. Added, removed, or retagged — by name, since
|
|
565
562
|
// a given's name is its identity in every `where:` that reads it.
|
|
566
563
|
const givensBefore = new Map(
|
|
@@ -569,7 +566,9 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
569
566
|
const givensAfter = new Map(
|
|
570
567
|
(next.localGivens ?? []).map((g) => [g.name, g]),
|
|
571
568
|
);
|
|
572
|
-
const declared =
|
|
569
|
+
const declared = new Map<string, TreeGiven>(
|
|
570
|
+
parsed.givens.map((g) => [g.name, g]),
|
|
571
|
+
);
|
|
573
572
|
const removedLines = new Set<number>();
|
|
574
573
|
|
|
575
574
|
for (const [name, was] of givensBefore) {
|
|
@@ -586,7 +585,7 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
586
585
|
// removed declaration is the exception and takes every tag with it: a
|
|
587
586
|
// `#` line left behind does not lapse, it attaches to whatever is
|
|
588
587
|
// declared next, so an orphaned `#(secure)` would silently move.
|
|
589
|
-
const { tags } = blockAbove(lines, at.line);
|
|
588
|
+
const { tags } = blockAbove(parsed, lines, at.line);
|
|
590
589
|
const owned =
|
|
591
590
|
want === undefined
|
|
592
591
|
? tags
|
|
@@ -598,14 +597,23 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
598
597
|
edits.push({ ...wholeLine(tag.line), text: "" });
|
|
599
598
|
removedLines.add(tag.line);
|
|
600
599
|
}
|
|
600
|
+
// A declaration can run past its first line -- `NAME :: string is` with
|
|
601
|
+
// its default below it is ordinary Malloy -- so the lines to take come
|
|
602
|
+
// from the span, not from the line the name sits on. Taking only the
|
|
603
|
+
// first left the continuation behind as a statement of its own.
|
|
604
|
+
const lastLine = lineOf(parsed, at.span.end - 1);
|
|
601
605
|
if (want === undefined) {
|
|
602
|
-
edits.push({
|
|
603
|
-
|
|
606
|
+
edits.push({
|
|
607
|
+
start: wholeLine(at.line).start,
|
|
608
|
+
end: wholeLine(lastLine).end,
|
|
609
|
+
text: "",
|
|
610
|
+
});
|
|
611
|
+
for (let l = at.line; l <= lastLine; l++) removedLines.add(l);
|
|
604
612
|
// A declaration set off by blank lines takes one of them with it, or
|
|
605
613
|
// the two separators meet and the file gains an empty line per edit.
|
|
606
614
|
const first = Math.min(at.line, ...tags.map((tag) => tag.line));
|
|
607
615
|
const above = at.blockHeader ?? first;
|
|
608
|
-
const belowIsBlank = (lines[
|
|
616
|
+
const belowIsBlank = (lines[lastLine + 1] ?? "x").trim() === "";
|
|
609
617
|
const aboveIsBlank = above === 0 || lines[above - 1].trim() === "";
|
|
610
618
|
const lastInBlock =
|
|
611
619
|
at.blockHeader === undefined ||
|
|
@@ -623,10 +631,10 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
623
631
|
belowIsBlank &&
|
|
624
632
|
aboveIsBlank &&
|
|
625
633
|
lastInBlock &&
|
|
626
|
-
!removedLines.has(
|
|
634
|
+
!removedLines.has(lastLine + 1)
|
|
627
635
|
) {
|
|
628
|
-
edits.push({ ...wholeLine(
|
|
629
|
-
removedLines.add(
|
|
636
|
+
edits.push({ ...wholeLine(lastLine + 1), text: "" });
|
|
637
|
+
removedLines.add(lastLine + 1);
|
|
630
638
|
}
|
|
631
639
|
continue;
|
|
632
640
|
}
|
|
@@ -641,9 +649,13 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
641
649
|
? at.blockHeader === undefined
|
|
642
650
|
? givenDeclaration(want)
|
|
643
651
|
: `${want.name} :: ${want.type} is ${want.default}`
|
|
644
|
-
: lines
|
|
652
|
+
: lines
|
|
653
|
+
.slice(at.line, lastLine + 1)
|
|
654
|
+
.join("\n")
|
|
655
|
+
.trim();
|
|
645
656
|
edits.push({
|
|
646
|
-
|
|
657
|
+
start: wholeLine(at.line).start,
|
|
658
|
+
end: wholeLine(lastLine).end,
|
|
647
659
|
text:
|
|
648
660
|
(tagLine ? `${indent}${tagLine}\n` : "") +
|
|
649
661
|
`${indent}${declaration}\n`,
|
|
@@ -710,7 +722,7 @@ function planGivens(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
710
722
|
}
|
|
711
723
|
|
|
712
724
|
function planDrills(ctx: SpliceContext): SpliceFailure | undefined {
|
|
713
|
-
const { lines, wholeLine, indentOf, current, next, edits } = ctx;
|
|
725
|
+
const { lines, wholeLine, indentOf, current, next, edits, parsed } = ctx;
|
|
714
726
|
// DRILLS. A drill is one `# drill` tag on a dimension THIS FILE declares —
|
|
715
727
|
// added above the declaration, rewritten, or taken off. The dimension itself
|
|
716
728
|
// is never written: a dimension no view reads is a dead drill, and the
|
|
@@ -733,9 +745,9 @@ function planDrills(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
733
745
|
reason: `The drill on \`${key}\` names no destination.`,
|
|
734
746
|
};
|
|
735
747
|
}
|
|
736
|
-
const at =
|
|
737
|
-
drill.
|
|
738
|
-
|
|
748
|
+
const at = parsed.sources
|
|
749
|
+
.find((source) => source.name === drill.source)
|
|
750
|
+
?.dimensions.find((d) => d.name === drill.name);
|
|
739
751
|
if (at === undefined) {
|
|
740
752
|
return {
|
|
741
753
|
ok: false,
|
|
@@ -744,7 +756,7 @@ function planDrills(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
744
756
|
`in this file, so a drill cannot be put on it here.`,
|
|
745
757
|
};
|
|
746
758
|
}
|
|
747
|
-
const { tags } = blockAbove(lines, at.line);
|
|
759
|
+
const { tags } = blockAbove(parsed, lines, at.line);
|
|
748
760
|
const existing = tags.find((tag) => /^#\s*drill\b/.test(tag.text));
|
|
749
761
|
const indent = indentOf(at.line);
|
|
750
762
|
if (want === undefined) {
|
|
@@ -768,25 +780,52 @@ function planDrills(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
768
780
|
}
|
|
769
781
|
|
|
770
782
|
function planRemovedTiles(ctx: SpliceContext): SpliceFailure | undefined {
|
|
771
|
-
const { lines, wholeLine, removedTiles, edits } = ctx;
|
|
783
|
+
const { lines, wholeLine, removedTiles, edits, parsed } = ctx;
|
|
772
784
|
// REMOVED TILES: the declaration and its `#` tags. An inline view's body
|
|
773
785
|
// runs to its closing brace; a reference is one line. An inherited tile has
|
|
774
786
|
// nothing here to remove — its entry left the artifact list above.
|
|
775
787
|
for (const tile of removedTiles) {
|
|
776
788
|
if (tile.declaration.kind === "inherited") continue;
|
|
777
|
-
const
|
|
778
|
-
if (
|
|
789
|
+
const view = viewOf(ctx, tile);
|
|
790
|
+
if (!view) {
|
|
779
791
|
return {
|
|
780
792
|
ok: false,
|
|
781
793
|
reason: `Could not find where \`${tile.name}\` is declared.`,
|
|
782
794
|
};
|
|
783
795
|
}
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
796
|
+
if (view.siblings > 1) {
|
|
797
|
+
return {
|
|
798
|
+
ok: false,
|
|
799
|
+
reason:
|
|
800
|
+
`\`${tile.name}\` is one of several views declared by a single ` +
|
|
801
|
+
`\`view:\` statement, which the builder does not restructure.`,
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
// The declaration and its `#` tags, which is what the statement's own
|
|
805
|
+
// span covers. A `//` comment above is NOT taken: it is left where the
|
|
806
|
+
// author put it rather than assumed to belong to the tile.
|
|
807
|
+
const first = lineOf(parsed, view.statement.start);
|
|
808
|
+
const endLine = lineOf(parsed, view.statement.end - 1);
|
|
809
|
+
// Taking whole lines is right only when this statement is the only thing
|
|
810
|
+
// on them. Malloy lets a second declaration share a line, and one that is
|
|
811
|
+
// not a tile of this dashboard is invisible to the readback gate -- so
|
|
812
|
+
// widening to the line would delete it and report success.
|
|
813
|
+
const trailing = parsed.trailingComment(endLine);
|
|
814
|
+
const tail =
|
|
815
|
+
trailing && trailing.start >= view.statement.end
|
|
816
|
+
? trailing.end
|
|
817
|
+
: view.statement.end;
|
|
818
|
+
const sharesLine =
|
|
819
|
+
parsed.text
|
|
820
|
+
.slice(parsed.lineStarts[first], view.statement.start)
|
|
821
|
+
.trim() !== "" ||
|
|
822
|
+
parsed.text.slice(tail, wholeLine(endLine).end).trim() !== "";
|
|
823
|
+
if (sharesLine) {
|
|
824
|
+
edits.push({ ...statementCut(parsed, view.statement), text: "" });
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
788
827
|
edits.push({
|
|
789
|
-
start:
|
|
828
|
+
start: parsed.lineStarts[first],
|
|
790
829
|
end: wholeLine(endLine).end,
|
|
791
830
|
text: "",
|
|
792
831
|
});
|
|
@@ -810,11 +849,11 @@ function planAddedTiles(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
810
849
|
sourceText,
|
|
811
850
|
lines,
|
|
812
851
|
wholeLine,
|
|
813
|
-
current,
|
|
814
852
|
addedTiles,
|
|
815
853
|
newSources,
|
|
816
854
|
currentSources,
|
|
817
855
|
edits,
|
|
856
|
+
parsed,
|
|
818
857
|
} = ctx;
|
|
819
858
|
// ADDED TILES: a `view:` with its tags, inside the extension of the source
|
|
820
859
|
// the tile reads — before that extension's closing brace — or in a new
|
|
@@ -838,25 +877,33 @@ function planAddedTiles(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
838
877
|
`${indent}view: ${tile.name} is ${from}${bindings ? ` + { ${bindings} }` : ""}`,
|
|
839
878
|
].join("\n");
|
|
840
879
|
};
|
|
880
|
+
// An anchor for a NEW extension: the last line any source's declaration
|
|
881
|
+
// reaches, so one added after it cannot land inside another.
|
|
841
882
|
let lastExtensionEnd = -1;
|
|
842
|
-
for (const source of
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
declarationEnd(lines, open),
|
|
848
|
-
);
|
|
849
|
-
}
|
|
883
|
+
for (const source of parsed.sources)
|
|
884
|
+
lastExtensionEnd = Math.max(
|
|
885
|
+
lastExtensionEnd,
|
|
886
|
+
lineOf(parsed, source.span.end - 1),
|
|
887
|
+
);
|
|
850
888
|
for (const [sourceName, tiles] of byExtension) {
|
|
851
889
|
if (currentSources.has(sourceName)) {
|
|
852
|
-
const
|
|
853
|
-
if (
|
|
890
|
+
const source = parsed.sources.find((s) => s.name === sourceName);
|
|
891
|
+
if (!source) {
|
|
854
892
|
return {
|
|
855
893
|
ok: false,
|
|
856
894
|
reason: `Could not find where the source \`${sourceName}\` is declared.`,
|
|
857
895
|
};
|
|
858
896
|
}
|
|
859
|
-
|
|
897
|
+
if (!source.properties) {
|
|
898
|
+
return {
|
|
899
|
+
ok: false,
|
|
900
|
+
reason:
|
|
901
|
+
`\`${sourceName}\` never opens an \`extend { … }\` block, so a ` +
|
|
902
|
+
`new tile cannot be added inside it.`,
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
const open = source.line;
|
|
906
|
+
const close = lineOf(parsed, source.properties.closeStart);
|
|
860
907
|
// The indent the extension already uses for its views, else two spaces.
|
|
861
908
|
let indent = " ";
|
|
862
909
|
for (let i = open + 1; i < close; i++) {
|
|
@@ -914,7 +961,8 @@ function planAddedTiles(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
914
961
|
}
|
|
915
962
|
|
|
916
963
|
function planTilePresentation(ctx: SpliceContext): SpliceFailure | undefined {
|
|
917
|
-
const { lines, starts, wholeLine, indentOf, current, next, edits } =
|
|
964
|
+
const { lines, starts, wholeLine, indentOf, current, next, edits, parsed } =
|
|
965
|
+
ctx;
|
|
918
966
|
// Presentation edits are matched by IDENTITY, not by position: after a
|
|
919
967
|
// reorder `next.tiles[i]` and `current.tiles[i]` are different tiles, and
|
|
920
968
|
// comparing them pairwise would report every moved tile as changed and
|
|
@@ -927,9 +975,9 @@ function planTilePresentation(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
927
975
|
if (was === undefined) continue;
|
|
928
976
|
if (canonical(was) === canonical(tile)) continue;
|
|
929
977
|
|
|
930
|
-
//
|
|
931
|
-
//
|
|
932
|
-
//
|
|
978
|
+
// Declared in the model, not here, so there is nothing in this file to
|
|
979
|
+
// patch -- saying so beats writing a tag that would land on the wrong
|
|
980
|
+
// object.
|
|
933
981
|
if (tile.declaration.kind === "inherited") {
|
|
934
982
|
return {
|
|
935
983
|
ok: false,
|
|
@@ -939,7 +987,11 @@ function planTilePresentation(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
939
987
|
};
|
|
940
988
|
}
|
|
941
989
|
|
|
942
|
-
|
|
990
|
+
// An `opaque` tile falls through on purpose: its TAGS are ordinary `#`
|
|
991
|
+
// lines in this file, so a label or colspan change is safe. Only a filter
|
|
992
|
+
// has nowhere to go, and the filter pass refuses that on its own.
|
|
993
|
+
|
|
994
|
+
const declLine = viewDeclarationLine(parsed, tile.source, tile.name);
|
|
943
995
|
if (declLine < 0) {
|
|
944
996
|
return {
|
|
945
997
|
ok: false,
|
|
@@ -949,7 +1001,7 @@ function planTilePresentation(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
949
1001
|
};
|
|
950
1002
|
}
|
|
951
1003
|
|
|
952
|
-
const { tags } = blockAbove(lines, declLine);
|
|
1004
|
+
const { tags } = blockAbove(parsed, lines, declLine);
|
|
953
1005
|
const indent = indentOf(declLine);
|
|
954
1006
|
const wanted = tagsFor(tile);
|
|
955
1007
|
const wantedByKey = new Map(
|
|
@@ -986,34 +1038,337 @@ function planTilePresentation(ctx: SpliceContext): SpliceFailure | undefined {
|
|
|
986
1038
|
});
|
|
987
1039
|
}
|
|
988
1040
|
|
|
989
|
-
// The filter bindings live in the declaration itself
|
|
990
|
-
//
|
|
991
|
-
//
|
|
992
|
-
//
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
)
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1041
|
+
// The filter bindings live in the declaration itself either way, and
|
|
1042
|
+
// only the BINDING clauses are ours: a `limit:`, an `order_by:` or a
|
|
1043
|
+
// `where:` on a literal someone wrote is unmodeled Malloy and stays
|
|
1044
|
+
// exactly as written, in both shapes.
|
|
1045
|
+
if (canonical(was.filters) === canonical(tile.filters)) continue;
|
|
1046
|
+
const view = viewOf(ctx, tile);
|
|
1047
|
+
if (!view) {
|
|
1048
|
+
return {
|
|
1049
|
+
ok: false,
|
|
1050
|
+
reason: `Could not find \`${tile.name}\`'s declaration to filter it.`,
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
if (view.body.kind === "reference") {
|
|
1054
|
+
const failure = planReferenceFilters(ctx, tile, view);
|
|
1055
|
+
if (failure) return failure;
|
|
1056
|
+
continue;
|
|
1057
|
+
}
|
|
1058
|
+
if (view.body.kind !== "inline") {
|
|
1059
|
+
return {
|
|
1060
|
+
ok: false,
|
|
1061
|
+
reason:
|
|
1062
|
+
`\`${tile.name}\`'s body is ${view.body.why}, so there is no ` +
|
|
1063
|
+
`single first stage to write its filter into.`,
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
const clash = givenCollision(tile.filters, view.body.stage);
|
|
1067
|
+
if (clash) return collisionRefusal(tile.name, clash.given);
|
|
1068
|
+
const failure = planStageFilters(ctx, tile, view.body.stage);
|
|
1069
|
+
if (failure) return failure;
|
|
1070
|
+
}
|
|
1071
|
+
return undefined;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/** The 0-based line an offset falls on. */
|
|
1075
|
+
const lineOf = (parsed: ParsedMalloy, offset: number): number =>
|
|
1076
|
+
parsed.lineStarts.findLastIndex((at) => at <= offset);
|
|
1077
|
+
|
|
1078
|
+
/** The parsed declaration a tile names, if this file declares it. */
|
|
1079
|
+
function viewOf(ctx: SpliceContext, tile: DashboardTile): TreeView | undefined {
|
|
1080
|
+
return ctx.parsed.sources
|
|
1081
|
+
.find((source) => source.name === tile.source)
|
|
1082
|
+
?.views.find((view) => view.name === tile.name);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/**
|
|
1086
|
+
* The requested binding whose given is ALREADY filtered on by a clause the
|
|
1087
|
+
* builder does not own.
|
|
1088
|
+
*
|
|
1089
|
+
* Binding it again would filter the tile on one control twice while only one
|
|
1090
|
+
* of the two could ever be unbound, so the edit is refused instead. The test
|
|
1091
|
+
* is structural: a clause the tree says is not a `field <op> $GIVEN` binding,
|
|
1092
|
+
* which nonetheless references that given. The scan this replaces looked for
|
|
1093
|
+
* `$NAME` in text and could be disarmed by an apostrophe in a comment.
|
|
1094
|
+
*/
|
|
1095
|
+
function givenCollision(
|
|
1096
|
+
filters: Array<{ field: string; given: string; op?: string }> | undefined,
|
|
1097
|
+
stage: TreeStage | undefined,
|
|
1098
|
+
): { given: string } | undefined {
|
|
1099
|
+
if (!stage) return undefined;
|
|
1100
|
+
const unmanaged = new Set<string>();
|
|
1101
|
+
for (const where of stage.wheres)
|
|
1102
|
+
for (const clause of where.clauses)
|
|
1103
|
+
if (!clause.binding) for (const g of clause.givens) unmanaged.add(g);
|
|
1104
|
+
return (filters ?? []).find((f) => unmanaged.has(f.given));
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function collisionRefusal(tileName: string, given: string): SpliceFailure {
|
|
1108
|
+
return {
|
|
1109
|
+
ok: false,
|
|
1110
|
+
reason:
|
|
1111
|
+
`\`${tileName}\` already filters on \`$${given}\` in a \`where:\` ` +
|
|
1112
|
+
`the builder does not manage, so binding it again would filter on ` +
|
|
1113
|
+
`it twice. Edit that \`where:\` in the file instead.`,
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
const bindingText = (f: { field: string; given: string; op?: string }) =>
|
|
1118
|
+
`where: ${f.field} ${f.op ?? "~"} $${f.given}`;
|
|
1119
|
+
|
|
1120
|
+
/**
|
|
1121
|
+
* THE RULE the three removal paths obey: a comment goes only with a
|
|
1122
|
+
* declaration the author asked to delete outright. A filter edit is a REWRITE
|
|
1123
|
+
* of a declaration that stays, so a comment in the range it would clear is
|
|
1124
|
+
* nobody's to destroy — and one it would leave standing over the closing brace
|
|
1125
|
+
* is nobody's to strand either. Both refuse, and say which comment.
|
|
1126
|
+
*
|
|
1127
|
+
* Removing a TILE is the other case, and the one exception: the declaration
|
|
1128
|
+
* itself is what was asked for, so its own comments go with it, and the builder
|
|
1129
|
+
* shows that diff before a structural save.
|
|
1130
|
+
*/
|
|
1131
|
+
function commentRefusal(
|
|
1132
|
+
tileName: string,
|
|
1133
|
+
parsed: ParsedMalloy,
|
|
1134
|
+
comment: Span,
|
|
1135
|
+
): SpliceFailure {
|
|
1136
|
+
return {
|
|
1137
|
+
ok: false,
|
|
1138
|
+
reason:
|
|
1139
|
+
`Removing a filter from \`${tileName}\` would also remove the comment ` +
|
|
1140
|
+
`written beside it (\`${parsed.text
|
|
1141
|
+
.slice(comment.start, comment.end)
|
|
1142
|
+
.trim()}\`), so nothing was written. Delete it in the file first.`,
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* The comment a whole-statement cut would take with it, or strand behind.
|
|
1148
|
+
*
|
|
1149
|
+
* `commentIn` covers what the cut spans. The second half covers what it does
|
|
1150
|
+
* not: a comment TRAILING the statement's last line survives the cut and is
|
|
1151
|
+
* left explaining the closing brace, which says something its author never
|
|
1152
|
+
* wrote. Only a comment that directly follows the statement counts — a second
|
|
1153
|
+
* statement sharing the line owns its own trailing comment, and that one the
|
|
1154
|
+
* cut never reaches.
|
|
1155
|
+
*/
|
|
1156
|
+
function commentLostBy(
|
|
1157
|
+
parsed: ParsedMalloy,
|
|
1158
|
+
statement: Span,
|
|
1159
|
+
cut: Span,
|
|
1160
|
+
): Span | undefined {
|
|
1161
|
+
const inside = parsed.commentIn(cut);
|
|
1162
|
+
if (inside) return inside;
|
|
1163
|
+
const trailing = parsed.trailingComment(lineOf(parsed, statement.end - 1));
|
|
1164
|
+
return trailing &&
|
|
1165
|
+
trailing.start >= statement.end &&
|
|
1166
|
+
parsed.text.slice(statement.end, trailing.start).trim() === ""
|
|
1167
|
+
? trailing
|
|
1168
|
+
: undefined;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* The span to delete to remove ONE clause of a `where:` list, separator and
|
|
1173
|
+
* all: up to the next clause when there is one, back to the previous when it
|
|
1174
|
+
* is the last. Taking only the clause's own span would leave the comma beside
|
|
1175
|
+
* it, which is a syntax error.
|
|
1176
|
+
*/
|
|
1177
|
+
function clauseCut(clauses: Array<{ span: Span }>, index: number): Span {
|
|
1178
|
+
const self = clauses[index].span;
|
|
1179
|
+
if (index + 1 < clauses.length)
|
|
1180
|
+
return { start: self.start, end: clauses[index + 1].span.start };
|
|
1181
|
+
if (index > 0) return { start: clauses[index - 1].span.end, end: self.end };
|
|
1182
|
+
return self;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
/**
|
|
1186
|
+
* The span to delete to remove a whole statement. A statement alone on its
|
|
1187
|
+
* line takes the line with it, so no blank one is left behind; one sharing a
|
|
1188
|
+
* line with a brace or another statement gives up only its own span and the
|
|
1189
|
+
* space before it, so the brace survives.
|
|
1190
|
+
*/
|
|
1191
|
+
function statementCut(parsed: ParsedMalloy, span: Span): Span {
|
|
1192
|
+
const line =
|
|
1193
|
+
parsed.lineStarts[
|
|
1194
|
+
parsed.lineStarts.findLastIndex((at) => at <= span.start)
|
|
1195
|
+
];
|
|
1196
|
+
const before = parsed.text.slice(line, span.start);
|
|
1197
|
+
const lineEnd = parsed.text.indexOf("\n", span.end);
|
|
1198
|
+
const after = parsed.text.slice(span.end, lineEnd < 0 ? undefined : lineEnd);
|
|
1199
|
+
if (before.trim() === "" && after.trim() === "")
|
|
1200
|
+
return {
|
|
1201
|
+
start: line,
|
|
1202
|
+
end: lineEnd < 0 ? parsed.text.length : lineEnd + 1,
|
|
1203
|
+
};
|
|
1204
|
+
if (before.trim() === "") {
|
|
1205
|
+
// Only indentation before it, and something after -- a closing brace,
|
|
1206
|
+
// say. The indent stays and belongs to whatever follows, so the space
|
|
1207
|
+
// between goes instead.
|
|
1208
|
+
const gap = /^\s*/.exec(after)?.[0].length ?? 0;
|
|
1209
|
+
return { start: span.start, end: span.end + gap };
|
|
1210
|
+
}
|
|
1211
|
+
// A separator belonging to the statement BEFORE this one goes with it:
|
|
1212
|
+
// leaving the comma behind turns the previous statement into a list whose
|
|
1213
|
+
// last entry has just been deleted.
|
|
1214
|
+
const trimmed = before.replace(/[\s,]*$/, "");
|
|
1215
|
+
return { start: line + trimmed.length, end: span.end };
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* Rewrite a tile's builder-managed `where:` bindings inside `stage`.
|
|
1220
|
+
*
|
|
1221
|
+
* One implementation for both tile shapes, because a reference tile's
|
|
1222
|
+
* `+ { … }` refinement and an inline tile's own first stage are the same
|
|
1223
|
+
* thing to the parser. Every position comes from the tree, so a clause is
|
|
1224
|
+
* replaced, dropped or appended by SPAN: a `limit:`, a compound predicate, a
|
|
1225
|
+
* brace sharing the line or a trailing comment all sit outside those spans
|
|
1226
|
+
* and are carried over untouched.
|
|
1227
|
+
*/
|
|
1228
|
+
function planStageFilters(
|
|
1229
|
+
ctx: SpliceContext,
|
|
1230
|
+
tile: DashboardTile,
|
|
1231
|
+
stage: TreeStage,
|
|
1232
|
+
): SpliceFailure | undefined {
|
|
1233
|
+
const { parsed, edits } = ctx;
|
|
1234
|
+
const wanted = new Map((tile.filters ?? []).map((f) => [f.given, f]));
|
|
1235
|
+
const seen = new Set<string>();
|
|
1236
|
+
let lastManaged: Span | undefined;
|
|
1237
|
+
|
|
1238
|
+
for (const where of stage.wheres) {
|
|
1239
|
+
const managed = where.clauses
|
|
1240
|
+
.map((clause, index) => ({ clause, index }))
|
|
1241
|
+
.filter(({ clause }) => clause.binding !== undefined);
|
|
1242
|
+
if (managed.length === 0) continue;
|
|
1243
|
+
const surviving = managed.filter(({ clause }) =>
|
|
1244
|
+
wanted.has(clause.binding!.given),
|
|
1006
1245
|
);
|
|
1007
|
-
const
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
if (
|
|
1012
|
-
|
|
1246
|
+
for (const { clause } of managed) seen.add(clause.binding!.given);
|
|
1247
|
+
|
|
1248
|
+
// Every clause of this `where:` was ours and none survives: the
|
|
1249
|
+
// statement itself goes, rather than being left as a bare `where:`.
|
|
1250
|
+
if (surviving.length === 0 && managed.length === where.clauses.length) {
|
|
1251
|
+
const cut = statementCut(parsed, where.span);
|
|
1252
|
+
const lost = commentLostBy(parsed, where.span, cut);
|
|
1253
|
+
if (lost) return commentRefusal(tile.name, parsed, lost);
|
|
1254
|
+
edits.push({ ...cut, text: "" });
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (surviving.length > 0) lastManaged = where.span;
|
|
1258
|
+
// Highest index first, so an earlier cut cannot move a later span.
|
|
1259
|
+
for (let i = managed.length - 1; i >= 0; i--) {
|
|
1260
|
+
const { clause, index } = managed[i];
|
|
1261
|
+
const want = wanted.get(clause.binding!.given);
|
|
1262
|
+
if (!want) {
|
|
1263
|
+
const cut = clauseCut(where.clauses, index);
|
|
1264
|
+
// The separator between two clauses is the builder's to take; a
|
|
1265
|
+
// comment written in that gap is not, and nothing downstream can
|
|
1266
|
+
// see it go.
|
|
1267
|
+
const comment = parsed.commentIn(cut);
|
|
1268
|
+
if (comment) return commentRefusal(tile.name, parsed, comment);
|
|
1269
|
+
edits.push({ ...cut, text: "" });
|
|
1270
|
+
continue;
|
|
1271
|
+
}
|
|
1272
|
+
// The clause text without its `where:` keyword, which the statement
|
|
1273
|
+
// already carries.
|
|
1274
|
+
const text = bindingText(want).replace(/^where:\s*/, "");
|
|
1275
|
+
if (text !== parsed.text.slice(clause.span.start, clause.span.end))
|
|
1276
|
+
edits.push({ ...clause.span, text });
|
|
1277
|
+
}
|
|
1013
1278
|
}
|
|
1279
|
+
|
|
1280
|
+
const added = (tile.filters ?? []).filter((f) => !seen.has(f.given));
|
|
1281
|
+
if (added.length === 0) return undefined;
|
|
1282
|
+
|
|
1283
|
+
// After the last statement already inside the block, so a new binding
|
|
1284
|
+
// follows what was there; inside the opening brace when there is none.
|
|
1285
|
+
let anchor =
|
|
1286
|
+
lastManaged?.end ??
|
|
1287
|
+
(stage.statements.length > 0
|
|
1288
|
+
? stage.statements[stage.statements.length - 1].span.end
|
|
1289
|
+
: stage.openEnd);
|
|
1290
|
+
// A `//` comment is trivia, so it sits outside every span and the anchor
|
|
1291
|
+
// lands BEFORE one written after the statement it explains. Inserting a
|
|
1292
|
+
// line there would slide that comment onto the new `where:`, where it says
|
|
1293
|
+
// something the author never wrote -- and nothing downstream can tell,
|
|
1294
|
+
// because the comment is still in the file.
|
|
1295
|
+
if (!stage.oneLine) {
|
|
1296
|
+
const trailing = parsed.trailingComment(lineOf(parsed, anchor));
|
|
1297
|
+
if (trailing && trailing.start >= anchor) anchor = trailing.end;
|
|
1298
|
+
}
|
|
1299
|
+
// A comma only ever follows another `where:`. Malloy rejects one after a
|
|
1300
|
+
// `limit:` -- the writer used to emit `{ limit: 5, where: … }` and that is
|
|
1301
|
+
// a parse error -- while a space parses after every statement form. A
|
|
1302
|
+
// one-line `where:` whose own span already ends at the separator needs none
|
|
1303
|
+
// added, or the file gains a `,,`.
|
|
1304
|
+
const lead =
|
|
1305
|
+
lastManaged !== undefined && !/,\s*$/.test(parsed.text.slice(0, anchor))
|
|
1306
|
+
? ", "
|
|
1307
|
+
: " ";
|
|
1308
|
+
const text = stage.oneLine
|
|
1309
|
+
? `${lead}${added.map(bindingText).join(", ")}`
|
|
1310
|
+
: added.map((f) => `\n${stage.indent}${bindingText(f)}`).join("");
|
|
1311
|
+
edits.push({ start: anchor, end: anchor, text });
|
|
1014
1312
|
return undefined;
|
|
1015
1313
|
}
|
|
1016
1314
|
|
|
1315
|
+
/**
|
|
1316
|
+
* A reference tile's filters, which live in a `+ { … }` refinement that may
|
|
1317
|
+
* have to be created or removed outright.
|
|
1318
|
+
*/
|
|
1319
|
+
function planReferenceFilters(
|
|
1320
|
+
ctx: SpliceContext,
|
|
1321
|
+
tile: DashboardTile,
|
|
1322
|
+
view: TreeView,
|
|
1323
|
+
): SpliceFailure | undefined {
|
|
1324
|
+
if (view.body.kind !== "reference") return undefined;
|
|
1325
|
+
const { edits } = ctx;
|
|
1326
|
+
const { fromSpan, refinement } = view.body;
|
|
1327
|
+
|
|
1328
|
+
const clash = givenCollision(tile.filters, refinement);
|
|
1329
|
+
if (clash) return collisionRefusal(tile.name, clash.given);
|
|
1330
|
+
|
|
1331
|
+
const filters = tile.filters ?? [];
|
|
1332
|
+
if (!refinement) {
|
|
1333
|
+
if (filters.length === 0) return undefined;
|
|
1334
|
+
// Appended at the END of the base expression, which is before any
|
|
1335
|
+
// trailing `//` comment because the comment is not part of it.
|
|
1336
|
+
const text = ` + { ${filters.map(bindingText).join(", ")} }`;
|
|
1337
|
+
edits.push({ start: fromSpan.end, end: fromSpan.end, text });
|
|
1338
|
+
return undefined;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// Everything in the refinement that is not a managed binding. If nothing
|
|
1342
|
+
// is left and nothing is wanted, the whole `+ { … }` goes with it.
|
|
1343
|
+
const unmanaged = refinement.wheres.some((w) =>
|
|
1344
|
+
w.clauses.some((c) => !c.binding),
|
|
1345
|
+
);
|
|
1346
|
+
const otherStatements = refinement.statements.some(
|
|
1347
|
+
(st) => !refinement.wheres.some((w) => w.span.start === st.span.start),
|
|
1348
|
+
);
|
|
1349
|
+
if (filters.length === 0 && !unmanaged && !otherStatements) {
|
|
1350
|
+
const cut = { start: fromSpan.end, end: refinement.span.end };
|
|
1351
|
+
// The `+` and the block are the builder's; a comment written between or
|
|
1352
|
+
// inside them is not, and it is trivia to the parser, so deleting it
|
|
1353
|
+
// leaves a file that parses and reads back exactly as asked for.
|
|
1354
|
+
const comment = ctx.parsed.commentIn(cut);
|
|
1355
|
+
if (comment) {
|
|
1356
|
+
return {
|
|
1357
|
+
ok: false,
|
|
1358
|
+
reason:
|
|
1359
|
+
`Removing the last filter from \`${tile.name}\` would also ` +
|
|
1360
|
+
`remove the comment beside its refinement (\`${ctx.parsed.text
|
|
1361
|
+
.slice(comment.start, comment.end)
|
|
1362
|
+
.trim()}\`), so nothing was written. Delete it in the file ` +
|
|
1363
|
+
`first, or edit the refinement there.`,
|
|
1364
|
+
};
|
|
1365
|
+
}
|
|
1366
|
+
edits.push({ ...cut, text: "" });
|
|
1367
|
+
return undefined;
|
|
1368
|
+
}
|
|
1369
|
+
return planStageFilters(ctx, tile, refinement);
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1017
1372
|
export async function spliceDashboardDocument(
|
|
1018
1373
|
sourceText: string,
|
|
1019
1374
|
next: DashboardDocument,
|
|
@@ -1029,6 +1384,13 @@ export async function spliceDashboardDocument(
|
|
|
1029
1384
|
const shape = checkShape(current, next);
|
|
1030
1385
|
if ("reason" in shape) return shape;
|
|
1031
1386
|
|
|
1387
|
+
const parse = await parseMalloy(sourceText);
|
|
1388
|
+
if (parseRefused(parse))
|
|
1389
|
+
return {
|
|
1390
|
+
ok: false,
|
|
1391
|
+
reason: `Cannot edit a file that will not open: ${parse.reason}`,
|
|
1392
|
+
};
|
|
1393
|
+
|
|
1032
1394
|
const lines = sourceText.split("\n");
|
|
1033
1395
|
const starts = lineStarts(sourceText);
|
|
1034
1396
|
const wholeLine = (line: number): { start: number; end: number } => ({
|
|
@@ -1041,6 +1403,7 @@ export async function spliceDashboardDocument(
|
|
|
1041
1403
|
const ctx: SpliceContext = {
|
|
1042
1404
|
...shape,
|
|
1043
1405
|
sourceText,
|
|
1406
|
+
parsed: parse.parsed,
|
|
1044
1407
|
lines,
|
|
1045
1408
|
starts,
|
|
1046
1409
|
wholeLine,
|
|
@@ -1103,8 +1466,8 @@ export async function spliceDashboardDocument(
|
|
|
1103
1466
|
return {
|
|
1104
1467
|
ok: false,
|
|
1105
1468
|
reason:
|
|
1106
|
-
"
|
|
1107
|
-
"
|
|
1469
|
+
"No part of this edit was recognized, so nothing was written. " +
|
|
1470
|
+
"Your changes are still here.",
|
|
1108
1471
|
};
|
|
1109
1472
|
}
|
|
1110
1473
|
return { ok: true, source: sourceText };
|
|
@@ -1112,9 +1475,36 @@ export async function spliceDashboardDocument(
|
|
|
1112
1475
|
|
|
1113
1476
|
const spliced = applyEdits(sourceText, edits);
|
|
1114
1477
|
|
|
1115
|
-
// The gate
|
|
1116
|
-
//
|
|
1117
|
-
//
|
|
1478
|
+
// The first gate, and the only one that can see damage OUTSIDE the part of
|
|
1479
|
+
// the file the builder models. The readback below compares the projection
|
|
1480
|
+
// -- tiles, tags, filters -- so text this writer strands beside a binding
|
|
1481
|
+
// it did rewrite is invisible to it: the orphan is not a filter, so the
|
|
1482
|
+
// comparison it would have to fail never looks at it. Malloy's parser is
|
|
1483
|
+
// the only reader here that judges the whole file.
|
|
1484
|
+
//
|
|
1485
|
+
// A file with a syntax error never reaches here -- the parse above refuses
|
|
1486
|
+
// it, and the editor refuses to open it at all -- so the guard below is
|
|
1487
|
+
// belt and braces against those two ever disagreeing.
|
|
1488
|
+
//
|
|
1489
|
+
// Comparing the two error lists instead, and refusing what looks new, is
|
|
1490
|
+
// what this replaced: the parser's messages quote the tokens around the
|
|
1491
|
+
// error, so inserting an unrelated line rewrites the message of a fault
|
|
1492
|
+
// that was already there and it reads as one we just caused.
|
|
1493
|
+
if ((await syntaxErrors(sourceText)).length === 0) {
|
|
1494
|
+
const broke = await syntaxErrors(spliced);
|
|
1495
|
+
if (broke.length > 0) {
|
|
1496
|
+
return {
|
|
1497
|
+
ok: false,
|
|
1498
|
+
reason:
|
|
1499
|
+
"The edit would have produced a file Malloy cannot parse, so " +
|
|
1500
|
+
`it was not written (${broke[0]}). Your changes are still here.`,
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
// The second gate. Read back what was actually written and compare it
|
|
1506
|
+
// against what was asked for. Comments survived because they were never
|
|
1507
|
+
// rewritten; correctness is established here rather than assumed.
|
|
1118
1508
|
const after = await readDashboardDocument(spliced);
|
|
1119
1509
|
if (readFailed(after)) {
|
|
1120
1510
|
return {
|
|
@@ -1126,7 +1516,7 @@ export async function spliceDashboardDocument(
|
|
|
1126
1516
|
return {
|
|
1127
1517
|
ok: false,
|
|
1128
1518
|
reason:
|
|
1129
|
-
"
|
|
1519
|
+
"What was written did not read back as what was asked for, so it " +
|
|
1130
1520
|
"was not written. Your changes are still here.",
|
|
1131
1521
|
};
|
|
1132
1522
|
}
|