protected void RunAutoInsertTest(string input, string expected, int tabSize = 4, bool insertSpaces = true, string fileKind = default, IReadOnlyList <TagHelperDescriptor> tagHelpers = default)
        {
            // Arrange
            TestFileMarkupParser.GetPosition(input, out input, out var location);

            var source = SourceText.From(input);

            source.GetLineAndOffset(location, out var line, out var column);
            var position = new Position(line, column);

            var path         = "file:///path/to/document.razor";
            var uri          = new Uri(path);
            var codeDocument = CreateCodeDocument(source, uri.AbsolutePath, tagHelpers, fileKind: fileKind);
            var options      = new FormattingOptions()
            {
                TabSize      = tabSize,
                InsertSpaces = insertSpaces,
            };

            var provider = CreateProvider();
            var context  = FormattingContext.Create(uri, Mock.Of <DocumentSnapshot>(MockBehavior.Strict), codeDocument, options, TestAdhocWorkspaceFactory.Instance);

            // Act
            if (!provider.TryResolveInsertion(position, context, out var edit, out _))
            {
                edit = null;
            }

            // Assert
            var edited = edit is null ? source : ApplyEdit(source, edit);
            var actual = edited.ToString();

            Assert.Equal(expected, actual);
        }
Esempio n. 2
0
        protected async Task RunOnTypeFormattingTestAsync(string input, string expected, int tabSize = 4, bool insertSpaces = true, string fileKind = null)
        {
            // Arrange
            fileKind ??= FileKinds.Component;

            TestFileMarkupParser.GetPosition(input, out input, out var beforeTrigger);

            var source = SourceText.From(input);

            var path = "file:///path/to/document.razor";
            var uri  = new Uri(path);

            var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(source, uri.AbsolutePath, fileKind: fileKind);
            var options = new FormattingOptions()
            {
                TabSize      = tabSize,
                InsertSpaces = insertSpaces,
            };

            var formattingService = CreateFormattingService(codeDocument);

            var(kind, projectedEdits) = GetFormattedEdits(codeDocument, expected, beforeTrigger);

            // Act
            var edits = await formattingService.ApplyFormattedEditsAsync(uri, documentSnapshot, kind, projectedEdits, options, CancellationToken.None);

            // Assert
            var edited = ApplyEdits(source, edits);
            var actual = edited.ToString();

            new XUnitVerifier().EqualOrDiff(expected, actual);
        }
        protected async Task RunCodeActionFormattingTestAsync(
            string input,
            TextEdit[] codeActionEdits,
            string expected,
            int tabSize       = 4,
            bool insertSpaces = true,
            string?fileKind   = null)
        {
            if (codeActionEdits is null)
            {
                throw new NotImplementedException("Code action formatting must provide edits.");
            }

            // Arrange
            fileKind ??= FileKinds.Component;

            TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger);

            var razorSourceText = SourceText.From(input);
            var path            = "file:///path/to/Document.razor";
            var uri             = new Uri(path);

            var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind);

#pragma warning disable CS0618 // Type or member is obsolete
            var mappingService = new DefaultRazorDocumentMappingService();
#pragma warning restore CS0618 // Type or member is obsolete
            var languageKind = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger);
            if (languageKind == RazorLanguageKind.Html)
            {
                throw new NotImplementedException("Code action formatting is not yet supported for HTML in Razor.");
            }

            if (!mappingService.TryMapToProjectedDocumentPosition(codeDocument, positionAfterTrigger, out _, out var _))
            {
                throw new InvalidOperationException("Could not map from Razor document to generated document");
            }

            var formattingService = CreateFormattingService(codeDocument);
            var options           = new FormattingOptions()
            {
                TabSize      = tabSize,
                InsertSpaces = insertSpaces,
            };

            // Act
            var edits = await formattingService.FormatCodeActionAsync(uri, documentSnapshot, languageKind, codeActionEdits, options, CancellationToken.None);

            // Assert
            var edited = ApplyEdits(razorSourceText, edits);
            var actual = edited.ToString();

            new XUnitVerifier().EqualOrDiff(expected, actual);
        }
        protected async Task <(SourceText, TextEdit[])> GetOnTypeFormattingEditsAsync(
            string input,
            char triggerCharacter,
            int tabSize       = 4,
            bool insertSpaces = true,
            string fileKind   = null)
        {
            // Arrange
            fileKind ??= FileKinds.Component;

            TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger);

            var razorSourceText = SourceText.From(input);
            var path            = "file:///path/to/Document.razor";
            var uri             = new Uri(path);

            var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind);

            var mappingService = new DefaultRazorDocumentMappingService();
            var languageKind   = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger);

            if (!mappingService.TryMapToProjectedDocumentPosition(codeDocument, positionAfterTrigger, out _, out var projectedIndex))
            {
                throw new InvalidOperationException("Could not map from Razor document to generated document");
            }

            var projectedEdits = Array.Empty <TextEdit>();

            if (languageKind == RazorLanguageKind.CSharp)
            {
                projectedEdits = await GetFormattedCSharpEditsAsync(
                    codeDocument, triggerCharacter, projectedIndex, insertSpaces, tabSize).ConfigureAwait(false);
            }
            else if (languageKind == RazorLanguageKind.Html)
            {
                throw new NotImplementedException("OnTypeFormatting is not yet supported for HTML in Razor.");
            }

            var formattingService = CreateFormattingService(codeDocument);
            var options           = new FormattingOptions()
            {
                TabSize      = tabSize,
                InsertSpaces = insertSpaces,
            };

            // Act
            var edits = await formattingService.ApplyFormattedEditsAsync(
                uri, documentSnapshot, languageKind, projectedEdits, options, CancellationToken.None);

            return(razorSourceText, edits);
        }
        protected async Task RunOnTypeFormattingTestAsync(
            string input,
            string expected,
            char triggerCharacter,
            int tabSize       = 4,
            bool insertSpaces = true,
            string?fileKind   = null)
        {
            // Arrange
            fileKind ??= FileKinds.Component;

            TestFileMarkupParser.GetPosition(input, out input, out var positionAfterTrigger);

            var razorSourceText = SourceText.From(input);
            var path            = "file:///path/to/Document.razor";
            var uri             = new Uri(path);

            var(codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(razorSourceText, uri.AbsolutePath, fileKind: fileKind);

            var mappingService = new DefaultRazorDocumentMappingService(LoggerFactory);
            var languageKind   = mappingService.GetLanguageKind(codeDocument, positionAfterTrigger);

            var formattingService = CreateFormattingService(codeDocument);
            var options           = new FormattingOptions()
            {
                TabSize      = tabSize,
                InsertSpaces = insertSpaces,
            };

            // Act
            var edits = await formattingService.FormatOnTypeAsync(uri, documentSnapshot, languageKind, Array.Empty <TextEdit>(), options, hostDocumentIndex : positionAfterTrigger, triggerCharacter : triggerCharacter, CancellationToken.None);

            // Assert
            var edited = ApplyEdits(razorSourceText, edits);
            var actual = edited.ToString();

            new XUnitVerifier().EqualOrDiff(expected, actual);

            if (input.Equals(expected))
            {
                Assert.Empty(edits);
            }
        }