Extract shorten() into string utils.

R=brianwilkerson@google.com

Bug:
Change-Id: I68d17461b4b5b478df5713cadb120a130f492406
Reviewed-on: https://dart-review.googlesource.com/16602
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov
2017-10-25 18:54:34 +00:00
parent cded10154c
commit 577bb2dc7e
4 changed files with 30 additions and 11 deletions
@@ -2,6 +2,7 @@
// 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 'package:analysis_server/src/services/correction/strings.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
@@ -187,13 +188,13 @@ String getWidgetPresentationText(InstanceCreationExpression node) {
if (_isExactWidget(
element, 'Icon', 'package:flutter/src/widgets/icon.dart')) {
String text = arguments[0].toString();
String arg = _shortenText(text, 32);
String arg = shorten(text, 32);
return 'Icon($arg)';
}
if (_isExactWidget(
element, 'Text', 'package:flutter/src/widgets/text.dart')) {
String text = arguments[0].toString();
String arg = _shortenText(text, 32);
String arg = shorten(text, 32);
return 'Text($arg)';
}
return element.name;
@@ -257,11 +258,3 @@ bool _isExactWidget(ClassElement element, String type, String uri) {
element.name == type &&
element.source.uri.toString() == uri;
}
String _shortenText(String text, int limit) {
if (text.length > limit) {
int half = limit ~/ 2 - 2;
return text.substring(0, half) + '...' + text.substring(text.length - half);
}
return text;
}
@@ -218,6 +218,20 @@ String repeat(String s, int n) {
return sb.toString();
}
/**
* If the [text] length is above the [limit], replace the middle with `...`.
*/
String shorten(String text, int limit) {
if (text.length > limit) {
int headLength = limit ~/ 2 - 1;
int tailLength = limit - headLength - 3;
return text.substring(0, headLength) +
'...' +
text.substring(text.length - tailLength);
}
return text;
}
/**
* Gets the substring after the last occurrence of a separator.
* The separator is not returned.
@@ -60,7 +60,7 @@ var w = const Text('${'abc' * 100}');
''');
var w = _getTopVariableCreation('w');
expect(
getWidgetPresentationText(w), "Text('abcabcabcabca...cabcabcabcabc')");
getWidgetPresentationText(w), "Text('abcabcabcabcab...cabcabcabcabc')");
}
test_getFlutterWidgetPresentationText_unresolved() async {
@@ -154,6 +154,18 @@ class StringsTest {
expect(repeat('abc', 3), 'abcabcabc');
}
void test_shorten() {
expect(shorten('', 10), '');
expect(shorten('0', 10), '0');
expect(shorten('012', 10), '012');
expect(shorten('0123456789', 10), '0123456789');
expect(shorten('0123456789abcd', 10), '0123...bcd');
expect(shorten('0123456789abcde', 10), '0123...cde');
expect(shorten('0123456789abcdef', 10), '0123...def');
expect(shorten('0123456789abcdef', 11), '0123...cdef');
expect(shorten('0123456789abcdef', 12), '01234...cdef');
}
void test_substringAfterLast() {
expect(substringAfterLast('', '/'), '');
expect(substringAfterLast('abc', ''), '');