Skip to content

Commit 99703f7

Browse files
committed
Rename "--line-length" option to "--page-width".
The formatter has always been inconsistent about how to refer to the limit that it tries to fit code in. The option name (which is older than when I started working on the formatter) is "--line-length". But the public API and all of the internal code uses "page width". This renames that option to "--page-width". The old name is still supported (but hidden in help) for backwards compatibility. In practice, this option isn't used often, so this likely won't affect many users.
1 parent f408350 commit 99703f7

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@
7373
intended to be extended or implemented. They are now all marked `final` to
7474
make that intention explicit.
7575

76+
* **Rename the `--line-length` option to `--page-width`.** This is consistent
77+
with the public API, internal implementation, and docs, which all use "page
78+
width" to refer to the limit that the formatter tries to fit code into.
79+
80+
The `--line-length` name is still supported for backwards compatibility, but
81+
may be removed at some point in the future. You're encouraged to move to
82+
`--page-width`. Use of this option (however its named) is rare, and will
83+
likely be even rarer now that project-wide configuration is supported, so
84+
this shouldn't affect many users.
85+
7686
## 2.3.7
7787

7888
* Allow passing a language version to `DartFomatter()`. Formatted code will be

lib/src/cli/format_command.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ final class FormatCommand extends Command<int> {
7878

7979
if (verbose) argParser.addSeparator('Other options:');
8080

81+
argParser.addOption('page-width',
82+
help: 'Try to keep lines no longer than this.',
83+
defaultsTo: '80',
84+
hide: !verbose);
85+
// This is the old name for "--page-width". We keep it for backwards
86+
// compatibility but don't show it in the help output.
8187
argParser.addOption('line-length',
8288
abbr: 'l',
8389
help: 'Wrap lines longer than this.',
8490
defaultsTo: '80',
8591
hide: true);
92+
8693
argParser.addOption('indent',
8794
abbr: 'i',
8895
help: 'Add this many spaces of leading indentation.',
@@ -189,11 +196,23 @@ final class FormatCommand extends Command<int> {
189196
}
190197
}
191198

199+
// Allow the old option name if the new one wasn't passed.
200+
String? pageWidthString;
201+
if (argResults.wasParsed('page-width')) {
202+
pageWidthString = argResults['page-width'] as String;
203+
} else if (argResults.wasParsed('line-length')) {
204+
pageWidthString = argResults['line-length'] as String;
205+
}
206+
192207
int? pageWidth;
193-
if (argResults.wasParsed('line-length')) {
194-
pageWidth = int.tryParse(argResults['line-length'] as String) ??
195-
usageException('--line-length must be an integer, was '
196-
'"${argResults['line-length']}".');
208+
if (pageWidthString != null) {
209+
pageWidth = int.tryParse(pageWidthString);
210+
if (pageWidth == null) {
211+
usageException(
212+
'Page width must be an integer, was "$pageWidthString".');
213+
} else if (pageWidth <= 0) {
214+
usageException('Page width must be a positive number, was $pageWidth.');
215+
}
197216
}
198217

199218
var indent = int.tryParse(argResults['indent'] as String) ??

test/cli/page_width_test.dart

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,54 @@ import '../utils.dart';
1212
void main() {
1313
compileFormatter();
1414

15+
group('--page-width', () {
16+
test('must be a number', () async {
17+
var process = await runFormatter(['--page-width=12.34']);
18+
await process.shouldExit(64);
19+
});
20+
21+
test('must be an integer', () async {
22+
var process = await runFormatter(['--page-width=notint']);
23+
await process.shouldExit(64);
24+
});
25+
26+
test('must be positive', () async {
27+
var process = await runFormatter(['--page-width=-123']);
28+
await process.shouldExit(64);
29+
30+
process = await runFormatter(['--page-width=0']);
31+
await process.shouldExit(64);
32+
});
33+
34+
test('format at given width', () async {
35+
await d.dir('foo', [
36+
d.file('main.dart', _unformatted),
37+
]).create();
38+
39+
var process = await runFormatterOnDir([
40+
'--enable-experiment=tall-style',
41+
'--page-width=30',
42+
]);
43+
await process.shouldExit(0);
44+
45+
await d.dir('foo', [d.file('main.dart', _formatted30)]).validate();
46+
});
47+
48+
test('support old --line-length option name', () async {
49+
await d.dir('foo', [
50+
d.file('main.dart', _unformatted),
51+
]).create();
52+
53+
var process = await runFormatterOnDir([
54+
'--enable-experiment=tall-style',
55+
'--line-length=30',
56+
]);
57+
await process.shouldExit(0);
58+
59+
await d.dir('foo', [d.file('main.dart', _formatted30)]).validate();
60+
});
61+
});
62+
1563
test('no options search if experiment is off', () async {
1664
await d.dir('foo', [
1765
analysisOptionsFile(pageWidth: 20),
@@ -33,7 +81,7 @@ void main() {
3381

3482
var process = await runFormatterOnDir([
3583
'--language-version=latest', // Error to not have language version.
36-
'--line-length=30',
84+
'--page-width=30',
3785
'--enable-experiment=tall-style'
3886
]);
3987
await process.shouldExit(0);
@@ -173,7 +221,7 @@ void main() {
173221
var process = await runFormatter([
174222
'--language-version=latest',
175223
'--enable-experiment=tall-style',
176-
'--line-length=30',
224+
'--page-width=30',
177225
'--stdin-name=foo/main.dart'
178226
]);
179227

0 commit comments

Comments
 (0)