From c4bb1ee4ee990b7c08044d5c1752f5db9a3b6298 Mon Sep 17 00:00:00 2001 From: Sara Bine Date: Fri, 12 May 2017 20:27:55 -0600 Subject: [PATCH] Improve PHP-CS-Fixer config file support Add a PHP-CS-Fixer setting to specify a custom config file. If no setting is provided, check first the working directory then the project root for `.php_cs` and `.php_cs.dist`. Update the example link to PHP-CS-Fixer source to accommodate the new code. Fix capitalization in the "Allow risky rules" description. --- docs/add-languages-and-beautifiers.md | 2 +- docs/options.md | 53 ++++++++++++++++++++++++++- src/beautifiers/php-cs-fixer.coffee | 15 ++++++-- src/languages/php.coffee | 7 +++- src/options.json | 18 ++++++++- 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/docs/add-languages-and-beautifiers.md b/docs/add-languages-and-beautifiers.md index 056d9b0..b702f91 100644 --- a/docs/add-languages-and-beautifiers.md +++ b/docs/add-languages-and-beautifiers.md @@ -35,7 +35,7 @@ Now your Language is available and can be detected and beautifiers can support i 2. Implement beautifier: - See examples of beautifiers: - Prettydiff is a good example of complex options: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/prettydiff.coffee - - PHP-CS-Fixer is a good example of a CLI beautifier with arguments: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L15-L39 + - PHP-CS-Fixer is a good example of a CLI beautifier with arguments: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/php-cs-fixer.coffee#L15-L47 - `options` - the key represents the Language's name. The value could be `true` (supports all options), `false` (supports language, with no options), or an `object` whose keys are option keys and values are complex mappings. If you need to use these, let me know. `true` is probably what you want. - The `beautify` function should return a `Promise` (use `@Promise` as shown). The arguments passed are: - __`text`__ - the source code from Atom's Text Editor diff --git a/docs/options.md b/docs/options.md index 76b5ae0..835c68f 100644 --- a/docs/options.md +++ b/docs/options.md @@ -9571,6 +9571,7 @@ Specify a configuration file which will override the default name of .perltidyrc | `default_beautifier` | :white_check_mark: | :white_check_mark: | :white_check_mark: | | `beautify_on_save` | :white_check_mark: | :white_check_mark: | :white_check_mark: | | `allow_risky` | :white_check_mark: | :x: | :x: | +| `cs_fixer_config_file` | :white_check_mark: | :x: | :x: | | `cs_fixer_path` | :white_check_mark: | :x: | :x: | | `cs_fixer_version` | :white_check_mark: | :x: | :x: | | `fixers` | :white_check_mark: | :x: | :x: | @@ -9655,7 +9656,7 @@ Automatically beautify PHP files on save **Description**: -allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer) +Allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer) **Example `.jsbeautifyrc` Configuration** @@ -9667,6 +9668,30 @@ allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer } ``` +##### [PHP-CS-Fixer Config File](#php-cs-fixer-config-file) + +**Namespace**: `php` + +**Key**: `cs_fixer_config_file` + +**Type**: `string` + +**Supported Beautifiers**: [`PHP-CS-Fixer`](#php-cs-fixer) + +**Description**: + +Path to php-cs-fixer config file. Will use local `.php_cs` or `.php_cs.dist` if found in the working directory or project root. (Supported by PHP-CS-Fixer) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "php": { + "cs_fixer_config_file": "" + } +} +``` + ##### [PHP-CS-Fixer Path](#php-cs-fixer-path) **Namespace**: `php` @@ -17409,6 +17434,30 @@ Absolute path to the `php-cs-fixer` CLI executable (Supported by PHP-CS-Fixer) } ``` +##### [PHP-CS-Fixer Config File](#php-cs-fixer-config-file) + +**Namespace**: `php` + +**Key**: `cs_fixer_config_file` + +**Type**: `string` + +**Supported Beautifiers**: [`PHP-CS-Fixer`](#php-cs-fixer) + +**Description**: + +Path to php-cs-fixer config file. Will use local `.php_cs` or `.php_cs.dist` if found in the working directory or project root. (Supported by PHP-CS-Fixer) + +**Example `.jsbeautifyrc` Configuration** + +```json +{ + "php": { + "cs_fixer_config_file": "" + } +} +``` + ##### [Fixers](#fixers) **Namespace**: `php` @@ -17497,7 +17546,7 @@ Add rule(s). i.e. line_ending,-full_opening_tag,@PSR2 (PHP-CS-Fixer 2 only) (Sup **Description**: -allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer) +Allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer) **Example `.jsbeautifyrc` Configuration** diff --git a/src/beautifiers/php-cs-fixer.coffee b/src/beautifiers/php-cs-fixer.coffee index 9fa408d..db46818 100644 --- a/src/beautifiers/php-cs-fixer.coffee +++ b/src/beautifiers/php-cs-fixer.coffee @@ -17,6 +17,7 @@ module.exports = class PHPCSFixer extends Beautifier rules: true cs_fixer_path: true cs_fixer_version: true + cs_fixer_config_file: true allow_risky: true level: true fixers: true @@ -24,12 +25,20 @@ module.exports = class PHPCSFixer extends Beautifier beautify: (text, language, options, context) -> @debug('php-cs-fixer', options) version = options.cs_fixer_version + configFiles = ['.php_cs', '.php_cs.dist'] + + # Find a config file in the working directory if a custom one was not provided + if not options.cs_fixer_config_file + options.cs_fixer_config_file = if context? and context.filePath? then @findFile(path.dirname(context.filePath), configFiles) + + # Try again to find a config file in the project root + if not options.cs_fixer_config_file + options.cs_fixer_config_file = @findFile(atom.project.getPaths()[0], configFiles) - configFile = if context? and context.filePath? then @findFile(path.dirname(context.filePath), '.php_cs') phpCsFixerOptions = [ "fix" "--rules=#{options.rules}" if options.rules - "--config=#{configFile}" if configFile + "--config=#{options.cs_fixer_config_file}" if options.cs_fixer_config_file "--allow-risky=#{options.allow_risky}" if options.allow_risky "--using-cache=no" ] @@ -38,7 +47,7 @@ module.exports = class PHPCSFixer extends Beautifier "fix" "--level=#{options.level}" if options.level "--fixers=#{options.fixers}" if options.fixers - "--config-file=#{configFile}" if configFile + "--config-file=#{options.cs_fixer_config_file}" if options.cs_fixer_config_file ] runOptions = { ignoreReturnCode: true diff --git a/src/languages/php.coffee b/src/languages/php.coffee index e22873f..ee6d8f0 100644 --- a/src/languages/php.coffee +++ b/src/languages/php.coffee @@ -32,6 +32,11 @@ module.exports = { type: 'integer' default: 2 enum: [1, 2] + cs_fixer_config_file: + title: "PHP-CS-Fixer Config File" + type: 'string' + default: "" + description: "Path to php-cs-fixer config file. Will use local `.php_cs` or `.php_cs.dist` if found in the working directory or project root." fixers: type: 'string' default: "" @@ -49,7 +54,7 @@ module.exports = { type: 'string' default: "no" enum: ["no", "yes"] - description: "allow risky rules to be applied (PHP-CS-Fixer 2 only)" + description: "Allow risky rules to be applied (PHP-CS-Fixer 2 only)" phpcbf_path: title: "PHPCBF Path" type: 'string' diff --git a/src/options.json b/src/options.json index 7ead5ed..46811b9 100644 --- a/src/options.json +++ b/src/options.json @@ -5810,6 +5810,20 @@ }, "description": " (Supported by PHP-CS-Fixer)" }, + "cs_fixer_config_file": { + "title": "PHP-CS-Fixer Config File", + "type": "string", + "default": "", + "description": "Path to php-cs-fixer config file. Will use local `.php_cs` or `.php_cs.dist` if found in the working directory or project root. (Supported by PHP-CS-Fixer)", + "beautifiers": [ + "PHP-CS-Fixer" + ], + "key": "cs_fixer_config_file", + "language": { + "name": "PHP", + "namespace": "php" + } + }, "fixers": { "type": "string", "default": "", @@ -5860,7 +5874,7 @@ "no", "yes" ], - "description": "allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer)", + "description": "Allow risky rules to be applied (PHP-CS-Fixer 2 only) (Supported by PHP-CS-Fixer)", "beautifiers": [ "PHP-CS-Fixer" ], @@ -9037,7 +9051,7 @@ "properties": { "padding": { "type": "integer", - "default": null, + "default": 0, "minimum": 0, "description": "The amount of padding to add next to each line. (Supported by align-yaml)", "title": "Padding",