Skip to content

Commit

Permalink
Remove need for reloads during extension setup
Browse files Browse the repository at this point in the history
  • Loading branch information
millerdev committed Jan 3, 2021
1 parent 885e0fd commit a87b66f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

## Installation and setup

See [Packaging](#packaging) below for instructions to build the .vsix file.

- Install the extension in VS Code
- Setup a virtualenv with Python 3.7+
```sh
python3 -m venv /path/to/your/virtualenv
python3 -m venv /path/to/your/virtualenv # or your preferred way to create a virtualenv
```
- Install requirements in the virtualenv (you may prefer to use the tag of the
version being installed rather than `master`)
```sh
/path/to/your/virtualenv/bin/pip install -r \
https://github.com/millerdev/PyXT/raw/master/requirements.txt
```
- Install the extension in VS Code
- Reload VS Code
- Open Settings in VS Code and search for `pyxt.pythonPath`. Set the value to
the path of the `python` executable in the virtualenv created above. Something
like `/path/to/your/virtualenv/bin/python`.
- Reload VS Code
the path of the `python` executable in the virtualenv created above. It will
be something like `/path/to/your/virtualenv/bin/python`.

## Usage

Expand Down Expand Up @@ -48,8 +44,8 @@ _PyXT: Open File_), and may be assigned a keyboard shortcut.
- `python EXECUTABLE SCOPE OPTIONS...` - Run selected text or entire file
(depending on `SCOPE`) with the given Python `EXECUTABLE` (or virtualenv) and
show the result, which consists of printed output plus non-null result of the
final expression. Accept (by pressing Enter) the result quick-pick item to
copy it to the clipboard.
final expression. Accept (by pressing Enter) the result to copy it to the
clipboard.
VS Code command: _PyXT: Python_

The command name should not be typed when it is invoked directly via its
Expand Down
12 changes: 7 additions & 5 deletions client/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ const errable = require("./errors").errable
const pkg = require("../package.json")
let history

function subscribe(client, context) {
function subscribe(getClient, context) {
pkg.contributes.commands.forEach(cmd => {
registerCommand(cmd.command, client, context)
registerCommand(cmd.command, getClient, context)
})
}

function registerCommand(id, client, context) {
function registerCommand(id, getClient, context) {
const slug = id === "pyxt.command" ? "" : (id.slice(5) + " ")
const cmd = () => command(client, slug)
const reg = vscode.commands.registerCommand(id, cmd)
const reg = vscode.commands.registerCommand(id, () => {
const client = getClient()
client && command(client, slug)
})
context.subscriptions.push(reg)
}

Expand Down
43 changes: 29 additions & 14 deletions client/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,35 @@ const DEBUG_PORT = 2087
let client

function activate(context) {
if (isStartedInDebugMode()) {
client = startLangServerTCP(DEBUG_PORT)
} else {
client = startLangServer()
commander.subscribe(() => getClient(context), context)
}

function deactivate() {
return client ? client.stop() : Promise.resolve()
}

function getClient(context) {
if (!client) {
client = startServer()
if (client) {
setup(client, context)
}
}
return client
}

function setup(client, context) {
context.subscriptions.push(client.start())
jsproxy.publish(client, context)
commander.subscribe(client, context)
commander.setHistory(createHistory(context.globalState))
jsproxy.publish(client, context)
loadUserScript(client)
}

function deactivate() {
return client ? client.stop() : Promise.resolve()
function startServer() {
if (isStartedInDebugMode()) {
return startLangServerTCP(DEBUG_PORT)
}
return startLangServer()
}

function isStartedInDebugMode() {
Expand All @@ -46,13 +61,13 @@ function startLangServerTCP(addr) {
function startLangServer() {
const command = workspace.getConfiguration("pyxt").get("pythonPath")
if (!command) {
const url = "https://github.com/millerdev/pyxt/#installation-and-setup"
vscode.window.showErrorMessage(
"pyxt.pythonPath not set. Create a virtualenv, install " +
"requirements, and set pyxt.pythonPath in settings. " +
"See PyXT README for detailed instructions: " +
"https://github.com/millerdev/pyxt/"
"[Setup required](" + url + "): create a virtualenv, install " +
"requirements, and set pyxt.pythonPath in settings. See the " +
"[PyXT README](" + url + ") for detailed instructions."
)
throw new Error("pyxt.pythonPath not set")
return
}
const cwd = path.join(__dirname, "..")
const args = ["-m", "pyxt"]
Expand All @@ -78,7 +93,7 @@ async function loadUserScript(client) {
}

function getClientOptions() {
return {outputChannelName: "PyXT Server"}
return {outputChannelName: "PyXT"}
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
],
"configuration": {
"type": "object",
"title": "pyxt",
"title": "PyXT",
"properties": {
"pyxt.pythonPath": {
"type": "string",
Expand All @@ -61,7 +61,7 @@
"pyxt.userScript": {
"type": "string",
"default": null,
"description": "File path or importable Python module path referencing user-defined commands. See 'Adding your own custom commands' in the PyXT README for more details."
"markdownDescription": "File path or importable Python module path referencing user-defined commands. See [Adding your own custom commands](https://github.com/millerdev/pyxt/#adding-your-own-custom-commands) in the [PyXT README](https://github.com/millerdev/pyxt/) for more details."
}
}
}
Expand Down

0 comments on commit a87b66f

Please sign in to comment.