<commit_before>var completion = {};

function init_completion(el)
{
	if (completion[el.id]) return;
	var load = document.createElement("img");
	load.className = "ajaxload";
	load.src = uribase + "static/ajaxload.gif";
	load.style.visibility = "hidden";
	el.parentNode.insertBefore(load, el.nextSibling);
	completion[el.id] = {"value": el.value, "x": null, "skip": false,
	                     "load": load, "abort": false, "tO": false,
	                     "r": null, "list": null, "complete": ""};
	el.onfocus = null;
	el.onkeypress = soon_completion_ev;
	el.onblur = remove_completion_ev;
}

function find_word(el)
{
	var start = el.selectionStart;
	var end = el.selectionEnd;
	if (!start || start != end) return "";
	var txt = el.value;
	if (end < txt.length && txt.substr(end, 1) != " ") return "";
	while (start > 0 && txt.substr(start - 1, 1) != " ") start--;
	return txt.substr(start, end - start);
}

function soon_completion(el)
{
	var c = completion[el.id];
	if (!c) return;
	if (c.tO) clearTimeout(c.tO);
	clear_completion(el);
	c.tO = setTimeout(function () { run_completion(el); }, 160);
}

function soon_completion_ev()
{
	soon_completion(this);
}

function set_complete(el, r)
{
	var alts = r.alts;
	if (!alts) return;
	if (!alts.length) {
		if (r.type) {
			alts = [[r.complete, r.type]];
		} else {
			return;
		}
	}
	if (alts.length == 1) {
		var word = tag_clean(find_word(el));
		if (word == alts[0][0]) return;
	}
	var old_div = document.getElementById("suggestions");
	if (old_div) {
		/* There seems to be a bug where we sometimes *
		 * still have an old one, so get rid of that. */
		document.getElementsByTagName("body")[0].removeChild(old_div);
	}
	var div = document.createElement("div");
	var c = completion[el.id];
	c.complete = r.complete;
	div.id = "suggestions";
	var pos = findpos(el);
	div.style.left = "" + pos.x + "px";
	div.style.top = "" + pos.y + "px";
	div.style.minWidth = "" + el.offsetWidth + "px";
	if (el.id == "tagmode-tags") div.style.position = "fixed";
	var ul = document.createElement("ul");
	div.appendChild(ul);
	_foreach(alts, function (td) {
		var n = td[0];
		var t = td[1];
		var li = document.createElement("li");
		var span = document.createElement("span");
		span.appendChild(document.createTextNode(n));
		span.className = "tt-" + t;
		li.appendChild(span);
		li.onmouseover = sel_this;
		li.onclick = function (ev) {
			try {
				ev.stopPropagation();
			} catch(e) {}
			return sel_done(el, li.firstChild.firstChild.data, true);
		};
		ul.appendChild(li);
	});
	document.getElementsByTagName("body")[0].appendChild(div);
	c.list = div;
	el.onkeydown = function (ev) {
		if (ev.keyCode == 40) { /* down */
			sel_move(ul, 1);
			c.skip = true;
		} else if (ev.keyCode == 38) { /* up */
			sel_move(ul, -1);
			c.skip = true;
		} else {
			c.skip = false;
		}
		return true;
	};
	el.onkeypress = function (ev) {
		var d = {"idx": -1};
		var val = "";
		var full = true;
		if (ev.keyCode == 9) { /* tab */
			d = sel_find(ul);
			if (d.idx == -1) {
				if (d.lis.length == 1) {
					d.idx = 0;
				} else {
					val = c.complete;
					full = false;
				}
			}
		} else if (ev.keyCode == 13) { /* return */
			d = sel_find(ul);
		}
		if (d.idx >= 0) {
			val = d.lis[d.idx].firstChild.firstChild.data;
		}
		if (val) {
			return sel_done(el, val, full);
		}
		if (c.skip) return false;
		clear_completion(el);
		soon_completion(el);
		return true;
	};
}

