-
Notifications
You must be signed in to change notification settings - Fork 4.2k
On-the-fly-docs Pass along additional context #77510
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
Merged
akhera99
merged 12 commits into
dotnet:main
from
akhera99:dev/ankitakhera/additional_context_otfd
Mar 18, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1537484
wip
akhera99 dac602a
works end to end
akhera99 160b756
Merge branch 'main' into dev/ankitakhera/additional_context_otfd
akhera99 34451ee
some feedback
akhera99 eb5cdd9
feedback
akhera99 b5fb070
fix bugs
akhera99 f8c05d9
Merge branch 'main' into dev/ankitakhera/additional_context_otfd
akhera99 c2d27d7
tests
akhera99 7c1c823
revert settings
akhera99 b396e6e
fix code analysis service
akhera99 7c942c2
fix formatting
akhera99 cff1b42
Feedback
akhera99 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
This file contains hidden or 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
130 changes: 130 additions & 0 deletions
130
src/EditorFeatures/CSharpTest/OnTheFlyDocs/OnTheFlyDocsUtilitiesTests.cs
This file contains hidden or 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,130 @@ | ||
| // 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. | ||
|
|
||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis.CSharp.QuickInfo; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.OnTheFlyDocs; | ||
|
|
||
| [UseExportProvider] | ||
| [Trait(Traits.Feature, Traits.Features.OnTheFlyDocs)] | ||
| public sealed class OnTheFlyDocsUtilitiesTests | ||
| { | ||
| [Fact] | ||
| public async Task TestAdditionalContextNoContext() | ||
| { | ||
| var testCode = """ | ||
| class C | ||
| { | ||
| void AddMethod(int a, int b) | ||
| { | ||
| return a + b; | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| using var workspace = EditorTestWorkspace.CreateCSharp(testCode); | ||
| var solution = workspace.CurrentSolution; | ||
| var document = solution.Projects.First().Documents.First(); | ||
|
|
||
| var syntaxTree = await document.GetSyntaxTreeAsync(); | ||
| var semanticModel = await document.GetSemanticModelAsync(); | ||
|
|
||
| var methodDeclaration = syntaxTree!.GetRoot() | ||
| .DescendantNodes() | ||
| .OfType<MethodDeclarationSyntax>() | ||
| .First(); | ||
|
|
||
| var methodSymbol = semanticModel!.GetDeclaredSymbol(methodDeclaration); | ||
|
|
||
| var result = OnTheFlyDocsUtilities.GetAdditionalOnTheFlyDocsContext(solution, methodSymbol!); | ||
| Assert.True(result.All(item => item == null)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestAdditionalContextWithTypeParameters() | ||
| { | ||
| var testCode = """ | ||
| class C | ||
| { | ||
| int AddMethod(A a, int b) | ||
| { | ||
| return a.x + b; | ||
| } | ||
| } | ||
|
|
||
| class A | ||
| { | ||
| public int x; | ||
| } | ||
| """; | ||
|
|
||
| using var workspace = EditorTestWorkspace.CreateCSharp(testCode); | ||
| var solution = workspace.CurrentSolution; | ||
| var document = solution.Projects.First().Documents.First(); | ||
|
|
||
| var syntaxTree = await document.GetSyntaxTreeAsync(); | ||
| var semanticModel = await document.GetSemanticModelAsync(); | ||
|
|
||
| var methodDeclaration = syntaxTree!.GetRoot() | ||
| .DescendantNodes() | ||
| .OfType<MethodDeclarationSyntax>() | ||
| .First(); | ||
|
|
||
| var methodSymbol = semanticModel!.GetDeclaredSymbol(methodDeclaration); | ||
|
|
||
| var result = OnTheFlyDocsUtilities.GetAdditionalOnTheFlyDocsContext(solution, methodSymbol!); | ||
| Assert.NotNull(result.First()); | ||
| Assert.Null(result.Last()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestAdditionalContextWithTypeArguments() | ||
| { | ||
| var testCode = """ | ||
| class C | ||
| { | ||
| void Method<T, U>(T t, U u) where T : class where U : struct | ||
| { | ||
| } | ||
|
|
||
| void CallMethod() | ||
| { | ||
| Method<CustomClass, CustomStruct>(new CustomClass(), new CustomStruct()); | ||
| } | ||
| } | ||
|
|
||
| class CustomClass | ||
| { | ||
| public string Name { get; set; } | ||
| } | ||
|
|
||
| struct CustomStruct | ||
| { | ||
| public int Value { get; set; } | ||
| } | ||
| """; | ||
|
|
||
| using var workspace = EditorTestWorkspace.CreateCSharp(testCode); | ||
| var solution = workspace.CurrentSolution; | ||
| var document = solution.Projects.First().Documents.First(); | ||
|
|
||
| var syntaxTree = await document.GetSyntaxTreeAsync(); | ||
| var semanticModel = await document.GetSemanticModelAsync(); | ||
|
|
||
| var methodInvocation = syntaxTree!.GetRoot() | ||
| .DescendantNodes() | ||
| .OfType<InvocationExpressionSyntax>() | ||
| .First(); | ||
|
|
||
| var methodSymbol = semanticModel!.GetSymbolInfo(methodInvocation).Symbol; | ||
|
|
||
| var result = OnTheFlyDocsUtilities.GetAdditionalOnTheFlyDocsContext(solution, methodSymbol!); | ||
| Assert.True(result.All(item => item is not null)); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
src/Features/CSharp/Portable/QuickInfo/OnTheFlyDocsUtilities.cs
This file contains hidden or 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,50 @@ | ||
| // 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. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.QuickInfo; | ||
| using Microsoft.CodeAnalysis.Shared.Extensions; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.QuickInfo; | ||
|
|
||
| internal static class OnTheFlyDocsUtilities | ||
| { | ||
| public static ImmutableArray<OnTheFlyDocsRelevantFileInfo?> GetAdditionalOnTheFlyDocsContext(Solution solution, ISymbol symbol) | ||
| { | ||
| var parameters = symbol.GetParameters(); | ||
| var typeArguments = symbol.GetTypeArguments(); | ||
|
|
||
| var parameterStrings = parameters.Select(parameter => | ||
| { | ||
| var typeSymbol = parameter.Type; | ||
| return GetOnTheFlyDocsRelevantFileInfo(typeSymbol); | ||
|
|
||
| }).ToImmutableArray(); | ||
|
|
||
| var typeArgumentStrings = typeArguments.Select(typeArgument => | ||
| { | ||
| return GetOnTheFlyDocsRelevantFileInfo(typeArgument); | ||
|
|
||
| }).ToImmutableArray(); | ||
|
|
||
| return parameterStrings.AddRange(typeArgumentStrings); | ||
|
|
||
| OnTheFlyDocsRelevantFileInfo? GetOnTheFlyDocsRelevantFileInfo(ITypeSymbol typeSymbol) | ||
| { | ||
| var typeSyntaxReference = typeSymbol.DeclaringSyntaxReferences.FirstOrDefault(); | ||
| if (typeSyntaxReference is not null) | ||
| { | ||
| var typeSpan = typeSyntaxReference.Span; | ||
| var syntaxReferenceDocument = solution.GetDocument(typeSyntaxReference.SyntaxTree); | ||
| if (syntaxReferenceDocument is not null) | ||
| { | ||
| return new OnTheFlyDocsRelevantFileInfo(syntaxReferenceDocument, typeSpan); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
src/Features/Core/Portable/QuickInfo/OnTheFlyDocsRelevantFileInfo.cs
This file contains hidden or 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,20 @@ | ||
| // 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. | ||
|
|
||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.QuickInfo; | ||
|
|
||
| internal sealed record OnTheFlyDocsRelevantFileInfo | ||
| { | ||
| public Document Document { get; } | ||
| public TextSpan TextSpan { get; } | ||
|
|
||
| public OnTheFlyDocsRelevantFileInfo(Document document, TextSpan textSpan) | ||
| { | ||
| Document = document; | ||
| TextSpan = textSpan; | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.