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 prettier beautifier and bracket_spacing option #2047

Merged
merged 11 commits into from
Mar 1, 2018
Merged

Add prettier beautifier and bracket_spacing option #2047

merged 11 commits into from
Mar 1, 2018

Conversation

stevenzeck
Copy link
Contributor

@stevenzeck stevenzeck commented Feb 19, 2018

What does this implement/fix? Explain your changes.

  • Adds a new option, bracket_spacing, to print spaces between brackets in object literals for JavaScript
  • Adds Prettier as a supported beautifier for JavaScript, CSS, LESS, SCSS, Vue, JSON, and Markdown.
    ...

Does this close any currently open issues?

Closes #1497
Closes #802
...

Any other comments?

...

Checklist

Check all those that are applicable and complete.

  • Merged with latest master branch
  • Regenerate documentation with npm run docs
  • Add change details to CHANGELOG.md under "Next" section
  • Added examples for testing to examples/ directory
  • Travis CI passes (Mac support)
  • AppVeyor passes (Windows support)

_:
tabWidth: "indent_size"
useTabs: "indent_with_tabs"
JavaScript:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript is also a Prettier supported language.

options: {
_:
tabWidth: "indent_size"
useTabs: "indent_with_tabs"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend something like https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/prettydiff.coffee#L10-L13

useTabs: ["indent_with_tabs", "indent_char", (indent_with_tabs, indent_char) ->
  return (indent_with_tabs is true) or (indent_char is "\t")
]

which is the following in JavaScript:

 useTabs: [
    "indent_with_tabs",
    "indent_char",
    function(indent_with_tabs,
    indent_char) {
      return (indent_with_tabs === true) || (indent_char === "\t");
    }
  ]

@@ -108,5 +108,9 @@ module.exports = {
default: "System Default"
enum: ["CRLF","LF","System Default"]
description: "Override EOL from line-ending-selector"
bracket_spacing:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about object_curly_spacing to align with Unibeautify and ESLint? See https://unibeautify.com/docs/option-object-curly-spacing.html

Or object_bracket_spacing?

src/options.json Outdated
"description": "Insert spaces between brackets in object literals (Supported by Coffee Formatter)",
"title": "Bracket spacing",
"beautifiers": [
"Coffee Formatter"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange. Should be Prettier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's under the CoffeeScript language, which falls back to JavaScript.

@stevenzeck
Copy link
Contributor Author

stevenzeck commented Feb 19, 2018

@Glavin001 made the changes you requested. Let me know what you want to do about the option being included in all of the languages that have js as a fallback.

The Travis build that fails will be fixed once #2048 is merged, so I'm x'ing that one.

@Glavin001
Copy link
Owner

@szeck87 : Merged #2048. Please update this PR and we can see Travis CI passing. Thanks!

@stevenzeck
Copy link
Contributor Author

The build passed, but I just noticed AB is not reading from the configuration file for prettier. Let me fix that.

@stevenzeck
Copy link
Contributor Author

@Glavin001 good to go.

Copy link
Owner

@Glavin001 Glavin001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation shows beautifiers supporting the new object_curly_spacing option which I know do not.

docs/options.md Outdated

**Type**: `boolean`

**Supported Beautifiers**: [`TypeScript Formatter`](#typescript-formatter)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this beautifier supports this option.

docs/options.md Outdated

**Type**: `boolean`

**Supported Beautifiers**: [`Vue Beautifier`](#vue-beautifier)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this beautifier supports this option.

docs/options.md Outdated

**Type**: `boolean`

**Supported Beautifiers**: [`JS Beautify`](#js-beautify)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this beautifier supports this option.

docs/options.md Outdated

**Type**: `boolean`

**Supported Beautifiers**: [`JS Beautify`](#js-beautify)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this beautifier supports this option.

@Glavin001
Copy link
Owner

@szeck87 : For example: https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/typescript-formatter.coffee#L8-L9

This is false. TypeScript Formatter does NOT support all options therefore should not be true.

Copy link
Owner

@Glavin001 Glavin001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it would be better to revert the last commit about fixing the beautifier support. We can hold this off until a future PR then since it is more complicated than anticipated.

@@ -7,7 +7,7 @@ module.exports = class CoffeeFormatter extends Beautifier
link: "https://github.com/Glavin001/Coffee-Formatter"

options: {
CoffeeScript: true
CoffeeScript: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@@ -5,7 +5,7 @@ module.exports = class TypeScriptFormatter extends Beautifier
name: "TypeScript Formatter"
link: "https://github.com/vvakame/typescript-formatter"
options: {
TypeScript: true
TypeScript: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope.

See rest of file:

        if options.indent_with_tabs
          opts.convertTabsToSpaces = false
        else
          opts.tabSize = options.tab_width or options.indent_size
          opts.indentSize = options.indent_size

Firstly, this beautifier supports indent_with_tabs, tab_with, and indent_size.

Secondly, I expect TypeScript and TSX languages to have the same support, unless otherwise specified.

@@ -6,7 +6,7 @@ module.exports = class VueBeautifier extends Beautifier
link: "https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/vue-beautifier.coffee"

options:
Vue: true
Vue: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this uses JS Beautify may as well leave as true.

else
reject(new Error("Unknown language for Prettier"))

filePath = atom.workspace.getActiveTextEditor().getPath()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use context.filePath instead.
See

beautify: (text, language, options, context) ->
cwd = context.filePath and path.dirname context.filePath
for example.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does Prettier resolveConfig do if filePath = undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns null if it can't find anything.

@@ -2,7 +2,7 @@ module.exports = {

name: "TSX"
namespace: "tsx"
fallback: ['ts']
fallback: ['js']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -18,4 +18,6 @@ module.exports = {
"ts"
]

defaultBeautifier: "TypeScript Formatter"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This was the previous default beautifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the build failed originally because it used prettier for the tests.

@kachkaev
Copy link
Contributor

kachkaev commented Feb 21, 2018

Despite being an author of #2004 and #2008, I'd vote for making prettier a default beautifier for markdown and ignoring a switch to remark-cli! I've been using Prettier from a command line recently and quite like its design! If I'm not wrong, Prettier uses remark to parse markdown, so we'll still keep remark under the hood 😉

If there is a plan to make prettier a default markdown beautifier, feel free to close my two PRs mentioned above!

I can also return to #1990 once Prettier lands atom-beautify. Although prettier already formats some code blocks recursively, it'd be cool to additionally beautify a user-defined set of langauges with atom-beautify. So until prettier supports language XYZ as a plugin, people can enjoy nested code block formatting in their markdowns 👍

@kachkaev
Copy link
Contributor

To be completely fair about prettier, there is one inconvenience in it that could make markdown beautification a bit frustrating for some users. It is to do with how numbered lists are indented, see prettier/prettier#3459 (comment)

There is a PR to solve this prettier/prettier#3990, but until it’s discussed and merged it’d be perhaps wise to keep Prettier non-default (at least in markdown).

Markdown support in prettier is still pretty new, but I like the pace at which they are improving stuff!

@stevenzeck
Copy link
Contributor Author

@kachkaev prettier won't be the default for anything yet. I want to get it out there for people to start using and make sure it doesn't have any serious problems. But certainly open to it once it's been out there for a while.

]
JavaScript:
bracketSpacing: "object_curly_spacing"
TypeScript: false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be true?

@Glavin001 Glavin001 merged commit 26d8cf0 into Glavin001:master Mar 1, 2018
@kachkaev kachkaev mentioned this pull request Mar 2, 2018
6 tasks
@stevenzeck stevenzeck deleted the add-prettier branch March 2, 2018 13:51
@kachkaev
Copy link
Contributor

kachkaev commented Mar 6, 2018

May be related: #2070

@harmtemolder
Copy link

I can't find any documentation on the bracket_spacing option implemented here. What are its possible values in .jsbeautifyrc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider using "prettier" for JS and JSX Disallow or enforce spaces inside of curly braces in objects?
4 participants