<commit_before>﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Linq;
using System.Threading;

namespace UnityShell
{

public class Gui : MonoBehaviour
{
	static public Gui instance;
	private History history_ = new History();

	public InputField input;
	public Text output;

	public CompletionView completionView;
	public float completionTimer = 0.5f;
	private bool isComplementing_ = false;
	private bool isCompletionStopped_ = false;
	private float elapsedTimeFromLastInput_ = 0f;

	private string partial_ = "";
	private string completionPrefix_ = "";

	private Thread completionThread_;
	private string[] completions_;

	void Awake()
	{
		instance = this;
	}

	void Start()
	{
		RegisterListeners();
		history_.Load();
	}

	void OnDestroy()
	{
		AbortCompletion();
		UnregisterListeners();
		history_.Save();
	}

	void Update()
	{
		if (input.isFocused) {
			CheckCommands();
			CheckEmacsLikeCommands();
		}

		UpdateCompletion();
	}

	private void Prev()
	{
		if (isComplementing_) {
			completionView.Next();
		} else {
			if (history_.IsFirst()) history_.SetInputting(input.text);
			input.text = history_.Prev();
			isCompletionStopped_ = true;
		}
		input.MoveTextEnd(false);
	}

	private void Next()
	{
		if (isComplementing_) {
			completionView.Prev();
		} else {
			input.text = history_.Next();
			isCompletionStopped_ = true;
		}
		input.MoveTextEnd(false);
	}

	private void CheckCommands()
	{
		if (Input.GetKeyDown(KeyCode.UpArrow)) {
			Prev();
		}
		if (Input.GetKeyDown(KeyCode.DownArrow)) {
			Next();
		}
		if (Input.GetKeyDown(KeyCode.Tab)) {
			isCompletionStopped_ = false;
			if (isComplementing_) {
				DoCompletion();
			} else {
				SetCompletions();
			}
		}
		if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) {
			if (isComplementing_) {
				DoCompletion();
			} else {
				OnSubmit(input.text);
			}
		}
		if (Input.GetKeyDown(KeyCode.Escape)) {
			StopCompletion();
		}
	}

