Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format switch cases that aren't valid patterns. (dart-lang#1177)
* Better style for inline case bodies. In the previous PR, any case body that fit on one line was allowed to even if other cases in the same switch didn't. I tested it on a corpus and I found that led to confusing switches where it wasn't always clear where the case body starts. I think you really want it all or nothing: either every single case fits on the same line in which case you can make the whole switch compact, or every case should be on its own line, even the ones that would fit. Unfortunately, it's a little tricky to have formatter rules that span code containing hard splits, so getting that working took some doing. It also regressed performance pretty badly. But I figured out some optimizations in ChunkBuilder and it's basically back to the same performance it had before. Also, this incidentally fixes a bug where parameter metadata in trailing comma parameter lists was also supposed to have that same all-or-nothing splitting logic but didn't. I've tried this on a corpus and I'm pretty happy with the results. Right now, relatively few switches benefit because the mandatory breaks mean a lot of switches have at least two statements (which always causes the case to split). But as those breaks are removed, I think we'll see more compact switches. Even today, this code does improve some switches where every case is just a short return statement. * Format switch cases that aren't valid patterns. Fix dart-lang#1164. The solution is kind of hacky, but users will probably never run into it and it avoids complicated the user experience of the formatter. To get this working, I had to update to analyzer 5.5.0 because 5.4.0 had an assert failure when it tried to parse an invalid switch case. But 5.5.0 also has a bug which is causing a couple of formatter tests to fail: dart-lang/sdk#51415. I'll probably wait until there's a fix for that out before this gets merged to master. Analyzer 5.5.0 also changes some of the AST types. Refactored how binary expressions and patterns are formatted to avoid copy/paste from that change. * Better docs.
- Loading branch information