Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(760)

Side by Side Diff: pkg/analysis_server/lib/src/services/refactoring/inline_local.dart

Issue 695383002: Issue 21496. Fix for inlining negate/decrement into negate. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/inline_local_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.src.refactoring.inline_local; 5 library services.src.refactoring.inline_local;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/src/protocol_server.dart' hide Element; 9 import 'package:analysis_server/src/protocol_server.dart' hide Element;
10 import 'package:analysis_server/src/services/correction/source_range.dart'; 10 import 'package:analysis_server/src/services/correction/source_range.dart';
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 _variableNode.getAncestor((node) => node is VariableDeclarationStateme nt); 130 _variableNode.getAncestor((node) => node is VariableDeclarationStateme nt);
131 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); 131 SourceRange range = utils.getLinesRangeStatements([declarationStatement]);
132 doSourceChange_addElementEdit( 132 doSourceChange_addElementEdit(
133 change, 133 change,
134 unitElement, 134 unitElement,
135 newSourceEdit_range(range, '')); 135 newSourceEdit_range(range, ''));
136 } 136 }
137 // prepare initializer 137 // prepare initializer
138 Expression initializer = _variableNode.initializer; 138 Expression initializer = _variableNode.initializer;
139 String initializerCode = utils.getNodeText(initializer); 139 String initializerCode = utils.getNodeText(initializer);
140 int initializerPrecedence = getExpressionPrecedence(initializer);
141 // replace references 140 // replace references
142 for (SearchMatch reference in _references) { 141 for (SearchMatch reference in _references) {
143 SourceRange range = reference.sourceRange; 142 SourceRange range = reference.sourceRange;
144 // prepare context 143 // prepare context
145 int offset = range.offset; 144 int offset = range.offset;
146 AstNode node = utils.findNode(offset); 145 AstNode node = utils.findNode(offset);
147 AstNode parent = node.parent; 146 AstNode parent = node.parent;
148 // prepare code 147 // prepare code
149 String codeForReference; 148 String codeForReference;
150 if (parent is InterpolationExpression) { 149 if (parent is InterpolationExpression) {
(...skipping 13 matching lines...) Expand all
164 codeForReference = codeForReference.substring(1); 163 codeForReference = codeForReference.substring(1);
165 } else if (codeForReference.startsWith('\r\n')) { 164 } else if (codeForReference.startsWith('\r\n')) {
166 codeForReference = codeForReference.substring(2); 165 codeForReference = codeForReference.substring(2);
167 } 166 }
168 } 167 }
169 } else if (_shouldBeExpressionInterpolation(parent, initializer)) { 168 } else if (_shouldBeExpressionInterpolation(parent, initializer)) {
170 codeForReference = '{$initializerCode}'; 169 codeForReference = '{$initializerCode}';
171 } else { 170 } else {
172 codeForReference = initializerCode; 171 codeForReference = initializerCode;
173 } 172 }
174 } else if (initializerPrecedence < getExpressionParentPrecedence(node)) { 173 } else if (_shouldUseParenthesis(initializer, node)) {
175 codeForReference = '($initializerCode)'; 174 codeForReference = '($initializerCode)';
176 } else { 175 } else {
177 codeForReference = initializerCode; 176 codeForReference = initializerCode;
178 } 177 }
179 // do replace 178 // do replace
180 doSourceChange_addElementEdit( 179 doSourceChange_addElementEdit(
181 change, 180 change,
182 unitElement, 181 unitElement,
183 newSourceEdit_range(range, codeForReference)); 182 newSourceEdit_range(range, codeForReference));
184 } 183 }
185 // done 184 // done
186 return new Future.value(change); 185 return new Future.value(change);
187 } 186 }
188 187
189 @override 188 @override
190 bool requiresPreview() => false; 189 bool requiresPreview() => false;
191 190
192 static bool _shouldBeExpressionInterpolation(InterpolationExpression target, 191 static bool _shouldBeExpressionInterpolation(InterpolationExpression target,
193 Expression expression) { 192 Expression expression) {
194 TokenType targetType = target.beginToken.type; 193 TokenType targetType = target.beginToken.type;
195 return targetType == TokenType.STRING_INTERPOLATION_IDENTIFIER && 194 return targetType == TokenType.STRING_INTERPOLATION_IDENTIFIER &&
196 expression is! SimpleIdentifier; 195 expression is! SimpleIdentifier;
197 } 196 }
197
198 static bool _shouldUseParenthesis(Expression init, AstNode node) {
199 // check precedence
200 int initPrecedence = getExpressionPrecedence(init);
201 if (initPrecedence < getExpressionParentPrecedence(node)) {
202 return true;
203 }
204 // special case for '-'
205 AstNode parent = node.parent;
206 if (init is PrefixExpression && parent is PrefixExpression) {
207 if (parent.operator.type == TokenType.MINUS) {
208 TokenType initializerOperator = init.operator.type;
209 if (initializerOperator == TokenType.MINUS ||
210 initializerOperator == TokenType.MINUS_MINUS) {
211 return true;
212 }
213 }
214 }
215 // no () is needed
216 return false;
217 }
198 } 218 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/inline_local_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698