示例#1
0
        public EditorViewModel(EditorModel model) : base(model)
        {
            disposables      = new CompositeDisposable();
            highlightingData = new ObservableCollection <OffsetSyntaxHighlightingData>();

            BeforeTextChangedCommand = ReactiveCommand.Create();
            disposables.Add(BeforeTextChangedCommand.Subscribe(model.OnBeforeTextChanged));

            TextChangedCommand = ReactiveCommand.Create();
            disposables.Add(TextChangedCommand.Subscribe(model.OnTextChanged));

            SaveCommand = ReactiveCommand.Create();
            disposables.Add(SaveCommand.Subscribe(param => Save()));

            disposables.Add(CloseCommand.Subscribe(_ =>
            {
                Model.ProjectFile.FileModifiedExternally -= ProjectFile_FileModifiedExternally;
                Model.Editor.CaretChangedByPointerClick  -= Editor_CaretChangedByPointerClick;
                Save();
                Model.ShutdownBackgroundWorkers();
                Model.UnRegisterLanguageService();

                intellisenseManager?.Dispose();
                intellisenseManager = null;

                Diagnostics?.Clear();

                ShellViewModel.Instance.InvalidateErrors();

                Model.Dispose();
                Intellisense.Dispose();
                disposables.Dispose();

                Model.TextDocument = null;
            }));

            AddWatchCommand = ReactiveCommand.Create();
            disposables.Add(AddWatchCommand.Subscribe(_ => { IoC.Get <IWatchList>()?.AddWatch(WordAtCaret); }));

            tabCharacter = "    ";

            model.DocumentLoaded += (sender, e) =>
            {
                model.ProjectFile.FileModifiedExternally -= ProjectFile_FileModifiedExternally;
                Model.Editor.CaretChangedByPointerClick  -= Editor_CaretChangedByPointerClick;

                foreach (var bgRenderer in languageServiceBackgroundRenderers)
                {
                    BackgroundRenderers.Remove(bgRenderer);
                }

                languageServiceBackgroundRenderers.Clear();

                foreach (var transformer in languageServiceDocumentLineTransformers)
                {
                    DocumentLineTransformers.Remove(transformer);
                }

                languageServiceDocumentLineTransformers.Clear();

                if (model.LanguageService != null)
                {
                    languageServiceBackgroundRenderers.AddRange(model.LanguageService.GetBackgroundRenderers(model.ProjectFile));

                    foreach (var bgRenderer in languageServiceBackgroundRenderers)
                    {
                        BackgroundRenderers.Add(bgRenderer);
                    }

                    languageServiceDocumentLineTransformers.AddRange(
                        model.LanguageService.GetDocumentLineTransformers(model.ProjectFile));

                    foreach (var textTransformer in languageServiceDocumentLineTransformers)
                    {
                        DocumentLineTransformers.Add(textTransformer);
                    }

                    intellisenseManager = new IntellisenseManager(Model.Editor, Intellisense, Intellisense.CompletionAssistant, model.LanguageService, model.ProjectFile);

                    EventHandler <KeyEventArgs> tunneledKeyUpHandler = (send, ee) =>
                    {
                        if (caretIndex > 0)
                        {
                            intellisenseManager.OnKeyUp(ee, CaretIndex, CaretTextLocation.Line, CaretTextLocation.Column);
                        }
                    };

                    EventHandler <KeyEventArgs> tunneledKeyDownHandler = (send, ee) =>
                    {
                        if (caretIndex > 0)
                        {
                            intellisenseManager.OnKeyDown(ee, CaretIndex, CaretTextLocation.Line, CaretTextLocation.Column);
                        }
                    };

                    EventHandler <TextInputEventArgs> tunneledTextInputHandler = (send, ee) =>
                    {
                        if (caretIndex > 0)
                        {
                            intellisenseManager.OnTextInput(ee, CaretIndex, CaretTextLocation.Line, CaretTextLocation.Column);
                        }
                    };

                    Model.Editor.CaretChangedByPointerClick += Editor_CaretChangedByPointerClick;

                    disposables.Add(Model.Editor.AddHandler(InputElement.KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel));
                    disposables.Add(Model.Editor.AddHandler(InputElement.KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel));
                    disposables.Add(Model.Editor.AddHandler(InputElement.TextInputEvent, tunneledTextInputHandler, RoutingStrategies.Bubble));
                }

                model.CodeAnalysisCompleted += (s, ee) =>
                {
                    Diagnostics = model.CodeAnalysisResults.Diagnostics;

                    HighlightingData =
                        new ObservableCollection <OffsetSyntaxHighlightingData>(model.CodeAnalysisResults.SyntaxHighlightingData);

                    IndexItems         = new ObservableCollection <IndexEntry>(model.CodeAnalysisResults.IndexItems);
                    selectedIndexEntry = IndexItems.FirstOrDefault();
                    this.RaisePropertyChanged(nameof(SelectedIndexEntry));

                    ShellViewModel.Instance.InvalidateErrors();
                };

                model.ProjectFile.FileModifiedExternally += ProjectFile_FileModifiedExternally;

                TextDocument = model.TextDocument;

                Title = Model.ProjectFile.Name;
            };

            model.TextChanged += (sender, e) =>
            {
                if (ShellViewModel.Instance.DocumentTabs.TemporaryDocument == this)
                {
                    Dock = Dock.Left;

                    ShellViewModel.Instance.DocumentTabs.TemporaryDocument = null;
                }

                IsDirty = model.IsDirty;
            };

            intellisense = new IntellisenseViewModel(model, this);

            documentLineTransformers = new ObservableCollection <IDocumentLineTransformer>();

            backgroundRenderers = new ObservableCollection <IBackgroundRenderer>();
            backgroundRenderers.Add(new SelectedLineBackgroundRenderer());

            DebugLineHighlighter = new SelectedDebugLineBackgroundRenderer();
            backgroundRenderers.Add(DebugLineHighlighter);

            backgroundRenderers.Add(new ColumnLimitBackgroundRenderer());
            wordAtCaretHighlighter = new SelectedWordBackgroundRenderer();
            backgroundRenderers.Add(wordAtCaretHighlighter);
            backgroundRenderers.Add(new SelectionBackgroundRenderer());

            margins = new ObservableCollection <TextViewMargin>();

            Dock = Dock.Right;
        }
