Skip to content

Update CodeActionOnSaveParticipant - #108193

Merged
Matt Bierner (mjbvz) merged 2 commits into
microsoft:masterfrom
turara:fix-106924
Nov 20, 2020
Merged

Update CodeActionOnSaveParticipant#108193
Matt Bierner (mjbvz) merged 2 commits into
microsoft:masterfrom
turara:fix-106924

Conversation

@turara

@turara turara (turara) commented Oct 6, 2020

Copy link
Copy Markdown
Contributor

This PR fixes #106924.

I updated CodeActionOnSaveParticipant#participate method to filter out CodeActionKind with 'source.fixAll.eslint' from codeActionsOnSave when "source.fixAll" is true.


The reason why the eslint code action provider is called twice with the settings:

{
    "editor.codeActionsOnSave": {
        "source.fixAll": true,
        "source.fixAll.eslint": true,
    }
}

is as follows.

  1. codeActionsOnSave array has two CodeActionKinds with values of 'source.fixAll' and 'source.fixAll.eslint'

    const codeActionsOnSave = settingItems
    .map(x => new CodeActionKind(x));

  2. getActionsToRun method returns CodeActionSet with provider from eslint for both of them.

    for (const codeActionKind of codeActionsOnSave) {
    const actionsToRun = await this.getActionsToRun(model, codeActionKind, excludes, getActionProgress, token);


Should I add filtering code for other CodeActionKind such as SourceOrganizeImports, Refactor, etc...?

Please let me know, in the first place, if there are better approaches.

Add filtering CodeActionKind.SourceFixAll derivatives for codeActionsOnSave
let codeActionsOnSave = settingItems
.map(x => new CodeActionKind(x));

if (!excludedActions.some(a => a.equals(CodeActionKind.SourceFixAll))) {

@mjbvz Matt Bierner (mjbvz) Oct 20, 2020

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 don't think we should hardcode FixAll here. Instead, the logic should be:

  • If there are multiple code actions on save
  • Remove any of them that are strict subsets of the other ones (such as source.fixAll.eslint being a subset of source.fixall)

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.

Matt Bierner (@mjbvz) Thank you for the comment. I've updated the code to remove subsets from settingItems.

@mjbvz Matt Bierner (mjbvz) added this to the November 2020 milestone Oct 27, 2020
const actionSeeds = new Set<string>();

// Remove subsets
const len = settingItems.length;

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.

Instead of nested for loops, can you use array.filter? That should simplify the logic

@mjbvz
Matt Bierner (mjbvz) merged commit f1cfe2d into microsoft:master Nov 20, 2020
@mjbvz

Copy link
Copy Markdown
Contributor

Merging and will look at using array.filter

Matt Bierner (mjbvz) pushed a commit that referenced this pull request Nov 21, 2020
@turara

Copy link
Copy Markdown
Contributor Author

Matt Bierner (@mjbvz) Sorry that I missed your comment. Thank you for your commit and merging!

chenjg88 (chenjigeng) pushed a commit to chenjigeng/vscode that referenced this pull request Nov 22, 2020
* Update CodeActionOnSaveParticipant

Add filtering CodeActionKind.SourceFixAll derivatives for codeActionsOnSave

* Add CodeActionOnSaveParticipant#createCodeActionsOnSave method to remove subsets.

Fixes microsoft#106924.
chenjg88 (chenjigeng) pushed a commit to chenjigeng/vscode that referenced this pull request Nov 22, 2020
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code action provider called twice on save

2 participants