Skip to content

Commit 38df6ea

Browse files
committed
Only show closing labels that have nesting.
Bug: Change-Id: Ifc9177973961f2a74be3e2871aa2d58c28007478 Reviewed-on: https://dart-review.googlesource.com/29540 Reviewed-by: Brian Wilkerson <[email protected]>
1 parent df479ca commit 38df6ea

File tree

2 files changed

+178
-68
lines changed

2 files changed

+178
-68
lines changed

pkg/analysis_server/lib/src/computer/computer_closingLabels.dart

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
88
import 'package:analyzer/src/generated/source.dart';
99

10-
// TODO(devoncarew): We should look into not creating any labels until there's
11-
// at least 2 levels of nesting.
12-
1310
/**
1411
* A computer for [CompilationUnit] closing labels.
1512
*/
1613
class DartUnitClosingLabelsComputer {
1714
final LineInfo _lineInfo;
1815
final CompilationUnit _unit;
1916
final List<ClosingLabel> _closingLabels = [];
17+
final Set<ClosingLabel> hasNestingSet = new Set();
18+
final Set<ClosingLabel> isSingleLineSet = new Set();
2019

2120
DartUnitClosingLabelsComputer(this._lineInfo, this._unit);
2221

@@ -25,7 +24,20 @@ class DartUnitClosingLabelsComputer {
2524
*/
2625
List<ClosingLabel> compute() {
2726
_unit.accept(new _DartUnitClosingLabelsComputerVisitor(this));
28-
return _closingLabels;
27+
28+
return _closingLabels.where((ClosingLabel label) {
29+
// Filter labels that don't have some nesting.
30+
// Filter labels that start and end on the same line.
31+
return hasNestingSet.contains(label) && !isSingleLineSet.contains(label);
32+
}).toList();
33+
}
34+
35+
void setHasNesting(ClosingLabel label) {
36+
hasNestingSet.add(label);
37+
}
38+
39+
void setSingleLine(ClosingLabel label) {
40+
isSingleLineSet.add(label);
2941
}
3042
}
3143

@@ -37,36 +49,53 @@ class _DartUnitClosingLabelsComputerVisitor
3749
final DartUnitClosingLabelsComputer computer;
3850

3951
int interpolatedStringsEntered = 0;
52+
List<ClosingLabel> labelStack = [];
4053

4154
_DartUnitClosingLabelsComputerVisitor(this.computer);
4255

4356
@override
4457
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
58+
ClosingLabel label;
59+
4560
if (node.argumentList != null) {
46-
var label = node.constructorName.type.name.name;
61+
String labelText = node.constructorName.type.name.name;
4762
if (node.constructorName.name != null) {
48-
label += ".${node.constructorName.name.name}";
63+
labelText += ".${node.constructorName.name.name}";
4964
}
5065
// We override the node used for doing line calculations because otherwise
5166
// constructors that split over multiple lines (but have parens on same
5267
// line) would incorrectly get labels, because node.start on an instance
5368
// creation expression starts at the start of the expression.
54-
_addLabel(node, label, checkLinesUsing: node.argumentList);
69+
label = _addLabel(node, labelText, checkLinesUsing: node.argumentList);
5570
}
5671

57-
return super.visitInstanceCreationExpression(node);
72+
if (label != null) _pushLabel(label);
73+
74+
try {
75+
return super.visitInstanceCreationExpression(node);
76+
} finally {
77+
if (label != null) _popLabel();
78+
}
5879
}
5980

6081
@override
6182
Object visitListLiteral(ListLiteral node) {
6283
final NodeList<TypeAnnotation> args = node.typeArguments?.arguments;
6384
final String typeName = args != null ? args[0]?.toString() : null;
6485

86+
ClosingLabel label;
87+
6588
if (typeName != null) {
66-
_addLabel(node, "<$typeName>[]");
89+
label = _addLabel(node, "<$typeName>[]");
6790
}
6891

69-
return super.visitListLiteral(node);
92+
if (label != null) _pushLabel(label);
93+
94+
try {
95+
return super.visitListLiteral(node);
96+
} finally {
97+
if (label != null) _popLabel();
98+
}
7099
}
71100

72101
@override
@@ -79,10 +108,11 @@ class _DartUnitClosingLabelsComputerVisitor
79108
}
80109
}
81110

82-
void _addLabel(AstNode node, String label, {AstNode checkLinesUsing}) {
111+
ClosingLabel _addLabel(AstNode node, String label,
112+
{AstNode checkLinesUsing}) {
83113
// Never add labels if we're inside strings.
84114
if (interpolatedStringsEntered > 0) {
85-
return;
115+
return null;
86116
}
87117

88118
checkLinesUsing = checkLinesUsing ?? node;
@@ -92,14 +122,32 @@ class _DartUnitClosingLabelsComputerVisitor
92122
final LineInfo_Location end =
93123
computer._lineInfo.getLocation(checkLinesUsing.end - 1);
94124

125+
final ClosingLabel closingLabel =
126+
new ClosingLabel(node.offset, node.length, label);
127+
95128
int spannedLines = end.lineNumber - start.lineNumber;
96129
if (spannedLines < 1) {
97-
return;
130+
computer.setSingleLine(closingLabel);
98131
}
99132

100-
final ClosingLabel closingLabel =
101-
new ClosingLabel(node.offset, node.length, label);
133+
ClosingLabel parent = _currentLabel;
134+
if (parent != null) {
135+
computer.setHasNesting(parent);
136+
computer.setHasNesting(closingLabel);
137+
}
102138

103139
computer._closingLabels.add(closingLabel);
140+
141+
return closingLabel;
142+
}
143+
144+
void _pushLabel(ClosingLabel label) {
145+
labelStack.add(label);
146+
}
147+
148+
ClosingLabel get _currentLabel => labelStack.isEmpty ? null : labelStack.last;
149+
150+
void _popLabel() {
151+
labelStack.removeLast();
104152
}
105153
}

0 commit comments

Comments
 (0)