<commit_before>'use babel';

import { CompositeDisposable } from 'atom';
import { markAbbreviation, clearMarkers } from './abbreviation-marker';
import { hasAutoActivateContext } from './detect-syntax';

/**
 * As user types in current text editor, marks Emmet abbreviation under caret in
 * text buffer. This abbreviation mark can be used for further analysis and
 * to provide autocomplete suggestions.
 */
export default class MarkerManager {
	constructor() {
		this.editor = null;
		this.subscriptions = new CompositeDisposable();
		this.markAbbreviation = this.markAbbreviation.bind(this);
		this.markAbbreviationForKnownSyntax = this.markAbbreviationForKnownSyntax.bind(this);
		this.subscriptions.add(atom.textEditors.observe(editor => {
			const view = atom.views.getView(editor);

			// attach to initially focused editor
			if (view === document.activeElement.closest('atom-text-editor')) {
				this.setCurrentEditor(editor);
			}

			view.addEventListener('focus', () => this.setCurrentEditor(editor));
		}));
	}

	setCurrentEditor(editor) {
		if (!editor || editor === this.editor) {
			return;
		}

		this._disposeEditor();

		if (!this.isSupportedEditor(editor)) {
			return;
		}

		this.editor = editor;
		this.editorSubscriptions = new CompositeDisposable();
		const buffer = this.editor.getBuffer();

		if (typeof buffer.onDidChangeText === 'function') {
			this.editorSubscriptions.add(buffer.onDidChangeText(this.markAbbreviationForKnownSyntax));
		} else {
			// TODO: Remove this after `TextBuffer.prototype.onDidChangeText` lands on Atom stable.
			this.editorSubscriptions.add(buffer.onDidChange(this.markAbbreviationForKnownSyntax));
		}
	}

	/**
	 * Marks Emmet abbreviation under caret of current editor
	 * @return {DisplayMarker}
	 */
	markAbbreviation() {
		// NB: do not check for snippet expansions here because user have to
		// decide wether he wants to expand abbreviation or move to next tabstop
		if (this.editor) {
			return markAbbreviation(this.editor, this.editor.getCursorBufferPosition());
		}
	}

	/**
	 * Marks Emmet abbreviation under caret only if editor has supported syntax.
	 * Also, for some syntaxes (like JavaScript) tries to decide if we should
	 * automatically plant marker for current scope
	 * @return {DisplayMarker}
	 */
	markAbbreviationForKnownSyntax() {
		if (this.editor && hasAutoActivateContext(this.editor)) {
			return this.markAbbreviation();
		}
	}

	/**
	 * Check if given editor should receive Emmet abbreviation markers
	 */
	isSupportedEditor(editor) {
		// NB install abbreviation marker for every editor but auto-insert it
		// for known syntaxes only
		return true;
	}

	/**
	 * Removes Emmet markers from active editor
	 */
	clearMarkers() {
		if (this.editor) {
			clearMarkers(this.editor);
		}
	}

	_disposeEditor() {
		if (this.editorSubscriptions) {
			this.editorSubscriptions.dispose();
		}

		this.clearMarkers();
		this.editor = this.editorSubscriptions = null;
	}

	dispose() {
		this.subscriptions.dispose();
		this._disposeCurrentEditor();
		this.subscriptions = null;
	}
}<commit_msg>Fixed marker manager disposal<commit_after>'use babel';

import { CompositeDisposable } from 'atom';
import { markAbbreviation, clearMarkers } from './abbreviation-marker';
import { hasAutoActivateContext } from './detect-syntax';

/**
 * As user types in current text editor, marks Emmet abbreviation under caret in
 * text buffer. This abbreviation mark can be used for further analysis and
 * to provide autocomplete suggestions.
 */
export default class MarkerManager {
	constructor() {
		this.editor = null;
		this.subscriptions = new CompositeDisposable();
		this.markAbbreviation = this.markAbbreviation.bind(this);
		this.markAbbreviationForKnownSyntax = this.markAbbreviationForKnownSyntax.bind(this);
		this.subscriptions.add(atom.textEditors.observe(editor => {
			const view = atom.views.getView(editor);

			// attach to initially focused editor
			if (view === document.activeElement.closest('atom-text-editor')) {
				this.setCurrentEditor(editor);
			}

			view.addEventListener('focus', () => this.setCurrentEditor(editor));
		}));
	}

	setCurrentEditor(editor) {
		if (!editor || editor === this.editor) {
			return;
		}

		this._disposeEditor();

		if (!this.isSupportedEditor(editor)) {
			return;
		}

		this.editor = editor;
		this.editorSubscriptions = new CompositeDisposable();
		const buffer = this.editor.getBuffer();

		if (typeof buffer.onDidChangeText === 'function') {
			this.editorSubscriptions.add(buffer.onDidChangeText(this.markAbbreviationForKnownSyntax));
		} else {
			// TODO: Remove this after `TextBuffer.prototype.onDidChangeText` lands on Atom stable.
			this.editorSubscriptions.add(buffer.onDidChange(this.markAbbreviationForKnownSyntax));
		}
	}

	/**
	 * Marks Emmet abbreviation under caret of current editor
	 * @return {DisplayMarker}
	 */
	markAbbreviation() {
		// NB: do not check for snippet expansions here because user have to
		// decide wether he wants to expand abbreviation or move to next tabstop
		if (this.editor) {
			return markAbbreviation(this.editor, this.editor.getCursorBufferPosition());
		}
	}

	/**
	 * Marks Emmet abbreviation under caret only if editor has supported syntax.
	 * Also, for some syntaxes (like JavaScript) tries to decide if we should
	 * automatically plant marker for current scope
	 * @return {DisplayMarker}
	 */
	markAbbreviationForKnownSyntax() {
		if (this.editor && hasAutoActivateContext(this.editor)) {
			return this.markAbbreviation();
		}
	}

	/**
	 * Check if given editor should receive Emmet abbreviation markers
	 */
	isSupportedEditor(editor) {
		// NB install abbreviation marker for every editor but auto-insert it
		// for known syntaxes only
		return true;
	}

	/**
	 * Removes Emmet markers from active editor
	 */
	clearMarkers() {
		if (this.editor) {
			clearMarkers(this.editor);
		}
	}

	_disposeEditor() {
		if (this.editorSubscriptions) {
			this.editorSubscriptions.dispose();
		}

		this.clearMarkers();
		this.editor = this.editorSubscriptions = null;
	}

	dispose() {
		this.subscriptions.dispose();
		this._disposeEditor();
		this.subscriptions = null;
	}
}
{'fraction_non_alphanumeric': 0.11141789832710736, 'fraction_numerical': 0.0, 'mean_word_length': 4.174789915966387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1266816', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1785, 'n_words': 469}