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

Add type hints for collection expressions #74051

Merged
merged 6 commits into from
Jun 21, 2024
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 @@ -70,7 +70,8 @@ protected override ITaggerEventSource CreateEventSource(ITextView textView, ITex
option.Equals(InlineHintsOptionsStorage.EnabledForTypes) ||
option.Equals(InlineHintsOptionsStorage.ForImplicitVariableTypes) ||
option.Equals(InlineHintsOptionsStorage.ForLambdaParameterTypes) ||
option.Equals(InlineHintsOptionsStorage.ForImplicitObjectCreation)));
option.Equals(InlineHintsOptionsStorage.ForImplicitObjectCreation) ||
option.Equals(InlineHintsOptionsStorage.ForCollectionExpressions)));
}

protected override void AddSpansToTag(ITextView? textView, ITextBuffer subjectBuffer, ref TemporaryArray<SnapshotSpan> result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public CSharpInlineTypeHintsService()
bool forImplicitVariableTypes,
bool forLambdaParameterTypes,
bool forImplicitObjectCreation,
bool forCollectionExpressions,
CancellationToken cancellationToken)
{
if (forImplicitVariableTypes || displayAllOverride)
Expand Down Expand Up @@ -101,6 +102,19 @@ public CSharpInlineTypeHintsService()
}
}

