Skip to content

Commit 2e76301

Browse files
committed
Bugfix: Fixed issue when converting entire file.
1 parent 909b871 commit 2e76301

File tree

1 file changed

+68
-24
lines changed

1 file changed

+68
-24
lines changed

src/Convert.ts

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,96 @@
11
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
23

34
export abstract class Convert {
45

5-
private static mapSelection(doMapping: (text: string) => string) {
6+
private static mapSelection(doMapping: (data: string | Buffer) => string) {
67
const editor = vscode.window.activeTextEditor;
78
if (!editor) {
89
return; // No open text editor
910
}
1011
const selection = editor.selection;
11-
if (selection === undefined || selection.isEmpty) {
12-
const firstLine = editor.document.lineAt(0);
13-
const lastLine = editor.document.lineAt(editor.document.lineCount - 1);
14-
const textRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
15-
const text = editor.document.getText(textRange);
16-
const convertedText = doMapping(text);
17-
editor.edit(editBuilder => {
18-
editBuilder.replace(textRange, convertedText);
19-
});
20-
} else {
21-
const text = editor.document.getText(selection);
22-
const convertedText = doMapping(text);
23-
editor.edit(editBuilder => {
24-
editBuilder.replace(selection, convertedText);
12+
if (this.fileExists(editor.document.fileName)) { // If a specific file is opened, then we read it as binary to avoid issues with VSCode converting the characters to unicode
13+
fs.readFile(editor.document.fileName, null, (err, data) => {
14+
if (selection === undefined || selection.isEmpty) {
15+
const convertedText = doMapping(data);
16+
editor.edit(editBuilder => {
17+
const firstLine = editor.document.lineAt(0);
18+
const lastLine = editor.document.lineAt(editor.document.lineCount - 1);
19+
const textRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
20+
editBuilder.replace(textRange, convertedText);
21+
});
22+
} else {
23+
const selectionData = data.slice(selection.start.character, selection.end.character);
24+
const convertedText = doMapping(selectionData);
25+
editor.edit(editBuilder => {
26+
editBuilder.replace(selection, convertedText);
27+
});
28+
}
2529
});
30+
} else { // If no file is opened we fetch the text from the editor
31+
if (selection === undefined || selection.isEmpty) {
32+
const firstLine = editor.document.lineAt(0);
33+
const lastLine = editor.document.lineAt(editor.document.lineCount - 1);
34+
const textRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
35+
const text = editor.document.getText(textRange);
36+
const convertedText = doMapping(text);
37+
editor.edit(editBuilder => {
38+
editBuilder.replace(textRange, convertedText);
39+
});
40+
} else {
41+
const text = editor.document.getText(selection);
42+
const convertedText = doMapping(text);
43+
editor.edit(editBuilder => {
44+
editBuilder.replace(selection, convertedText);
45+
});
46+
}
2647
}
2748
}
2849

2950
public static EbcdicSelectionToAscii(mappingArray: string[]) {
30-
Convert.mapSelection((text) => {
51+
Convert.mapSelection((data) => {
3152
let convertedText: string = '';
32-
for (let i = 0; i < text.length; i++) {
33-
const charCode: number = text.charCodeAt(i);
34-
convertedText += mappingArray[charCode];
53+
if (typeof (data) === 'string') {
54+
for (let i = 0; i < data.length; i++) {
55+
const charCode: number = data.charCodeAt(i);
56+
convertedText += mappingArray[charCode];
57+
}
58+
} else {
59+
for (let i = 0; i < data.length; i++) {
60+
const charCode: number = data[i];
61+
convertedText += mappingArray[charCode];
62+
}
3563
}
3664
return convertedText;
3765
});
3866
}
3967

4068
public static AsciiSelectionToEbcdic(mappingArray: number[]) {
41-
Convert.mapSelection((text) => {
69+
Convert.mapSelection((data) => {
4270
let convertedText: string = '';
43-
for (let i = 0; i < text.length; i++) {
44-
const charCode: number = text.charCodeAt(i);
45-
if (charCode !== 0xd) { // The conversion will mess up if we convert both \r (0xd) and \n (0xa), that is why we dont convert \r
46-
convertedText += String.fromCharCode(mappingArray[charCode]);
71+
if (typeof (data) === 'string') {
72+
for (let i = 0; i < data.length; i++) {
73+
const charCode: number = data.charCodeAt(i);
74+
if (charCode !== 0xd) { // The conversion will mess up if we convert both \r (0xd) and \n (0xa), that is why we dont convert \r
75+
convertedText += String.fromCharCode(mappingArray[charCode]);
76+
}
77+
}
78+
} else {
79+
for (let i = 0; i < data.length; i++) {
80+
const charCode: number = data[i];
81+
if (charCode !== 0xd) { // The conversion will mess up if we convert both \r (0xd) and \n (0xa), that is why we dont convert \r
82+
convertedText += String.fromCharCode(mappingArray[charCode]);
83+
}
4784
}
4885
}
4986
return convertedText;
5087
});
5188
}
89+
public static fileExists(filename: string): boolean {
90+
try {
91+
return fs.lstatSync(filename).isFile();
92+
} catch {
93+
return false;
94+
}
95+
}
5296
}

0 commit comments

Comments
 (0)