Skip to content

Use yaml package for formatting#983

Open
remcohaszing wants to merge 2 commits intoredhat-developer:mainfrom
remcohaszing:replace-prettier
Open

Use yaml package for formatting#983
remcohaszing wants to merge 2 commits intoredhat-developer:mainfrom
remcohaszing:replace-prettier

Conversation

@remcohaszing
Copy link
Contributor

What does this PR do?

This replaces Prettier as the LSP formatting solution with the yaml package. The yaml package can format YAML just fine, and we already have it as a dependency. Prettier is a big dependency.

If people want to use Prettier, other Prettier integrations are probably better suited for them. For example, the YAML language server doesn’t respect Prettier configuration files.

This is a proof of concept. I tried to map existing options to the new implementation. A more ideal solution might be to change the formatting options.

What issues does this PR fix or reference?

Refs #933

Is it tested? How?

npm test

I discovered eemeli/yaml#562 while working on this.

This replaces Prettier as the LSP formatting solution with the `yaml`
package. The `yaml` package can format YAML just fine, and we already
have it as a dependency. Prettier is a big dependency.

If people want to use Prettier, other Prettier integrations are probably
better suited for them. For example, the YAML language server doesn’t
respect Prettier configuration files.

This is a proof of concept. I tried to map existing options to the new
implementation. A more ideal solution might be to change the formatting
options.

Refs redhat-developer#933
@coveralls
Copy link

Coverage Status

coverage: 84.206% (+0.03%) from 84.174%
when pulling db44f9b on remcohaszing:replace-prettier
into f039273 on redhat-developer:main.

const formatted = prettier.format(text, prettierOptions);
const formatted = doc.toString(toStringOptions);

if (formatted === text) {
Copy link

Choose a reason for hiding this comment

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

Can we get this broken out as a separate PR? My LSP client gets confused when the original text gets sent back in response to a formatting request.

@datho7561
Copy link
Contributor

I took another look at this; this seems like a feasible alternative to prettier. The main drawback of this approach is that we still don't have direct control of the formatting logic. There's quite a few bugs about weird formatting behaviour and feature requests for new formatting settings. I guess we could always submit PRs upstream to eemeli/yaml, and I'm guessing the maintainer of the parser is more receptive to adding new formatter options than prettier.

There's one test failure after the rebase: "Formatting handles trailing commas (enabled)". Prettier added this option so I added it to the extension a while back, but eemeli/yaml doesn't support this yet. In order to preserve this functionality, we'd need to file a bug upstream, and probably submit the patch to implement it.

if (shouldFormat) {
this.formatterEnabled = shouldFormat.format;
this.yamlVersion = shouldFormat.yamlVersion;
this.customTags = shouldFormat.customTags;
Copy link
Contributor

Choose a reason for hiding this comment

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

these customTags aren't used anywhere. There's a helper method getCustomTags that converts them from the string format to the format that parseDocument is expecting.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants