private bool TryExecuteCommand(FormatDocumentCommandArgs args) { if (!args.SubjectBuffer.CanApplyChangeDocumentToWorkspace()) { return false; } var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return false; } var formattingService = document.GetLanguageService<IEditorFormattingService>(); if (formattingService == null || !formattingService.SupportsFormatDocument) { return false; } var result = false; _waitIndicator.Wait( title: EditorFeaturesResources.FormatDocument, message: EditorFeaturesResources.FormattingDocument, allowCancel: true, action: waitContext => { Format(args.TextView, document, null, waitContext.CancellationToken); result = true; }); // We don't call nextHandler, since we have handled this command. return result; }
public void ExecuteCommand(FormatDocumentCommandArgs args, Action nextHandler) { if (!TryExecuteCommand(args)) { nextHandler(); } }
public CommandState GetCommandState(FormatDocumentCommandArgs args, Func<CommandState> nextHandler) { return GetCommandState(args.SubjectBuffer, nextHandler); }
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)); } }