Skip to content

Commit a57cdff

Browse files
author
Akos Kitta
committed
fix: naive search restriction for ambiguous boards
Signed-off-by: Akos Kitta <[email protected]>
1 parent 3806314 commit a57cdff

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

arduino-ide-extension/src/browser/boards/boards-config-component.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace BoardsConfigComponent {
2323
* This is not the real config, it's only living in the dialog. Users can change it without update and can cancel any modifications.
2424
*/
2525
readonly boardsConfig: BoardsConfigDialogState;
26+
readonly searchSet: BoardIdentifier[] | undefined;
2627
readonly notificationCenter: NotificationCenter;
2728
readonly appState: FrontendApplicationState;
2829
readonly onFocusNodeSet: (element: HTMLElement | undefined) => void;
@@ -130,13 +131,6 @@ export class BoardsConfigComponent extends React.Component<
130131
this.props.onFilteredTextDidChangeEvent((query) => {
131132
if (typeof query === 'string') {
132133
this.setState({ query }, () => this.updateBoards(this.state.query));
133-
} else {
134-
const action = query?.action;
135-
if (action === 'clear') {
136-
this.setState({ query: '' }, () =>
137-
this.updateBoards(this.state.query)
138-
);
139-
}
140134
}
141135
}),
142136
]);
@@ -159,10 +153,20 @@ export class BoardsConfigComponent extends React.Component<
159153
);
160154
};
161155

