Skip to content

Commit

Permalink
fmt [nfc]: Adjust newline placement to Flutter style
Browse files Browse the repository at this point in the history
From the Flutter repo I've picked up a preference to avoid a
line break just after `=` or `=>`, and in a few similar "cliffhanger"
locations.  This is paired with a willingness to make some lines
longer than 80 columns when that helps readability:
  https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#prefer-a-maximum-line-length-of-80-characters
  https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#consider-using-for-short-functions-and-methods

We have some such "cliffhanger" newlines in our existing code,
much of it in places where the Dart autoformatter put it there
before I'd gotten a grasp on what I want to keep from the
autoformatter's output and what I don't.  So here's a quick sweep
through our code fixing those.

Conversely, fix a couple of too-long lines, and mismatched formatting
of parallel constructs, that I noticed while doing the sweep.
  • Loading branch information
gnprice committed May 26, 2023
1 parent 36c25ec commit 21cca5b
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 59 deletions.
3 changes: 1 addition & 2 deletions lib/api/model/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ class MessageEvent extends Event {
id: json['id'] as int,
message: Message.fromJson({
...json['message'] as Map<String, dynamic>,
'flags':
(json['flags'] as List<dynamic>).map((e) => e as String).toList(),
'flags': (json['flags'] as List<dynamic>).map((e) => e as String).toList(),
}),
);

Expand Down
5 changes: 3 additions & 2 deletions lib/api/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ class User {
return (value != null && value.isNotEmpty) ? value : null;
}

static bool? _readIsSystemBot(Map json, String key) =>
json[key] ?? json['is_cross_realm_bot'];
static bool? _readIsSystemBot(Map json, String key) {
return json[key] ?? json['is_cross_realm_bot'];
}

