private static async Task AssertFormatAfterTypeCharAsync(string code, string expected, Dictionary<OptionKey, object> changedOptionSet = null) { using (var workspace = await TestWorkspace.CreateCSharpAsync(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()); } }
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()); } }
protected static async Task AssertFormatWithPasteOrReturnAsync(string expectedWithMarker, string codeWithMarker, bool allowDocumentChanges, bool isPaste = true) { using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromLinesAsync(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)); } }
internal static async Task AutoFormatTokenAsync(string markup, string expected) { using (var workspace = await TestWorkspace.CreateCSharpAsync(markup)) { var subjectDocument = workspace.Documents.Single(); var optionService = workspace.Services.GetService<IOptionService>(); 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 async Task AssertFormatWithViewAsync(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 = await CSharpWorkspaceFactory.CreateWorkspaceFromLinesAsync(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(TestWaitIndicator.Default, workspace.GetService<ITextUndoHistoryRegistry>(), editorOperationsFactoryService.Object); var commandArgs = new FormatDocumentCommandArgs(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)); } }