forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#51911 from dibarbet/intents
Add intellicode API for generating code action edits from intents and
- Loading branch information
Showing
20 changed files
with
662 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
205 changes: 205 additions & 0 deletions
205
src/EditorFeatures/CSharpTest/Intents/GenerateConstructorIntentTests.cs
This file contains 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,205 @@ | ||
// 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 System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CSharp.CodeStyle; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests.Extensions; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; | ||
using Microsoft.CodeAnalysis.ExternalAccess.IntelliCode.Api; | ||
using Microsoft.CodeAnalysis.Features.Intents; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CodeAnalysis.Text.Shared.Extensions; | ||
using Microsoft.VisualStudio.Text; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Intents | ||
{ | ||
[UseExportProvider] | ||
public class GenerateConstructorIntentTests | ||
{ | ||
[Fact] | ||
public async Task GenerateConstructorSimpleResult() | ||
{ | ||
var initialText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
{|typed:public C|} | ||
}"; | ||
var expectedText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
public C(int someInt) | ||
{ | ||
_someInt = someInt; | ||
} | ||
}"; | ||
|
||
await VerifyExpectedTextAsync(initialText, expectedText).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateConstructorTypedPrivate() | ||
{ | ||
var initialText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
{|typed:private C|} | ||
}"; | ||
var expectedText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
public C(int someInt) | ||
{ | ||
_someInt = someInt; | ||
} | ||
}"; | ||
|
||
await VerifyExpectedTextAsync(initialText, expectedText).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateConstructorWithFieldsInPartial() | ||
{ | ||
var initialText = | ||
@"partial class C | ||
{ | ||
{|typed:public C|} | ||
}"; | ||
var additionalDocuments = new string[] | ||
{ | ||
@"partial class C | ||
{ | ||
private readonly int _someInt; | ||
}" | ||
}; | ||
var expectedText = | ||
@"partial class C | ||
{ | ||
public C(int someInt) | ||
{ | ||
_someInt = someInt; | ||
} | ||
}"; | ||
|
||
await VerifyExpectedTextAsync(initialText, additionalDocuments, expectedText).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateConstructorWithReferenceType() | ||
{ | ||
var initialText = | ||
@"class C | ||
{ | ||
private readonly object _someObject; | ||
{|typed:public C|} | ||
}"; | ||
var expectedText = | ||
@"class C | ||
{ | ||
private readonly object _someObject; | ||
public C(object someObject) | ||
{ | ||
_someObject = someObject; | ||
} | ||
}"; | ||
|
||
await VerifyExpectedTextAsync(initialText, expectedText).ConfigureAwait(false); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateConstructorWithExpressionBodyOption() | ||
{ | ||
var initialText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
{|typed:public C|} | ||
}"; | ||
var expectedText = | ||
@"class C | ||
{ | ||
private readonly int _someInt; | ||
public C(int someInt) => _someInt = someInt; | ||
}"; | ||
|
||
await VerifyExpectedTextAsync(initialText, expectedText, | ||
options: new OptionsCollection(LanguageNames.CSharp) | ||
{ | ||
{ CSharpCodeStyleOptions.PreferExpressionBodiedConstructors, CSharpCodeStyleOptions.WhenPossibleWithSilentEnforcement } | ||
}).ConfigureAwait(false); | ||
} | ||
|
||
private static Task VerifyExpectedTextAsync(string markup, string expectedText, OptionsCollection? options = null) | ||
{ | ||
return VerifyExpectedTextAsync(markup, new string[] { }, expectedText, options); | ||
} | ||
|
||
private static async Task VerifyExpectedTextAsync(string activeDocument, string[] additionalDocuments, string expectedText, OptionsCollection? options = null) | ||
{ | ||
var documentSet = additionalDocuments.Prepend(activeDocument).ToArray(); | ||
using var workspace = TestWorkspace.CreateCSharp(documentSet, exportProvider: EditorTestCompositions.EditorFeatures.ExportProviderFactory.CreateExportProvider()); | ||
if (options != null) | ||
{ | ||
workspace.ApplyOptions(options!); | ||
} | ||
|
||
var intentSource = workspace.ExportProvider.GetExportedValue<IIntentSourceProvider>(); | ||
|
||
// The first document will be the active document. | ||
var document = workspace.Documents.Single(d => d.Name == "test1.cs"); | ||
var textBuffer = document.GetTextBuffer(); | ||
var annotatedSpan = document.AnnotatedSpans["typed"].Single(); | ||
|
||
// Get the current snapshot span and selection. | ||
var currentSelectedSpan = document.SelectedSpans.FirstOrDefault(); | ||
if (currentSelectedSpan.IsEmpty) | ||
{ | ||
currentSelectedSpan = TextSpan.FromBounds(annotatedSpan.End, annotatedSpan.End); | ||
} | ||
|
||
var currentSnapshotSpan = new SnapshotSpan(textBuffer.CurrentSnapshot, currentSelectedSpan.ToSpan()); | ||
|
||
// Determine the edits to rewind to the prior snapshot by removing the changes in the annotated span. | ||
var rewindTextChange = new TextChange(annotatedSpan, ""); | ||
|
||
var intentContext = new IntentRequestContext( | ||
WellKnownIntents.GenerateConstructor, | ||
currentSnapshotSpan, | ||
ImmutableArray.Create(rewindTextChange), | ||
TextSpan.FromBounds(rewindTextChange.Span.Start, rewindTextChange.Span.Start), | ||
intentData: null); | ||
var results = await intentSource.ComputeIntentsAsync(intentContext, CancellationToken.None).ConfigureAwait(false); | ||
|
||
// For now, we're just taking the first result to match intellicode behavior. | ||
var result = results.First(); | ||
|
||
using var edit = textBuffer.CreateEdit(); | ||
foreach (var change in result.TextChanges) | ||
{ | ||
edit.Replace(change.Span.ToSpan(), change.NewText); | ||
} | ||
edit.Apply(); | ||
|
||
Assert.Equal(expectedText, textBuffer.CurrentSnapshot.GetText()); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/EditorFeatures/Core/ExternalAccess/IntelliCode/Api/IIntentSourceProvider.cs
This file contains 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,97 @@ | ||
// 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.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Features.Intents; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.VisualStudio.Text; | ||
|
||
namespace Microsoft.CodeAnalysis.ExternalAccess.IntelliCode.Api | ||
{ | ||
internal interface IIntentSourceProvider | ||
{ | ||
/// <summary> | ||
/// For an input intent, computes the edits required to apply that intent and returns them. | ||
/// </summary> | ||
/// <param name="context">the intents with the context in which the intent was found.</param> | ||
/// <returns>the edits that should be applied to the current snapshot.</returns> | ||
Task<ImmutableArray<IntentSource>> ComputeIntentsAsync(IntentRequestContext context, CancellationToken cancellationToken = default); | ||
} | ||
|
||
/// <summary> | ||
/// Defines the data needed to compute the code action edits from an intent. | ||
/// </summary> | ||
internal readonly struct IntentRequestContext | ||
{ | ||
/// <summary> | ||
/// The intent name. <see cref="WellKnownIntents"/> contains all intents roslyn knows how to handle. | ||
/// </summary> | ||
public string IntentName { get; } | ||
|
||
/// <summary> | ||
/// JSON formatted data specific to the intent that must be deserialized into the appropriate object. | ||
/// </summary> | ||
public string? IntentData { get; } | ||
|
||
/// <summary> | ||
/// The text snapshot and selection when <see cref="IIntentSourceProvider.ComputeIntentsAsync"/> | ||
/// was called to compute the text edits and against which the resulting text edits will be calculated. | ||
/// </summary> | ||
public SnapshotSpan CurrentSnapshotSpan { get; } | ||
|
||
/// <summary> | ||
/// The text edits that should be applied to the <see cref="CurrentSnapshotSpan"/> to calculate | ||
/// a prior text snapshot before the intent happened. The snapshot is used to calculate the actions. | ||
/// </summary> | ||
public ImmutableArray<TextChange> PriorTextEdits { get; } | ||
|
||
/// <summary> | ||
/// The caret position / selection in the snapshot calculated by applying | ||
/// <see cref="PriorTextEdits"/> to the <see cref="CurrentSnapshotSpan"/> | ||
/// </summary> | ||
public TextSpan PriorSelection { get; } | ||
|
||
public IntentRequestContext(string intentName, SnapshotSpan currentSnapshotSpan, ImmutableArray<TextChange> textEditsToPrior, TextSpan priorSelection, string? intentData) | ||
{ | ||
IntentName = intentName ?? throw new ArgumentNullException(nameof(intentName)); | ||
IntentData = intentData; | ||
CurrentSnapshotSpan = currentSnapshotSpan; | ||
PriorTextEdits = textEditsToPrior; | ||
PriorSelection = priorSelection; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Defines the text changes needed to apply an intent. | ||
/// </summary> | ||
internal readonly struct IntentSource | ||
{ | ||
/// <summary> | ||
/// The title associated with this intent result. | ||
/// </summary> | ||
public readonly string Title { get; } | ||
|
||
/// <summary> | ||
/// The text changes that should be applied to the <see cref="IntentRequestContext.CurrentSnapshotSpan"/> | ||
/// </summary> | ||
public readonly ImmutableArray<TextChange> TextChanges { get; } | ||
|
||
/// <summary> | ||
/// Contains metadata that can be used to identify the kind of sub-action these edits | ||
/// apply to for the requested intent. Used for telemetry purposes only. | ||
/// For example, the code action type name like FieldDelegatingCodeAction. | ||
/// </summary> | ||
public readonly string ActionName { get; } | ||
|
||
public IntentSource(string title, ImmutableArray<TextChange> textChanges, string actionName) | ||
{ | ||
TextChanges = textChanges; | ||
Title = title ?? throw new ArgumentNullException(nameof(title)); | ||
ActionName = actionName ?? throw new ArgumentNullException(nameof(actionName)); | ||
} | ||
} | ||
} |
Oops, something went wrong.