User({
required this.userId,
Expand Down
16 changes: 8 additions & 8 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ final _emojiClassRegexp = RegExp(r"^emoji(-[0-9a-f]+)?$");

InlineContentNode parseInlineContent(dom.Node node) {
final debugHtmlNode = kDebugMode ? node : null;
InlineContentNode unimplemented() =>
UnimplementedInlineContentNode(htmlNode: node);
InlineContentNode unimplemented() => UnimplementedInlineContentNode(htmlNode: node);

if (node is dom.Text) {
return TextNode(node.text, debugHtmlNode: debugHtmlNode);
Expand All @@ -239,8 +238,9 @@ InlineContentNode parseInlineContent(dom.Node node) {
final element = node;
final localName = element.localName;
final classes = element.classes;
List<InlineContentNode> nodes() =>
element.nodes.map(parseInlineContent).toList(growable: false);
List<InlineContentNode> nodes() {
return element.nodes.map(parseInlineContent).toList(growable: false);
}

if (localName == 'br' && classes.isEmpty) {
return LineBreakInlineNode(debugHtmlNode: debugHtmlNode);
Expand Down Expand Up @@ -410,8 +410,9 @@ BlockContentNode parseBlockContent(dom.Node node) {
final localName = element.localName;
final classes = element.classes;
List<BlockContentNode> blockNodes() => parseBlockContentList(element.nodes);
List<InlineContentNode> inlineNodes() =>
element.nodes.map(parseInlineContent).toList(growable: false);
List<InlineContentNode> inlineNodes() {
return element.nodes.map(parseInlineContent).toList(growable: false);
}

if (localName == 'br' && classes.isEmpty) {
return LineBreakNode(debugHtmlNode: debugHtmlNode);
Expand Down Expand Up @@ -489,8 +490,7 @@ List<BlockContentNode> parseImplicitParagraphBlockContentList(dom.NodeList nodes
void consumeParagraph() {
result.add(ParagraphNode(
wasImplicit: true,
nodes:
currentParagraph.map(parseInlineContent).toList(growable: false)));
nodes: currentParagraph.map(parseInlineContent).toList(growable: false)));
currentParagraph.clear();
}

Expand Down
9 changes: 5 additions & 4 deletions lib/model/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ class MessageListView extends ChangeNotifier {
assert(messages.isEmpty);
assert(contents.isEmpty);
// TODO schedule all this in another isolate
final result =
await getMessages(store.connection, numBefore: 100, numAfter: 10);
final result = await getMessages(store.connection,
numBefore: 100,
numAfter: 10,
);
messages.addAll(result.messages);
contents.addAll(_contentsOfMessages(result.messages));
_fetched = true;
Expand Down Expand Up @@ -92,8 +94,7 @@ class MessageListView extends ChangeNotifier {
notifyListeners();
}

static Iterable<ZulipContent> _contentsOfMessages(
Iterable<Message> messages) {
static Iterable<ZulipContent> _contentsOfMessages(Iterable<Message> messages) {
// This will get more complicated to handle the ways that messages interact
// with the display of neighboring messages: sender headings,
// recipient headings, and date separators.
Expand Down
3 changes: 2 additions & 1 deletion lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class PerAccountStore extends ChangeNotifier {
required this.connection,
required InitialSnapshot initialSnapshot,
}) : zulipVersion = initialSnapshot.zulipVersion,
users = Map.fromEntries(initialSnapshot.realmUsers
users = Map.fromEntries(
initialSnapshot.realmUsers
.followedBy(initialSnapshot.realmNonActiveUsers)
.followedBy(initialSnapshot.crossRealmBots)
.map((user) => MapEntry(user.userId, user))),
Expand Down
9 changes: 7 additions & 2 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ Future<void> _uploadFiles({
context: context,
title: 'File(s) too large',
message:
'${tooLargeFiles.length} file(s) are larger than the server\'s limit of ${store.maxFileUploadSizeMib} MiB and will not be uploaded:\n\n$listMessage');
'${tooLargeFiles.length} file(s) are larger than the server\'s limit'
' of ${store.maxFileUploadSizeMib} MiB and will not be uploaded:'
'\n\n$listMessage');
}

final List<(int, _File)> uploadsInProgress = [];
Expand Down Expand Up @@ -320,7 +322,10 @@ abstract class _AttachUploadsButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
return IconButton(icon: Icon(icon), tooltip: tooltip, onPressed: () => _handlePress(context));
return IconButton(
icon: Icon(icon),
tooltip: tooltip,
onPressed: () => _handlePress(context));
}
}

Expand Down
13 changes: 5 additions & 8 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class BlockContentNodeWidget extends StatelessWidget {
border: Border(
left: BorderSide(
width: 5,
color: const HSLColor.fromAHSL(1, 0, 0, 0.87)
.toColor()))),
color: const HSLColor.fromAHSL(1, 0, 0, 0.87).toColor()))),
child: BlockContentList(nodes: node.nodes)));
} else if (node is CodeBlockNode) {
return CodeBlock(node: node);
Expand Down Expand Up @@ -311,15 +310,13 @@ InlineSpan _buildInlineNode(InlineContentNode node) {
return styled(node.nodes,
TextStyle(color: const HSLColor.fromAHSL(1, 200, 1, 0.4).toColor()));
} else if (node is UserMentionNode) {
return WidgetSpan(
alignment: PlaceholderAlignment.middle, child: UserMention(node: node));
return WidgetSpan(alignment: PlaceholderAlignment.middle,
child: UserMention(node: node));
} else if (node is UnicodeEmojiNode) {
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
return WidgetSpan(alignment: PlaceholderAlignment.middle,
child: MessageUnicodeEmoji(node: node));
} else if (node is ImageEmojiNode) {
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
return WidgetSpan(alignment: PlaceholderAlignment.middle,
child: MessageImageEmoji(node: node));
} else if (node is UnimplementedInlineContentNode) {
return _errorUnimplemented(node);
Expand Down
10 changes: 8 additions & 2 deletions lib/widgets/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Widget _dialogActionText(String text) {
}

// TODO(i18n): title, message, and action-button text
void showErrorDialog({required BuildContext context, required String title, String? message}) {
void showErrorDialog({
required BuildContext context,
required String title,
String? message,
}) {
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
Expand All @@ -35,7 +39,9 @@ void showSuggestedActionDialog({
required String? actionButtonText,
required VoidCallback onActionButtonPress,
}) {
showDialog(context: context, builder: (BuildContext context) => AlertDialog(
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text(title),
content: SingleChildScrollView(child: Text(message)),
actions: [
Expand Down
8 changes: 3 additions & 5 deletions lib/widgets/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'sticky_header.dart';
import 'store.dart';

class MessageList extends StatefulWidget {
const MessageList({Key? key}) : super(key: key);
const MessageList({super.key});

@override
State<StatefulWidget> createState() => _MessageListState();
Expand Down Expand Up @@ -212,8 +212,7 @@ class StreamTopicRecipientHeader extends StatelessWidget {
RecipientHeaderChevronContainer(
color: streamColor,
// TODO globe/lock icons for web-public and private streams
child:
Text(streamName, style: TextStyle(color: contrastingColor))),
child: Text(streamName, style: TextStyle(color: contrastingColor))),
Expanded(
child: Padding(
// Web has padding 9, 3, 3, 2 here; but 5px is the chevron.
Expand All @@ -229,8 +228,7 @@ class StreamTopicRecipientHeader extends StatelessWidget {
}
}

final _kStreamMessageBorderColor =
const HSLColor.fromAHSL(1, 0, 0, 0.88).toColor();
final _kStreamMessageBorderColor = const HSLColor.fromAHSL(1, 0, 0, 0.88).toColor();

class PmRecipientHeader extends StatelessWidget {
const PmRecipientHeader({super.key, required this.message});
Expand Down
27 changes: 14 additions & 13 deletions lib/widgets/sticky_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ class RenderSliverStickyHeaderList extends RenderSliverList {

double childScrollOffset;
if (innerChild.direction == constraints.axisDirection) {
childScrollOffset =
math.max(0.0, scrollOffset - parentData.layoutOffset!);
childScrollOffset = math.max(0.0,
scrollOffset - parentData.layoutOffset!);
} else {
final childEndOffset =
parentData.layoutOffset! + child.size.onAxis(constraints.axis);
// TODO should this be our layoutExtent or paintExtent, or what?
childScrollOffset = math.max(
0.0, childEndOffset - (scrollOffset + geometry!.layoutExtent));
childScrollOffset = math.max(0.0,
childEndOffset - (scrollOffset + geometry!.layoutExtent));
}
innerChild.provideScrollPosition(childScrollOffset);
}
Expand All @@ -211,11 +211,12 @@ class RenderSliverStickyHeaderList extends RenderSliverList {
enum StickyHeaderSlot { header, content }

class StickyHeader extends SlottedMultiChildRenderObjectWidget<StickyHeaderSlot, RenderBox> {
const StickyHeader(
{super.key,
const StickyHeader({
super.key,
this.direction = AxisDirection.down,
this.header,
this.content});
this.content,
});

final AxisDirection direction;
final Widget? header;
Expand All @@ -241,8 +242,7 @@ class StickyHeader extends SlottedMultiChildRenderObjectWidget<StickyHeaderSlot,
}
}

class RenderStickyHeader extends RenderBox
with SlottedContainerRenderObjectMixin<StickyHeaderSlot, RenderBox> {
class RenderStickyHeader extends RenderBox with SlottedContainerRenderObjectMixin<StickyHeaderSlot, RenderBox> {
RenderStickyHeader({required AxisDirection direction})
: _direction = direction;

Expand All @@ -260,8 +260,10 @@ class RenderStickyHeader extends RenderBox
}

@override
Iterable<RenderBox> get children =>
[if (_header != null) _header!, if (_content != null) _content!];
Iterable<RenderBox> get children => [
if (_header != null) _header!,
if (_content != null) _content!,
];

double? _slackSize;

Expand Down Expand Up @@ -351,8 +353,7 @@ class RenderStickyHeader extends RenderBox
return false;
}

BoxParentData _parentData(RenderBox child) =>
child.parentData! as BoxParentData;
BoxParentData _parentData(RenderBox child) => child.parentData! as BoxParentData;
}

Size sizeOn(Axis axis, {double main = 0, double cross = 0}) {
Expand Down
27 changes: 15 additions & 12 deletions lib/widgets/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class GlobalStoreWidget extends StatefulWidget {
/// * [PerAccountStoreWidget.of], for the user's data associated with a
/// particular Zulip account.
static GlobalStore of(BuildContext context) {
final widget = context
.dependOnInheritedWidgetOfExactType<_GlobalStoreInheritedWidget>();
final widget = context.dependOnInheritedWidgetOfExactType<_GlobalStoreInheritedWidget>();
assert(widget != null, 'No GlobalStoreWidget ancestor');
return widget!.store;
}
Expand Down Expand Up @@ -75,9 +74,10 @@ class _GlobalStoreWidgetState extends State<GlobalStoreWidget> {
// a [StatefulWidget] to get hold of the store, and an [InheritedWidget] to
// provide it to descendants, and one widget can't be both of those.
class _GlobalStoreInheritedWidget extends InheritedNotifier<GlobalStore> {
const _GlobalStoreInheritedWidget(
{required GlobalStore store, required super.child})
: super(notifier: store);
const _GlobalStoreInheritedWidget({
required GlobalStore store,
required super.child,
}) : super(notifier: store);

GlobalStore get store => notifier!;

Expand All @@ -103,8 +103,11 @@ class _GlobalStoreInheritedWidget extends InheritedNotifier<GlobalStore> {
/// * [GlobalStoreWidget], for the app's data beyond that of a
/// particular account.
class PerAccountStoreWidget extends StatefulWidget {
const PerAccountStoreWidget(
{super.key, required this.accountId, required this.child});
const PerAccountStoreWidget({
super.key,
required this.accountId,
required this.child,
});

final int accountId;
final Widget child;
Expand Down Expand Up @@ -137,8 +140,7 @@ class PerAccountStoreWidget extends StatefulWidget {
/// particular account.
/// * [InheritedNotifier], which provides the "dependency" mechanism.
static PerAccountStore of(BuildContext context) {
final widget = context
.dependOnInheritedWidgetOfExactType<_PerAccountStoreInheritedWidget>();
final widget = context.dependOnInheritedWidgetOfExactType<_PerAccountStoreInheritedWidget>();
assert(widget != null, 'No PerAccountStoreWidget ancestor');
return widget!.store;
}
Expand Down Expand Up @@ -223,9 +225,10 @@ class _PerAccountStoreWidgetState extends State<PerAccountStoreWidget> {
// [StatefulWidget] to get hold of the store, and an [InheritedWidget] to
// provide it to descendants, and one widget can't be both of those.
class _PerAccountStoreInheritedWidget extends InheritedNotifier<PerAccountStore> {
const _PerAccountStoreInheritedWidget(
{required PerAccountStore store, required super.child})
: super(notifier: store);
const _PerAccountStoreInheritedWidget({
required PerAccountStore store,
required super.child,
}) : super(notifier: store);

PerAccountStore get store => notifier!;

Expand Down

0 comments on commit 21cca5b

Please sign in to comment.