-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Add editor.codeActionsOnSave #48086
Merged
Merged
Add editor.codeActionsOnSave #48086
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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 |
---|---|---|
|
@@ -30,6 +30,14 @@ import { isFalsyOrEmpty } from 'vs/base/common/arrays'; | |
import { ILogService } from 'vs/platform/log/common/log'; | ||
import { shouldSynchronizeModel } from 'vs/editor/common/services/modelService'; | ||
import { SnippetController2 } from 'vs/editor/contrib/snippet/snippetController2'; | ||
import { ITextModelService } from 'vs/editor/common/services/resolverService'; | ||
import { ICommandService } from 'vs/platform/commands/common/commands'; | ||
import { IFileService } from 'vs/platform/files/common/files'; | ||
import { CodeActionKind } from 'vs/editor/contrib/codeAction/codeActionTrigger'; | ||
import { CodeAction } from 'vs/editor/common/modes'; | ||
import { applyCodeAction } from 'vs/editor/contrib/codeAction/codeActionCommands'; | ||
import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction'; | ||
import { ICodeActionsOnSaveOptions } from 'vs/editor/common/config/editorOptions'; | ||
|
||
export interface ISaveParticipantParticipant extends ISaveParticipant { | ||
// progressMessage: string; | ||
|
@@ -259,6 +267,59 @@ class FormatOnSaveParticipant implements ISaveParticipantParticipant { | |
} | ||
} | ||
|
||
class CodeActionOnParticipant implements ISaveParticipant { | ||
|
||
constructor( | ||
@ITextModelService private readonly _textModelService: ITextModelService, | ||
@IFileService private readonly _fileService: IFileService, | ||
@ICommandService private readonly _commandService: ICommandService, | ||
@ICodeEditorService private readonly _codeEditorService: ICodeEditorService, | ||
@IConfigurationService private readonly _configurationService: IConfigurationService | ||
) { } | ||
|
||
async participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise<void> { | ||
if (env.reason === SaveReason.AUTO) { | ||
return undefined; | ||
} | ||
|
||
const model = editorModel.textEditorModel; | ||
const editor = findEditor(model, this._codeEditorService); | ||
if (!editor) { | ||
return undefined; | ||
} | ||
|
||
const settingsOverrides = { overrideIdentifier: model.getLanguageIdentifier().language, resource: editorModel.getResource() }; | ||
const setting = this._configurationService.getValue<ICodeActionsOnSaveOptions>('editor.codeActionsOnSave', settingsOverrides); | ||
if (!setting) { | ||
return undefined; | ||
} | ||
|
||
const codeActionsOnSave = Object.keys(setting).filter(x => setting[x]).map(x => new CodeActionKind(x)); | ||
if (!codeActionsOnSave.length) { | ||
return undefined; | ||
} | ||
|
||
const timeout = this._configurationService.getValue<number>('editor.codeActionsOnSaveTimeout', settingsOverrides); | ||
|
||
return new Promise<CodeAction[]>((resolve, reject) => { | ||
setTimeout(() => reject(localize('codeActionsOnSave.didTimeout', "Aborted codeActionsOnSave after {0}ms", timeout)), timeout); | ||
this.getActionsToRun(model, codeActionsOnSave).then(resolve); | ||
}).then(actionsToRun => this.applyCodeActions(actionsToRun, editor)); | ||
} | ||
|
||
private async applyCodeActions(actionsToRun: CodeAction[], editor: ICodeEditor) { | ||
for (const action of actionsToRun) { | ||
await applyCodeAction(action, this._textModelService, this._fileService, this._commandService, editor); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like - running them one after the other is the sane thing to do IMO |
||
} | ||
} | ||
|
||
private async getActionsToRun(model: ITextModel, codeActionsOnSave: CodeActionKind[]) { | ||
const actions = await getCodeActions(model, model.getFullModelRange(), { kind: CodeActionKind.Source, includeSourceActions: true }); | ||
const actionsToRun = actions.filter(returnedAction => returnedAction.kind && codeActionsOnSave.some(onSaveKind => onSaveKind.contains(returnedAction.kind))); | ||
return actionsToRun; | ||
} | ||
} | ||
|
||
class ExtHostSaveParticipant implements ISaveParticipantParticipant { | ||
|
||
private _proxy: ExtHostDocumentSaveParticipantShape; | ||
|
@@ -303,6 +364,7 @@ export class SaveParticipant implements ISaveParticipant { | |
) { | ||
this._saveParticipants = [ | ||
instantiationService.createInstance(TrimWhitespaceParticipant), | ||
instantiationService.createInstance(CodeActionOnParticipant), | ||
instantiationService.createInstance(FormatOnSaveParticipant), | ||
instantiationService.createInstance(FinalNewLineParticipant), | ||
instantiationService.createInstance(TrimFinalNewLinesParticipant), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add timeout and make it configurable, like
editor.formatOnSaveTimeout