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); }
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); }
protected virtual Task <IList <TextChange> > GetFormattingChangesAsync( IEditorFormattingService formattingService, Document document, TextSpan?textSpan, DocumentOptionSet documentOptions, CancellationToken cancellationToken) => formattingService.GetFormattingChangesAsync(document, textSpan, documentOptions, cancellationToken);
protected virtual Task <IList <TextChange>?> GetFormattingChangesAsync( IEditorFormattingService formattingService, Document document, char typedChar, int position, DocumentOptionSet documentOptions, CancellationToken cancellationToken) => formattingService.GetFormattingChangesAsync(document, typedChar, position, documentOptions, cancellationToken);
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); }
protected virtual Task <IList <TextChange> > GetFormattingChangesAsync(IEditorFormattingService formattingService, Document document, TextSpan?textSpan, CancellationToken cancellationToken) { return(formattingService.GetFormattingChangesAsync(document, textSpan, cancellationToken)); }
public Task <IList <TextChange> > GetFormattingChangesAsync(Document document, TextSpan?textSpan, CancellationToken cancellationToken) => _underlyingObject.GetFormattingChangesAsync(document, textSpan, cancellationToken);