Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment change and autocomplete for Principal type #298

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,32 @@
"title": "Motoko: Restart language server"
},
{
"command": "motoko.deployPlayground",
"command": "motoko.deployPlaygroundSelection",
"title": "Motoko: Deploy (20 minutes)"
},
{
"command": "motoko.importMopsPackage",
"title": "Motoko: Import Mops Package..."
},
{
"command": "motoko.startReplica",
"title": "Motoko: Start local replica"
},
{
"command": "motoko.stopReplica",
"title": "Motoko: Stop local replica"
},
{
"command": "motoko.deployLocalSelection",
"title": "Motoko: Deploy (local replica)..."
},
{
"command": "motoko.candidLocalSelection",
"title": "Motoko: Open Candid UI for canister (local replica)..."
},
{
"command": "motoko.candidPlaygroundSelection",
"title": "Motoko: Open Candid UI for canister (playground)..."
}
],
"jsonValidation": [
Expand All @@ -149,14 +169,64 @@
"explorer/context": [
{
"when": "resourceLangId == motoko",
"command": "motoko.deployPlayground",
"command": "motoko.startReplica",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.stopReplica",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.deployPlaygroundSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.candidPlaygroundSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.deployLocalSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.candidLocalSelection",
"group": "2_main"
}
],
"editor/context": [
{
"when": "resourceLangId == motoko",
"command": "motoko.deployPlayground",
"command": "motoko.startReplica",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.stopReplica",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.deployPlaygroundSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.candidPlaygroundSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.deployLocalSelection",
"group": "2_main"
},
{
"when": "resourceLangId == motoko",
"command": "motoko.candidLocalSelection",
"group": "2_main"
}
]
Expand Down
5 changes: 5 additions & 0 deletions src/common/connectionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export const TEST_FILE_REQUEST = new RequestType<TestParams, TestResult, any>(
'vscode-motoko/run-test-file',
);

export enum ENVIRONMENT {
LOCAL = 'local',
PLAYGROUND = 'playground',
}

export interface TestParams {
uri: string;
}
Expand Down
211 changes: 197 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,134 @@ import {
IMPORT_MOPS_PACKAGE,
TestParams,
TestResult,
ENVIRONMENT,
} from './common/connectionTypes';
import { ignoreGlobPatterns, watchGlob } from './common/watchConfig';
import { formatDocument } from './formatter';
import { startReplica, stopReplica } from './server/replicaManager';
import {
getCanisterNames,
getCanisterNamesForCandid,
} from './server/canisterNames';
import { resolveCandidUIAddress } from './server/candidAddressProvider';
import { TerminalProvider } from './server/terminalProvider';

const config = workspace.getConfiguration('motoko');

let client: LanguageClient;

export function activate(context: ExtensionContext) {
const outputChannel = vscode.window.createOutputChannel('ICP logs');
const terminalProvider = new TerminalProvider();
context.subscriptions.push(outputChannel);
context.subscriptions.push(
commands.registerCommand('motoko.startService', () =>
startServer(context),
),
);
// context.subscriptions.push(
// commands.registerCommand(
// 'motoko.deployPlayground',
// async (relevantUri?: Uri) => {
// const uri =
// relevantUri?.toString() ||
// window.activeTextEditor?.document?.uri.toString();
// if (!uri || !uri.endsWith('.mo')) {
// window.showErrorMessage(
// 'Invalid deploy URI:',
// uri ?? `(${uri})`,
// );
// return;
// }
// await deployPlayground(context, uri);
// },
// ),
// );
context.subscriptions.push(
commands.registerCommand('motoko.importMopsPackage', async () => {
await importMopsPackage(context);
}),
);
context.subscriptions.push(
commands.registerCommand('motoko.startReplica', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could also include a motoko.stopReplica command for completeness?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i will include it

await startReplica(terminalProvider.get());
}),
);
context.subscriptions.push(
commands.registerCommand('motoko.stopReplica', async () => {
await stopReplica(terminalProvider.get());
}),
);
context.subscriptions.push(
commands.registerCommand('motoko.deployLocalSelection', async () => {
await showLocalCanisterDeployOptions();
}),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.deployLocal',
async (canisterName?: string) => {
await runCommand(
`dfx deploy ${canisterName ? canisterName : ''}`,
terminalProvider.get(),
);
},
),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.deployPlaygroundSelection',
async () => {
await showPlaygroundCanisterDeployOptions();
},
),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.deployPlayground',
async (relevantUri?: Uri) => {
const uri =
relevantUri?.toString() ||
window.activeTextEditor?.document?.uri.toString();
if (!uri || !uri.endsWith('.mo')) {
window.showErrorMessage(
'Invalid deploy URI:',
uri ?? `(${uri})`,
);
return;
}
await deployPlayground(context, uri);
async (canisterName?: string) => {
await runCommand(
`dfx canister create ${
canisterName ? canisterName : '--all'
} &&
dfx build ${canisterName || ''} &&
dfx deploy ${canisterName || ''} --playground`,
terminalProvider.get(),
);
},
),
);

