0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

VSCode で YAMLのキー重複を調べる

VSCodeのプラグインでYAMLのキー重複を調べる

VSCodeのYAMLプラグインのフォーマット機能を使うと、キーが重複しているとエラーで知らせてくれる。

VSCodeのプラグイン

名前: YAML
Id: redhat.vscode-yaml
説明: YAML Language Support by Red Hat, with built-in Kubernetes and Kedge syntax support
バージョン: 0.2.0
発行者: Red Hat
VS Marketplace のリンク: https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml

image.png

設定

インストールすると、SettingsにYAMLが追加されている。

Settingsを開いて、"yaml"で検索したところ。
image.png

そんで、この FormatEnable にする。

Yaml › Format: Enable
Enable/disable default YAML formatter (requires restart)

VSCodeを再起動すると設定が反映される。

重複の調べ方

  1. YAMLファイルをVSCodeで開く
  2. エディタにフォーカスがある状態で、option + Shift + F (mac)
  3. コンソールに重複エラーが出力される。

sample text
grapesが重複している。

en-US:
  # Note that our comments are important:
  # Don't let your yaml sorter delete them!
  bananas: |
    Bananas are "nice":
      - They are <b> sweet </b>.
      this: not a key  
    That is why everyone like bananas!
  grapes: We eat them.
  apples: >
    Apples are fine,
    just don't eat your 
    iPods!
  grapes: We dont' eat them.

実行結果

message: Map keys must be unique; "grapes" is repeated (4:3)

[Error - 20:06:14] Request textDocument/formatting failed.
  Message: Request textDocument/formatting failed with message: Map keys must be unique; "grapes" is repeated (4:3)
   2 |   # Note that our comments are important:
   3 |   # Don't let your yaml sorter delete them!
>  4 |   bananas: |
     |   ^^^^^^^^^
>  5 |     Bananas are "nice":
     | ^^^^^^^^^^^^^^^^^^^^^^^
>  6 |       - They are <b> sweet </b>.
     | ^^^^^^^^^^^^^^^^^^^^^^^
>  7 |       this: not a key  
     | ^^^^^^^^^^^^^^^^^^^^^^^
>  8 |     That is why everyone like bananas!
     | ^^^^^^^^^^^^^^^^^^^^^^^
>  9 |   grapes: We eat them.
     | ^^^^^^^^^^^^^^^^^^^^^^^
> 10 |   apples: >
     | ^^^^^^^^^^^^^^^^^^^^^^^
> 11 |     Apples are fine,
     | ^^^^^^^^^^^^^^^^^^^^^^^
> 12 |     just don't eat your 
     | ^^^^^^^^^^^^^^^^^^^^^^^
> 13 |     iPods!
     | ^^^^^^^^^^^^^^^^^^^^^^^
> 14 |   grapes: We dont' eat them.
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Code: -32603 

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?