Skip to content

Commit

Permalink
add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-marut-work committed Sep 5, 2023
1 parent ac1323c commit 414bc6d
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 55 deletions.
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@
"items": {
"type": "string"
},
"default": [
"gdb",
"embedded-debug",
"arm-debug"
],
"default": [ "gdb", "embedded-debug", "arm-debug" ],
"description": "C-based debuggers to activate (requires debug session restart)"
},
"memory-inspector.refreshOnStop": {
Expand Down Expand Up @@ -167,12 +163,12 @@
"default": 4,
"description": "Default groups per row"
},
"memory-inspector.showVariablesColumn": {
"memory-inspector.variablesVisible": {
"type": "boolean",
"default": false,
"description": "Show variables column?"
},
"memory-inspector.showAsciiColumn": {
"memory-inspector.asciiVisible": {
"type": "boolean",
"default": false,
"description": "Show ASCII column?"
Expand Down
9 changes: 6 additions & 3 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
import type { DebugProtocol } from '@vscode/debugprotocol';
import type { NotificationType, RequestType } from 'vscode-messenger-common';
import type { VariableRange } from './memory-range';
import { MemoryInspectorConfiguration } from '../webview/utils/view-types';
import { ColumnVisibilityStatus, MemoryDisplayConfiguration, MemoryDisplayConfigurationChangeRequest } from '../webview/utils/view-types';

export type MemoryReadResult = DebugProtocol.ReadMemoryResponse['body'];
export type MemoryWriteResult = DebugProtocol.WriteMemoryResponse['body'];

export const readyType: NotificationType<void> = { method: 'ready' };
export const logMessageType: RequestType<string, void> = { method: 'logMessage' };
export const getConfigurationType: RequestType<void, MemoryInspectorConfiguration> = { method: 'getConfiguration' };
export const configurationDidChangeType: NotificationType<MemoryInspectorConfiguration> = { method: 'configurationDidChangeType' };
export const getMemoryDisplayConfigurationType: RequestType<void, MemoryDisplayConfiguration> = { method: 'getMemoryDisplayConfiguration' };
export const setMemoryDisplayConfigurationType: NotificationType<MemoryDisplayConfigurationChangeRequest> = { method: 'setMemoryDisplayConfiguration' };
export const memoryDisplayConfigurationChangedType: NotificationType<MemoryDisplayConfiguration> = { method: 'memoryDisplayConfigurationChanged' };
export const getColumnsVisibility: RequestType<void, ColumnVisibilityStatus[]> = { method: 'getColumnsVisibility' };
export const columnVisibilityType: NotificationType<ColumnVisibilityStatus> = { method: 'columnVisibility' };
export const setOptionsType: RequestType<Partial<DebugProtocol.ReadMemoryArguments | undefined>, void> = { method: 'setOptions' };
export const readMemoryType: RequestType<DebugProtocol.ReadMemoryArguments, MemoryReadResult> = { method: 'readMemory' };
export const writeMemoryType: RequestType<DebugProtocol.WriteMemoryArguments, MemoryWriteResult> = { method: 'writeMemory' };
Expand Down
6 changes: 2 additions & 4 deletions src/plugin/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,5 @@ export const CONFIG_WORDS_PER_GROUP = 'wordsPerGroup';
export const DEFAULT_WORDS_PER_GROUP = 1;
export const CONFIG_GROUPS_PER_ROW = 'groupsPerRow';
export const DEFAULT_GROUPS_PER_ROW = 4;
export const CONFIG_SHOW_VARIABLES_COLUMN = 'showVariablesColumn';
export const DEFAULT_SHOW_VARIABLES_COLUMN = false;
export const CONFIG_SHOW_ASCII_COLUMN = 'showasciiColumn';
export const DEFAULT_SHOW_ASCII_COLUMN = false;
export const CONFIG_SHOW_VARIABLES_COLUMN = 'variablesVisible';
export const CONFIG_SHOW_ASCII_COLUMN = 'asciiVisible';
70 changes: 54 additions & 16 deletions src/plugin/memory-webview-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ import {
MemoryReadResult,
MemoryWriteResult,
getVariables,
getConfigurationType,
configurationDidChangeType
memoryDisplayConfigurationChangedType,
columnVisibilityType,
setMemoryDisplayConfigurationType,
getColumnsVisibility,
getMemoryDisplayConfigurationType,
} from '../common/messaging';
import { MemoryProvider } from './memory-provider';
import { outputChannelLogger } from './logger';
import { VariableRange } from '../common/memory-range';
import { MemoryInspectorConfiguration } from '../webview/utils/view-types';
import { ColumnVisibilityStatus, MemoryDisplayConfiguration as MemoryDisplayConfiguration } from '../webview/utils/view-types';

interface Variable {
name: string;
Expand Down Expand Up @@ -141,47 +144,82 @@ export class MemoryWebview {
const disposables = [
this.messenger.onNotification(readyType, () => this.refresh(participant, options), { sender: participant }),
this.messenger.onRequest(logMessageType, message => outputChannelLogger.info('[webview]:', message), { sender: participant }),
this.messenger.onRequest(getConfigurationType, () => this.getConfiguration(), { sender: participant }),
this.messenger.onRequest(readMemoryType, request => this.readMemory(request), { sender: participant }),
this.messenger.onRequest(writeMemoryType, request => this.writeMemory(request), { sender: participant }),
this.messenger.onRequest(getVariables, request => this.getVariables(request), { sender: participant }),
this.messenger.onRequest(getVariables, request => this.getVariables(request), { sender: participant }),
this.messenger.onRequest(getMemoryDisplayConfigurationType, () => this.getMemoryDisplayConfiguration(), { sender: participant }),
this.messenger.onRequest(getColumnsVisibility, () => this.getColumnConfigurations(), { sender: participant }),
this.messenger.onNotification(setMemoryDisplayConfigurationType, request => this.setConfiguration(request), { sender: participant }),
this.messenger.onNotification(columnVisibilityType, request => this.handleColumnToggled(request), { sender: participant }),

this.memoryProvider.onDidStopDebug(() => {
if (this.refreshOnStop === RefreshEnum.on) {
this.refresh(participant);
}
}),
this.onConfigurationChanged(participant),
this.onMemoryDisplayConfigurationChanged(participant),
this.onColumnVisibilityConfigurationChanged(participant),
];

panel.onDidDispose(() => disposables.forEach(disposible => disposible.dispose()));
}

protected handleColumnToggled(request: ColumnVisibilityStatus): void {
const { id, active: visible } = request;
vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).update(`${id}Visible`, visible, vscode.ConfigurationTarget.Global);
}

protected setConfiguration(request: { id: string, value: unknown }): void {
const { id, value } = request;
vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).update(id, value, vscode.ConfigurationTarget.Global);
}

protected async refresh(participant: WebviewIdMessageParticipant, options?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> {
this.messenger.sendRequest(setOptionsType, participant, options);
}

protected getConfiguration(): MemoryInspectorConfiguration {
protected getMemoryDisplayConfiguration(): MemoryDisplayConfiguration {
const memoryInspectorConfiguration = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME);
const wordsPerGroup = memoryInspectorConfiguration.get<number>(manifest.CONFIG_WORDS_PER_GROUP) || manifest.DEFAULT_WORDS_PER_GROUP;
const groupsPerRow = memoryInspectorConfiguration.get<number>(manifest.CONFIG_GROUPS_PER_ROW) || manifest.DEFAULT_GROUPS_PER_ROW;
const showVariablesColumn = memoryInspectorConfiguration.get<boolean>(manifest.CONFIG_SHOW_VARIABLES_COLUMN) || manifest.DEFAULT_SHOW_VARIABLES_COLUMN;
const showAsciiColumn = memoryInspectorConfiguration.get<boolean>(manifest.CONFIG_SHOW_ASCII_COLUMN) || manifest.DEFAULT_SHOW_ASCII_COLUMN;
return { wordsPerGroup, groupsPerRow, showAsciiColumn, showVariablesColumn };
return { wordsPerGroup, groupsPerRow };
}

protected onConfigurationChanged(participant: MessageParticipant): vscode.Disposable {
const VIEW_CONFIGURATION_OPTIONS = [
protected onMemoryDisplayConfigurationChanged(participant: MessageParticipant): vscode.Disposable {
const memoryDisplayConfigurations = [
manifest.CONFIG_WORDS_PER_GROUP,
manifest.CONFIG_GROUPS_PER_ROW,
];
return vscode.workspace.onDidChangeConfiguration(e => {
if (memoryDisplayConfigurations.some(configurationOption => e.affectsConfiguration(`${manifest.PACKAGE_NAME}.${configurationOption}`))) {
const configuration = this.getMemoryDisplayConfiguration();
this.messenger.sendNotification(memoryDisplayConfigurationChangedType, participant, configuration);
}
});
}

protected onColumnVisibilityConfigurationChanged(participant: MessageParticipant): vscode.Disposable {
const columnConfigurations = [
manifest.CONFIG_SHOW_ASCII_COLUMN,
manifest.CONFIG_SHOW_VARIABLES_COLUMN,
];
return vscode.workspace.onDidChangeConfiguration(e => {
if (VIEW_CONFIGURATION_OPTIONS.some(configurationOption => e.affectsConfiguration(`${manifest.PACKAGE_NAME}.${configurationOption}`))) {
const configuration = this.getConfiguration();
this.messenger.sendNotification(configurationDidChangeType, participant, configuration);
}
columnConfigurations.forEach(configuration => {
if (e.affectsConfiguration(`${manifest.PACKAGE_NAME}.${configuration}`)) {
const [id] = configuration.split('Visible');
const active = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<boolean>(configuration) ?? true;
this.messenger.sendNotification(columnVisibilityType, participant, { id, active });
}
});
});
}

protected getColumnConfigurations(): ColumnVisibilityStatus[] {
const COLUMN_CONFIGURATIONS = [manifest.CONFIG_SHOW_VARIABLES_COLUMN, manifest.CONFIG_SHOW_ASCII_COLUMN];
return COLUMN_CONFIGURATIONS.map(configuration => {
const [id] = configuration.split('Visible');
const active = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<boolean>(configuration) ?? false;
return { id, active };
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/webview/columns/ascii-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import { ReactNode } from 'react';
import { BigIntMemoryRange, toOffset } from '../../common/memory-range';
import { Memory } from '../utils/view-types';
import { ColumnContribution, TableRenderOptions } from './column-contribution-service';
import { Memory } from '../utils/view-types';

function isPrintableAsAscii(input: number): boolean {
return input >= 32 && input < (128 - 1);
Expand Down
4 changes: 2 additions & 2 deletions src/webview/components/memory-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
VSCodeDataGridRow,
VSCodeDataGridCell
} from '@vscode/webview-ui-toolkit/react';
import { Decoration, Memory, StylableNodeAttributes } from '../utils/view-types';
import { Decoration, Memory, MemoryDisplayConfiguration, StylableNodeAttributes } from '../utils/view-types';
import { toHexStringWithRadixMarker } from '../../common/memory-range';
import { TableRenderOptions } from '../columns/column-contribution-service';

interface MemoryTableProps extends TableRenderOptions {
interface MemoryTableProps extends TableRenderOptions, MemoryDisplayConfiguration {
memory?: Memory;
decorations: Decoration[];
}
Expand Down
16 changes: 12 additions & 4 deletions src/webview/components/memory-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { DebugProtocol } from '@vscode/debugprotocol';
import React from 'react';
import { MemoryTable } from './memory-table';
import { OptionsWidget } from './options-widget';
import { Decoration, Endianness, Memory } from '../utils/view-types';
import { Decoration, Endianness, Memory, MemoryDisplayConfiguration } from '../utils/view-types';
import { messenger } from '../view-messenger';
import { getMemoryDisplayConfigurationType, memoryDisplayConfigurationChangedType } from '../../common/messaging';
import { HOST_EXTENSION } from 'vscode-messenger-common';
import { ColumnStatus } from '../columns/column-contribution-service';

interface MemoryWidgetProps {
Expand All @@ -33,11 +36,9 @@ interface MemoryWidgetProps {
toggleColumn(id: string, active: boolean): void;
}

interface MemoryWidgetState {
interface MemoryWidgetState extends MemoryDisplayConfiguration {
endianness: Endianness;
wordSize: number;
wordsPerGroup: number;
groupsPerRow: number;
}

const defaultOptions: MemoryWidgetState = {
Expand All @@ -53,6 +54,13 @@ export class MemoryWidget extends React.Component<MemoryWidgetProps, MemoryWidge
this.state = { ...defaultOptions };
}

public componentDidMount(): void {
messenger.onNotification(memoryDisplayConfigurationChangedType, configuration => this.setState(configuration));
messenger.sendRequest(getMemoryDisplayConfigurationType, HOST_EXTENSION, undefined).then(configuration => {
this.setState(configuration);
});
}

override render(): React.ReactNode {
return <>
<OptionsWidget
Expand Down
19 changes: 13 additions & 6 deletions src/webview/components/options-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

import React from 'react';
import type { DebugProtocol } from '@vscode/debugprotocol';
import { SerializedTableRenderOptions } from '../utils/view-types';
import { MemoryDisplayConfiguration, MemoryDisplayConfigurationChangeRequest, SerializedTableRenderOptions } from '../utils/view-types';
import { VSCodeButton, VSCodeDivider, VSCodeDropdown, VSCodeOption, VSCodeTextField } from '@vscode/webview-ui-toolkit/react';
import { TableRenderOptions } from '../columns/column-contribution-service';
import { MultiSelectWithLabel } from './multi-select-bar';
import { messenger } from '../view-messenger';
import { setMemoryDisplayConfigurationType } from '../../common/messaging';
import { HOST_EXTENSION } from 'vscode-messenger-common';
import { TableRenderOptions } from '../columns/column-contribution-service';

export interface OptionsWidgetProps extends TableRenderOptions, Required<DebugProtocol.ReadMemoryArguments> {
export interface OptionsWidgetProps extends TableRenderOptions, Required<DebugProtocol.ReadMemoryArguments>, MemoryDisplayConfiguration {
updateRenderOptions: (options: Partial<SerializedTableRenderOptions>) => void;
updateMemoryArguments: (memoryArguments: Partial<DebugProtocol.ReadMemoryArguments>) => void;
refreshMemory: () => void;
Expand Down Expand Up @@ -99,7 +102,7 @@ export class OptionsWidget extends React.Component<OptionsWidgetProps, OptionsWi
this.state.showRenderOptions && <>
<VSCodeDivider />
<div className="advanced-options" style={AdvancedOptionsStyle}>
<label htmlFor={InputId.wordsPerGroup}>Bytes per Group</label>
<label htmlFor={InputId.wordsPerGroup}>Words per Group</label>
<VSCodeDropdown id={InputId.wordsPerGroup} onChange={this.handleInputChange} value={this.props.wordsPerGroup.toString()}>
<VSCodeOption>1</VSCodeOption>
<VSCodeOption>2</VSCodeOption>
Expand Down Expand Up @@ -140,11 +143,15 @@ export class OptionsWidget extends React.Component<OptionsWidgetProps, OptionsWi
case InputId.Address: return this.props.updateMemoryArguments({ memoryReference: event.currentTarget.value });
case InputId.Offset: return !Number.isNaN(event.currentTarget.value) && this.props.updateMemoryArguments({ offset: Number(event.currentTarget.value) });
case InputId.Length: return !Number.isNaN(event.currentTarget.value) && this.props.updateMemoryArguments({ count: Number(event.currentTarget.value) });
case InputId.wordsPerGroup: return this.props.updateRenderOptions({ wordsPerGroup: Number(event.currentTarget.value) });
case InputId.GroupsPerRow: return this.props.updateRenderOptions({ groupsPerRow: Number(event.currentTarget.value) });
case InputId.wordsPerGroup: return this.updateConfiguration({ id: 'wordsPerGroup', value: Number(event.currentTarget.value) });
case InputId.GroupsPerRow: return this.updateConfiguration({ id: 'groupsPerRow', value: Number(event.currentTarget.value) });
}
}

protected updateConfiguration(viewConfigurationChangeRequest: MemoryDisplayConfigurationChangeRequest): void {
return messenger.sendNotification(setMemoryDisplayConfigurationType, HOST_EXTENSION, viewConfigurationChangeRequest);
}

protected toggleRenderOptions = () => this.doToggleRenderOptions();

protected doToggleRenderOptions(): void {
Expand Down
27 changes: 21 additions & 6 deletions src/webview/memory-webview-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import {
readyType,
logMessageType,
setOptionsType,
readMemoryType
readMemoryType,
columnVisibilityType,
getColumnsVisibility
} from '../common/messaging';
import type { DebugProtocol } from '@vscode/debugprotocol';
import { Decoration, Memory, MemoryState } from './utils/view-types';
import { ColumnVisibilityStatus, Decoration, Memory, MemoryState } from './utils/view-types';
import { MemoryWidget } from './components/memory-widget';
import { messenger } from './view-messenger';
import { columnContributionService, ColumnStatus } from './columns/column-contribution-service';
import { ColumnStatus, columnContributionService } from './columns/column-contribution-service';
import { decorationService } from './decorations/decoration-service';
import { variableDecorator } from './variables/variable-decorations';
import { AsciiColumn } from './columns/ascii-column';
Expand All @@ -39,6 +41,9 @@ export interface MemoryAppState extends MemoryState {
columns: ColumnStatus[];
}

export const DEFAULT_WORDS_PER_GROUP = 1;
export const DEFAULT_GROUPS_PER_ROW = 4;

class App extends React.Component<{}, MemoryAppState> {

public constructor(props: {}) {
Expand All @@ -61,6 +66,11 @@ class App extends React.Component<{}, MemoryAppState> {
public componentDidMount(): void {
messenger.onRequest(setOptionsType, options => this.setOptions(options));
messenger.sendNotification(readyType, HOST_EXTENSION, undefined);
messenger.onNotification(columnVisibilityType, request => this.handleColumnVisibilityChanged(request));
messenger.sendRequest(getColumnsVisibility, HOST_EXTENSION, undefined).then(columnsVisibility => {
columnsVisibility.forEach(columnVisibility => this.handleColumnVisibilityChanged(columnVisibility));
});

}

public render(): React.ReactNode {
Expand All @@ -77,6 +87,12 @@ class App extends React.Component<{}, MemoryAppState> {
/>;
}

protected async handleColumnVisibilityChanged(request: ColumnVisibilityStatus): Promise<void> {
const { active, id } = request;
const columns = active ? await columnContributionService.show(id, this.state) : columnContributionService.hide(id);
this.setState({ columns });
}

protected updateMemoryState = (newState: Partial<MemoryState>) => this.setState(prevState => ({ ...prevState, ...newState }));

protected async setOptions(options?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> {
Expand Down Expand Up @@ -114,9 +130,8 @@ class App extends React.Component<{}, MemoryAppState> {

protected toggleColumn = (id: string, active: boolean): void => { this.doToggleColumn(id, active); };

protected async doToggleColumn(id: string, active: boolean): Promise<void> {
const columns = active ? await columnContributionService.show(id, this.state) : columnContributionService.hide(id);
this.setState({ columns });
protected doToggleColumn(id: string, active: boolean): void {
messenger.sendNotification(columnVisibilityType, HOST_EXTENSION, { id, active });
}
}

Expand Down
Loading

0 comments on commit 414bc6d

Please sign in to comment.