diff --git a/ets2panda/bindings/src/common/arkTSConfigGenerator.ts b/ets2panda/bindings/src/common/arkTSConfigGenerator.ts index 23998489346c34cf426f44cd4aff0c7f626817fd..5340128eb996498a11ea34dc0d753857d8b1aee1 100644 --- a/ets2panda/bindings/src/common/arkTSConfigGenerator.ts +++ b/ets2panda/bindings/src/common/arkTSConfigGenerator.ts @@ -15,9 +15,10 @@ import * as path from 'path'; import * as fs from 'fs'; +import * as JSON5 from 'json5'; import { changeFileExtension, ensurePathExists, getFileLanguageVersion } from './utils'; -import { BuildConfig, ModuleInfo } from './types'; +import { AliasConfig, BuildConfig, ModuleInfo } from './types'; import { LANGUAGE_VERSION, PANDA_SDK_PATH_FROM_SDK, SYSTEM_SDK_PATH_FROM_SDK } from './preDefine'; interface DependencyItem { @@ -82,6 +83,7 @@ export class ArkTSConfigGenerator { private traverse( pathSection: Record, currentDir: string, + aliasConfigObj: Record | undefined, prefix: string = '', isInteropSdk: boolean = false, relativePath: string = '', @@ -103,11 +105,11 @@ export class ArkTSConfigGenerator { const key = isExcludedDir ? basename : relativePath ? `${relativePath}${separator}${basename}` : basename; pathSection[prefix + key] = isInteropSdk ? { - language: 'js', - path: itemPath, - ohmUrl: '', - alias: [key] - } + language: 'js', + path: itemPath, + ohmUrl: '', + alias: aliasConfigObj ? this.processAlias(basename, aliasConfigObj) : undefined + } : [changeFileExtension(itemPath, '', '.d.ets')]; } if (stat.isDirectory()) { @@ -120,6 +122,7 @@ export class ArkTSConfigGenerator { this.traverse( pathSection, path.resolve(currentDir, item), + aliasConfigObj, prefix, isInteropSdk, newRelativePath, @@ -135,10 +138,10 @@ export class ArkTSConfigGenerator { let systemSdkPath = path.resolve(this.systemSdkPath, dir); let externalApiPath = path.resolve(this.externalApiPath, dir); fs.existsSync(systemSdkPath) - ? this.traverse(pathSection, systemSdkPath) + ? this.traverse(pathSection, systemSdkPath, undefined) : console.warn(`sdk path ${systemSdkPath} not exist.`); fs.existsSync(externalApiPath) - ? this.traverse(pathSection, externalApiPath) + ? this.traverse(pathSection, externalApiPath, undefined) : console.warn(`sdk path ${externalApiPath} not exist.`); }); } @@ -225,7 +228,40 @@ export class ArkTSConfigGenerator { }); } - private generateSystemSdkDependenciesSection(dependencySection: Record): void { + private parseSdkAliasConfigFile(sdkAliasConfigFilePath?: string): Record | undefined { + if (!sdkAliasConfigFilePath) { + return; + } + const rawContent = fs.readFileSync(sdkAliasConfigFilePath, 'utf-8'); + const jsonData = JSON5.parse(rawContent); + const aliasConfigObj: Record = {}; + for (const [aliasKey, config] of Object.entries(jsonData)) { + const aliasConfig = config as AliasConfig; + aliasConfigObj[aliasKey] = aliasConfig; + } + return aliasConfigObj; + } + + private processAlias(basename: string, aliasConfigObj: Record): string[] | undefined { + let alias: string[] = []; + for (const [aliasName, aliasConfig] of Object.entries(aliasConfigObj)) { + if (aliasConfig.isStatic) { + continue; + } + if (basename === aliasConfig.originalAPIName) { + alias.push(aliasName); + } + } + if (alias.length !== 0) { + return alias; + } + } + + private generateSystemSdkDependenciesSection( + dependencySection: Record, + moduleInfo: ModuleInfo + ): void { + const aliasConfigObj = this.parseSdkAliasConfigFile(moduleInfo.sdkAliasConfigPath); let directoryNames: string[] = ['api', 'arkts', 'kits', 'component']; directoryNames.forEach((dirName) => { const basePath = path.resolve(this.interopApiPath, dirName); @@ -234,15 +270,15 @@ export class ArkTSConfigGenerator { return; } if (dirName === 'component') { - this.traverse(dependencySection, basePath, 'component/', true); + this.traverse(dependencySection, basePath, aliasConfigObj, 'component/', true); } else { - this.traverse(dependencySection, basePath, 'dynamic/', true); + this.traverse(dependencySection, basePath, aliasConfigObj, 'dynamic/', true); } }); } private getDependenciesSection(moduleInfo: ModuleInfo, dependencySection: Record): void { - this.generateSystemSdkDependenciesSection(dependencySection); + this.generateSystemSdkDependenciesSection(dependencySection, moduleInfo); let depModules: string[] = moduleInfo.dynamicDepModuleInfos; depModules.forEach((depModuleName: string) => { let depModuleInfo = this.moduleInfos[depModuleName]; diff --git a/ets2panda/bindings/src/common/types.ts b/ets2panda/bindings/src/common/types.ts index 39fc958ab7a167174b9d00ceab569414fa81d051..6de302c1ba9ab63788887c2ec8b695ce9c6b9378 100644 --- a/ets2panda/bindings/src/common/types.ts +++ b/ets2panda/bindings/src/common/types.ts @@ -177,6 +177,7 @@ export interface BuildConfig extends DeclgenConfig, ModuleConfig, PathConfig { plugins: PluginsConfig; compileFiles: string[]; depModuleCompileFiles: string[]; + sdkAliasConfigPath?: string; } // ProjectConfig ends @@ -195,6 +196,7 @@ export interface ModuleInfo { language: string; dependencies?: string[]; declFilesPath?: string; + sdkAliasConfigPath?: string; } export interface Job { @@ -285,3 +287,8 @@ export interface NodeInfo { name: string; kind: AstNodeType; } + +export interface AliasConfig { + originalAPIName: string; + isStatic: boolean; +} diff --git a/ets2panda/bindings/src/lsp/generateArkTSConfig.ts b/ets2panda/bindings/src/lsp/generateArkTSConfig.ts index 9cb0874bc14ac099e6515e80109edbb16967b431..d1e70e993d61001d027319414abaed9dfc3e29a7 100644 --- a/ets2panda/bindings/src/lsp/generateArkTSConfig.ts +++ b/ets2panda/bindings/src/lsp/generateArkTSConfig.ts @@ -67,7 +67,8 @@ export function generateModuleInfo(allBuildConfig: Record, dynamicDepModuleInfos: [], language: buildConfig.language, dependencies: buildConfig.dependencies, - declFilesPath: buildConfig.declFilesPath + declFilesPath: buildConfig.declFilesPath, + sdkAliasConfigPath: buildConfig.sdkAliasConfigPath ? buildConfig.sdkAliasConfigPath : undefined }; collectDepModuleInfos(moduleInfo, allBuildConfig); return moduleInfo; diff --git a/ets2panda/bindings/src/lsp/generateBuildConfig.ts b/ets2panda/bindings/src/lsp/generateBuildConfig.ts index 0f58474a8953e4b1ea7f3a33306767ba69426a74..eca1175e683fbe2012ae79b47579295fe117309d 100644 --- a/ets2panda/bindings/src/lsp/generateBuildConfig.ts +++ b/ets2panda/bindings/src/lsp/generateBuildConfig.ts @@ -31,6 +31,7 @@ export interface ModuleDescriptor { srcPath: string; arktsversion?: string; aceModuleJsonPath?: string; + sdkAliasConfigPath?: string; } interface Json5Object { @@ -254,7 +255,8 @@ export function generateBuildConfigs( dependencies: dependencies.map((dep) => { const depModule = definedModules.find((m) => m.srcPath === dep); return depModule ? depModule.name : ''; - }) + }), + sdkAliasConfigPath: module.sdkAliasConfigPath ? module.sdkAliasConfigPath : undefined }; addPluginPathConfigs(allBuildConfigs[module.name], module); } diff --git a/ets2panda/bindings/src/lsp/lsp_helper.ts b/ets2panda/bindings/src/lsp/lsp_helper.ts index e5fab550454031e98f113fb2ad98fffe58e01319..3b854d7012dd86ef72ea710b11d85dc3ef0b9d77 100644 --- a/ets2panda/bindings/src/lsp/lsp_helper.ts +++ b/ets2panda/bindings/src/lsp/lsp_helper.ts @@ -272,7 +272,11 @@ export class Lsp { const result = new LspDefinitionData(ptr); const moduleName = this.moduleInfos[filename.valueOf()].packageName; const declgenOutDir = this.buildConfigs[moduleName].declgenOutDir; - if (result.fileName.endsWith(DECL_ETS_SUFFIX) && result.fileName.startsWith(declgenOutDir)) { + if ( + (result.fileName.endsWith(DECL_ETS_SUFFIX) && result.fileName.startsWith(declgenOutDir)) || + (this.buildConfigs[moduleName].interopApiPath && + result.fileName.startsWith(this.buildConfigs[moduleName].interopApiPath!)) + ) { let ptr: KPointer; const [declFileCfg, declFileCtx] = this.createContext(result.fileName, false); try { @@ -290,7 +294,15 @@ export class Lsp { private getDefinitionAtPositionByNodeInfos(declFilePath: String, nodeInfos: NodeInfo[]): LspDefinitionData { let ptr: KPointer; let nodeInfoPtrs: KPointer[] = []; - const sourceFilePath = this.declFileMap[declFilePath.valueOf()]; + let sourceFilePath = this.declFileMap[declFilePath.valueOf()]; + if (sourceFilePath === undefined) { + let unifiedPath = declFilePath.replace(/\\/g, '/'); + const targetSegment = 'build-tools/interop/declaration'; + if (unifiedPath.includes(targetSegment)) { + unifiedPath = unifiedPath.replace(targetSegment, ''); + sourceFilePath = path.normalize(unifiedPath); + } + } const [cfg, ctx] = this.createContext(sourceFilePath, false); try { nodeInfos.forEach((nodeInfo) => { diff --git a/ets2panda/bindings/test/expected/getCodeFixesAtPosition.json b/ets2panda/bindings/test/expected/getCodeFixesAtPosition.json index 198d10973c7959790ea9d207b926dec4b5acf505..7fb47ab103c0a48c5c46210b0f12b9cf102c7cff 100644 --- a/ets2panda/bindings/test/expected/getCodeFixesAtPosition.json +++ b/ets2panda/bindings/test/expected/getCodeFixesAtPosition.json @@ -13,15 +13,15 @@ "newText": "" } ] - } - ], - "description": "Remove the duplicate 'Entry' annotation", - "fixName": "Fix", - "fixId_": "UI_PLUGIN_SUGGEST", - "fixAllDescription_": "Fix All Description" - }, - { - "changes": [ + } + ], + "description": "Remove the duplicate 'Entry' annotation", + "fixName": "Fix", + "fixId_": "UI_PLUGIN_SUGGEST", + "fixAllDescription_": "Fix All Description" + }, + { + "changes": [ { "fileName": "getCodeFixesAtPosition1.ets", "textChanges": []