<commit_before>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoTest.Core.Presenters;
using AutoTest.Core.Messaging;
using AutoTest.Core.FileSystem;
using AutoTest.Core.Configuration;
using System.Threading;
using AutoTest.Core.Messaging.MessageConsumers;
using AutoTest.Core.BuildRunners;
using AutoTest.Core.TestRunners;
using System.IO;
using AutoTest.Core.Caching;
using AutoTest.Core.Caching.RunResultCache;

namespace AutoTest.WinForms
{
    public partial class FeedbackForm : Form, IOverviewForm, IRunFeedbackView
    {
        private SynchronizationContext _syncContext;
        private IRunFeedbackPresenter _runPresenter;
        private IDirectoryWatcher _watcher;
        private IInformationForm _informationForm;
        private IRunResultCache _runResultCache;

        private ToolTip _toolTipProvider = new ToolTip();
        private bool _isRefreshingFeedback = false;

        private int _rightSpacing = 0;
        private int _listBottomSpacing = 0;
        private int _infoBottomSpacing = 0;

        public FeedbackForm(IDirectoryWatcher watcher, IConfiguration configuration, IRunFeedbackPresenter runPresenter, IInformationForm informationForm, IRunResultCache runResultCache)
        {
            _syncContext = AsyncOperationManager.SynchronizationContext;
            _toolTipProvider.AutoPopDelay = 30000;
            _watcher = watcher;
            _runResultCache = runResultCache;
            _runPresenter = runPresenter;
            _runPresenter.View = this;
            _informationForm = informationForm;
            _informationForm.MessageArrived += new EventHandler<MessageRecievedEventArgs>(_informationForm_MessageArrived);
            InitializeComponent();
            configuration.ValidateSettings();
            _watcher.Watch(configuration.DirectoryToWatch);
            readFormSpacing();
			FeedbackForm_Resize(this, new EventArgs());
        }

        private void readFormSpacing()
        {
            _rightSpacing = Width - (runFeedbackList.Left + runFeedbackList.Width);
            _listBottomSpacing = linkLabelInfo.Top - (runFeedbackList.Top + runFeedbackList.Height);
            _infoBottomSpacing = Height - (linkLabelInfo.Top + linkLabelInfo.Height);
        }

        void _informationForm_MessageArrived(object sender, MessageRecievedEventArgs e)
        {
            buttonInformation.Text = "|||";
            if (e.Type.Equals(MessageType.Warning) && buttonInformation.ForeColor != Color.Red)
                buttonInformation.ForeColor = Color.Yellow;
            else if (e.Type.Equals(MessageType.Error))
                buttonInformation.ForeColor = Color.Red;
        }

        #region IOverviewForm Members

        public Form Form
        {
            get { return this; }
        }

        #endregion
    
        #region IRunFeedbackView Members

        public void  RecievingRunStartedMessage(RunStartedMessage message)
        {
            _syncContext.Post(s =>
                                  {
                                      setRunInProgressFeedback("");
                                      generateSummary(null);
                                  }, null);
        }

        private void setRunInProgressFeedback(string additionalInfo)
        {
            var text = "Detected changes, running..";
            if (additionalInfo.Length > 0)
                text += string.Format(" ({0})", additionalInfo);
            labelRunState.Text = text;
            labelRunState.ForeColor = Color.Black;
        }

        public void  RecievingRunFinishedMessage(RunFinishedMessage message)
        {
            _syncContext.Post(
                x =>
                {
                    var report = (RunReport) x;
                    labelRunState.Text = string.Format(
                        "Ran {0} build(s) ({1} succeeded, {2} failed) and {3} test(s) ({4} passed, {5} failed, {6} ignored)",
                        report.NumberOfProjectsBuilt,
                        report.NumberOfBuildsSucceeded,
                        report.NumberOfBuildsFailed,
                        report.NumberOfTestsRan,
                        report.NumberOfTestsPassed,
                        report.NumberOfTestsFailed,
                        report.NumberOfTestsIgnored);
                    labelRunState.ForeColor =
                        report.NumberOfBuildsFailed > 0 ||
                        report.NumberOfTestsFailed > 0 ||
                        report.NumberOfTestsIgnored > 0
                            ? Color.Red
                            : Color.Green;
                    generateSummary(report);
                },
                message.Report);
        }

