Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 2 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { CombinedScriptGenDataProvider } from "./combinedScriptGenDataProvider"
import { TriggerFlowWebViewManager } from "./triggerFlowWebViewManager"
import { GenericSessionStorage } from "./genericSessionStorage"
import { extractTarGzToDisk, isMacOS } from "./utility"
import { convertTspToPython } from "./tspConverter"
import { wrapTspToPython } from "./tspConverter"
import {
checkSystemDependencies,
checkVisaInstallation,
Expand Down Expand Up @@ -569,12 +569,7 @@ export async function activate(context: vscode.ExtensionContext) {
{
name: "tsp.wrapTspForPython",
cb: async (e: vscode.Uri) => {
const outputUri = await pickPythonOutputFile(e)
if (!outputUri) {
return
}

await convertTspToPython(e, _tspConverterDiagnostics, outputUri)
await wrapTspToPython(e, _tspConverterDiagnostics)
},
},
{
Expand Down Expand Up @@ -1168,25 +1163,6 @@ 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
}
Comment thread
jharajeev55 marked this conversation as resolved.

export async function pickConnection(): Promise<Connection | undefined> {
const options: vscode.QuickPickItem[] =
InstrumentProvider.instance.getQuickPickOptions()
Expand Down
64 changes: 47 additions & 17 deletions src/tspConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,34 @@ function loadTspInterop(): TspInterop {
* the result in a new editor tab. Any converter diagnostics are surfaced in
* the VS Code Problems panel.
*/
export async function convertTspToPython(
export async function wrapTspToPython(
uri: vscode.Uri | undefined,
Comment thread
esarver marked this conversation as resolved.
diagnosticCollection: vscode.DiagnosticCollection,
outputUri: vscode.Uri | undefined,
Comment thread
jharajeev55 marked this conversation as resolved.
): Promise<void> {
// Allow invocation from command palette (no URI) by falling back to the
// active editor.
const fileUri =
uri ??
(vscode.window.activeTextEditor?.document.uri.fsPath.endsWith(".tsp")
if (!uri) {
uri = vscode.window.activeTextEditor?.document.uri.fsPath.endsWith(
".tsp",
)
? vscode.window.activeTextEditor.document.uri
: undefined)
: undefined
if (!uri) {
const user_selected = await vscode.window.showOpenDialog({
title: "Select a TSP file to generate Python wrapper",
filters: { "TSP Files": ["tsp"] },
})
if (user_selected && user_selected[0]) {
uri = user_selected[0]
} else {
vscode.window.showErrorMessage(
"Unable to generate Python file: no TSP file selected",
)
return
}
}
}

if (!fileUri) {
vscode.window.showErrorMessage(
"No TSP file selected. Open a .tsp file or right-click it in the Explorer.",
)
const outputUri = await pickPythonOutputFile(uri)
if (!outputUri) {
return
}

Expand All @@ -51,7 +62,7 @@ export async function convertTspToPython(
// Read source
let source: string
try {
source = (await vscode.workspace.fs.readFile(fileUri)).toString()
source = (await vscode.workspace.fs.readFile(uri)).toString()
} catch (err) {
vscode.window.showErrorMessage(
`Could not read file: ${err instanceof Error ? err.message : String(err)}`,
Expand All @@ -60,7 +71,7 @@ export async function convertTspToPython(
}

// Derive a class name from the file name (e.g. "my_script.tsp" → "MyScript")
const baseName = path.basename(fileUri.fsPath, ".tsp")
const baseName = path.basename(uri.fsPath, ".tsp")
const className = baseName
.split(/[_\-\s]+/)
.map((s) => s.charAt(0).toUpperCase() + s.slice(1))
Expand Down Expand Up @@ -103,13 +114,13 @@ export async function convertTspToPython(
if (d.hint)
diag.relatedInformation = [
new vscode.DiagnosticRelatedInformation(
new vscode.Location(fileUri, range),
new vscode.Location(uri, range),
d.hint,
),
]
return diag
})
diagnosticCollection.set(fileUri, vsDiagnostics)
diagnosticCollection.set(uri, vsDiagnostics)

if (!result.ok || !result.code) {
const errMsg = result.diagnostics?.[0]?.message ?? "Unknown error"
Expand All @@ -119,7 +130,7 @@ export async function convertTspToPython(

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

await vscode.workspace.fs.createDirectory(
vscode.Uri.file(path.dirname(target.fsPath)),
Expand All @@ -135,3 +146,22 @@ export async function convertTspToPython(
preview: false,
})
}

// 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: "Generate python wrapper file",
filters: { Python: ["py"] },
})

if (!target) {
return undefined
}

return target
}
Loading