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

Make code action's nested actions publicly accessible #71050

Closed
josefpihrt opened this issue Dec 1, 2023 · 5 comments · Fixed by #71327
Closed

Make code action's nested actions publicly accessible #71050

josefpihrt opened this issue Dec 1, 2023 · 5 comments · Fixed by #71327
Assignees
Labels
api-approved API was approved in API review, it can be implemented Area-IDE Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request

Comments

@josefpihrt
Copy link
Contributor

josefpihrt commented Dec 1, 2023

Background and Motivation

Currently, it's possible to group code actions which is great but it's not possible to access them from parent code action.

Proposed API

Change accessibility of property CodeAction.NestedCodeAction from internal to public:

public abstract class CodeAction
{
-    internal virtual ImmutableArray<CodeAction> NestedCodeActions => ImmutableArray<CodeAction>.Empty;
+    public virtual ImmutableArray<CodeAction> NestedCodeActions => ImmutableArray<CodeAction>.Empty;
}

Usage Examples

I need to unit test code fixes and refactorings which may be registered as nested code actions.

CodeAction? action = null;

var context = new CodeRefactoringContext(
    document,
    span,
    a =>
    {
        // 'a' may be code action with nested actions
        // it's necessary to loop through all nested actions

        if (data.EquivalenceKey is null
            || string.Equals(a.EquivalenceKey, data.EquivalenceKey, StringComparison.Ordinal))
        {
            action = a;
        }
    },
    cancellationToken);

await refactoringProvider.ComputeRefactoringsAsync(context);

Workaround

It's possible to use reflection to access nested code action.

public static ImmutableArray<CodeAction> GetNestedActions(this CodeAction codeAction)
{
    Type type = codeAction.GetType();
    if (type.Name == "CodeActionWithNestedActions")
    {
        PropertyInfo propertyInfo = type.GetProperty("NestedCodeActions", BindingFlags.NonPublic | BindingFlags.Instance);
        return (ImmutableArray<CodeAction>)propertyInfo.GetValue(codeAction);
    }

    return ImmutableArray<CodeAction>.Empty;
}

Or is there any other way how to access nested code actions from parent code action?

@josefpihrt josefpihrt added Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request labels Dec 1, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 1, 2023
@CyrusNajmabadi CyrusNajmabadi removed the untriaged Issues and PRs which have not yet been triaged by a lead label Dec 1, 2023
@CyrusNajmabadi
Copy link
Member

hey @josefpihrt i'll run point on this. I would not make CodeActionWithNestedCodeActions , but i would make this internal property to be public:

internal virtual ImmutableArray<CodeAction> NestedCodeActions

I would also point out in teh proposal that we already expose this factory method to create teh code action:

public static CodeAction Create(string title, ImmutableArray<CodeAction> nestedActions, bool isInlinable, CodeActionPriority priority = CodeActionPriority.Default)

So it is simply weird that you can create the code action with nested actions, but then can't get to them

@CyrusNajmabadi CyrusNajmabadi self-assigned this Dec 1, 2023
@josefpihrt
Copy link
Contributor Author

Thanks for fast answer @CyrusNajmabadi

I'd be perfectly fine with making property NestedCodeActions public.

@CyrusNajmabadi
Copy link
Member

@josefpihrt Great. Once you update the proposal lmk and i'll put it on the docket.

@josefpihrt
Copy link
Contributor Author

Proposal updated.

@CyrusNajmabadi CyrusNajmabadi added the api-ready-for-review API is ready for review, it is NOT ready for implementation label Dec 1, 2023
@333fred
Copy link
Member

333fred commented Dec 7, 2023

API Review

  • We'll want to remove the OmniSharp EA API for this now that it's public.
  • Let's make sure that the naming matches what we do for creation.

Conclusion: Approved

@333fred 333fred added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented Area-IDE Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants