VSCode: activeTextEditor encoding

空扰寡人 提交于 2020-03-02 04:22:05

问题


Is there any way to get current document encoding (that is in the bottom bar) in my extension code?

Something like vscode.window.activeTextEditor.encoding


回答1:


This does not appear to be possible.

Since it's nearly impossible to prove a negative, the rest of this answer documents what I explored.

The string "encoding" does not appear (in this sense) anywhere in the API docs nor in the index.d.ts file it is derived from. (With VSCode 1.37.1, current as of writing.)

I dug into the vscode sources to see if there might be a clever solution, but came up empty. The code that executes when the encoding is changed by the user is in editorStatus.ts, class ChangeEncodingAction. This makes its way to textFileEditorModel.ts, function updatePreferredEncoding, which sets preferredEncoding. That field controls what happens when the file is saved, and is used to populate the status indicator, but doesn't go anywhere else I can find.

Reading the status indicator itself does not appear possible since the API allows extensions to create new indicators with window.createStatusBarItem but not enumerate existing ones. And directly accessing the DOM is not possible.

I also came up empty searching through VSCode issues related to encoding, both open and closed, but only skimmed the most recent ~100 closed issue titles.

Alternatives

My main suggestion at this point would be to file an enhancement request on the VSCode github.

It should also be possible to do something with reflection but of course it would be fragile.

Finally, the encoding controls how the document in memory (a sequence of characters) maps to a file on disk (a sequence of bytes). Depending on what you're trying to do, it might work to speculatively encode the document in several encodings and compare each to what is on disk (so long as the file is not dirty).



来源:https://stackoverflow.com/questions/57524800/vscode-activetexteditor-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!