如何根据文件的扩展来获取文件的语言?

发布于 2025-02-09 12:07:51 字数 110 浏览 1 评论 0 原文

我在这个问题上遇到问题,如何使用VSCODE API根据文件的扩展来找到该文件的语言?我尝试了 vscode.languages.getlanguages ,但我认为这不是正确的。

I am having problem on this one, How to find the language of the file according to its extension using the vscode API? i tried vscode.languages.getLanguages but I don't think that's the right one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2025-02-16 12:07:51

您的意思是获取目标打开的文件的语言吗?因为您可以使用 window.activetexteditor?.document.languageId 。docs 在此

vscode.languages.getlanguages 是获得VSCODE注册语言的API,换句话说,获取VSCODE可以识别的语言列表。如果您想知道打开的文件的语言,则应使用 window.activetexteditor?.document.languageId

另外,您可以使用Event vscode.window.ondidchangeactiveTextEditor ,以使您始终获得打开的文件的语言。

例如,在我的扩展程序中,我像这样编码:

//When the texteditor change,it will be called
private initialListener(){
  vscode.window.onDidChangeActiveTextEditor(this.theScheduler["changeLanguage"])
}             

thescheduler 是一个对象:

{
 changeLanguage:function(){
    let text = window.activeTextEditor?.document.languageId; //Get the targeted file's language
//do something
}

我的扩展名源代码为在这里,您可以将其视为参考。很抱歉,该代码的注释是中文友好的,希望它能为您提供帮助。

Do you mean get the targeted opened-file's language?For that you can use window.activeTextEditor?.document.languageId .The docs are here.

vscode.languages.getLanguages is the API that get the vscode's registered language,in other words,get the language list that vscode can recognize.If you want to know the opened file's language,you should use window.activeTextEditor?.document.languageId.

Also,you can use the event vscode.window.onDidChangeActiveTextEditor with it to keep you always get the opened-file's language.

For instance,in my extension,I code like this:

//When the texteditor change,it will be called
private initialListener(){
  vscode.window.onDidChangeActiveTextEditor(this.theScheduler["changeLanguage"])
}             

theScheduler is a object like this:

{
 changeLanguage:function(){
    let text = window.activeTextEditor?.document.languageId; //Get the targeted file's language
//do something
}

My extension's source code are here,you can view it as a reference. I'm sorry that the code's annotation is chinese friendly,do hope it can help you some.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文