<commit_before>import {
	Range, Position, TextEditor, ExtensionContext,
	commands, window, StatusBarAlignment,
	TextEditorSelectionChangeEvent 
} from 'vscode'; 
import { indentMode, parenMode, IPosition } from './parinfer';

enum Mode {
	Paren,
	Indent
}

function debounce(fn: (...args: any[]) => void, delay: number): (...args: any[])=>void {
	let timer;
	return (...args: any[]) => {
		clearTimeout(timer);
		timer = setTimeout(() => fn(...args), delay);
	};
}

function fromEditorPosition(editorPosition: Position): IPosition {
	return { row: editorPosition.line, column: editorPosition.character }; 
}

export function activate(context: ExtensionContext) {
	let mode: Mode = Mode.Paren;
	
	const statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right);
	statusBarItem.show();
	
	function render() {
		statusBarItem.text = mode === Mode.Paren ? 'Paren' : 'Indent';
	}

	function toggleMode() {
		mode = mode === Mode.Paren ? Mode.Indent : Mode.Paren;
		render();
	}
	
	function parinfer(editor: TextEditor) {
		const document = editor.document;
		const input = document.getText();
		const fn = mode === Mode.Paren ? parenMode : indentMode;
		const output = fn(input, fromEditorPosition(editor.selection.active));
		
		if (typeof output !== 'string') {
			console.log('got from parinfer:', output);
			return;
		}
		
		const range = new Range(new Position(0, 0), editor.document.positionAt(input.length));
		editor.edit(builder => builder.replace(range, output));
		console.log('success');
	}
	
	const eventuallyParinfer = debounce(parinfer, 50);
	
	function onSelectionChange(e: TextEditorSelectionChangeEvent) {
		if (!/\.clj$/.test(e.textEditor.document.fileName)) {
			return;
		}
		
		eventuallyParinfer(e.textEditor);
	}
	
	context.subscriptions.push(
		commands.registerCommand('parinfer.toggleMode', toggleMode),
		window.onDidChangeTextEditorSelection(onSelectionChange)
	);
	
	render();
}<commit_msg>run paren on editor change<commit_after>import {
	Range, Position, TextEditor, ExtensionContext,
	commands, window, StatusBarAlignment,
	TextEditorSelectionChangeEvent 
} from 'vscode'; 
import { indentMode, parenMode, IPosition } from './parinfer';

enum Mode {
	Paren,
	Indent
}

function debounce(fn: (...args: any[]) => void, delay: number): (...args: any[])=>void {
	let timer;
	return (...args: any[]) => {
		clearTimeout(timer);
		timer = setTimeout(() => fn(...args), delay);
	};
}

function fromEditorPosition(editorPosition: Position): IPosition {
	if (!editorPosition) {
		return null;
	}
	
	return { row: editorPosition.line, column: editorPosition.character }; 
}

function shouldRun(fileName: string): boolean {
	return /\.clj$/.test(fileName);
}

export function activate(context: ExtensionContext) {
	let mode: Mode = Mode.Paren;
	
	const statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right);
	statusBarItem.show();
	
	function render() {
		statusBarItem.text = mode === Mode.Paren ? 'Paren' : 'Indent';
	}

	function toggleMode() {
		mode = mode === Mode.Paren ? Mode.Indent : Mode.Paren;
		render();
	}
	
	function parinfer(editor: TextEditor, position: Position = null) {
		const document = editor.document;
		const input = document.getText();
		const fn = (mode === Mode.Paren || !position) ? parenMode : indentMode;
		const output = fn(input, fromEditorPosition(position));
		
		if (typeof output !== 'string') {
			console.log('got from parinfer:', output);
			return;
		}
		
		const range = new Range(new Position(0, 0), document.positionAt(input.length));
		editor.edit(builder => builder.replace(range, output));
		console.log('success');
	}
	
	const eventuallyParinfer = debounce(parinfer, 50);
	
	function onSelectionChange({ textEditor }: TextEditorSelectionChangeEvent) {
		if (!shouldRun(textEditor.document.fileName)) {
			return;
		}
		
		eventuallyParinfer(textEditor, textEditor.selection.active);
	}
	
	function onEditorChange(editor: TextEditor) {
		if (!shouldRun(editor.document.fileName)) {
			return;
		}
		
		parinfer(editor);
	}
	
	context.subscriptions.push(
		commands.registerCommand('parinfer.toggleMode', toggleMode),
		window.onDidChangeTextEditorSelection(onSelectionChange),
		window.onDidChangeActiveTextEditor(onEditorChange)
	);
	
	render();
}
{'fraction_non_alphanumeric': 0.14272234017456947, 'fraction_numerical': 0.001887237556027365, 'mean_word_length': 4.72972972972973, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2790591', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1294, 'n_words': 290}