You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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:
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.
The text was updated successfully, but these errors were encountered: