Skip to content

Commit

Permalink
Simplify stub port selection
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed May 28, 2024
1 parent bc0645e commit 42f498e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
53 changes: 34 additions & 19 deletions src/activator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Stubs, {
fetchAvailableStubsVersions,
installIncludedStubs,
installStubsByVersion,
STUB_PORTS,
stubPortToDisplayString,
} from "./stubs.mjs";
import Settings, { SettingsKey } from "./settings.mjs";
Expand Down Expand Up @@ -1093,38 +1094,52 @@ export default class Activator {
disposable = vscode.commands.registerCommand(
commandPrefix + "extra.switchStubs",
async () => {
const versions: string[] = [];

Object.entries(await fetchAvailableStubsVersions()).forEach(
([key, values]) => {
// Map each value to "key - value" and push to resultArray
versions.push(
...values.map(
value => `${stubPortToDisplayString(key)} - ${value}`
)
);
}
);

// show quick pick
const version = await vscode.window.showQuickPick(
["Included", ...versions],
// let use chose between stub port
const stubPort = await vscode.window.showQuickPick(
["Included", ...STUB_PORTS.map(stubPortToDisplayString)],
{
canPickMany: false,
placeHolder: "Select the stubs version you want to use",
placeHolder: "Select the stubs port you want to use",
ignoreFocusOut: false,
}
);

if (version === undefined) {
if (stubPort === undefined) {
return;
}

if (version.toLowerCase() === "included") {
if (stubPort.toLowerCase() === "included") {
await installIncludedStubs(settings);

void vscode.window.showInformationMessage("Included stubs selected.");
} else {
const availableStubVersions = await fetchAvailableStubsVersions(
stubPort
);
const versions: string[] = [];

Object.entries(availableStubVersions).forEach(([key, values]) => {
// Map each value to "key - value" and push to resultArray
versions.push(
...values.map(value =>
Object.keys(availableStubVersions).length > 1
? `${stubPortToDisplayString(key)} - ${value}`
: value
)
);
});

// show quick pick
const version = await vscode.window.showQuickPick(versions, {
canPickMany: false,
placeHolder: "Select the stubs version you want to use",
ignoreFocusOut: false,
});

if (version === undefined) {
return;
}

await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
Expand Down
20 changes: 14 additions & 6 deletions src/stubs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ enum StubPorts {
esp32s3 = "micropython-esp32-esp32_generic_s3-stubs",
}

const STUB_PORTS: string[] = [
export const STUB_PORTS: string[] = [
StubPorts.picoW,
StubPorts.pico,
StubPorts.esp32,
Expand Down Expand Up @@ -349,16 +349,24 @@ export async function installStubsByVersion(
return false;
}

// TODO: support for other stubs distributions
export async function fetchAvailableStubsVersions(): Promise<{
export async function fetchAvailableStubsVersions(
displayPort?: string
): Promise<{
[key: string]: string[];
}> {
const versions: { [key: string]: string[] } = {};

for (const port of STUB_PORTS) {
const stubsVersions = await fetchAvailableStubsVersionsForPort(port);
if (displayPort !== undefined) {
const stubPort = displayStringToStubPort(displayPort);
const stubsVersions = await fetchAvailableStubsVersionsForPort(stubPort);

versions[port] = stubsVersions.reverse();
versions[stubPort] = stubsVersions.reverse();
} else {
for (const port of STUB_PORTS) {
const stubsVersions = await fetchAvailableStubsVersionsForPort(port);

versions[port] = stubsVersions.reverse();
}
}

return versions;
Expand Down

0 comments on commit 42f498e

Please sign in to comment.