From c5d9043bba77fd3a75cd928df2beeaca2522a292 Mon Sep 17 00:00:00 2001 From: wangshi Date: Fri, 7 Feb 2025 17:09:00 +0800 Subject: [PATCH] refator h2dts,h2dtscpp,h2sa,h2hdf,dts2cpp Signed-off-by: wangshi --- src/vscode_plugin/src/common/tool.ts | 9 +- src/vscode_plugin/src/common/widget.ts | 35 +++++- src/vscode_plugin/src/extension.ts | 160 ++++++++++++++----------- src/vscode_plugin/src/gen/gendts.ts | 14 ++- src/vscode_plugin/src/gen/genhdf.ts | 11 +- 5 files changed, 144 insertions(+), 85 deletions(-) diff --git a/src/vscode_plugin/src/common/tool.ts b/src/vscode_plugin/src/common/tool.ts index 86ae0a83..070945cd 100644 --- a/src/vscode_plugin/src/common/tool.ts +++ b/src/vscode_plugin/src/common/tool.ts @@ -14,10 +14,13 @@ */ export function replaceAll(s: string, sfrom: string, sto: any) { + console.log('[replaceall] s:'+s+' sfrom:'+sfrom+' sto:'+sto); + if (s && sfrom && sto) { while (s.indexOf(sfrom) >= 0) { - s = s.replace(sfrom, sto); - } - return s; + s = s.replace(sfrom, sto); + } + } + return s; } export function getTab(tabLv: number) { diff --git a/src/vscode_plugin/src/common/widget.ts b/src/vscode_plugin/src/common/widget.ts index 4173c1a9..b160c013 100644 --- a/src/vscode_plugin/src/common/widget.ts +++ b/src/vscode_plugin/src/common/widget.ts @@ -17,7 +17,40 @@ // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -import { EVENT_ERROR, EVENT_INFORMATION, EVENT_WARNING } from './eventtype'; +import { + EVENT_ERROR, + EVENT_INFORMATION, + EVENT_WARNING +} from './eventtype'; +import { Callback } from './define'; + +export function doAsyncQuickPick(valueList: string[], options?: vscode.QuickPickOptions, cb?: Callback) { + vscode.window.showQuickPick(valueList, options).then((value) => { + if (cb) { + cb(value); + } else { + toastMsg(EVENT_ERROR, 'No cb in showQuickPick'); + } + }) +} + +export async function doSyncQuickPick(valueList: string[], options?: vscode.QuickPickOptions) { + return await vscode.window.showQuickPick(valueList, options); +} + +export function doAsyncInputBox(options?: vscode.InputBoxOptions, cb?: Callback) { + vscode.window.showInputBox(options).then((value) => { + if (cb) { + cb(value); + } else { + toastMsg(EVENT_ERROR, 'No cb in showInputBox'); + } + }) +} + +export async function doSyncInputBox(options?: vscode.InputBoxOptions) { + return await vscode.window.showInputBox(options); +} export function toastMsg(event: string, msg: string) { switch(event) { diff --git a/src/vscode_plugin/src/extension.ts b/src/vscode_plugin/src/extension.ts index a251b447..2f20dfde 100644 --- a/src/vscode_plugin/src/extension.ts +++ b/src/vscode_plugin/src/extension.ts @@ -29,6 +29,10 @@ import { genDtsFile } from './gen/gendts'; import { genHdfFile } from './gen/genhdf'; import { genDtsCppFile, genCppFile } from './gen/gendtscpp'; import { H2dtsCtrl } from './controller/h2dtsctrl'; +import { H2saCtrl } from './controller/h2sactrl'; +import { H2hdfCtrl } from './controller/h2hdfctrl'; +import { H2dtscppCtrl } from './controller/h2dtscppctrl'; +import { Dts2cppCtrl } from './controller/dts2cppctrl'; // ��ȡ���ػ��ַ��� const SELECTED_DIR = vscode.l10n.t('You selected a directory:'); @@ -429,43 +433,49 @@ export function activate(context: vscode.ExtensionContext) { // Now provide the implementation of the command with registerCommand // The commandId parameter must match the command field in package.json const h2sa = vscode.commands.registerCommand('extension.h2sa', async (uri) => { + let h2saCtrl = new H2saCtrl(uri); + h2saCtrl.init(); // The code you place here will be executed every time your command is executed - if (uri && uri.fsPath) { - let versionTag = '3.2'; - const version = await vscode.window.showQuickPick(['OpenHarmony 4.1 release', 'OpenHarmony 3.2 release'], { placeHolder: SELECT_VERSION }); - if (version === 'OpenHarmony 4.1 release') { - versionTag = '4.1' - } else if (version === 'OpenHarmony 3.2 release') { - versionTag = '3.2' - } - const serviceId = await vscode.window.showInputBox({ - placeHolder: INPUT_SERVICEID, - value: "19000", // ����Ĭ��ֵ - validateInput: (input) => { - if (!input) { - return INPUT_NO_EMPTY; - } - if (!Number(input)) { - return INPUT_NUMBER - } - } - }); - generateSa(uri.fsPath, versionTag, serviceId as string); - } + // if (uri && uri.fsPath) { + // let versionTag = '3.2'; + // const version = await vscode.window.showQuickPick( + // ['OpenHarmony 4.1 release', 'OpenHarmony 3.2 release'], + // { placeHolder: SELECT_VERSION } + // ); + // if (version === 'OpenHarmony 4.1 release') { + // versionTag = '4.1' + // } else if (version === 'OpenHarmony 3.2 release') { + // versionTag = '3.2' + // } + // const serviceId = await vscode.window.showInputBox({ + // placeHolder: INPUT_SERVICEID, + // value: "19000", // ����Ĭ��ֵ + // validateInput: (input) => { + // if (!input) { + // return INPUT_NO_EMPTY; + // } + // if (!Number(input)) { + // return INPUT_NUMBER + // } + // } + // }); + // generateSa(uri.fsPath, versionTag, serviceId as string); + // } }); - context.subscriptions.push(h2sa); const h2hdf = vscode.commands.registerCommand('extension.h2hdf', async (uri) => { - // The code you place here will be executed every time your command is executed - if (uri && uri.fsPath) { - let versionTag = '4.1'; - const version = await vscode.window.showQuickPick(['OpenHarmony 4.1 release'], { placeHolder: SELECT_VERSION }); - if (version === 'OpenHarmony 4.1 release') { - versionTag = '4.1' - } - generateHdf(uri.fsPath, versionTag); - } + let h2hdfCtrl = new H2hdfCtrl(uri); + h2hdfCtrl.init(); + // // The code you place here will be executed every time your command is executed + // if (uri && uri.fsPath) { + // let versionTag = '4.1'; + // const version = await vscode.window.showQuickPick(['OpenHarmony 4.1 release'], { placeHolder: SELECT_VERSION }); + // if (version === 'OpenHarmony 4.1 release') { + // versionTag = '4.1' + // } + // generateHdf(uri.fsPath, versionTag); + // } }); context.subscriptions.push(h2hdf); @@ -499,45 +509,49 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(h2dts); const h2dtscpp = vscode.commands.registerCommand('extension.h2dtscpp', async (uri) => { - // The code you place here will be executed every time your command is executed - if (uri && uri.fsPath) { - generateDtscpp(uri.fsPath); - } + let h2dtscppCtrl = new H2dtscppCtrl(uri); + h2dtscppCtrl.init(); + // // The code you place here will be executed every time your command is executed + // if (uri && uri.fsPath) { + // generateDtscpp(uri.fsPath); + // } }); context.subscriptions.push(h2dtscpp); const dts2cpp = vscode.commands.registerCommand('extension.dts2cpp', (uri) => { - // The code you place here will be executed every time your command is executed - console.log('uri is : ' + uri.fsPath ); - if (uri && uri.fsPath) { - vscode.window.withProgress({ - location: vscode.ProgressLocation.Notification, - title: "Generating CPP...", - cancellable: false - }, async (progress) => { - const filename = path.basename(uri.fsPath); - console.log('get filename ' ); - if (filename.endsWith('.d.ts')) { - // Display a message box to the user - // analyze - let res = parseTsFile(uri.fsPath); - console.info('res: ' + JSON.stringify(res)); - progress.report({ increment: 50, message: PARSE_COMPLETE }); - // generator - let out = path.dirname(uri.fsPath); - genCppFile(res, uri.fsPath, out); - progress.report({ increment: 100, message: GEN_COMPLETE + out }); - } else { - console.log('not dts uri is : ' + uri.fsPath ); - // Display a message box to the user - vscode.window.showInformationMessage(`${uri.fsPath} is not a .d.ts file!`); - } - }); - } + let dts2cppCtrl = new Dts2cppCtrl(uri); + dts2cppCtrl.init(); + // The code you place here will be executed every time your command is executed + // console.log('uri is : ' + uri.fsPath ); + // if (uri && uri.fsPath) { + // vscode.window.withProgress({ + // location: vscode.ProgressLocation.Notification, + // title: "Generating CPP...", + // cancellable: false + // }, async (progress) => { + // const filename = path.basename(uri.fsPath); + // console.log('get filename ' ); + // if (filename.endsWith('.d.ts')) { + // // Display a message box to the user + // // analyze + // let res = parseTsFile(uri.fsPath); + // console.info('res: ' + JSON.stringify(res)); + // progress.report({ increment: 50, message: PARSE_COMPLETE }); + // // generator + // let out = path.dirname(uri.fsPath); + // genCppFile(res, uri.fsPath, out); + // progress.report({ increment: 100, message: GEN_COMPLETE + out }); + // } else { + // console.log('not dts uri is : ' + uri.fsPath ); + // // Display a message box to the user + // vscode.window.showInformationMessage(`${uri.fsPath} is not a .d.ts file!`); + // } + // }); + // } }); context.subscriptions.push(dts2cpp); - // ��ӭ�˵�ҳ�� + // welcome page in vscode when no file or dir is opened const ohGenerator = vscode.commands.registerCommand('extension.ohGenerator', async () => { // The code you place here will be executed every time your command is executed let hPath = path.join(__dirname, '../test/test.h'); @@ -552,10 +566,10 @@ export function activate(context: vscode.ExtensionContext) { if (input !== value) { return INPUT_INCONSISTENT; } - } + } }); if (value === HDF_FRAMEWORK) { - // ����汾 + // input the version of oh let versionTag = '4.1'; const version = await vscode.window.showQuickPick(['OpenHarmony 4.1 release'], { placeHolder: SELECT_VERSION }) if (version === 'OpenHarmony 4.1 release') { @@ -563,7 +577,7 @@ export function activate(context: vscode.ExtensionContext) { } generateHdf(hdfInputPath, versionTag); } else if (value === SA_FRAMEWORK) { - // ����汾 + // input the version of oh let versionTag = '3.2'; const version = await vscode.window.showQuickPick(['OpenHarmony 3.2 release', 'OpenHarmony 4.1 release'], { placeHolder: SELECT_VERSION }) if (version === 'OpenHarmony 4.1 release') { @@ -613,10 +627,10 @@ async function generateHdf(hdfInputPath: string, versionTag: string) { genHdfFile(rootInfo, out); progress.report({ increment: 100, message: GEN_COMPLETE + out}); }); - // ��ʾ������·�� + // show output path const choice = await vscode.window.showInformationMessage('outPath:', path.dirname(hdfInputPath), OPEN_IN_EXPLORER); if (choice === OPEN_IN_EXPLORER) { - // ���ļ����ڵ�Ŀ¼ + // open the output folder vscode.commands.executeCommand('revealFileInOS', vscode.Uri.file(hdfInputPath)); } } @@ -646,10 +660,10 @@ async function generateSa(hPath: string, versionTag: string, serviceId: string) genServiceFile(rootInfo, out); progress.report({ increment: 100, message: GEN_COMPLETE + out }); }); - // ��ʾ������·�� + // show the output path const choice = await vscode.window.showInformationMessage('outPath:', path.dirname(hPath), OPEN_IN_EXPLORER); if (choice === OPEN_IN_EXPLORER) { - // ���ļ����ڵ�Ŀ¼ + // open output dir vscode.commands.executeCommand('revealFileInOS', vscode.Uri.file(hPath)); } } @@ -679,10 +693,10 @@ async function generateDtscpp(hFilePath: string) { genDtsCppFile(rootInfo, out); progress.report({ increment: 100, message: GEN_COMPLETE + out }); }); - // ��ʾ������·�� + // show genarate path const choice = await vscode.window.showInformationMessage('outPath:', path.dirname(hFilePath), OPEN_IN_EXPLORER); if (choice === OPEN_IN_EXPLORER) { - // ���ļ����ڵ�Ŀ¼ + // open the folder vscode.commands.executeCommand('revealFileInOS', vscode.Uri.file(hFilePath)); } } diff --git a/src/vscode_plugin/src/gen/gendts.ts b/src/vscode_plugin/src/gen/gendts.ts index e229a0f4..7260baff 100644 --- a/src/vscode_plugin/src/gen/gendts.ts +++ b/src/vscode_plugin/src/gen/gendts.ts @@ -112,6 +112,9 @@ function getInterFuncName(str: string) { } export function getJsTypeFromC(cType: string) { + if (!cType) { + return ''; + } let basicCtype = cType; let matchs = re.match('(std::)?vector<([\x21-\x7e ]+)>', basicCtype); let isArray = 0; @@ -179,11 +182,14 @@ function isJsBasicType(type: string) { function removeMarco(type: string) { // 去掉宏定义 - let leftCraftIndex = type.indexOf('('); - let rightCraftIndex = type.indexOf(')'); - if (leftCraftIndex >= 0 && rightCraftIndex > 0) { - type = removeTab(type.substring(leftCraftIndex + 1, rightCraftIndex)); + if (type) { + let leftCraftIndex = type.indexOf('('); + let rightCraftIndex = type.indexOf(')'); + if (leftCraftIndex >= 0 && rightCraftIndex > 0) { + type = removeTab(type.substring(leftCraftIndex + 1, rightCraftIndex)); + } } + return type; } diff --git a/src/vscode_plugin/src/gen/genhdf.ts b/src/vscode_plugin/src/gen/genhdf.ts index 5e29de43..1d8fbd4d 100644 --- a/src/vscode_plugin/src/gen/genhdf.ts +++ b/src/vscode_plugin/src/gen/genhdf.ts @@ -101,10 +101,13 @@ function getIdlType(cType: string) { } function isReturn(type: string) { - if (type.indexOf('&')>0 || type.indexOf('**')>0) { - return true; - } - return false; + console.log('isReturn in type: ', type); + if (type) { + if (type.indexOf('&')>0 || type.indexOf('**')>0) { + return true; + } + } + return false; } function getIdlFuncParamStr(funcObj: FuncObj) {