Skip to content

Commit cebad41

Browse files
committed
Added FoldingRanges support to TextDocumentClient.
1 parent e7fdd68 commit cebad41

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

src/Client/Clients/TextDocumentClient.DocumentHighlight.cs renamed to src/Client/Clients/TextDocumentClient.DocumentHighlights.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public partial class TextDocumentClient
3030
/// <returns>
3131
/// A <see cref="Task{TResult}"/> that resolves to the completions or <c>null</c> if no document highlights are available at the specified position.
3232
/// </returns>
33-
public Task<DocumentHighlightContainer> DocumentHighlight(string filePath, int line, int column, CancellationToken cancellationToken = default(CancellationToken))
33+
public Task<DocumentHighlightContainer> DocumentHighlights(string filePath, int line, int column, CancellationToken cancellationToken = default(CancellationToken))
3434
{
3535
if (string.IsNullOrWhiteSpace(filePath))
3636
throw new ArgumentException($"Argument cannot be null, empty, or entirely composed of whitespace: {nameof(filePath)}.", nameof(filePath));
3737

3838
var documentUri = DocumentUri.FromFileSystemPath(filePath);
3939

40-
return DocumentHighlight(documentUri, line, column, cancellationToken);
40+
return DocumentHighlights(documentUri, line, column, cancellationToken);
4141
}
4242

4343
/// <summary>
@@ -58,7 +58,7 @@ public partial class TextDocumentClient
5858
/// <returns>
5959
/// A <see cref="Task{TResult}"/> that resolves to the completions or <c>null</c> if no document highlights are available at the specified position.
6060
/// </returns>
61-
public Task<DocumentHighlightContainer> DocumentHighlight(Uri documentUri, int line, int column, CancellationToken cancellationToken = default(CancellationToken))
61+
public Task<DocumentHighlightContainer> DocumentHighlights(Uri documentUri, int line, int column, CancellationToken cancellationToken = default(CancellationToken))
6262
{
6363
return PositionalRequest<DocumentHighlightContainer>(DocumentNames.DocumentHighlight, documentUri, line, column, cancellationToken);
6464
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using OmniSharp.Extensions.LanguageServer.Client.Utilities;
5+
using OmniSharp.Extensions.LanguageServer.Protocol;
6+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
7+
8+
namespace OmniSharp.Extensions.LanguageServer.Client.Clients
9+
{
10+
/// <summary>
11+
/// Client for the LSP Text Document API.
12+
/// </summary>
13+
public partial class TextDocumentClient
14+
{
15+
/// <summary>
16+
/// Request document folding ranges.
17+
/// </summary>
18+
/// <param name="filePath">
19+
/// The full file-system path of the text document.
20+
/// </param>
21+
/// <param name="cancellationToken">
22+
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
23+
/// </param>
24+
/// <returns>
25+
/// A <see cref="Task{TResult}"/> that resolves to the completions or <c>null</c> if no document highlights are available at the specified position.
26+
/// </returns>
27+
public Task<Container<FoldingRange>> FoldingRanges(string filePath, CancellationToken cancellationToken = default(CancellationToken))
28+
{
29+
if (string.IsNullOrWhiteSpace(filePath))
30+
throw new ArgumentException($"Argument cannot be null, empty, or entirely composed of whitespace: {nameof(filePath)}.", nameof(filePath));
31+
32+
var documentUri = DocumentUri.FromFileSystemPath(filePath);
33+
34+
return FoldingRanges(documentUri, cancellationToken);
35+
}
36+
37+
/// <summary>
38+
/// Request document highlights at the specified document position.
39+
/// </summary>
40+
/// <param name="documentUri">
41+
/// The document URI.
42+
/// </param>
43+
/// <param name="line">
44+
/// The target line (0-based).
45+
/// </param>
46+
/// <param name="column">
47+
/// The target column (0-based).
48+
/// </param>
49+
/// <param name="cancellationToken">
50+
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
51+
/// </param>
52+
/// <returns>
53+
/// A <see cref="Task{TResult}"/> that resolves to the completions or <c>null</c> if no document highlights are available at the specified position.
54+
/// </returns>
55+
public async Task<Container<FoldingRange>> FoldingRanges(Uri documentUri, CancellationToken cancellationToken = default(CancellationToken))
56+
{
57+
var request = new FoldingRangeRequestParam {
58+
TextDocument = new TextDocumentItem {
59+
Uri = documentUri
60+
}
61+
};
62+
63+
return await Client.SendRequest<Container<FoldingRange>>(DocumentNames.FoldingRange, request, cancellationToken).ConfigureAwait(false);
64+
}
65+
}
66+
}

test/Client.Tests/ClientTests.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public async Task Definition_Success()
320320
/// Ensure that the language client can successfully request DocumentHighlight.
321321
/// </summary>
322322
[Fact(DisplayName = "Language client can successfully request document highlights", Skip = "Periodic failures")]
323-
public async Task DocumentHighlight_Success()
323+
public async Task DocumentHighlights_Success()
324324
{
325325
await Connect();
326326

@@ -355,7 +355,7 @@ public async Task DocumentHighlight_Success()
355355
return Task.FromResult(expectedHighlights);
356356
});
357357

