<commit_before>'use babel';

import { CompositeDisposable } from 'atom';

export default {
	events: null,
	timer1: null,
	
	activate(state) {
		this.subscriptions = new CompositeDisposable();
		this.subscriptions.add(
			atom.workspace.observeTextEditors(this.callback.bind(this))
		);
		this.subscriptions.add(
			atom.grammars.onDidAddGrammar(this.callback_grammar_added.bind(this))
		);
	},
	
	callback(editor) {
		if (editor.getGrammar() != atom.grammars.nullGrammar) {
			return;
		}
		
		line = editor.lineTextForBufferRow(0);
		if (! line.startsWith('#!')) {
			return;
		}
		
		if (! line.endsWith('python')) {
			return;
		}
		
		grammar = atom.grammars.grammarForScopeName('source.python');
		if (grammar == null) {
			return;
		}
		
		editor.setGrammar(grammar);
	},
	
	callback_grammar_added(grammar) {
		if (this.timer1 != null) {<commit_msg>improvement: not hardcoded for python anymore but dynamic: "/usr/bin/python -a -b -c" -> "python" -> load grammar source.python<commit_after>'use babel';

import { CompositeDisposable } from 'atom';

export default {
	events: null,
	timer1: null,
	bin2grammar: {
		'bash': 'shell',
		'sh': 'shell'
	},
	
	activate(state) {
		this.subscriptions = new CompositeDisposable();
		this.subscriptions.add(
			atom.workspace.observeTextEditors(this.callback.bind(this))
		);
		this.subscriptions.add(
			atom.grammars.onDidAddGrammar(this.callback_grammar_added.bind(this))
		);
	},
	
	callback(editor) {
		if (editor.getGrammar() != atom.grammars.nullGrammar) {
			return;
		}
		
		line = editor.lineTextForBufferRow(0);
		if (! line.startsWith('#!')) {
			return;
		}
		
		var bin_name = this.get_shebang_basename(line.substr(2).trim());
		if (bin_name in this.bin2grammar) {
			bin_name = this.bin2grammar[bin_name];
		}
		
		grammar = atom.grammars.grammarForScopeName('source.'+bin_name);
		if (grammar == null) {
			return;
		}
		
		editor.setGrammar(grammar);
	},
	
	get_shebang_basename(line) {
		/*
			/usr/bin/python 	=> python
			/usr/bin/python -e 	=> python
			/bin/bash 			=> bash
			perl -e 			=> perl
		*/
		
		var parts = line.split(/[\s\t]+/);
		for (var i=parts.length-1; i>0; i--) {
			if (parts[i].startsWith("-")) {
				continue;
			}
			
			return parts[i];
		}
		
		parts = parts[0].split("/");
		return parts[parts.length - 1];
	},
	
	callback_grammar_added(grammar) {
		if (this.timer1 != null) {
{'fraction_non_alphanumeric': 0.15998311523849726, 'fraction_numerical': 0.0059096665259603205, 'mean_word_length': 3.463276836158192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '1616549', 'n_tokens_mistral': 1028, 'n_tokens_neox': 886, 'n_words': 143}