FormatDocumentRangeTests.cs 2.1 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4 5 6 7

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
D
David Barbet 已提交
8
using Roslyn.Test.Utilities;
9 10 11 12 13
using Xunit;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Formatting
{
D
tests.  
David Barbet 已提交
14
    public class FormatDocumentRangeTests : AbstractLanguageServerProtocolTests
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    {
        [Fact]
        public async Task TestFormatDocumentRangeAsync()
        {
            var markup =
@"class A
{
{|format:void|} M()
{
            int i = 1;
    }
}";
            var expected =
@"class A
{
    void M()
{
            int i = 1;
    }
}";
35
            using var workspace = CreateTestWorkspace(markup, out var locations);
D
David Barbet 已提交
36
            var rangeToFormat = locations["format"].Single();
37
            var documentText = await workspace.CurrentSolution.GetDocuments(rangeToFormat.Uri).Single().GetTextAsync();
38

39
            var results = await RunFormatDocumentRangeAsync(workspace.CurrentSolution, rangeToFormat);
40 41 42 43 44
            var actualText = ApplyTextEdits(results, documentText);
            Assert.Equal(expected, actualText);
        }

        private static async Task<LSP.TextEdit[]> RunFormatDocumentRangeAsync(Solution solution, LSP.Location location)
D
David Barbet 已提交
45
            => await GetLanguageServer(solution).ExecuteRequestAsync<LSP.DocumentRangeFormattingParams, LSP.TextEdit[]>(LSP.Methods.TextDocumentRangeFormattingName,
46
                CreateDocumentRangeFormattingParams(location), new LSP.ClientCapabilities(), null, CancellationToken.None);
47 48 49 50 51 52 53 54 55 56 57 58 59

        private static LSP.DocumentRangeFormattingParams CreateDocumentRangeFormattingParams(LSP.Location location)
            => new LSP.DocumentRangeFormattingParams()
            {
                Range = location.Range,
                TextDocument = CreateTextDocumentIdentifier(location.Uri),
                Options = new LSP.FormattingOptions()
                {
                    // TODO - Format should respect formatting options.
                }
            };
    }
}