Skip to content

Commit 7d6e95d

Browse files
[VSC-1661] use createQuickPick for openocd board to show currently active item. (#1527)
* use createQuickPick for openocd board to show currently active item. * rm duplicated code * fix giving workspace folder to save openOCD configs * fix wait for openBoard selection
1 parent 9f1c322 commit 7d6e95d

File tree

3 files changed

+111
-111
lines changed

3 files changed

+111
-111
lines changed

src/espIdf/openOcd/boardConfiguration.ts

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
* limitations under the License.
1717
*/
1818
import { join } from "path";
19-
import * as idfConf from "../../idfConfiguration";
19+
import { readParameter, writeParameter } from "../../idfConfiguration";
2020
import { readJSON } from "fs-extra";
2121
import { Logger } from "../../logger/logger";
22-
import { Uri } from "vscode";
22+
import { commands, ConfigurationTarget, l10n, Uri, window } from "vscode";
2323
import { defaultBoards } from "./defaultBoards";
2424
import { IdfToolsManager } from "../../idfToolsManager";
25+
import { getIdfTargetFromSdkconfig } from "../../workspaceConfig";
2526

2627
export interface IdfBoard {
2728
name: string;
@@ -31,15 +32,11 @@ export interface IdfBoard {
3132
}
3233

3334
export async function getOpenOcdScripts(workspace: Uri): Promise<string> {
34-
const idfPathDir = idfConf.readParameter(
35-
"idf.espIdfPath",
36-
workspace
37-
) as string;
38-
const toolsPath = idfConf.readParameter("idf.toolsPath", workspace) as string;
39-
const userExtraVars = idfConf.readParameter(
40-
"idf.customExtraVars",
41-
workspace
42-
) as { [key: string]: string };
35+
const idfPathDir = readParameter("idf.espIdfPath", workspace) as string;
36+
const toolsPath = readParameter("idf.toolsPath", workspace) as string;
37+
const userExtraVars = readParameter("idf.customExtraVars", workspace) as {
38+
[key: string]: string;
39+
};
4340
const idfToolsManager = await IdfToolsManager.createIdfToolsManager(
4441
idfPathDir
4542
);
@@ -113,3 +110,98 @@ export async function getBoards(
113110
return idfTarget ? filteredDefaultBoards : defaultBoards;
114111
}
115112
}
113+
114+
export async function selectOpenOcdConfigFiles(
115+
workspaceFolder: Uri,
116+
idfTarget?: string
117+
) {
118+
try {
119+
const openOcdScriptsPath = await getOpenOcdScripts(workspaceFolder);
120+
if (!idfTarget) {
121+
idfTarget = await getIdfTargetFromSdkconfig(workspaceFolder);
122+
if (!idfTarget) {
123+
commands.executeCommand("espIdf.setTarget");
124+
return;
125+
}
126+
}
127+
const currentOpenOcdConfigs = readParameter(
128+
"idf.openOcdConfigs",
129+
workspaceFolder
130+
) as string[];
131+
const boards = await getBoards(openOcdScriptsPath, idfTarget);
132+
const choices = boards.map((b) => {
133+
return {
134+
description: `${b.description} (${b.configFiles})`,
135+
label: b.name,
136+
target: b,
137+
picked: currentOpenOcdConfigs
138+
.join(",")
139+
.includes(b.configFiles.join(",")),
140+
};
141+
});
142+
const selectOpenOCdConfigsMsg = l10n.t(
143+
"Enter OpenOCD Configuration File Paths list"
144+
);
145+
const boardQuickPick = window.createQuickPick<{
146+
description: string;
147+
label: string;
148+
target: IdfBoard;
149+
picked: boolean;
150+
}>();
151+
boardQuickPick.items = choices;
152+
boardQuickPick.placeholder = selectOpenOCdConfigsMsg;
153+
boardQuickPick.onDidHide(() => {
154+
boardQuickPick.dispose();
155+
});
156+
boardQuickPick.activeItems = boardQuickPick.items.filter(
157+
(item) => item.picked
158+
);
159+
160+
return new Promise<void>((resolve) => {
161+
boardQuickPick.onDidHide(() => {
162+
boardQuickPick.dispose();
163+
resolve();
164+
});
165+
boardQuickPick.onDidAccept(async () => {
166+
const selectedBoard = boardQuickPick.selectedItems[0];
167+
if (!selectedBoard) {
168+
Logger.infoNotify(
169+
`ESP-IDF board not selected. Remember to set the configuration files for OpenOCD with idf.openOcdConfigs`
170+
);
171+
} else if (selectedBoard && selectedBoard.target) {
172+
if (selectedBoard.label.indexOf("Custom board") !== -1) {
173+
const inputBoard = await window.showInputBox({
174+
placeHolder: "Enter comma-separated configuration files",
175+
value: selectedBoard.target.configFiles.join(","),
176+
});
177+
if (inputBoard) {
178+
selectedBoard.target.configFiles = inputBoard.split(",");
179+
}
180+
}
181+
await writeParameter(
182+
"idf.openOcdConfigs",
183+
selectedBoard.target.configFiles,
184+
ConfigurationTarget.WorkspaceFolder,
185+
workspaceFolder
186+
);
187+
Logger.infoNotify(
188+
l10n.t("OpenOCD Board configuration files are updated.")
189+
);
190+
boardQuickPick.dispose();
191+
resolve();
192+
}
193+
boardQuickPick.hide();
194+
});
195+
boardQuickPick.show();
196+
});
197+
} catch (error) {
198+
const errMsg =
199+
error.message || "Failed to select openOCD configuration files";
200+
Logger.errorNotify(
201+
errMsg,
202+
error,
203+
"boardConfiguration selectOpenOcdConfigFiles"
204+
);
205+
return;
206+
}
207+
}

src/espIdf/setTarget/index.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from "../../idfConfiguration";
3232
import { Logger } from "../../logger/logger";
3333
import { OutputChannel } from "../../logger/outputChannel";
34-
import { getBoards, getOpenOcdScripts } from "../openOcd/boardConfiguration";
34+
import { selectOpenOcdConfigFiles } from "../openOcd/boardConfiguration";
3535
import { getTargetsFromEspIdf } from "./getTargets";
3636
import { setTargetInIDF } from "./setTargetInIdf";
3737
import { updateCurrentProfileIdfTarget } from "../../project-conf";
@@ -74,45 +74,12 @@ export async function setIdfTarget(
7474
if (!selectedTarget) {
7575
return;
7676
}
77-
const openOcdScriptsPath = await getOpenOcdScripts(workspaceFolder.uri);
78-
const boards = await getBoards(
79-
openOcdScriptsPath,
77+
await selectOpenOcdConfigFiles(
78+
workspaceFolder.uri,
8079
selectedTarget.target
8180
);
82-
const choices = boards.map((b) => {
83-
return {
84-
description: `${b.description} (${b.configFiles})`,
85-
label: b.name,
86-
target: b.configFiles,
87-
};
88-
});
89-
const selectedBoard = await window.showQuickPick(choices, {
90-
placeHolder: "Enter OpenOCD Configuration File Paths list",
91-
});
92-
if (!selectedBoard) {
93-
Logger.infoNotify(
94-
`ESP-IDF board not selected. Remember to set the configuration files for OpenOCD with idf.openOcdConfigs`
95-
);
96-
}
97-
await setTargetInIDF(workspaceFolder, selectedTarget);
98-
if (selectedBoard && selectedBoard.target) {
99-
if (selectedBoard.label.indexOf("Custom board") !== -1) {
100-
const inputBoard = await window.showInputBox({
101-
placeHolder: "Enter comma-separated configuration files",
102-
value: selectedBoard.target.join(","),
103-
});
104-
if (inputBoard) {
105-
selectedBoard.target = inputBoard.split(",");
106-
}
107-
}
108-
await writeParameter(
109-
"idf.openOcdConfigs",
110-
selectedBoard.target,
111-
configurationTarget,
112-
workspaceFolder.uri
113-
);
114-
}
11581

82+
await setTargetInIDF(workspaceFolder, selectedTarget);
11683
const customExtraVars = readParameter(
11784
"idf.customExtraVars",
11885
workspaceFolder

src/extension.ts

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ import {
9393
import del from "del";
9494
import { NVSPartitionTable } from "./espIdf/nvs/partitionTable/panel";
9595
import {
96-
getBoards,
9796
getOpenOcdScripts,
97+
selectOpenOcdConfigFiles,
9898
} from "./espIdf/openOcd/boardConfiguration";
9999
import { generateConfigurationReport } from "./support";
100100
import { initializeReportObject } from "./support/initReportObj";
@@ -2294,68 +2294,9 @@ export async function activate(context: vscode.ExtensionContext) {
22942294
return result.trim();
22952295
});
22962296

2297-
registerIDFCommand("espIdf.selectOpenOcdConfigFiles", async () => {
2298-
try {
2299-
const openOcdScriptsPath = await getOpenOcdScripts(workspaceRoot);
2300-
let idfTarget = await getIdfTargetFromSdkconfig(workspaceRoot);
2301-
if (!idfTarget) {
2302-
vscode.commands.executeCommand("espIdf.setTarget");
2303-
return;
2304-
}
2305-
const boards = await getBoards(openOcdScriptsPath, idfTarget);
2306-
const choices = boards.map((b) => {
2307-
return {
2308-
description: `${b.description} (${b.configFiles})`,
2309-
label: b.name,
2310-
target: b,
2311-
};
2312-
});
2313-
const selectOpenOCdConfigsMsg = vscode.l10n.t(
2314-
"Enter OpenOCD Configuration File Paths list"
2315-
);
2316-
const selectedBoard = await vscode.window.showQuickPick(choices, {
2317-
placeHolder: selectOpenOCdConfigsMsg,
2318-
});
2319-
if (!selectedBoard) {
2320-
return;
2321-
}
2322-
2323-
if (selectedBoard.target.name.indexOf("Custom board") !== -1) {
2324-
const inputBoard = await vscode.window.showInputBox({
2325-
placeHolder: vscode.l10n.t(
2326-
"Enter comma-separated configuration files"
2327-
),
2328-
value: selectedBoard.target.configFiles.join(","),
2329-
});
2330-
if (inputBoard) {
2331-
selectedBoard.target.configFiles = inputBoard.split(",");
2332-
}
2333-
}
2334-
2335-
const target = idfConf.readParameter("idf.saveScope");
2336-
if (
2337-
!PreCheck.isWorkspaceFolderOpen() &&
2338-
target !== vscode.ConfigurationTarget.Global
2339-
) {
2340-
const noWsOpenMSg = vscode.l10n.t(`Open a workspace or folder first.`);
2341-
Logger.warnNotify(noWsOpenMSg);
2342-
throw new Error(noWsOpenMSg);
2343-
}
2344-
await idfConf.writeParameter(
2345-
"idf.openOcdConfigs",
2346-
selectedBoard.target.configFiles,
2347-
target
2348-
);
2349-
Logger.infoNotify(
2350-
vscode.l10n.t("OpenOCD Board configuration files are updated.")
2351-
);
2352-
} catch (error) {
2353-
const errMsg =
2354-
error.message || "Failed to select openOCD configuration files";
2355-
Logger.errorNotify(errMsg, error, "extension selectOpenOcdConfigFiles");
2356-
return;
2357-
}
2358-
});
2297+
registerIDFCommand("espIdf.selectOpenOcdConfigFiles", async () =>
2298+
selectOpenOcdConfigFiles(workspaceRoot)
2299+
);
23592300

23602301
registerIDFCommand("espIdf.getOpenOcdScriptValue", async () => {
23612302
return await getOpenOcdScripts(workspaceRoot);

0 commit comments

Comments
 (0)