Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensions to get TextDocument character encoding #11690

Closed
vysker opened this issue Sep 8, 2016 · 2 comments
Closed

Allow extensions to get TextDocument character encoding #11690

vysker opened this issue Sep 8, 2016 · 2 comments
Labels
api *duplicate Issue identified as a duplicate of another issue(s)

Comments

@vysker
Copy link

vysker commented Sep 8, 2016

Currently I am writing an extension that reads (part of) the active document and writes the file's content to an external file. The content of the external file will then be altered, after which the content will be put back into the original file (active document).

The issue here is that there is no way to be absolutely sure which character encoding the external file uses. So, when trying to read from the external file, the encoding has to be guessed. This is not very reliable, of course. The same goes for writing to that file.

So, in code this looks like:

var fs = require('fs');

// [...]

var activeDocumentContent = vscode.window.activeTextEditor.document.getText();
fs.writeFileSync('myfile.txt', activeDocumentContent, {encoding: 'utf8'});

// [...] do stuff to the external file

fs.createReadStream('myfile.txt', {encoding: 'utf8'});

It would be great if there was a way to get the document's encoding, like so, for example:

vscode.window.activeTextEditor.document.getEncoding();

The workaround at the moment is to use a package that detects character encodings.

vysker added a commit to vysker/vscode-php-formatter that referenced this issue Sep 8, 2016
This resolves #3, where fixing a file would erase the contents. It was the result of the fs.readFileSync method which handled the file descriptor cursor position poorly. Using a read stream, we can set the file cursor position to the first line when reading from the temp file.

Be aware, however, that this code assumes utf-8 character encoding throughout, which could cause a lot of issues. Hence we are waiting for a feature to be implemented with which extensions can get the a file's character encoding. Feature request here: microsoft/vscode#11690.

Also added some more debug logging, as well as a convenience method that wraps a string in double quotes.
@ramya-rao-a
Copy link
Contributor

Duplicate of #824
@Dickurt upvote #824 to give it more support

@ramya-rao-a ramya-rao-a added *duplicate Issue identified as a duplicate of another issue(s) api labels Sep 8, 2016
@vysker
Copy link
Author

vysker commented Sep 8, 2016

Couldn't find that one while searching. Will continue there.
Thanks for the link!

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api *duplicate Issue identified as a duplicate of another issue(s)
Projects
None yet
Development

No branches or pull requests

2 participants