Skip to content

Commit c8dc2ab

Browse files
tobinguyennd8vjork
authored andcommitted
feat: Add Laravel pint --dirty option
1 parent 803d02f commit c8dc2ab

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@
119119
"type": "string",
120120
"markdownDescription": "%ext.config.sailExecutablePath%",
121121
"scope": "resource"
122+
},
123+
"laravel-pint.dirtyOnly": {
124+
"default": false,
125+
"type": "boolean",
126+
"markdownDescription": "%ext.config.dirtyOnly%",
127+
"scope": "resource"
122128
}
123129
}
124130
},

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"ext.config.executablePath": "**Workspace-relative path** to the executable binary. Default: `vendor/bin/pint`.",
77
"ext.config.fallbackToGlobalBin": "Enable/disable fallback to global composer Pint binary in case local wasn't found. Default: `true`.",
88
"ext.config.runInLaravelSail": "Run formatting through Docker using [Laravel Sail](https://laravel.com/docs/master/sail). This will ignore `#laravel-pint.executablePath#`.",
9-
"ext.config.sailExecutablePath": "Path to Laravel Sail executable, only used if is enabled `#laravel-pint.runInLaravelSail#`. Default: `vendor/bin/sail`."
9+
"ext.config.sailExecutablePath": "Path to Laravel Sail executable, only used if is enabled `#laravel-pint.runInLaravelSail#`. Default: `vendor/bin/sail`.",
10+
"ext.config.dirtyOnly": "Enable/disable formatting only on **active workspace** dirty files. Only work with command `Format active workspace files using Laravel Pint`."
1011
}

src/ModuleResolver.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ export class ModuleResolver {
1616
return new PhpCommand(globalPintPath, args);
1717
}
1818

19-
public async getPintCommand(workspaceFolder: WorkspaceFolder, input?: string): Promise<PhpCommand | undefined> {
19+
public async getPintCommand(
20+
workspaceFolder: WorkspaceFolder,
21+
input?: string,
22+
isFormatWorkspace = false
23+
): Promise<PhpCommand | undefined> {
2024
if (!workspace.isTrusted) {
2125
this.loggingService.logDebug(UNTRUSTED_WORKSPACE_USING_GLOBAL_PINT);
2226

2327
// This doesn't respect fallbackToGlobal config
24-
return this.getGlobalPintCommand(await this.getPintConfigAsArgs(workspaceFolder, input));
28+
return this.getGlobalPintCommand(
29+
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace)
30+
);
2531
}
2632

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

3844
if (!isExecutable && fallbackToGlobal) {
39-
return this.getGlobalPintCommand(await this.getPintConfigAsArgs(workspaceFolder, input));
45+
return this.getGlobalPintCommand(
46+
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace)
47+
);
4048
}
4149

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

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

52-
return new PhpCommand(cmd, await this.getPintConfigAsArgs(workspaceFolder, input), cwd);
60+
return new PhpCommand(
61+
cmd,
62+
await this.getPintConfigAsArgs(workspaceFolder, input, isFormatWorkspace),
63+
cwd
64+
);
5365
}
5466

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

84-
private async getPintConfigAsArgs(workspaceFolder: WorkspaceFolder, input?: string) {
96+
private async getPintConfigAsArgs(
97+
workspaceFolder: WorkspaceFolder,
98+
input?: string,
99+
isFormatWorkspace = false
100+
) {
85101
const executableArgs: Record<string, string> = {};
86102
const configPath = getWorkspaceConfig('configPath', CONFIG_FILE_NAME);
87103

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

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

124+
if (isFormatWorkspace) {
125+
const dirtyOnly = getWorkspaceConfig("dirtyOnly", false);
126+
if (dirtyOnly) {
127+
executableArgsAsArray.push("--dirty");
128+
}
129+
}
130+
108131
return executableArgsAsArray;
109132
}
110133
}

src/PintEditService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ export default class PintEditService implements Disposable {
176176
return;
177177
}
178178

179-
await this.formatFile(workspaceFolder.uri);
179+
await this.formatFile(workspaceFolder.uri, true);
180180
}
181181

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

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

191191
let command = workspaceFolder
192-
? await this.moduleResolver.getPintCommand(workspaceFolder, file.fsPath)
192+
? await this.moduleResolver.getPintCommand(workspaceFolder, file.fsPath, isFormatWorkspace)
193193
: await this.moduleResolver.getGlobalPintCommand([file.fsPath]);
194194

195195
if (!command) {

src/test/suite/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ suite('Extension Test Suite', () => {
3333
assert.strictEqual(config.get<PresetOptions>('preset'), 'auto');
3434
assert.strictEqual(config.get<boolean>('runInLaravelSail'), false);
3535
assert.strictEqual(config.get<string>('sailExecutablePath'), '');
36+
assert.strictEqual(config.get<boolean>('dirtyOnly'), false);
3637
});
3738

3839
test('Build command with config args', async () => {

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ExtensionConfig {
99
fallbackToGlobalBin: boolean
1010
runInLaravelSail: boolean
1111
sailExecutablePath: string
12+
dirtyOnly: boolean
1213
}
1314

1415
export interface ExtensionFormattingOptions {

0 commit comments

Comments
 (0)