Skip to content

Commit

Permalink
Add usings on paste (#48501)
Browse files Browse the repository at this point in the history
Addresses #45853

Hook up paste command to also automatically add usings. Operation uses thread await dialog to pop up to a user if it is taking a while with a "cancel" button. The dialog will not always show, but follows the IUIThreadOperationContext scope rules

Edit:

This is disabled by default. The following followup issues were filed
#49442
#49443
#49444
  • Loading branch information
ryzngard authored Nov 20, 2020
1 parent 3fe6412 commit 8d278c2
Show file tree
Hide file tree
Showing 92 changed files with 913 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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;
using System.ComponentModel.Composition;
using Microsoft.CodeAnalysis.Editor.Implementation.AddImports;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Utilities;

namespace Microsoft.CodeAnalysis.Editor.CSharp.AddImports
{
[Export]
[Export(typeof(ICommandHandler))]
[ContentType(ContentTypeNames.CSharpContentType)]
[Name(PredefinedCommandHandlerNames.AddImportsPaste)]
// Order is important here, this command needs to execute before PasteTracking
// since it may modify the pasted span. Paste tracking dismisses if
// the span is modified. It doesn't need to be before FormatDocument, but
// this helps the order of execution be more constant in case there
// are problems that arise. This command will always execute the next
// command before doing operations.
[Order(After = PredefinedCommandHandlerNames.PasteTrackingPaste)]
[Order(Before = PredefinedCommandHandlerNames.FormatDocument)]
internal class CSharpAddImportsPasteCommandHandler : AbstractAddImportsPasteCommandHandler
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpAddImportsPasteCommandHandler(IThreadingContext threadingContext) : base(threadingContext)
{
}

public override string DisplayName => CSharpEditorResources.Add_Missing_Usings_on_Paste;
protected override string DialogText => CSharpEditorResources.Adding_missing_usings;
}
}
8 changes: 8 additions & 0 deletions src/EditorFeatures/CSharp/CSharpEditorResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,12 @@
<data name="Generate_Event_Subscription" xml:space="preserve">
<value>Generate Event Subscription</value>
</data>
<data name="Adding_missing_usings" xml:space="preserve">
<value>Adding missing usings...</value>
<comment>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</comment>
</data>
<data name="Add_Missing_Usings_on_Paste" xml:space="preserve">
<value>Add Missing Usings on Paste</value>
<comment>"usings" is a language specific term and should not be localized</comment>
</data>
</root>
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Zvolená verze: {0}</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Ausgewählte Version: {0}</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Versión elegida: "{0}"</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Version choisie : '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Versione selezionata: '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">選択されたバージョン: '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">선택한 버전: '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Wybrana wersja: „{0}”</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Versão escolhida: '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Выбранная версия: "{0}"</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="tr" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">Seçilen sürüm: '{0}'</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-HANS" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">所选版本: "{0}"</target>
Expand Down
10 changes: 10 additions & 0 deletions src/EditorFeatures/CSharp/xlf/CSharpEditorResources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-HANT" original="../CSharpEditorResources.resx">
<body>
<trans-unit id="Add_Missing_Usings_on_Paste">
<source>Add Missing Usings on Paste</source>
<target state="new">Add Missing Usings on Paste</target>
<note>"usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Adding_missing_usings">
<source>Adding missing usings...</source>
<target state="new">Adding missing usings...</target>
<note>Shown in a thread await dialog. "usings" is a language specific term and should not be localized</note>
</trans-unit>
<trans-unit id="Chosen_version_0">
<source>Chosen version: '{0}'</source>
<target state="translated">選擇的版本: '{0}'</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.ComponentModel.Composition;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.VisualStudio.Text;
Expand Down Expand Up @@ -56,9 +57,7 @@ public InlineHintsTaggerProvider(

public ITagger<T>? CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag
{
// Determining of the textView's buffer does not match the buffer in order to skip showing the hints for
// the interactive window
if (buffer != textView.TextBuffer)
if (textView.IsNotSurfaceBufferOfTextView(buffer))
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ internal static class PredefinedCommandHandlerNames
/// </summary>
public const string PasteTrackingPaste = "Paste Tracking Paste Command Handler";

/// <summary>
/// Command handler name for Paste in Add Imports.
/// </summary>
public const string AddImportsPaste = "Add Imports Paste Command Handler";

/// <summary>
/// Command handler name for Edit and Continue file save handler.
/// </summary>
Expand Down
Loading

0 comments on commit 8d278c2

Please sign in to comment.