public void UpdatePreview(string text) { const string start = "//["; const string end = "//]"; var sourceText = SourceText.From(text); var syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText); var edits = Formatter.GetEdits(syntaxTree, new TextSpan(sourceText, 0, text.Length), _optionsService.FormattingOptions); var formatted = Formatter.ApplyEdits(text, edits); var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted, _contentType); var bufferText = textBuffer.CurrentSnapshot.GetText().ToString(); var startIndex = bufferText.IndexOf(start, StringComparison.Ordinal); var endIndex = bufferText.IndexOf(end, StringComparison.Ordinal); var startLine = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(startIndex) + 1; var endLine = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(endIndex); var projection = _projectionBufferFactory.CreateProjectionBufferWithoutIndentation(_contentTypeRegistryService, _editorOptions.CreateOptions(), textBuffer.CurrentSnapshot, "", LineSpan.FromBounds(startLine, endLine)); var textView = _textEditorFactoryService.CreateTextView(projection, _textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Analyzable)); this.TextViewHost = _textEditorFactoryService.CreateTextViewHost(textView, setFocus: false); }
private static void AssertFormat(string unformattedText, string expectedNewText, TextSpan?textSpan = null, FormattingOptions options = null, Dictionary <string, string> includes = null, bool allowSyntaxErrors = false) { Func <string, SyntaxTree> parse = code => SyntaxFactory.ParseSyntaxTree(SourceText.From(code), fileSystem: new InMemoryFileSystem(includes ?? new Dictionary <string, string>())); // Arrange. var syntaxTree = parse(unformattedText); if (textSpan == null) { textSpan = new TextSpan(syntaxTree.Text, 0, unformattedText.Length); } if (options == null) { options = new FormattingOptions(); } // Act. var edits = Formatter.GetEdits(syntaxTree, textSpan.Value, options); var formattedCode = Formatter.ApplyEdits(unformattedText, edits); // Assert. if (!allowSyntaxErrors) { ShaderTestUtility.CheckForParseErrors(parse(formattedCode)); } Assert.That(formattedCode, Is.EqualTo(expectedNewText)); }
protected override string ApplyFormatting(string text) { var sourceText = SourceText.From(text); var syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceText); var edits = Formatter.GetEdits(syntaxTree, new TextSpan(sourceText, 0, text.Length), _optionsService.FormattingOptions); return(Formatter.ApplyEdits(text, edits)); }
private string Format(string unformattedText, FormattingOptions options = null) { SyntaxTree syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(unformattedText), null); TextSpan span = new TextSpan(0, unformattedText.Length); if (options == null) { options = new FormattingOptions(); } IList <TextChange> edits = Formatter.GetEdits(syntaxTree, (SyntaxNode)syntaxTree.Root, span, options); return(Formatter.ApplyEdits(unformattedText, edits)); }
private static string FormatCode(string sourceCode, SyntaxTree syntaxTree) { var edits = Formatter.GetEdits(syntaxTree, new TextSpan(syntaxTree.Text, 0, sourceCode.Length), new FormattingOptions()); return(Formatter.ApplyEdits(sourceCode, edits)); }