15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VSCodeでimport文の自動ソート

Last updated at Posted at 2019-08-16

やりたいこと

import文をセーブ時に自動ソートしたいけどprettierではできない。
VSCodeのソースアクションでimportの整理ができるがいちいち手動でやるのはめんどくさいのでセーブしたときに自動で実行するようにしたい。

方法

settings.jsonに以下を追加。

  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  }

formatOnSaveとの実行順序とかちゃんと分かっていないけど、手元ではとりあえず動いてるからまあ良し!


追記 (2019/8/18)

formatOnSaveとの実行順序でやっぱり不具合が起きたので、organizeImportsをonSave実行するのはやめて別コマンドで手動実行することにした。

「キーボードショートカット」で "organizeImports" で検索すると出てくるので、これを好きなキーバインドに変更して使用。

multiCommandという拡張も試したが、organizeImportsが非同期実行されているせいか、 organizeImports -> format -> save の順に実行されず、うまく動かないケースが多発したので使用をやめた。

15
5
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
15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?