Skip to content

Commit 52e866c

Browse files
committed
Added basic MicroPython project creation interop
Signed-off-by: paulober <[email protected]>
1 parent 3524eb1 commit 52e866c

File tree

7 files changed

+854
-13
lines changed

7 files changed

+854
-13
lines changed

src/commands/newProject.mts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import { Command } from "./command.mjs";
22
import Logger from "../logger.mjs";
3-
import { type Uri } from "vscode";
3+
import { window, type Uri } from "vscode";
44
import { NewProjectPanel } from "../webview/newProjectPanel.mjs";
5+
import { NewMicroPythonProjectPanel } from "../webview/newMicroPythonProjectPanel.mjs";
6+
7+
/**
8+
* Enum for the language of the project.
9+
* Can be used to specify the language of the project
10+
* in the `NewProjectCommand` class.
11+
*/
12+
export enum ProjectLang {
13+
cCpp = 1,
14+
micropython = 2,
15+
}
516

617
export default class NewProjectCommand extends Command {
718
private readonly _logger: Logger = new Logger("NewProjectCommand");
819
private readonly _extensionUri: Uri;
20+
private static readonly micropythonOption = "MicroPython";
21+
private static readonly cCppOption = "C/C++";
922

1023
public static readonly id = "newProject";
1124

@@ -15,8 +28,38 @@ export default class NewProjectCommand extends Command {
1528
this._extensionUri = extensionUri;
1629
}
1730

18-
execute(): void {
19-
// show webview where the process of creating a new project is continued
20-
NewProjectPanel.createOrShow(this._extensionUri);
31+
private preSelectedTypeToStr(preSelectedType?: number): string | undefined {
32+
return preSelectedType === ProjectLang.cCpp
33+
? NewProjectCommand.cCppOption
34+
: preSelectedType === ProjectLang.micropython
35+
? NewProjectCommand.micropythonOption
36+
: undefined;
37+
}
38+
39+
async execute(preSelectedType?: number): Promise<void> {
40+
// ask the user what language to use
41+
const lang =
42+
this.preSelectedTypeToStr(preSelectedType) ??
43+
(await window.showQuickPick(
44+
[NewProjectCommand.cCppOption, NewProjectCommand.micropythonOption],
45+
{
46+
placeHolder: "Select which language to use for your new project",
47+
canPickMany: false,
48+
ignoreFocusOut: false,
49+
title: "New Pico Project",
50+
}
51+
));
52+
53+
if (lang === undefined) {
54+
return;
55+
}
56+
57+
if (lang === NewProjectCommand.micropythonOption) {
58+
// create a new project with MicroPython
59+
NewMicroPythonProjectPanel.createOrShow(this._extensionUri);
60+
} else {
61+
// show webview where the process of creating a new project is continued
62+
NewProjectPanel.createOrShow(this._extensionUri);
63+
}
2164
}
2265
}

src/extension.mts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import NewExampleProjectCommand from "./commands/newExampleProject.mjs";
7474
import SwitchBoardCommand from "./commands/switchBoard.mjs";
7575
import UninstallPicoSDKCommand from "./commands/uninstallPicoSDK.mjs";
7676
import FlashProjectSWDCommand from "./commands/flashProjectSwd.mjs";
77+
import { NewMicroPythonProjectPanel } from "./webview/newMicroPythonProjectPanel.mjs";
7778

7879
export async function activate(context: ExtensionContext): Promise<void> {
7980
Logger.info(LoggerSource.extension, "Extension activation triggered");
@@ -144,6 +145,17 @@ export async function activate(context: ExtensionContext): Promise<void> {
144145
})
145146
);
146147

148+
context.subscriptions.push(
149+
window.registerWebviewPanelSerializer(NewMicroPythonProjectPanel.viewType, {
150+
// eslint-disable-next-line @typescript-eslint/require-await
151+
async deserializeWebviewPanel(webviewPanel: WebviewPanel): Promise<void> {
152+
// Reset the webview options so we use latest uri for `localResourceRoots`.
153+
webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
154+
NewMicroPythonProjectPanel.revive(webviewPanel, context.extensionUri);
155+
},
156+
})
157+
);
158+
147159
context.subscriptions.push(
148160
window.registerTreeDataProvider(
149161
PicoProjectActivityBar.viewType,

src/webview/activityBar.mts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "vscode";
1010
import Logger from "../logger.mjs";
1111
import { extensionName } from "../commands/command.mjs";
12-
import NewProjectCommand from "../commands/newProject.mjs";
12+
import NewProjectCommand, { ProjectLang } from "../commands/newProject.mjs";
1313
import CompileProjectCommand from "../commands/compileProject.mjs";
1414
import RunProjectCommand from "../commands/runProject.mjs";
1515
import SwitchSDKCommand from "../commands/switchSDK.mjs";
@@ -39,7 +39,8 @@ const COMMON_COMMANDS_PARENT_LABEL = "General";
3939
const PROJECT_COMMANDS_PARENT_LABEL = "Project";
4040
const DOCUMENTATION_COMMANDS_PARENT_LABEL = "Documentation";
4141

42-
const NEW_PROJECT_LABEL = "New Project";
42+
const NEW_C_CPP_PROJECT_LABEL = "New C/C++ Project";
43+
const NEW_MICROPYTHON_PROJECT_LABEL = "New MicroPython Project";
4344
const IMPORT_PROJECT_LABEL = "Import Project";
4445
const EXAMPLE_PROJECT_LABEL = "New Project From Example";
4546
const SWITCH_SDK_LABEL = "Switch SDK";
@@ -78,10 +79,13 @@ export class PicoProjectActivityBar
7879
element: QuickAccessCommand
7980
): TreeItem | Thenable<TreeItem> {
8081
switch (element.label) {
81-
case NEW_PROJECT_LABEL:
82+
case NEW_C_CPP_PROJECT_LABEL:
8283
// alt. "new-folder"
8384
element.iconPath = new ThemeIcon("file-directory-create");
8485
break;
86+
case NEW_MICROPYTHON_PROJECT_LABEL:
87+
element.iconPath = new ThemeIcon("file-directory-create");
88+
break;
8589
case IMPORT_PROJECT_LABEL:
8690
// alt. "repo-pull"
8791
element.iconPath = new ThemeIcon("repo-clone");
@@ -158,11 +162,21 @@ export class PicoProjectActivityBar
158162
} else if (element.label === COMMON_COMMANDS_PARENT_LABEL) {
159163
return [
160164
new QuickAccessCommand(
161-
NEW_PROJECT_LABEL,
165+
NEW_C_CPP_PROJECT_LABEL,
166+
TreeItemCollapsibleState.None,
167+
{
168+
command: `${extensionName}.${NewProjectCommand.id}`,
169+
title: NEW_C_CPP_PROJECT_LABEL,
170+
arguments: [ProjectLang.cCpp],
171+
}
172+
),
173+
new QuickAccessCommand(
174+
NEW_MICROPYTHON_PROJECT_LABEL,
162175
TreeItemCollapsibleState.None,
163176
{
164177
command: `${extensionName}.${NewProjectCommand.id}`,
165-
title: NEW_PROJECT_LABEL,
178+
title: NEW_MICROPYTHON_PROJECT_LABEL,
179+
arguments: [ProjectLang.micropython],
166180
}
167181
),
168182
new QuickAccessCommand(

0 commit comments

Comments
 (0)