Skip to content

Code clean up #26802

Merged
JieCarolHu merged 46 commits intodotnet:masterfrom
JieCarolHu:codeCleanUp_master
Jun 6, 2018
Merged

Code clean up #26802
JieCarolHu merged 46 commits intodotnet:masterfrom
JieCarolHu:codeCleanUp_master

Conversation

@JieCarolHu
Copy link
Copy Markdown
Contributor

@JieCarolHu JieCarolHu commented May 11, 2018

Code cleanup with Format document button (Ctrl+k, d)

Customer scenario

When user ctrl+k, d on a C# document, it brings up the code cleanup info bar to ask user to configure the code cleanup options. Once user configures the options, next time they click ctrl+k, d, it will clean up using the enabled fixers and format the document.

Bugs this fixes

NA

Workarounds, if any

NA

Risk

Low, it only affects the scenario of ctrl+k, d.

Performance impact

Low, it only affects the scenario of ctrl+k, d. It adds some time for the ctrl+k,d if user configured a lot of fixers

Is this a regression from a previous update?

NA

Root cause analysis

NA

How was the bug found?

NA

Test documentation updated?

No

JieCarolHu and others added 15 commits April 23, 2018 14:40
1. put open file editorconfig change tracking back
2. handle open file leaking when solution close with files opened case
3. don't share locks between editorconfig events and roslyn events
add editorconfig related changes
…than fix everything in the document. also not apply changes to buffer until everything is done rather than doing it per fix.
changed code fix part to use "fix all" and only fixes P1 list rather …
add expression body as one of fix to offer
@JieCarolHu JieCarolHu added the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label May 11, 2018
@JieCarolHu JieCarolHu requested a review from a team as a code owner May 11, 2018 18:36
<Reference Include="System.Core" />
<PackageReference Include="Microsoft.VisualStudio.Shell.15.0" Version="15.0.26730-alpha" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Framework" Version="15.0.26730-alpha" />
<PackageReference Include="Microsoft.VisualStudio.TextManager.Interop" Version="7.10.6070" />
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonmalinowski Is it ok to reference these here from this package? Will this be a concern at all for vs4mac?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, that can't go there. But this is PR for Personal Review, so at this point @JieCarolHu is still getting things working.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

<value>Code cleanup is not configured</value>
</data>
<data name="Config_it_now" xml:space="preserve">
<value>Config it now</value>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a good phrase :) Perhaps "Configure it now" instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

<value>Config it now</value>
</data>
<data name="Donot_show_this_again" xml:space="preserve">
<value>Don't show this again</value>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name does not match actual value. Also, in general, abbreviations are discouraged from localized messages. So this should be "Do not show this message again".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


