Skip to content

Commit 5a174a8

Browse files
authored
Merge pull request #210 from mathworks/model/set-active-config
Set as Active Configuration in the model editor
2 parents be8d62f + 57b43c6 commit 5a174a8

13 files changed

Lines changed: 199 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to the Simulink Data Explorer extension.
55
## Unreleased
66

77
- You can now edit the Model Workspace of a Simulink model (.slx), and save it.
8+
- You can now choose which configuration of a Simulink model (.slx) is active.
89

910
## [1.54.1] — 2026-11-02
1011

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ A textual Simulink data dictionary (SLDD) file is backed by a JSON text document
7575
| Files | Formats | Support |
7676
| --- | --- | --- |
7777
| Simulink data dictionaries | `.sldd` JSON and compressed-binary | Read and write |
78-
| Simulink models | `.slx`, `.mdl` | Read only, but the Model Workspace of an `.slx`: read and write |
78+
| Simulink models | `.slx`, `.mdl` | Read only, but the Model Workspace and the active configuration of an `.slx`: read and write |
7979
| MAT-files | `.mat` Level 5 (`-v6`/`-v7`) | Read and write |
8080
| MATLAB Projects | `.prj`, `matlab.toml` | Read only |
8181

8282
## Limitations
8383

8484
- Requires Visual Studio Code 1.106.0 or later.
8585
- Editing supports Simulink data dictionary (SLDD) files, MAT-files and the Model Workspace of an SLX model. The rest of a model, MDL models and project files are read only. A model workspace loaded from a MAT-file or made by MATLAB code is read only; edit its source instead.
86+
- To make another configuration of an SLX model the active one, right-click it in the Configurations section and choose **Set as Active Configuration**. A configuration reference can be made active too: without MATLAB the extension cannot check that the configuration it refers to will be found, so it warns you to confirm in MATLAB.
8687
- `.mat` support covers the Level 5 format only. **`-v7.3`** files, which are HDF5 and are required by MATLAB for variables above 2 GB, are not supported.
8788
- Large textual (JSON) `.sldd` files are limited by size. Above **50 MB**, the file opens as a read only table. Above **512 MB**, the file opens in Visual Studio Code's text editor instead.
8889
- Paste creates a new top level entry in the target section. Pasting as a child of a structure or bus is not supported.