        private void generateSummary(RunReport report)
        {
            if (report == null)
            {
                _toolTipProvider.RemoveAll();
                return;
            }

            var builder = new SummaryBuilder(report);
            _toolTipProvider.SetToolTip(labelRunState, builder.Build());
        }

        public void  RecievingBuildMessage(BuildRunMessage message)
        {
            _syncContext.Post(m => relistFromCache(), null);
        }

        public void  RecievingTestRunMessage(TestRunMessage message)
        {
            _syncContext.Post(m => relistFromCache(), null);
        }

        public void RecievingRunInformationMessage(RunInformationMessage message)
        {
            var text = "";
            switch (message.Type)
            {
                case InformationType.Build:
                    text = string.Format("building {0}", Path.GetFileName(message.Project));
                    break;
                case InformationType.TestRun:
                    text = string.Format("testing {0}", Path.GetFileName(message.Assembly));
                    break;
            }
            _syncContext.Post(m => setRunInProgressFeedback(m.ToString()), text);
        }

        #endregion

        private void relistFromCache()
        {
            _isRefreshingFeedback = true;

            IItem selected = null;
            if (runFeedbackList.SelectedItems.Count == 1)
                selected = (IItem) runFeedbackList.SelectedItems[0].Tag;

            runFeedbackList.Items.Clear();
            foreach (var error in _runResultCache.Errors)
                addFeedbackItem("Build error", formatBuildResult(error), Color.Red, error, selected);

            foreach (var failed in _runResultCache.Failed)
                addFeedbackItem("Test failed", formatTestResult(failed), Color.Red, failed, selected);

            foreach (var warning in _runResultCache.Warnings)
                addFeedbackItem("Build warning", formatBuildResult(warning), Color.Black, warning, selected);
            
            foreach (var ignored in _runResultCache.Ignored)
                addFeedbackItem("Test ignored", formatTestResult(ignored), Color.Black, ignored, selected);

            if (runFeedbackList.Items.Count != 1)
                setInfoText("");

            _isRefreshingFeedback = false;
        }

        private void addFeedbackItem(string type, string message, Color colour, IItem tag, IItem selected)
        {
            var item = runFeedbackList.Items.Add(type);
            item.SubItems.Add(message);
            item.ForeColor = colour;
            item.Tag = tag;
            if (selected != null && tag.Equals(selected))
                item.Selected = true;
        }

        private string formatBuildResult(BuildItem item)
        {
            var buildMessage = item.Value;
            return string.Format("{0}, {1}", buildMessage.ErrorMessage, buildMessage.File);
        }

        private string formatTestResult(TestItem item)
        {
            var test = item.Value;
            return string.Format("{0} -> {1}", test.Status, test.Name);
        }

        private void runFeedbackList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_isRefreshingFeedback)
                return;

            if (runFeedbackList.SelectedItems.Count != 1)
            {
                setInfoText("");
                return;
            }

