microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.93k stars 28.77k forks source link

Support JSON Schema for YAML files #1176

Closed mohsen1 closed 4 years ago

mohsen1 commented 8 years ago

It's getting more common to use YAML for configuration. For example docker-compose.yml or Swagger files are usually written in YAML. The JSON Schema based autocomplete for JSON files is a neat feature, please enable that for YAML files also.

egamma commented 8 years ago

@aeschli fyi

mohsen1 commented 8 years ago

I'm eager to implement this. Can you point me to where can I start?

aeschli commented 8 years ago

You basically have to implement an extension that adds a validation support for YAML. In there you can implement the validation logic yourself, as JSON does it, or delegate to another executable. If you implement the validation logic yourself in TypeScript/JavaScipt, you'd do that as a language server. A language server performs the validation in it's own process.

To see that in action, have a look at the JSON extension at (https://github.com/Microsoft/vscode/tree/master/extensions/json). To try it out:

djabraham commented 8 years ago

@mohsen1 Have you done any work in this regard yet?

mohsen1 commented 8 years ago

@djabraham no. Never found the time to do this

djabraham commented 8 years ago

I Friken Love YAML

..so I gave it a try.

https://github.com/djabraham/vscode-yaml-validation

https://marketplace.visualstudio.com/items?itemName=djabraham.vscode-yaml-validation

Uses the well known js-yaml parser with some TS types added, converts the returned structures to their JSON AST Equivalent, and then uses the standard JSON validation already built into VS Code. The excellent example cited above, is actually the source for that validation.

This still needs some work, because the completions aren't being returned. But basic validation seems to be working. I will put some more time into it, when I can.

screen shot 04-21-16 at 08 31 pm

screen shot 04-21-16 at 08 30 pm

aeschli commented 8 years ago

@djabraham Awesome! Let me know if I can help!

adamvoss commented 7 years ago

@djabraham Thanks for showing this is possible!!

I wanted to add more functionality, but thought more good could come long-term with less forking. The result was that I wrote vscode-yaml (marketplace) (GitHub).

From the user perspective, I believe it provides all the functionality of vscode-yaml-validation plus:

I kept the same design decision to use the json.schemas setting key, so it should function as a drop-in replacement for vscode-yaml-validator.

Sadly, no completion in this extension either. It was more-or-less working, but all the suggestions generated JSON. While the JSON was valid YAML, it was not what anyone would want, so I disabled it. My determination was that it would be best to write the suggestion code from scratch rather than trying to "patch" the code from vscode-json-languageservice. I spent more time on this than I anticipated and I'm not sure I am not sure I will ever get around to completion. (PRs welcome of course).

Out of convenience it forks vscode-json, which is just the piece that wires the service to the VS Code client. It takes a source-level dependency on vscode-json-languageservice to be access some internal types. Otherwise, it doesn't fork its dependencies. This caused submitting one upstream bug fix Microsoft/vscode-json-languageservice#7 and I have submitted the scalar classification code written for this to the upstream parser library possible future inclusion (mulesoft-labs/yaml-ast-parser#14).

djabraham commented 7 years ago

@adamvoss

I like the fact that validation is integrated with completion and formatting. That makes perfect sense. It's good to see major progress, cause I love the YAML format. Especially for schema files and Kubernetes configs.

One Q, do you plan to eventually add support for multiple yaml (concatenated) files? That seems to be a major pain point for advanced users. I started restructuring the repo in another branch, per the latest JSON validation project and the MS example for newer Language Server; but a new position currently demands focus and I kept putting it off.

https://github.com/djabraham/vscode-yaml-validation/issues/2

Thanks regardless, and nice work!

adamvoss commented 7 years ago

@djabraham Thanks! To avoid spamming this thread, I'll respond on your linked issue.

purefan commented 6 years ago

Good stuff! My use-case is however slightly different, Im looking for a serverless intellisense formatter, its basically yaml + keywords that start with !Ref, for example: Timeout: !Ref DefaultLambdaTimeout

Is there a quick way to add this to the current yaml formatter?

adamvoss commented 6 years ago

@purefan see adamvoss/vscode-yaml#16, specifically this comment.

Dominik-K commented 6 years ago

FYI: Another option would be the YAML Support by Red Hat extension. The underlying yaml-language-server integrates all schemas from the JSON Schema Store. Kubernetes schema is currently included from a Gist.

@adamvoss @djabraham You want to share forces with them, maybe?

djabraham commented 6 years ago

My extension was retired a while back. It looks like the vscode-yaml extension of Adam's was also recently retired. The RedHat extension was a possible factor: https://github.com/adamvoss/vscode-yaml/issues/23

@adamvoss You guys did great work in a much needed area. I hope experience gained from growing a team and any new skills you've acquired, serve you well into the future.

Many thanks to those who create new inroads for technology, and to those who broaden them. You've helped great many travelers, from getting mired down.

aeschli commented 4 years ago

No plans on our side to add this functionality given that there are extensions implementing it already. I'll continue to open up the vscode-json-languageservice so it can also be used for other parsers that the JSON parser. If there's anything missing please file issues!