|
1 | 1 | import * as vscode from 'vscode';
|
| 2 | +import * as fs from 'fs'; |
2 | 3 |
|
3 | 4 | export abstract class Convert {
|
4 | 5 |
|
5 |
| - private static mapSelection(doMapping: (text: string) => string) { |
| 6 | + private static mapSelection(doMapping: (data: string | Buffer) => string) { |
6 | 7 | const editor = vscode.window.activeTextEditor;
|
7 | 8 | if (!editor) {
|
8 | 9 | return; // No open text editor
|
9 | 10 | }
|
10 | 11 | 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 | + } |
25 | 29 | });
|
| 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 | + } |
26 | 47 | }
|
27 | 48 | }
|
28 | 49 |
|
29 | 50 | public static EbcdicSelectionToAscii(mappingArray: string[]) {
|
30 |
| - Convert.mapSelection((text) => { |
| 51 | + Convert.mapSelection((data) => { |
31 | 52 | 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 | + } |
35 | 63 | }
|
36 | 64 | return convertedText;
|
37 | 65 | });
|
38 | 66 | }
|
39 | 67 |
|
40 | 68 | public static AsciiSelectionToEbcdic(mappingArray: number[]) {
|
41 |
| - Convert.mapSelection((text) => { |
| 69 | + Convert.mapSelection((data) => { |
42 | 70 | 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 | + } |
47 | 84 | }
|
48 | 85 | }
|
49 | 86 | return convertedText;
|
50 | 87 | });
|
51 | 88 | }
|
| 89 | + public static fileExists(filename: string): boolean { |
| 90 | + try { |
| 91 | + return fs.lstatSync(filename).isFile(); |
| 92 | + } catch { |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
52 | 96 | }
|
0 commit comments