diff --git a/.vscode/settings.json b/.vscode/settings.json index df299a7c6..2b626a5bc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,10 @@ // Place your settings in this file to overwrite default and user settings. { "editor.codeActionsOnSave": { + "source.prettier.forceFormatDocument": true, "source.fixAll.eslint": true }, - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": false, "editor.formatOnType": false, "files.eol": "\n", "cSpell.words": [ diff --git a/CHANGELOG.md b/CHANGELOG.md index c925938cc..89004d7d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to the "prettier-vscode" extension will be documented in thi - Search for ignore file in all containing workspace folders, not just innermost - Fix wrong time unit in log. - Allow formatting ranges in `jsonc` (JSON with Comments). +- Add `prettier.forceFormatDocument` to code actions ## [9.5.0] diff --git a/src/CodeActionOnSave.ts b/src/CodeActionOnSave.ts new file mode 100644 index 000000000..224b9e5ce --- /dev/null +++ b/src/CodeActionOnSave.ts @@ -0,0 +1,35 @@ +import { + CodeAction, + CodeActionKind, + type CodeActionProvider, + type ProviderResult, +} from "vscode"; + +const FORCE_FORMAT_DOCUMENT_COMMAND = "prettier.forceFormatDocument"; +const FORCE_FORMAT_DOCUMENT_CODE_ACTION_KIND = CodeActionKind.Source.append( + FORCE_FORMAT_DOCUMENT_COMMAND +); +const FORCE_FORMAT_DOCUMENT_TITLE = "Format Document (Forced)"; + +export default class CodeActionOnSave implements CodeActionProvider { + public static readonly providedCodeActionKinds = [ + FORCE_FORMAT_DOCUMENT_CODE_ACTION_KIND, + ]; + + public provideCodeActions(): ProviderResult { + const forceFormatDocumentAction = this.createForceFormatDocumentAction(); + return [forceFormatDocumentAction]; + } + + private createForceFormatDocumentAction(): CodeAction { + const action = new CodeAction( + FORCE_FORMAT_DOCUMENT_TITLE, + FORCE_FORMAT_DOCUMENT_CODE_ACTION_KIND + ); + action.command = { + command: FORCE_FORMAT_DOCUMENT_COMMAND, + title: FORCE_FORMAT_DOCUMENT_TITLE, + }; + return action; + } +} diff --git a/src/extension.ts b/src/extension.ts index 1bbc50e78..cc9df09e6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,6 @@ -import { commands, ExtensionContext, workspace } from "vscode"; +import { commands, ExtensionContext, languages, workspace } from "vscode"; import { createConfigFile } from "./commands"; +import CodeActionOnSave from "./CodeActionOnSave"; import { LoggingService } from "./LoggingService"; import { ModuleResolver } from "./ModuleResolver"; import PrettierEditService from "./PrettierEditService"; @@ -75,4 +76,11 @@ export function activate(context: ExtensionContext) { forceFormatDocumentCommand, ...editService.registerDisposables() ); + + const forceFormatDocumentCodeActionProvider = + languages.registerCodeActionsProvider("*", new CodeActionOnSave(), { + providedCodeActionKinds: CodeActionOnSave.providedCodeActionKinds, + }); + + context.subscriptions.push(forceFormatDocumentCodeActionProvider); }