更改活动文件时未生成代码



我正在编写一个vscode扩展,它可以根据您选择的语言生成更漂亮的配置。Javascript和JSON。代码在第一个文档上运行良好,但如果您更改到另一个文件并尝试生成,请再次单击按钮。代码无法启动。有没有办法检查当前文档是否已更改,谢谢。

import * as vscode from "vscode";
import { TextDocument } from "vscode";
import {
activeEditor
} from "./constants";
var statusBar: vscode.StatusBarItem;
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand("prettier-config.prettierConfig", async () => {
var changedDocument = false;
const fileType = await vscode.window.showInformationMessage(
"Which file to generate the Prettier Config for?",
"Javascript",
"JSON"
);
if (fileType === "Javascript" && changedDocument === false) {
const javascript = activeEditor.edit(
(edit: { insert: (arg0: any, arg1: string) => void }) => {
edit.insert(
new vscode.Position(0, 0),
`module.exports = {n   singleQuote: true,n   printWidth: 120,n   tabWidth: 4,n   trailingComma: all,n   endOfLine: auton};`,
);
}
);
}
else if (fileType === "JSON" && changedDocument === false) {
const json = activeEditor.edit(
(edit: { insert: (arg0: any, arg1: string) => void }) => {
edit.insert(
new vscode.Position(0, 0),
`{n   "singleQuote": true,n   "printWidth": 120,n   "tabWidth": 4,n   "trailingComma": "all",n   "endOfLine": "auto"n}`,
);
}
);
}
})
);
statusBar = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
100,
);
statusBar.command = "prettier-config.prettierConfig";
context.subscriptions.push(statusBar);
context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(updateStatusBar),
vscode.window.onDidChangeTextEditorSelection(updateStatusBar),
);
updateStatusBar();
}
function updateStatusBar(): void {
statusBar.text = `$(edit) Prettier Config`;
statusBar.show();
}
function deactivate() {
statusBar.dispose();
}

如果您需要活动编辑器,只需使用即可

const editor = vscode.window.activeTextEditor;

或使用

vscode.commands.registerTextEditorCommand

它将活动编辑器作为一个参数。

要获取文档的更改状态,请使用:

editor.document.isDirty

相关内容

  • 没有找到相关文章

最新更新