Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- **tsp-toolkit-language-interop** - Implement tsp language interop feature
- Added configurable 'Connection Timeout' as a VS Code setting for instrument connections.
- Allow username input when authenticating with MP5000 instruments
- Allow users to choose whether to overwrite an existing file or create a new file when generating a Python wrapper from a TSP script.


## [1.5.1]
Expand Down
26 changes: 25 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,12 @@ export async function activate(context: vscode.ExtensionContext) {
{
name: "tsp.convertToPython",
cb: async (e: vscode.Uri) => {
await convertTspToPython(e, _tspConverterDiagnostics)
const outputUri = await pickPythonOutputFile(e)
if (!outputUri) {
return
}

await convertTspToPython(e, _tspConverterDiagnostics, outputUri)
},
},
{
Expand Down Expand Up @@ -1163,6 +1168,25 @@ async function resetToolkitDefaults() {
vscode.window.showInformationMessage("Reset completed successfully.")
}

// Prompt for the Python output file, confirming before overwriting an existing
// one and returning to the file dialog if the user declines.
async function pickPythonOutputFile(
defaultUri: vscode.Uri,
): Promise<vscode.Uri | undefined> {
const target = await vscode.window.showSaveDialog({
title: "Select Python Output File",
defaultUri: vscode.Uri.file(defaultUri.fsPath.replace(/\.tsp$/, ".py")),
saveLabel: "Convert",
filters: { Python: ["py"] },
})

if (!target) {
return undefined
}

return target
}

export async function pickConnection(): Promise<Connection | undefined> {
const options: vscode.QuickPickItem[] =
InstrumentProvider.instance.getQuickPickOptions()
Expand Down
12 changes: 8 additions & 4 deletions src/tspConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function loadTspInterop(): TspInterop {
export async function convertTspToPython(
uri: vscode.Uri | undefined,
diagnosticCollection: vscode.DiagnosticCollection,
outputUri: vscode.Uri | undefined,
): Promise<void> {
// Allow invocation from command palette (no URI) by falling back to the
// active editor.
Expand Down Expand Up @@ -117,15 +118,18 @@ export async function convertTspToPython(
}

// Save generated Python to a file with .py extension
const outputPath = fileUri.fsPath.replace(/\.tsp$/, ".py")
const outputUri = vscode.Uri.file(outputPath)
const target =
outputUri ?? vscode.Uri.file(fileUri.fsPath.replace(/\.tsp$/, ".py"))

await vscode.workspace.fs.createDirectory(
vscode.Uri.file(path.dirname(target.fsPath)),
)
// Write the file to disk
const encoder = new TextEncoder()
await vscode.workspace.fs.writeFile(outputUri, encoder.encode(result.code))
await vscode.workspace.fs.writeFile(target, encoder.encode(result.code))

// Open the saved file in editor
const doc = await vscode.workspace.openTextDocument(outputUri)
const doc = await vscode.workspace.openTextDocument(target)
await vscode.window.showTextDocument(doc, {
viewColumn: vscode.ViewColumn.Beside,
preview: false,
Expand Down
Loading