162-
private readonly queryBoards = (
156+
private readonly queryBoards = async (
163157
options: { query?: string } = {}
164158
): Promise<Array<BoardWithPackage>> => {
165-
return this.props.searchBoards(options);
159+
const result = await this.props.searchBoards(options);
160+
const { searchSet } = this.props;
161+
if (searchSet) {
162+
return result.filter((board) =>
163+
searchSet.some(
164+
(restriction) =>
165+
restriction.fqbn === board.fqbn || restriction.name === board.fqbn
166+
)
167+
);
168+
}
169+
return result;
166170
};
167171

168172
private readonly toggleFilterPorts = () => {

arduino-ide-extension/src/browser/boards/boards-config-dialog.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import React from '@theia/core/shared/react';
1313
import type { ReactNode } from '@theia/core/shared/react/index';
1414
import { EditBoardsConfigActionParams } from '../../common/protocol/board-list';
1515
import {
16+
BoardIdentifier,
1617
BoardsConfig,
1718
BoardWithPackage,
1819
DetectedPort,
@@ -66,6 +67,10 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfigDialogState> {
6667
return this.boardsServiceProvider.boardList.ports(predicate);
6768
};
6869
private _boardsConfig: BoardsConfigDialogState;
70+
/**
71+
* When the dialog's boards result set is limited to a subset of boards when searching, this field is set.
72+
*/
73+
private _searchSet: BoardIdentifier[] | undefined;
6974
private focusNode: HTMLElement | undefined;
7075

7176
constructor(
@@ -88,15 +93,20 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfigDialogState> {
8893
this.boardsServiceProvider.onBoardListDidChange(() => {
8994
this._boardsConfig = deepClone(this.boardsServiceProvider.boardsConfig);
9095
this.update();
91-
}); // Do not add to `toDispose`!
96+
});
9297
this._boardsConfig = deepClone(this.boardsServiceProvider.boardsConfig);
9398
}
9499

95100
override async open(
96101
params?: EditBoardsConfigActionParams
97102
): Promise<BoardsConfig | undefined> {
103+
this._searchSet = undefined;
104+
this._boardsConfig.selectedBoard =
105+
this.boardsServiceProvider.boardsConfig.selectedBoard;
106+
this._boardsConfig.selectedPort =
107+
this.boardsServiceProvider.boardsConfig.selectedPort;
98108
if (params) {
99-
if (params.query || typeof params.query === 'string') {
109+
if (typeof params.query === 'string') {
100110
this.onFilterTextDidChangeEmitter.fire(params.query);
101111
}
102112
if (params.portToSelect) {
@@ -105,6 +115,9 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfigDialogState> {
105115
if (params.boardToSelect) {
106116
this._boardsConfig.selectedBoard = params.boardToSelect;
107117
}
118+
if (params.searchSet) {
119+
this._searchSet = params.searchSet.slice();
120+
}
108121
}
109122
return super.open();
110123
}
@@ -137,6 +150,7 @@ export class BoardsConfigDialog extends ReactDialog<BoardsConfigDialogState> {
137150
<div className="selectBoardContainer">
138151
<BoardsConfigComponent
139152
boardsConfig={this._boardsConfig}
153+
searchSet={this._searchSet}
140154
onBoardSelected={this.onBoardSelected}
141155
onPortSelected={this.onPortSelected}
142156
notificationCenter={this.notificationCenter}

arduino-ide-extension/src/browser/boards/boards-toolbar-item.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ export class BoardsToolBarItem extends React.Component<
343343
<BoardListDropDown
344344
coords={coords}
345345
boardList={boardList}
346-
openBoardsConfig={() => boardList.onEdit()}
346+
openBoardsConfig={() => boardList.onEdit({ query: '' })}
347347
/>
348348
</React.Fragment>
349349
);

arduino-ide-extension/src/common/protocol/board-list.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -242,38 +242,31 @@ export function createBoardListItemLabels(
242242
item: BoardListItem
243243
): BoardListItemLabels {
244244
const { port } = item;
245-
let boardIdentifier: BoardIdentifier = {
246-
name: unknownBoard,
247-
fqbn: undefined,
248-
};
249-
if (item.board) {
250-
boardIdentifier = item.board;
251-
}
245+
const portLabel = port.address;
246+
const portProtocol = port.protocol;
247+
let board = item.board; // use default board label if any
252248
if (isInferredBoardListItem(item)) {
253-
boardIdentifier = item.inferredBoard;
249+
board = item.inferredBoard; // inferred board overrides any discovered boards
254250
}
255-
if (!item.board && isMultiBoardsBoardListItem(item)) {
256-
const name = findUniqueBoardName(item);
257-
if (name) {
258-
boardIdentifier = { name, fqbn: undefined };
259-
} else {
260-
boardIdentifier = { name: unconfirmedBoard, fqbn: undefined };
261-
}
251+
// if the board is still missing, maybe it's ambiguous
252+
if (!board && isMultiBoardsBoardListItem(item)) {
253+
const name =
254+
// get a unique board name
255+
findUniqueBoardName(item) ??
256+
// or fall back to something else than unknown board
257+
unconfirmedBoard;
258+
board = { name, fqbn: undefined };
262259
}
263-
const portLabel = port.address;
264-
let boardLabel = unknownBoard;
265-
let boardLabelWithFqbn = unknownBoard;
266-
if (boardIdentifier) {
267-
boardLabel = boardIdentifier.name;
268-
if (boardIdentifier.fqbn) {
269-
boardLabelWithFqbn = `${boardLabel} (${boardIdentifier.fqbn})`;
270-
}
260+
const boardLabel = board?.name ?? unknownBoard;
261+
let boardLabelWithFqbn = boardLabel;
262+
if (board?.fqbn) {
263+
boardLabelWithFqbn += ` (${board.fqbn})`;
271264
}
272265
return {
273266
boardLabel,
274267
boardLabelWithFqbn,
275268
portLabel,
276-
portProtocol: port.protocol,
269+
portProtocol,
277270
tooltip: `${boardLabelWithFqbn}\n${portLabel}`,
278271
};
279272
}
@@ -347,7 +340,7 @@ export interface SelectBoardsConfigAction {
347340
export interface EditBoardsConfigActionParams {
348341
readonly portToSelect?: PortIdentifier;
349342
readonly boardToSelect?: BoardIdentifier;
350-
readonly query?: string | undefined | { action: 'clear' };
343+
readonly query?: string;
351344
readonly searchSet?: readonly BoardIdentifier[];
352345
}
353346
export interface EditBoardsConfigAction {
@@ -575,17 +568,14 @@ export function createBoardList(
575568
},
576569
};
577570
}
578-
const params: Mutable<EditBoardsConfigActionParams> = {};
571+
const params: Mutable<EditBoardsConfigActionParams> = {
572+
portToSelect: item.port,
573+
};
579574
if (isMultiBoardsBoardListItem(item)) {
580575
const uniqueBoardName = findUniqueBoardName(item);
581-
if (uniqueBoardName) {
582-
params.query = uniqueBoardName;
583-
} else {
584-
params.query = '';
585-
}
576+
params.query = uniqueBoardName ?? '';
586577
params.searchSet = item.boards;
587578
} else {
588-
params.portToSelect = item.port;
589579
params.query = '';
590580
}
591581
return {

0 commit comments

Comments
 (0)