Skip to content

Commit 12c9365

Browse files
fix: fix: fix hms detect in language server, get ets.hmsPath from vscode settings and add ets.hmsPath to initialize options (#296)
1 parent 79e6ecb commit 12c9365

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

.changeset/tricky-otters-sin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@arkts/language-server": patch
3+
"@arkts/shared": patch
4+
"vscode-naily-ets": patch
5+
---
6+
7+
fix: fix hms detect in language server, get ets.hmsPath from vscode settings and add ets.hmsPath to initialize options

packages/language-server/src/classes/config-resolver.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export class ConfigResolver {
5252
}
5353

5454
getHmsSdkPath(): string | undefined {
55-
return this.params.initializationOptions?.ets?.hmsSdkPath
56-
? path.resolve(this.params.initializationOptions?.ets?.hmsSdkPath)
57-
: this.params.initializationOptions?.ets?.hmsSdkPath
55+
return this.params.initializationOptions?.ets?.hmsPath
56+
? path.resolve(this.params.initializationOptions?.ets?.hmsPath)
57+
: this.params.initializationOptions?.ets?.hmsPath
5858
}
5959

6060
getEtsLoaderPath(): string {
@@ -137,28 +137,36 @@ export class ConfigResolver {
137137
}
138138

139139
private async hmsToTypeScriptCompilerOptionsPaths(): Promise<import('typescript').MapLike<string[]>> {
140-
const hmsSdkPath = this.getHmsSdkPath()
141-
if (!hmsSdkPath) return {}
142-
const hmsApiFolder = Utils.joinPath(URI.file(hmsSdkPath), 'api')
143-
const hmsKitsFolder = Utils.joinPath(URI.file(hmsSdkPath), 'kits')
144-
if (!hmsApiFolder || !hmsKitsFolder) return {}
145-
146-
const paths: import('typescript').MapLike<string[]> = {}
147-
const apiFiles = await this.fs.readDirectory(hmsApiFolder)
148-
const kitsFiles = await this.fs.readDirectory(hmsKitsFolder)
149-
for (const [fileNameWithExtension, fileType] of apiFiles) {
150-
if (fileType !== FileType.File) continue
151-
const fileName = this.getFileNameWithoutExtension(fileNameWithExtension)
152-
paths[fileName] = [Utils.joinPath(hmsApiFolder, fileNameWithExtension).fsPath]
153-
paths[`${fileName}/*`] = [Utils.joinPath(hmsApiFolder, fileNameWithExtension, '*').fsPath]
140+
try {
141+
const hmsSdkPath = this.getHmsSdkPath()
142+
if (!hmsSdkPath) return {}
143+
const hmsApiFolder = Utils.joinPath(URI.file(hmsSdkPath), 'ets', 'api')
144+
const hmsKitsFolder = Utils.joinPath(URI.file(hmsSdkPath), 'ets', 'kits')
145+
if (!hmsApiFolder || !hmsKitsFolder) return {}
146+
147+
const paths: import('typescript').MapLike<string[]> = {}
148+
const apiFiles = await this.fs.readDirectory(hmsApiFolder)
149+
const kitsFiles = await this.fs.readDirectory(hmsKitsFolder)
150+
for (const [fileNameWithExtension, fileType] of apiFiles) {
151+
if (fileType !== FileType.File) continue
152+
const fileName = this.getFileNameWithoutExtension(fileNameWithExtension)
153+
paths[fileName] = [Utils.joinPath(hmsApiFolder, fileNameWithExtension).fsPath]
154+
paths[`${fileName}/*`] = [Utils.joinPath(hmsApiFolder, fileNameWithExtension, '*').fsPath]
155+
}
156+
for (const [fileNameWithExtension, fileType] of kitsFiles) {
157+
if (fileType !== FileType.File) continue
158+
const fileName = this.getFileNameWithoutExtension(fileNameWithExtension)
159+
paths[fileName] = [Utils.joinPath(hmsKitsFolder, fileNameWithExtension).fsPath]
160+
paths[`${fileName}/*`] = [Utils.joinPath(hmsKitsFolder, fileNameWithExtension, '*').fsPath]
161+
}
162+
return paths
154163
}
155-
for (const [fileNameWithExtension, fileType] of kitsFiles) {
156-
if (fileType !== FileType.File) continue
157-
const fileName = this.getFileNameWithoutExtension(fileNameWithExtension)
158-
paths[fileName] = [Utils.joinPath(hmsKitsFolder, fileNameWithExtension).fsPath]
159-
paths[`${fileName}/*`] = [Utils.joinPath(hmsKitsFolder, fileNameWithExtension, '*').fsPath]
164+
catch (error) {
165+
this.logger.getConsola().error(`Failed to detect ets.hmsPath, please check the ets.hmsPath in the initialization options: ${error}`)
166+
if (error instanceof Error) this.logger.getConsola().error(error.stack)
167+
this.connection.window.showErrorMessage(`Failed to detect ets.hmsPath, please check the ets.hmsPath in the initialization options.`)
168+
return {}
160169
}
161-
return paths
162170
}
163171

164172
async getPaths(): Promise<ets.MapLike<string[]>> {

packages/shared/src/client-options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ export interface EtsServerClientOptions {
55
ets?: {
66
/** The currently ohos sdk path. If not exists the lsp will not work. */
77
sdkPath: string | undefined
8+
/** The currently hms sdk path. If not exists the lsp will not work. */
9+
hmsPath: string | undefined
810
}
911
}

packages/vscode/src/language-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ export class EtsLanguageServer extends LanguageServerContext implements Command,
137137
throw new Error(this.translator.t('sdk.error.validSdkPath'))
138138
}
139139

140+
const hmsPath = await this.sdkManager.getAnalyzedHmsSdkPath().then(uri => uri?.fsPath)
141+
140142
return {
141143
documentSelector: [
142144
{ language: 'ets' },
@@ -146,7 +148,7 @@ export class EtsLanguageServer extends LanguageServerContext implements Command,
146148
outputChannel: this.getOutputChannel(),
147149
initializationOptions: {
148150
debug: vscode.workspace.getConfiguration('ets').get<boolean>('lspDebugMode'),
149-
ets: { sdkPath },
151+
ets: { sdkPath, hmsPath },
150152
} satisfies EtsServerClientOptions,
151153
synchronize: {
152154
fileEvents: [

0 commit comments

Comments
 (0)