Skip to content

Commit

Permalink
added force formatting command
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Mar 13, 2021
1 parent bc3cb8a commit dcc531f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->

## [6.1.0] Beta

- Added command to force formatting regardless of ignores `Format Document (Forced)`

## [6.0.0] Beta

- Automatically detect package manager
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -395,6 +395,11 @@
{
"command": "prettier.resetModuleExecutionState",
"title": "%ext.command.resetModuleExecutionState.title%"
},
{
"command": "prettier.forceFormatDocument",
"title": "%ext.command.forceFormatDocument.title%",
"when": "editorFocus"
}
]
}
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 6 additions & 3 deletions src/PrettierEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TextDocument,
TextEdit,
} from "vscode";
import { RangeFormattingOptions } from "./types";
import { ExtensionFormattingOptions } from "./types";

export class PrettierEditProvider
implements
Expand All @@ -16,7 +16,7 @@ export class PrettierEditProvider
constructor(
private provideEdits: (
document: TextDocument,
options?: RangeFormattingOptions
options: ExtensionFormattingOptions
) => Promise<TextEdit[]>
) {}

Expand All @@ -31,6 +31,7 @@ export class PrettierEditProvider
return this.provideEdits(document, {
rangeEnd: document.offsetAt(range.end),
rangeStart: document.offsetAt(range.start),
force: false,
});
}

Expand All @@ -41,6 +42,8 @@ export class PrettierEditProvider
// eslint-disable-next-line @typescript-eslint/no-unused-vars
token: CancellationToken
): Promise<TextEdit[]> {
return this.provideEdits(document);
return this.provideEdits(document, {
force: false,
});
}
}
39 changes: 35 additions & 4 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -303,7 +326,7 @@ export default class PrettierEditService implements Disposable {

private provideEdits = async (
document: TextDocument,
options?: RangeFormattingOptions
options: ExtensionFormattingOptions
): Promise<TextEdit[]> => {
const hrStart = process.hrtime();
const result = await this.format(document.getText(), document, options);
Expand All @@ -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<string | undefined> {
this.loggingService.logInfo(`Formatting ${fileName}`);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
6 changes: 6 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ export interface RangeFormattingOptions {
rangeStart: number;
rangeEnd: number;
}

export interface ExtensionFormattingOptions {
rangeStart?: number;
rangeEnd?: number;
force: boolean;
}

0 comments on commit dcc531f

Please sign in to comment.