358-
var definitions = await LanguageClient.TextDocument.DocumentHighlight(AbsoluteDocumentPath, line, column);
358+
var definitions = await LanguageClient.TextDocument.DocumentHighlights(AbsoluteDocumentPath, line, column);
359359

360360
var actualDefinitions = definitions.ToArray();
361361
Assert.Collection(actualDefinitions, actualHighlight => {
@@ -373,6 +373,47 @@ public async Task DocumentHighlight_Success()
373373
});
374374
}
375375

376+
/// <summary>
377+
/// Ensure that the language client can successfully request FoldingRanges.
378+
/// </summary>
379+
[Fact(DisplayName = "Language client can successfully request document folding ranges", Skip = "Periodic failures")]
380+
public async Task FoldingRanges_Success()
381+
{
382+
await Connect();
383+
384+
var expectedDocumentPath = AbsoluteDocumentPath;
385+
var expectedDocumentUri = DocumentUri.FromFileSystemPath(expectedDocumentPath);
386+
387+
var expectedFoldingRanges = new Container<FoldingRange>(
388+
new FoldingRange {
389+
Kind = FoldingRangeKind.Region,
390+
StartLine = 5,
391+
StartCharacter = 1,
392+
EndLine = 7,
393+
EndCharacter = 2,
394+
});
395+
396+
ServerDispatcher.HandleRequest<FoldingRangeRequestParam, Container<FoldingRange>>(DocumentNames.FoldingRange, (request, cancellationToken) => {
397+
Assert.NotNull(request.TextDocument);
398+
Assert.Equal(expectedDocumentUri, request.TextDocument.Uri);
399+
return Task.FromResult(expectedFoldingRanges);
400+
});
401+
402+
var foldingRanges = await LanguageClient.TextDocument.FoldingRanges(AbsoluteDocumentPath);
403+
404+
var actualFoldingRanges = foldingRanges.ToArray();
405+
Assert.Collection(actualFoldingRanges, actualFoldingRange => {
406+
var expectedFoldingRange = expectedFoldingRanges.Single();
407+
408+
Assert.Equal(FoldingRangeKind.Region, expectedFoldingRange.Kind);
409+
410+
Assert.Equal(expectedFoldingRange.StartLine, actualFoldingRange.StartLine);
411+
Assert.Equal(expectedFoldingRange.StartCharacter, actualFoldingRange.StartCharacter);
412+
Assert.Equal(expectedFoldingRange.EndLine, actualFoldingRange.EndLine);
413+
Assert.Equal(expectedFoldingRange.EndCharacter, actualFoldingRange.EndCharacter);
414+
});
415+
}
416+
376417
/// <summary>
377418
/// Ensure that the language client can successfully receive Diagnostics from the server.
378419
/// </summary>

0 commit comments

Comments
 (0)