Skip to content

Commit

Permalink
Support *-app.py and *_app.py filename patterns (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 authored Oct 11, 2023
1 parent c0018ff commit a32611b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->

## 0.1.5

- Recognize files named \*-app.py and \*\_app.py as potentially Shiny apps, enabling the "Run Shiny App" and "Debug Shiny App" buttons. (We continue to recognize app.py, app-\*.py, and app\_\*.py.) (#19)
- Stop using an exported API from the ms-python.python extension that has been deprecated and replaced. (#19)

## 0.1.4

- No code changes; required a new version number to recover from continuous integration issue.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "shiny-python",
"displayName": "Shiny for Python",
"description": "Shiny for Python support",
"version": "0.1.4",
"version": "0.1.5",
"publisher": "Posit",
"icon": "logo.png",
"repository": {
Expand Down
28 changes: 27 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ function updateContext(): boolean {
!!editor &&
editor.document.languageId === "python" &&
!editor.document.isUntitled &&
/^app([_-].+)?\.py$/i.test(path.basename(editor.document.fileName ?? "")) &&
!!editor.document.fileName &&
isShinyAppUsername(editor.document.fileName) &&
editor.document.getText().search(/\bshiny\b/) >= 0;
vscode.commands.executeCommand("setContext", "shiny.python.active", active);
return active;
Expand Down Expand Up @@ -114,3 +115,28 @@ class Throttler {
this._pending = false;
}
}

function isShinyAppUsername(filename: string): boolean {
filename = path.basename(filename);

// Only .py files
if (!/\.py$/i.test(filename)) {
return false;
}

// Accepted patterns:
// app.py
// app-*.py
// app_*.py
// *-app.py
// *_app.py
if (/^app\.py$/i.test(filename)) {
return true;
} else if (/^app[-_]/i.test(filename)) {
return true;
} else if (/[-_]app\.py$/i.test(filename)) {
return true;
}

return false;
}
15 changes: 10 additions & 5 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ export async function runApp(context: vscode.ExtensionContext) {
// only to re-use a terminal if it's using the same interpreter.
const pythonAPI =
vscode.extensions.getExtension("ms-python.python")!.exports;
const pythonExecCommand = (
await pythonAPI.environment.getExecutionDetails(
vscode.window.activeTextEditor?.document.uri
)
).execCommand.join(" ");

// The getActiveEnvironmentPath docstring says: "Note that this can be an
// invalid environment, use resolveEnvironment to get full details."
const unresolvedEnv = pythonAPI.environments.getActiveEnvironmentPath(
vscode.window.activeTextEditor?.document.uri
);
const resolvedEnv = await pythonAPI.environments.resolveEnvironment(
unresolvedEnv
);
const pythonExecCommand = resolvedEnv.path;

const shinyTerminals = vscode.window.terminals.filter(
(term) => term.name === TERMINAL_NAME
Expand Down

0 comments on commit a32611b

Please sign in to comment.