Skip to content

Commit

Permalink
feat: Add Laravel pint --dirty option
Browse files Browse the repository at this point in the history
  • Loading branch information
tobinguyenn authored and d8vjork committed Mar 13, 2024
1 parent 803d02f commit c8dc2ab
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@
"type": "string",
"markdownDescription": "%ext.config.sailExecutablePath%",
"scope": "resource"
},
"laravel-pint.dirtyOnly": {
"default": false,
"type": "boolean",
"markdownDescription": "%ext.config.dirtyOnly%",
"scope": "resource"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"ext.config.executablePath": "**Workspace-relative path** to the executable binary. Default: `vendor/bin/pint`.",
"ext.config.fallbackToGlobalBin": "Enable/disable fallback to global composer Pint binary in case local wasn't found. Default: `true`.",
"ext.config.runInLaravelSail": "Run formatting through Docker using [Laravel Sail](https://laravel.com/docs/master/sail). This will ignore `#laravel-pint.executablePath#`.",
"ext.config.sailExecutablePath": "Path to Laravel Sail executable, only used if is enabled `#laravel-pint.runInLaravelSail#`. Default: `vendor/bin/sail`."
"ext.config.sailExecutablePath": "Path to Laravel Sail executable, only used if is enabled `#laravel-pint.runInLaravelSail#`. Default: `vendor/bin/sail`.",
"ext.config.dirtyOnly": "Enable/disable formatting only on **active workspace** dirty files. Only work with command `Format active workspace files using Laravel Pint`."
}
33 changes: 28 additions & 5 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export class ModuleResolver {
return new PhpCommand(globalPintPath, args);
}

public async getPintCommand(workspaceFolder: WorkspaceFolder, input?: string): Promise<PhpCommand | undefined> {
public async getPintCommand(
workspaceFolder: WorkspaceFolder,
input?: string,
isFormatWorkspace = false
): Promise<PhpCommand | undefined> {
if (!workspace.isTrusted) {
this.loggingService.logDebug(UNTRUSTED_WORKSPACE_USING_GLOBAL_PINT);

// This doesn't respect fallbackToGlobal config
return this.getGlobalPintCommand(await this.getPintConfigAsArgs(workspaceFolder, input));
return this.getGlobalPintCommand(
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace)
);
}

const executableArr = await resolvePathFromWorkspaces(
Expand All @@ -36,7 +42,9 @@ export class ModuleResolver {
const fallbackToGlobal = getWorkspaceConfig('fallbackToGlobalBin') && commandExists.sync('pint');

if (!isExecutable && fallbackToGlobal) {
return this.getGlobalPintCommand(await this.getPintConfigAsArgs(workspaceFolder, input));
return this.getGlobalPintCommand(
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace)
);
}

if (!isExecutable && !fallbackToGlobal) {
Expand All @@ -49,7 +57,11 @@ export class ModuleResolver {

const cwd = path.normalize(executable).replace(path.normalize(cmd), '');

return new PhpCommand(cmd, await this.getPintConfigAsArgs(workspaceFolder, input), cwd);
return new PhpCommand(
cmd,
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace),
cwd
);
}

public async getPintCommandWithinSail(workspaceFolder: WorkspaceFolder, input?: string): Promise<PhpCommand | undefined> {
Expand Down Expand Up @@ -81,7 +93,11 @@ export class ModuleResolver {
return new PhpCommand(executable, ['bin', 'pint', ...await this.getPintConfigAsArgs(workspaceFolder, input)]);
}

private async getPintConfigAsArgs(workspaceFolder: WorkspaceFolder, input?: string) {
private async getPintConfigAsArgs(
workspaceFolder: WorkspaceFolder,
input?: string,
isFormatWorkspace = false
) {
const executableArgs: Record<string, string> = {};
const configPath = getWorkspaceConfig('configPath', CONFIG_FILE_NAME);

Expand All @@ -105,6 +121,13 @@ export class ModuleResolver {

executableArgsAsArray.push(input || workspaceFolder.uri.fsPath);

if (isFormatWorkspace) {
const dirtyOnly = getWorkspaceConfig("dirtyOnly", false);
if (dirtyOnly) {
executableArgsAsArray.push("--dirty");
}
}

return executableArgsAsArray;
}
}
6 changes: 3 additions & 3 deletions src/PintEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ export default class PintEditService implements Disposable {
return;
}

await this.formatFile(workspaceFolder.uri);
await this.formatFile(workspaceFolder.uri, true);
}

public async formatFile(file: Uri) {
public async formatFile(file: Uri, isFormatWorkspace = false) {
if (this.isDocumentExcluded(file)) {
this.loggingService.logWarning(`The file "${file.fsPath}" is excluded either by you or by Laravel Pint`);

Expand All @@ -189,7 +189,7 @@ export default class PintEditService implements Disposable {
const workspaceFolder = workspace.getWorkspaceFolder(file);

let command = workspaceFolder
? await this.moduleResolver.getPintCommand(workspaceFolder, file.fsPath)
? await this.moduleResolver.getPintCommand(workspaceFolder, file.fsPath, isFormatWorkspace)
: await this.moduleResolver.getGlobalPintCommand([file.fsPath]);

if (!command) {
Expand Down
1 change: 1 addition & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite('Extension Test Suite', () => {
assert.strictEqual(config.get<PresetOptions>('preset'), 'auto');
assert.strictEqual(config.get<boolean>('runInLaravelSail'), false);
assert.strictEqual(config.get<string>('sailExecutablePath'), '');
assert.strictEqual(config.get<boolean>('dirtyOnly'), false);
});

test('Build command with config args', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ExtensionConfig {
fallbackToGlobalBin: boolean
runInLaravelSail: boolean
sailExecutablePath: string
dirtyOnly: boolean
}

export interface ExtensionFormattingOptions {
Expand Down

0 comments on commit c8dc2ab

Please sign in to comment.