'Code not generating when changing active file
I am writing a vscode extension that generates a prettier config based on your chosen language. Javascript and JSON. The code runs fine on the first document but if you change to another file and try generating clicking the button again. The code fails to start. Is there a way to check if the current document has changed thanks.
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: auto\n};`,
);
}
);
}
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();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
