How to make my own encoding for a file in VSCode Editor

Viewed 57

Is it possible to have an own encoding in VSCode editor, inheritd from an exising?

class myEcoding implements utf-8 
{
  // changes for some codes
}

I have some files, which contains german characters like "ä ö ü" that are encoded as unicode numbers in this file.

So for example, the file conatins the following line

Pr\u00FCfsignal

While I want to edit this file with the correct german characters, it should exist on the harddisk in the form above.

This is how I want to see it in the editor

Prüfsignal

I already have a function, that can transform a string in both directions:

function translate(content: string, direction: boolean): string {
    if (direction) {

        content = content
            .replace(/\\u00E4/g, "ä")
            .replace(/\\u00F6/g, "ö")
            .replace(/\\u00FC/g, "ü")
            .replace(/\\u00C4/g, "Ä")
            .replace(/\\u00D6/g, "Ö")
            .replace(/\\u00DC/g, "Ü")
            .replace(/\\u00DF/g, "ß")
            .replace(/\\u00B0/g, "°")
            .replace(/\\u00B1/g, "±")
            .replace(/\\u00B5/g, "µ");
    }
    else {
        content = content
            .replace(/ä/g, "\\u00E4")
            .replace(/ö/g, "\\u00F6")
            .replace(/ü/g, "\\u00FC")
            .replace(/Ä/g, "\\u00C4")
            .replace(/Ö/g, "\\u00D6")
            .replace(/Ü/g, "\\u00DC")
            .replace(/ß/g, "\\u00DF")
            .replace(/°/g, "\\u00B0")
            .replace(/±/g, "\\u00B1")
            .replace(/µ/g, "\\u00B5");
    }
    return content;
}

Can this be solved with a custom encoding, and if yes, any hints? Is there possibly a better solution?

1 Answers

There has been an open feature request for some years to Provide encoding-related APIs for editor extensions: https://github.com/microsoft/vscode/issues/824

For now you could just wrap that function in a loop that encodes all files in the working directory.

Related