@hydranium/cli 1.0.0-next.304 → 1.0.0-next.305
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/lib/commands/init-templates.d.ts.map +1 -1
- package/lib/commands/init-templates.js +80 -484
- package/lib/commands/init-templates.js.map +1 -1
- package/package.json +12 -11
- package/src/commands/init-templates.ts +84 -499
- package/templates/server/gitignore +6 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/create-node-operation-handler.ts +61 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/gmodel-factory.ts +57 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/state.ts +18 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/storage.ts +16 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/submission-handler.ts +24 -0
- package/templates/server/src/glsp/__GRAMMAR_ID__/types.ts +26 -0
- package/templates/server/src/grammar/common.langium +12 -0
- package/templates/server/src/language-server/__GRAMMAR_ID__-serializer.ts +53 -0
- package/templates/server/src/language-server/ast.ts +23 -0
- package/templates/server/src/services.ts +12 -0
- package/templates/server/test/__GRAMMAR_ID__-diagram.test.ts +68 -0
- package/templates/server/test/tsconfig.json +4 -0
- package/templates/server/tsconfig.test.json +10 -0
- package/templates/server/vitest.config.ts +14 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The starter operation handler for the __GRAMMAR__ diagram: the tool-palette
|
|
2
|
+
// entry that creates a `__NODE_RULE__`.
|
|
3
|
+
//
|
|
4
|
+
// **Deleting this file and its `configureOperationHandlers` registration in
|
|
5
|
+
// `diagram-module.ts` gives a read-only viewer**, and nothing else has to change
|
|
6
|
+
// — creation is offered through the palette rather than through a type hint, so
|
|
7
|
+
// every hint in `diagram-configuration.ts` is already `false`. The scaffold emits
|
|
8
|
+
// the editable direction because that asymmetry runs one way: editable →
|
|
9
|
+
// read-only is a deletion the compiler checks, while read-only → editable is
|
|
10
|
+
// authoring against a seam you have not used yet.
|
|
11
|
+
//
|
|
12
|
+
// **It edits the operation's working copy.** A handler runs inside an operation,
|
|
13
|
+
// where `sourceRoot` is a copy of the built root: the recording command's
|
|
14
|
+
// runnable appends to the copy, and the operation writes the copy's text through
|
|
15
|
+
// the grammar's `Serializer` once, gated on the version the copy was made at,
|
|
16
|
+
// with undo and redo applying that one change. A command that writes the
|
|
17
|
+
// document itself instead reads no text off the copy, which has no `$document`,
|
|
18
|
+
// and the operation's own write then overwrites it.
|
|
19
|
+
//
|
|
20
|
+
// **The drop location is discarded.** `needsClientLayout` is `true` and the
|
|
21
|
+
// starter grammar persists no bounds, so there is nowhere to put a coordinate: the
|
|
22
|
+
// node is appended at the end of the document and the client places it.
|
|
23
|
+
//
|
|
24
|
+
// Like `types.ts`, `gmodel-factory.ts` and `diagram-configuration.ts`, this file
|
|
25
|
+
// knows the starter grammar's shape — the `nodes` list and the `__NODE_RULE__`
|
|
26
|
+
// it holds. Replacing the grammar means replacing these four together.
|
|
27
|
+
|
|
28
|
+
import { type Command, type CreateNodeOperation, JsonCreateNodeOperationHandler, type MaybePromise } from '@eclipse-glsp/server';
|
|
29
|
+
import { appendChild } from '@hydranium/core';
|
|
30
|
+
import { HydraniumGlspRecordingCommand } from '@hydranium/glsp-server';
|
|
31
|
+
import { findNextUnique } from '@hydranium/protocol';
|
|
32
|
+
import { injectable } from 'inversify';
|
|
33
|
+
import type { __NODE_RULE__ } from '../../language-server/ast.js';
|
|
34
|
+
import { __GRAMMAR_ID_CONST___NODE_TYPE } from './types.js';
|
|
35
|
+
import type { __GRAMMAR__GlspState } from './state.js';
|
|
36
|
+
|
|
37
|
+
/** Proposed name for a new node, uniquified against the ones the document already has. */
|
|
38
|
+
const NODE_NAME_STEM = 'Node';
|
|
39
|
+
|
|
40
|
+
@injectable()
|
|
41
|
+
export class __GRAMMAR__CreateNodeOperationHandler extends JsonCreateNodeOperationHandler {
|
|
42
|
+
declare protected modelState: __GRAMMAR__GlspState;
|
|
43
|
+
|
|
44
|
+
/** The palette's word for the thing it creates, so a noun rather than an action. */
|
|
45
|
+
override readonly label = '__NODE_RULE__';
|
|
46
|
+
elementTypeIds = [__GRAMMAR_ID_CONST___NODE_TYPE];
|
|
47
|
+
|
|
48
|
+
override createCommand(operation: CreateNodeOperation): MaybePromise<Command | undefined> {
|
|
49
|
+
if (!this.elementTypeIds.includes(operation.elementTypeId)) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
return new HydraniumGlspRecordingCommand(this.modelState, this.label, () => {
|
|
53
|
+
const root = this.modelState.sourceRoot;
|
|
54
|
+
const name = findNextUnique(
|
|
55
|
+
NODE_NAME_STEM,
|
|
56
|
+
root.nodes.map(node => node.name)
|
|
57
|
+
);
|
|
58
|
+
appendChild(root, 'nodes', root.nodes, { $type: '__NODE_RULE__', name } as __NODE_RULE__);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// AST → GModel for the __GRAMMAR__ diagram: one node per `__NODE_RULE__`, one
|
|
2
|
+
// edge per resolved `target` reference.
|
|
3
|
+
//
|
|
4
|
+
// Ids come from the index rather than being composed here, so the id strategy
|
|
5
|
+
// stays in one place. Nodes are emitted before edges, because a GLSP edge
|
|
6
|
+
// pointing at an id that does not exist fails client-side with a far less
|
|
7
|
+
// obvious error than a missing edge. An unresolved reference is SKIPPED rather
|
|
8
|
+
// than treated as an error: a dangling reference is ordinary editing state, and
|
|
9
|
+
// the LSP head already reports it as a diagnostic.
|
|
10
|
+
|
|
11
|
+
import { DefaultTypes, GEdge, GGraph, GLabel, type GModelFactory, GNode, ModelState } from '@eclipse-glsp/server';
|
|
12
|
+
import { inject, injectable } from 'inversify';
|
|
13
|
+
import type { __ENTRY_RULE__ } from '../../language-server/ast.js';
|
|
14
|
+
import { __GRAMMAR_ID_CONST___EDGE_TYPE, __GRAMMAR_ID_CONST___NODE_TYPE } from './types.js';
|
|
15
|
+
import type { __GRAMMAR__GlspState } from './state.js';
|
|
16
|
+
|
|
17
|
+
@injectable()
|
|
18
|
+
export class __GRAMMAR__GModelFactory implements GModelFactory {
|
|
19
|
+
@inject(ModelState) protected readonly modelState!: __GRAMMAR__GlspState;
|
|
20
|
+
|
|
21
|
+
createModel(): void {
|
|
22
|
+
const root = this.modelState.sourceRoot;
|
|
23
|
+
const graph = GGraph.builder().id(this.modelState.sourceUri).build();
|
|
24
|
+
if (root) {
|
|
25
|
+
this.buildGraph(root, graph);
|
|
26
|
+
}
|
|
27
|
+
this.modelState.updateRoot(graph);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected buildGraph(root: __ENTRY_RULE__, graph: GGraph): void {
|
|
31
|
+
for (const node of root.nodes) {
|
|
32
|
+
const id = this.modelState.index.createId(node);
|
|
33
|
+
graph.children.push(
|
|
34
|
+
GNode.builder()
|
|
35
|
+
.id(id)
|
|
36
|
+
.type(__GRAMMAR_ID_CONST___NODE_TYPE)
|
|
37
|
+
.add(GLabel.builder().id(`${id}_name`).text(node.name).type(DefaultTypes.LABEL).build())
|
|
38
|
+
.build()
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
for (const node of root.nodes) {
|
|
42
|
+
const target = node.target?.ref;
|
|
43
|
+
if (!target) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const sourceId = this.modelState.index.createId(node);
|
|
47
|
+
graph.children.push(
|
|
48
|
+
GEdge.builder()
|
|
49
|
+
.id(`${sourceId}_target`)
|
|
50
|
+
.type(__GRAMMAR_ID_CONST___EDGE_TYPE)
|
|
51
|
+
.sourceId(sourceId)
|
|
52
|
+
.targetId(this.modelState.index.createId(target))
|
|
53
|
+
.build()
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// GLSP state for the __GRAMMAR__ diagram.
|
|
2
|
+
//
|
|
3
|
+
// `FullTextHydraniumGlspState` is the simplest of the framework source-model
|
|
4
|
+
// strategies (the others project a structured transfer model, over one document
|
|
5
|
+
// or several): the source model is the whole document text, serialised through
|
|
6
|
+
// the per-URI `Serializer` and round-tripped by re-parsing. Both seams resolve
|
|
7
|
+
// through shared services, so narrowing the root type is all an adopter adds.
|
|
8
|
+
//
|
|
9
|
+
// It cannot field-merge — every concurrent edit on a whole-document model is a
|
|
10
|
+
// same-document collision, so undo / redo degrade to drop-on-divergence. Move to
|
|
11
|
+
// `ReconcilingTransferHydraniumGlspState` when you need field-level undo.
|
|
12
|
+
|
|
13
|
+
import { FullTextHydraniumGlspState } from '@hydranium/glsp-server';
|
|
14
|
+
import { injectable } from 'inversify';
|
|
15
|
+
import type { __ENTRY_RULE__ } from '../../language-server/ast.js';
|
|
16
|
+
|
|
17
|
+
@injectable()
|
|
18
|
+
export class __GRAMMAR__GlspState extends FullTextHydraniumGlspState<__ENTRY_RULE__> {}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Source-model storage for the __GRAMMAR__ diagram, inheriting both framework
|
|
2
|
+
// defaults: `loadSourceModel` (open + settle + `setSourceRoot`) and
|
|
3
|
+
// `saveSourceModel` (flush the store's text for the primary and every tracked
|
|
4
|
+
// secondary through the diagram's client session).
|
|
5
|
+
//
|
|
6
|
+
// No serializer runs on the save path — a save persists what the diagram's
|
|
7
|
+
// operations already wrote to the store.
|
|
8
|
+
//
|
|
9
|
+
// The subclass exists so a bespoke load or save has a stable place to land.
|
|
10
|
+
|
|
11
|
+
import { type FullTextSourceModel, HydraniumGlspStorage } from '@hydranium/glsp-server';
|
|
12
|
+
import { injectable } from 'inversify';
|
|
13
|
+
import type { __ENTRY_RULE__ } from '../../language-server/ast.js';
|
|
14
|
+
|
|
15
|
+
@injectable()
|
|
16
|
+
export class __GRAMMAR__GlspStorage extends HydraniumGlspStorage<__ENTRY_RULE__, FullTextSourceModel> {}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Submission handler for the __GRAMMAR__ diagram.
|
|
2
|
+
//
|
|
3
|
+
// Inherits the framework's `readyEvent = IntegrityService.SettledState`, which
|
|
4
|
+
// is load-bearing rather than incidental: the GModel factory resolves
|
|
5
|
+
// `target.ref`, so it needs a fully-linked AST. Without the gate those reads
|
|
6
|
+
// fire mid-build and warn about resolution before scopes are computed.
|
|
7
|
+
//
|
|
8
|
+
// Only `formatSourceRoot` is overridden, so the submit log names the model and
|
|
9
|
+
// its node count instead of a bare `$type`.
|
|
10
|
+
|
|
11
|
+
import { ModelState } from '@eclipse-glsp/server';
|
|
12
|
+
import { type FullTextSourceModel, HydraniumGlspSubmissionHandler } from '@hydranium/glsp-server';
|
|
13
|
+
import { inject, injectable } from 'inversify';
|
|
14
|
+
import type { __ENTRY_RULE__ } from '../../language-server/ast.js';
|
|
15
|
+
import type { __GRAMMAR__GlspState } from './state.js';
|
|
16
|
+
|
|
17
|
+
@injectable()
|
|
18
|
+
export class __GRAMMAR__SubmissionHandler extends HydraniumGlspSubmissionHandler<__ENTRY_RULE__, FullTextSourceModel> {
|
|
19
|
+
@inject(ModelState) declare protected modelState: __GRAMMAR__GlspState;
|
|
20
|
+
|
|
21
|
+
protected override formatSourceRoot(root: __ENTRY_RULE__ | undefined): string {
|
|
22
|
+
return root ? `__ENTRY_RULE__ nodes=${root.nodes.length}` : 'none';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// GLSP diagram-type and element-type ids for the __GRAMMAR__ diagram.
|
|
2
|
+
//
|
|
3
|
+
// **Authoritative half of a client/server contract.** Every id here has to be
|
|
4
|
+
// registered on the client too: sprotty's registries are exact-key maps with no
|
|
5
|
+
// prefix fallback, so an id the client does not know yields an element with none
|
|
6
|
+
// of a node's features, rendered by `MissingView` with only a
|
|
7
|
+
// `no registered view for type '…'` console warning. Nothing fails server-side.
|
|
8
|
+
//
|
|
9
|
+
// The ids stay namespaced under the GLSP defaults (`node:` / `edge:`) so a
|
|
10
|
+
// reader can tell a node id from an edge id at a glance.
|
|
11
|
+
|
|
12
|
+
import { DefaultTypes } from '@eclipse-glsp/server';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The GLSP diagram type — the string GLSP routes every per-diagram-type request
|
|
16
|
+
* by. Mirrored by the client; a mismatch silently DROPS the request rather than
|
|
17
|
+
* reporting an unknown diagram type. Same value as the language id, so the two
|
|
18
|
+
* cannot drift apart as a diagram gains element types.
|
|
19
|
+
*/
|
|
20
|
+
export const __GRAMMAR_ID_CONST___DIAGRAM_TYPE = '__LANGUAGE_ID__';
|
|
21
|
+
|
|
22
|
+
/** A `__NODE_RULE__` — the starter grammar's one named, referenceable node. */
|
|
23
|
+
export const __GRAMMAR_ID_CONST___NODE_TYPE = `${DefaultTypes.NODE}:__GRAMMAR_ID__-node`;
|
|
24
|
+
|
|
25
|
+
/** A resolved `target` reference, drawn as a connection between two nodes. */
|
|
26
|
+
export const __GRAMMAR_ID_CONST___EDGE_TYPE = `${DefaultTypes.EDGE}:__GRAMMAR_ID__-target`;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Shared lexical basis for this project's grammars. Imported, never registered:
|
|
2
|
+
// it has no entry rule and no `langium-config.json` entry, so it is a fragment
|
|
3
|
+
// rather than a language.
|
|
4
|
+
//
|
|
5
|
+
// Keeping the tokens here rather than in one of the languages means a grammar
|
|
6
|
+
// imports `common` for its TOKENS and another language only when it needs that
|
|
7
|
+
// language's TYPES — separate reasons that are worth keeping separate.
|
|
8
|
+
|
|
9
|
+
hidden terminal WS: /\s+/;
|
|
10
|
+
terminal ID: /[_a-zA-Z][\w_]*/;
|
|
11
|
+
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;
|
|
12
|
+
hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Concrete-syntax emitter for __GRAMMAR__ — the parser's inverse, turning a
|
|
2
|
+
// model back into text this grammar accepts. Derived from the starter grammar
|
|
3
|
+
// exactly as `generated/ast.ts` is, so replacing the grammar replaces this too.
|
|
4
|
+
//
|
|
5
|
+
// Without it every structured write fails: the framework's default binding at
|
|
6
|
+
// `services.serializer.Serializer` THROWS, because a concrete syntax is
|
|
7
|
+
// language knowledge no framework can derive. A client session's `update` /
|
|
8
|
+
// `save`, the data head's `saveModelDocument` and a GLSP `SaveModelAction`
|
|
9
|
+
// all reach it.
|
|
10
|
+
//
|
|
11
|
+
// `AbstractSerializer`'s generic property walk lays out FORMAT-structured
|
|
12
|
+
// output — its YAML and JSON subclasses are what it exists for — and cannot
|
|
13
|
+
// produce a keyword-delimited syntax like `node a -> b`. So `serializeNode` is
|
|
14
|
+
// a per-`$type` emitter here and the two array hooks are unreachable.
|
|
15
|
+
|
|
16
|
+
import { AbstractSerializer } from '@hydranium/core';
|
|
17
|
+
import type { AstNode } from '@hydranium/langium';
|
|
18
|
+
import { type __ENTRY_RULE__, type __NODE_RULE__, is__ENTRY_RULE__, is__NODE_RULE__ } from './ast.js';
|
|
19
|
+
|
|
20
|
+
export class __GRAMMAR__Serializer extends AbstractSerializer<__ENTRY_RULE__> {
|
|
21
|
+
protected override serializeNode(node: AstNode | Record<string, unknown>): string {
|
|
22
|
+
if (is__ENTRY_RULE__(node)) {
|
|
23
|
+
return node.nodes.map(child => this.emitNode(child)).join('\n');
|
|
24
|
+
}
|
|
25
|
+
if (is__NODE_RULE__(node)) {
|
|
26
|
+
return this.emitNode(node);
|
|
27
|
+
}
|
|
28
|
+
// Defensive: a rule added to the grammar with no emitter added here.
|
|
29
|
+
throw new Error(`__GRAMMAR__Serializer: no emitter for $type ${(node as AstNode).$type}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Unreachable — this grammar's one list is emitted by its `__ENTRY_RULE__` parent. */
|
|
33
|
+
protected override serializeArray(): string {
|
|
34
|
+
throw new Error('__GRAMMAR__Serializer: arrays are emitted by the per-$type parent, not the generic dispatch.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Unreachable — same reasoning as {@link serializeArray}. */
|
|
38
|
+
protected override serializeReferenceArray(): string {
|
|
39
|
+
throw new Error('__GRAMMAR__Serializer: reference arrays are emitted by the per-$type parent, not the generic dispatch.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `serializeReferenceText` rather than `node.target?.$refText`: it is the one
|
|
44
|
+
* read that spans BOTH input shapes. A transfer model reaching
|
|
45
|
+
* `serializeTransfer` carries `target` as a plain string rather than a
|
|
46
|
+
* `Reference`, and a serializer that reaches for `$refText` directly emits
|
|
47
|
+
* the AST correctly and drops every reference on the transfer path.
|
|
48
|
+
*/
|
|
49
|
+
private emitNode(node: __NODE_RULE__): string {
|
|
50
|
+
const target = this.serializeReferenceText(node.target);
|
|
51
|
+
return target === undefined ? `node ${node.name}` : `node ${node.name} -> ${target}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The language's AST entry point: a re-export of what `langium generate`
|
|
2
|
+
// emits, plus anywhere you augment those types.
|
|
3
|
+
//
|
|
4
|
+
// Import the AST from HERE rather than from `./generated/ast.js`, so any
|
|
5
|
+
// augmentation below travels with every import. One `langium-cli` run over N
|
|
6
|
+
// grammars emits ONE combined AST module sharing one reflection, so a further
|
|
7
|
+
// grammar needs no change here.
|
|
8
|
+
//
|
|
9
|
+
// `generate:transfer-model` reads this file as its `--augmentation-file`: the
|
|
10
|
+
// generated wire types are derived from the AST *as augmented*, not from the
|
|
11
|
+
// raw generated module. Augment a type here and the transfer model follows.
|
|
12
|
+
//
|
|
13
|
+
// A `@derived` property is computed at build time rather than parsed, so it is
|
|
14
|
+
// declared here and populated by an AST-extension contribution:
|
|
15
|
+
//
|
|
16
|
+
// declare module './generated/ast.js' {
|
|
17
|
+
// interface __ENTRY_RULE__ {
|
|
18
|
+
// /** @derived Populated by an `ast.extensions.computedProperties` contribution. */
|
|
19
|
+
// readonly _summary?: string;
|
|
20
|
+
// }
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
export * from './generated/ast.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Zero-arg service factory for the headless `hydranium-cli` tooling: the
|
|
2
|
+
// `reflect` / `lint-grammar` / `validate` subcommands import this via
|
|
3
|
+
// `--services ./lib/services.js`. The head wires its own filesystem here.
|
|
4
|
+
//
|
|
5
|
+
// The contract is language-count-agnostic — a second grammar needs no edit here.
|
|
6
|
+
|
|
7
|
+
import { NodeFileSystem } from '@hydranium/core/node';
|
|
8
|
+
import { create__NAME__Services } from './language-server/__PROJECT_ID__-module.js';
|
|
9
|
+
|
|
10
|
+
export function createServices(): ReturnType<typeof create__NAME__Services> {
|
|
11
|
+
return create__NAME__Services({ ...NodeFileSystem });
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The starter create-node handler, run the way a client runs it: an in-process
|
|
2
|
+
// GLSP server opens a document, a create-node operation adds a node, and an undo
|
|
3
|
+
// and a redo take it out and put it back. Every step must keep the nodes the
|
|
4
|
+
// document already had, and its comments: the diagram writes the document from
|
|
5
|
+
// its AST, which carries none.
|
|
6
|
+
|
|
7
|
+
import 'reflect-metadata';
|
|
8
|
+
import { type Action, CreateNodeOperation, RedoAction, ServerModule, SetDirtyStateAction, UndoAction } from '@eclipse-glsp/server';
|
|
9
|
+
import { initializeWorkspaceProgrammatically } from '@hydranium/core';
|
|
10
|
+
import { makeScratchWorkspace, type ScratchWorkspace } from '@hydranium/core/testing/node';
|
|
11
|
+
import { HydraniumGlspAppModule } from '@hydranium/glsp-server';
|
|
12
|
+
import { type GlspHarness, makeGlspHarness } from '@hydranium/glsp-server/testing';
|
|
13
|
+
import { waitFor } from '@hydranium/protocol/testing';
|
|
14
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
15
|
+
import { __GRAMMAR__DiagramModule } from '../src/glsp/__GRAMMAR_ID__/diagram-module.js';
|
|
16
|
+
import type { __GRAMMAR__GlspState } from '../src/glsp/__GRAMMAR_ID__/state.js';
|
|
17
|
+
import { __GRAMMAR_ID_CONST___DIAGRAM_TYPE, __GRAMMAR_ID_CONST___NODE_TYPE } from '../src/glsp/__GRAMMAR_ID__/types.js';
|
|
18
|
+
import { createServices } from '../src/services.js';
|
|
19
|
+
|
|
20
|
+
let workspace: ScratchWorkspace | undefined;
|
|
21
|
+
let diagram: GlspHarness<__GRAMMAR__GlspState> | undefined;
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
diagram?.dispose();
|
|
25
|
+
workspace?.dispose();
|
|
26
|
+
diagram = undefined;
|
|
27
|
+
workspace = undefined;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** Send `action` and wait for the dirty state the server answers it with, which it sends once the edit is written. */
|
|
31
|
+
async function send(harness: GlspHarness<__GRAMMAR__GlspState>, action: Action, reason: string): Promise<void> {
|
|
32
|
+
const before = harness.actions.length;
|
|
33
|
+
harness.dispatch(action);
|
|
34
|
+
await waitFor(() => harness.actions.slice(before).some(sent => SetDirtyStateAction.is(sent) && sent.reason === reason), {
|
|
35
|
+
message: `no '${reason}' dirty state`
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('__GRAMMAR__ diagram', () => {
|
|
40
|
+
it('creates a node after the existing ones, keeping their comments, and undoes and redoes it', async () => {
|
|
41
|
+
workspace = makeScratchWorkspace({ prefix: '__GRAMMAR_ID__-diagram-' });
|
|
42
|
+
const original = '// keep this comment\nnode first -> second\n// about second\nnode second';
|
|
43
|
+
const file = workspace.write('model.__EXTENSION__', original);
|
|
44
|
+
const { shared } = createServices();
|
|
45
|
+
await initializeWorkspaceProgrammatically(shared, workspace.root);
|
|
46
|
+
diagram = makeGlspHarness<__GRAMMAR__GlspState>({
|
|
47
|
+
serverModule: new ServerModule().configureDiagramModule(new __GRAMMAR__DiagramModule()),
|
|
48
|
+
diagramType: __GRAMMAR_ID_CONST___DIAGRAM_TYPE,
|
|
49
|
+
appModules: [new HydraniumGlspAppModule({ shared })]
|
|
50
|
+
});
|
|
51
|
+
await diagram.start();
|
|
52
|
+
await diagram.openDocument(file);
|
|
53
|
+
const uri = workspace.uri('model.__EXTENSION__');
|
|
54
|
+
const text = (): string | undefined => shared.workspace.TextDocuments.get(uri)?.getText();
|
|
55
|
+
|
|
56
|
+
await send(diagram, CreateNodeOperation.create(__GRAMMAR_ID_CONST___NODE_TYPE), 'operation');
|
|
57
|
+
const created = text();
|
|
58
|
+
await send(diagram, UndoAction.create(), 'undo');
|
|
59
|
+
const undone = text();
|
|
60
|
+
await send(diagram, RedoAction.create(), 'redo');
|
|
61
|
+
|
|
62
|
+
expect({ created, undone, redone: text() }).toEqual({
|
|
63
|
+
created: `${original}\nnode Node`,
|
|
64
|
+
undone: original,
|
|
65
|
+
redone: `${original}\nnode Node`
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
// Vitest transforms TypeScript itself and resolves `.js` specifiers to their
|
|
4
|
+
// `.ts` source, so tests import from `../src/` with the same specifiers the
|
|
5
|
+
// compiled output uses. `include` is scoped to `test/` so `lib/` is never
|
|
6
|
+
// scanned; `npm test` runs `typecheck:test` first, because vitest's
|
|
7
|
+
// transform strips types without checking them.
|
|
8
|
+
|
|
9
|
+
export default defineConfig({
|
|
10
|
+
test: {
|
|
11
|
+
environment: 'node',
|
|
12
|
+
include: ['test/**/*.{test,spec}.ts']
|
|
13
|
+
}
|
|
14
|
+
});
|