Skip to content

Commit

Permalink
implement export registers feature (#33)
Browse files Browse the repository at this point in the history
* implement export registers feature

* implement export registers feature

* implement export registers feature

* implement export registers feature
  • Loading branch information
QuocTrung76 authored Dec 31, 2024
1 parent 5787e16 commit 89663e2
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 6 deletions.
48 changes: 46 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"vscode-messenger": "^0.4.5",
"vscode-messenger-common": "^0.4.5",
"vscode-messenger-webview": "^0.4.5",
"xml2js": "^0.4.23"
"xml2js": "^0.4.23",
"xmlbuilder2": "^3.0.0"
},
"devDependencies": {
"@types/node": "^12.20.0",
Expand Down Expand Up @@ -97,6 +98,11 @@
"title": "Update Value",
"icon": "$(edit)"
},
{
"command": "peripheral-inspector.svd.exportNode",
"title": "Export Register",
"icon": "$(desktop-download)"
},
{
"command": "peripheral-inspector.svd.copyValue",
"title": "Copy Value",
Expand Down Expand Up @@ -131,6 +137,11 @@
"command": "peripheral-inspector.svd.collapseAll",
"title": "Collapse All",
"icon": "$(collapse-all)"
},
{
"command": "peripheral-inspector.svd.exportAll",
"title": "Export All",
"icon": "$(desktop-download)"
}
],
"menus": {
Expand All @@ -143,6 +154,14 @@
"command": "peripheral-inspector.svd.copyValue",
"when": "false"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "false"
},
{
"command": "peripheral-inspector.svd.exportAll",
"when": "false"
},
{
"command": "peripheral-inspector.svd.forceRefresh",
"when": "false"
Expand Down Expand Up @@ -181,6 +200,26 @@
"command": "peripheral-inspector.svd.forceRefresh",
"when": "view == peripheral-inspector.svd && viewItem =~ /peripheral.*/"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "view == peripheral-inspector.svd && viewItem == cluster"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "view == peripheral-inspector.svd && viewItem == registerRW"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "view == peripheral-inspector.svd && viewItem == register"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "view == peripheral-inspector.svd && viewItem == registerRO"
},
{
"command": "peripheral-inspector.svd.exportNode",
"when": "view == peripheral-inspector.svd"
},
{
"command": "peripheral-inspector.svd.pin",
"when": "view == peripheral-inspector.svd && viewItem == peripheral"
Expand All @@ -200,6 +239,11 @@
"command": "peripheral-inspector.svd.collapseAll",
"when": "view =~ /peripheral-inspector.peripheral-*/ && debugState == stopped",
"group": "navigation"
},
{
"command": "peripheral-inspector.svd.exportAll",
"when": "view =~ /peripheral-inspector.peripheral-*/ && debugState == stopped",
"group": "navigation"
}
],
"webview/context": [
Expand Down Expand Up @@ -264,4 +308,4 @@
"workspace",
"ui"
]
}
}
23 changes: 23 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CTDTreeWebviewContext } from './components/tree/types';
import { Commands } from './manifest';
import { PeripheralBaseNode } from './plugin/peripheral/nodes';
import { PeripheralDataTracker } from './plugin/peripheral/tree/peripheral-data-tracker';
import { getFilePath } from './fileUtils';

