Skip to content

Commit

Permalink
feat: safer types
Browse files Browse the repository at this point in the history
 - translations for dev commands,
 - split up the "magic" board finding function to clarify their behavior
 - hide board list on revert
  • Loading branch information
Akos Kitta committed Jul 27, 2023
1 parent d4d0b1c commit 8e76373
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Disposable } from '@theia/core/lib/common/disposable';
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import {
Expand All @@ -7,8 +6,10 @@ import {
CommandRegistry,
CommandService,
} from '@theia/core/lib/common/command';
import type { Disposable } from '@theia/core/lib/common/disposable';
import { Emitter } from '@theia/core/lib/common/event';
import { ILogger } from '@theia/core/lib/common/logger';
import { nls } from '@theia/core/lib/common/nls';
import type { Mutable } from '@theia/core/lib/common/types';
import {
inject,
Expand Down Expand Up @@ -534,18 +535,24 @@ const USE_INHERITED_CONFIG: Command = {

const DUMP_BOARD_LIST: Command = {
id: 'arduino-dump-board-list',
label: 'Dump the Board List', // TODO: if remains in IDE2, add translations.
label: nls.localize('arduino/developer/dumpBoardList', 'Dump the Board List'),
category: 'Developer (Arduino)',
};

const CLEAR_BOARD_LIST_HISTORY: Command = {
id: 'arduino-clear-board-list-history',
label: 'Clear the Board List History', // TODO: if remains in IDE2, add translations.
label: nls.localize(
'arduino/developer/clearBoardList',
'Clear the Board List History'
),
category: 'Developer (Arduino)',
};

const CLEAR_BOARDS_CONFIG: Command = {
id: 'arduino-clear-boards-config',
label: 'Clear the Board and Port Configuration', // TODO: if remains in IDE2, add translations.
label: nls.localize(
'arduino/developer/clearBoardsConfig',
' Clear the Board and Port Configuration'
),
category: 'Developer (Arduino)',
};
28 changes: 17 additions & 11 deletions arduino-ide-extension/src/browser/boards/boards-toolbar-item.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { codicon } from '@theia/core/lib/browser/index';
import { TabBarToolbar } from '@theia/core/lib/browser/shell/tab-bar-toolbar/tab-bar-toolbar';
import { CommandRegistry } from '@theia/core/lib/common/command';
import { codicon } from '@theia/core/lib/browser/widgets/widget';
import type { CommandRegistry } from '@theia/core/lib/common/command';
import {
Disposable,
DisposableCollection,
Expand All @@ -12,7 +12,7 @@ import classNames from 'classnames';
import { unknownBoard } from '../../common/protocol';
import {
BoardListItem,
getBoardListItemBoard,
getInferredBoardOrBoard,
InferredBoardListItem,
isInferredBoardListItem,
} from '../../common/protocol/board-list';
Expand All @@ -35,6 +35,10 @@ export namespace BoardsDropDown {
readonly coords: BoardsDropDownListCoords | 'hidden';
readonly boardList: BoardListUI;
readonly openBoardsConfig: () => void;
/**
* Hides the board list dropdown, if it's visible. Otherwise, NOOP.
*/
readonly hide: () => void;
}
}

Expand Down Expand Up @@ -67,7 +71,7 @@ export class BoardListDropDown extends React.Component<BoardsDropDown.Props> {
);
}

protected renderBoardListItems(): React.ReactNode {
private renderBoardListItems(): React.ReactNode {
const { coords, boardList } = this.props;
if (coords === 'hidden') {
return '';
Expand Down Expand Up @@ -120,14 +124,10 @@ export class BoardListDropDown extends React.Component<BoardsDropDown.Props> {
onEdit: EditBoardsConfigAction;
}): React.ReactNode {
const port = item.port;
const boardLabel = isInferredBoardListItem(item)
? item.inferredBoard.name
: item.board?.name ?? unknownBoard;
const boardFqbn = isInferredBoardListItem(item)
? item.inferredBoard.fqbn
: item.board?.fqbn;
const board = getInferredBoardOrBoard(item);
const boardLabel = board?.name ?? unknownBoard;
const boardFqbn = board?.fqbn;
const onDefaultAction = () => {
const board = getBoardListItemBoard(item, 'inferred');
if (board) {
onSelect({ selectedBoard: board, selectedPort: port });
} else {
Expand Down Expand Up @@ -218,6 +218,7 @@ export class BoardListDropDown extends React.Component<BoardsDropDown.Props> {
event.preventDefault();
event.stopPropagation();
onRevert({ selectedBoard: inferredItem.board, selectedPort: port });
this.props.hide();
}}
/>
) : undefined;
Expand Down Expand Up @@ -289,6 +290,10 @@ export class BoardsToolBarItem extends React.Component<
event.nativeEvent.stopImmediatePropagation();
};

private readonly hide = () => {
this.setState({ coords: 'hidden' });
};

override render(): React.ReactNode {
const { coords, boardList } = this.state;
const { selectedBoard, selectedPort } = boardList.boardsConfig;
Expand Down Expand Up @@ -333,6 +338,7 @@ export class BoardsToolBarItem extends React.Component<
openBoardsConfig={() =>
boardList.onEdit({ query: { action: 'clear-if-not-empty' } })
}
hide={this.hide}
></BoardListDropDown>
</React.Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from '@theia/core/shared/react';
import {
BoardList,
BoardListItem,
getBoardListItemBoard,
getInferredBoardOrBoard,
} from '../../../common/protocol/board-list';
import { boardIdentifierEquals } from '../../../common/protocol/boards-service';
import { ArduinoSelect } from '../../widgets/arduino-select';
Expand Down Expand Up @@ -57,7 +57,7 @@ export const SelectBoardComponent = ({
const selectedItem: BoardListItem | undefined =
boardList[boardList.selectedIndex];
const selectedBoard = selectedItem
? getBoardListItemBoard(selectedItem, 'inferred')
? getInferredBoardOrBoard(selectedItem)
: undefined;
const boardsList: BoardOption[] = updatableBoards.map((item, i) => {
if (!!selectedBoard && boardIdentifierEquals(item.board, selectedBoard)) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export const SelectBoardComponent = ({
if (selectedItem) {
selBoard = boardsList
.map((boardOpt) => boardOpt.value)
.indexOf(getBoardListItemBoard(selectedItem, 'inferred')?.fqbn || '');
.indexOf(getInferredBoardOrBoard(selectedItem)?.fqbn || '');
}

selectOption(boardsList[selBoard] || null);
Expand Down
38 changes: 16 additions & 22 deletions arduino-ide-extension/src/common/protocol/board-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,26 @@ export function isBoardListItem(arg: unknown): arg is BoardListItem {
);
}

/**
* Gets the board or the inferred one if applicable.
* If `resolveStrategy` is `'default'`, and `item` has a `board`, the inferred is ignored.
* If the strategy is `'inferred'`, the inferred board has higher precedence.
*/
export function getBoardListItemBoard(
item: BoardListItem,
resolveStrategy: 'default' | 'inferred'
export function getBoardOrInferredBoard(
item: BoardListItem
): BoardIdentifier | undefined {
let board: BoardIdentifier | undefined = undefined;
if (resolveStrategy === 'default') {
board = item.board;
if (!board && isInferredBoardListItem(item)) {
board = item.inferredBoard;
}
} else {
if (isInferredBoardListItem(item)) {
board = item.inferredBoard;
}
if (!board) {
board = item.board;
}
board = item.board;
if (!board && isInferredBoardListItem(item)) {
board = item.inferredBoard;
}
return board;
}

export function getInferredBoardOrBoard(
item: BoardListItem
): BoardIdentifier | undefined {
if (isInferredBoardListItem(item)) {
return item.inferredBoard;
}
return item.board;
}

interface BoardSelectedBoardListItem extends BoardListItem {
readonly inferredBoard: BoardIdentifier;
readonly type: Extract<InferenceType, 'board-select'>;
Expand Down Expand Up @@ -136,8 +130,8 @@ function boardListItemComparator(

// compare by board
result = boardIdentifierComparator(
getBoardListItemBoard(left, 'default'),
getBoardListItemBoard(right, 'default')
getBoardOrInferredBoard(left),
getBoardOrInferredBoard(right)
);
if (result) {
return result;
Expand Down
14 changes: 7 additions & 7 deletions arduino-ide-extension/src/common/protocol/boards-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export namespace Port {
export namespace Properties {
export function create(
properties: [string, string][] | undefined
): Properties {
if (!properties) {
return {};
): Properties | undefined {
if (!properties || !properties.length) {
return undefined;
}
return properties.reduce((acc, curr) => {
const [key, value] = curr;
Expand Down Expand Up @@ -303,10 +303,10 @@ export namespace BoardsPackage {
}
}

export interface Board {
readonly name: string;
readonly fqbn?: string;
}
/**
* @deprecated user `BoardIdentifier` instead.
*/
export type Board = BoardIdentifier;

export interface BoardUserField {
readonly toolId: string;
Expand Down
6 changes: 3 additions & 3 deletions arduino-ide-extension/src/node/board-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ export class BoardDiscovery
protocol: rpcPort.getProtocol(),
protocolLabel: rpcPort.getProtocolLabel(),
properties: Port.Properties.create(rpcPort.getPropertiesMap().toObject()),
hardwareId: rpcPort.getHardwareId(),
hardwareId: rpcPort.getHardwareId() || undefined, // prefer undefined over empty string
};
}

private fireSoonHandle?: NodeJS.Timeout;
private bufferedEvents: DetectedPortChangeEvent[] = [];
private fireSoonHandle: NodeJS.Timeout | undefined;
private readonly bufferedEvents: DetectedPortChangeEvent[] = [];
private fireSoon(event: DetectedPortChangeEvent): void {
this.bufferedEvents.push(event);
clearTimeout(this.fireSoonHandle);
Expand Down
5 changes: 5 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@
"optimizeForDebugging": "Optimize for Debugging",
"sketchIsNotCompiled": "Sketch '{0}' must be verified before starting a debug session. Please verify the sketch and start debugging again. Do you want to verify the sketch now?"
},
"developer": {
"clearBoardList": "Clear the Board List History",
"clearBoardsConfig": " Clear the Board and Port Configuration",
"dumpBoardList": "Dump the Board List"
},
"dialog": {
"dontAskAgain": "Don't ask again"
},
Expand Down

0 comments on commit 8e76373

Please sign in to comment.