Skip to content

Instantly share code, notes, and snippets.

@FWeinb
Created June 20, 2019 15:54
Show Gist options
  • Save FWeinb/eedb824fdff6830e8466c836f7fe6f5b to your computer and use it in GitHub Desktop.
Save FWeinb/eedb824fdff6830e8466c836f7fe6f5b to your computer and use it in GitHub Desktop.
Quick and dirty poc
import * as vscode from 'vscode'
import { DevtoolsPanel } from './DevtoolsPanel'
import { log } from './utils/Logger'
import * as path from 'path'
const DevtoolBackend = require('overmind-devtools-client/DevtoolBackend')
// !!!This is really bad!!!
const findActionRegex = /^\s*export (?:const|let)\s([^:]+)\s*?:\s*?Action(?:<[^>]*?>)?\s*?=/g
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
const storage = {
get(key: string) {
return new Promise((resolve) => {
resolve(context.workspaceState.get(key))
})
},
set(key: string, value: any) {
return new Promise((resolve) => {
context.workspaceState.update(key, value)
resolve()
})
},
clear() {
return new Promise((resolve) => {
resolve()
})
},
}
const devtoolBackend = DevtoolBackend.create({
onRelaunch() {
devtoolBackend.close()
startDevtools()
},
storage,
})
const devtoolsPanel = new DevtoolsPanel({
context,
onMessage: (command, text) => {
switch (command) {
case 'newPort':
storage.set('overmind-devtools.port', text)
startDevtools()
break
case 'restart':
devtoolBackend.close()
startDevtools()
break
}
},
onDispose() {
devtoolBackend.close()
},
})
function startDevtools() {
const port: number =
vscode.workspace.getConfiguration().get('overmind-devtools.port') || 3031
devtoolBackend
.connect(port)
.then(() => {
let scriptFile: vscode.Uri | string
// TODO consider https://github.com/microsoft/vscode/commit/fbcdb4c6a736ff38e60a7e9d0a3f1be5ccbaf3af ?
if (process.env.VSCODE_DEBUG_MODE) {
scriptFile = 'http://localhost:8080/bundle.js'
} else {
const onDiskPath = vscode.Uri.file(
path.join(context.extensionPath, 'devtoolsDist', 'bundle.js')
)
scriptFile = onDiskPath.with({ scheme: 'vscode-resource' })
}
devtoolsPanel.show(devtoolBackend.getMarkup(scriptFile, port))
})
.catch(() => {
devtoolsPanel.show(
devtoolBackend.getChangePortMarkup(
port,
function onNewPortSubmit(newPort: string) {
// @ts-ignore
const vscode = (window.vscode =
// @ts-ignore
window.vscode || acquireVsCodeApi())
vscode.postMessage({
command: 'newPort',
text: newPort,
})
},
function onRestart() {
// @ts-ignore
const vscode = (window.vscode =
// @ts-ignore
window.vscode || acquireVsCodeApi())
vscode.postMessage({
command: 'restart',
})
}
)
)
})
}
const runAction = (actionName: string) => {
vscode.window.showInformationMessage(`Run: ${actionName}`)
}
const startOvermindCommand = vscode.commands.registerCommand(
'overmind-devtools.start',
startDevtools
)
const runActionCommand = vscode.commands.registerCommand(
'overmind-devtools.runAction',
runAction
)
context.subscriptions.push(startOvermindCommand)
context.subscriptions.push(runActionCommand)
vscode.languages.registerCodeLensProvider(
{ scheme: 'file', language: 'typescript' },
{
provideCodeLenses(doc, token) {
const text = doc.getText()
const lenses: vscode.CodeLens[] = []
const lines = text.split('\n')
lines.forEach((line: string, index) => {
const match = findActionRegex.exec(line)
if (match !== null) {
lenses.push(
new vscode.CodeLens(
new vscode.Range(
new vscode.Position(index, match.index),
new vscode.Position(index, match.index + match[0].length)
),
{
title: 'Run Action...',
command: 'overmind-devtools.runAction',
arguments: [match[1]],
}
)
)
}
})
return lenses
},
resolveCodeLens(codeLens, token) {
return codeLens
},
}
)
// This is supposed keep Overmind extension in same position on restart, though
// do not think we need that really? https://code.visualstudio.com/api/extension-guides/webview
/*
if (vscode.window.registerWebviewPanelSerializer) {
vscode.window.registerWebviewPanelSerializer(DevtoolsPanel.viewType, {
async deserializeWebviewPanel(
webViewPanel: vscode.WebviewPanel,
state: any
) {
DevtoolsPanel.revive(webViewPanel, context.extensionPath)
},
})
}
*/
}
// this method is called when your extension is deactivated
export function deactivate() {
log('Extension deactivated')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment