<commit_before>import {
    commands,
    ExtensionContext,
    StatusBarItem,
    StatusBarAlignment,
    window,
    workspace
} from 'vscode';

import * as fs from 'fs';
import * as path from 'path';

import {
    LinterConfig,
    LINTERS,
} from './linter-configs';

export function activate(context: ExtensionContext) {
    const autoLinter = new AutoLinter();
    let disposable = commands.registerCommand('extension.setLinter', () => autoLinter.autosetLinters());
    context.subscriptions.push(disposable);
}

export function deactivate() {
}

function updateConfigIfChanged(section: string, value: any) {
    const config = workspace.getConfiguration();

    if (config.get(section) !== value) {
        config.update(section, value, false);
    }
}

class AutoLinter {

    statusBarItem: StatusBarItem;

    get isEnabled(): boolean {
        return <boolean>workspace.getConfiguration().get('jsAutolint.enable');
    }

    get showStatus(): boolean {
        return <boolean>workspace.getConfiguration().get('jsAutolint.showStatus');
    }

    get defaultLinters(): LinterConfig[] {
        const settings = workspace.getConfiguration().get<string[]>('jsAutolint.defaultLinters');

        return settings.map((setting) => {
            return LINTERS.find((linter) => linter.enableConfig.startsWith(setting));
        });
    }

    constructor() {
        if (this.isEnabled) {
            this.autosetLinters();
        }

        workspace.onDidChangeConfiguration(this.autosetLinters, this);
    }

    autosetLinters() {
        const { rootPath } = workspace;

        if (!rootPath) {
            return;
        }

        let lintersInProject = [
            ...this.findLintersInWorkspace(),
            ...this.findLintersInPackageJSON()
        ];

        // Get rid of duplicate entries
        lintersInProject = [...new Set(lintersInProject)];

        this.setWorkspaceSettings(lintersInProject);

        if (this.showStatus) {
            this.setStatusbarInformation(lintersInProject);
        } else if (this.statusBarItem) {
            this.statusBarItem.hide();
        }
    }

    findLintersInWorkspace(): LinterConfig[] {<commit_msg>Prevent automatically setting of linters when extension is disabled and vscode config is changed<commit_after>import {
    commands,
    ExtensionContext,
    StatusBarItem,
    StatusBarAlignment,
    window,
    workspace
} from 'vscode';

import * as fs from 'fs';
import * as path from 'path';

import {
    LinterConfig,
    LINTERS,
} from './linter-configs';

export function activate(context: ExtensionContext) {
    const autoLinter = new AutoLinter();
    let disposable = commands.registerCommand('extension.setLinter', () => autoLinter.autosetLinters());
    context.subscriptions.push(disposable);
}

export function deactivate() {
}

function updateConfigIfChanged(section: string, value: any) {
    const config = workspace.getConfiguration();

    if (config.get(section) !== value) {
        config.update(section, value, false);
    }
}

class AutoLinter {

    statusBarItem: StatusBarItem;

    get isEnabled(): boolean {
        return <boolean>workspace.getConfiguration().get('jsAutolint.enable');
    }

    get showStatus(): boolean {
        return <boolean>workspace.getConfiguration().get('jsAutolint.showStatus');
    }

    get defaultLinters(): LinterConfig[] {
        const settings = workspace.getConfiguration().get<string[]>('jsAutolint.defaultLinters');

        return settings.map((setting) => {
            return LINTERS.find((linter) => linter.enableConfig.startsWith(setting));
        });
    }

    constructor() {
        if (this.isEnabled) {
            this.autosetLinters();
        }

        workspace.onDidChangeConfiguration(this.autosetLinters, this);
    }

    autosetLinters() {
        const { rootPath } = workspace;

        if (!rootPath || !this.isEnabled) {
            return;
        }

        let lintersInProject = [
            ...this.findLintersInWorkspace(),
            ...this.findLintersInPackageJSON()
        ];

        // Get rid of duplicate entries
        lintersInProject = [...new Set(lintersInProject)];

        this.setWorkspaceSettings(lintersInProject);

        if (this.showStatus) {
            this.setStatusbarInformation(lintersInProject);
        } else if (this.statusBarItem) {
            this.statusBarItem.hide();
        }
    }

    findLintersInWorkspace(): LinterConfig[] {
{'fraction_non_alphanumeric': 0.11938958707360861, 'fraction_numerical': 0.0, 'mean_word_length': 2.7739204064352245, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7102575', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1253, 'n_words': 242}