context.subscriptions.push(
commands.registerCommand('motoko.importMopsPackage', async () => {
await importMopsPackage(context);
commands.registerCommand('motoko.candidLocalSelection', async () => {
await showLocalCanisterCandidOptions();
}),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.candidLocal',
async (canisterName: string) => {
await openCandid(context, ENVIRONMENT.LOCAL, canisterName);
},
),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.candidPlaygroundSelection',
async () => {
await showPlaygroundCanisterCandidOptions();
},
),
);
context.subscriptions.push(
commands.registerCommand(
'motoko.candidPlayground',
async (canisterName: string) => {
await openCandid(context, ENVIRONMENT.PLAYGROUND, canisterName);
},
),
);

context.subscriptions.push(
languages.registerDocumentFormattingEditProvider(['motoko', 'candid'], {
provideDocumentFormattingEdits(
Expand Down Expand Up @@ -102,6 +193,48 @@ export function activate(context: ExtensionContext) {
setupTests(context);
}

async function showLocalCanisterDeployOptions() {
const options = await getCanisterNames();
const selection = await vscode.window.showQuickPick(options, {
placeHolder: 'Select canister to deploy',
});

if (selection === options[0]) {
vscode.commands.executeCommand('motoko.deployLocal');
} else {
vscode.commands.executeCommand('motoko.deployLocal', selection);
}
}

async function showPlaygroundCanisterDeployOptions() {
const options = await getCanisterNames();
const selection = await vscode.window.showQuickPick(options, {
placeHolder: 'Select canister to deploy',
});

if (selection === options[0]) {
vscode.commands.executeCommand('motoko.deployPlayground');
} else {
vscode.commands.executeCommand('motoko.deployPlayground', selection);
}
}

async function showLocalCanisterCandidOptions() {
const options = await getCanisterNamesForCandid(ENVIRONMENT.LOCAL);
const selection = await vscode.window.showQuickPick(options, {
placeHolder: 'Select canister to view in Candid UI',
});
vscode.commands.executeCommand('motoko.candidLocal', selection);
}

async function showPlaygroundCanisterCandidOptions() {
const options = await getCanisterNamesForCandid(ENVIRONMENT.PLAYGROUND);
const selection = await vscode.window.showQuickPick(options, {
placeHolder: 'Select canister to view in Candid UI',
});
vscode.commands.executeCommand('motoko.candidPlayground', selection);
}

export async function deactivate() {
if (client) {
await client.stop();
Expand Down Expand Up @@ -403,6 +536,49 @@ const deployingSet = new Set<string>();
const deployPanelMap = new Map<string, vscode.WebviewPanel>();
let tag = Math.floor(Math.random() * 1e12);

async function openCandid(
_context: ExtensionContext,
env: ENVIRONMENT,
canisterName: string,
) {
const candidUIAddress = await resolveCandidUIAddress(
env,
canisterName,
tag,
);
if (!candidUIAddress) {
return;
}
try {
const key = env + canisterName;
let panel = deployPanelMap.get(key);
if (!panel) {
panel = window.createWebviewPanel(
'candid-ui',
'Candid UI',
ViewColumn.Beside,
{
enableScripts: true,
retainContextWhenHidden: true,
},
);
deployPanelMap.set(key, panel);
panel.onDidDispose(() => deployPanelMap.delete(key));
}
panel.webview.html = `
<iframe
src="${candidUIAddress}"
style="width:100vw; height:100vh; border:none"
/>`;
} catch (err: any) {
window.showErrorMessage(
err?.message
? String(err.message)
: 'Unexpected error while deploying canister',
);
}
}

async function deployPlayground(_context: ExtensionContext, uri: string) {
try {
if (deployingSet.has(uri)) {
Expand Down Expand Up @@ -547,3 +723,10 @@ async function importMopsPackage(_context: ExtensionContext) {

await loadInitial();
}

async function runCommand(command: string, terminal: vscode.Terminal) {
terminal.show();
terminal.show();
terminal.sendText(command);
return true;
}
Loading
Loading