VSCode:activeTextEditor 编码 [英] VSCode: activeTextEditor encoding

查看:31
本文介绍了VSCode:activeTextEditor 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在我的扩展代码中获取当前文档编码(在底部栏中)?

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

类似vscode.window.activeTextEditor.encoding

推荐答案

这似乎不可能.

因为几乎不可能证明否定,所以本答案的其余部分记录了我的探索.

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.

API 中的任何地方都没有出现字符串encoding"(在这个意义上)docs 也不在 index.d.ts 文件是从它派生的.(使用 VSCode 1.37.1,截至撰写本文时.)

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.)

我研究了 vscode 来源 以查看是否有聪明的解决方案,但我提出了空的.用户更改编码时执行的代码在 editorStatus.ts,类 ChangeEncodingAction.这使得 textFileEditorModel.ts,函数updatePreferredEncoding,设置preferredEncoding.该字段控制保存文件时发生的情况,并用于填充状态指示器,但不会出现在我能找到的任何其他地方.

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.

读取状态指示器本身似乎是不可能的,因为 API 允许扩展使用 window.createStatusBarItem 但不枚举现有的.而直接访问DOM是不可能的.

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.

我还通过与编码相关的 VSCode 问题进行了空搜索,包括开放式和封闭式,但只浏览了最近的 ~100 个已关闭的问题.

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.

此时我的主要建议是在 VSCode github 上提交增强请求.

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).

这篇关于VSCode:activeTextEditor 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