示例#2
0
        public EditorViewModel(EditorModel model) : base(model)
        {
            disposables      = new CompositeDisposable();
            highlightingData = new ObservableCollection <OffsetSyntaxHighlightingData>();

            BeforeTextChangedCommand = ReactiveCommand.Create();
            disposables.Add(BeforeTextChangedCommand.Subscribe(model.OnBeforeTextChanged));

            TextChangedCommand = ReactiveCommand.Create();
            disposables.Add(TextChangedCommand.Subscribe(model.OnTextChanged));

            SaveCommand = ReactiveCommand.Create();
            disposables.Add(SaveCommand.Subscribe(param => Save()));

            CloseCommand = ReactiveCommand.Create();

            disposables.Add(CloseCommand.Subscribe(_ =>
            {
                Model.ProjectFile.FileModifiedExternally -= ProjectFile_FileModifiedExternally;
                Save();
                Model.ShutdownBackgroundWorkers();
                Model.UnRegisterLanguageService();

                if (ShellViewModel.Instance.DocumentTabs.TemporaryDocument == this)
                {
                    ShellViewModel.Instance.DocumentTabs.TemporaryDocument = null;
                }

                ShellViewModel.Instance.InvalidateErrors();

                Model.Dispose();
                Intellisense.Dispose();
                disposables.Dispose();

                Model.TextDocument = null;
                ShellViewModel.Instance.DocumentTabs.Documents.Remove(this);
            }));

            AddWatchCommand = ReactiveCommand.Create();
            disposables.Add(AddWatchCommand.Subscribe(_ => { IoC.Get <IWatchList>()?.AddWatch(WordAtCaret); }));

            tabCharacter = "    ";

            model.DocumentLoaded += (sender, e) =>
            {
                model.ProjectFile.FileModifiedExternally -= ProjectFile_FileModifiedExternally;

                foreach (var bgRenderer in languageServiceBackgroundRenderers)
                {
                    BackgroundRenderers.Remove(bgRenderer);
                }

                languageServiceBackgroundRenderers.Clear();

                foreach (var transformer in languageServiceDocumentLineTransformers)
                {
                    DocumentLineTransformers.Remove(transformer);
                }

                languageServiceDocumentLineTransformers.Clear();

                if (model.LanguageService != null)
                {
                    languageServiceBackgroundRenderers.AddRange(model.LanguageService.GetBackgroundRenderers(model.ProjectFile));

                    foreach (var bgRenderer in languageServiceBackgroundRenderers)
                    {
                        BackgroundRenderers.Add(bgRenderer);
                    }

                    languageServiceDocumentLineTransformers.AddRange(
                        model.LanguageService.GetDocumentLineTransformers(model.ProjectFile));

                    foreach (var textTransformer in languageServiceDocumentLineTransformers)
                    {
                        DocumentLineTransformers.Add(textTransformer);
                    }
                }

                model.CodeAnalysisCompleted += (s, ee) =>
                {
                    Diagnostics = model.CodeAnalysisResults.Diagnostics;

                    foreach (var marker in Diagnostics)
                    {
                        if (marker.Length == 0)
                        {
                            var line      = TextDocument.GetLineByOffset(marker.StartOffset);
                            var endoffset = TextUtilities.GetNextCaretPosition(TextDocument, marker.StartOffset,
                                                                               TextUtilities.LogicalDirection.Forward, TextUtilities.CaretPositioningMode.WordBorderOrSymbol);

                            if (endoffset == -1)
                            {
                                marker.Length = line.Length;
                            }
                            else
                            {
                                marker.EndOffset = endoffset;
                            }
                        }
                    }

                    HighlightingData =
                        new ObservableCollection <OffsetSyntaxHighlightingData>(model.CodeAnalysisResults.SyntaxHighlightingData);

                    IndexItems         = new ObservableCollection <IndexEntry>(model.CodeAnalysisResults.IndexItems);
                    selectedIndexEntry = IndexItems.FirstOrDefault();
                    this.RaisePropertyChanged(nameof(SelectedIndexEntry));

                    ShellViewModel.Instance.InvalidateErrors();
                };

                model.ProjectFile.FileModifiedExternally += ProjectFile_FileModifiedExternally;
                TextDocument = model.TextDocument;
                this.RaisePropertyChanged(nameof(Title));
            };

            model.TextChanged += (sender, e) =>
            {
                if (ShellViewModel.Instance.DocumentTabs.TemporaryDocument == this)
                {
                    Dock = Dock.Left;

                    ShellViewModel.Instance.DocumentTabs.TemporaryDocument = null;
                }

                this.RaisePropertyChanged(nameof(Title));
            };

            intellisense = new IntellisenseViewModel(model, this);

            documentLineTransformers = new ObservableCollection <IDocumentLineTransformer>();

            backgroundRenderers = new ObservableCollection <IBackgroundRenderer>();
            backgroundRenderers.Add(new SelectedLineBackgroundRenderer());

            DebugLineHighlighter = new SelectedDebugLineBackgroundRenderer();
            backgroundRenderers.Add(DebugLineHighlighter);

            backgroundRenderers.Add(new ColumnLimitBackgroundRenderer());
            wordAtCaretHighlighter = new SelectedWordBackgroundRenderer();
            backgroundRenderers.Add(wordAtCaretHighlighter);
            backgroundRenderers.Add(new SelectionBackgroundRenderer());

            margins = new ObservableCollection <TextViewMargin>();

            dock = Dock.Right;
        }