public void DetachEditor()
        {
            if (_textEditor is null)
            {
                return;
            }

            _textEditor.TextArea.TextView.ElementGenerators.Remove(_elementGenerator);

            _textEditor.TextArea.TextView.LineTransformers.Remove(_highlightAllOccurrencesOfSelectedWordTransformer);
            _textEditor.TextArea.TextView.LineTransformers.Remove(_firstLineAlwaysBoldTransformer);

            _textEditor.TextArea.SelectionChanged           -= OnTextAreaSelectionChanged;
            _textEditor.TextArea.Caret.PositionChanged      -= OnCaretPositionChanged;
            _textEditor.TextArea.PreviewMouseLeftButtonDown -= OnPreviewMouseLeftButtonDown;
            _textEditor.TextChanged           -= OnTextChanged;
            _textEditor.PreviewKeyDown        -= OnPreviewKeyDown;
            _textEditor.TextArea.TextEntering -= OnTextEntering;

            var document = _textEditor.Document;

            document.Changed -= OnTextDocumentChanged;

            _textEditor = null;
            _highlightAllOccurrencesOfSelectedWordTransformer = null;

            EditorDetached?.Invoke(this, EventArgs.Empty);
        }
        public void AttachEditor(object editor)
        {
            Argument.IsNotNull(() => editor);

            DetachEditor();

            _textEditor = editor as TextEditor;
            if (_textEditor is null)
            {
                return;
            }

            _csvTextEditorControl = _textEditor.FindLogicalOrVisualAncestorByType <CsvTextEditorControl>();
            if (_csvTextEditorControl is null)
            {
                return;
            }

            // NOTE: Performance tips: https://github.com/icsharpcode/AvalonEdit/issues/11
            // Need to make these options accessible to the user in the settings window
            _textEditor.SetCurrentValue(TextEditor.ShowLineNumbersProperty, true);
            _textEditor.Options.HighlightCurrentLine     = true;
            _textEditor.Options.ShowEndOfLine            = false;
            _textEditor.Options.ShowTabs                 = false;
            _textEditor.Options.ShowSpaces               = false;
            _textEditor.Options.EnableEmailHyperlinks    = false;
            _textEditor.Options.EnableHyperlinks         = false;
            _textEditor.Options.AllowScrollBelowDocument = false;

            _elementGenerator = _typeFactory.CreateInstance <TabSpaceElementGenerator>();

            _textEditor.TextArea.TextView.ElementGenerators.Add(_elementGenerator);

            _textEditor.TextArea.SelectionChanged           += OnTextAreaSelectionChanged;
            _textEditor.TextArea.Caret.PositionChanged      += OnCaretPositionChanged;
            _textEditor.TextArea.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;
            _textEditor.TextChanged    += OnTextChanged;
            _textEditor.PreviewKeyDown += OnPreviewKeyDown;

            _textEditor.TextArea.TextEntering += OnTextEntering;

            _highlightAllOccurrencesOfSelectedWordTransformer = new HighlightAllOccurencesOfSelectedWordTransformer();
            _textEditor.TextArea.TextView.LineTransformers.Add(_highlightAllOccurrencesOfSelectedWordTransformer);

            _firstLineAlwaysBoldTransformer = new FirstLineAlwaysBoldTransformer();
            _textEditor.TextArea.TextView.LineTransformers.Add(_firstLineAlwaysBoldTransformer);

            _invisibleQuotesTransformer = new GrayedQuotesDocumentColorizingTransformer();
            _textEditor.TextArea.TextView.LineTransformers.Add(_invisibleQuotesTransformer);

            _initializer.Initialize(_textEditor, this);

            EditorAttached?.Invoke(this, EventArgs.Empty);
        }
        public CsvTextEditorService(TextEditor textEditor, ICommandManager commandManager, ICsvTextEditorServiceInitializer initializer,
                                    IDispatcherService dispatcherService)
        {
            Argument.IsNotNull(() => textEditor);
            Argument.IsNotNull(() => commandManager);
            Argument.IsNotNull(() => initializer);
            Argument.IsNotNull(() => dispatcherService);

            _textEditor        = textEditor;
            _commandManager    = commandManager;
            _dispatcherService = dispatcherService;

            _tools = new List <ICsvTextEditorTool>();

            // Need to make these options accessible to the user in the settings window
            _textEditor.ShowLineNumbers = true;
            _textEditor.Options.HighlightCurrentLine = true;
            _textEditor.Options.ShowEndOfLine        = true;
            _textEditor.Options.ShowTabs             = true;

            var serviceLocator = this.GetServiceLocator();
            var typeFactory    = serviceLocator.ResolveType <ITypeFactory>();

            _elementGenerator = typeFactory.CreateInstance <TabSpaceElementGenerator>();

            _textEditor.TextArea.TextView.ElementGenerators.Add(_elementGenerator);

            _textEditor.TextArea.SelectionChanged           += OnTextAreaSelectionChanged;
            _textEditor.TextArea.Caret.PositionChanged      += OnCaretPositionChanged;
            _textEditor.TextArea.PreviewMouseLeftButtonDown += OnPreviewMouseLeftButtonDown;
            _textEditor.TextChanged    += OnTextChanged;
            _textEditor.PreviewKeyDown += OnPreviewKeyDown;

            _textEditor.TextArea.TextEntering += OnTextEntering;

            _highlightAllOccurencesOfSelectedWordTransformer = new HighlightAllOccurencesOfSelectedWordTransformer();
            _textEditor.TextArea.TextView.LineTransformers.Add(_highlightAllOccurencesOfSelectedWordTransformer);

            _textEditor.TextArea.TextView.LineTransformers.Add(new FirstLineAlwaysBoldTransformer());

            initializer.Initialize(textEditor, this);
        }