Home > Back-end >  Code not generating when changing active file
Code not generating when changing active file

Time:02-11

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();
}

CodePudding user response:

if you need the active editor just get it with:

const editor = vscode.window.activeTextEditor;

or use

vscode.commands.registerTextEditorCommand

it has the active editor as an argument.

To get the changed state of a document use:

editor.document.isDirty
  • Related