From dcc531f6382fe0580edbf62386fc48ca233e9c2e Mon Sep 17 00:00:00 2001 From: Nathan Totten Date: Sat, 13 Mar 2021 10:12:48 -0500 Subject: [PATCH] added force formatting command --- CHANGELOG.md | 4 ++++ package.json | 7 ++++++- package.nls.json | 1 + src/PrettierEditProvider.ts | 9 ++++++--- src/PrettierEditService.ts | 39 +++++++++++++++++++++++++++++++++---- src/extension.ts | 5 +++++ src/types.d.ts | 6 ++++++ 7 files changed, 63 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63055652f..8c19cf9b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi +## [6.1.0] Beta + +- Added command to force formatting regardless of ignores `Format Document (Forced)` + ## [6.0.0] Beta - Automatically detect package manager diff --git a/package.json b/package.json index ea25efd4b..64aa359f1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "prettier-vscode-beta", "displayName": "Prettier - Code formatter", "description": "Code formatter using prettier", - "version": "6.0.0", + "version": "6.1.0", "publisher": "prettier", "author": "Prettier <@prettiercode>", "preview": true, @@ -395,6 +395,11 @@ { "command": "prettier.resetModuleExecutionState", "title": "%ext.command.resetModuleExecutionState.title%" + }, + { + "command": "prettier.forceFormatDocument", + "title": "%ext.command.forceFormatDocument.title%", + "when": "editorFocus" } ] } diff --git a/package.nls.json b/package.nls.json index a0375ca87..e18986240 100644 --- a/package.nls.json +++ b/package.nls.json @@ -1,6 +1,7 @@ { "ext.command.createConfigFile.title": "Prettier: Create Configuration File", "ext.command.resetModuleExecutionState.title": "Prettier: Reset Module Execution State", + "ext.command.forceFormatDocument.title": "Format Document (Forced)", "ext.config.arrowParens": "Include parentheses around a sole arrow function parameter", "ext.config.bracketSpacing": "Controls the printing of spaces inside object literals", "ext.config.configPath": "Path to the prettier configuration file", diff --git a/src/PrettierEditProvider.ts b/src/PrettierEditProvider.ts index 55f495a50..639846a71 100644 --- a/src/PrettierEditProvider.ts +++ b/src/PrettierEditProvider.ts @@ -7,7 +7,7 @@ import { TextDocument, TextEdit, } from "vscode"; -import { RangeFormattingOptions } from "./types"; +import { ExtensionFormattingOptions } from "./types"; export class PrettierEditProvider implements @@ -16,7 +16,7 @@ export class PrettierEditProvider constructor( private provideEdits: ( document: TextDocument, - options?: RangeFormattingOptions + options: ExtensionFormattingOptions ) => Promise ) {} @@ -31,6 +31,7 @@ export class PrettierEditProvider return this.provideEdits(document, { rangeEnd: document.offsetAt(range.end), rangeStart: document.offsetAt(range.start), + force: false, }); } @@ -41,6 +42,8 @@ export class PrettierEditProvider // eslint-disable-next-line @typescript-eslint/no-unused-vars token: CancellationToken ): Promise { - return this.provideEdits(document); + return this.provideEdits(document, { + force: false, + }); } } diff --git a/src/PrettierEditService.ts b/src/PrettierEditService.ts index 0b620c653..5bc03eb58 100644 --- a/src/PrettierEditService.ts +++ b/src/PrettierEditService.ts @@ -27,7 +27,11 @@ import { ModuleResolver } from "./ModuleResolver"; import { NotificationService } from "./NotificationService"; import { PrettierEditProvider } from "./PrettierEditProvider"; import { FormatterStatus, StatusBar } from "./StatusBar"; -import { PrettierModule, RangeFormattingOptions } from "./types"; +import { + ExtensionFormattingOptions, + PrettierModule, + RangeFormattingOptions, +} from "./types"; import { getConfig, getWorkspaceRelativePath } from "./util"; interface ISelectors { @@ -100,6 +104,25 @@ export default class PrettierEditService implements Disposable { ]; } + public forceFormatDocument = async () => { + const editor = window.activeTextEditor; + if (!editor) { + this.loggingService.logInfo("No active document. Nothing was formatted."); + return; + } + + this.loggingService.logInfo("Forced formatting will not use ignore files."); + + const edits = await this.provideEdits(editor.document, { force: true }); + if (edits.length !== 1) { + return; + } + + await editor.edit((editBuilder) => { + editBuilder.replace(edits[0].range, edits[0].newText); + }); + }; + private prettierConfigChanged = async (uri: Uri) => this.resetFormatters(uri); private resetFormatters = async (uri?: Uri) => { @@ -303,7 +326,7 @@ export default class PrettierEditService implements Disposable { private provideEdits = async ( document: TextDocument, - options?: RangeFormattingOptions + options: ExtensionFormattingOptions ): Promise => { const hrStart = process.hrtime(); const result = await this.format(document.getText(), document, options); @@ -327,7 +350,7 @@ export default class PrettierEditService implements Disposable { private async format( text: string, { fileName, languageId, uri, isUntitled }: TextDocument, - rangeFormattingOptions?: RangeFormattingOptions + options: ExtensionFormattingOptions ): Promise { this.loggingService.logInfo(`Formatting ${fileName}`); @@ -421,7 +444,7 @@ export default class PrettierEditService implements Disposable { this.loggingService.logInfo("File Info:", fileInfo); } - if (fileInfo && fileInfo.ignored) { + if (!options.force && fileInfo && fileInfo.ignored) { this.loggingService.logInfo("File is ignored, skipping."); this.statusBar.update(FormatterStatus.Ignore); return; @@ -450,6 +473,14 @@ export default class PrettierEditService implements Disposable { return; } + let rangeFormattingOptions: RangeFormattingOptions | undefined; + if (options.rangeEnd && options.rangeStart) { + rangeFormattingOptions = { + rangeEnd: options.rangeEnd, + rangeStart: options.rangeStart, + }; + } + const prettierOptions = this.getPrettierOptions( fileName, parser as prettier.BuiltInParserName, diff --git a/src/extension.ts b/src/extension.ts index 5a361bc3e..2a818abca 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -74,12 +74,17 @@ export function activate(context: ExtensionContext) { loggingService.show(); } ); + const forceFormatDocumentCommand = commands.registerCommand( + "prettier.forceFormatDocument", + editService.forceFormatDocument + ); context.subscriptions.push( editService, createConfigFileCommand, resetModuleExecutionStateCommand, openOutputCommand, + forceFormatDocumentCommand, ...editService.registerDisposables() ); } diff --git a/src/types.d.ts b/src/types.d.ts index 10a3fcce7..2d365503b 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -60,3 +60,9 @@ export interface RangeFormattingOptions { rangeStart: number; rangeEnd: number; } + +export interface ExtensionFormattingOptions { + rangeStart?: number; + rangeEnd?: number; + force: boolean; +}