UTF-8 / GB2312 Auto Encoding
A deliberately small VS Code extension for mixed legacy projects that contain only UTF-8 and GB2312 source files.
Behavior
- Reads the original file bytes when a file becomes active.
- Strict UTF-8 validation passes -> UTF-8.
- Strict UTF-8 validation fails -> GB2312.
- Pure ASCII is ambiguous;
smartEncoding.asciiDefault controls the first guess.
- Stores the result in
<workspace>/.vscode/smart-encoding.json using relative file paths. It no longer grows VS Code workspaceStorage on the system drive.
- If VS Code opened the file with the wrong encoding and the file is clean, the extension closes the tab and reopens it using the detected/remembered encoding.
- The status bar shows
ENC: UTF-8 or ENC: GB2312.
- Click the status item to manually correct a file once. The extension remembers that choice.
- The extension never intentionally converts a file from GB2312 to UTF-8 or vice versa.
Important safety behavior
The extension refuses to switch/reopen a dirty file. This avoids writing text back to disk using a wrong encoding.
Run in development
- Install Node.js.
- Open this folder in VS Code 1.136+.
- Run
npm install.
- Press
F5 and choose Run Extension if prompted.
- In the Extension Development Host, open a folder containing UTF-8 and GB2312 files.
Commands
Smart Encoding: Switch UTF-8 / GB2312
Smart Encoding: Forget and Detect Again
Pure ASCII files
ASCII bytes are identical under UTF-8 and GB2312, so automatic detection is impossible. For an HMI-heavy workspace you can set:
{
"smartEncoding.asciiDefault": "gb2312"
}
For a normal modern workspace, leave the default as utf8.
Encoding record file
The first remembered file creates:
<your-project>/.vscode/smart-encoding.json
Example:
{
"version": 1,
"files": {
"HMI/main.c": "gb2312",
"src/app.c": "utf8"
}
}
Only relative paths are stored, so the record continues to work if the project folder is moved or copied to another drive/computer. You may commit this file to Git if the whole team should share the same encoding choices, or add it to .gitignore if the choices should remain local.
Version 0.0.2 automatically migrates the previous workspaceState record for the current workspace into this file, then removes the old extension key from workspaceState.
Files opened outside any VS Code folder/workspace are still detected, but are not persisted because there is no project .vscode directory to store them in.