Gitattributes Encoding
日本語
Switches the file encoding in the editor according to working-tree-encoding in .gitattributes.
*.bat working-tree-encoding=SHIFT-JIS eol=crlf
data/*.csv working-tree-encoding=CP932
With the settings above, *.bat and data/*.csv are reopened as Shift JIS when you open them, and saved as Shift JIS. Use this instead of charset in .editorconfig, which cannot specify encodings such as Shift_JIS or EUC-JP.
How it works
- On startup, it runs
git ls-files (tracked files and untracked files that are not ignored) and git check-attr once per workspace folder, and keeps only the files that have working-tree-encoding in an in-memory table. Opening a file only looks up the table; git is not run.
- Git itself evaluates the attributes, so nested
.gitattributes files, .git/info/attributes, core.attributesFile, and macro attributes are all respected.
- When a file is opened with an encoding different from the table, it is reopened with that encoding (
workspace.openTextDocument(uri, { encoding })), the same way the EditorConfig extension does. The file may briefly appear in the default encoding before it is reopened.
- The table from the previous session is saved and used right after startup, before git finishes.
- Creating, changing, or deleting a
.gitattributes file triggers a full rescan. Newly created files are checked individually.
- Files with unsaved changes are not switched. If you change the encoding of an open file manually, it is left as is.
- It uses the same git as VS Code's built-in Git extension (the
git.path setting, or git on PATH).
- Logs are written to the "Gitattributes Encoding" output channel, in English or Japanese according to the VS Code display language.
Supported encodings
Names in working-tree-encoding are matched case-insensitively, ignoring - and _.
| working-tree-encoding |
VS Code |
SHIFT-JIS SJIS CP932 MS932 Windows-31J |
Shift JIS (shiftjis) |
EUC-JP |
EUC-JP (eucjp) |
UTF-8 / UTF-8-BOM |
utf8 / utf8bom |
UTF-16LE-BOM / UTF-16BE-BOM |
utf16le / utf16be |
ISO-8859-1, windows-1252, CP1251, GB18030, EUC-KR, etc. |
The same encoding, if VS Code supports it |
The following cannot be read and written in the same form by VS Code, so they are not switched (only logged):
UTF-16LE / UTF-16BE without BOM: VS Code always writes a BOM when saving UTF-16, so saving would change the file. UTF-16 (detected by BOM) is detected by VS Code automatically.
ISO-2022-JP, UTF-32, etc.: Not supported by VS Code.
Limitations
- Nothing happens if a workspace folder is not inside a git working tree, or git is not available.
- Files inside submodules are not checked (
git ls-files does not descend into submodules).
- Changes to
.git/info/attributes or core.attributesFile are not detected automatically. Reload the window to apply them.
- Because it runs git in the repository, it is disabled in untrusted workspaces.
Requirements
- VS Code 1.100 or later
- git
日本語
.gitattributes の working-tree-encoding に従って、ファイルを開くときの文字コードを切り替える拡張機能。
*.bat working-tree-encoding=SHIFT-JIS eol=crlf
data/*.csv working-tree-encoding=CP932
と書いておけば、*.bat や data/*.csv を開いたときに Shift_JIS で読み直し、保存も Shift_JIS で行う。.editorconfig の charset は Shift_JIS や EUC-JP を指定できないため、その代わりに使う。
動作
- 起動時に、ワークスペースフォルダーごとに
git ls-files(追跡中と、無視されていない未追跡のファイル)と git check-attr を 1 回ずつ実行し、working-tree-encoding が指定されたファイルだけをメモリ上の表に持つ。ファイルを開くときは表を引くだけで、git は実行しない。
- 属性は git が判断するので、サブディレクトリの
.gitattributes、.git/info/attributes、core.attributesFile、マクロ属性もそのまま反映される。
- 文字コードが表と違うファイルが開かれたら、EditorConfig 拡張と同じく、その文字コードで開き直す(
workspace.openTextDocument(uri, { encoding }))。開き直すまでの一瞬は、既定の文字コードで表示されることがある。
- 表は前回の結果を保存しておき、起動直後は git で調べ終わるのを待たずにそれを使う。
.gitattributes の作成・変更・削除を検知したら全体を調べ直す。新しくできたファイルは、そのファイルだけを調べる。
- 保存していない変更があるファイルは切り替えない。開いたあとに手動で文字コードを変えた場合も、そのままにする。
- 使う git は VS Code 標準の Git 拡張と同じ(
git.path の設定、なければ PATH 上の git)。
- 処理の記録は出力パネルの「Gitattributes Encoding」に、VS Code の表示言語に合わせて日本語か英語で出る。
対応する文字コード
working-tree-encoding の名前(大文字・小文字と - _ の違いは問わない)を VS Code の文字コードに対応させる。
| working-tree-encoding |
VS Code |
SHIFT-JIS SJIS CP932 MS932 Windows-31J |
Shift JIS(shiftjis) |
EUC-JP |
EUC-JP(eucjp) |
UTF-8 / UTF-8-BOM |
utf8 / utf8bom |
UTF-16LE-BOM / UTF-16BE-BOM |
utf16le / utf16be |
ISO-8859-1 windows-1252 CP1251 GB18030 EUC-KR など |
VS Code に同じものがあれば、それ |
次のものは VS Code で同じ形で読み書きできないため、切り替えない(出力パネルに記録だけ残す)。
- BOM なしの
UTF-16LE / UTF-16BE: VS Code は UTF-16 で保存するときに必ず BOM を付けるため、開けても保存で内容が変わってしまう。UTF-16(BOM で判別)は、VS Code が BOM を見て自動で判別する。
ISO-2022-JP、UTF-32 など: VS Code が対応していない。
制限
- ワークスペースフォルダーが git の作業ツリーの中にない場合や、git が使えない場合は何もしない。
- submodule の中のファイルは調べない(
git ls-files が submodule の中に入らないため)。
.git/info/attributes や core.attributesFile を変更したときは自動では調べ直さない。ウィンドウを再読み込みすると反映される。
- リポジトリで git を実行するため、信頼されていないワークスペースでは動作しない。
必要なもの
開発
npm install # typescript と @types/vscode、@types/node だけ
npm test # ビルドして、文字コード名の変換と git での調べ方のテストを実行(node:test)
npm run package # .vsix を作成(vsce は npx でその都度取得)
npm run icon # images/icon.svg を images/icon.png(256×256)に書き出す(sharp-cli は npx でその都度取得)
npm run publish:marketplace # VS Code Marketplace に公開(事前に vsce login ExpressionInc)
F5 で起動する開発ホストは、npm run dev が作り直す .sandbox/project(Shift_JIS と EUC-JP のファイルを含む git リポジトリ)を開く。