	void CheckEmacsLikeCommands()
	{
		if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
			if (Input.GetKeyDown(KeyCode.P)) {
				Prev();
			}
			if (Input.GetKeyDown(KeyCode.N)) {
				Next();
			}
			if (Input.GetKeyDown(KeyCode.F)) {
				input.caretPosition = Mathf.Min(input.caretPosition + 1, input.text.Length);
			}
			if (Input.GetKeyDown(KeyCode.B)) {
				input.caretPosition = Mathf.Max(input.caretPosition - 1, 0);
			}
			if (Input.GetKeyDown(KeyCode.A)) {
				input.MoveTextStart(false);
			}
			if (Input.GetKeyDown(KeyCode.E)) {
				input.MoveTextEnd(false);
			}
			if (Input.GetKeyDown(KeyCode.H)) {
				if (input.caretPosition > 0) {
					input.text = input.text.Remove(input.caretPosition - 1, 1);
				}
			}
			if (Input.GetKeyDown(KeyCode.D)) {
				if (input.caretPosition < input.text.Length) {
					input.text = input.text.Remove(input.caretPosition, 1);
				}
			}
			if (Input.GetKeyDown(KeyCode.K)) {
				if (input.caretPosition < input.text.Length) {
					input.text = input.text.Remove(input.caretPosition);
				}
			}
		}
	}

	private void UpdateCompletion()
	{
		elapsedTimeFromLastInput_ += Time.deltaTime;

		// stop completion thread if it is running to avoid hang.
		if (elapsedTimeFromLastInput_ > completionTimer + 0.5f /* margin */) {
			AbortCompletion();
		}

		// show completion view after waiting for completionTimer.
		if (!isCompletionStopped_ && !isComplementing_ && !IsCompletionThreadAlive()) {
			if (elapsedTimeFromLastInput_ >= completionTimer) {
				SetCompletions();
			}
		}<commit_msg>fix the caret position after ctrl-h pressed.<commit_after>﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Linq;
using System.Threading;

namespace UnityShell
{

public class Gui : MonoBehaviour
{
	static public Gui instance;
	private History history_ = new History();

	public InputField input;
	public Text output;

	public CompletionView completionView;
	public float completionTimer = 0.5f;
	private bool isComplementing_ = false;
	private bool isCompletionStopped_ = false;
	private float elapsedTimeFromLastInput_ = 0f;

	private string partial_ = "";
	private string completionPrefix_ = "";

	private Thread completionThread_;
	private string[] completions_;

	void Awake()
	{
		instance = this;
	}

	void Start()
	{
		RegisterListeners();
		history_.Load();
	}

	void OnDestroy()
	{
		AbortCompletion();
		UnregisterListeners();
		history_.Save();
	}

	void Update()
	{
		if (input.isFocused) {
			CheckCommands();
			CheckEmacsLikeCommands();
		}

		UpdateCompletion();
	}

	private void Prev()
	{
		if (isComplementing_) {
			completionView.Next();
		} else {
			if (history_.IsFirst()) history_.SetInputting(input.text);
			input.text = history_.Prev();
			isCompletionStopped_ = true;
		}
		input.MoveTextEnd(false);
	}

	private void Next()
	{
		if (isComplementing_) {
			completionView.Prev();
		} else {
			input.text = history_.Next();
			isCompletionStopped_ = true;
		}
		input.MoveTextEnd(false);
	}

	private void CheckCommands()
	{
		if (Input.GetKeyDown(KeyCode.UpArrow)) {
			Prev();
		}
		if (Input.GetKeyDown(KeyCode.DownArrow)) {
			Next();
		}
		if (Input.GetKeyDown(KeyCode.Tab)) {
			isCompletionStopped_ = false;
			if (isComplementing_) {
				DoCompletion();
			} else {
				SetCompletions();
			}
		}
		if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter)) {
			if (isComplementing_) {
				DoCompletion();
			} else {
				OnSubmit(input.text);
			}
		}
		if (Input.GetKeyDown(KeyCode.Escape)) {
			StopCompletion();
		}
	}

	void CheckEmacsLikeCommands()
	{
		if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
			if (Input.GetKeyDown(KeyCode.P)) {
				Prev();
			}
			if (Input.GetKeyDown(KeyCode.N)) {
				Next();
			}
			if (Input.GetKeyDown(KeyCode.F)) {
				input.caretPosition = Mathf.Min(input.caretPosition + 1, input.text.Length);
			}
			if (Input.GetKeyDown(KeyCode.B)) {
				input.caretPosition = Mathf.Max(input.caretPosition - 1, 0);
			}
			if (Input.GetKeyDown(KeyCode.A)) {
				input.MoveTextStart(false);
			}
			if (Input.GetKeyDown(KeyCode.E)) {
				input.MoveTextEnd(false);
			}
			if (Input.GetKeyDown(KeyCode.H)) {
				if (input.caretPosition > 0) {
					input.text = input.text.Remove(input.caretPosition - 1, 1);
					--input.caretPosition;
				}
			}
			if (Input.GetKeyDown(KeyCode.D)) {
				if (input.caretPosition < input.text.Length) {
					input.text = input.text.Remove(input.caretPosition, 1);
				}
			}
			if (Input.GetKeyDown(KeyCode.K)) {
				if (input.caretPosition < input.text.Length) {
					input.text = input.text.Remove(input.caretPosition);
				}
			}
		}
	}

	private void UpdateCompletion()
	{
		elapsedTimeFromLastInput_ += Time.deltaTime;

		// stop completion thread if it is running to avoid hang.
		if (elapsedTimeFromLastInput_ > completionTimer + 0.5f /* margin */) {
			AbortCompletion();
		}

		// show completion view after waiting for completionTimer.
		if (!isCompletionStopped_ && !isComplementing_ && !IsCompletionThreadAlive()) {
			if (elapsedTimeFromLastInput_ >= completionTimer) {
				SetCompletions();
			}
		}
{'fraction_non_alphanumeric': 0.13036831132731064, 'fraction_numerical': 0.0033356497567755385, 'mean_word_length': 3.59514687100894, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '805256', 'n_tokens_mistral': 2867, 'n_tokens_neox': 2464, 'n_words': 465}