Skip to content

Commit

Permalink
Added prettier config file option. Closes #254
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Nov 2, 2019
1 parent ce36cff commit 4bae34c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to the "prettier-vscode" extension will be documented in thi
- Registers `.graphql` files as `graphql` language in order to provide formatting. (#989)
- Ignore files are only read from the workspace root folder to behave the [same as prettier](https://github.com/prettier/prettier/issues/4081).
- Added configuration option `prettier.prettierPath` to override module resolution.
- Added configuration option `prettier.configPath` to override configuration file resolution.

## [2.3.0]

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ Other settings will only be fallbacks in case they could not be inferred from st

Require a 'prettierconfig' to format

#### prettier.ignorePath (default: .prettierignore)
#### prettier.ignorePath

Supply the path to an ignore file such as `.gitignore` or `.prettierignore`.
Files which match will not be formatted. Set to `null` to not read ignore files. Restart required.

#### prettier.configPath

Supply a custom path to the prettier configuration file.

#### prettier.prettierPath

Supply a custom path to the prettier module.
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
"description": "%ext.config.prettierPath%",
"scope": "resource"
},
"prettier.configPath": {
"type": "string",
"description": "%ext.config.configPath%",
"scope": "resource"
},
"prettier.printWidth": {
"type": "integer",
"default": 80,
Expand Down
43 changes: 22 additions & 21 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{
"ext.config.title": "Prettier - Code formatter configuration",
"ext.config.arrowParens": "Include parentheses around a sole arrow function parameter",
"ext.config.bracketSpacing": "Controls the printing of spaces inside object literals",
"ext.config.configPath": "Path to the prettier configuration file",
"ext.config.disableLanguages": "A list of languages IDs to disable this extension on",
"ext.config.requireConfig": "Require a 'prettierconfig' to format",
"ext.config.endOfLine": "Specify the end of line used by prettier",
"ext.config.eslintIntegration": "Use 'prettier-eslint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from eslint rules.",
"ext.config.failedToLoadModule": "Failed to load module. If you are referencing prettier in your package.json, ensure you have run `npm install`",
"ext.config.htmlWhitespaceSensitivity": "Specify the global whitespace sensitivity for HTML files.\n Valid options: \n'css' - Respect the default value of CSS display property. \n'strict' - Whitespaces are considered sensitive. \n'ignore' - Whitespaces are considered insensitive.",
"ext.config.ignorePath": "Path to a .prettierignore or similar file",
"ext.config.prettierPath": "Path to the prettier module",
"ext.config.printWidth": "Fit code within this line limit",
"ext.config.tabWidth": "Number of spaces it should use per tab",
"ext.config.singleQuote": "If true, will use single instead of double quotes",
"ext.config.trailingComma": "Controls the printing of trailing commas wherever possible.\n Valid options:\n 'none' - No trailing commas\n 'es5' - Trailing commas where valid in ES5 (objects, arrays, etc)\n 'all' - Trailing commas wherever possible (function arguments)",
"ext.config.bracketSpacing": "Controls the printing of spaces inside object literals",
"ext.config.jsxBracketSameLine": "If true, puts the `>` of a multi-line jsx element at the end of the last line instead of being alone on the next line",
"ext.config.jsxSingleQuote": "Use single quotes instead of double quotes in JSX",
"ext.config.lintDeprecationMessage": "This setting has been deprecated. See: https://github.com/prettier/prettier-vscode#vscode-eslint-and-tslint-integration",
"ext.config.outdatedPrettiereVersion": "Your project is configured to use version {{version}} of prettier. You may encounter issues. It is recommended you upgrade to the latest version of prettier.",
"ext.config.parser": "Override the parser. You shouldn't have to change this setting.",
"ext.config.semi": "Whether to add a semicolon at the end of every line",
"ext.config.useTabs": "Indent lines with tabs",
"ext.config.prettierPath": "Path to the prettier module",
"ext.config.prettierSettingsDeprecatedMessage": "This extension no longer supports setting Prettier options using VS Code configuration. Use the prettier configuration file instead. https://prettier.io/docs/en/configuration.html",
"ext.config.printWidth": "Fit code within this line limit",
"ext.config.proseWrap": "(Markdown) wrap prose over multiple lines",
"ext.config.arrowParens": "Include parentheses around a sole arrow function parameter",
"ext.config.jsxSingleQuote": "Use single quotes instead of double quotes in JSX",
"ext.config.htmlWhitespaceSensitivity": "Specify the global whitespace sensitivity for HTML files.\n Valid options: \n'css' - Respect the default value of CSS display property. \n'strict' - Whitespaces are considered sensitive. \n'ignore' - Whitespaces are considered insensitive.",
"ext.config.endOfLine": "Specify the end of line used by prettier",
"ext.config.quoteProps": "Change when properties in objects are quoted",
"ext.config.eslintIntegration": "Use 'prettier-eslint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from eslint rules.",
"ext.config.tslintIntegration": "Use 'prettier-tslint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from tslint rules.",
"ext.config.requireConfig": "Require a 'prettierconfig' to format",
"ext.config.semi": "Whether to add a semicolon at the end of every line",
"ext.config.singleQuote": "If true, will use single instead of double quotes",
"ext.config.stylelintIntegration": "Use 'prettier-stylelint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from stylelint rules.",
"ext.config.lintDeprecationMessage": "This setting has been deprecated. See: https://github.com/prettier/prettier-vscode#vscode-eslint-and-tslint-integration",
"ext.config.prettierSettingsDeprecatedMessage": "This extension no longer supports setting Prettier options using VS Code configuration. Use the prettier configuration file instead. https://prettier.io/docs/en/configuration.html",
"ext.config.outdatedPrettiereVersion": "Your project is configured to use version {{version}} of prettier. You may encounter issues. It is recommended you upgrade to the latest version of prettier.",
"ext.createConfigFile.title": "Prettier: Create Configuration File",
"ext.config.failedToLoadModule": "Failed to load module. If you are referencing prettier in your package.json, ensure you have run `npm install`"
"ext.config.tabWidth": "Number of spaces it should use per tab",
"ext.config.title": "Prettier - Code formatter configuration",
"ext.config.trailingComma": "Controls the printing of trailing commas wherever possible.\n Valid options:\n 'none' - No trailing commas\n 'es5' - Trailing commas where valid in ES5 (objects, arrays, etc)\n 'all' - Trailing commas wherever possible (function arguments)",
"ext.config.tslintIntegration": "Use 'prettier-tslint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from tslint rules.",
"ext.config.useTabs": "Indent lines with tabs",
"ext.createConfigFile.title": "Prettier: Create Configuration File"
}
15 changes: 5 additions & 10 deletions src/ConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ export class ConfigResolver {
public async getPrettierOptions(
fileName: string,
parser: prettier.BuiltInParserName,
rangeFormattingOptions?: RangeFormattingOptions,
editorconfig?: boolean
options: prettier.ResolveConfigOptions,
rangeFormattingOptions?: RangeFormattingOptions
): Promise<Partial<prettier.Options>> {
const { config: configOptions, error } = await this.resolveConfig(
fileName,
{
editorconfig
}
options
);

if (error) {
Expand Down Expand Up @@ -62,13 +60,10 @@ export class ConfigResolver {
*/
private async resolveConfig(
filePath: string,
options?: { editorconfig?: boolean }
options?: prettier.ResolveConfigOptions
): Promise<IResolveConfigResult> {
try {
const config = (await prettier.resolveConfig(
filePath,
options
)) as prettier.Options;
const config = await prettier.resolveConfig(filePath, options);
return { config };
} catch (error) {
return { config: null, error };
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
workspace
// tslint:disable-next-line: no-implicit-dependencies
} from "vscode";
import { getConfig } from "./ConfigResolver";
import { LanguageResolver } from "./LanguageResolver";
import { LoggingService } from "./LoggingService";
import { ModuleResolver } from "./ModuleResolver";
import EditProvider from "./PrettierEditProvider";
import { getConfig } from "./util";

interface ISelectors {
rangeLanguageSelector: DocumentSelector;
Expand Down
16 changes: 9 additions & 7 deletions src/PrettierEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
TextEdit
// tslint:disable-next-line: no-implicit-dependencies
} from "vscode";
import {
ConfigResolver,
getConfig,
RangeFormattingOptions
} from "./ConfigResolver";
import { ConfigResolver, RangeFormattingOptions } from "./ConfigResolver";
import { IgnorerResolver } from "./IgnorerResolver";
import { LanguageResolver } from "./LanguageResolver";
import { LoggingService } from "./LoggingService";
Expand All @@ -25,6 +21,7 @@ import {
PrettierEslintFormat,
PrettierTslintFormat
} from "./types.d";
import { getConfig, getWorkspaceRelativePath } from "./util";

export default class PrettierEditProvider
implements
Expand Down Expand Up @@ -154,8 +151,13 @@ export default class PrettierEditProvider
const prettierOptions = await this.configResolver.getPrettierOptions(
fileName,
parser as prettier.BuiltInParserName,
rangeFormattingOptions,
true /* use editorconfig */
{
config: vscodeConfig.configPath
? getWorkspaceRelativePath(fileName, vscodeConfig.configPath)
: undefined,
editorconfig: true
},
rangeFormattingOptions
);

this.loggingService.appendLine("Prettier Options:", "INFO");
Expand Down
2 changes: 1 addition & 1 deletion src/Watchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const fileWatcher = (listener: () => any) => {
// Listen for changes that disable/enable languages
const configWatcher = (listener: () => any) =>
workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration("prettier.disableLanguages")) {
if (event.affectsConfiguration("prettier")) {
listener();
}
});
Expand Down
7 changes: 5 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ interface IExtensionConfig {
/**
* Path to prettier module.
*/
prettierPath: string;

prettierPath: string | undefined;
/**
* Path to prettier configuration file.
*/
configPath: string | undefined;
/**
* If true will skip formatting if a prettierconfig isn't found.
*/
Expand Down

0 comments on commit 4bae34c

Please sign in to comment.