<commit_before>'use strict';

import * as path from 'path';

import * as vscode from 'vscode';
import * as postcss from 'postcss';

const stylefmt = require('stylefmt');
const configScanner = require('stylefmt/lib/params');
const scssSyntax = require('postcss-scss');

interface IStylefmtOptions {
	formatOnSave: boolean;
	useStylelintConfigOverrides: boolean;
}

interface IStylelintOptions {
	configOverrides: any;
}

interface IResult {
	css: string;
	range: vscode.Range;
}

let editorConfiguration: IStylefmtOptions;
let output: vscode.OutputChannel;

/**
 * Show message in output channel.
 */
function showOutput(msg: string): void {
	msg = msg.toString();

	if (!output) {
		output = vscode.window.createOutputChannel('Stylefmt');
	}

	output.clear();
	output.appendLine('[Stylefmt]');
	output.append(msg);
	output.show();
}

/**
 * Use Stylefmt module.
 */
function useStylefmt(document: vscode.TextDocument, range: vscode.Range, provider = false): Promise<any> {
	const settings = vscode.workspace.getConfiguration();
	const cwd = document.uri.fsPath ? path.dirname(document.uri.fsPath) : vscode.workspace.rootPath;

	let overrides = null;
	const useOverrides = settings.get<IStylefmtOptions>('stylefmt').useStylelintConfigOverrides;
	if (useOverrides) {
		overrides = settings.get<IStylelintOptions>('stylelint').configOverrides;
	}

	let text;
	if (!range) {
		const lastLine = document.lineAt(document.lineCount - 1);
		const start = new vscode.Position(0, 0);
		const end = new vscode.Position(document.lineCount - 1, lastLine.text.length);

		range = new vscode.Range(start, end);
		text = document.getText();
	} else {
		text = document.getText(range);
	}

	return configScanner({ cwd }).then(async (options) => {
		options.skip = true;

		if (overrides) {
			options.stylelint = Object.assign(options.stylelint, overrides);
		}

		return postcss([stylefmt(options)])
			.process(text, document.languageId === 'scss' && { syntax: scssSyntax })
			.then((result) => {
				if (!provider) {
					return <IResult>{
						css: result.css,
						range
					};
				}

				return [vscode.TextEdit.replace(range, result.css)];
			});
	}).catch(showOutput);
}

export function activate(context: vscode.ExtensionContext) {
	editorConfiguration = vscode.workspace.getConfiguration().get<IStylefmtOptions>('stylefmt');

	const supportedDocuments: vscode.DocumentSelector = [
		{ language: 'css', scheme: 'file' },
		{ language: 'scss', scheme: 'file' }
	];

	const command = vscode.commands.registerTextEditorCommand('stylefmt.execute', (textEditor) => {
		useStylefmt(textEditor.document, null)
			.then((result) => {
				textEditor.edit((editBuilder) => {
					editBuilder.replace(result.range, result.css);
				});
			});
	});

	const formatFullCode = vscode.languages.registerDocumentFormattingEditProvider(supportedDocuments, {
		provideDocumentFormattingEdits(document: vscode.TextDocument) {
			return useStylefmt(document, null, true);
		}
	});

	const formatRangeCode = vscode.languages.registerDocumentRangeFormattingEditProvider(supportedDocuments, {
		provideDocumentRangeFormattingEdits(document, range) {
			return useStylefmt(document, range, true);
		}
	});

	// Subscriptions
	context.subscriptions.push(command);
	context.subscriptions.push(formatFullCode);
	context.subscriptions.push(formatRangeCode);
}<commit_msg>Small tweaks for #28<commit_after>'use strict';

import * as path from 'path';

import * as vscode from 'vscode';
import * as postcss from 'postcss';

const stylefmt = require('stylefmt');
const configScanner = require('stylefmt/lib/params');
const scssSyntax = require('postcss-scss');

interface IStylefmtOptions {
	formatOnSave: boolean;
	useStylelintConfigOverrides: boolean;
}

interface IStylelintOptions {
	configOverrides: any;
}

interface IResult {
	css: string;
	range: vscode.Range;
}

let editorConfiguration: IStylefmtOptions;
let output: vscode.OutputChannel;

/**
 * Show message in output channel.
 */
function showOutput(msg: string): void {
	msg = msg.toString();

	if (!output) {
		output = vscode.window.createOutputChannel('Stylefmt');
	}

	output.clear();
	output.appendLine('[Stylefmt]');
	output.append(msg);
	output.show();
}

/**
 * Use Stylefmt module.
 */
function useStylefmt(document: vscode.TextDocument, range: vscode.Range): Promise<IResult> {
	const settings = vscode.workspace.getConfiguration();
	const cwd = document.uri.fsPath ? path.dirname(document.uri.fsPath) : vscode.workspace.rootPath;

	let overrides = null;
	const useOverrides = settings.get<IStylefmtOptions>('stylefmt').useStylelintConfigOverrides;
	if (useOverrides) {
		overrides = settings.get<IStylelintOptions>('stylelint').configOverrides;
	}

	let text;
	if (!range) {
		const lastLine = document.lineAt(document.lineCount - 1);
		const start = new vscode.Position(0, 0);
		const end = new vscode.Position(document.lineCount - 1, lastLine.text.length);

		range = new vscode.Range(start, end);
		text = document.getText();
	} else {
		text = document.getText(range);
	}

	return configScanner({ cwd }).then(async (options) => {
		options.skip = true;

		if (overrides) {
			options.stylelint = Object.assign(options.stylelint, overrides);
		}

		return postcss([stylefmt(options)])
			.process(text, document.languageId === 'scss' && { syntax: scssSyntax })
			.then((result) => (<IResult>{
				css: result.css,
				range
			}));
	});
}

export function activate(context: vscode.ExtensionContext) {
	editorConfiguration = vscode.workspace.getConfiguration().get<IStylefmtOptions>('stylefmt');

	const supportedDocuments: vscode.DocumentSelector = [
		{ language: 'css', scheme: 'file' },
		{ language: 'scss', scheme: 'file' }
	];

	const command = vscode.commands.registerTextEditorCommand('stylefmt.execute', (textEditor) => {
		useStylefmt(textEditor.document, null)
			.then((result) => {
				textEditor.edit((editBuilder) => {
					editBuilder.replace(result.range, result.css);
				});
			})
			.catch(showOutput);
	});

	const formatCode = vscode.languages.registerDocumentRangeFormattingEditProvider(supportedDocuments, {
		provideDocumentRangeFormattingEdits(document, range) {
			return useStylefmt(document, range).then((result) => {
				return [vscode.TextEdit.replace(range, result.css)];
			}).catch(showOutput);
		}
	});

	// Subscriptions
	context.subscriptions.push(command);
	context.subscriptions.push(formatCode);
}
{'fraction_non_alphanumeric': 0.13996865203761755, 'fraction_numerical': 0.001567398119122257, 'mean_word_length': 4.9915492957746475, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 24, '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': '6671256', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2042, 'n_words': 396}