‎package-lock.json‎

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@
251251
},
252252
"dependencies": {
253253
"@lit/context": "^1.1.6",
254-
"data-explorer-core": "github:mathworks/data-explorer-core#v1.55.0",
255-
"data-explorer-view": "github:mathworks/data-explorer-view#v1.4.0",
254+
"data-explorer-core": "github:mathworks/data-explorer-core#v1.56.0",
255+
"data-explorer-view": "github:mathworks/data-explorer-view#v1.5.0",
256256
"fast-xml-parser": "^5.8.0",
257257
"fflate": "^0.8.3",
258258
"highlight.js": "^11.11.1",

‎src/common/protocol.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ export interface RequestEditTextMessage {
373373
rowId: string;
374374
}
375375

376+
/**
377+
* Set as Active Configuration, chosen on a model's configuration set: make it the active one. The
378+
* model editor's engine marks the sets that take it (`_canSetActive`), and answers it.
379+
*/
380+
export interface SetActiveConfigSetMessage {
381+
type: 'setActiveConfigSet';
382+
rowId: string;
383+
}
384+
376385
/** Every message the host receives from the table webview. */
377386
export type TableToHostMessage =
378387
| ReadyMessage
@@ -392,7 +401,8 @@ export type TableToHostMessage =
392401
| DropMessage
393402
| RequestMatrixMessage
394403
| RequestChildrenMessage
395-
| RequestEditTextMessage;
404+
| RequestEditTextMessage
405+
| SetActiveConfigSetMessage;
396406

397407
/**
398408
* A link on the project page was activated: open what it names.

‎src/host/MatEditorProvider.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ export class MatEditorProvider implements vscode.CustomEditorProvider<MatDocumen
183183
else if (msg?.type === 'dragStart') table.dragStart?.(msg.rowIds);
184184
else if (msg?.type === 'dragEnd') table.dragEnd?.();
185185
else if (msg?.type === 'drop') table.drop?.(msg.rowId, msg.mode);
186+
// Set as Active Configuration, which only a model's configuration sets offer.
187+
else if (msg?.type === 'setActiveConfigSet') table.setActiveConfigSet?.(msg.rowId);
186188
};
187189

188190
// A message that throws is said, as a gesture's throw is, over every row painted again from
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- Copyright 2026 The MathWorks, Inc. -->
2+
3+
# Active configuration fixtures
4+
5+
Copied from data-explorer-core's `test/fixtures/active-config`, where
6+
`test/parity/matlab/gen_active_config.m` writes them (R2027a), for
7+
`test-integration/suite/modelActiveConfig.test.ts`:
8+
9+
| File | What it holds |
10+
|---|---|
11+
| `acs_sets.slx` | three configuration sets, Configuration (active), Fast and Slow |
12+
| `acs_ref.slx` | Configuration (active) and RefFromDict, a `Simulink.ConfigSetRef` into `acs_dict.sldd` (not copied: the editor does not look for it) |
10.9 KB
Binary file not shown.
18.7 KB
Binary file not shown.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2026 The MathWorks, Inc.
2+
//
3+
// Set as Active Configuration in the model editor (MatEditorProvider's MODEL_EDITOR): the message
4+
// the table's menu sends (setActiveConfigSet) reaches the model's engine, makes the set active as
5+
// one edit on VS Code's stack, undone and redone with it, and a save writes it into the file, every
6+
// other part as read. A reference to a set is made active too, with VS Code's warning of what that
7+
// cannot check without MATLAB (read off `vscode.window.showWarningMessage`, wrapped for this suite:
8+
// the extension's `vscode` module is one object for every file of it). Driven through the provider
9+
// with a fake panel, as the MAT-file editor's suite is (matEdit.test.ts).
10+
import * as assert from 'assert';
11+
import * as vscode from 'vscode';
12+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
13+
import { join } from 'path';
14+
import { tmpdir } from 'os';
15+
import { unzipSync } from 'fflate';
16+
import { parseSlx } from 'data-explorer-core';
17+
import { MatEditorProvider, MODEL_EDITOR } from '../../src/host/MatEditorProvider';
18+
import { ctx, fakePanel, nameOf, quiet, token, waitFor } from './tools/hostHarness';
19+
20+
/* eslint-disable @typescript-eslint/no-explicit-any */
21+
22+
const fixture = (name: string): Uint8Array =>
23+
new Uint8Array(readFileSync(join(ctx().extensionUri.fsPath, 'test-integration', 'fixtures', 'active-config', name)));
24+
const buffer = (b: Uint8Array) => b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength) as ArrayBuffer;
25+
const activeIn = (b: Uint8Array) => parseSlx(buffer(b), 'saved.slx').configSets.filter((c: any) => c.active).map((c: any) => c.name);
26+
const same = (a: Uint8Array, b: Uint8Array) => a.byteLength === b.byteLength && a.every((x, i) => x === b[i]);
27+
28+
/** The rows the table holds now: the last setRows with every updateEntryRows after it spliced in. */
29+
function tableOf(posts: any[]): any[] {
30+
let rows: any[] = [];
31+
for (const p of posts) {
32+
if (p.type === 'setRows') rows = p.rows;
33+
else if (p.type === 'updateEntryRows') {
34+
const start = rows.findIndex((r) => r.ID === p.entryRowId);
35+
if (start < 0) continue;
36+
const subtree = new Set([p.entryRowId]);
37+
let end = start + 1;
38+
while (end < rows.length && subtree.has(rows[end].parent)) subtree.add(rows[end++].ID);
39+
rows = [...rows.slice(0, start), ...p.rows, ...rows.slice(end)];
40+
}
41+
}
42+
return rows;
43+
}
44+
45+
const setRow = (posts: any[], name: string): any => {
46+
const row = tableOf(posts).find((r) => r.parent === 'section:config' && nameOf(r) === name);
47+
assert.ok(row, `the Configurations section has a row for "${name}"`);
48+
return row;
49+
};
50+
51+
suite('Model editor: Set as Active Configuration', () => {
52+
let dir: string;
53+
let provider: MatEditorProvider;
54+
const opened: { doc: any; view: ReturnType<typeof fakePanel> }[] = [];
55+
const warned: string[] = [];
56+
const window = vscode.window as any;
57+
const warn = window.showWarningMessage;
58+
59+
setup(() => {
60+
dir = mkdtempSync(join(tmpdir(), 'dex-activeconfig-'));
61+
provider = new MatEditorProvider(ctx(), MODEL_EDITOR);
62+
warned.length = 0;
63+
window.showWarningMessage = (message: string) => {
64+
warned.push(message);
65+
return Promise.resolve(undefined);
66+
};
67+
});
68+
69+
teardown(async () => {
70+
window.showWarningMessage = warn;
71+
for (const { doc, view } of opened.splice(0)) {
72+
view.close();
73+
doc.dispose();
74+
}
75+
await quiet(200, 'the disposed watchers to stop watching before their directory goes');
76+
rmSync(dir, { recursive: true, force: true });
77+
});
78+
79+
async function open(name: string) {
80+
const path = join(dir, name);
81+
const original = fixture(name);
82+
writeFileSync(path, original);
83+
const doc: any = await provider.openCustomDocument(vscode.Uri.file(path), {} as vscode.CustomDocumentOpenContext, token());
84+
const view = fakePanel();
85+
opened.push({ doc, view });
86+
const events: vscode.CustomDocumentEditEvent<vscode.CustomDocument>[] = [];
87+
provider.onDidChangeCustomDocument((e) => {
88+
if (e.document === doc && 'undo' in e) events.push(e);
89+
});
90+
await provider.resolveCustomEditor(doc, view.panel, token());
91+
view.send({ type: 'ready' });
92+
await waitFor(`${name}'s first rows`, () => view.posts.some((p) => p.type === 'setRows' || p.type === 'error'));
93+
return { path, doc, view, events, original };
94+
}
95+
96+
const painted = (o: { view: any }, name: string) =>
97+
waitFor(`${name} painted active`, () => setRow(o.view.posts, name).Name.iconId === 'check_settings');
98+
99+
test('a set made active is one edit, undone and redone with VS Code’s stack, and saved', async () => {
100+
const o = await open('acs_sets.slx');
101+
assert.strictEqual(setRow(o.view.posts, 'Configuration')._canSetActive, false);
102+
assert.strictEqual(setRow(o.view.posts, 'Fast')._canSetActive, true);
103+
o.view.send({ type: 'setActiveConfigSet', rowId: setRow(o.view.posts, 'Fast').ID });
104+
await painted(o, 'Fast');
105+
assert.strictEqual(o.events.length, 1, 'one edit on the stack');
106+
assert.strictEqual(o.doc.edits.dirty, true);
107+
await o.events[0].undo();
108+
await painted(o, 'Configuration');
109+
assert.strictEqual(o.doc.edits.dirty, false, 'undone: clean');
110+
await o.events[0].redo();
111+
await painted(o, 'Fast');
112+
await provider.saveCustomDocument(o.doc, token());
113+
const saved = new Uint8Array(readFileSync(o.path));
114+
assert.deepStrictEqual(activeIn(saved), ['Fast']);
115+
const read = unzipSync(o.original);
116+
const written = unzipSync(saved);
117+
const changed = Object.keys(read).filter((n) => !same(read[n], written[n]));
118+
assert.deepStrictEqual(changed, ['simulink/configSetInfo.json'], 'the mark alone changed');
119+
assert.strictEqual(o.doc.edits.dirty, false);
120+
});
121+
122+
test('a reference to a set is made active, with a warning that MATLAB alone can check it, and saved', async () => {
123+
const o = await open('acs_ref.slx');
124+
const ref = setRow(o.view.posts, 'RefFromDict');
125+
assert.strictEqual(ref._canSetActive, true);
126+
o.view.send({ type: 'setActiveConfigSet', rowId: ref.ID });
127+
await waitFor('the reference painted active', () => setRow(o.view.posts, 'RefFromDict').Name.iconId === 'check_configurationReference');
128+
assert.strictEqual(o.events.length, 1);
129+
assert.deepStrictEqual(warned, [
130+
'"RefFromDict" refers to the configuration set "dictCfg" in the data dictionary "acs_dict.sldd". Without MATLAB this editor ' +
131+
'cannot check that the set is there: open the model in MATLAB to confirm.',
132+
]);
133+
await provider.saveCustomDocument(o.doc, token());
134+
assert.deepStrictEqual(activeIn(new Uint8Array(readFileSync(o.path))), ['RefFromDict']);
135+
});
136+
});

0 commit comments

Comments
 (0)