-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1970 from arjenwitteveen/lsp-find-implementations
Add support for textDocument/implementation in LSP mode
- Loading branch information
Showing
4 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpImplementationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using OmniSharp.Models; | ||
using OmniSharp.Models.FindImplementations; | ||
using static OmniSharp.LanguageServerProtocol.Helpers; | ||
|
||
namespace OmniSharp.LanguageServerProtocol.Handlers | ||
{ | ||
internal sealed class OmniSharpImplementationHandler : ImplementationHandler | ||
{ | ||
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers) | ||
{ | ||
foreach (var (selector, handler) in handlers | ||
.OfType<Mef.IRequestHandler<FindImplementationsRequest, QuickFixResponse>>()) | ||
if (handler != null) | ||
yield return new OmniSharpImplementationHandler(handler, selector); | ||
} | ||
|
||
private readonly Mef.IRequestHandler<FindImplementationsRequest, QuickFixResponse> _findImplementationsHandler; | ||
|
||
public OmniSharpImplementationHandler(Mef.IRequestHandler<FindImplementationsRequest, QuickFixResponse> findImplementationsHandler, DocumentSelector documentSelector) | ||
: base(new ImplementationRegistrationOptions() | ||
{ | ||
DocumentSelector = documentSelector | ||
}) | ||
{ | ||
_findImplementationsHandler = findImplementationsHandler; | ||
} | ||
|
||
public async override Task<LocationOrLocationLinks> Handle(ImplementationParams request, CancellationToken token) | ||
{ | ||
var omnisharpRequest = new FindImplementationsRequest() | ||
{ | ||
FileName = FromUri(request.TextDocument.Uri), | ||
Column = Convert.ToInt32(request.Position.Character), | ||
Line = Convert.ToInt32(request.Position.Line) | ||
}; | ||
|
||
var omnisharpResponse = await _findImplementationsHandler.Handle(omnisharpRequest); | ||
|
||
return omnisharpResponse?.QuickFixes?.Select(x => new LocationOrLocationLink(new Location | ||
{ | ||
Uri = ToUri(x.FileName), | ||
Range = ToRange((x.Column, x.Line)) | ||
})).ToArray() ?? Array.Empty<LocationOrLocationLink>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
tests/OmniSharp.Lsp.Tests/OmniSharpImplementationHandlerFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Extensions.LanguageServer.Protocol; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using TestUtility; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace OmniSharp.Lsp.Tests | ||
{ | ||
public class OmniSharpImplementationHandlerFacts : AbstractLanguageServerTestBase | ||
{ | ||
public OmniSharpImplementationHandlerFacts(ITestOutputHelper output) | ||
: base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfClass() | ||
{ | ||
const string code = @" | ||
public class Foo$$Base | ||
{ | ||
} | ||
public class FooDerivedA : FooBase | ||
{ | ||
} | ||
public class FooDerivedB : FooBase | ||
{ | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(3, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfInterface() | ||
{ | ||
const string code = @" | ||
public interface IF$$oo | ||
{ | ||
} | ||
public class FooA : IFoo | ||
{ | ||
} | ||
public class FooB : IFoo | ||
{ | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(2, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfVirtualFunction() | ||
{ | ||
const string code = @" | ||
public class FooBase | ||
{ | ||
public virtual int B$$ar() { return 1; } | ||
} | ||
public class FooDerivedA : FooBase | ||
{ | ||
public override int Bar() { return 2; } | ||
} | ||
public class FooDerivedB : FooBase | ||
{ | ||
public override int Bar() { return 3; } | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(3, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfAbstractFunction() | ||
{ | ||
const string code = @" | ||
public abstract class FooBase | ||
{ | ||
public abstract int B$$ar(); | ||
} | ||
public class FooDerivedA : FooBase | ||
{ | ||
public override int Bar() { return 2; } | ||
} | ||
public class FooDerivedB : FooBase | ||
{ | ||
public override int Bar() { return 3; } | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(2, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfVirtualProperty() | ||
{ | ||
const string code = @" | ||
public class FooBase | ||
{ | ||
public virtual int B$$ar => 1; | ||
} | ||
public class FooDerivedA : FooBase | ||
{ | ||
public override int Bar => 2; | ||
} | ||
public class FooDerivedB : FooBase | ||
{ | ||
public override int Bar => 3; | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(3, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CanFindImplementationsOfAbstractProperty() | ||
{ | ||
const string code = @" | ||
public abstract class FooBase | ||
{ | ||
public abstract int B$$ar { get; } | ||
} | ||
public class FooDerivedA : FooBase | ||
{ | ||
public override int Bar => 2; | ||
} | ||
public class FooDerivedB : FooBase | ||
{ | ||
public override int Bar => 3; | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Equal(2, implementations.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task CannotFindImplementationsWithoutSymbol() | ||
{ | ||
const string code = @" | ||
public class Foo | ||
{ | ||
$$ | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Empty(implementations); | ||
} | ||
|
||
[Fact] | ||
public async Task CannotFindImplementationsForUnsupportedSymbol() | ||
{ | ||
const string code = @" | ||
pub$$lic class Foo | ||
{ | ||
}"; | ||
|
||
var implementations = await FindImplementationsAsync(code); | ||
Assert.Empty(implementations); | ||
} | ||
|
||
[Fact] | ||
public async Task CannotFindImplementationsForEmptyFiles() | ||
{ | ||
var response = await Client.TextDocument.RequestImplementation(new ImplementationParams | ||
{ | ||
Position = (0, 0), | ||
TextDocument = "notfound.cs" | ||
}, CancellationToken); | ||
|
||
Assert.Empty(response); | ||
} | ||
|
||
private Task<LocationOrLocationLinks> FindImplementationsAsync(string code) | ||
{ | ||
return FindImplementationsAsync(new[] { new TestFile("dummy.cs", code) }); | ||
} | ||
|
||
private async Task<LocationOrLocationLinks> FindImplementationsAsync(TestFile[] testFiles) | ||
{ | ||
OmniSharpTestHost.AddFilesToWorkspace(testFiles | ||
.Select(f => | ||
new TestFile( | ||
((f.FileName.StartsWith("/") || f.FileName.StartsWith("\\")) ? f.FileName : ("/" + f.FileName)) | ||
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar), f.Content)) | ||
.ToArray() | ||
); | ||
|
||
var file = testFiles.Single(tf => tf.Content.HasPosition); | ||
var point = file.Content.GetPointFromPosition(); | ||
|
||
Client.TextDocument.DidChangeTextDocument(new DidChangeTextDocumentParams | ||
{ | ||
ContentChanges = new Container<TextDocumentContentChangeEvent>(new TextDocumentContentChangeEvent | ||
{ | ||
Text = file.Content.Code | ||
}), | ||
TextDocument = new VersionedTextDocumentIdentifier | ||
{ | ||
Uri = DocumentUri.From(file.FileName), | ||
Version = 1 | ||
} | ||
}); | ||
|
||
return await Client.TextDocument.RequestImplementation(new ImplementationParams | ||
{ | ||
Position = new Position(point.Line, point.Offset), | ||
TextDocument = new TextDocumentIdentifier(DocumentUri.From(file.FileName)) | ||
}, CancellationToken); | ||
} | ||
} | ||
} |