            setInfoText(runFeedbackList.Items[runFeedbackList.SelectedItems[0].Index].Tag.ToString());
        }

        private void setInfoText(string text)
        {
            int previousHeight = linkLabelInfo.Height;
            var parser = new LinkParser(text);
            var links = parser.Parse();
            linkLabelInfo.Text = parser.ParsedText;
            linkLabelInfo.LinkArea = new LinkArea(0, 0);
            foreach (var link in links)
                linkLabelInfo.Links.Add(link.Start, link.Length);
            var difference = linkLabelInfo.Height - previousHeight;
            Height = Height + difference;
        }

        private void FeedbackForm_Resize(object sender, EventArgs e)
        {
			labelRunState.Width = Width - ((Width - buttonInformation.Left) + _rightSpacing);
            linkLabelInfo.MaximumSize = new Size(Width - (linkLabelInfo.Left + _rightSpacing), 0);
			if (Environment.OSVersion.Platform.Equals(PlatformID.Unix))
			{
				linkLabelInfo.Top = Height - (linkLabelInfo.Height + _infoBottomSpacing + 100);
				runFeedbackList.Height = linkLabelInfo.Top - (runFeedbackList.Top + _listBottomSpacing);
            	runFeedbackList.Width = Width - (runFeedbackList.Left + _rightSpacing + 10);
			}
			else
			{
				linkLabelInfo.Top = Height - (linkLabelInfo.Height + _infoBottomSpacing);
            	runFeedbackList.Height = linkLabelInfo.Top - (runFeedbackList.Top + _listBottomSpacing);
            	runFeedbackList.Width = Width - (runFeedbackList.Left + _rightSpacing);
			}
        }

        private void linkLabelInfo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (runFeedbackList.SelectedItems.Count != 1)
                return;
            var item = (IItem) runFeedbackList.SelectedItems[0].Tag;
            item.HandleLink(linkLabelInfo.Text.Substring(e.Link.Start, e.Link.Length));
        }

        private void buttonInformation_Click(object sender, EventArgs e)
        {
            buttonInformation.Text = "";
            buttonInformation.ForeColor = BackColor;
            var form = _informationForm.Form;
            if (form.Visible == false)
                form.Visible = true;
            else
                form.Show(this);
        }

        private void FeedbackForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _informationForm.Form.Dispose();
        }
    }
}
<commit_msg>Couldn't stand not having a comment describing the mess I just made.. so.. added comment<commit_after>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutoTest.Core.Presenters;
using AutoTest.Core.Messaging;
using AutoTest.Core.FileSystem;
using AutoTest.Core.Configuration;
using System.Threading;
using AutoTest.Core.Messaging.MessageConsumers;
using AutoTest.Core.BuildRunners;
using AutoTest.Core.TestRunners;
using System.IO;
using AutoTest.Core.Caching;
using AutoTest.Core.Caching.RunResultCache;

namespace AutoTest.WinForms
{
    public partial class FeedbackForm : Form, IOverviewForm, IRunFeedbackView
    {
        private SynchronizationContext _syncContext;
        private IRunFeedbackPresenter _runPresenter;
        private IDirectoryWatcher _watcher;
        private IInformationForm _informationForm;
        private IRunResultCache _runResultCache;

        private ToolTip _toolTipProvider = new ToolTip();
        private bool _isRefreshingFeedback = false;

        private int _rightSpacing = 0;
        private int _listBottomSpacing = 0;
        private int _infoBottomSpacing = 0;

        public FeedbackForm(IDirectoryWatcher watcher, IConfiguration configuration, IRunFeedbackPresenter runPresenter, IInformationForm informationForm, IRunResultCache runResultCache)
        {
            _syncContext = AsyncOperationManager.SynchronizationContext;
            _toolTipProvider.AutoPopDelay = 30000;
            _watcher = watcher;
            _runResultCache = runResultCache;
            _runPresenter = runPresenter;
            _runPresenter.View = this;
            _informationForm = informationForm;
            _informationForm.MessageArrived += new EventHandler<MessageRecievedEventArgs>(_informationForm_MessageArrived);
            InitializeComponent();
            configuration.ValidateSettings();
            _watcher.Watch(configuration.DirectoryToWatch);
            readFormSpacing();
			FeedbackForm_Resize(this, new EventArgs());
        }

        private void readFormSpacing()
        {
            _rightSpacing = Width - (runFeedbackList.Left + runFeedbackList.Width);
            _listBottomSpacing = linkLabelInfo.Top - (runFeedbackList.Top + runFeedbackList.Height);
            _infoBottomSpacing = Height - (linkLabelInfo.Top + linkLabelInfo.Height);
        }