namespace Microsoft.CodeAnalysis.Editor.Implementation.Formatting
{
[Export]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the direct export?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Implementation.Formatting
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the CodeCleanupService in the Formatting namespace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

internal class CodeCleanupService : ICodeCleanupService
{
private static Lazy<IDictionary<PerLanguageOption<bool>, string[]>> _dictionary
= new Lazy<IDictionary<PerLanguageOption<bool>, string[]>>(GetCodeCleanupOptionMapping);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a comment explaining what this is for. it seems very weird htat an option would map statically to any data, given that an option generally represents mutable data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

Oh, this is for personal review. will wait until this is ready for comments.


namespace Microsoft.CodeAnalysis.Editor.Implementation.Formatting
{
public interface ICodeStyleConfigureService : IWorkspaceService
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you probably not want this to be public type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


namespace Microsoft.CodeAnalysis.Editor.Implementation.Formatting
{
interface ICodeCleanupService
Copy link
Copy Markdown
Contributor

@heejaechang heejaechang May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifier?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

return document;
}

private List<string> GetEnabledDiagnosticIds(DocumentOptionSet docOptions)
Copy link
Copy Markdown
Contributor

@heejaechang heejaechang Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEnumerable < string >

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
var diagnosticIds = new List<string>();

foreach (var codeCleanupOptions in _optionDiagnosticsMappings.Keys)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(var kv in _optionDia....)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does tuple deconstruct works with keyvaluepair?


foreach (var codeCleanupOptions in _optionDiagnosticsMappings.Keys)
{
if (docOptions.GetOption(codeCleanupOptions))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetOptions(kv.key)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
if (docOptions.GetOption(codeCleanupOptions))
{
diagnosticIds.AddRange(_optionDiagnosticsMappings[codeCleanupOptions]);
Copy link
Copy Markdown
Contributor

@heejaechang heejaechang Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach(var id in kv.Value) yield id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, is ordering matter? dictionary doesn't maintain ordering of its entries? you might want to just use ImmutableArray((option option, Immutablearray ids)) rather than dictionary since it looks like you won't actually need indexer access for your dictionary.


private static ImmutableDictionary<PerLanguageOption<bool>, ImmutableArray<string>> GetCodeCleanupOptionMapping()
{
var dictionary = new Dictionary<PerLanguageOption<bool>, ImmutableArray<string>>()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use ImmutableDictionaryBuilder rather than use Dictionary and do ToImmutableDictionary? is it to use built in dictionary object intializer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i don't find a way to use dictionary initializer for Immutable dictionary

<GroupBox x:Name="FormatDocumentSettingsGroupBox" x:Uid="FormatDocumentSettingsGroupBox" >
<StackPanel x:Uid="StackPanel_2">
<CheckBox x:Name="AreCodeCleanupRulesConfiguredCheckBox" x:Uid="AreCodeCleanupRulesConfiguredCheckBox" Visibility="Hidden" Height="0"/>
<CheckBox x:Name="AllCSharpFormattingRulesCheckBox" x:Uid="AllCSharpFormattingRulesCheckBox" IsEnabled="False" IsChecked="True"/>
Copy link
Copy Markdown
Contributor

@heejaechang heejaechang Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need this option? user can't change it anyway. and hard to distinguish it with outer clean up since those will call format for their own purpose. it will be impossible for users to know why format is called anyway by whom.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checkbox is called out (always checked, not allow to change) in @kuhlenh 's design note.

</GroupBox>
<GroupBox x:Name="FormatDocumentSettingsGroupBox" x:Uid="FormatDocumentSettingsGroupBox" >
<StackPanel x:Uid="StackPanel_2">
<CheckBox x:Name="AreCodeCleanupRulesConfiguredCheckBox" x:Uid="AreCodeCleanupRulesConfiguredCheckBox" Visibility="Hidden" Height="0"/>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there reason this hidden checkbox is here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

FixLanguageFeaturesCheckBox.Content = CSharpVSResources.Fix_language_features;

// IsCodeCleanupConfiguredCheckBox is hidden all the time, and it tracks if the user ever configured the code cleanup
BindToOption(AreCodeCleanupRulesConfiguredCheckBox, CodeCleanupOptions.AreCodeCleanupRulesConfigured, LanguageNames.CSharp);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't need to do this but use the Option straight when needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

internal void SetCodeCleanupAsConfigured()
{
// AreCodeCleanupConfiguredCheckBox is hidden all the time, and it tracks if the user ever configured the code cleanup
if (!AreCodeCleanupRulesConfiguredCheckBox.IsChecked.HasValue || AreCodeCleanupRulesConfiguredCheckBox.IsChecked.Value == false)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use "CodeCleanupOptions.AreCodeCleanupRulesConfigured" directly?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, if user click "cancel" then we repeatedly show the info bar to users? we might want to split infobar display checking and whether user opt-in to use code clean to 2 different options.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@heejaechang
Copy link
Copy Markdown
Contributor

heejaechang commented Jun 4, 2018 via email

@JieCarolHu
Copy link
Copy Markdown
Contributor Author

retest windows_release_vs-integration_prtest please

@JieCarolHu
Copy link
Copy Markdown
Contributor Author

retest windows_debug_spanish_unit32_prtest please

return GetFixesAsync(document, range, includeSuppressionFixes, diagnosticId: null, cancellationToken);
}

private async Task<ImmutableArray<CodeFixCollection>> GetFixesAsync(Document document, TextSpan range, bool includeSuppressionFixes, string diagnosticId, CancellationToken cancellationToken)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diagnosticId [](start = 148, length = 12)

Nit: Rename to diagnosticIdOpt

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

/// this can be expensive since it is force analyzing diagnostics if it doesn't have up-to-date one yet.
/// </summary>
Task<IEnumerable<DiagnosticData>> GetDiagnosticsForSpanAsync(Document document, TextSpan range, bool includeSuppressedDiagnostics = false, CancellationToken cancellationToken = default);
Task<IEnumerable<DiagnosticData>> GetDiagnosticsForSpanAsync(Document document, TextSpan range, bool includeSuppressedDiagnostics = false, string diagnosticId = null, CancellationToken cancellationToken = default);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diagnosticId [](start = 154, length = 12)

Rename to diagnosticIdOpt and also add comment explaining the semantics for null and non-null value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@JieCarolHu
Copy link
Copy Markdown
Contributor Author

created #27497 to add more tests

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Formatting
{
[UseExportProvider]
public class CodeCleanupTests
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeCleanupTests [](start = 17, length = 16)

Synced up with @carhu@microsoft.com offline and she plans to extend the tests, but probably not as part of this PR due to time constraints. I think we should at least have the following tests:

  1. Simple unit tests, one per option, that verify code cleanup works fine with only single option enabled.
  2. Unit tests with multiple options enabled, but the reported diagnostics are not conflicting, hence can all be fixed.
  3. Unit tests with multiple options enabled, where reported diagnostics have conflicting spans and hence not all can be fixed. Is this possible for our currently chosen diagnostics/fixers?
  4. Verify options persistence after changing the workspace option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, copied into #27497

nameof(CodeCleanupOptions), nameof(AreCodeCleanupRulesConfigured), defaultValue: false,
storageLocations: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Are Code Cleanup Rules Configured"));

public static readonly PerLanguageOption<bool> NeverShowCodeCleanupInfoBarAgain = new PerLanguageOption<bool>(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NeverShowCodeCleanupInfoBarAgain [](start = 55, length = 32)

Question: Which options go into AutomationObject and which don't? @heejaechang@outlook.com probably knows the answer..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tracked in #27511

Copy link
Copy Markdown
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@JieCarolHu
Copy link
Copy Markdown
Contributor Author

@jinujoseph could you approve this?

@jinujoseph
Copy link
Copy Markdown
Contributor

Approved to merge for 15.8.Preview3

@jinujoseph
Copy link
Copy Markdown
Contributor

test windows_release_vs-integration_prtest

@JieCarolHu JieCarolHu merged commit d3a28d4 into dotnet:master Jun 6, 2018
@JieCarolHu JieCarolHu deleted the codeCleanUp_master branch June 13, 2018 02:18
@JieCarolHu JieCarolHu restored the codeCleanUp_master branch June 28, 2018 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants