Skip to content

Commit 3941dcd

Browse files
authored
Merge pull request #909 from claytonrcarter/php-block-print-width
2 parents 4444f26 + 6b5ba60 commit 3941dcd

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

__tests__/formatter.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5339,4 +5339,82 @@ describe('formatter', () => {
53395339

53405340
await util.doubleFormatCheck(content, expected);
53415341
});
5342+
5343+
test('@php blocks should wrap long statements (#908)', async () => {
5344+
const content = [
5345+
`@php`,
5346+
`$categories = App\\Models\\Category::whereIn('id', $catids)`,
5347+
`->orderBy('description')`,
5348+
`->orderBy('description')`,
5349+
`->orderBy('description')`,
5350+
`->get();`,
5351+
`@endphp`,
5352+
].join('\n');
5353+
5354+
const expected = [
5355+
`@php`,
5356+
` $categories = App\\Models\\Category::whereIn('id', $catids)`,
5357+
` ->orderBy('description')`,
5358+
` ->orderBy('description')`,
5359+
` ->orderBy('description')`,
5360+
` ->get();`,
5361+
`@endphp`,
5362+
``,
5363+
].join('\n');
5364+
5365+
await util.doubleFormatCheck(content, expected);
5366+
});
5367+
5368+
test('@php blocks should attempt to respect to current indent level', async () => {
5369+
// This statement is 119 chars long and should fit on 1 line w/ the default print width of 120.
5370+
// Once it's indented within the @php block, though, the line will exceed 120, so it should have
5371+
// been indented.
5372+
let content = [
5373+
`@php`,
5374+
`$categories = App\\Models\\Category::whereIn('idss', $catids)`,
5375+
`->orderBy('description')`,
5376+
`->orderBy('description')`,
5377+
`->getNone();`,
5378+
`@endphp`,
5379+
].join('\n');
5380+
5381+
let expected = [
5382+
`@php`,
5383+
` $categories = App\\Models\\Category::whereIn('idss', $catids)`,
5384+
` ->orderBy('description')`,
5385+
` ->orderBy('description')`,
5386+
` ->getNone();`,
5387+
`@endphp`,
5388+
``,
5389+
].join('\n');
5390+
5391+
await util.doubleFormatCheck(content, expected);
5392+
5393+
// Now wrap the @php in a <div> and make the PHP statement 4 chars shorter to confirm that it
5394+
// still works at higher indent levels.
5395+
content = [
5396+
`<div>`,
5397+
`@php`,
5398+
`$categories = App\\Models\\Category::whereIn('idss', $catids)`,
5399+
`->orderBy('description')`,
5400+
`->orderBy('description')`,
5401+
`->get();`,
5402+
`@endphp`,
5403+
`</div>`,
5404+
].join('\n');
5405+
5406+
expected = [
5407+
`<div>`,
5408+
` @php`,
5409+
` $categories = App\\Models\\Category::whereIn('idss', $catids)`,
5410+
` ->orderBy('description')`,
5411+
` ->orderBy('description')`,
5412+
` ->get();`,
5413+
` @endphp`,
5414+
`</div>`,
5415+
``,
5416+
].join('\n');
5417+
5418+
await util.doubleFormatCheck(content, expected);
5419+
});
53425420
});

src/formatter.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,11 +1293,28 @@ export default class Formatter {
12931293
const matchedLine = content.match(new RegExp(`^(.*?)${placeholder}`, 'gmi')) ?? [''];
12941294
const indent = detectIndent(matchedLine[0]);
12951295

1296-
if (this.isInline(rawBlock) && (await this.isMultilineStatement(rawBlock))) {
1296+
const isOnSingleLine = this.isInline(rawBlock);
1297+
const isMultipleStatements = await this.isMultilineStatement(rawBlock);
1298+
if (isOnSingleLine && isMultipleStatements) {
1299+
// multiple statements on a single line
12971300
rawBlock = (await util.formatStringAsPhp(`<?php\n${rawBlock}\n?>`, this.options)).trim();
1298-
} else if (rawBlock.split('\n').length > 1) {
1301+
} else if (isMultipleStatements) {
1302+
// multiple statments on mult lines
1303+
1304+
const indentLevel = (indent.amount + 1) * this.indentSize;
1305+
1306+
rawBlock = (
1307+
await util.formatStringAsPhp(`<?php${rawBlock}?>`, {
1308+
...this.options,
1309+
useProjectPrintWidth: true,
1310+
adjustPrintWidthBy: indentLevel,
1311+
})
1312+
).trimEnd();
1313+
} else if (!isOnSingleLine) {
1314+
// single statement on mult lines
12991315
rawBlock = (await util.formatStringAsPhp(`<?php${rawBlock}?>`, this.options)).trimEnd();
13001316
} else {
1317+
// single statement on single line
13011318
rawBlock = `<?php${rawBlock}?>`;
13021319
}
13031320

src/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ export async function formatStringAsPhp(content: any, params: FormatPhpOption =
6565
...params,
6666
};
6767

68+
const adjust = params.adjustPrintWidthBy ?? 0;
69+
const printWidth = params.useProjectPrintWidth ? options.printWidth - adjust : printWidthForInline;
6870
try {
6971
return await prettier.format(content.replace(/\n$/, ''), {
7072
parser: 'php',
71-
printWidth: 1000,
73+
printWidth,
7274
singleQuote: !options.noSingleQuote,
7375
// @ts-ignore
7476
phpVersion: options.phpVersion,

0 commit comments

Comments
 (0)