if (forCollectionExpressions || displayAllOverride)
{
if (node is CollectionExpressionSyntax collectionExpression)
{
var type = semanticModel.GetTypeInfo(collectionExpression, cancellationToken).ConvertedType;
if (IsValidType(type))
{
var span = new TextSpan(collectionExpression.OpenBracketToken.SpanStart, 0);
return new(type, span, new TextChange(span, type.ToDisplayString(s_minimalTypeStyle)), leadingSpace: true);
}
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal abstract class AbstractInlineTypeHintsService : IInlineTypeHintsService
bool forImplicitVariableTypes,
bool forLambdaParameterTypes,
bool forImplicitObjectCreation,
bool forCollectionExpressions,
CancellationToken cancellationToken);

public async Task<ImmutableArray<InlineHint>> GetInlineHintsAsync(
Expand All @@ -42,7 +43,8 @@ public async Task<ImmutableArray<InlineHint>> GetInlineHintsAsync(
var forImplicitVariableTypes = enabledForTypes && options.ForImplicitVariableTypes;
var forLambdaParameterTypes = enabledForTypes && options.ForLambdaParameterTypes;
var forImplicitObjectCreation = enabledForTypes && options.ForImplicitObjectCreation;
if (!forImplicitVariableTypes && !forLambdaParameterTypes && !forImplicitObjectCreation && !displayAllOverride)
var forCollectionExpressions = enabledForTypes && options.ForCollectionExpressions;
if (!forImplicitVariableTypes && !forLambdaParameterTypes && !forImplicitObjectCreation && !forCollectionExpressions && !displayAllOverride)
return [];

var anonymousTypeService = document.GetRequiredLanguageService<IStructuralTypeDisplayService>();
Expand All @@ -59,6 +61,7 @@ public async Task<ImmutableArray<InlineHint>> GetInlineHintsAsync(
forImplicitVariableTypes,
forLambdaParameterTypes,
forImplicitObjectCreation,
forCollectionExpressions,
cancellationToken);
if (hintOpt == null)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal readonly record struct InlineTypeHintsOptions
[DataMember] public bool ForImplicitVariableTypes { get; init; } = true;
[DataMember] public bool ForLambdaParameterTypes { get; init; } = true;
[DataMember] public bool ForImplicitObjectCreation { get; init; } = true;
[DataMember] public bool ForCollectionExpressions { get; init; } = true;

public InlineTypeHintsOptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static InlineTypeHintsOptions GetInlineTypeHintsOptions(this IGlobalOptio
ForImplicitVariableTypes = globalOptions.GetOption(ForImplicitVariableTypes, language),
ForLambdaParameterTypes = globalOptions.GetOption(ForLambdaParameterTypes, language),
ForImplicitObjectCreation = globalOptions.GetOption(ForImplicitObjectCreation, language),
ForCollectionExpressions = globalOptions.GetOption(ForCollectionExpressions, language),
};

// Note: inlay hints is the term used in LSP, we Want to use the LSP name when communicate with the LSP client.
Expand Down Expand Up @@ -105,5 +106,10 @@ public static InlineTypeHintsOptions GetInlineTypeHintsOptions(this IGlobalOptio
new("csharp_enable_inlay_hints_for_implicit_object_creation",
defaultValue: InlineTypeHintsOptions.Default.ForImplicitObjectCreation,
group: s_inlayHintOptionGroup);

public static readonly PerLanguageOption2<bool> ForCollectionExpressions =
new("csharp_enable_inlay_hints_for_collection_expressions",
defaultValue: InlineTypeHintsOptions.Default.ForCollectionExpressions,
group: s_inlayHintOptionGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal partial class DidChangeConfigurationNotificationHandler
InlineHintsOptionsStorage.ForImplicitVariableTypes,
InlineHintsOptionsStorage.ForLambdaParameterTypes,
InlineHintsOptionsStorage.ForImplicitObjectCreation,
InlineHintsOptionsStorage.ForCollectionExpressions,
FormattingOptions2.TabSize,
FormattingOptions2.IndentationSize,
FormattingOptions2.UseTabs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ private void OnOptionChanged(object? sender, OptionChangedEventArgs e)
option.Equals(InlineHintsOptionsStorage.EnabledForTypes) ||
option.Equals(InlineHintsOptionsStorage.ForImplicitVariableTypes) ||
option.Equals(InlineHintsOptionsStorage.ForLambdaParameterTypes) ||
option.Equals(InlineHintsOptionsStorage.ForImplicitObjectCreation)))
option.Equals(InlineHintsOptionsStorage.ForImplicitObjectCreation) ||
option.Equals(InlineHintsOptionsStorage.ForCollectionExpressions)))
{
EnqueueRefreshNotification(documentUri: null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void VerifyLspClientOptionNames()
"inlay_hints.csharp_enable_inlay_hints_for_implicit_variable_types",
"inlay_hints.csharp_enable_inlay_hints_for_lambda_parameter_types",
"inlay_hints.csharp_enable_inlay_hints_for_implicit_object_creation",
"inlay_hints.csharp_enable_inlay_hints_for_collection_expressions",
"code_style.formatting.indentation_and_spacing.tab_width",
"code_style.formatting.indentation_and_spacing.indent_size",
"code_style.formatting.indentation_and_spacing.indent_style",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@
<CheckBox x:Uid="ShowHintsForImplicitObjectCreation"
x:Name="ShowHintsForImplicitObjectCreation"
Content="{x:Static local:AdvancedOptionPageStrings.Option_Show_hints_for_implicit_object_creation}" />
<CheckBox x:Uid="ShowHintsForCollectionExpressions"
x:Name="ShowHintsForCollectionExpressions"
Content="{x:Static local:AdvancedOptionPageStrings.Option_Show_hints_for_collection_expressions}" />
</StackPanel>
</StackPanel>
</GroupBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public AdvancedOptionPageControl(OptionStore optionStore, IComponentModel compon
BindToOption(ShowHintsForVariablesWithInferredTypes, InlineHintsOptionsStorage.ForImplicitVariableTypes, LanguageNames.CSharp);
BindToOption(ShowHintsForLambdaParameterTypes, InlineHintsOptionsStorage.ForLambdaParameterTypes, LanguageNames.CSharp);
BindToOption(ShowHintsForImplicitObjectCreation, InlineHintsOptionsStorage.ForImplicitObjectCreation, LanguageNames.CSharp);
BindToOption(ShowHintsForCollectionExpressions, InlineHintsOptionsStorage.ForCollectionExpressions, LanguageNames.CSharp);

// Inheritance Margin
// Leave the null converter here to make sure if the option value is get from the storage (if it is null), the feature will be enabled
Expand Down Expand Up @@ -244,6 +245,7 @@ private void UpdateInlineHintsOptions()
ShowHintsForVariablesWithInferredTypes.IsEnabled = enabledForTypes;
ShowHintsForLambdaParameterTypes.IsEnabled = enabledForTypes;
ShowHintsForImplicitObjectCreation.IsEnabled = enabledForTypes;
ShowHintsForCollectionExpressions.IsEnabled = enabledForTypes;
}

private void DisplayInlineParameterNameHints_Checked(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public static string Option_Show_hints_for_lambda_parameter_types
public static string Option_Show_hints_for_implicit_object_creation
=> ServicesVSResources.Show_hints_for_implicit_object_creation;

public static string Option_Show_hints_for_collection_expressions
=> ServicesVSResources.Show_hints_for_collection_expressions;

public static string Option_Display_diagnostics_inline_experimental
=> ServicesVSResources.Display_diagnostics_inline_experimental;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ public bool TryFetch(LocalUserRegistryOptionPersister persister, OptionKey2 opti
{"dotnet_enable_inlay_hints_for_parameters", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineParameterNameHints")},
{"csharp_enable_inlay_hints_for_types", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineTypeHints")},
{"csharp_enable_inlay_hints_for_implicit_object_creation", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineTypeHints.ForImplicitObjectCreation")},
{"csharp_enable_inlay_hints_for_collection_expressions", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineTypeHints.ForCollectionExpressions")},
{"csharp_enable_inlay_hints_for_implicit_variable_types", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineTypeHints.ForImplicitVariableTypes")},
{"dotnet_enable_inlay_hints_for_indexer_parameters", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineParameterNameHints.ForArrayIndexers")},
{"csharp_enable_inlay_hints_for_lambda_parameter_types", new RoamingProfileStorage("TextEditor.%LANGUAGE%.Specific.InlineTypeHints.ForLambdaParameterTypes")},
Expand Down
3 changes: 3 additions & 0 deletions src/VisualStudio/Core/Def/ServicesVSResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1936,4 +1936,7 @@ Additional information: {1}</value>
<data name="Prefer_static_anonymous_functions" xml:space="preserve">
<value>Prefer static anonymous functions</value>
</data>
<data name="Show_hints_for_collection_expressions" xml:space="preserve">
<value>Show hints for collection expressions</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading