Skip to content

Commit

Permalink
Resolve config for the active file
Browse files Browse the repository at this point in the history
... not for the workspace directory.

Passing a directory to resolveConfig was broken in prettier v3.1.1, as reported here:
prettier/prettier#15879
  • Loading branch information
elieux committed Jan 18, 2024
1 parent 70604a3 commit 73498f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

## [Unreleased]

Fixed the issue where VSCode was misrecognizing the path in output panel due to added quotes.
- Fixed the issue where VSCode was misrecognizing the path in output panel due to added quotes.
- Fixed config resolution that caused plugins to be ignored when using Prettier 3.1.1 or later. (#3252)

## [10.1.0]

Expand Down
20 changes: 11 additions & 9 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default class PrettierEditService implements Disposable {

const selectors = await this.getSelectors(
prettierInstance,
document.uri,
workspaceFolder.uri
);

Expand Down Expand Up @@ -255,22 +256,23 @@ export default class PrettierEditService implements Disposable {
*/
private getSelectors = async (
prettierInstance: PrettierModule | PrettierInstance,
uri?: Uri
documentUri?: Uri,
workspaceFolderUri?: Uri
): Promise<ISelectors> => {
const plugins: (string | PrettierPlugin)[] = [];

// Prettier v3 does not load plugins automatically
// So need to resolve config to get plugins info.
if (
uri &&
documentUri &&
"resolveConfig" in prettierInstance &&
isAboveV3(prettierInstance.version)
) {
const resolvedConfig = await this.moduleResolver.resolveConfig(
prettierInstance,
uri,
uri.fsPath,
getConfig(uri)
documentUri,
documentUri.fsPath,
getConfig(documentUri)
);
if (resolvedConfig === "error") {
this.statusBar.update(FormatterStatus.Error);
Expand Down Expand Up @@ -306,22 +308,22 @@ export default class PrettierEditService implements Disposable {
const { documentSelectors } = getConfig();

// Language selector for file extensions
const extensionLanguageSelector: DocumentFilter[] = uri
const extensionLanguageSelector: DocumentFilter[] = workspaceFolderUri
? this.allExtensions.length === 0
? []
: [
{
pattern: `${uri.fsPath}/**/*.{${this.allExtensions
pattern: `${workspaceFolderUri.fsPath}/**/*.{${this.allExtensions
.map((e) => e.substring(1))
.join(",")}}`,
scheme: "file",
},
]
: [];

const customLanguageSelectors: DocumentFilter[] = uri
const customLanguageSelectors: DocumentFilter[] = workspaceFolderUri
? documentSelectors.map((pattern) => ({
pattern: `${uri.fsPath}/${pattern}`,
pattern: `${workspaceFolderUri.fsPath}/${pattern}`,
scheme: "file",
}))
: [];
Expand Down

0 comments on commit 73498f8

Please sign in to comment.