public void OnNextParseAfterSetupSetsSelectionsBeforeReactivatingTheActiveCodePane()
        {
            var lastCalledMethod     = string.Empty;
            var selectionServiceMock = TestSelectionServiceMock();

            selectionServiceMock.Setup(m => m.TryActivate(It.IsAny <QualifiedModuleName>()))
            .Callback((QualifiedModuleName module) => lastCalledMethod = "TryActivate");
            selectionServiceMock.Setup(m => m.TrySetSelection(It.IsAny <QualifiedModuleName>(), It.IsAny <Selection>()))
            .Callback((QualifiedModuleName module, Selection selection) => lastCalledMethod = "TrySetSelection");

            selectionServiceMock.Setup(m => m.ActiveSelection()).Returns(() => _testModuleSelections[1]);

            var parseManagerMock   = new Mock <IParseManager>();
            var selectionRecoverer = new SelectionRecoverer(selectionServiceMock.Object, parseManagerMock.Object);

            var selectionRecoveryModules = _testModuleSelections
                                           .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(2);

            selectionRecoverer.SaveSelections(selectionRecoveryModules);
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();
            selectionRecoverer.SaveActiveCodePane();
            selectionRecoverer.RecoverActiveCodePaneOnNextParse();

            var stateEventArgs = new ParserStateEventArgs(_stateExpectedToTriggerTheRecovery, ParserState.Pending,
                                                          CancellationToken.None);

            parseManagerMock.Raise(m => m.StateChanged += null, stateEventArgs);

            Assert.AreEqual("TryActivate", lastCalledMethod);
        }
        public void SelectionAdjustmentAddsToReplacementSelectionOnParseAfterRecoverSelectionsOnNextParse()
        {
            var selectionServiceMock = TestSelectionServiceMock();
            var parseManagerMock     = new Mock <IParseManager>();
            var selectionRecoverer   = new SelectionRecoverer(selectionServiceMock.Object, parseManagerMock.Object);

            var selectionOffset      = new Selection(0, 2, 4, 5);
            var selectionReplacement = new Selection(22, 2, 44, 5);

            selectionRecoverer.SaveSelections(_testModuleSelections
                                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(1));
            selectionRecoverer.ReplaceSavedSelection(_testModuleSelections[0].QualifiedName, selectionReplacement);
            selectionRecoverer.AdjustSavedSelection(_testModuleSelections[0].QualifiedName, selectionOffset);
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();

            var stateEventArgs = new ParserStateEventArgs(_stateExpectedToTriggerTheRecovery, ParserState.Pending,
                                                          CancellationToken.None);

            parseManagerMock.Raise(m => m.StateChanged += null, stateEventArgs);

            var expectedAdjustedSelection = selectionReplacement.Offset(selectionOffset);

            selectionServiceMock.Verify(
                m => m.TrySetSelection(_testModuleSelections[0].QualifiedName, expectedAdjustedSelection), Times.Once);
        }
        public void SetsReplacementSelectionOnParseAfterRecoverSelectionsOnNextParse_SelectionSavedPreviously()
        {
            var selectionServiceMock = TestSelectionServiceMock();
            var parseManagerMock     = new Mock <IParseManager>();
            var selectionRecoverer   = new SelectionRecoverer(selectionServiceMock.Object, parseManagerMock.Object);

            var selectionReplacement = new Selection(22, 2, 44, 5);

            selectionRecoverer.SaveSelections(_testModuleSelections
                                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(2));
            selectionRecoverer.ReplaceSavedSelection(_testModuleSelections[0].QualifiedName, selectionReplacement);
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();

            var stateEventArgs = new ParserStateEventArgs(_stateExpectedToTriggerTheRecovery, ParserState.Pending,
                                                          CancellationToken.None);

            parseManagerMock.Raise(m => m.StateChanged += null, stateEventArgs);

            selectionServiceMock.Verify(
                m => m.TrySetSelection(_testModuleSelections[0].QualifiedName, selectionReplacement), Times.Once);
            selectionServiceMock.Verify(
                m => m.TrySetSelection(_testModuleSelections[1].QualifiedName, _testModuleSelections[1].Selection),
                Times.Once);
            selectionServiceMock.Verify(
                m => m.TrySetSelection(_testModuleSelections[2].QualifiedName, It.IsAny <Selection>()), Times.Never);
        }
        public void SetsExactlyLastSavedSelectionsOnParseAfterRecoverSelectionsOnNextParseAfterMultipleSaves()
        {
            var selectionServiceMock = TestSelectionServiceMock();
            var parseManagerMock     = new Mock <IParseManager>();
            var selectionRecoverer   = new SelectionRecoverer(selectionServiceMock.Object, parseManagerMock.Object);

            selectionRecoverer.SaveSelections(_testModuleSelections
                                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(2));
            selectionRecoverer.SaveSelections(_testModuleSelections
                                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Skip(1));
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();

            var stateEventArgs = new ParserStateEventArgs(_stateExpectedToTriggerTheRecovery, ParserState.Pending,
                                                          CancellationToken.None);

            parseManagerMock.Raise(m => m.StateChanged += null, stateEventArgs);

            foreach (var qualifiedSelection in _testModuleSelections.Skip(1))
            {
                selectionServiceMock.Verify(
                    m => m.TrySetSelection(qualifiedSelection.QualifiedName, qualifiedSelection.Selection), Times.Once);
            }

            selectionServiceMock.Verify(
                m => m.TrySetSelection(_testModuleSelections[0].QualifiedName, It.IsAny <Selection>()), Times.Never);
        }
        public void RecoverSelectionsOnNextParseDoesNotSetAnythingImmediately()
        {
            var selectionServiceMock = TestSelectionServiceMock();
            var parseManager         = new Mock <IParseManager>().Object;
            var selectionRecoverer   = new SelectionRecoverer(selectionServiceMock.Object, parseManager);

            var selectionOffset      = new Selection(0, 2, 4, 5);
            var selectionReplacement = new Selection(22, 2, 44, 5);

            selectionRecoverer.SaveSelections(_testModuleSelections
                                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(2));
            selectionRecoverer.ReplaceSavedSelection(_testModuleSelections[2].QualifiedName, selectionReplacement);
            selectionRecoverer.AdjustSavedSelection(_testModuleSelections[0].QualifiedName, selectionOffset);
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();

            foreach (var qualifiedSelection in _testModuleSelections)
            {
                selectionServiceMock.Verify(
                    m => m.TrySetSelection(qualifiedSelection.QualifiedName, qualifiedSelection.Selection),
                    Times.Never);
            }
        }
        public void OnNextParseAfterSetupOpenModulesWithSavedOpenStateBeforeSettingSelection()
        {
            var lastCalledMethod     = string.Empty;
            var selectionServiceMock = TestSelectionServiceMock();

            selectionServiceMock.Setup(m => m.TryActivate(It.IsAny <QualifiedModuleName>()))
            .Callback((QualifiedModuleName module) => lastCalledMethod = "TryActivate");
            selectionServiceMock.Setup(m => m.TrySetSelection(It.IsAny <QualifiedModuleName>(), It.IsAny <Selection>()))
            .Callback((QualifiedModuleName module, Selection selection) => lastCalledMethod = "TrySetSelection");

            var openModules = _testModuleSelections.Take(3)
                              .Select(qualifiedSelection => qualifiedSelection.QualifiedName)
                              .ToHashSet();

            selectionServiceMock.Setup(m => m.OpenModules()).Returns(openModules);

            var parseManagerMock   = new Mock <IParseManager>();
            var selectionRecoverer = new SelectionRecoverer(selectionServiceMock.Object, parseManagerMock.Object);

            var modulesForWhichToSaveOpenState = _testModuleSelections.Skip(1)
                                                 .Select(qualifiedSelection => qualifiedSelection.QualifiedName)
                                                 .ToHashSet();
            var selectionRecoveryModules = _testModuleSelections
                                           .Select(qualifiedSelection => qualifiedSelection.QualifiedName).Take(2);

            selectionRecoverer.SaveSelections(selectionRecoveryModules);
            selectionRecoverer.RecoverSavedSelectionsOnNextParse();
            selectionRecoverer.SaveOpenState(modulesForWhichToSaveOpenState);
            selectionRecoverer.RecoverOpenStateOnNextParse();

            var stateEventArgs = new ParserStateEventArgs(_stateExpectedToTriggerTheRecovery, ParserState.Pending,
                                                          CancellationToken.None);

            parseManagerMock.Raise(m => m.StateChanged += null, stateEventArgs);

            Assert.AreEqual("TrySetSelection", lastCalledMethod);
        }