protected BqlFormatterTests(BqlFormatter formatter) { _formatter = formatter; }
private async System.Threading.Tasks.Task CommandCallbackAsync() { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IWpfTextView textView = await ServiceProvider.GetWpfTextViewAsync(); if (textView == null) { return; } SnapshotPoint caretPosition = textView.Caret.Position.BufferPosition; ITextSnapshotLine caretLine = caretPosition.GetContainingLine(); if (caretLine == null) { return; } Document document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return; } await TaskScheduler.Default; //Go to background thread SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(); SemanticModel semanticModel = await document.GetSemanticModelAsync(); if (syntaxRoot == null || semanticModel == null) { return; } //TextSpan lineSpan = TextSpan.FromBounds(caretLine.Start.Position, caretLine.End.Position); //var memberNode = syntaxRoot.FindNode(lineSpan) as MemberDeclarationSyntax; // if (memberNode == null) // return; SyntaxNode fixedRoot; #region todo if (textView.Selection.IsActive && !textView.Selection.IsEmpty) // if has selection { // todo: check selection //// Find all nodes within the span and format them //var selectionSpan = TextSpan.FromBounds(textView.Selection.Start.Position, textView.Selection.End.Position); //SyntaxNode topNode = syntaxRoot.FindNode(selectionSpan); // can, return top node that intersects with selectionSpan, so we need SpanWalker here //if (topNode == null) // return; // nothing to format (e.g. selection contains only trivia) //var spanWalker = new SpanWalker(selectionSpan); //spanWalker.Visit(topNode); //if (spanWalker.NodesWithinSpan.Count == 0) // return; //fixedRoot = syntaxRoot.ReplaceNodes(spanWalker.NodesWithinSpan, (o, r) => formatter.Format(o, semanticModel)); } else { } #endregion fixedRoot = new AngleBracesBqlRewriter(semanticModel).Visit(syntaxRoot); var newDocument = document.WithSyntaxRoot(fixedRoot); SyntaxNode newSyntaxRoot = await newDocument.GetSyntaxRootAsync(); SemanticModel newSemanticModel = await newDocument.GetSemanticModelAsync(); if (newSyntaxRoot == null || newSemanticModel == null) { return; } // have to format, because cannot save all original indention BqlFormatter formatter = BqlFormatter.FromTextView(textView); var formatedRoot = formatter.Format(newSyntaxRoot, newSemanticModel); await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Return to UI thread if (!textView.TextBuffer.EditInProgress) { var formattedDocument = document.WithSyntaxRoot(formatedRoot); ApplyChanges(document, formattedDocument); } }