Skip to content

Commit d59099d

Browse files
committed
handle non utf8 encodings
1 parent a27ee0b commit d59099d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Open the Command Palette and search for:
8484
- `CSV: Change CSV Separator` (`csv.changeSeparator`)
8585
- `CSV: Change Font Family` (`csv.changeFontFamily`)
8686
- `CSV: Hide First N Rows` (`csv.changeIgnoreRows`)
87+
- `CSV: Change File Encoding` (`csv.changeEncoding`)
8788

8889

8990
## Settings
@@ -93,6 +94,7 @@ Global (Settings UI or `settings.json`):
9394
- `csv.enabled` (boolean, default `true`): Enable/disable the custom editor.
9495
- `csv.fontFamily` (string, default empty): Override font family; falls back to `editor.fontFamily`.
9596
- `csv.cellPadding` (number, default `4`): Vertical cell padding in pixels.
97+
- Per-file encoding: use `CSV: Change File Encoding` to set a file’s encoding (e.g., `utf8`, `utf16le`, `windows1250`, `gbk`). The extension will reopen the file using the chosen encoding.
9698

9799
Per-file (stored by the extension; set via commands):
98100

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"onCommand:csv.toggleSerialIndex",
2323
"onCommand:csv.changeSeparator",
2424
"onCommand:csv.changeFontFamily",
25-
"onCommand:csv.changeIgnoreRows"
25+
"onCommand:csv.changeIgnoreRows",
26+
"onCommand:csv.changeEncoding"
2627
],
2728
"main": "./out/extension.js",
2829
"contributes": {
@@ -46,7 +47,8 @@
4647
{ "command": "csv.toggleSerialIndex", "title": "CSV: Toggle Serial Index Column" },
4748
{ "command": "csv.changeSeparator", "title": "CSV: Change CSV Separator" },
4849
{ "command": "csv.changeFontFamily", "title": "CSV: Change Font Family" },
49-
{ "command": "csv.changeIgnoreRows", "title": "CSV: Hide First N Rows" }
50+
{ "command": "csv.changeIgnoreRows", "title": "CSV: Hide First N Rows" },
51+
{ "command": "csv.changeEncoding", "title": "CSV: Change File Encoding" }
5052
],
5153
"configuration": {
5254
"type": "object",

src/commands.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,60 @@ export function registerCsvCommands(context: vscode.ExtensionContext) {
9898
.forEach(ed => ed.refresh());
9999
vscode.window.showInformationMessage(`CSV: Hiding first ${n} row(s) for this file.`);
100100
})
101+
,
102+
vscode.commands.registerCommand('csv.changeEncoding', async () => {
103+
const active = CsvEditorProvider.getActiveProvider();
104+
if (!active) { vscode.window.showInformationMessage('Open a CSV/TSV file in the CSV editor.'); return; }
105+
const uri = active.getDocumentUri();
106+
107+
// Close any existing tabs for this URI so we can reuse the slot cleanly
108+
try {
109+
const toClose: vscode.Tab[] = [];
110+
vscode.window.tabGroups.all.forEach(g => {
111+
g.tabs.forEach(t => {
112+
const inp: any = (t as any).input;
113+
const u: vscode.Uri | undefined = inp?.uri instanceof vscode.Uri ? (inp.uri as vscode.Uri) : undefined;
114+
if (u && u.toString() === uri.toString()) toClose.push(t);
115+
});
116+
});
117+
if (toClose.length) {
118+
console.log(`[CSV(encoding)]: closing ${toClose.length} tab(s) for ${uri.fsPath}`);
119+
await vscode.window.tabGroups.close(toClose);
120+
}
121+
} catch (e) {
122+
console.warn('[CSV(encoding)]: failed to close existing tabs', e);
123+
}
124+
125+
// Open the default text editor in-place and invoke the built-in encoding picker
126+
try {
127+
console.log(`[CSV(encoding)]: opening default text editor for ${uri.fsPath}`);
128+
await vscode.commands.executeCommand('vscode.openWith', uri, 'default', { preview: true, preserveFocus: false });
129+
130+
console.log('[CSV(encoding)]: invoking workbench.action.editor.changeEncoding');
131+
await vscode.commands.executeCommand('workbench.action.editor.changeEncoding');
132+
133+
// Switch back to our custom editor
134+
console.log(`[CSV(encoding)]: reopening with custom editor for ${uri.fsPath}`);
135+
await vscode.commands.executeCommand('vscode.openWith', uri, CsvEditorProvider.viewType, { preview: false, preserveFocus: false });
136+
137+
// Best-effort: ensure no duplicate default tab remains
138+
try {
139+
const stale: vscode.Tab[] = [];
140+
vscode.window.tabGroups.all.forEach(g => g.tabs.forEach(t => {
141+
const inp: any = (t as any).input;
142+
const vt = inp?.viewType;
143+
const u: vscode.Uri | undefined = inp?.uri instanceof vscode.Uri ? (inp.uri as vscode.Uri) : undefined;
144+
if (u && u.toString() === uri.toString() && vt !== CsvEditorProvider.viewType) stale.push(t);
145+
}));
146+
if (stale.length) {
147+
console.log(`[CSV(encoding)]: closing ${stale.length} stale text tab(s)`);
148+
await vscode.window.tabGroups.close(stale);
149+
}
150+
} catch {}
151+
} catch (e) {
152+
console.error('CSV: encoding change flow failed', e);
153+
vscode.window.showWarningMessage('CSV: Could not invoke the built-in encoding picker. Please use File → Reopen with Encoding, then re-open the CSV view.');
154+
}
155+
})
101156
);
102157
}

0 commit comments

Comments
 (0)