<commit_before>﻿using Castle.MicroKernel;
using Castle.Windsor;
using Castle.Windsor.Installer;
using LinqEditor.Core.Backend.Models;
using LinqEditor.Core.Backend.Repository;
using LinqEditor.Core.CodeAnalysis.Editor;
using LinqEditor.Core.CodeAnalysis.Models;
using LinqEditor.UI.WinForm.Controls;
using ScintillaNET;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LinqEditor.UI.WinForm
{
    public class Main : Form
    {
        public static string TestConnectionString = "Data Source=.\\sqlexpress;Integrated Security=True;Initial Catalog=Opera18500DB";

        ToolStrip _toolbar;
        SplitContainer _mainContainer;
        ToolStripButton _executeButton;
        StatusStrip _statusBar;
        ToolStripStatusLabel _statusLabel;
        ToolStripStatusLabel _rowCountLabel;
        TextBox _connectionTextBox;
        CodeEditor _editor;
        OutputPane _outputPane;

        IBackgroundSession _connectionSession;
        IBackgroundCompletion _completionHelper;
        IWindsorContainer _container;

        private void InitializeWindsor()
        {
            _container = new WindsorContainer();
            _container.Install(FromAssembly.This());

            _connectionSession = _container.Resolve<IBackgroundSession>(new Arguments(new { connectionString = _connectionTextBox.Text }));
            _completionHelper = _container.Resolve<IBackgroundCompletion>();
        }

        public Main()
        {
            _statusBar = new StatusStrip();
            _statusBar.SuspendLayout();
            SuspendLayout();
            var minHeight = 300;
            var minWidth = 400;

            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            Text = "Linq Editor";
            Width = 800;
            Height = 500;
            MinimumSize = new Size(minWidth, minHeight);

            // loads schema etc in async handlers
            Load += Main_Load;
            FormClosed += Main_FormClosed;

            
            _mainContainer = new SplitContainer();
            _mainContainer.Location = new Point(0, 0);
            _mainContainer.Orientation = Orientation.Horizontal;
            _mainContainer.Dock = DockStyle.Fill;
            _mainContainer.TabStop = false;
            _mainContainer.FixedPanel = FixedPanel.None;
            // must set this for panel minsizes to work. subtract height for statusbar+toolbar
            _mainContainer.MinimumSize = new Size(minWidth - 10, minHeight- 80);
            // fudged values
            _mainContainer.SplitterDistance = minHeight / 10;
            _mainContainer.Panel1MinSize = minHeight / 4;
            _mainContainer.Panel2MinSize = minHeight / 3;
            _mainContainer.SizeChanged += _mainContainer_SizeChanged;

            // status
            _statusBar.Dock = DockStyle.Bottom;
            _statusBar.GripStyle = ToolStripGripStyle.Hidden;
            _statusBar.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
            _statusLabel = new ToolStripStatusLabel();
            _statusLabel.Alignment = ToolStripItemAlignment.Left;
            _statusLabel.Text = "Loading";
            _rowCountLabel = new ToolStripStatusLabel();
            _rowCountLabel.Alignment = ToolStripItemAlignment.Right;
            _statusBar.Items.AddRange(new[] { _statusLabel, _rowCountLabel });
            
            // connection
            _connectionTextBox = new TextBox();
            _connectionTextBox.Dock = DockStyle.Top;
            _connectionTextBox.ReadOnly = true;
            _connectionTextBox.Text = TestConnectionString;

            // scintilla
            _editor = new CodeEditor();
            _editor.Dock = DockStyle.Fill;

            // output thingy
            _outputPane = new OutputPane(_rowCountLabel);
            _outputPane.Dock = DockStyle.Fill;

            // toolbar
            _toolbar = new ToolStrip();
            _toolbar.Dock = DockStyle.Top;
            
            // play button
            _executeButton = new ToolStripButton();
            _executeButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
            _executeButton.Image = Resources.Icons.startwithoutdebugging_6556;
            _executeButton.Click += _executeButton_Click;
            _executeButton.Enabled = false;
            _toolbar.Items.Add(_executeButton);

            // add controls

            _mainContainer.Panel1.Controls.Add(_editor);
            _mainContainer.Panel1.Controls.Add(_connectionTextBox);
            _mainContainer.Panel2.Controls.Add(_outputPane);
            Controls.Add(_mainContainer);
            Controls.Add(_toolbar);
            Controls.Add(_statusBar);
            _statusBar.ResumeLayout(false);
            _statusBar.PerformLayout();
            ResumeLayout(false);
            PerformLayout();
            InitializeWindsor();
        }
        
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F5)
            {
                _executeButton.PerformClick();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private async Task<ExecuteResult> Execute()
        {
            return await _connectionSession.ExecuteAsync(_editor.SourceCode);
        }

        void _mainContainer_SizeChanged(object sender, EventArgs e)
        {
            var container = sender as SplitContainer;
            _connectionTextBox.Width = container.ClientSize.Width;
        }

        void Main_FormClosed(object sender, FormClosedEventArgs e)<commit_msg>maintain focus in editor when resizing splitter<commit_after>﻿using Castle.MicroKernel;
using Castle.Windsor;
using Castle.Windsor.Installer;
using LinqEditor.Core.Backend.Models;
using LinqEditor.Core.Backend.Repository;
using LinqEditor.Core.CodeAnalysis.Editor;
using LinqEditor.Core.CodeAnalysis.Models;
using LinqEditor.UI.WinForm.Controls;
using ScintillaNET;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace LinqEditor.UI.WinForm
{
    public class Main : Form
    {
        public static string TestConnectionString = "Data Source=.\\sqlexpress;Integrated Security=True;Initial Catalog=Opera18500DB";

        ToolStrip _toolbar;
        SplitContainer _mainContainer;
        ToolStripButton _executeButton;
        StatusStrip _statusBar;
        ToolStripStatusLabel _statusLabel;
        ToolStripStatusLabel _rowCountLabel;
        TextBox _connectionTextBox;
        CodeEditor _editor;
        OutputPane _outputPane;

        IBackgroundSession _connectionSession;
        IBackgroundCompletion _completionHelper;
        IWindsorContainer _container;

        Stopwatch _editorFocusTimer;
        bool _restoreEditorFocusOnSplitterMoved;

        private void InitializeWindsor()
        {
            _container = new WindsorContainer();
            _container.Install(FromAssembly.This());

            _connectionSession = _container.Resolve<IBackgroundSession>(new Arguments(new { connectionString = _connectionTextBox.Text }));
            _completionHelper = _container.Resolve<IBackgroundCompletion>();
        }

        public Main()
        {
            _statusBar = new StatusStrip();
            _statusBar.SuspendLayout();
            SuspendLayout();
            var minHeight = 300;
            var minWidth = 400;

            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            Text = "Linq Editor";
            Width = 800;
            Height = 500;
            MinimumSize = new Size(minWidth, minHeight);

            // loads schema etc in async handlers
            Load += Main_Load;
            FormClosed += Main_FormClosed;

            _editorFocusTimer = new Stopwatch();
            _mainContainer = new SplitContainer();
            _mainContainer.Location = new Point(0, 0);
            _mainContainer.Orientation = Orientation.Horizontal;
            _mainContainer.Dock = DockStyle.Fill;
            _mainContainer.TabStop = false;
            _mainContainer.FixedPanel = FixedPanel.None;
            // must set this for panel minsizes to work. subtract height for statusbar+toolbar
            _mainContainer.MinimumSize = new Size(minWidth - 10, minHeight- 80);
            // fudged values
            _mainContainer.SplitterDistance = minHeight / 10;
            _mainContainer.Panel1MinSize = minHeight / 4;
            _mainContainer.Panel2MinSize = minHeight / 3;
            _mainContainer.SizeChanged += _mainContainer_SizeChanged;
            _mainContainer.GotFocus +=_mainContainer_GotFocus;
            _mainContainer.SplitterMoved += _mainContainer_SplitterMoved;
            

            // status
            _statusBar.Dock = DockStyle.Bottom;
            _statusBar.GripStyle = ToolStripGripStyle.Hidden;
            _statusBar.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
            _statusLabel = new ToolStripStatusLabel();
            _statusLabel.Alignment = ToolStripItemAlignment.Left;
            _statusLabel.Text = "Loading";
            _rowCountLabel = new ToolStripStatusLabel();
            _rowCountLabel.Alignment = ToolStripItemAlignment.Right;
            _statusBar.Items.AddRange(new[] { _statusLabel, _rowCountLabel });
            
            // connection
            _connectionTextBox = new TextBox();
            _connectionTextBox.Dock = DockStyle.Top;
            _connectionTextBox.ReadOnly = true;
            _connectionTextBox.Text = TestConnectionString;

            // scintilla
            _editor = new CodeEditor();
            _editor.LostFocus += _editor_LostFocus;
            _editor.Dock = DockStyle.Fill;

            // output thingy
            _outputPane = new OutputPane(_rowCountLabel);
            _outputPane.Dock = DockStyle.Fill;

            // toolbar
            _toolbar = new ToolStrip();
            _toolbar.Dock = DockStyle.Top;
            
            // play button
            _executeButton = new ToolStripButton();
            _executeButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
            _executeButton.Image = Resources.Icons.startwithoutdebugging_6556;
            _executeButton.Click += _executeButton_Click;
            _executeButton.Enabled = false;
            _toolbar.Items.Add(_executeButton);

            // add controls

            _mainContainer.Panel1.Controls.Add(_editor);
            _mainContainer.Panel1.Controls.Add(_connectionTextBox);
            _mainContainer.Panel2.Controls.Add(_outputPane);
            Controls.Add(_mainContainer);
            Controls.Add(_toolbar);
            Controls.Add(_statusBar);
            _statusBar.ResumeLayout(false);
            _statusBar.PerformLayout();
            ResumeLayout(false);
            PerformLayout();
            InitializeWindsor();


        }

        // some custom focus juggling when resizing the splitcontainer when the editor had focus
        void _mainContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            if (_restoreEditorFocusOnSplitterMoved)
            {
                _editor.Focus();
            }
        }

        void _editor_LostFocus(object sender, EventArgs e)
        {
            _editorFocusTimer.Restart();
            _restoreEditorFocusOnSplitterMoved = false;
        }

        void _mainContainer_GotFocus(object sender, EventArgs e)
        {
            _editorFocusTimer.Stop();
            _restoreEditorFocusOnSplitterMoved = _editorFocusTimer.ElapsedMilliseconds < 50; // more fudging
        }
                
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F5)
            {
                _executeButton.PerformClick();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private async Task<ExecuteResult> Execute()
        {
            return await _connectionSession.ExecuteAsync(_editor.SourceCode);
        }

        void _mainContainer_SizeChanged(object sender, EventArgs e)
        {
            var container = sender as SplitContainer;
            _connectionTextBox.Width = container.ClientSize.Width;
        }

        void Main_FormClosed(object sender, FormClosedEventArgs e)
{'fraction_non_alphanumeric': 0.07336785938480854, 'fraction_numerical': 0.006434400502197113, 'mean_word_length': 2.184657671164418, 'pattern_counts': {'":': 0, '<': 10, '<?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': '4485515', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3150, 'n_words': 648}