|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:_fe_analyzer_shared/src/parser/util.dart'; |
| 6 | +import 'package:analysis_server/src/services/correction/assist.dart'; |
| 7 | +import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; |
| 8 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/dart/ast/token.dart'; |
| 10 | +import 'package:analyzer_plugin/utilities/assist/assist.dart'; |
| 11 | +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; |
| 12 | +import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| 13 | + |
| 14 | +class AddDigitSeparatorEveryThreeDigits extends _AddDigitSeparators { |
| 15 | + AddDigitSeparatorEveryThreeDigits({required super.context}); |
| 16 | + |
| 17 | + @override |
| 18 | + int get _digitsPerGroup => 3; |
| 19 | + |
| 20 | + @override |
| 21 | + int get _minimumDigitCount => 5; |
| 22 | + |
| 23 | + @override |
| 24 | + Set<TokenType> get _tokenTypes => const { |
| 25 | + TokenType.INT, |
| 26 | + TokenType.INT_WITH_SEPARATORS, |
| 27 | + TokenType.DOUBLE, |
| 28 | + TokenType.DOUBLE_WITH_SEPARATORS, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +class AddDigitSeparatorEveryTwoDigits extends _AddDigitSeparators { |
| 33 | + AddDigitSeparatorEveryTwoDigits({required super.context}); |
| 34 | + |
| 35 | + @override |
| 36 | + int get _digitsPerGroup => 2; |
| 37 | + |
| 38 | + @override |
| 39 | + int get _minimumDigitCount => 4; |
| 40 | + |
| 41 | + @override |
| 42 | + Set<TokenType> get _tokenTypes => |
| 43 | + const {TokenType.HEXADECIMAL, TokenType.HEXADECIMAL_WITH_SEPARATORS}; |
| 44 | +} |
| 45 | + |
| 46 | +abstract class _AddDigitSeparators extends ResolvedCorrectionProducer { |
| 47 | + _AddDigitSeparators({required super.context}); |
| 48 | + |
| 49 | + @override |
| 50 | + CorrectionApplicability get applicability => |
| 51 | + CorrectionApplicability.automatically; |
| 52 | + |
| 53 | + @override |
| 54 | + AssistKind get assistKind => DartAssistKind.ADD_DIGIT_SEPARATORS; |
| 55 | + |
| 56 | + /// The number of digits in each group, to be separated by a separator. |
| 57 | + int get _digitsPerGroup; |
| 58 | + |
| 59 | + /// The minimum number of digits in order to offer the correction. |
| 60 | + int get _minimumDigitCount; |
| 61 | + |
| 62 | + /// The token types for which this correction is applicable. |
| 63 | + Set<TokenType> get _tokenTypes; |
| 64 | + |
| 65 | + @override |
| 66 | + Future<void> compute(ChangeBuilder builder) async { |
| 67 | + var node = this.node; |
| 68 | + if (node is IntegerLiteral) { |
| 69 | + if (!_tokenTypes.contains(node.literal.type)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + // We scrap whatever separators are in place. |
| 73 | + var source = stripSeparators(node.literal.lexeme); |
| 74 | + var isHex = node.literal.type == TokenType.HEXADECIMAL || |
| 75 | + node.literal.type == TokenType.HEXADECIMAL_WITH_SEPARATORS; |
| 76 | + if (isHex) { |
| 77 | + const hexPrefixLength = '0x'.length; |
| 78 | + var replacement = _addSeparators(source.substring(hexPrefixLength)); |
| 79 | + if (replacement == null) return; |
| 80 | + // Prefix the '0x' text. |
| 81 | + replacement = '${source.substring(0, hexPrefixLength)}$replacement'; |
| 82 | + // Don't offer the correction if the result is unchanged. |
| 83 | + if (replacement == node.literal.lexeme) return; |
| 84 | + await builder.addDartFileEdit(file, (builder) { |
| 85 | + builder.addSimpleReplacement(range.node(node), replacement!); |
| 86 | + }); |
| 87 | + } else { |
| 88 | + var replacement = _addSeparators(source); |
| 89 | + if (replacement == null) return; |
| 90 | + // Don't offer the correction if the result is unchanged. |
| 91 | + if (replacement == node.literal.lexeme) return; |
| 92 | + await builder.addDartFileEdit(file, (builder) { |
| 93 | + builder.addSimpleReplacement(range.node(node), replacement); |
| 94 | + }); |
| 95 | + } |
| 96 | + } else if (node is DoubleLiteral) { |
| 97 | + if (!_tokenTypes.contains(node.literal.type)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + // We scrap whatever separators are in place. |
| 101 | + var source = stripSeparators(node.literal.lexeme); |
| 102 | + |
| 103 | + String wholePart; |
| 104 | + String? fractionalPart; |
| 105 | + String? exponentialPart; |
| 106 | + var exponentialPartIsNegative = false; |
| 107 | + |
| 108 | + var eIndex = source.indexOf('e'); |
| 109 | + if (eIndex == -1) eIndex = source.indexOf('E'); |
| 110 | + if (eIndex > -1) { |
| 111 | + if (source.codeUnitAt(eIndex + 1) == 0x2D /* '-' */) { |
| 112 | + exponentialPartIsNegative = true; |
| 113 | + exponentialPart = source.substring(eIndex + 2); |
| 114 | + } else { |
| 115 | + exponentialPart = source.substring(eIndex + 1); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + var decimalIndex = source.indexOf('.'); |
| 120 | + if (decimalIndex > -1) { |
| 121 | + wholePart = source.substring(0, decimalIndex); |
| 122 | + fractionalPart = eIndex > -1 |
| 123 | + ? source.substring(decimalIndex + 1, eIndex) |
| 124 | + : source.substring(decimalIndex + 1); |
| 125 | + } else { |
| 126 | + assert( |
| 127 | + eIndex > -1, |
| 128 | + "There is neither a decimal point, nor an 'e', nor an 'E', in this " |
| 129 | + "double literal: '${node.literal.lexeme}'"); |
| 130 | + wholePart = source.substring(0, eIndex); |
| 131 | + fractionalPart = null; |
| 132 | + } |
| 133 | + |
| 134 | + var buffer = StringBuffer(); |
| 135 | + var replacement = _addSeparators(wholePart); |
| 136 | + buffer.write(replacement ?? wholePart); |
| 137 | + |
| 138 | + if (fractionalPart != null) { |
| 139 | + buffer.write('.'); |
| 140 | + |
| 141 | + // Reverse the fractional part, so that separators are aligned to the |
| 142 | + // left instead of the right. |
| 143 | + var fractionalPartReversed = fractionalPart.split('').reversed.join(); |
| 144 | + replacement = _addSeparators(fractionalPartReversed); |
| 145 | + |
| 146 | + // Reverse back the replacement. |
| 147 | + replacement = replacement?.split('').reversed.join(); |
| 148 | + buffer.write(replacement ?? fractionalPart); |
| 149 | + } |
| 150 | + |
| 151 | + if (exponentialPart != null) { |
| 152 | + buffer.write(source.substring(eIndex, eIndex + 1)); |
| 153 | + if (exponentialPartIsNegative) buffer.write('-'); |
| 154 | + replacement = _addSeparators(exponentialPart); |
| 155 | + buffer.write(replacement ?? exponentialPart); |
| 156 | + } |
| 157 | + |
| 158 | + replacement = buffer.toString(); |
| 159 | + // Don't offer the correction if the result is unchanged. |
| 160 | + if (replacement == node.literal.lexeme) return; |
| 161 | + |
| 162 | + await builder.addDartFileEdit(file, (builder) { |
| 163 | + builder.addSimpleReplacement(range.node(node), buffer.toString()); |
| 164 | + }); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /// Adds separators to [digits], returning the result. |
| 169 | + String? _addSeparators(String digits) { |
| 170 | + // Only offer to add separators when the number has some minimum number of |
| 171 | + // digits. |
| 172 | + var digitCount = digits.length; |
| 173 | + if (digitCount < _minimumDigitCount) return null; |
| 174 | + |
| 175 | + var buffer = StringBuffer(); |
| 176 | + var offset = digitCount % _digitsPerGroup; |
| 177 | + if (offset == 0) offset = _digitsPerGroup; |
| 178 | + buffer.write(digits.substring(0, offset)); |
| 179 | + offset += _digitsPerGroup; |
| 180 | + for (; offset <= digits.length; offset += _digitsPerGroup) { |
| 181 | + buffer.write('_'); |
| 182 | + buffer.write(digits.substring(offset - _digitsPerGroup, offset)); |
| 183 | + } |
| 184 | + return buffer.toString(); |
| 185 | + } |
| 186 | +} |
0 commit comments