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

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

Issue 633823002: Issue 21233. Improve inlining strings into string interpolations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 months 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/pubspec.yaml » ('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/status.dart'; 11 import 'package:analysis_server/src/services/correction/status.dart';
11 import 'package:analysis_server/src/services/correction/util.dart'; 12 import 'package:analysis_server/src/services/correction/util.dart';
12 import 'package:analysis_server/src/services/refactoring/refactoring.dart'; 13 import 'package:analysis_server/src/services/refactoring/refactoring.dart';
13 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da rt'; 14 import 'package:analysis_server/src/services/refactoring/refactoring_internal.da rt';
14 import 'package:analysis_server/src/services/search/search_engine.dart'; 15 import 'package:analysis_server/src/services/search/search_engine.dart';
15 import 'package:analyzer/src/generated/ast.dart'; 16 import 'package:analyzer/src/generated/ast.dart';
16 import 'package:analyzer/src/generated/element.dart'; 17 import 'package:analyzer/src/generated/element.dart';
17 import 'package:analyzer/src/generated/java_core.dart'; 18 import 'package:analyzer/src/generated/java_core.dart';
18 import 'package:analyzer/src/generated/scanner.dart'; 19 import 'package:analyzer/src/generated/scanner.dart';
19 import 'package:analyzer/src/generated/source.dart'; 20 import 'package:analyzer/src/generated/source.dart';
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 Statement declarationStatement = 129 Statement declarationStatement =
129 _variableNode.getAncestor((node) => node is VariableDeclarationStateme nt); 130 _variableNode.getAncestor((node) => node is VariableDeclarationStateme nt);
130 SourceRange range = utils.getLinesRangeStatements([declarationStatement]); 131 SourceRange range = utils.getLinesRangeStatements([declarationStatement]);
131 doSourceChange_addElementEdit( 132 doSourceChange_addElementEdit(
132 change, 133 change,
133 unitElement, 134 unitElement,
134 newSourceEdit_range(range, '')); 135 newSourceEdit_range(range, ''));
135 } 136 }
136 // prepare initializer 137 // prepare initializer
137 Expression initializer = _variableNode.initializer; 138 Expression initializer = _variableNode.initializer;
138 String initializerSource = utils.getNodeText(initializer); 139 String initializerCode = utils.getNodeText(initializer);
139 int initializerPrecedence = getExpressionPrecedence(initializer); 140 int initializerPrecedence = getExpressionPrecedence(initializer);
140 // replace references 141 // replace references
141 for (SearchMatch reference in _references) { 142 for (SearchMatch reference in _references) {
142 SourceRange range = reference.sourceRange; 143 SourceRange range = reference.sourceRange;
143 String sourceForReference = 144 // prepare context
144 _getSourceForReference(range, initializerSource, initializerPrecedence ); 145 int offset = range.offset;
146 AstNode node = utils.findNode(offset);
147 AstNode parent = node.parent;
148 // prepare code
149 String codeForReference;
150 if (parent is InterpolationExpression) {
151 StringInterpolation stringInterpolation = parent.parent;
152 if (initializer is SingleStringLiteral && !initializer.isRaw) {
153 range = rangeNode(parent);
154 int initOffset = initializer.contentsOffset;
155 int initLength = initializer.contentsEnd - initOffset;
156 codeForReference = utils.getText(initOffset, initLength);
Paul Berry 2014/10/07 01:14:56 I think there will be a problem if the two strings
scheglov 2014/10/07 16:24:24 Done.
157 } else if (_isIdentifierStringInterpolation(parent)) {
158 codeForReference = '{$initializerCode}';
159 } else {
160 codeForReference = initializerCode;
161 }
162 } else if (initializerPrecedence < getExpressionParentPrecedence(node)) {
163 codeForReference = '($initializerCode)';
164 } else {
165 codeForReference = initializerCode;
166 }
167 // do replace
145 doSourceChange_addElementEdit( 168 doSourceChange_addElementEdit(
146 change, 169 change,
147 unitElement, 170 unitElement,
148 newSourceEdit_range(range, sourceForReference)); 171 newSourceEdit_range(range, codeForReference));
149 } 172 }
150 // done 173 // done
151 return new Future.value(change); 174 return new Future.value(change);
152 } 175 }
153 176
154 @override 177 @override
155 bool requiresPreview() => false; 178 bool requiresPreview() => false;
156 179
157 /** 180 static bool _isIdentifierStringInterpolation(InterpolationExpression e) {
158 * Returns the source which should be used to replace the reference with the 181 return e.beginToken.type == TokenType.STRING_INTERPOLATION_IDENTIFIER;
159 * given [SourceRange].
160 *
161 * [range] - the [SourceRange] of the reference.
162 * [source] - the source of the initializer, to be inserted at [range].
163 * [precedence] - the precedence of the initializer [source].
164 */
165 String _getSourceForReference(SourceRange range, String source,
166 int precedence) {
167 int offset = range.offset;
168 AstNode node = utils.findNode(offset);
169 AstNode parent = node.parent;
170 if (_isIdentifierStringInterpolation(parent)) {
171 return '{${source}}';
172 }
173 if (precedence < getExpressionParentPrecedence(node)) {
174 return '(${source})';
175 }
176 return source;
177 }
178
179 /**
180 * Checks if the given node is a string interpolation in form `$name`.
181 */
182 bool _isIdentifierStringInterpolation(AstNode parent) {
183 if (parent is InterpolationExpression) {
184 InterpolationExpression element = parent;
185 return element.beginToken.type ==
186 TokenType.STRING_INTERPOLATION_IDENTIFIER;
187 }
188 return false;
189 } 182 }
190 } 183 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698