如何为VSCode扩展中的语言设置缩进选项?

问题描述 投票:1回答:2

我正在为Dart开发VS Code扩展。 Dart的约定是缩进2个空格(呃,我也讨厌这个)所以我想在用户打开Dart文件而不是使用默认值时自动设置它。

insertSpaces类中有一个FormattingOptions属性但是我不清楚如何设置它,也不是最好的方法(在语言级别设置它比在文档打开时保持设置更好),例如)。

dart visual-studio-code vscode-extensions
2个回答
2
投票

VS Code现在支持每种语言缩进设置,您可以在package.json中设置默认值:

"configurationDefaults": {
    "[dart]": {
        "editor.tabSize": 2,
        "editor.insertSpaces": true
    },

2
投票

更新:请参阅下面的答案,适用于较新版本的VS Code。

为了使用FormattingOptions,在扩展程序的vscode.window.onDidChangeActiveTextEditor()函数中为activate()设置回调:

let disposable = vscode.window.onDidChangeActiveTextEditor((editor) => {
    if(!editor) {
        return;
    }

    if (editor.document.languageId != "your_id_here") {
        return;
    }

    editor.options = {
        cursorStyle: editor.options.cursorStyle,
        insertSpaces: true,
        tabSize: 2
    };
});

context.subscriptions.push(disposable);
© www.soinside.com 2019 - 2024. All rights reserved.