forked from eclipse-cdt-cloud/vscode-peripheral-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
162 lines (140 loc) · 7.28 KB
/
Copy pathcommands.ts
File metadata and controls
162 lines (140 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/********************************************************************************
* Copyright (C) 2023 Marcel Ball, Arm Limited and others.
*
* This program and the accompanying materials are made available under the
* terms of the MIT License as outlined in the LICENSE File
********************************************************************************/
import { CDTTreeMessengerType, CDTTreeWebviewContext } from '@eclipse-cdt-cloud/vscode-ui-components';
import * as vscode from 'vscode';
import { NumberFormat } from './common/format';
import { PERIPHERAL_ID_SEP } from './common/peripheral-dto';
import { VSCodeContextKeys } from './common/vscode-context';
import { getFilePath } from './fileUtils';
import { Commands } from './manifest';
import { PeripheralBaseNode } from './model/peripheral/nodes';
import { PeripheralConfigurationProvider } from './model/peripheral/tree/peripheral-configuration-provider';
import { PeripheralDataTracker } from './model/peripheral/tree/peripheral-data-tracker';
import { PeripheralsTreeTableWebView } from './views/peripheral/peripheral-view-provider';
export class PeripheralCommands {
public constructor(
protected readonly dataTracker: PeripheralDataTracker,
protected readonly config: PeripheralConfigurationProvider,
protected readonly webview: PeripheralsTreeTableWebView) {
}
public async activate(context: vscode.ExtensionContext): Promise<void> {
this.updateIgnoredPeripheralsContext();
context.subscriptions.push(
this.config.onDidChangeIgnorePeripherals(() => this.updateIgnoredPeripheralsContext()),
// VSCode specific commands
vscode.commands.registerCommand(Commands.FIND_COMMAND_ID, () => this.find()),
vscode.commands.registerCommand(Commands.SET_FORMAT_COMMAND_ID, (node) => this.peripheralsSetFormat(node)),
vscode.commands.registerCommand(Commands.REFRESH_ALL_COMMAND_ID, () => this.peripheralsForceRefresh()),
vscode.commands.registerCommand(Commands.COLLAPSE_ALL_COMMAND_ID, () => this.collapseAll()),
vscode.commands.registerCommand(Commands.EXPORT_ALL_COMMAND_ID, () => this.peripheralsExportAll()),
vscode.commands.registerCommand(Commands.IGNORE_PERIPHERAL_ID, (context) => this.ignorePeripheral(context)),
vscode.commands.registerCommand(Commands.CLEAR_IGNORED_PERIPHERAL_ID, () => this.clearIgnoredPeripherals()),
vscode.commands.registerCommand(Commands.PERIODIC_REFRESH_ID, (context) => this.periodicRefreshMode(context)),
vscode.commands.registerCommand(Commands.PERIODIC_REFRESH_INTERVAL_ID, (context) => this.periodicRefreshInterval(context)),
// Commands manually rendered in the DOM
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.FORCE_REFRESH_COMMAND.commandId, (node) => this.peripheralsForceRefresh(node)),
vscode.commands.registerCommand(Commands.PIN_COMMAND.commandId, (node) => this.peripheralsTogglePin(node)),
vscode.commands.registerCommand(Commands.UNPIN_COMMAND.commandId, (node) => this.peripheralsTogglePin(node)),
);
}
private updateIgnoredPeripheralsContext(): void {
const ignoredPeripherals = this.config.ignorePeripherals();
vscode.commands.executeCommand('setContext', VSCodeContextKeys.IGNORED_PERIPHERALS_LENGTH, ignoredPeripherals.length);
}
private async peripheralsUpdateNode(node: PeripheralBaseNode, value?: unknown): Promise<void> {
try {
const result = await node.performUpdate(value);
if (result) {
// Update the tree view
this.dataTracker.fireOnDidChange();
}
} catch (error) {
vscode.debug.activeDebugConsole.appendLine(`Unable to update value: ${(error as Error).message}`);
}
}
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);
}
}
private collapseAll(): void {
this.dataTracker.collapseAll();
}
private async peripheralsSetFormat(context: PeripheralBaseNode | CDTTreeWebviewContext): Promise<void> {
const result = await vscode.window.showQuickPick([
{ label: 'Auto', description: 'Automatically choose format (Inherits from parent)', value: NumberFormat.Auto },
{ label: 'Hex', description: 'Format value in hexadecimal', value: NumberFormat.Hexadecimal },
{ label: 'Decimal', description: 'Format value in decimal', value: NumberFormat.Decimal },
{ label: 'Binary', description: 'Format value in binary', value: NumberFormat.Binary }
]);
if (result === undefined) {
return;
}
let node: PeripheralBaseNode;
if (CDTTreeWebviewContext.is(context)) {
node = this.dataTracker.getNodeByPath(context.cdtTreeItemId.split(PERIPHERAL_ID_SEP));
} else {
node = context;
}
node.format = result.value;
this.dataTracker.fireOnDidChange();
}
private async find(): Promise<void> {
this.webview.sendNotification(CDTTreeMessengerType.openSearch);
}
private async peripheralsForceRefresh(node?: PeripheralBaseNode): Promise<void> {
if (node) {
const peripheral = node.getPeripheral();
const changes: PeripheralBaseNode[] = [];
if (peripheral) {
await peripheral.updateData({ changes });
}
this.dataTracker.fireOnDidChange(changes);
} else {
await this.dataTracker.updateData();
}
}
private peripheralsTogglePin(node: PeripheralBaseNode): void {
this.dataTracker.togglePin(node);
}
private ignorePeripheral(context: CDTTreeWebviewContext): void {
const node = this.dataTracker.getNodeByPath(context.cdtTreeItemId.split(PERIPHERAL_ID_SEP));
if (node.name) {
this.config.addIgnorePeripherals(node.name);
}
}
private clearIgnoredPeripherals(): void {
this.config.setIgnorePeripherals();
}
private periodicRefreshMode(_context?: CDTTreeWebviewContext): void {
this.config.queryPeriodicRefreshMode();
}
private periodicRefreshInterval(_context?: CDTTreeWebviewContext): void {
this.config.queryPeriodicRefreshInterval();
}
}