<commit_before>import * as vscode from 'vscode';
import * as cp from 'child_process';
import { RubocopOutput, RubocopFile, RubocopOffense } from './rubocopOutput';
import * as path from 'path';

interface RubocopConfig {
	executePath: string;
	options: string[];
}

'use strict';
export class Rubocop {
	private config: vscode.WorkspaceConfiguration;
	private diag: vscode.DiagnosticCollection;
	private path: string;
	private command: string;
	private onSave: boolean;

	constructor(diagnostics: vscode.DiagnosticCollection) {
		this.config = vscode.workspace.getConfiguration('ruby.rubocop');
		this.diag = diagnostics;
		this.path = this.config.get('executePath', '');
		this.command = (process.platform === 'win32') ? 'rubocop.bat' : 'rubocop';
		this.onSave = this.config.get('onSave', true);
	}

	public execute(document: vscode.TextDocument): void {
		if (document.languageId !== 'ruby') {
			return;
		}

		if (!this.path || 0 === this.path.length) {
			vscode.window.showWarningMessage('execute path is empty! please check ruby.rubocop.executePath config');
			return;
		}

		const fileName = document.fileName;
		let currentPath = vscode.workspace.rootPath;
		if (!currentPath) {
			currentPath = path.dirname(fileName);
		}

		const executeFile = this.path + this.command;

		let onDidExec = (error: Error, stdout: Buffer, stderr: Buffer) => {
			if (error && (<any>error).code === 'ENOENT') {
				vscode.window.showWarningMessage(`${executeFile} is not executable`);
				return;
			} else if (error && (<any>error).code !== 0) {
                let errorMessage = stderr.toString();
				vscode.window.showWarningMessage(errorMessage);
			    console.log(error.message);
				return;
            }

			this.diag.clear();
            let out = stdout.toString();
			const rubocop: RubocopOutput = JSON.parse(out || 'null');
            if (rubocop === null) {
                return;
            }
            
			let entries: [vscode.Uri, vscode.Diagnostic[]][] = [];
			rubocop.files.forEach((file: RubocopFile) => {
				let diagnostics = [];
				const url = vscode.Uri.file(fileName);
				file.offenses.forEach((offence: RubocopOffense) => {
					const loc = offence.location;
					const range = new vscode.Range(
						loc.line - 1, loc.column - 1, loc.line - 1, loc.length + loc.column - 1);
					const sev = this.severity(offence.severity);
					const message = `${offence.message} (${offence.severity}:${offence.cop_name})`;
					const diagnostic = new vscode.Diagnostic(
						range, message, sev);
					diagnostics.push(diagnostic);
				});
				entries.push([url, diagnostics]);
			});

			this.diag.set(entries);
		};

		cp.execFile(executeFile, [fileName, '--format', 'json'], {cwd: currentPath}, onDidExec);
	}

	public get isOnSave(): boolean {
		return this.onSave;
	}

	private severity(sev: string): vscode.DiagnosticSeverity {
		switch (sev) {
			case 'refactor': return vscode.DiagnosticSeverity.Hint;
			case 'convention': return vscode.DiagnosticSeverity.Information;
			case 'warning': return vscode.DiagnosticSeverity.Warning;
			case 'error': return vscode.DiagnosticSeverity.Error;
			case 'fatal': return vscode.DiagnosticSeverity.Error;
			default: return vscode.DiagnosticSeverity.Error;
		}
	}
}
<commit_msg>a syntax error returns 1. command error returns 127<commit_after>import * as vscode from 'vscode';
import * as cp from 'child_process';
import { RubocopOutput, RubocopFile, RubocopOffense } from './rubocopOutput';
import * as path from 'path';

interface RubocopConfig {
	executePath: string;
	options: string[];
}

'use strict';
export class Rubocop {
	private config: vscode.WorkspaceConfiguration;
	private diag: vscode.DiagnosticCollection;
	private path: string;
	private command: string;
	private onSave: boolean;

	constructor(diagnostics: vscode.DiagnosticCollection) {
		this.config = vscode.workspace.getConfiguration('ruby.rubocop');
		this.diag = diagnostics;
		this.path = this.config.get('executePath', '');
		this.command = (process.platform === 'win32') ? 'rubocop.bat' : 'rubocop';
		this.onSave = this.config.get('onSave', true);
	}

	public execute(document: vscode.TextDocument): void {
		if (document.languageId !== 'ruby') {
			return;
		}

		if (!this.path || 0 === this.path.length) {
			vscode.window.showWarningMessage('execute path is empty! please check ruby.rubocop.executePath config');
			return;
		}

		const fileName = document.fileName;
		let currentPath = vscode.workspace.rootPath;
		if (!currentPath) {
			currentPath = path.dirname(fileName);
		}

		const executeFile = this.path + this.command;

		let onDidExec = (error: Error, stdout: Buffer, stderr: Buffer) => {
			if (error && (<any>error).code === 'ENOENT') {
				vscode.window.showWarningMessage(`${executeFile} is not executable`);
				return;
			} else if (error && (<any>error).code === 127) {
                let errorMessage = stderr.toString();
				vscode.window.showWarningMessage(errorMessage);
			    console.log(error.message);
				return;
            }

			this.diag.clear();
            let out = stdout.toString();
			const rubocop: RubocopOutput = JSON.parse(out || 'null');
            if (rubocop === null) {
                return;
            }
            
			let entries: [vscode.Uri, vscode.Diagnostic[]][] = [];
			rubocop.files.forEach((file: RubocopFile) => {
				let diagnostics = [];
				const url = vscode.Uri.file(fileName);
				file.offenses.forEach((offence: RubocopOffense) => {
					const loc = offence.location;
					const range = new vscode.Range(
						loc.line - 1, loc.column - 1, loc.line - 1, loc.length + loc.column - 1);
					const sev = this.severity(offence.severity);
					const message = `${offence.message} (${offence.severity}:${offence.cop_name})`;
					const diagnostic = new vscode.Diagnostic(
						range, message, sev);
					diagnostics.push(diagnostic);
				});
				entries.push([url, diagnostics]);
			});

			this.diag.set(entries);
		};

		cp.execFile(executeFile, [fileName, '--format', 'json'], {cwd: currentPath}, onDidExec);
	}

	public get isOnSave(): boolean {
		return this.onSave;
	}

	private severity(sev: string): vscode.DiagnosticSeverity {
		switch (sev) {
			case 'refactor': return vscode.DiagnosticSeverity.Hint;
			case 'convention': return vscode.DiagnosticSeverity.Information;
			case 'warning': return vscode.DiagnosticSeverity.Warning;
			case 'error': return vscode.DiagnosticSeverity.Error;
			case 'fatal': return vscode.DiagnosticSeverity.Error;
			default: return vscode.DiagnosticSeverity.Error;
		}
	}
}

{'fraction_non_alphanumeric': 0.14073506891271056, 'fraction_numerical': 0.0033690658499234303, 'mean_word_length': 3.61228813559322, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '4067902', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2113, 'n_words': 464}