简体   繁体   English

获取当前文档 vscode 的文件扩展名

[英]Get file extension of current document vscode

I am writing a vscode extension that takes in the current file type and generates a HelloWorld starter code for it.我正在编写一个接受当前文件类型并为其生成 HelloWorld 起始代码的 vscode 扩展。 However I can not find out how to get the current file type in the vscode docs and none of the code found can work.但是我无法找到如何在 vscode 文档中获取当前文件类型,并且找到的代码都无法正常工作。 Help will be appreciated.帮助将不胜感激。 The current code takes in the filename and then the fileType before generating the relevant code.当前代码在生成相关代码之前先接收文件名,然后是文件类型。

        const documentFileType = null;
        const documentFileName = vscode.window.activeTextEditor?.document.fileName;

        if documentFileType === 'ts' {
            vscode.window.activeTextEditor.edit(editBuilder => {
                editBuilder.insert(new vscode.Position(0, 0), 'console.log("Hello World!");');
            });
        }

The full code if anyone wants to see it...如果有人想看完整的代码...

import * as vscode from 'vscode';
import "typescript";

export function activate(context: vscode.ExtensionContext) {
    console.log("code-boilerplate is active!");
    
    let disposable = vscode.commands.registerCommand('code-boilerplate.CodeBoilerPlate', () => {
    
        const documentFileType = null;
        const documentFileName = vscode.window.activeTextEditor?.document.fileName;

        if documentFileType === 'ts' {
            vscode.window.activeTextEditor.edit(editBuilder => {
                editBuilder.insert(new vscode.Position(0, 0), 'console.log("Hello World!");');
            });
        }
        else if documentFileType == "py" {
            vscode.window.activeTextEditor.edit(editBuilder => {
                editBuilder.insert(new vscode.Position(0, 0), 'print("Hello World!")');
            });
        }
        else if documentFileType === "js" {
            vscode.window.activeTextEditor?.edit(editBuilder => {
                editBuilder.insert(new vscode.Position(0,0), 'console.log("Hello World!")');
            })
        }
        
            vscode.window.showInformationMessage("Generating code boilerplate...");
    });

    context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() {
    console.log("code-boilerplate is deactivated!");
}

You can get the language of the current document using vscode.window.activeTextEditor?.document.languageId您可以使用vscode.window.activeTextEditor?.document.languageId获取当前文档的语言

See https://code.visualstudio.com/api/references/vscode-api#TextDocument请参阅https://code.visualstudio.com/api/references/vscode-api#TextDocument

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM