-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for textDocument/implementation in LSP mode #1970
Merged
david-driscoll
merged 1 commit into
OmniSharp:master
from
arjenwitteveen:lsp-find-implementations
Oct 8, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh I can't believe it wasn't there already