Skip to content
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

Fix issue with highlight refs where we might highlight something not under the caret. #52795

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Microsoft.CodeAnalysis.Editor.Tagging;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.

Imports System.Threading
Imports Microsoft.CodeAnalysis.Editor.ReferenceHighlighting
Imports Microsoft.CodeAnalysis.Editor.Shared.Extensions
Imports Microsoft.CodeAnalysis.Editor.Shared.Options
Expand All @@ -12,7 +11,6 @@ Imports Microsoft.CodeAnalysis.Editor.Tagging
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Remote.Testing
Imports Microsoft.CodeAnalysis.Shared.TestHooks
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.VisualStudio.Text
Imports Roslyn.Utilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,5 +808,55 @@ class C

Await VerifyHighlightsAsync(input, testHost)
End Function

<WpfTheory>
<CombinatorialData>
Public Async Function TestNotOnNewInObjectCreation(testHost As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
namespace X
{
class B
{
public void M()
{
$$new B();
new B();
}
}
}
</Document>
</Project>
</Workspace>

Await VerifyHighlightsAsync(input, testHost)
End Function

<WpfTheory>
<CombinatorialData>
Public Async Function TestOnTypeInObjectCreation(testHost As TestHost) As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
namespace X
{
class {|Definition:B|}
{
public void M()
{
new $${|Reference:B|}();
new {|Reference:B|}();
}
}
}
</Document>
</Project>
</Workspace>

Await VerifyHighlightsAsync(input, testHost)
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,28 @@ private async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsInCu
var result = await TryGetEmbeddedLanguageHighlightsAsync(
document, position, documentsToSearch, cancellationToken).ConfigureAwait(false);
if (!result.IsDefaultOrEmpty)
{
return result;
}

var solution = document.Project.Solution;

var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(
semanticModel, position, solution.Workspace, cancellationToken).ConfigureAwait(false);
if (symbol == null)
{
return ImmutableArray<DocumentHighlights>.Empty;
}

// Get unique tags for referenced symbols
return await GetTagsForReferencedSymbolAsync(
var tags = await GetTagsForReferencedSymbolAsync(
symbol, document, documentsToSearch, cancellationToken).ConfigureAwait(false);

// Only accept these highlights if at least one of them actually intersected with the
// position the caller was asking for. For example, if the user had `$$new X();` then
// SymbolFinder will consider that the symbol `X`. However, the doc highlights won't include
// the `new` part, so it's not appropriate for us to highlight `X` in that case.
if (!tags.Any(t => t.HighlightSpans.Any(hs => hs.TextSpan.IntersectsWith(position))))
return ImmutableArray<DocumentHighlights>.Empty;

return tags;
}

private static async Task<ImmutableArray<DocumentHighlights>> TryGetEmbeddedLanguageHighlightsAsync(
Expand Down