LoginSignup
1
0

More than 5 years have passed since last update.

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

Posted at

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 

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0