        void _informationForm_MessageArrived(object sender, MessageRecievedEventArgs e)
        {
            buttonInformation.Text = "|||";
            if (e.Type.Equals(MessageType.Warning) && buttonInformation.ForeColor != Color.Red)
                buttonInformation.ForeColor = Color.Yellow;
            else if (e.Type.Equals(MessageType.Error))
                buttonInformation.ForeColor = Color.Red;
        }

        #region IOverviewForm Members

        public Form Form
        {
            get { return this; }
        }

        #endregion
    
        #region IRunFeedbackView Members

        public void  RecievingRunStartedMessage(RunStartedMessage message)
        {
            _syncContext.Post(s =>
                                  {
                                      setRunInProgressFeedback("");
                                      generateSummary(null);
                                  }, null);
        }

        private void setRunInProgressFeedback(string additionalInfo)
        {
            var text = "Detected changes, running..";
            if (additionalInfo.Length > 0)
                text += string.Format(" ({0})", additionalInfo);
            labelRunState.Text = text;
            labelRunState.ForeColor = Color.Black;
        }

        public void  RecievingRunFinishedMessage(RunFinishedMessage message)
        {
            _syncContext.Post(
                x =>
                {
                    var report = (RunReport) x;
                    labelRunState.Text = string.Format(
                        "Ran {0} build(s) ({1} succeeded, {2} failed) and {3} test(s) ({4} passed, {5} failed, {6} ignored)",
                        report.NumberOfProjectsBuilt,
                        report.NumberOfBuildsSucceeded,
                        report.NumberOfBuildsFailed,
                        report.NumberOfTestsRan,
                        report.NumberOfTestsPassed,
                        report.NumberOfTestsFailed,
                        report.NumberOfTestsIgnored);
                    labelRunState.ForeColor =
                        report.NumberOfBuildsFailed > 0 ||
                        report.NumberOfTestsFailed > 0 ||
                        report.NumberOfTestsIgnored > 0
                            ? Color.Red
                            : Color.Green;
                    generateSummary(report);
                },
                message.Report);
        }

        private void generateSummary(RunReport report)
        {
            if (report == null)
            {
                _toolTipProvider.RemoveAll();
                return;
            }

            var builder = new SummaryBuilder(report);
            _toolTipProvider.SetToolTip(labelRunState, builder.Build());
        }

        public void  RecievingBuildMessage(BuildRunMessage message)
        {
            _syncContext.Post(m => relistFromCache(), null);
        }

        public void  RecievingTestRunMessage(TestRunMessage message)
        {
            _syncContext.Post(m => relistFromCache(), null);
        }

        public void RecievingRunInformationMessage(RunInformationMessage message)
        {
            var text = "";
            switch (message.Type)
            {
                case InformationType.Build:
                    text = string.Format("building {0}", Path.GetFileName(message.Project));
                    break;
                case InformationType.TestRun:
                    text = string.Format("testing {0}", Path.GetFileName(message.Assembly));
                    break;
            }
            _syncContext.Post(m => setRunInProgressFeedback(m.ToString()), text);
        }

        #endregion

        private void relistFromCache()
        {
            _isRefreshingFeedback = true;

            IItem selected = null;
            if (runFeedbackList.SelectedItems.Count == 1)
                selected = (IItem) runFeedbackList.SelectedItems[0].Tag;

            runFeedbackList.Items.Clear();
            foreach (var error in _runResultCache.Errors)
                addFeedbackItem("Build error", formatBuildResult(error), Color.Red, error, selected);

            foreach (var failed in _runResultCache.Failed)
                addFeedbackItem("Test failed", formatTestResult(failed), Color.Red, failed, selected);

            foreach (var warning in _runResultCache.Warnings)
                addFeedbackItem("Build warning", formatBuildResult(warning), Color.Black, warning, selected);
            
            foreach (var ignored in _runResultCache.Ignored)
                addFeedbackItem("Test ignored", formatTestResult(ignored), Color.Black, ignored, selected);

            if (runFeedbackList.Items.Count != 1)
                setInfoText("");

            _isRefreshingFeedback = false;
        }

