diff --git a/pkg/analysis_server/lib/src/services/correction/flutter_util.dart b/pkg/analysis_server/lib/src/services/correction/flutter_util.dart index aa85215be29..7fe7cdcc559 100644 --- a/pkg/analysis_server/lib/src/services/correction/flutter_util.dart +++ b/pkg/analysis_server/lib/src/services/correction/flutter_util.dart @@ -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; -} diff --git a/pkg/analysis_server/lib/src/services/correction/strings.dart b/pkg/analysis_server/lib/src/services/correction/strings.dart index f7fb39e7652..33a4c6a99d3 100644 --- a/pkg/analysis_server/lib/src/services/correction/strings.dart +++ b/pkg/analysis_server/lib/src/services/correction/strings.dart @@ -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. diff --git a/pkg/analysis_server/test/services/correction/flutter_util_test.dart b/pkg/analysis_server/test/services/correction/flutter_util_test.dart index dff3c419702..f43a39c0559 100644 --- a/pkg/analysis_server/test/services/correction/flutter_util_test.dart +++ b/pkg/analysis_server/test/services/correction/flutter_util_test.dart @@ -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 { diff --git a/pkg/analysis_server/test/services/correction/strings_test.dart b/pkg/analysis_server/test/services/correction/strings_test.dart index 6ff5aaad75a..abd449379e1 100644 --- a/pkg/analysis_server/test/services/correction/strings_test.dart +++ b/pkg/analysis_server/test/services/correction/strings_test.dart @@ -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', ''), '');