function run_completion(el)
{
	var c = completion[el.id];
	if (!c) return;
	if (c.value == el.value) return;
	c.value = el.value;
	var word = find_word(el);
	word = tag_clean(word);
	if (!word) return;
	c.load.style.visibility = "visible";
	c.abort = false;
	var x = new XMLHttpRequest();
	x.open("GET", uribase + "ajax-completetag?q=" + encodeURIComponent(word));
	x.onreadystatechange = function () {
		if (c.abort) return;
		if (x.readyState != 4) return;
		c.load.style.visibility = "hidden";
		if (x.status != 200) return;
		var txt = x.responseText;
		if (txt.substr(0, 1) != "{") return;<commit_msg>Don't try to run completion with less than two characters.<commit_after>var completion = {};

function init_completion(el)
{
	if (completion[el.id]) return;
	var load = document.createElement("img");
	load.className = "ajaxload";
	load.src = uribase + "static/ajaxload.gif";
	load.style.visibility = "hidden";
	el.parentNode.insertBefore(load, el.nextSibling);
	completion[el.id] = {"value": el.value, "x": null, "skip": false,
	                     "load": load, "abort": false, "tO": false,
	                     "r": null, "list": null, "complete": ""};
	el.onfocus = null;
	el.onkeypress = soon_completion_ev;
	el.onblur = remove_completion_ev;
}

function find_word(el)
{
	var start = el.selectionStart;
	var end = el.selectionEnd;
	if (!start || start != end) return "";
	var txt = el.value;
	if (end < txt.length && txt.substr(end, 1) != " ") return "";
	while (start > 0 && txt.substr(start - 1, 1) != " ") start--;
	return txt.substr(start, end - start);
}

function soon_completion(el)
{
	var c = completion[el.id];
	if (!c) return;
	if (c.tO) clearTimeout(c.tO);
	clear_completion(el);
	c.tO = setTimeout(function () { run_completion(el); }, 160);
}

function soon_completion_ev()
{
	soon_completion(this);
}

function set_complete(el, r)
{
	var alts = r.alts;
	if (!alts) return;
	if (!alts.length) {
		if (r.type) {
			alts = [[r.complete, r.type]];
		} else {
			return;
		}
	}
	if (alts.length == 1) {
		var word = tag_clean(find_word(el));
		if (word == alts[0][0]) return;
	}
	var old_div = document.getElementById("suggestions");
	if (old_div) {
		/* There seems to be a bug where we sometimes *
		 * still have an old one, so get rid of that. */
		document.getElementsByTagName("body")[0].removeChild(old_div);
	}
	var div = document.createElement("div");
	var c = completion[el.id];
	c.complete = r.complete;
	div.id = "suggestions";
	var pos = findpos(el);
	div.style.left = "" + pos.x + "px";
	div.style.top = "" + pos.y + "px";
	div.style.minWidth = "" + el.offsetWidth + "px";
	if (el.id == "tagmode-tags") div.style.position = "fixed";
	var ul = document.createElement("ul");
	div.appendChild(ul);
	_foreach(alts, function (td) {
		var n = td[0];
		var t = td[1];
		var li = document.createElement("li");
		var span = document.createElement("span");
		span.appendChild(document.createTextNode(n));
		span.className = "tt-" + t;
		li.appendChild(span);
		li.onmouseover = sel_this;
		li.onclick = function (ev) {
			try {
				ev.stopPropagation();
			} catch(e) {}
			return sel_done(el, li.firstChild.firstChild.data, true);
		};
		ul.appendChild(li);
	});
	document.getElementsByTagName("body")[0].appendChild(div);
	c.list = div;
	el.onkeydown = function (ev) {
		if (ev.keyCode == 40) { /* down */
			sel_move(ul, 1);
			c.skip = true;
		} else if (ev.keyCode == 38) { /* up */
			sel_move(ul, -1);
			c.skip = true;
		} else {
			c.skip = false;
		}
		return true;
	};
	el.onkeypress = function (ev) {
		var d = {"idx": -1};
		var val = "";
		var full = true;
		if (ev.keyCode == 9) { /* tab */
			d = sel_find(ul);
			if (d.idx == -1) {
				if (d.lis.length == 1) {
					d.idx = 0;
				} else {
					val = c.complete;
					full = false;
				}
			}
		} else if (ev.keyCode == 13) { /* return */
			d = sel_find(ul);
		}
		if (d.idx >= 0) {
			val = d.lis[d.idx].firstChild.firstChild.data;
		}
		if (val) {
			return sel_done(el, val, full);
		}
		if (c.skip) return false;
		clear_completion(el);
		soon_completion(el);
		return true;
	};
}

function run_completion(el)
{
	var c = completion[el.id];
	if (!c) return;
	if (c.value == el.value) return;
	c.value = el.value;
	var word = find_word(el);
	word = tag_clean(word);
	if (word.length < 2) return;
	c.load.style.visibility = "visible";
	c.abort = false;
	var x = new XMLHttpRequest();
	x.open("GET", uribase + "ajax-completetag?q=" + encodeURIComponent(word));
	x.onreadystatechange = function () {
		if (c.abort) return;
		if (x.readyState != 4) return;
		c.load.style.visibility = "hidden";
		if (x.status != 200) return;
		var txt = x.responseText;
		if (txt.substr(0, 1) != "{") return;
{'fraction_non_alphanumeric': 0.1843892799802396, 'fraction_numerical': 0.008521674694331233, 'mean_word_length': 3.271097046413502, 'pattern_counts': {'":': 20, '<': 6, '<?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': '2597125', 'n_tokens_mistral': 3397, 'n_tokens_neox': 3095, 'n_words': 715}