        private void addFeedbackItem(string type, string message, Color colour, IItem tag, IItem selected)
        {
            var item = runFeedbackList.Items.Add(type);
            item.SubItems.Add(message);
            item.ForeColor = colour;
            item.Tag = tag;
            if (selected != null && tag.Equals(selected))
                item.Selected = true;
        }

        private string formatBuildResult(BuildItem item)
        {
            var buildMessage = item.Value;
            return string.Format("{0}, {1}", buildMessage.ErrorMessage, buildMessage.File);
        }

        private string formatTestResult(TestItem item)
        {
            var test = item.Value;
            return string.Format("{0} -> {1}", test.Status, test.Name);
        }

        private void runFeedbackList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_isRefreshingFeedback)
                return;

            if (runFeedbackList.SelectedItems.Count != 1)
            {
                setInfoText("");
                return;
            }

            setInfoText(runFeedbackList.Items[runFeedbackList.SelectedItems[0].Index].Tag.ToString());
        }

        private void setInfoText(string text)
        {
            int previousHeight = linkLabelInfo.Height;
            var parser = new LinkParser(text);
            var links = parser.Parse();
            linkLabelInfo.Text = parser.ParsedText;
            linkLabelInfo.LinkArea = new LinkArea(0, 0);
            foreach (var link in links)
                linkLabelInfo.Links.Add(link.Start, link.Length);
            var difference = linkLabelInfo.Height - previousHeight;
            Height = Height + difference;
        }

        private void FeedbackForm_Resize(object sender, EventArgs e)
        {
			labelRunState.Width = Width - ((Width - buttonInformation.Left) + _rightSpacing);
            linkLabelInfo.MaximumSize = new Size(Width - (linkLabelInfo.Left + _rightSpacing), 0);
			// This is truely horrendous but it does the job for now
			if (Environment.OSVersion.Platform.Equals(PlatformID.Unix))
			{
				linkLabelInfo.Top = Height - (linkLabelInfo.Height + _infoBottomSpacing + 100);
				runFeedbackList.Height = linkLabelInfo.Top - (runFeedbackList.Top + _listBottomSpacing);
            	runFeedbackList.Width = Width - (runFeedbackList.Left + _rightSpacing + 10);
			}
			else
			{
				linkLabelInfo.Top = Height - (linkLabelInfo.Height + _infoBottomSpacing);
            	runFeedbackList.Height = linkLabelInfo.Top - (runFeedbackList.Top + _listBottomSpacing);
            	runFeedbackList.Width = Width - (runFeedbackList.Left + _rightSpacing);
			}
        }

        private void linkLabelInfo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (runFeedbackList.SelectedItems.Count != 1)
                return;
            var item = (IItem) runFeedbackList.SelectedItems[0].Tag;
            item.HandleLink(linkLabelInfo.Text.Substring(e.Link.Start, e.Link.Length));
        }

        private void buttonInformation_Click(object sender, EventArgs e)
        {
            buttonInformation.Text = "";
            buttonInformation.ForeColor = BackColor;
            var form = _informationForm.Form;
            if (form.Visible == false)
                form.Visible = true;
            else
                form.Show(this);
        }

        private void FeedbackForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _informationForm.Form.Dispose();
        }
    }
}

{'fraction_non_alphanumeric': 0.08252629627913442, 'fraction_numerical': 0.003799638570965201, 'mean_word_length': 1.9355277475516866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7375540', 'n_tokens_mistral': 5636, 'n_tokens_neox': 5352, 'n_words': 1113}