(
metadata: FileMetadata,
options: FormattingOptions = {},
)
| 386 | } |
| 387 | |
| 388 | async formatDocument( |
| 389 | metadata: FileMetadata, |
| 390 | options: FormattingOptions = {}, |
| 391 | ): Promise<boolean> { |
| 392 | const { uri: originalUri, languageId, languageName, view, file } = metadata; |
| 393 | |
| 394 | const effectiveLang = safeString(languageId ?? languageName).toLowerCase(); |
| 395 | if (!effectiveLang || !view) return false; |
| 396 | |
| 397 | const servers = serverRegistry.getServersForLanguage(effectiveLang); |
| 398 | if (!servers.length) return false; |
| 399 | |
| 400 | for (const server of servers) { |
| 401 | if (isSettingsOrKeybindingsFile(server, originalUri, file)) { |
| 402 | continue; |
| 403 | } |
| 404 | if (!supportsBuiltinFormatting(server)) continue; |
| 405 | try { |
| 406 | const target = await this.#resolveRuntimeTarget(server, { |
| 407 | uri: originalUri, |
| 408 | file, |
| 409 | view, |
| 410 | languageId: effectiveLang, |
| 411 | rootUri: metadata.rootUri, |
| 412 | }); |
| 413 | if (!target) { |
| 414 | console.warn( |
| 415 | `Cannot resolve document URI for formatting with ${server.id}: ${originalUri}`, |
| 416 | ); |
| 417 | continue; |
| 418 | } |
| 419 | const normalizedUri = target.documentUri; |
| 420 | const context: RootUriContext = { |
| 421 | uri: normalizedUri, |
| 422 | languageId: effectiveLang, |
| 423 | view, |
| 424 | file, |
| 425 | rootUri: target.normalizedRootUri ?? undefined, |
| 426 | }; |
| 427 | const state = await this.#ensureClient(server, context, target); |
| 428 | const capabilities = state.client.serverCapabilities; |
| 429 | if (!capabilities?.documentFormattingProvider) continue; |
| 430 | state.attach(normalizedUri, view); |
| 431 | const plugin = LSPPlugin.get(view); |
| 432 | if (!plugin) continue; |
| 433 | plugin.client.sync(); |
| 434 | const edits = await state.client.request< |
| 435 | { textDocument: { uri: string }; options: FormattingOptions }, |
| 436 | TextEdit[] | null |
| 437 | >("textDocument/formatting", { |
| 438 | textDocument: { uri: normalizedUri }, |
| 439 | options: buildFormattingOptions(view, options), |
| 440 | }); |
| 441 | if (!edits || !edits.length) { |
| 442 | plugin.client.sync(); |
| 443 | return true; |
| 444 | } |
| 445 | const applied = applyTextEdits(plugin, view, edits); |
no test coverage detected