71 lines
2.5 KiB
TypeScript
71 lines
2.5 KiB
TypeScript
import * as vscode from 'vscode';
|
|
import * as path from 'path';
|
|
import { CodeParser } from './parser/codeParser';
|
|
import { TkinterDesignerProvider } from './webview/tkinterDesignerProvider';
|
|
|
|
export function activate(context: vscode.ExtensionContext) {3
|
|
const openDesignerCommand = vscode.commands.registerCommand(
|
|
'tkinter-designer.openDesigner',
|
|
() => {
|
|
TkinterDesignerProvider.createNew(context.extensionUri);
|
|
}
|
|
);
|
|
|
|
const parseCodeCommand = vscode.commands.registerCommand(
|
|
'tkinter-designer.parseCode',
|
|
async () => {
|
|
const activeEditor = vscode.window.activeTextEditor;
|
|
|
|
if (activeEditor && activeEditor.document.languageId === 'python') {
|
|
const parser = new CodeParser();
|
|
const code = activeEditor.document.getText();
|
|
const fileName = path.basename(
|
|
activeEditor.document.fileName,
|
|
'.py'
|
|
);
|
|
|
|
try {
|
|
const designData = await parser.parseCodeToDesign(
|
|
code,
|
|
fileName
|
|
);
|
|
if (
|
|
designData &&
|
|
designData.widgets &&
|
|
designData.widgets.length > 0
|
|
) {
|
|
const designerInstance = TkinterDesignerProvider.createNew(
|
|
context.extensionUri
|
|
);
|
|
|
|
designerInstance.loadDesignData(designData);
|
|
|
|
vscode.window.showInformationMessage(
|
|
`Code parsed successfully! Found ${designData.widgets.length} widgets.`
|
|
);
|
|
} else {
|
|
vscode.window.showWarningMessage(
|
|
'No tkinter widgets found in the code. Make sure your code contains tkinter widget creation statements like tk.Label(), tk.Button(), etc.'
|
|
);
|
|
}
|
|
} catch (error) {
|
|
vscode.window.showErrorMessage(
|
|
`Error parsing code: ${error}`
|
|
);
|
|
}
|
|
} else {
|
|
vscode.window.showErrorMessage(
|
|
'Please open a Python file with tkinter code'
|
|
);
|
|
}
|
|
}
|
|
);
|
|
|
|
context.subscriptions.push(
|
|
openDesignerCommand,
|
|
parseCodeCommand
|
|
);
|
|
}
|
|
|
|
export function deactivate() {}
|