private static void AssertFormatAfterTypeChar(string code, string expected, Dictionary <OptionKey, object> changedOptionSet = null) { using (var workspace = TestWorkspace.CreateCSharp(code)) { if (changedOptionSet != null) { var options = workspace.Options; foreach (var entry in changedOptionSet) { options = options.WithChangedOption(entry.Key, entry.Value); } workspace.Options = options; } var subjectDocument = workspace.Documents.Single(); var textUndoHistory = new Mock <ITextUndoHistoryRegistry>(); var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>(); var editorOperations = new Mock <IEditorOperations>(); editorOperationsFactory.Setup(x => x.GetEditorOperations(subjectDocument.GetTextView())).Returns(editorOperations.Object); var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, textUndoHistory.Object, editorOperationsFactory.Object); var typedChar = subjectDocument.GetTextBuffer().CurrentSnapshot.GetText(subjectDocument.CursorPosition.Value - 1, 1); commandHandler.ExecuteCommand(new TypeCharCommandArgs(subjectDocument.GetTextView(), subjectDocument.TextBuffer, typedChar[0]), () => { }); var newSnapshot = subjectDocument.TextBuffer.CurrentSnapshot; Assert.Equal(expected, newSnapshot.GetText()); } }
protected static void AssertFormatWithView(string expectedWithMarker, string codeWithMarker, bool debugMode = false) { var editorOperations = new Mock <IEditorOperations>(MockBehavior.Strict); var editorOperationsFactoryService = new Mock <IEditorOperationsFactoryService>(MockBehavior.Strict); editorOperations.Setup(o => o.AddAfterTextBufferChangePrimitive()); editorOperations.Setup(o => o.AddBeforeTextBufferChangePrimitive()); editorOperationsFactoryService.Setup(s => s.GetEditorOperations(It.IsAny <ITextView>())).Returns(editorOperations.Object); using (var workspace = TestWorkspace.CreateCSharp(codeWithMarker)) { // set up caret position var testDocument = workspace.Documents.Single(); var view = testDocument.GetTextView(); view.Caret.MoveTo(new SnapshotPoint(view.TextSnapshot, testDocument.CursorPosition.Value)); // get original buffer var buffer = workspace.Documents.First().GetTextBuffer(); var commandHandler = new FormatCommandHandler(workspace.GetService <ITextUndoHistoryRegistry>(), editorOperationsFactoryService.Object); var commandArgs = new FormatDocumentCommandArgs(view, view.TextBuffer); commandHandler.ExecuteCommand(commandArgs, TestCommandExecutionContext.Create()); MarkupTestFile.GetPosition(expectedWithMarker, out var expected, out int expectedPosition); Assert.Equal(expected, view.TextSnapshot.GetText()); var caretPosition = view.Caret.Position.BufferPosition.Position; Assert.True(expectedPosition == caretPosition, string.Format("Caret positioned incorrectly. Should have been {0}, but was {1}.", expectedPosition, caretPosition)); } }
protected CommandFilter CreateCommandFilter(ITextBuffer textBuffer) { Requires.NotNull(textBuffer, nameof(textBuffer)); CommandFilter filter = new CommandFilter(); FormatCommandHandler formattingManager = new FormatCommandHandler(textBuffer, this.WpfTextView, this.core); filter.MiniFilters.Add(formattingManager); IOleCommandTarget nextFilter = null; this.VsTextView.AddCommandFilter(filter, out nextFilter); filter.Next = nextFilter; return(filter); }
protected static void AssertFormatWithPasteOrReturn(string expectedWithMarker, string codeWithMarker, bool allowDocumentChanges, bool isPaste = true) { using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromLines(codeWithMarker)) { workspace.CanApplyChangeDocument = allowDocumentChanges; // set up caret position var testDocument = workspace.Documents.Single(); var view = testDocument.GetTextView(); view.Caret.MoveTo(new SnapshotPoint(view.TextSnapshot, testDocument.CursorPosition.Value)); // get original buffer var buffer = workspace.Documents.First().GetTextBuffer(); var optionService = workspace.Services.GetService <IOptionService>(); if (isPaste) { optionService.SetOptions(optionService.GetOptions().WithChangedOption(FeatureOnOffOptions.FormatOnPaste, LanguageNames.CSharp, true)); var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, null, null); var commandArgs = new PasteCommandArgs(view, view.TextBuffer); commandHandler.ExecuteCommand(commandArgs, () => { }); } else { // Return Key Command var textUndoHistory = new Mock <ITextUndoHistoryRegistry>(); var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>(); var editorOperations = new Mock <IEditorOperations>(); editorOperationsFactory.Setup(x => x.GetEditorOperations(testDocument.GetTextView())).Returns(editorOperations.Object); var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, textUndoHistory.Object, editorOperationsFactory.Object); var commandArgs = new ReturnKeyCommandArgs(view, view.TextBuffer); commandHandler.ExecuteCommand(commandArgs, () => { }); } string expected; int expectedPosition; MarkupTestFile.GetPosition(expectedWithMarker, out expected, out expectedPosition); Assert.Equal(expected, view.TextSnapshot.GetText()); var caretPosition = view.Caret.Position.BufferPosition.Position; Assert.True(expectedPosition == caretPosition, string.Format("Caret positioned incorrectly. Should have been {0}, but was {1}.", expectedPosition, caretPosition)); } }
private static void AssertFormatAfterTypeChar(string code, string expected) { using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code)) { var subjectDocument = workspace.Documents.Single(); var textUndoHistory = new Mock <ITextUndoHistoryRegistry>(); var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>(); var editorOperations = new Mock <IEditorOperations>(); editorOperationsFactory.Setup(x => x.GetEditorOperations(subjectDocument.GetTextView())).Returns(editorOperations.Object); var commandHandler = new FormatCommandHandler(TestWaitIndicator.Default, textUndoHistory.Object, editorOperationsFactory.Object); var typedChar = subjectDocument.GetTextBuffer().CurrentSnapshot.GetText(subjectDocument.CursorPosition.Value - 1, 1); commandHandler.ExecuteCommand(new TypeCharCommandArgs(subjectDocument.GetTextView(), subjectDocument.TextBuffer, typedChar[0]), () => { }); var newSnapshot = subjectDocument.TextBuffer.CurrentSnapshot; Assert.Equal(expected, newSnapshot.GetText()); } }