Skip to content

Commit

Permalink
Add plugin support
Browse files Browse the repository at this point in the history
Closes #395
Closes #612
  • Loading branch information
esamattis committed Mar 17, 2019
1 parent 5800b23 commit 81de68e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
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.

## 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
48 changes: 46 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,36 @@ 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)) {
console.log('NEW', id);
newLanguages.push(id);
}
}
}

return [...ids, ...newLanguages];
},
[] as string[]
);
}
Expand All @@ -59,3 +88,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);
});
}

0 comments on commit 81de68e

Please sign in to comment.