我正在为Dart开发VS Code扩展。 Dart的约定是缩进2个空格(呃,我也讨厌这个)所以我想在用户打开Dart文件而不是使用默认值时自动设置它。
在insertSpaces
类中有一个FormattingOptions
属性但是我不清楚如何设置它,也不是最好的方法(在语言级别设置它比在文档打开时保持设置更好),例如)。
VS Code现在支持每种语言缩进设置,您可以在package.json
中设置默认值:
"configurationDefaults": {
"[dart]": {
"editor.tabSize": 2,
"editor.insertSpaces": true
},
更新:请参阅下面的答案,适用于较新版本的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);