Last active
April 10, 2020 18:24
-
-
Save rebornix/67c48cd8ae5b83575b8a59cf3292868c to your computer and use it in GitHub Desktop.
Powershell Notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as vscode from 'vscode'; | |
class PSNB implements vscode.NotebookProvider { | |
constructor() {} | |
async resolveNotebook(editor: vscode.NotebookEditor): Promise<void> { | |
editor.document.languages = ['powershell']; | |
const uri = editor.document.uri; | |
const data = (await vscode.workspace.fs.readFile(uri)).toString(); | |
const lines = data.split(/\r|\n|\r\n/g); | |
await editor.edit(editBuilder => { | |
let curr: string[] = []; | |
let cellKind: vscode.CellKind | undefined; | |
for (let i = 0; i < lines.length; i++) { | |
const kind = /^\#/.exec(lines[i]) === null ? vscode.CellKind.Code : vscode.CellKind.Markdown; | |
if (kind === cellKind) { | |
curr.push(kind === vscode.CellKind.Markdown ? lines[i].replace(/^\#\s*/, '') : lines[i]); | |
} else { | |
// new cell | |
if (cellKind !== undefined) { | |
editBuilder.insert(0, curr, cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell', cellKind!, [], undefined) | |
} | |
curr = []; | |
cellKind = kind; | |
curr.push(kind === vscode.CellKind.Markdown ? lines[i].replace(/^\#/, '') : lines[i]); | |
} | |
} | |
if (curr.length) { | |
editBuilder.insert(0, curr, cellKind === vscode.CellKind.Markdown ? 'markdown' : 'powershell', cellKind!, [], undefined) | |
} | |
}); | |
return; | |
} | |
async executeCell(document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined, token: vscode.CancellationToken): Promise<void> { | |
throw new Error("Method not implemented."); | |
} | |
async save(document: vscode.NotebookDocument): Promise<boolean> { | |
const uri = document.uri; | |
const retArr: string[] = []; | |
document.cells.forEach(cell => { | |
if (cell.cellKind === vscode.CellKind.Code) { | |
retArr.push(...cell.source.split(/\r|\n|\r\n/)); | |
} else { | |
retArr.push(...cell.source.split(/\r|\n|\r\n/).map(line => `# ${line}`)); | |
} | |
}); | |
try { | |
await vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(retArr.join('\n'))) | |
} catch (e) { | |
return false; | |
} | |
return true; | |
} | |
} | |
export function activate(context: vscode.ExtensionContext) { | |
context.subscriptions.push(vscode.notebook.registerNotebookProvider('psnb', new PSNB())); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "psnb", | |
"displayName": "psnb", | |
"description": "", | |
"version": "0.0.1", | |
"enableProposedApi": true, | |
"engines": { | |
"vscode": "^1.44.0" | |
}, | |
"categories": [ | |
"Other" | |
], | |
"activationEvents": [ | |
"onNotebookEditor:psnb" | |
], | |
"main": "./out/extension.js", | |
"contributes": { | |
"notebookProvider": [ | |
{ | |
"viewType": "psnb", | |
"displayName": "Powershell Notebook", | |
"selector": [ | |
{ | |
"filenamePattern": "*.ps1" | |
} | |
] | |
} | |
] | |
}, | |
"scripts": { | |
"vscode:prepublish": "npm run compile", | |
"compile": "tsc -p ./", | |
"lint": "eslint src --ext ts", | |
"watch": "tsc -watch -p ./", | |
"pretest": "npm run compile && npm run lint", | |
"test": "node ./out/test/runTest.js", | |
"download-api": "vscode-dts dev", | |
"postdownload-api": "vscode-dts master", | |
"postinstall": "npm run download-api" | |
}, | |
"devDependencies": { | |
"@types/glob": "^7.1.1", | |
"@types/mocha": "^7.0.1", | |
"@types/node": "^12.11.7", | |
"@types/vscode": "^1.44.0", | |
"eslint": "^6.8.0", | |
"@typescript-eslint/parser": "^2.18.0", | |
"@typescript-eslint/eslint-plugin": "^2.18.0", | |
"glob": "^7.1.6", | |
"mocha": "^7.0.1", | |
"typescript": "^3.7.5", | |
"vscode-test": "^1.3.0", | |
"vscode-dts": "^0.3.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment