@dxos/deus 0.0.0-stage → 0.0.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/LICENSE +105 -0
- package/README.md +24 -2
- package/bin/deus.ts +42 -0
- package/dist/lib/chunk-blocks.mjs +151 -0
- package/dist/lib/chunk-blocks.mjs.map +1 -0
- package/dist/lib/chunk-check.mjs +452 -0
- package/dist/lib/chunk-check.mjs.map +1 -0
- package/dist/lib/chunk-completion.mjs +112 -0
- package/dist/lib/chunk-completion.mjs.map +1 -0
- package/dist/lib/chunk-extension.mjs +396 -0
- package/dist/lib/chunk-extension.mjs.map +1 -0
- package/dist/lib/chunk-parse.mjs +21 -0
- package/dist/lib/chunk-parse.mjs.map +1 -0
- package/dist/lib/cli.mjs +946 -0
- package/dist/lib/cli.mjs.map +1 -0
- package/dist/lib/extension.mjs +4 -0
- package/dist/lib/index.mjs +6 -0
- package/dist/lib/parse.mjs +4 -0
- package/dist/lib/review.mjs +260 -0
- package/dist/lib/review.mjs.map +1 -0
- package/dist/lib/spec-index.mjs +28 -0
- package/dist/lib/spec-index.mjs.map +1 -0
- package/dist/types/bin/deus.d.ts +3 -0
- package/dist/types/bin/deus.d.ts.map +1 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps/dx.config.d.ts +8 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps/dx.config.d.ts.map +1 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-diverged/dx.config.d.ts +8 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-diverged/dx.config.d.ts.map +1 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-empty/dx.config.d.ts +7 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-empty/dx.config.d.ts.map +1 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-mismatch/dx.config.d.ts +8 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-mismatch/dx.config.d.ts.map +1 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-noplugin/dx.config.d.ts +5 -0
- package/dist/types/src/cli/__fixtures__/review/plugin-deps-noplugin/dx.config.d.ts.map +1 -0
- package/dist/types/src/cli/changelog.d.ts +25 -0
- package/dist/types/src/cli/changelog.d.ts.map +1 -0
- package/dist/types/src/cli/cli.test.d.ts +2 -0
- package/dist/types/src/cli/cli.test.d.ts.map +1 -0
- package/dist/types/src/cli/errors.d.ts +183 -0
- package/dist/types/src/cli/errors.d.ts.map +1 -0
- package/dist/types/src/cli/features.d.ts +58 -0
- package/dist/types/src/cli/features.d.ts.map +1 -0
- package/dist/types/src/cli/git.d.ts +8 -0
- package/dist/types/src/cli/git.d.ts.map +1 -0
- package/dist/types/src/cli/index.d.ts +12 -0
- package/dist/types/src/cli/index.d.ts.map +1 -0
- package/dist/types/src/cli/lint.d.ts +17 -0
- package/dist/types/src/cli/lint.d.ts.map +1 -0
- package/dist/types/src/cli/qa-lint.d.ts +40 -0
- package/dist/types/src/cli/qa-lint.d.ts.map +1 -0
- package/dist/types/src/cli/qa.d.ts +86 -0
- package/dist/types/src/cli/qa.d.ts.map +1 -0
- package/dist/types/src/cli/review.d.ts +38 -0
- package/dist/types/src/cli/review.d.ts.map +1 -0
- package/dist/types/src/cli/spec-index.d.ts +17 -0
- package/dist/types/src/cli/spec-index.d.ts.map +1 -0
- package/dist/types/src/extension/blocks.d.ts +65 -0
- package/dist/types/src/extension/blocks.d.ts.map +1 -0
- package/dist/types/src/extension/check.d.ts +99 -0
- package/dist/types/src/extension/check.d.ts.map +1 -0
- package/dist/types/src/extension/check.test.d.ts +2 -0
- package/dist/types/src/extension/check.test.d.ts.map +1 -0
- package/dist/types/src/extension/code.d.ts +7 -0
- package/dist/types/src/extension/code.d.ts.map +1 -0
- package/dist/types/src/extension/complete.d.ts +6 -0
- package/dist/types/src/extension/complete.d.ts.map +1 -0
- package/dist/types/src/extension/completion.d.ts +10 -0
- package/dist/types/src/extension/completion.d.ts.map +1 -0
- package/dist/types/src/extension/constants.d.ts +6 -0
- package/dist/types/src/extension/constants.d.ts.map +1 -0
- package/dist/types/src/extension/fences.d.ts +10 -0
- package/dist/types/src/extension/fences.d.ts.map +1 -0
- package/dist/types/src/extension/gen/mdl.d.ts +3 -0
- package/dist/types/src/extension/gen/mdl.d.ts.map +1 -0
- package/dist/types/src/extension/gen/mdl.terms.d.ts +2 -0
- package/dist/types/src/extension/gen/mdl.terms.d.ts.map +1 -0
- package/dist/types/src/extension/index.d.ts +8 -0
- package/dist/types/src/extension/index.d.ts.map +1 -0
- package/dist/types/src/extension/language.d.ts +20 -0
- package/dist/types/src/extension/language.d.ts.map +1 -0
- package/dist/types/src/extension/lint.d.ts +8 -0
- package/dist/types/src/extension/lint.d.ts.map +1 -0
- package/dist/types/src/extension/parse.d.ts +6 -0
- package/dist/types/src/extension/parse.d.ts.map +1 -0
- package/dist/types/src/extension/parser.test.d.ts +2 -0
- package/dist/types/src/extension/parser.test.d.ts.map +1 -0
- package/dist/types/src/extension/refs.d.ts +32 -0
- package/dist/types/src/extension/refs.d.ts.map +1 -0
- package/dist/types/src/extension/refs.test.d.ts +2 -0
- package/dist/types/src/extension/refs.test.d.ts.map +1 -0
- package/dist/types/src/extension/syntax.d.ts +12 -0
- package/dist/types/src/extension/syntax.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/review/fields.d.ts +9 -0
- package/dist/types/src/review/fields.d.ts.map +1 -0
- package/dist/types/src/review/index.d.ts +5 -0
- package/dist/types/src/review/index.d.ts.map +1 -0
- package/dist/types/src/review/review.d.ts +70 -0
- package/dist/types/src/review/review.d.ts.map +1 -0
- package/dist/types/src/review/review.test.d.ts +2 -0
- package/dist/types/src/review/review.test.d.ts.map +1 -0
- package/dist/types/src/review/source-key.d.ts +35 -0
- package/dist/types/src/review/source-key.d.ts.map +1 -0
- package/dist/types/src/review/symbol.d.ts +17 -0
- package/dist/types/src/review/symbol.d.ts.map +1 -0
- package/dist/types/src/review/symbol.test.d.ts +2 -0
- package/dist/types/src/review/symbol.test.d.ts.map +1 -0
- package/dist/types/src/spec-index/index.d.ts +3 -0
- package/dist/types/src/spec-index/index.d.ts.map +1 -0
- package/dist/types/src/spec-index/parse.d.ts +11 -0
- package/dist/types/src/spec-index/parse.d.ts.map +1 -0
- package/dist/types/src/spec-index/spec-index.d.ts +21 -0
- package/dist/types/src/spec-index/spec-index.d.ts.map +1 -0
- package/dist/types/src/spec-index/spec-index.test.d.ts +2 -0
- package/dist/types/src/spec-index/spec-index.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/docs/DESIGN.md +561 -0
- package/docs/IDIOMS.md +265 -0
- package/lang/PLUGIN-.template.mdl +115 -0
- package/lang/app.mdl +200 -0
- package/lang/changelog.mdl +184 -0
- package/lang/core.mdl +203 -0
- package/lang/examples/chess-1.mdl +149 -0
- package/lang/examples/chess-2.mdl +203 -0
- package/lang/examples/chess-3.mdl +231 -0
- package/lang/features.mdl +92 -0
- package/lang/plugin.mdl +59 -0
- package/lang/qa.mdl +383 -0
- package/lang/review.mdl +103 -0
- package/lang/sql.mdl +72 -0
- package/package.json +74 -3
- package/src/cli/__fixtures__/bad-decision.mdl +13 -0
- package/src/cli/__fixtures__/features/CYCLE.mdl +25 -0
- package/src/cli/__fixtures__/features/FEATURES.mdl +47 -0
- package/src/cli/__fixtures__/features/SPEC.mdl +22 -0
- package/src/cli/__fixtures__/features/SPEC.review.mdl +32 -0
- package/src/cli/__fixtures__/features/TASKS.md +5 -0
- package/src/cli/__fixtures__/lint-refs/SPEC.mdl +7 -0
- package/src/cli/__fixtures__/lint-refs/SPEC.review.mdl +7 -0
- package/src/cli/__fixtures__/qa-lint/packages/plugins/plugin-risky/PLUGIN.mdl +29 -0
- package/src/cli/__fixtures__/qa-list/plugin-sample/spec/PLUGIN.mdl +51 -0
- package/src/cli/__fixtures__/review/SPEC.mdl +23 -0
- package/src/cli/__fixtures__/review/SPEC.review.mdl +26 -0
- package/src/cli/__fixtures__/review/plugin-deps/PLUGIN.mdl +9 -0
- package/src/cli/__fixtures__/review/plugin-deps/PLUGIN.review.mdl +13 -0
- package/src/cli/__fixtures__/review/plugin-deps/dx.config.ts +10 -0
- package/src/cli/__fixtures__/review/plugin-deps-diverged/PLUGIN.mdl +9 -0
- package/src/cli/__fixtures__/review/plugin-deps-diverged/PLUGIN.review.mdl +14 -0
- package/src/cli/__fixtures__/review/plugin-deps-diverged/dx.config.ts +10 -0
- package/src/cli/__fixtures__/review/plugin-deps-empty/PLUGIN.mdl +9 -0
- package/src/cli/__fixtures__/review/plugin-deps-empty/PLUGIN.review.mdl +13 -0
- package/src/cli/__fixtures__/review/plugin-deps-empty/dx.config.ts +9 -0
- package/src/cli/__fixtures__/review/plugin-deps-mismatch/PLUGIN.mdl +9 -0
- package/src/cli/__fixtures__/review/plugin-deps-mismatch/PLUGIN.review.mdl +13 -0
- package/src/cli/__fixtures__/review/plugin-deps-mismatch/dx.config.ts +10 -0
- package/src/cli/__fixtures__/review/plugin-deps-noplugin/PLUGIN.mdl +9 -0
- package/src/cli/__fixtures__/review/plugin-deps-noplugin/PLUGIN.review.mdl +13 -0
- package/src/cli/__fixtures__/review/plugin-deps-noplugin/dx.config.ts +7 -0
- package/src/cli/changelog.ts +122 -0
- package/src/cli/cli.test.ts +359 -0
- package/src/cli/errors.ts +23 -0
- package/src/cli/features.ts +320 -0
- package/src/cli/git.ts +35 -0
- package/src/cli/index.ts +27 -0
- package/src/cli/lint.ts +75 -0
- package/src/cli/qa-lint.ts +206 -0
- package/src/cli/qa.ts +300 -0
- package/src/cli/review.ts +359 -0
- package/src/cli/spec-index.ts +54 -0
- package/src/extension/blocks.ts +169 -0
- package/src/extension/check.test.ts +629 -0
- package/src/extension/check.ts +552 -0
- package/src/extension/code.ts +99 -0
- package/src/extension/complete.ts +34 -0
- package/src/extension/completion.ts +64 -0
- package/src/extension/constants.ts +28 -0
- package/src/extension/fences.ts +270 -0
- package/src/extension/gen/mdl.terms.ts +16 -0
- package/src/extension/gen/mdl.ts +16 -0
- package/src/extension/index.ts +25 -0
- package/src/extension/language.ts +33 -0
- package/src/extension/lint.ts +19 -0
- package/src/extension/mdl.grammar +60 -0
- package/src/extension/parse.ts +31 -0
- package/src/extension/parser.test.ts +248 -0
- package/src/extension/refs.test.ts +95 -0
- package/src/extension/refs.ts +94 -0
- package/src/extension/syntax.ts +25 -0
- package/src/index.ts +6 -0
- package/src/review/fields.ts +31 -0
- package/src/review/index.ts +8 -0
- package/src/review/review.test.ts +111 -0
- package/src/review/review.ts +184 -0
- package/src/review/source-key.ts +63 -0
- package/src/review/symbol.test.ts +47 -0
- package/src/review/symbol.ts +96 -0
- package/src/spec-index/index.ts +6 -0
- package/src/spec-index/parse.ts +22 -0
- package/src/spec-index/spec-index.test.ts +56 -0
- package/src/spec-index/spec-index.ts +49 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Diagnostic } from '@codemirror/lint';
|
|
6
|
+
|
|
7
|
+
import { frontmatter, reqIds } from '../spec-index/parse.ts';
|
|
8
|
+
import { type SpecIndex } from '../spec-index/spec-index.ts';
|
|
9
|
+
import {
|
|
10
|
+
type BlockHeader,
|
|
11
|
+
extensionTerms,
|
|
12
|
+
findInnerBlockHeaders,
|
|
13
|
+
parseBlockHeaders,
|
|
14
|
+
parseMdlBlocks,
|
|
15
|
+
} from './blocks.ts';
|
|
16
|
+
import { BLOCK_TYPES } from './constants.ts';
|
|
17
|
+
import { findSpecKeys, findSpecRefs, specDependencies } from './refs.ts';
|
|
18
|
+
|
|
19
|
+
const KNOWN_BLOCK_TYPES = new Set(BLOCK_TYPES);
|
|
20
|
+
|
|
21
|
+
const USER_REF = /^user:\S+$/;
|
|
22
|
+
const AGENT_REF = /^agent:\S+$/;
|
|
23
|
+
|
|
24
|
+
const fieldValue = (body: string, field: string): string | undefined =>
|
|
25
|
+
body.match(new RegExp(`^\\s+${field}:[ \\t]*(.*?)[ \\t]*(?:#.*)?$`, 'm'))?.[1];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Checks the attribution rules of the changelog dialect (`lang/changelog.mdl`), which keep
|
|
29
|
+
* decisions made by people and decisions made by agents separable. A `question` is attributed the same way.
|
|
30
|
+
*/
|
|
31
|
+
const lintAttribution = (body: string, noun: 'Decision' | 'Question'): string[] => {
|
|
32
|
+
const errors: string[] = [];
|
|
33
|
+
const by = fieldValue(body, 'by');
|
|
34
|
+
const principal = fieldValue(body, 'for');
|
|
35
|
+
if (!by) {
|
|
36
|
+
errors.push(`${noun} is missing \`by:\` — name the user: or agent: that made it.`);
|
|
37
|
+
} else if (AGENT_REF.test(by)) {
|
|
38
|
+
if (!principal) {
|
|
39
|
+
errors.push(`Agent ${noun.toLowerCase()} is missing \`for:\` — name the user: the agent acted for.`);
|
|
40
|
+
}
|
|
41
|
+
} else if (USER_REF.test(by)) {
|
|
42
|
+
if (principal !== undefined) {
|
|
43
|
+
errors.push(`\`for:\` applies only to agent ${noun.toLowerCase()}s.`);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
errors.push(`${noun} \`by: ${by}\` must be \`user:<handle>\` or \`agent:<name>\`.`);
|
|
47
|
+
}
|
|
48
|
+
if (principal && !USER_REF.test(principal)) {
|
|
49
|
+
errors.push(`\`for: ${principal}\` must be \`user:<handle>\`.`);
|
|
50
|
+
}
|
|
51
|
+
return errors;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type LintOptions = {
|
|
55
|
+
/** Enables `mdl/ref` and `mdl/uses`, which need every other spec to resolve against. */
|
|
56
|
+
index?: SpecIndex;
|
|
57
|
+
/** The spec's own id; defaults to the frontmatter `id`. */
|
|
58
|
+
selfId?: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const namespace = (id: string): string => id.split('.').slice(0, 2).join('.');
|
|
62
|
+
|
|
63
|
+
/** Lint message for an id the index does not hold. */
|
|
64
|
+
export const unknownSpecMessage = (id: string): string => `Unknown spec \`${id}\`.`;
|
|
65
|
+
|
|
66
|
+
/** Lint message for a reference to an item the spec does not declare. */
|
|
67
|
+
export const missingItemMessage = (id: string, item: string): string => `\`${id}\` has no item \`${item}\`.`;
|
|
68
|
+
|
|
69
|
+
/** Lint message for an id more than one file claims. */
|
|
70
|
+
export const duplicateSpecMessage = (id: string): string => `Spec id \`${id}\` is declared by more than one file.`;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Whether a `<id>#<item>` candidate is a spec reference rather than prose (a file name, an external schema id):
|
|
74
|
+
* its id is indexed, or shares its first two dotted segments with an indexed id.
|
|
75
|
+
*/
|
|
76
|
+
export const specReferenceFilter = (index: SpecIndex): ((id: string) => boolean) => {
|
|
77
|
+
const namespaces = new Set([...Object.keys(index.specs), ...Object.keys(index.duplicates)].map(namespace));
|
|
78
|
+
return (id) => namespaces.has(namespace(id));
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const lintReferences = (text: string, index: SpecIndex, selfId: string | undefined): Diagnostic[] => {
|
|
82
|
+
const diagnostics: Diagnostic[] = [];
|
|
83
|
+
const report = (source: string, from: number, to: number, message: string) =>
|
|
84
|
+
diagnostics.push({ from, to, severity: 'error', source, message });
|
|
85
|
+
const dependencies = specDependencies(text);
|
|
86
|
+
const dependsOn = new Set(dependencies.filter(({ via }) => via === 'dependsOn').map(({ id }) => id));
|
|
87
|
+
const declared = new Set<string>();
|
|
88
|
+
|
|
89
|
+
if (selfId && selfId in index.duplicates) {
|
|
90
|
+
const idLine = /^id:.*$/m.exec(text);
|
|
91
|
+
report(
|
|
92
|
+
'mdl/uses',
|
|
93
|
+
idLine?.index ?? 0,
|
|
94
|
+
(idLine?.index ?? 0) + (idLine?.[0].length ?? 0),
|
|
95
|
+
duplicateSpecMessage(selfId),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for (const { id, via, from, to } of dependencies) {
|
|
100
|
+
if (id === selfId) {
|
|
101
|
+
report('mdl/uses', from, to, 'A spec cannot use itself.');
|
|
102
|
+
} else if (id in index.duplicates) {
|
|
103
|
+
report('mdl/uses', from, to, duplicateSpecMessage(id));
|
|
104
|
+
} else if (!(id in index.specs)) {
|
|
105
|
+
report('mdl/uses', from, to, unknownSpecMessage(id));
|
|
106
|
+
} else if (via === 'uses' && dependsOn.has(id)) {
|
|
107
|
+
report('mdl/uses', from, to, `\`${id}\` is in dependsOn; do not repeat it under uses.`);
|
|
108
|
+
} else if (declared.has(id)) {
|
|
109
|
+
report('mdl/uses', from, to, `\`${id}\` is declared twice.`);
|
|
110
|
+
}
|
|
111
|
+
declared.add(id);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const isReference = specReferenceFilter(index);
|
|
115
|
+
const dependencyIds = new Set(dependencies.map(({ id }) => id));
|
|
116
|
+
// The spec's own items come from its text, since the index may predate the edit being linted.
|
|
117
|
+
let ownItems: Set<string> | undefined;
|
|
118
|
+
for (const { id, item, from, to } of findSpecRefs(text)) {
|
|
119
|
+
if (id === selfId) {
|
|
120
|
+
ownItems ??= new Set(
|
|
121
|
+
parseMdlBlocks(text).flatMap(({ id: own, body }) => [...(own ? [own] : []), ...reqIds(body)]),
|
|
122
|
+
);
|
|
123
|
+
if (!ownItems.has(item)) {
|
|
124
|
+
report('mdl/ref', from, to, missingItemMessage(id, item));
|
|
125
|
+
}
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!isReference(id)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (id in index.duplicates) {
|
|
132
|
+
report('mdl/ref', from, to, duplicateSpecMessage(id));
|
|
133
|
+
} else if (!(id in index.specs)) {
|
|
134
|
+
report('mdl/ref', from, to, unknownSpecMessage(id));
|
|
135
|
+
} else if (!dependencyIds.has(id)) {
|
|
136
|
+
report('mdl/ref', from, to, `\`${id}\` is not a dependency; add \`uses ${id}: …\` under ## Dependencies.`);
|
|
137
|
+
} else if (!index.specs[id].items.includes(item)) {
|
|
138
|
+
report('mdl/ref', from, to, missingItemMessage(id, item));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// The changelog is append-only, so a decision that predates this rule keeps the key it was written with.
|
|
143
|
+
const changelog = changelogRange(text);
|
|
144
|
+
for (const { from, to } of findSpecKeys(text)) {
|
|
145
|
+
if (!changelog || from < changelog.from || from >= changelog.to) {
|
|
146
|
+
report('mdl/ref', from, to, 'Refer to a spec by id (`<id>#<item>`), not by a github: key.');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return diagnostics;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Lints the text of a Spec .mdl document.
|
|
154
|
+
*
|
|
155
|
+
* Checks:
|
|
156
|
+
* - Unknown block types (first body line of ```mdl blocks) → error
|
|
157
|
+
* - `decision` and `question` attribution (`by:` / `for:` actor prefixes) → error
|
|
158
|
+
* - `mdl/block`: a second block header inside one fence, which every tool reads as body → error
|
|
159
|
+
* - `mdl/ref` and `mdl/uses`: spec references and dependencies, when an index is given → error
|
|
160
|
+
*/
|
|
161
|
+
export const lintMdl = (text: string, { index, selfId = frontmatter(text).id }: LintOptions = {}): Diagnostic[] => {
|
|
162
|
+
const diagnostics: Diagnostic[] = [];
|
|
163
|
+
for (const { type, from, to, body } of parseBlockHeaders(text)) {
|
|
164
|
+
if (type && !KNOWN_BLOCK_TYPES.has(type as (typeof BLOCK_TYPES)[number])) {
|
|
165
|
+
diagnostics.push({
|
|
166
|
+
from,
|
|
167
|
+
to,
|
|
168
|
+
severity: 'error',
|
|
169
|
+
source: 'mdl/block-type',
|
|
170
|
+
message: `Unknown block type "${type}". Declare it in the Extensions table or define it with an \`ext\` block.`,
|
|
171
|
+
});
|
|
172
|
+
} else if (type === 'decision' || type === 'question') {
|
|
173
|
+
for (const message of lintAttribution(body, type === 'decision' ? 'Decision' : 'Question')) {
|
|
174
|
+
diagnostics.push({ from, to, severity: 'error', source: 'mdl/decision', message });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const types = new Set<string>([...BLOCK_TYPES, ...extensionTerms(text)]);
|
|
179
|
+
for (const { type, id, from, to } of findInnerBlockHeaders(text, types)) {
|
|
180
|
+
diagnostics.push({
|
|
181
|
+
from,
|
|
182
|
+
to,
|
|
183
|
+
severity: 'error',
|
|
184
|
+
source: 'mdl/block',
|
|
185
|
+
message: `A second block header (\`${[type, id].filter(Boolean).join(' ')}\`) inside one fence; give each block its own \`\`\`mdl fence.`,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (index) {
|
|
189
|
+
diagnostics.push(...lintReferences(text, index, selfId));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return diagnostics;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/** One `decision` block of a spec's `## Changelog`, with the fields of `lang/changelog.mdl` it carries. */
|
|
196
|
+
export type ChangelogEntry = {
|
|
197
|
+
id: string;
|
|
198
|
+
/** The whole block body, for comparing entries. */
|
|
199
|
+
text: string;
|
|
200
|
+
/** The decision itself: the inline prose after `decision D-n:`. */
|
|
201
|
+
title: string;
|
|
202
|
+
date?: string;
|
|
203
|
+
/** The decider, `user:<handle>` or `agent:<name>`. */
|
|
204
|
+
by?: string;
|
|
205
|
+
/** The person an agent acted for. */
|
|
206
|
+
for?: string;
|
|
207
|
+
quote?: string;
|
|
208
|
+
context?: string;
|
|
209
|
+
why?: string;
|
|
210
|
+
source?: string;
|
|
211
|
+
supersedes?: string;
|
|
212
|
+
/** The question this decision answers. */
|
|
213
|
+
answers?: string;
|
|
214
|
+
/** Block ids the decision constrains. */
|
|
215
|
+
affects: string[];
|
|
216
|
+
/** Other decisions the prose cites as `[D-n]`, in order of first mention. */
|
|
217
|
+
refs: string[];
|
|
218
|
+
rejected: string[];
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/** One suggested answer of a `question`. */
|
|
222
|
+
export type QuestionOption = { title: string; description?: string };
|
|
223
|
+
|
|
224
|
+
/** One `question` block of a spec's `## Changelog`: a choice nobody has made yet (`lang/changelog.mdl` rule 10). */
|
|
225
|
+
export type ChangelogQuestion = {
|
|
226
|
+
id: string;
|
|
227
|
+
/** The whole block body, for comparing entries. */
|
|
228
|
+
text: string;
|
|
229
|
+
/** The question itself: the inline prose after `question Q-n:`. */
|
|
230
|
+
title: string;
|
|
231
|
+
date?: string;
|
|
232
|
+
/** The asker, `user:<handle>` or `agent:<name>`. */
|
|
233
|
+
by?: string;
|
|
234
|
+
/** The person an agent asked for. */
|
|
235
|
+
for?: string;
|
|
236
|
+
context?: string;
|
|
237
|
+
source?: string;
|
|
238
|
+
/** Block ids the answer will constrain. */
|
|
239
|
+
affects: string[];
|
|
240
|
+
options: QuestionOption[];
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const CHANGELOG_HEADING = /^## Changelog[ \t]*$/m;
|
|
244
|
+
|
|
245
|
+
/** The offsets of the `## Changelog` section, up to the next `## ` heading. */
|
|
246
|
+
const changelogRange = (text: string): { from: number; to: number } | undefined => {
|
|
247
|
+
const heading = CHANGELOG_HEADING.exec(text);
|
|
248
|
+
if (!heading) {
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
const after = heading.index + heading[0].length;
|
|
252
|
+
const next = text.slice(after).search(/^## /m);
|
|
253
|
+
return { from: heading.index, to: next === -1 ? text.length : after + next };
|
|
254
|
+
};
|
|
255
|
+
const DECISION_ID = /^D-(\d+)$/;
|
|
256
|
+
const QUESTION_ID = /^Q-(\d+)$/;
|
|
257
|
+
const DECISION_REF = /\[(D-\d+)\]/g;
|
|
258
|
+
|
|
259
|
+
const indentOf = (line: string): number => line.length - line.trimStart().length;
|
|
260
|
+
|
|
261
|
+
/** A field's value as lines: the inline value, or the indented lines of a `|` block or a `- item` list. */
|
|
262
|
+
const fieldLines = (body: string, field: string): string[] | undefined => {
|
|
263
|
+
const lines = body.split('\n');
|
|
264
|
+
const index = lines.findIndex((line) => new RegExp(`^\\s+${field}:`).test(line));
|
|
265
|
+
if (index === -1) {
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
const inline = fieldValue(lines[index], field);
|
|
269
|
+
if (inline && inline !== '|' && inline !== '>') {
|
|
270
|
+
return [inline];
|
|
271
|
+
}
|
|
272
|
+
const indent = indentOf(lines[index]);
|
|
273
|
+
const end = lines.findIndex((line, next) => next > index && line.trim() !== '' && indentOf(line) <= indent);
|
|
274
|
+
return lines.slice(index + 1, end === -1 ? undefined : end).map((line) => line.trim());
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const fieldText = (body: string, field: string): string | undefined => fieldLines(body, field)?.join('\n').trim();
|
|
278
|
+
|
|
279
|
+
const parseList = (value: string | undefined): string[] =>
|
|
280
|
+
value
|
|
281
|
+
?.replace(/^\[|\]$/g, '')
|
|
282
|
+
.split(',')
|
|
283
|
+
.map((item) => item.trim())
|
|
284
|
+
.filter(Boolean) ?? [];
|
|
285
|
+
|
|
286
|
+
const inlineProse = (body: string): string =>
|
|
287
|
+
body
|
|
288
|
+
.split('\n')[0]
|
|
289
|
+
.replace(/^\w+\s+[^:]*:\s*/, '')
|
|
290
|
+
.trim();
|
|
291
|
+
|
|
292
|
+
const parseDecision = ({ id, body }: BlockHeader): ChangelogEntry => {
|
|
293
|
+
const title = inlineProse(body);
|
|
294
|
+
const supersedes = fieldValue(body, 'supersedes');
|
|
295
|
+
const why = fieldText(body, 'why');
|
|
296
|
+
const context = fieldText(body, 'context');
|
|
297
|
+
const refs = [title, why, context]
|
|
298
|
+
.flatMap((prose) => [...(prose ?? '').matchAll(DECISION_REF)].map((match) => match[1]))
|
|
299
|
+
.filter((ref, index, all) => ref !== id && all.indexOf(ref) === index);
|
|
300
|
+
return {
|
|
301
|
+
id,
|
|
302
|
+
text: body.trim(),
|
|
303
|
+
title,
|
|
304
|
+
date: fieldValue(body, 'date'),
|
|
305
|
+
by: fieldValue(body, 'by'),
|
|
306
|
+
for: fieldValue(body, 'for'),
|
|
307
|
+
quote: fieldText(body, 'quote'),
|
|
308
|
+
context,
|
|
309
|
+
why,
|
|
310
|
+
source: fieldValue(body, 'source'),
|
|
311
|
+
supersedes,
|
|
312
|
+
answers: fieldValue(body, 'answers'),
|
|
313
|
+
affects: parseList(fieldValue(body, 'affects')),
|
|
314
|
+
refs,
|
|
315
|
+
rejected: (fieldLines(body, 'rejected') ?? []).filter(Boolean).map((line) => line.replace(/^-\s*/, '')),
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/** An option line is `<title>: <description>`, or just a title. */
|
|
320
|
+
const parseOption = (line: string): QuestionOption => {
|
|
321
|
+
const separator = line.indexOf(': ');
|
|
322
|
+
return separator === -1
|
|
323
|
+
? { title: line.trim() }
|
|
324
|
+
: { title: line.slice(0, separator).trim(), description: line.slice(separator + 2).trim() };
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const parseQuestion = ({ id, body }: BlockHeader): ChangelogQuestion => ({
|
|
328
|
+
id,
|
|
329
|
+
text: body.trim(),
|
|
330
|
+
title: inlineProse(body),
|
|
331
|
+
date: fieldValue(body, 'date'),
|
|
332
|
+
by: fieldValue(body, 'by'),
|
|
333
|
+
for: fieldValue(body, 'for'),
|
|
334
|
+
context: fieldText(body, 'context'),
|
|
335
|
+
source: fieldValue(body, 'source'),
|
|
336
|
+
affects: parseList(fieldValue(body, 'affects')),
|
|
337
|
+
options: (fieldLines(body, 'options') ?? [])
|
|
338
|
+
.filter((line) => line.startsWith('-'))
|
|
339
|
+
.map((line) => parseOption(line.replace(/^-\s*/, ''))),
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Splits a spec into its `## Changelog` decisions, its questions, and everything else.
|
|
344
|
+
* The section runs from its heading to the next level-1 or level-2 heading.
|
|
345
|
+
*/
|
|
346
|
+
export const parseChangelog = (
|
|
347
|
+
text: string,
|
|
348
|
+
): { entries: ChangelogEntry[]; questions: ChangelogQuestion[]; rest: string } => {
|
|
349
|
+
const heading = CHANGELOG_HEADING.exec(text);
|
|
350
|
+
if (!heading) {
|
|
351
|
+
return { entries: [], questions: [], rest: text };
|
|
352
|
+
}
|
|
353
|
+
const sectionStart = heading.index;
|
|
354
|
+
const afterHeading = sectionStart + heading[0].length;
|
|
355
|
+
const next = /^#{1,2} /m.exec(text.slice(afterHeading));
|
|
356
|
+
const sectionEnd = next ? afterHeading + next.index : text.length;
|
|
357
|
+
const section = text.slice(sectionStart, sectionEnd);
|
|
358
|
+
const blocks = parseBlockHeaders(section);
|
|
359
|
+
const entries = blocks.filter(({ type }) => type === 'decision').map(parseDecision);
|
|
360
|
+
const questions = blocks.filter(({ type }) => type === 'question').map(parseQuestion);
|
|
361
|
+
return { entries, questions, rest: text.slice(0, sectionStart) + text.slice(sectionEnd) };
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const normalize = (text: string): string =>
|
|
365
|
+
text
|
|
366
|
+
.split('\n')
|
|
367
|
+
.map((line) => line.trimEnd())
|
|
368
|
+
.join('\n')
|
|
369
|
+
.trim();
|
|
370
|
+
|
|
371
|
+
/** Reported when a spec changed beyond a trivial edit with no new decision; the one error a waiver lifts. */
|
|
372
|
+
export const MISSING_DECISION = 'The spec changed but no decision was added to `## Changelog`.';
|
|
373
|
+
|
|
374
|
+
const TYPO_MAX_WORDS = 3;
|
|
375
|
+
|
|
376
|
+
// One letter in these flips what a requirement says (must/most, can/can't), so they are never a typo fix.
|
|
377
|
+
const NORMATIVE_WORDS = new Set([
|
|
378
|
+
'all',
|
|
379
|
+
'always',
|
|
380
|
+
'any',
|
|
381
|
+
'can',
|
|
382
|
+
'cannot',
|
|
383
|
+
'every',
|
|
384
|
+
'may',
|
|
385
|
+
'must',
|
|
386
|
+
'never',
|
|
387
|
+
'no',
|
|
388
|
+
'none',
|
|
389
|
+
'not',
|
|
390
|
+
'only',
|
|
391
|
+
'shall',
|
|
392
|
+
'should',
|
|
393
|
+
]);
|
|
394
|
+
|
|
395
|
+
/** Optimal string alignment distance: insertions, deletions, substitutions and adjacent swaps each cost one. */
|
|
396
|
+
const editDistance = (left: string, right: string): number => {
|
|
397
|
+
const rows = Array.from({ length: left.length + 1 }, (_, row) =>
|
|
398
|
+
Array.from({ length: right.length + 1 }, (_, col) => (row === 0 ? col : col === 0 ? row : 0)),
|
|
399
|
+
);
|
|
400
|
+
for (let row = 1; row <= left.length; row++) {
|
|
401
|
+
for (let col = 1; col <= right.length; col++) {
|
|
402
|
+
const cost = left[row - 1] === right[col - 1] ? 0 : 1;
|
|
403
|
+
rows[row][col] = Math.min(rows[row - 1][col] + 1, rows[row][col - 1] + 1, rows[row - 1][col - 1] + cost);
|
|
404
|
+
if (row > 1 && col > 1 && left[row - 1] === right[col - 2] && left[row - 2] === right[col - 1]) {
|
|
405
|
+
rows[row][col] = Math.min(rows[row][col], rows[row - 2][col - 2] + 1);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return rows[left.length][right.length];
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const bareWord = (word: string): string => word.toLowerCase().replace(/[^a-z']/g, '');
|
|
413
|
+
|
|
414
|
+
const isTypoFix = (prev: string, next: string): boolean =>
|
|
415
|
+
!/\d/.test(prev + next) &&
|
|
416
|
+
!NORMATIVE_WORDS.has(bareWord(prev)) &&
|
|
417
|
+
!NORMATIVE_WORDS.has(bareWord(next)) &&
|
|
418
|
+
editDistance(prev, next) <= Math.floor(Math.max(prev.length, next.length) / 3);
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* True when `after` differs from `before` only by whitespace or a few small typo fixes, which do not change what
|
|
422
|
+
* the spec requires. Words with digits are ids or numbers, where one character is a different meaning.
|
|
423
|
+
*/
|
|
424
|
+
export const isTrivialEdit = (before: string, after: string): boolean => {
|
|
425
|
+
const prevWords = before.split(/\s+/).filter(Boolean);
|
|
426
|
+
const nextWords = after.split(/\s+/).filter(Boolean);
|
|
427
|
+
if (prevWords.length !== nextWords.length) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
const changed = prevWords
|
|
431
|
+
.map((word, index) => [word, nextWords[index]] as const)
|
|
432
|
+
.filter(([prev, next]) => prev !== next);
|
|
433
|
+
return changed.length <= TYPO_MAX_WORDS && changed.every(([prev, next]) => isTypoFix(prev, next));
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
/** Questions follow the decisions' append-only rules, with ids of their own sequence. */
|
|
437
|
+
const checkQuestions = (prev: readonly ChangelogQuestion[], next: readonly ChangelogQuestion[]): string[] => {
|
|
438
|
+
const errors: string[] = [];
|
|
439
|
+
const ids = next.map(({ id }) => id);
|
|
440
|
+
for (const id of ids) {
|
|
441
|
+
if (!QUESTION_ID.test(id)) {
|
|
442
|
+
errors.push(`Question id \`${id}\` must be \`Q-<n>\`.`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
for (const id of new Set(ids.filter((id, index) => ids.indexOf(id) !== index))) {
|
|
446
|
+
errors.push(`Question ${id} appears more than once.`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const nextById = new Map(next.map((question) => [question.id, question]));
|
|
450
|
+
for (const { id, text } of prev) {
|
|
451
|
+
const current = nextById.get(id);
|
|
452
|
+
if (!current) {
|
|
453
|
+
errors.push(`Question ${id} was removed — the changelog is append-only; answer it with a decision instead.`);
|
|
454
|
+
} else if (normalize(current.text) !== normalize(text)) {
|
|
455
|
+
errors.push(`Question ${id} was edited — the changelog is append-only; ask a new question instead.`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const prevIds = new Set(prev.map(({ id }) => id));
|
|
460
|
+
let expected = Math.max(0, ...prev.map(({ id }) => Number(QUESTION_ID.exec(id)?.[1] ?? 0))) + 1;
|
|
461
|
+
for (const { id } of next.filter(({ id }) => !prevIds.has(id))) {
|
|
462
|
+
if (id !== `Q-${expected}`) {
|
|
463
|
+
errors.push(`Question ${id} is out of sequence — the next id is Q-${expected}.`);
|
|
464
|
+
}
|
|
465
|
+
expected++;
|
|
466
|
+
}
|
|
467
|
+
return errors;
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Checks one spec change against the changelog rules: every change except a trivial edit adds a decision, and the
|
|
472
|
+
* log is append-only with monotonic ids. `before` is undefined for a new spec.
|
|
473
|
+
*/
|
|
474
|
+
export const checkChangelogChange = (before: string | undefined, after: string): string[] => {
|
|
475
|
+
const errors: string[] = [];
|
|
476
|
+
const prev = before === undefined ? { entries: [], questions: [], rest: '' } : parseChangelog(before);
|
|
477
|
+
const next = parseChangelog(after);
|
|
478
|
+
|
|
479
|
+
const ids = next.entries.map(({ id }) => id);
|
|
480
|
+
for (const id of ids) {
|
|
481
|
+
if (!DECISION_ID.test(id)) {
|
|
482
|
+
errors.push(`Decision id \`${id}\` must be \`D-<n>\`.`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const duplicates = ids.filter((id, index) => ids.indexOf(id) !== index);
|
|
486
|
+
for (const id of new Set(duplicates)) {
|
|
487
|
+
errors.push(`Decision ${id} appears more than once.`);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const nextById = new Map(next.entries.map((entry) => [entry.id, entry]));
|
|
491
|
+
for (const { id, text } of prev.entries) {
|
|
492
|
+
const current = nextById.get(id);
|
|
493
|
+
if (!current) {
|
|
494
|
+
errors.push(`Decision ${id} was removed — the changelog is append-only; supersede it instead.`);
|
|
495
|
+
} else if (normalize(current.text) !== normalize(text)) {
|
|
496
|
+
errors.push(`Decision ${id} was edited — the changelog is append-only; supersede it instead.`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const prevIds = new Set(prev.entries.map(({ id }) => id));
|
|
501
|
+
const added = next.entries.filter(({ id }) => !prevIds.has(id));
|
|
502
|
+
const kept = ids.filter((id) => prevIds.has(id));
|
|
503
|
+
const firstAdded = ids.findIndex((id) => !prevIds.has(id));
|
|
504
|
+
const keptInOrder =
|
|
505
|
+
kept.join() ===
|
|
506
|
+
prev.entries
|
|
507
|
+
.filter(({ id }) => nextById.has(id))
|
|
508
|
+
.map(({ id }) => id)
|
|
509
|
+
.join();
|
|
510
|
+
if (!keptInOrder || (firstAdded !== -1 && ids.slice(firstAdded).some((id) => prevIds.has(id)))) {
|
|
511
|
+
errors.push('Decisions must stay in order, with new ones appended last.');
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
let expected = Math.max(0, ...prev.entries.map(({ id }) => Number(DECISION_ID.exec(id)?.[1] ?? 0))) + 1;
|
|
515
|
+
for (const { id } of added) {
|
|
516
|
+
if (id !== `D-${expected}`) {
|
|
517
|
+
errors.push(`Decision ${id} is out of sequence — the next id is D-${expected}.`);
|
|
518
|
+
}
|
|
519
|
+
expected++;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
for (const { id, supersedes } of next.entries) {
|
|
523
|
+
if (supersedes !== undefined && !nextById.has(supersedes)) {
|
|
524
|
+
errors.push(`Decision ${id} supersedes ${supersedes}, which is not in the changelog.`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
errors.push(...checkQuestions(prev.questions, next.questions));
|
|
529
|
+
const questionIds = new Set(next.questions.map(({ id }) => id));
|
|
530
|
+
const answeredBy = new Map<string, string>();
|
|
531
|
+
for (const { id, answers, supersedes } of next.entries) {
|
|
532
|
+
if (answers === undefined) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
const first = answeredBy.get(answers);
|
|
536
|
+
if (!questionIds.has(answers)) {
|
|
537
|
+
errors.push(`Decision ${id} answers ${answers}, which is not in the changelog.`);
|
|
538
|
+
} else if (first !== undefined && supersedes !== first) {
|
|
539
|
+
errors.push(`Decision ${id} answers ${answers}, which ${first} already answered — supersede ${first} instead.`);
|
|
540
|
+
} else {
|
|
541
|
+
answeredBy.set(answers, id);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (added.length === 0 && (before === undefined || !isTrivialEdit(prev.rest, next.rest))) {
|
|
546
|
+
errors.push(
|
|
547
|
+
before === undefined ? 'A new spec needs a `## Changelog` with at least one decision.' : MISSING_DECISION,
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return errors;
|
|
552
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type EditorState, type Extension, RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';
|
|
8
|
+
import { highlightTree, tagHighlighter, tags } from '@lezer/highlight';
|
|
9
|
+
|
|
10
|
+
/** Fences that other extensions colour, or that have no nested language. */
|
|
11
|
+
const SKIPPED_FENCES = new Set(['', 'mdl', 'diagram', 'mermaid']);
|
|
12
|
+
|
|
13
|
+
const highlighter = tagHighlighter([
|
|
14
|
+
{
|
|
15
|
+
tag: [
|
|
16
|
+
tags.keyword,
|
|
17
|
+
tags.controlKeyword,
|
|
18
|
+
tags.definitionKeyword,
|
|
19
|
+
tags.moduleKeyword,
|
|
20
|
+
tags.operatorKeyword,
|
|
21
|
+
tags.modifier,
|
|
22
|
+
],
|
|
23
|
+
class: 'cm-code-keyword',
|
|
24
|
+
},
|
|
25
|
+
{ tag: [tags.string, tags.special(tags.string), tags.regexp, tags.escape], class: 'cm-code-string' },
|
|
26
|
+
{ tag: [tags.number, tags.bool, tags.null, tags.atom], class: 'cm-code-literal' },
|
|
27
|
+
{ tag: [tags.comment, tags.lineComment, tags.blockComment, tags.docComment], class: 'cm-code-comment' },
|
|
28
|
+
{ tag: [tags.typeName, tags.className, tags.namespace], class: 'cm-code-type' },
|
|
29
|
+
{ tag: [tags.function(tags.variableName), tags.function(tags.propertyName)], class: 'cm-code-function' },
|
|
30
|
+
{ tag: [tags.propertyName, tags.definition(tags.propertyName)], class: 'cm-code-property' },
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
// The markdown highlight style sets code tokens to `color: inherit !important`, so a mark's colour shows through.
|
|
34
|
+
const theme = EditorView.baseTheme({
|
|
35
|
+
'.cm-code-keyword': { color: 'var(--color-violet-400)' },
|
|
36
|
+
'.cm-code-string': { color: 'var(--color-emerald-400)' },
|
|
37
|
+
'.cm-code-literal': { color: 'var(--color-orange-400)' },
|
|
38
|
+
'.cm-code-comment': { color: 'var(--color-slate-400)', fontStyle: 'italic' },
|
|
39
|
+
'.cm-code-type': { color: 'var(--color-sky-400)' },
|
|
40
|
+
'.cm-code-function': { color: 'var(--color-amber-400)' },
|
|
41
|
+
'.cm-code-property': { color: 'var(--color-blue-400)' },
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const fenceLanguage = (state: EditorState, from: number, to: number): string =>
|
|
45
|
+
state.sliceDoc(from, to).trim().split(/\s/)[0].toLowerCase();
|
|
46
|
+
|
|
47
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
48
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
49
|
+
const tree = syntaxTree(view.state);
|
|
50
|
+
for (const { from, to } of view.visibleRanges) {
|
|
51
|
+
tree.iterate({
|
|
52
|
+
from,
|
|
53
|
+
to,
|
|
54
|
+
enter: (node) => {
|
|
55
|
+
if (node.name !== 'FencedCode') {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const info = node.node.getChild('CodeInfo');
|
|
59
|
+
const text = node.node.getChild('CodeText');
|
|
60
|
+
if (text && !SKIPPED_FENCES.has(info ? fenceLanguage(view.state, info.from, info.to) : '')) {
|
|
61
|
+
highlightTree(
|
|
62
|
+
tree,
|
|
63
|
+
highlighter,
|
|
64
|
+
(start, end, cls) => builder.add(start, end, Decoration.mark({ class: cls })),
|
|
65
|
+
Math.max(text.from, from),
|
|
66
|
+
Math.min(text.to, to),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return builder.finish();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Colours the fenced code of other languages (`ts`, `sql`, …) in a spec, whose tokens the markdown
|
|
78
|
+
* highlight style leaves uncoloured.
|
|
79
|
+
*/
|
|
80
|
+
export const codeFenceHighlight: Extension = [
|
|
81
|
+
theme,
|
|
82
|
+
ViewPlugin.fromClass(
|
|
83
|
+
class {
|
|
84
|
+
decorations: DecorationSet;
|
|
85
|
+
|
|
86
|
+
constructor(view: EditorView) {
|
|
87
|
+
this.decorations = buildDecorations(view);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
update(update: ViewUpdate) {
|
|
91
|
+
// A fence language loads after the first parse, which changes the tree without changing the doc.
|
|
92
|
+
if (update.docChanged || update.viewportChanged || syntaxTree(update.startState) !== syntaxTree(update.state)) {
|
|
93
|
+
this.decorations = buildDecorations(update.view);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{ decorations: (plugin) => plugin.decorations },
|
|
98
|
+
),
|
|
99
|
+
];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type CompletionContext, type CompletionResult, autocompletion } from '@codemirror/autocomplete';
|
|
6
|
+
import { type Extension } from '@codemirror/state';
|
|
7
|
+
|
|
8
|
+
import { fieldCompletionContext } from './completion.ts';
|
|
9
|
+
|
|
10
|
+
/** Field-name completions inside a Spec fenced block, from {@link fieldCompletionContext}. */
|
|
11
|
+
const mdlCompletionSource = (context: CompletionContext): CompletionResult | null => {
|
|
12
|
+
const found = fieldCompletionContext(context.state.doc.toString(), context.pos);
|
|
13
|
+
if (!found) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const word = context.matchBefore(/\w[\w-]*/);
|
|
17
|
+
if (!word && !context.explicit) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
from: word ? word.from : context.pos,
|
|
23
|
+
options: found.fields.map((field) => ({
|
|
24
|
+
label: field,
|
|
25
|
+
type: 'property',
|
|
26
|
+
apply: `${field}: `,
|
|
27
|
+
})),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Autocompletion extension for Spec .mdl documents.
|
|
33
|
+
*/
|
|
34
|
+
export const mdlComplete: Extension = autocompletion({ override: [mdlCompletionSource] });
|