export class PeripheralCommands {
public constructor(
Expand All @@ -22,13 +23,15 @@ export class PeripheralCommands {
public async activate(context: vscode.ExtensionContext): Promise<void> {
context.subscriptions.push(
vscode.commands.registerCommand(Commands.UPDATE_NODE_COMMAND.commandId, (node, value) => this.peripheralsUpdateNode(node, value)),
vscode.commands.registerCommand(Commands.EXPORT_NODE_COMMAND.commandId, node => this.peripheralsExportNode(node)),
vscode.commands.registerCommand(Commands.COPY_VALUE_COMMAND.commandId, (node, value) => this.peripheralsCopyValue(node, value)),
vscode.commands.registerCommand(Commands.SET_FORMAT_COMMAND.commandId, (node) => this.peripheralsSetFormat(node)),
vscode.commands.registerCommand(Commands.FORCE_REFRESH_COMMAND.commandId, (node) => this.peripheralsForceRefresh(node)),
vscode.commands.registerCommand(Commands.PIN_COMMAND.commandId, (node, _, context) => this.peripheralsTogglePin(node, context)),
vscode.commands.registerCommand(Commands.UNPIN_COMMAND.commandId, (node, _, context) => this.peripheralsTogglePin(node, context)),
vscode.commands.registerCommand(Commands.REFRESH_ALL_COMMAND.commandId, () => this.peripheralsForceRefresh()),
vscode.commands.registerCommand(Commands.COLLAPSE_ALL_COMMAND.commandId, () => this.collapseAll()),
vscode.commands.registerCommand(Commands.EXPORT_ALL_COMMAND.commandId, () => this.peripheralsExportAll()),
);
}

Expand All @@ -45,6 +48,26 @@ export class PeripheralCommands {
}
}

private async peripheralsExportNode(
node: PeripheralBaseNode,
): Promise<void> {
const filePath = await getFilePath();
if (!filePath) {
this.dataTracker.fireOnDidChange();
return;
}
this.dataTracker.exportNodeToXml(node, filePath);
}

private async peripheralsExportAll(): Promise<void> {
const filePath = await getFilePath();
if (!filePath) {
this.dataTracker.fireOnDidChange();
return;
}
this.dataTracker.exportAllNodesToXml(filePath);
}

private peripheralsCopyValue(_node: PeripheralBaseNode, value?: string): void {
if (value) {
vscode.env.clipboard.writeText(value);
Expand Down
2 changes: 2 additions & 0 deletions src/common/peripheral-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export namespace PeripheralBaseTreeNodeDTO {

export const PERIPHERAL_ID_SEP = '-';
export interface PeripheralBaseNodeDTO extends PeripheralBaseTreeNodeDTO {
name: string;
format: NumberFormat;
pinned?: boolean;
session: string | undefined;
Expand Down Expand Up @@ -159,6 +160,7 @@ export interface PeripheralRegisterNodeDTO extends ClusterOrRegisterBaseNodeDTO,
resetValue: number;
hexLength: number;
offset: number;
size: number;
address: number;
}
export namespace PeripheralRegisterNodeDTO {
Expand Down
15 changes: 12 additions & 3 deletions src/components/tree/integration/peripheral-tree-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class PeripheralNodeConverter implements TreeResourceConverter<Peripheral
},
'actions': {
type: 'action',
commands: [Commands.FORCE_REFRESH_COMMAND]
commands: [Commands.FORCE_REFRESH_COMMAND, Commands.EXPORT_NODE_COMMAND]
}
};
}
Expand Down Expand Up @@ -127,9 +127,9 @@ export class PeripheralRegisterNodeConverter implements TreeResourceConverter<Pe

switch (contextValue) {
case 'registerRO':
return [copyValue, Commands.FORCE_REFRESH_COMMAND];
return [copyValue, Commands.FORCE_REFRESH_COMMAND, Commands.EXPORT_NODE_COMMAND];
case 'registerRW':
return [copyValue, Commands.FORCE_REFRESH_COMMAND, updateNode];
return [copyValue, Commands.FORCE_REFRESH_COMMAND, updateNode, Commands.EXPORT_NODE_COMMAND];
case 'registerWO':
return [];
default:
Expand Down Expand Up @@ -256,6 +256,11 @@ export class PeripheralClusterNodeConverter {

// ==== Rendering ====

private getCommands(): CDTTreeTableActionColumnCommand[] {

return [Commands.EXPORT_NODE_COMMAND];
}

private getColumns(peripheral: PeripheralClusterNodeDTO, _context: TreeConverterContext<PeripheralTreeNodeDTOs>): Record<string, CDTTreeTableColumn> {
const labelValue = hexFormat(peripheral.offset, 0);

Expand All @@ -269,6 +274,10 @@ export class PeripheralClusterNodeConverter {
type: 'string',
label: labelValue,
tooltip: labelValue
},
'actions': {
type: 'action',
commands: this.getCommands()
}
};
}
Expand Down
16 changes: 16 additions & 0 deletions src/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as vscode from 'vscode';

export async function getFilePath(): Promise<vscode.Uri | undefined> {
const workspaceFolder = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri : undefined;
if (!workspaceFolder) {
return;
}
const defaultFilePath = vscode.Uri.joinPath(workspaceFolder, 'registers.xml');
const fileUri = await vscode.window.showSaveDialog({
defaultUri: defaultFilePath,
filters: {
'xml': ['xml'],
},
});
return fileUri;
}
10 changes: 10 additions & 0 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export namespace Commands {
icon: 'edit',
title: 'Update Value'
} as const;
export const EXPORT_NODE_COMMAND: CommandDefinition = {
commandId: `${PACKAGE_NAME}.svd.exportNode`,
icon: 'desktop-download',
title: 'Export Register'
} as const;
export const COPY_VALUE_COMMAND: CommandDefinition = {
commandId: `${PACKAGE_NAME}.svd.copyValue`,
icon: 'files',
Expand Down Expand Up @@ -64,6 +69,11 @@ export namespace Commands {
icon: 'collapse-all',
title: 'Collapse All'
} as const;
export const EXPORT_ALL_COMMAND: CommandDefinition = {
commandId: `${PACKAGE_NAME}.svd.exportAll`,
icon: 'desktop-download',
title: 'Export All'
} as const;
}

export namespace IgnorePeripherals {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/peripheral/nodes/base-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export abstract class PeripheralBaseNode extends BaseTreeNode {
return PeripheralBaseNodeDTO.create({
...super.serialize(),
id: this.getId(),
name: this.name || '',
format: this.format,
pinned: this.pinned,
session: this.session?.id,
Expand Down
1 change: 1 addition & 0 deletions src/plugin/peripheral/nodes/peripheral-cluster-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class PeripheralClusterNode extends ClusterOrRegisterBaseNode {
return PeripheralClusterNodeDTO.create({
...super.serialize(),
...this.options,
name: this.name,
offset: this.offset,
children: []
});
Expand Down
1 change: 1 addition & 0 deletions src/plugin/peripheral/nodes/peripheral-field-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class PeripheralFieldNode extends PeripheralBaseNode {
return PeripheralFieldNodeDTO.create({
...super.serialize(),
...this.options,
name: this.name,
parentAddress: this.parent.getAddress(),
previousValue: this.previousValue,
currentValue: this.getCurrentValue(),
Expand Down
1 change: 1 addition & 0 deletions src/plugin/peripheral/nodes/peripheral-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export class PeripheralNode extends PeripheralBaseNode {
...super.serialize(),
...this.options,
groupName: this.groupName,
name: this.name,
children: []
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/peripheral/nodes/peripheral-register-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ export class PeripheralRegisterNode extends ClusterOrRegisterBaseNode {
return PeripheralRegisterNodeDTO.create({
...super.serialize(),
...this.options,
name: this.name,
previousValue: this.previousValue,
currentValue: this.currentValue,
resetValue: this.resetValue,
offset: this.offset,
size: this.size,
hexLength: this.hexLength,
children: [],
address: this.getAddress()
Expand Down
Loading

0 comments on commit 89663e2

Please sign in to comment.