Skip to content

Commit

Permalink
Expand usage of local prettier instance
Browse files Browse the repository at this point in the history
Update the status bar and edit provider logic to use the local Prettier
instance when possible.
  • Loading branch information
jarrodldavis committed Apr 29, 2019
1 parent fe9cf2d commit 5927e46
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
} from 'vscode';

import { allEnabledLanguages, getConfig } from './utils';
import { PrettierVSCodeConfig } from './types';
import { PrettierVSCodeConfig, Prettier } from './types';
import { requireLocalPkg } from './requirePkg';

let statusBarItem: StatusBarItem;
let outputChannel: OutputChannel;
Expand All @@ -34,7 +35,8 @@ function toggleStatusBarItem(editor: TextEditor | undefined): void {
return;
}

const score = languages.match(allEnabledLanguages(), editor.document);
const prettierInstance = requireLocalPkg(editor.document.uri.fsPath, 'prettier') as Prettier;
const score = languages.match(allEnabledLanguages(prettierInstance), editor.document);
const disabledLanguages: PrettierVSCodeConfig["disableLanguages"] = getConfig(editor.document.uri).disableLanguages;

if (score > 0 && !disabledLanguages.includes(editor.document.languageId)) {
Expand Down
15 changes: 14 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
} from './utils';
import configFileListener from './configCacheHandler';
import ignoreFileHandler from './ignoreFileHandler';
import { requireLocalPkg } from './requirePkg';
import { Prettier } from './types.d';

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

interface Selectors {
rangeLanguageSelector: DocumentSelector;
Expand All @@ -40,7 +44,16 @@ function disposeHandlers() {
* Build formatter selectors
*/
function selectors(): Selectors {
const allLanguages = allEnabledLanguages();
let prettierInstance: Prettier;

const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders[0]) {
prettierInstance = requireLocalPkg(workspaceFolders[0].uri.fsPath, 'prettier') as Prettier;
} else {
prettierInstance = bundledPrettier;
}

const allLanguages = allEnabledLanguages(prettierInstance);
const allRangeLanguages = rangeSupportedLanguages();
const { disableLanguages } = getConfig();
const globalLanguageSelector = allLanguages.filter(
Expand Down
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function getParsersFromLanguageId(
return language.parsers;
}

export function allEnabledLanguages(): string[] {
return getSupportLanguages().reduce(
export function allEnabledLanguages(prettierInstance: Prettier): string[] {
return getSupportLanguages(prettierInstance).reduce(
(ids, language) => [...ids, ...(language.vscodeLanguageIds || [])],
[] as string[]
);
Expand All @@ -52,11 +52,11 @@ export function rangeSupportedLanguages(): string[] {
];
}

export function getGroup(group: string): PrettierSupportInfo['languages'] {
return getSupportLanguages().filter(language => language.group === group);
export function getGroup(group: string, prettierInstance: Prettier): PrettierSupportInfo['languages'] {
return getSupportLanguages(prettierInstance).filter(language => language.group === group);
}

function getSupportLanguages(prettierInstance: Prettier = bundledPrettier) {
function getSupportLanguages(prettierInstance: Prettier) {
// prettier.getSupportInfo was added in [email protected]
if (prettierInstance.getSupportInfo) {
return prettierInstance.getSupportInfo(prettierInstance.version).languages;
Expand Down

0 comments on commit 5927e46

Please sign in to comment.