예제 #1
0
        internal async Task TestWithLineFeedOnlyBlankLinesAtEndOfFileAsync(EndOfFileHandling? newlineAtEndOfFile, string result)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\n\n";
            var fixedCode = BaseCode + result;

            var expected = this.CSharpDiagnostic().WithLocation(8, 2);
            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
        /// <summary>
        /// Fixes the whitespace at the end of a document.
        /// </summary>
        /// <param name="document">The document to be changed.</param>
        /// <param name="diagnostic">The diagnostic to fix.</param>
        /// <param name="newlineAtEndOfFile">A <see cref="EndOfFileHandling"/> value indicating the desired behavior.</param>
        /// <param name="cancellationToken">The cancellation token associated with the fix action.</param>
        /// <returns>The transformed document.</returns>
        private static async Task <Document> FixEndOfFileAsync(Document document, Diagnostic diagnostic, EndOfFileHandling newlineAtEndOfFile, CancellationToken cancellationToken)
        {
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            string replacement = newlineAtEndOfFile == EndOfFileHandling.Omit ? string.Empty : "\r\n";

            return(document.WithText(text.WithChanges(new TextChange(diagnostic.Location.SourceSpan, replacement))));
        }
 /// <summary>
 /// Fixes the whitespace at the end of a document.
 /// </summary>
 /// <param name="document">The document to be changed.</param>
 /// <param name="diagnostic">The diagnostic to fix.</param>
 /// <param name="newlineAtEndOfFile">A <see cref="EndOfFileHandling"/> value indicating the desired behavior.</param>
 /// <param name="cancellationToken">The cancellation token associated with the fix action.</param>
 /// <returns>The transformed document.</returns>
 private static async Task<Document> FixEndOfFileAsync(Document document, Diagnostic diagnostic, EndOfFileHandling newlineAtEndOfFile, CancellationToken cancellationToken)
 {
     var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
     string replacement = newlineAtEndOfFile == EndOfFileHandling.Omit ? string.Empty : "\r\n";
     return document.WithText(text.WithChanges(new TextChange(diagnostic.Location.SourceSpan, replacement)));
 }
예제 #4
0
        internal async Task TestWithSingleCarriageReturnLineFeedAtEndOfFileAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\r\n";
            var fixedCode = BaseCode + expectedText;

            if (expectedText == null)
            {
                await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = this.CSharpDiagnostic().WithLocation(8, 2);
                await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
            }
        }
예제 #5
0
        internal async Task TestCodeFixProviderStripsTrailingLinefeedOnlyBlankLinesIncludingWhitespaceAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\n   \n   \n";
            var fixedCode = BaseCode + expectedText;

            var expected = this.CSharpDiagnostic().WithLocation(8, 2);
            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
예제 #6
0
        internal async Task TestCodeFixProviderOnlyStripsTrailingBlankLinesAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n   \r\n";
            var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;

            var expected = this.CSharpDiagnostic().WithLocation(10, 7);
            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
예제 #7
0
        internal async Task TestFileEndingWithCommentAndSpuriousWhitespaceAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\r\n// Test comment\r\n   \r\n";
            var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;

            var expected = this.CSharpDiagnostic().WithLocation(9, 16);
            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
예제 #8
0
        internal async Task TestFileEndingWithEndIfAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n";
            var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;

            if (expectedText == null)
            {
                await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = this.CSharpDiagnostic().WithLocation(10, 7);
                await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
            }
        }
예제 #9
0
        internal async Task TestFileEndsWithSpacesAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\r\n          ";
            var fixedCode = BaseCode + expectedText;

            var expected = this.CSharpDiagnostic().WithLocation(8, 2);
            await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
        }
 protected internal LayoutSettings()
 {
     this.newlineAtEndOfFile = EndOfFileHandling.Allow;
 }
예제 #11
0
        private DiagnosticDescriptor GetDescriptor(EndOfFileHandling? endOfFileHandling)
        {
            switch (endOfFileHandling)
            {
            case EndOfFileHandling.Require:
                return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorRequire;

            case EndOfFileHandling.Omit:
                return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorOmit;

            case EndOfFileHandling.Allow:
            case null:
            default:
                return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorAllow;
            }
        }
예제 #12
0
        internal async Task TestFileEndingWithCommentAsync(EndOfFileHandling? newlineAtEndOfFile, string expectedText)
        {
            this.newlineAtEndOfFile = newlineAtEndOfFile;

            var testCode = BaseCode + "\r\n// Test comment";
            var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;

            if (expectedText == null)
            {
                await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            }
            else
            {
                var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
                await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
                await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
            }
        }
예제 #13
0
 protected internal LayoutSettings()
 {
     this.newlineAtEndOfFile = EndOfFileHandling.Allow;
 }