Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin support #757

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -51,6 +51,10 @@ You can turn on format-on-save on a per-language basis by scoping the setting:

`eslint`, `tslint`, and all peer dependencies required by your specific configuration must be installed locally. Global installations will not be recognized.

### Prettier Plugins

Prettier plugins are supported when using a local Prettier installation. Just install the plugin you need locally to your project, restart vscode and you are good to go.
esamattis marked this conversation as resolved.
Show resolved Hide resolved

## Settings

### Prettier's Settings
Expand Down
10 changes: 9 additions & 1 deletion src/PrettierEditProvider.ts
Expand Up @@ -6,10 +6,11 @@ import {
FormattingOptions,
CancellationToken,
TextEdit,
window,
} from 'vscode';

import { safeExecution, addToOutput, setUsedModule } from './errorHandler';
import { getParsersFromLanguageId, getConfig } from './utils';
import { getParsersFromLanguageId, getConfig, supportsLanguage } from './utils';
import { requireLocalPkg } from './requirePkg';

import {
Expand Down Expand Up @@ -103,6 +104,13 @@ async function format(
return text;
}

if (!supportsLanguage(languageId, localPrettier)) {
window.showErrorMessage(
`Prettier does not support "${languageId}". Maybe a plugin is missing from the workspace?`
);
return text;
}

const dynamicParsers = getParsersFromLanguageId(
languageId,
localPrettier,
Expand Down
47 changes: 45 additions & 2 deletions src/utils.ts
Expand Up @@ -6,6 +6,7 @@ import {
PrettierSupportInfo,
ParserOption,
} from './types.d';
import { requireLocalPkg } from './requirePkg';

const bundledPrettier = require('prettier') as Prettier;

Expand Down Expand Up @@ -35,8 +36,35 @@ export function getParsersFromLanguageId(
}

export function allEnabledLanguages(): string[] {
return getSupportLanguages().reduce(
(ids, language) => [...ids, ...(language.vscodeLanguageIds || [])],
if (!workspace.workspaceFolders) {
return getSupportLanguages().reduce(
(ids, language) => [...ids, ...(language.vscodeLanguageIds || [])],
[] as string[]
);
}

return workspace.workspaceFolders.reduce(
(ids, workspaceFolder) => {
const workspacePrettier = requireLocalPkg(
workspaceFolder.uri.fsPath,
'prettier'
) as Prettier;

const newLanguages: string[] = [];

for (const language of getSupportLanguages(workspacePrettier)) {
if (!language.vscodeLanguageIds) {
continue;
}
for (const id of language.vscodeLanguageIds) {
if (!ids.includes(id)) {
newLanguages.push(id);
}
}
}

return [...ids, ...newLanguages];
},
[] as string[]
);
}
Expand All @@ -59,3 +87,18 @@ export function getGroup(group: string): PrettierSupportInfo['languages'] {
function getSupportLanguages(prettierInstance: Prettier = bundledPrettier) {
return prettierInstance.getSupportInfo(prettierInstance.version).languages;
}

export function supportsLanguage(
vscodeLanguageId: string,
prettierInstance: Prettier
) {
return prettierInstance
.getSupportInfo(prettierInstance.version)
.languages.some(language => {
if (!language.vscodeLanguageIds) {
return false;
}

return language.vscodeLanguageIds.includes(vscodeLanguageId);
});
}