Skip to content

Commit 4a0b2d7

Browse files
rob's feedback
1 parent 2bf0340 commit 4a0b2d7

File tree

5 files changed

+73
-51
lines changed

5 files changed

+73
-51
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"@types/glob": "^7.1.2",
5959
"@types/mocha": "~7.0.2",
6060
"@types/mock-fs": "~4.10.0",
61-
"@types/node": "~14.0.1",
61+
"@types/node": "~14.0.22",
6262
"@types/node-fetch": "~2.5.7",
6363
"@types/rewire": "~2.5.28",
6464
"@types/semver": "~7.2.0",
@@ -75,7 +75,7 @@
7575
"typescript": "~3.9.3",
7676
"vsce": "~1.77.0",
7777
"vscode-test": "~1.4.0",
78-
"vscode-dts": "^0.3.1"
78+
"vscode-dts": "~0.3.1"
7979
},
8080
"extensionDependencies": [
8181
"vscode.powershell"

src/features/GetCommands.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ class CommandsExplorerProvider implements vscode.TreeDataProvider<Command> {
9393
}
9494

9595
public refresh(): void {
96-
// we pass in null because no event data in needed.
97-
this.didChangeTreeData.fire(null);
96+
this.didChangeTreeData.fire();
9897
}
9998

10099
public getTreeItem(element: Command): vscode.TreeItem {

src/features/PowerShellNotebooks.ts

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
1515
private languageClient: LanguageClient;
1616

1717
private _onDidChangeNotebook = new vscode.EventEmitter<vscode.NotebookDocumentEditEvent>();
18-
onDidChangeNotebook: vscode.Event<vscode.NotebookDocumentEditEvent> = this._onDidChangeNotebook.event;
19-
kernel?: vscode.NotebookKernel;
18+
public onDidChangeNotebook: vscode.Event<vscode.NotebookDocumentEditEvent> = this._onDidChangeNotebook.event;
19+
public kernel?: vscode.NotebookKernel;
2020

21-
constructor() {
21+
public label: string = 'PowerShell';
22+
public preloads?: vscode.Uri[];
23+
24+
public constructor() {
25+
// VS Code Notebook API uses this property for handling cell execution.
2226
this.kernel = this;
23-
this.showNotebookModeCommand = vscode.commands.registerCommand("PowerShell.ShowNotebookMode", async () => {
24-
const uri = vscode.window.activeTextEditor.document.uri;
25-
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
26-
await vscode.commands.executeCommand("vscode.openWith", uri, "PowerShellNotebookMode");
27-
});
2827

29-
this.hideNotebookModeCommand = vscode.commands.registerCommand("PowerShell.HideNotebookMode", async () => {
30-
const uri = vscode.notebook.activeNotebookEditor.document.uri;
31-
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
32-
await vscode.commands.executeCommand("vscode.openWith", uri, "default");
33-
});
34-
}
28+
this.showNotebookModeCommand = vscode.commands.registerCommand(
29+
"PowerShell.ShowNotebookMode",
30+
PowerShellNotebooksFeature.showNotebookMode);
3531

36-
label: string = 'PowerShell';
37-
preloads?: vscode.Uri[];
32+
this.hideNotebookModeCommand = vscode.commands.registerCommand(
33+
"PowerShell.HideNotebookMode",
34+
PowerShellNotebooksFeature.hideNotebookMode);
35+
}
3836

39-
async openNotebook(uri: vscode.Uri, context: vscode.NotebookDocumentOpenContext): Promise<vscode.NotebookData> {
37+
public async openNotebook(uri: vscode.Uri, context: vscode.NotebookDocumentOpenContext): Promise<vscode.NotebookData> {
4038
// load from backup if needed.
4139
const actualUri = context.backupId ? vscode.Uri.parse(context.backupId) : uri;
4240

@@ -49,43 +47,49 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
4947
metadata: {}
5048
}
5149

52-
let curr: string[] = [];
50+
let currentCellSource: string[] = [];
5351
let cellKind: vscode.CellKind | undefined;
5452
let insideBlockComment: boolean = false;
5553

54+
// Iterate through all lines in a document (aka ps1 file) and group the lines
55+
// into cells (markdown or code) that will be rendered in Notebook mode.
5656
// tslint:disable-next-line: prefer-for-of
5757
for (let i = 0; i < lines.length; i++) {
5858
// Handle block comments
5959
if (insideBlockComment) {
6060
if (lines[i] === "#>") {
61+
// We've reached the end of a block comment,
62+
// push a markdown cell.
6163
insideBlockComment = false;
6264

6365
notebookData.cells.push({
6466
cellKind: vscode.CellKind.Markdown,
6567
language: "markdown",
6668
outputs: [],
67-
source: curr.join("\n"),
69+
source: currentCellSource.join("\n"),
6870
metadata: {
6971
custom: {
7072
commentType: CommentType.BlockComment
7173
}
7274
}
7375
});
7476

75-
curr = [];
77+
currentCellSource = [];
7678
cellKind = undefined;
7779
continue;
78-
} else {
79-
curr.push(lines[i]);
80-
continue;
8180
}
81+
82+
// If we're still in a block comment, push the line and continue.
83+
currentCellSource.push(lines[i]);
84+
continue;
8285
} else if (lines[i] === "<#") {
83-
// Insert what we saw leading up to this.
86+
// If we found the start of a block comment,
87+
// insert what we saw leading up to this.
8488
notebookData.cells.push({
8589
cellKind: cellKind!,
8690
language: cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell',
8791
outputs: [],
88-
source: curr.join("\n"),
92+
source: currentCellSource.join("\n"),
8993
metadata: {
9094
custom: {
9195
commentType: cellKind === vscode.CellKind.Markdown ? CommentType.LineComment : CommentType.Disabled,
@@ -94,7 +98,7 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
9498
});
9599

96100
// reset state because we're starting a new Markdown cell.
97-
curr = [];
101+
currentCellSource = [];
98102
cellKind = vscode.CellKind.Markdown;
99103
insideBlockComment = true;
100104
continue;
@@ -104,17 +108,17 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
104108
// If a line starts with # it's a comment
105109
const kind: vscode.CellKind = lines[i].startsWith("#") ? vscode.CellKind.Markdown : vscode.CellKind.Code;
106110

107-
// If this line is a continuation of the previous cell type, then add this line to curr.
111+
// If this line is a continuation of the previous cell type, then add this line to the current cell source.
108112
if (kind === cellKind) {
109-
curr.push(kind === vscode.CellKind.Markdown && !insideBlockComment ? lines[i].replace(/^\#\s*/, '') : lines[i]);
113+
currentCellSource.push(kind === vscode.CellKind.Markdown && !insideBlockComment ? lines[i].replace(/^\#\s*/, '') : lines[i]);
110114
} else {
111-
// If cellKind is not undifined, then we can add the cell we've just computed to the editBuilder.
115+
// If cellKind is not undifined, then we can add the cell we've just computed.
112116
if (cellKind !== undefined) {
113117
notebookData.cells.push({
114118
cellKind: cellKind!,
115119
language: cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell',
116120
outputs: [],
117-
source: curr.join("\n"),
121+
source: currentCellSource.join("\n"),
118122
metadata: {
119123
custom: {
120124
commentType: cellKind === vscode.CellKind.Markdown ? CommentType.LineComment : CommentType.Disabled,
@@ -124,18 +128,21 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
124128
}
125129

126130
// set initial new cell state
127-
curr = [];
131+
currentCellSource = [];
128132
cellKind = kind;
129-
curr.push(kind === vscode.CellKind.Markdown ? lines[i].replace(/^\#\s*/, '') : lines[i]);
133+
currentCellSource.push(kind === vscode.CellKind.Markdown ? lines[i].replace(/^\#\s*/, '') : lines[i]);
130134
}
131135
}
132136

133-
if (curr.length) {
137+
// If we have some leftover lines that have not been added (for example,
138+
// when there is only the _start_ of a block comment but not an _end_.)
139+
// add the appropriate cell.
140+
if (currentCellSource.length) {
134141
notebookData.cells.push({
135142
cellKind: cellKind!,
136143
language: cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell',
137144
outputs: [],
138-
source: curr.join("\n"),
145+
source: currentCellSource.join("\n"),
139146
metadata: {
140147
custom: {
141148
commentType: cellKind === vscode.CellKind.Markdown ? CommentType.LineComment : CommentType.Disabled,
@@ -147,19 +154,20 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
147154
return notebookData;
148155
}
149156

150-
resolveNotebook(document: vscode.NotebookDocument, webview: { readonly onDidReceiveMessage: vscode.Event<any>; postMessage(message: any): Thenable<boolean>; asWebviewUri(localResource: vscode.Uri): vscode.Uri; }): Promise<void> {
157+
public resolveNotebook(document: vscode.NotebookDocument, webview: { readonly onDidReceiveMessage: vscode.Event<any>; postMessage(message: any): Thenable<boolean>; asWebviewUri(localResource: vscode.Uri): vscode.Uri; }): Promise<void> {
158+
// We don't need to do anything here because our Notebooks are backed by files.
151159
return;
152160
}
153161

154-
saveNotebook(document: vscode.NotebookDocument, cancellation: vscode.CancellationToken): Promise<void> {
162+
public saveNotebook(document: vscode.NotebookDocument, cancellation: vscode.CancellationToken): Promise<void> {
155163
return this._save(document, document.uri, cancellation);
156164
}
157165

158-
saveNotebookAs(targetResource: vscode.Uri, document: vscode.NotebookDocument, cancellation: vscode.CancellationToken): Promise<void> {
166+
public saveNotebookAs(targetResource: vscode.Uri, document: vscode.NotebookDocument, cancellation: vscode.CancellationToken): Promise<void> {
159167
return this._save(document, targetResource, cancellation);
160168
}
161169

162-
async backupNotebook(document: vscode.NotebookDocument, context: vscode.NotebookDocumentBackupContext, cancellation: vscode.CancellationToken): Promise<vscode.NotebookDocumentBackup> {
170+
public async backupNotebook(document: vscode.NotebookDocument, context: vscode.NotebookDocumentBackupContext, cancellation: vscode.CancellationToken): Promise<vscode.NotebookDocumentBackup> {
163171
await this._save(document, context.destination, cancellation);
164172

165173
return {
@@ -179,9 +187,9 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
179187
this.languageClient = languageClient;
180188
}
181189

182-
async _save(document: vscode.NotebookDocument, targetResource: vscode.Uri, _token: vscode.CancellationToken): Promise<void> {
190+
private async _save(document: vscode.NotebookDocument, targetResource: vscode.Uri, _token: vscode.CancellationToken): Promise<void> {
183191
const retArr: string[] = [];
184-
document.cells.forEach((cell) => {
192+
for (const cell of document.cells) {
185193
if (cell.cellKind === vscode.CellKind.Code) {
186194
retArr.push(...cell.document.getText().split(/\r|\n|\r\n/));
187195
} else {
@@ -197,18 +205,33 @@ export class PowerShellNotebooksFeature implements vscode.NotebookContentProvide
197205
retArr.push(...cell.document.getText().split(/\r|\n|\r\n/).map(line => `# ${line}`));
198206
}
199207
}
200-
});
208+
}
201209

202210
await vscode.workspace.fs.writeFile(targetResource, new TextEncoder().encode(retArr.join('\n')));
203211
}
204212

205-
async executeAllCells(document: vscode.NotebookDocument, token: vscode.CancellationToken): Promise<void> {
213+
private static async showNotebookMode() {
214+
const uri = vscode.window.activeTextEditor.document.uri;
215+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
216+
await vscode.commands.executeCommand("vscode.openWith", uri, "PowerShellNotebookMode");
217+
}
218+
219+
private static async hideNotebookMode() {
220+
const uri = vscode.notebook.activeNotebookEditor.document.uri;
221+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
222+
await vscode.commands.executeCommand("vscode.openWith", uri, "default");
223+
}
224+
225+
/*
226+
`vscode.NotebookKernel` implementations
227+
*/
228+
public async executeAllCells(document: vscode.NotebookDocument, token: vscode.CancellationToken): Promise<void> {
206229
for (const cell of document.cells) {
207230
await this.executeCell(document, cell, token);
208231
}
209232
}
210233

211-
async executeCell(document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined, token: vscode.CancellationToken): Promise<void> {
234+
public async executeCell(document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined, token: vscode.CancellationToken): Promise<void> {
212235
if (token.isCancellationRequested) {
213236
return;
214237
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function activate(context: vscode.ExtensionContext): void {
169169
context.subscriptions.push(vscode.notebook.registerNotebookContentProvider('PowerShellNotebookMode', powerShellNotebooksFeature));
170170
extensionFeatures.push(powerShellNotebooksFeature);
171171
} catch (e) {
172-
// This would happen in VS Code changes their API.
172+
// This would happen if VS Code changes their API.
173173
powerShellNotebooksFeature.dispose();
174174
logger.writeVerbose("Failed to register NotebookContentProvider", e);
175175
}

0 commit comments

Comments
 (0)