-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Format try statements. * Add try comment tests just in case. * Use token's API to add spaces. * Remove unnecessary override after rebase. * Put the exception into a delimited list and everything else into an adjacentpiece. * Remove unnecessary space.
- Loading branch information
Showing
6 changed files
with
383 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
import '../back_end/code_writer.dart'; | ||
import 'piece.dart'; | ||
|
||
/// A piece for a try statement. | ||
class TryPiece extends Piece { | ||
final List<_TrySectionPiece> _sections = []; | ||
|
||
void add(Piece header, Piece block) { | ||
_sections.add(_TrySectionPiece(header, block)); | ||
} | ||
|
||
@override | ||
void format(CodeWriter writer, State state) { | ||
for (var i = 0; i < _sections.length; i++) { | ||
var section = _sections[i]; | ||
writer.format(section.header); | ||
writer.space(); | ||
writer.format(section.body); | ||
|
||
// Adds the space between the end of a block and the next header. | ||
if (i < _sections.length - 1) { | ||
writer.space(); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
void forEachChild(void Function(Piece piece) callback) { | ||
for (var section in _sections) { | ||
callback(section.header); | ||
callback(section.body); | ||
} | ||
} | ||
} | ||
|
||
/// A section for a try. | ||
/// | ||
/// This could be a try, an on/catch, or a finally section. They are all | ||
/// formatted similarly. | ||
class _TrySectionPiece { | ||
final Piece header; | ||
final Piece body; | ||
|
||
_TrySectionPiece(this.header, this.body); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
40 columns | | ||
>>> Try and catch exception. | ||
try { | ||
doSomething(); | ||
} catch (e) { | ||
print(e); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} catch (e) { | ||
print(e); | ||
} | ||
>>> Try and catch exception with on clause. | ||
try{ | ||
doSomething(); | ||
}on Exception catch (e){ | ||
print(e); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} on Exception catch (e) { | ||
print(e); | ||
} | ||
>>> Try and catch exception with on clause and stack trace. | ||
try{ | ||
doSomething(); | ||
}on Exception catch (e, s){ | ||
print(e); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} on Exception catch (e, s) { | ||
print(e); | ||
} | ||
>>> Split empty catch if there is a finally. | ||
try {;} catch (err) {} finally {;} | ||
<<< | ||
try { | ||
; | ||
} catch (err) { | ||
} finally { | ||
; | ||
} | ||
>>> Split empty on if there is a finally. | ||
try {;} on Exception {} finally {;} | ||
<<< | ||
try { | ||
; | ||
} on Exception { | ||
} finally { | ||
; | ||
} | ||
>>> Split all empty catches if there is a finally. | ||
try {;} catch (err1) {} catch (err2) {} catch (err3) {} finally {;} | ||
<<< | ||
try { | ||
; | ||
} catch (err1) { | ||
} catch (err2) { | ||
} catch (err3) { | ||
} finally { | ||
; | ||
} | ||
>>> Split leading empty catches if there are multiple. | ||
try {;} catch (err1) {} catch (err2) {} catch (err3) {} | ||
<<< | ||
try { | ||
; | ||
} catch (err1) { | ||
} catch (err2) { | ||
} catch (err3) {} | ||
>>> Split empty catch with on clause if there is a finally. | ||
try { | ||
doSomething(); | ||
} on Exception catch (e) {} finally { | ||
cleanupSomething(); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} on Exception catch (e) { | ||
} finally { | ||
cleanupSomething(); | ||
} | ||
>>> Split multiple on clauses. | ||
try { | ||
doSomething(); | ||
} on FooException {} on BarException { | ||
doSomething(); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} on FooException { | ||
} on BarException { | ||
doSomething(); | ||
} | ||
>>> Don't split try. | ||
try { | ||
doSomething(); | ||
} on FooException {} on BarException { | ||
doSomething(); | ||
} | ||
<<< | ||
try { | ||
doSomething(); | ||
} on FooException { | ||
} on BarException { | ||
doSomething(); | ||
} |
Oops, something went wrong.