Skip to content

Commit 03b4dc7

Browse files
authored
[fix] digit separator handling in Color parsing (flutter#8525)
Add digit separator support to Color-parsing. Fixes: flutter#8510. --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide]([https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Dart contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Java and Kotlin contributions should strive to follow Java and Kotlin best practices ([discussion](flutter#8098)). </details>
1 parent fcef461 commit 03b4dc7

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/io/flutter/editor/ExpressionParsingUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class ExpressionParsingUtils {
2121
val = val.substring(0, index);
2222
}
2323
try {
24+
// Handle digit-separators.
25+
val = val.replaceAll("_", "");
2426
return val.startsWith("0x")
2527
? Integer.parseUnsignedInt(val.substring(2), 16)
2628
: Integer.parseUnsignedInt(val);

testSrc/unit/io/flutter/editor/FlutterColorProviderTest.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public void locatesColorCtor() throws Exception {
4242
});
4343
}
4444

45+
@Test
46+
public void locatesColorCtor_digitSeparators() throws Exception {
47+
run(() -> {
48+
final PsiElement testIdentifier = setUpDartElement("main() { Color(0xFF_E3_F2_FD); }", "Color", LeafPsiElement.class);
49+
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
50+
assertNotNull(color);
51+
final DartCallExpression element = DartSyntax.findEnclosingFunctionCall(testIdentifier, "Color");
52+
assertNotNull(element);
53+
});
54+
}
55+
56+
4557
@Test
4658
public void locatesConstColorCtor() throws Exception {
4759
run(() -> {
@@ -53,6 +65,17 @@ public void locatesConstColorCtor() throws Exception {
5365
});
5466
}
5567

68+
@Test
69+
public void locatesConstColorCtor_digitSeparators() throws Exception {
70+
run(() -> {
71+
final PsiElement testIdentifier = setUpDartElement("main() { const Color(0xFF_E3_F2_FD); }", "Color", LeafPsiElement.class);
72+
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
73+
assertNotNull(color);
74+
final DartNewExpression element = DartSyntax.findEnclosingNewExpression(testIdentifier);
75+
assertNotNull(element);
76+
});
77+
}
78+
5679
@Test
5780
public void locatesConstColorArray() throws Exception {
5881
run(() -> {
@@ -144,7 +167,8 @@ public void locatesColorArrayReference() throws Exception {
144167
@Test
145168
public void locatesCuppertinoColorReference() throws Exception {
146169
run(() -> {
147-
final PsiElement testIdentifier = setUpDartElement("main() { CupertinoColors.systemGreen; }", "CupertinoColors", LeafPsiElement.class);
170+
final PsiElement testIdentifier =
171+
setUpDartElement("main() { CupertinoColors.systemGreen; }", "CupertinoColors", LeafPsiElement.class);
148172
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
149173
assertNotNull(color);
150174
final DartReferenceExpression element = DartSyntax.findEnclosingReferenceExpression(testIdentifier);
@@ -155,7 +179,8 @@ public void locatesCuppertinoColorReference() throws Exception {
155179
@Test
156180
public void locatesColorReferenceWithComment() throws Exception {
157181
run(() -> {
158-
final PsiElement testIdentifier = setUpDartElement("main() { Colors . blue . /* darkish */ shade700; }", "shade700", LeafPsiElement.class);
182+
final PsiElement testIdentifier =
183+
setUpDartElement("main() { Colors . blue . /* darkish */ shade700; }", "shade700", LeafPsiElement.class);
159184
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
160185
assertNotNull(color);
161186
final DartReferenceExpression element = DartSyntax.findEnclosingReferenceExpression(testIdentifier);
@@ -166,7 +191,8 @@ public void locatesColorReferenceWithComment() throws Exception {
166191
@Test
167192
public void locatesCuppertinoColorReferenceWithWitespace() throws Exception {
168193
run(() -> {
169-
final PsiElement testIdentifier = setUpDartElement("main() { CupertinoColors . systemGreen; }", "CupertinoColors", LeafPsiElement.class);
194+
final PsiElement testIdentifier =
195+
setUpDartElement("main() { CupertinoColors . systemGreen; }", "CupertinoColors", LeafPsiElement.class);
170196
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
171197
assertNotNull(color);
172198
final DartReferenceExpression element = DartSyntax.findEnclosingReferenceExpression(testIdentifier);
@@ -177,12 +203,12 @@ public void locatesCuppertinoColorReferenceWithWitespace() throws Exception {
177203
@Test
178204
public void locatesCuppertinoColorReferenceWithLineEndComment() throws Exception {
179205
run(() -> {
180-
final PsiElement testIdentifier = setUpDartElement("main() { CupertinoColors . // comment\n systemGreen; }", "CupertinoColors", LeafPsiElement.class);
206+
final PsiElement testIdentifier =
207+
setUpDartElement("main() { CupertinoColors . // comment\n systemGreen; }", "CupertinoColors", LeafPsiElement.class);
181208
final Color color = new FlutterColorProvider().getColorFrom(testIdentifier);
182209
assertNotNull(color);
183210
final DartReferenceExpression element = DartSyntax.findEnclosingReferenceExpression(testIdentifier);
184211
assertNotNull(element);
185212
});
186213
}
187-
188214
}

0 commit comments

Comments
 (0)