Exemple #1
0
        public CodeEditor()
        {
            _codeAnalysisRunner = new JobRunner(1);

            _shell = IoC.Get <IShell>();

            _snippetManager = IoC.Get <SnippetManager>();

            _lineNumberMargin = new LineNumberMargin(this);

            _breakpointMargin = new BreakPointMargin(this, IoC.Get <IDebugManager2>().Breakpoints);

            _selectedLineBackgroundRenderer = new SelectedLineBackgroundRenderer(this);

            _selectedWordBackgroundRenderer = new SelectedWordBackgroundRenderer();

            _columnLimitBackgroundRenderer = new ColumnLimitBackgroundRenderer();

            _selectedDebugLineBackgroundRenderer = new SelectedDebugLineBackgroundRenderer();

            TextArea.TextView.Margin = new Thickness(10, 0, 0, 0);

            TextArea.TextView.BackgroundRenderers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.LineTransformers.Add(_selectedDebugLineBackgroundRenderer);

            TextArea.SelectionBrush        = Brush.Parse("#AA569CD6");
            TextArea.SelectionCornerRadius = 0;

            this.GetObservable(LineNumbersVisibleProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Add(_lineNumberMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_lineNumberMargin);
                }
            });

            this.GetObservable(ShowBreakpointsProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Insert(0, _breakpointMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_breakpointMargin);
                }
            });

            this.GetObservable(HighlightSelectedWordProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_selectedWordBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedWordBackgroundRenderer);
                }
            });

            this.GetObservable(HighlightSelectedLineProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Insert(0, _selectedLineBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedLineBackgroundRenderer);
                }
            });

            this.GetObservable(ShowColumnLimitProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_columnLimitBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_columnLimitBackgroundRenderer);
                }
            });

            Options = new AvaloniaEdit.TextEditorOptions
            {
                ConvertTabsToSpaces = true,
                IndentationSize     = 4
            };

            BackgroundRenderersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.OldValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.NewValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Add(renderer);
                        }
                    }
                }
            });

            DocumentLineTransformersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.OldValue)
                        {
                            TextArea.TextView.LineTransformers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.NewValue)
                        {
                            TextArea.TextView.LineTransformers.Add(renderer);
                        }
                    }
                }
            });

            this.GetObservable(CaretOffsetProperty).Subscribe(s =>
            {
                if (Document?.TextLength > s)
                {
                    CaretOffset = s;
                }
            });

            /*_analysisTriggerEvents.Select(_ => Observable.Timer(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance)
             * .SelectMany(o => DoCodeAnalysisAsync())).Switch().Subscribe(_ => { });*/

            _analysisTriggerEvents.Throttle(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(async _ =>
            {
                await DoCodeAnalysisAsync();
            });

            this.GetObservableWithHistory(SourceFileProperty).Subscribe((file) =>
            {
                if (file.Item1 != file.Item2)
                {
                    if (System.IO.File.Exists(file.Item2.Location))
                    {
                        using (var fs = System.IO.File.OpenText(file.Item2.Location))
                        {
                            Document = new TextDocument(fs.ReadToEnd())
                            {
                                FileName = file.Item2.Location
                            };
                        }
                    }

                    _isLoaded = true;

                    RegisterLanguageService(file.Item2);

                    TextArea.TextView.Redraw();
                }
            });

            TextArea.TextEntering += (sender, e) =>
            {
                _textEntering = true;
            };

            TextArea.Caret.PositionChanged += (sender, e) =>
            {
                if (_intellisenseManager != null && !_textEntering)
                {
                    if (TextArea.Selection.IsEmpty)
                    {
                        var location = Document.GetLocation(CaretOffset);
                        _intellisenseManager.SetCursor(CaretOffset, location.Line, location.Column, UnsavedFiles.ToList());
                    }
                    else
                    {
                        var offset = Document.GetOffset(TextArea.Selection.StartPosition.Location);
                        _intellisenseManager.SetCursor(offset, TextArea.Selection.StartPosition.Line, TextArea.Selection.StartPosition.Column, UnsavedFiles.ToList());
                    }
                }

                if (CaretOffset > 0)
                {
                    var prevLocation = new TextViewPosition(Document.GetLocation(CaretOffset - 1));

                    var visualLocation    = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineBottom);
                    var visualLocationTop = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineTop);

                    var position = visualLocation - TextArea.TextView.ScrollOffset;
                    position = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                    _intellisenseControl.SetLocation(position);
                    _completionAssistantControl.SetLocation(position);

                    _selectedWordBackgroundRenderer.SelectedWord = GetWordAtOffset(CaretOffset);

                    Line              = TextArea.Caret.Line;
                    Column            = TextArea.Caret.Column;
                    EditorCaretOffset = TextArea.Caret.Offset;
                }
            };

            TextArea.TextEntered += (sender, e) =>
            {
                _intellisenseManager?.OnTextInput(e, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                _textEntering = false;
            };

            _intellisense = new IntellisenseViewModel();

            _completionAssistant = new CompletionAssistantViewModel(_intellisense);

            EventHandler <KeyEventArgs> tunneledKeyUpHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyUp(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            EventHandler <KeyEventArgs> tunneledKeyDownHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyDown(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);

                    if (ee.Key == Key.Tab && _currentSnippetContext == null && LanguageService != null)
                    {
                        var wordStart = Document.FindPrevWordStart(CaretOffset);

                        if (wordStart > 0)
                        {
                            string word = Document.GetText(wordStart, CaretOffset - wordStart);

                            var codeSnippet = _snippetManager.GetSnippet(LanguageService, SourceFile.Project?.Solution, SourceFile.Project, word);

                            if (codeSnippet != null)
                            {
                                var snippet = SnippetParser.Parse(LanguageService, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column, codeSnippet.Snippet);

                                _intellisenseManager.CloseIntellisense();

                                using (Document.RunUpdate())
                                {
                                    Document.Remove(wordStart, CaretOffset - wordStart);

                                    _intellisenseManager.IncludeSnippets = false;
                                    _currentSnippetContext = snippet.Insert(TextArea);
                                }

                                if (_currentSnippetContext.ActiveElements.Count() > 0)
                                {
                                    IDisposable disposable = null;

                                    disposable = Observable.FromEventPattern <SnippetEventArgs>(_currentSnippetContext, nameof(_currentSnippetContext.Deactivated)).Take(1).Subscribe(o =>
                                    {
                                        _currentSnippetContext = null;
                                        _intellisenseManager.IncludeSnippets = true;

                                        disposable.Dispose();
                                    });
                                }
                                else
                                {
                                    _currentSnippetContext = null;
                                    _intellisenseManager.IncludeSnippets = true;
                                }
                            }
                        }
                    }
                }
            };

            EventHandler <TextInputEventArgs> tunneledTextInputHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnTextInput(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            AddHandler(KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel);
            AddHandler(KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel);
        }
        public CodeEditor() : base(new TextArea(), null)
        {
            TextArea.IndentationStrategy = null;

            _shell = IoC.Get <IShell>();

            _snippetManager = IoC.Get <SnippetManager>();

            _lineNumberMargin = new LineNumberMargin(this);

            _breakpointMargin = new BreakPointMargin(this, IoC.Get <IDebugManager2>()?.Breakpoints);

            _selectedLineBackgroundRenderer    = new SelectedLineBackgroundRenderer(this);
            _bracketMatchingBackgroundRenderer = new BracketMatchingBackgroundRenderer(this);

            _selectedWordBackgroundRenderer = new SelectedWordBackgroundRenderer();

            _columnLimitBackgroundRenderer = new ColumnLimitBackgroundRenderer();

            _selectedDebugLineBackgroundRenderer = new SelectedDebugLineBackgroundRenderer();

            TextArea.TextView.Margin = new Thickness(10, 0, 0, 0);

            TextArea.TextView.BackgroundRenderers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.LineTransformers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.BackgroundRenderers.Add(_bracketMatchingBackgroundRenderer);

            TextArea.SelectionBrush        = Brush.Parse("#AA569CD6");
            TextArea.SelectionCornerRadius = 0;

            void tunneledKeyUpHandler(object send, KeyEventArgs ee)
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyUp(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            }

            void tunneledKeyDownHandler(object send, KeyEventArgs ee)
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyDown(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);

                    if (ee.Key == Key.Tab && _currentSnippetContext == null && Editor is ICodeEditor codeEditor && codeEditor.LanguageService != null)
                    {
                        var wordStart = Document.FindPrevWordStart(CaretOffset);

                        if (wordStart > 0)
                        {
                            string word = Document.GetText(wordStart, CaretOffset - wordStart);

                            var codeSnippet = _snippetManager.GetSnippet(codeEditor.LanguageService, Editor.SourceFile.Project?.Solution, Editor.SourceFile.Project, word);

                            if (codeSnippet != null)
                            {
                                var snippet = SnippetParser.Parse(codeEditor.LanguageService, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column, codeSnippet.Snippet);

                                _intellisenseManager.CloseIntellisense();

                                using (Document.RunUpdate())
                                {
                                    Document.Remove(wordStart, CaretOffset - wordStart);

                                    _intellisenseManager.IncludeSnippets = false;
                                    _currentSnippetContext = snippet.Insert(TextArea);
                                }

                                if (_currentSnippetContext.ActiveElements.Count() > 0)
                                {
                                    IDisposable disposable = null;

                                    disposable = Observable.FromEventPattern <SnippetEventArgs>(_currentSnippetContext, nameof(_currentSnippetContext.Deactivated)).Take(1).Subscribe(o =>
                                    {
                                        _currentSnippetContext = null;
                                        _intellisenseManager.IncludeSnippets = true;

                                        disposable.Dispose();
                                    });
                                }
                                else
                                {
                                    _currentSnippetContext = null;
                                    _intellisenseManager.IncludeSnippets = true;
                                }
                            }
                        }
                    }
                }
            }

            _disposables = new CompositeDisposable {
                this.GetObservable(LineNumbersVisibleProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.LeftMargins.Add(_lineNumberMargin);
                    }
                    else
                    {
                        TextArea.LeftMargins.Remove(_lineNumberMargin);
                    }
                }),

                this.GetObservable(ShowBreakpointsProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.LeftMargins.Insert(0, _breakpointMargin);
                    }
                    else
                    {
                        TextArea.LeftMargins.Remove(_breakpointMargin);
                    }
                }),

                this.GetObservable(HighlightSelectedWordProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Add(_selectedWordBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_selectedWordBackgroundRenderer);
                    }
                }),

                this.GetObservable(HighlightSelectedLineProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Insert(0, _selectedLineBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_selectedLineBackgroundRenderer);
                    }
                }),

                this.GetObservable(ShowColumnLimitProperty).Subscribe(s =>
                {
                    if (s)
                    {
                        TextArea.TextView.BackgroundRenderers.Add(_columnLimitBackgroundRenderer);
                    }
                    else
                    {
                        TextArea.TextView.BackgroundRenderers.Remove(_columnLimitBackgroundRenderer);
                    }
                }),

                this.GetObservable(ColumnLimitProperty).Subscribe(limit =>
                {
                    _columnLimitBackgroundRenderer.Column = limit;
                    this.TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                }),

                this.GetObservable(ContextActionsIconProperty).Subscribe(icon =>
                {
                    if (_contextActionsRenderer != null)
                    {
                        _contextActionsRenderer.IconImage = icon;
                    }
                }),

                this.GetObservable(ColorSchemeProperty).Subscribe(colorScheme =>
                {
                    if (colorScheme != null)
                    {
                        Background = colorScheme.Background;
                        Foreground = colorScheme.Text;

                        _lineNumberMargin.Background = colorScheme.Background;

                        if (_diagnosticMarkersRenderer != null)
                        {
                            _diagnosticMarkersRenderer.ColorScheme = colorScheme;
                        }

                        _textColorizer?.RecalculateBrushes();
                        TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                        TextArea.TextView.Redraw();
                    }
                }),

                this.GetObservable(CaretOffsetProperty).Subscribe(s =>
                {
                    if (Document?.TextLength > s)
                    {
                        CaretOffset = s;
                        TextArea.Caret.BringCaretToView();
                    }
                }),

                BackgroundRenderersProperty.Changed.Subscribe(s =>
                {
                    if (s.Sender == this)
                    {
                        if (s.OldValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.OldValue)
                            {
                                TextArea.TextView.BackgroundRenderers.Remove(renderer);
                            }
                        }

                        if (s.NewValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.NewValue)
                            {
                                TextArea.TextView.BackgroundRenderers.Add(renderer);
                            }
                        }
                    }
                }),

                DocumentLineTransformersProperty.Changed.Subscribe(s =>
                {
                    if (s.Sender == this)
                    {
                        if (s.OldValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.OldValue)
                            {
                                TextArea.TextView.LineTransformers.Remove(renderer);
                            }
                        }

                        if (s.NewValue != null)
                        {
                            foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.NewValue)
                            {
                                TextArea.TextView.LineTransformers.Add(renderer);
                            }
                        }
                    }
                }),

                Observable.FromEventPattern(TextArea.Caret, nameof(TextArea.Caret.PositionChanged)).Subscribe(e =>
                {
                    if (_isLoaded && Document != null)
                    {
                        _lastLine = TextArea.Caret.Line;

                        EditorCaretOffset = TextArea.Caret.Offset;
                        Line   = TextArea.Caret.Line;
                        Column = TextArea.Caret.Column;

                        var location = new TextViewPosition(Document.GetLocation(CaretOffset));

                        var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                        var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                        var position = visualLocation - TextArea.TextView.ScrollOffset;
                        position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                        _intellisenseControl.SetLocation(position);
                    }
                }),

                Observable.FromEventPattern(TextArea.Caret, nameof(TextArea.Caret.PositionChanged)).Throttle(TimeSpan.FromMilliseconds(100)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(e =>
                {
                    if (Document != null)
                    {
                        var location = new TextViewPosition(Document.GetLocation(CaretOffset));

                        if (_intellisenseManager != null && !_textEntering)
                        {
                            if (TextArea.Selection.IsEmpty)
                            {
                                _intellisenseManager.SetCursor(CaretOffset, location.Line, location.Column, UnsavedFiles.ToList());
                            }
                            else if (_currentSnippetContext != null)
                            {
                                var offset = Document.GetOffset(TextArea.Selection.StartPosition.Location);
                                _intellisenseManager.SetCursor(offset, TextArea.Selection.StartPosition.Line, TextArea.Selection.StartPosition.Column, UnsavedFiles.ToList());
                            }
                        }

                        _selectedWordBackgroundRenderer.SelectedWord = GetWordAtOffset(CaretOffset);

                        TextArea.TextView.InvalidateLayer(KnownLayer.Background);
                    }
                }),

                this.WhenAnyValue(x => x.DebugHighlight).Where(loc => loc != null).Subscribe(location =>
                {
                    if (location.Line != -1)
                    {
                        SetDebugHighlight(location.Line, location.StartColumn, location.EndColumn);
                    }
                    else
                    {
                        ClearDebugHighlight();
                    }
                }),
                this.GetObservable(EditorProperty).Subscribe(editor =>
                {
                    if (editor != null)
                    {
                        if (editor.SourceFile.Project?.Solution != null)
                        {
                            _snippetManager.InitialiseSnippetsForSolution(editor.SourceFile.Project.Solution);
                        }

                        if (editor.SourceFile.Project != null)
                        {
                            _snippetManager.InitialiseSnippetsForProject(editor.SourceFile.Project);
                        }

                        SyntaxHighlighting = CustomHighlightingManager.Instance.GetDefinition(editor.SourceFile.ContentType);

                        if (editor.Document is AvalonStudioTextDocument td && Document != td.Document)
                        {
                            Document = td.Document;

                            if (editor.Offset <= Document.TextLength)
                            {
                                CaretOffset = editor.Offset;
                            }

                            _textColorizer = new TextColoringTransformer(Document);
                            _scopeLineBackgroundRenderer = new ScopeLineBackgroundRenderer(Document);


                            TextArea.TextView.BackgroundRenderers.Add(_scopeLineBackgroundRenderer);
                            TextArea.TextView.LineTransformers.Insert(0, _textColorizer);

                            _diagnosticMarkersRenderer = new TextMarkerService(Document);
                            _contextActionsRenderer    = new ContextActionsRenderer(this, _diagnosticMarkersRenderer);
                            TextArea.LeftMargins.Add(_contextActionsRenderer);
                            TextArea.TextView.BackgroundRenderers.Add(_diagnosticMarkersRenderer);
                        }

                        if (editor is ICodeEditor codeEditor)
                        {
                            if (codeEditor.Highlights != null)
                            {
                                _disposables.Add(
                                    Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(codeEditor.Highlights, nameof(codeEditor.Highlights.CollectionChanged))
                                    .Subscribe(observer =>
                                {
                                    var e = observer.EventArgs;

                                    switch (e.Action)
                                    {
                                    case NotifyCollectionChangedAction.Add:
                                        foreach (var(tag, highlightList) in  e.NewItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.SetTransformations(tag, highlightList);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Remove:
                                        foreach (var(tag, highlightList) in  e.OldItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.RemoveAll(i => i.Tag == tag);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Reset:
                                        foreach (var(tag, highlightList) in  e.OldItems.Cast <(object tag, SyntaxHighlightDataList highlightList)>())
                                        {
                                            _textColorizer.RemoveAll(i => true);
                                        }
                                        break;

                                    default:
                                        throw new NotSupportedException();
                                    }

                                    TextArea.TextView.Redraw();
                                }));

                                _disposables.Add(
                                    Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(codeEditor.Diagnostics, nameof(codeEditor.Diagnostics.CollectionChanged))
                                    .Subscribe(observer =>
                                {
                                    var e = observer.EventArgs;

                                    switch (e.Action)
                                    {
                                    case NotifyCollectionChangedAction.Add:
                                        foreach (var(tag, diagnostics) in  e.NewItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.SetDiagnostics(tag, diagnostics);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Remove:
                                        foreach (var(tag, diagnostics) in  e.OldItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.RemoveAll(x => x.Tag == tag);
                                        }
                                        break;

                                    case NotifyCollectionChangedAction.Reset:
                                        foreach (var(tag, diagnostics) in  e.OldItems.Cast <(object tag, IEnumerable <Diagnostic> diagnostics)>())
                                        {
                                            _diagnosticMarkersRenderer.RemoveAll(i => true);
                                        }
                                        break;

                                    default:
                                        throw new NotSupportedException();
                                    }

                                    TextArea.TextView.Redraw();
                                    _contextActionsRenderer.OnDiagnosticsUpdated();
                                }));

                                _disposables.Add(codeEditor.WhenAnyValue(x => x.CodeIndex).Subscribe(codeIndex =>
                                {
                                    _scopeLineBackgroundRenderer.ApplyIndex(codeIndex);
                                }));

                                _scopeLineBackgroundRenderer.ApplyIndex(codeEditor.CodeIndex);

                                foreach (var(tag, diagnostics) in codeEditor.Diagnostics)
                                {
                                    _diagnosticMarkersRenderer.SetDiagnostics(tag, diagnostics);
                                }

                                foreach (var(tag, highlights) in codeEditor.Highlights)
                                {
                                    _textColorizer.SetTransformations(tag, highlights);
                                }

                                TextArea.TextView.Redraw();
                            }

                            _intellisenseManager = new IntellisenseManager(editor, Intellisense, _completionAssistant, codeEditor.LanguageService, editor.SourceFile, offset =>
                            {
                                var location = new TextViewPosition(Document.GetLocation(offset));

                                var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                                var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                                var position = visualLocation - TextArea.TextView.ScrollOffset;
                                position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                                _completionAssistantControl.SetLocation(position);
                            });

                            _disposables.Add(_intellisenseManager);

                            foreach (var contextActionProvider in codeEditor.LanguageService.GetContextActionProviders())
                            {
                                _contextActionsRenderer.Providers.Add(contextActionProvider);
                            }
                        }

                        Dispatcher.UIThread.Post(() =>
                        {
                            TextArea.ScrollToLine(Line);
                            Focus();
                        });
                    }
                    else
                    {
                        if (Document != null)
                        {
                            Document = null;
                        }
                    }
                }),
                this.GetObservable(RenameOpenProperty).Subscribe(open =>
                {
                    if (_isLoaded && Editor != null)
                    {
                        var token    = Editor.Document.GetToken(CaretOffset);
                        var location = new TextViewPosition(Document.GetLocation(token.Offset));

                        var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                        var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                        var position = visualLocation - TextArea.TextView.ScrollOffset;
                        position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                        _renameControl.SetLocation(position);
                        _renameControl.Open(this, Editor.Document.GetText(token));
                    }
                }),

                AddHandler(KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel),
                AddHandler(KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel)
            };

            Options = new AvaloniaEdit.TextEditorOptions
            {
                ConvertTabsToSpaces   = true,
                IndentationSize       = 4,
                EnableHyperlinks      = false,
                EnableEmailHyperlinks = false,
            };

            //BackgroundRenderersProperty.Changed.Subscribe(s =>
            //{
            //    if (s.Sender == this)
            //    {
            //        if (s.OldValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IBackgroundRenderer>)s.OldValue)
            //            {
            //                TextArea.TextView.BackgroundRenderers.Remove(renderer);
            //            }
            //        }

            //        if (s.NewValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IBackgroundRenderer>)s.NewValue)
            //            {
            //                TextArea.TextView.BackgroundRenderers.Add(renderer);
            //            }
            //        }
            //    }
            //});

            //DocumentLineTransformersProperty.Changed.Subscribe(s =>
            //{
            //    if (s.Sender == this)
            //    {
            //        if (s.OldValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IVisualLineTransformer>)s.OldValue)
            //            {
            //                TextArea.TextView.LineTransformers.Remove(renderer);
            //            }
            //        }

            //        if (s.NewValue != null)
            //        {
            //            foreach (var renderer in (ObservableCollection<IVisualLineTransformer>)s.NewValue)
            //            {
            //                TextArea.TextView.LineTransformers.Add(renderer);
            //            }
            //        }
            //    }
            //});


            /*_analysisTriggerEvents.Select(_ => Observable.Timer(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance)
             * .SelectMany(o => DoCodeAnalysisAsync())).Switch().Subscribe(_ => { });*/

            Intellisense = new IntellisenseViewModel();

            _completionAssistant = new CompletionAssistantViewModel(Intellisense);

            TextArea.TextEntering += TextArea_TextEntering;

            TextArea.TextEntered += TextArea_TextEntered;
        }

        ~CodeEditor()
        {
        }