Ejemplo n.º 1
0
        public EditorState(IWin32Window window, string name, object tag, int tabIndex)
        {
            FilePath     = null;
            Name         = name;
            Tag          = tag;
            IsChanged    = false;
            FileEncoding = Encoding.UTF8;

            _styleNeededState = new StyleNeededState();

            _window  = window;
            TabIndex = tabIndex;

            _editor = new Scintilla();
            SetupEditor(_editor);

            _containerPanel      = new Panel();
            _containerPanel.Dock = DockStyle.Fill;
            _containerPanel.Controls.Add(_editor);

            _searchControl      = new SearchReplaceControl();
            _searchControl.Dock = DockStyle.Top;
            _containerPanel.Controls.Add(_searchControl);

            _searchControl.Search += (s, direction) =>
            {
                SearchText(_window, _searchControl.SearchText, direction, _searchControl.MatchCase, _searchControl.MatchWords, _searchControl.IsRegex, _searchControl.IsWrap);
            };
            _searchControl.Replace += (s, mode) =>
            {
                ReplaceText(_window, _searchControl.SearchText, _searchControl.ReplaceText, mode, _searchControl.MatchCase, _searchControl.MatchWords, _searchControl.IsRegex, _searchControl.IsWrap);
            };
            _searchControl.FocusChanged += (s, focused) =>
            {
                FocusChanged?.Invoke(this, focused);
            };
            _searchControl.Hide();

            _maxLineNumberCharLength = 0;
            _textChangedTimer        = new System.Windows.Forms.Timer()
            {
                Enabled = false, Interval = 250
            };
            _textChangedTimer.Tick += (s, e) =>
            {
                if (!_parseWorker.IsBusy)
                {
                    _textChangedTimer.Enabled = false;
                    ParseStarting?.Invoke(this);
                    _parseWorker.RunWorkerAsync(_editor.Text);
                }
            };
            _parseWorker = new BackgroundWorker();
            _parseWorker.WorkerSupportsCancellation = true;
            _parseWorker.DoWork += (s, e) =>
            {
                // @TODO(final): Support for incremental parsing, so only changes are applied
                string text = (string)e.Argument;
                Tokenize(text);
                Parse(text);
            };
            _parseWorker.RunWorkerCompleted += (s, e) =>
            {
                // @TODO(final): Dont colorize everything, just re-colorize the changes -> See "Support for continuous tokenization"
                if (_styleNeededState.Has > 0)
                {
                    _editor.Colorize(_styleNeededState.StartPos, _styleNeededState.EndPos);
                }
                else
                {
                    int firstLine = _editor.FirstVisibleLine;
                    int lastLine  = firstLine + _editor.LinesOnScreen;
                    int start     = _editor.Lines[firstLine].Index;
                    int end       = _editor.Lines[lastLine].Index;
                    _editor.Colorize(start, end);
                }
                ParseComplete?.Invoke(this);
            };
        }