bool TryFormat(IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken) { var document = DocumentContext.AnalysisDocument; IList <TextChange> changes; if (formatOnReturn) { if (!formattingService.SupportsFormatOnReturn) { return(false); } changes = formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken); } else { if (!formattingService.SupportsFormattingOnTypedCharacter(document, typedChar)) { return(false); } changes = formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken); } if (changes == null || changes.Count == 0) { return(false); } Editor.ApplyTextChanges(changes); return(true); }
protected virtual Task <IList <TextChange>?> GetFormattingChangesOnReturnAsync( IEditorFormattingService formattingService, Document document, int position, DocumentOptionSet documentOptions, CancellationToken cancellationToken) => formattingService.GetFormattingChangesOnReturnAsync(document, position, documentOptions, cancellationToken);
bool TryFormat(IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken) { var document = DocumentContext.AnalysisDocument; IEnumerable <TextChange> changes; if (formatOnReturn) { if (!formattingService.SupportsFormatOnReturn) { return(false); } changes = formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken); } else { if (!formattingService.SupportsFormattingOnTypedCharacter(document, typedChar)) { return(false); } changes = formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken); var line = Editor.GetLineByOffset(position); if (typedChar == '#') { changes = changes.Where(c => c.Span.Start >= line.Offset); } } if (changes == null) { return(false); } Editor.ApplyTextChanges(changes); return(true); }
private bool TryFormat( ITextView textView, Document document, IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken) { var changes = formatOnReturn ? formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken) : formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken); if (changes == null || changes.Count == 0) { return false; } using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting)) { transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance; document.Project.Solution.Workspace.ApplyTextChanges(document.Id, changes, cancellationToken); transaction.Complete(); } return true; }
private bool TryFormat( ITextView textView, Document document, IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken) { var changes = formatOnReturn ? formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken) : formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken); if (changes == null || changes.Count == 0) { return(false); } using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting)) { transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance; document.Project.Solution.Workspace.ApplyTextChanges(document.Id, changes, cancellationToken); transaction.Complete(); } return(true); }