| Index: pkg/analysis_server/lib/src/services/refactoring/extract_method.dart
|
| diff --git a/pkg/analysis_server/lib/src/services/refactoring/extract_method.dart b/pkg/analysis_server/lib/src/services/refactoring/extract_method.dart
|
| index 2f33fe138cabe14fb2d6e813e5e336115b25c4f2..361d537c14e27bc73630659971c4b66f02ba8b7d 100644
|
| --- a/pkg/analysis_server/lib/src/services/refactoring/extract_method.dart
|
| +++ b/pkg/analysis_server/lib/src/services/refactoring/extract_method.dart
|
| @@ -27,10 +27,8 @@ import 'package:analyzer/src/generated/resolver.dart' show ExitDetector;
|
| import 'package:analyzer/src/generated/scanner.dart';
|
| import 'package:analyzer/src/generated/source.dart';
|
|
|
| -
|
| const String _TOKEN_SEPARATOR = '\uFFFF';
|
|
|
| -
|
| /**
|
| * Returns the "normalized" version of the given source, which is reconstructed
|
| * from tokens, so ignores all the comments and spaces.
|
| @@ -40,7 +38,6 @@ String _getNormalizedSource(String src) {
|
| return StringUtils.join(selectionTokens, _TOKEN_SEPARATOR);
|
| }
|
|
|
| -
|
| /**
|
| * Returns the [Map] which maps [map] values to their keys.
|
| */
|
| @@ -52,15 +49,14 @@ Map<String, String> _inverseMap(Map map) {
|
| return result;
|
| }
|
|
|
| -
|
| /**
|
| * [ExtractMethodRefactoring] implementation.
|
| */
|
| -class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| - ExtractMethodRefactoring {
|
| +class ExtractMethodRefactoringImpl extends RefactoringImpl
|
| + implements ExtractMethodRefactoring {
|
| static const ERROR_EXITS =
|
| 'Selected statements contain a return statement, but not all possible '
|
| - 'execuion flows exit. Semantics may not be preserved.';
|
| + 'execuion flows exit. Semantics may not be preserved.';
|
|
|
| final SearchEngine searchEngine;
|
| final CompilationUnit unit;
|
| @@ -83,10 +79,10 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| Set<String> _usedNames = new Set<String>();
|
| Set<String> _excludedNames = new Set<String>();
|
| List<RefactoringMethodParameter> _parameters = <RefactoringMethodParameter>[];
|
| - Map<String, RefactoringMethodParameter> _parametersMap = <String,
|
| - RefactoringMethodParameter>{};
|
| - Map<String, List<SourceRange>> _parameterReferencesMap = <String,
|
| - List<SourceRange>>{};
|
| + Map<String, RefactoringMethodParameter> _parametersMap =
|
| + <String, RefactoringMethodParameter>{};
|
| + Map<String, List<SourceRange>> _parameterReferencesMap =
|
| + <String, List<SourceRange>>{};
|
| DartType _returnType;
|
| String _returnVariableName;
|
| AstNode _parentMember;
|
| @@ -181,7 +177,6 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| return result;
|
| }
|
|
|
| -
|
| @override
|
| Future<RefactoringStatus> checkInitialConditions() {
|
| RefactoringStatus result = new RefactoringStatus();
|
| @@ -336,8 +331,8 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| // insert declaration
|
| if (declarationSource != null) {
|
| int offset = _parentMember.end;
|
| - SourceEdit edit =
|
| - new SourceEdit(offset, 0, '${eol}${eol}${prefix}${declarationSource}');
|
| + SourceEdit edit = new SourceEdit(
|
| + offset, 0, '${eol}${eol}${prefix}${declarationSource}');
|
| doSourceChange_addElementEdit(change, unitElement, edit);
|
| }
|
| }
|
| @@ -373,8 +368,9 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| }
|
| }
|
| if (_usedNames.contains(parameter.name)) {
|
| - result.addError(
|
| - format("'{0}' is already used as a name in the selected code", parameter.name));
|
| + result.addError(format(
|
| + "'{0}' is already used as a name in the selected code",
|
| + parameter.name));
|
| return result;
|
| }
|
| }
|
| @@ -424,8 +420,7 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| // single expression selected
|
| if (selectedNodes.length == 1 &&
|
| !utils.selectionIncludesNonWhitespaceOutsideNode(
|
| - selectionRange,
|
| - selectionAnalyzer.firstSelectedNode)) {
|
| + selectionRange, selectionAnalyzer.firstSelectedNode)) {
|
| AstNode selectedNode = selectionAnalyzer.firstSelectedNode;
|
| if (selectedNode is Expression) {
|
| _selectionExpression = selectedNode;
|
| @@ -470,11 +465,8 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| List<SourceRange> ranges = _parameterReferencesMap[parameter.id];
|
| if (ranges != null) {
|
| for (SourceRange range in ranges) {
|
| - replaceEdits.add(
|
| - new SourceEdit(
|
| - range.offset - selectionRange.offset,
|
| - range.length,
|
| - parameter.name));
|
| + replaceEdits.add(new SourceEdit(range.offset - selectionRange.offset,
|
| + range.length, parameter.name));
|
| }
|
| }
|
| }
|
| @@ -558,11 +550,8 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| // prepare an enclosing parent - class or unit
|
| AstNode enclosingMemberParent = _parentMember.parent;
|
| // visit nodes which will able to access extracted method
|
| - enclosingMemberParent.accept(
|
| - new _InitializeOccurrencesVisitor(
|
| - this,
|
| - selectionPattern,
|
| - patternToSelectionName));
|
| + enclosingMemberParent.accept(new _InitializeOccurrencesVisitor(
|
| + this, selectionPattern, patternToSelectionName));
|
| }
|
|
|
| /**
|
| @@ -601,7 +590,7 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| if (_returnType != null) {
|
| result.addFatalError(
|
| 'Ambiguous return value: Selected block contains assignment(s) to '
|
| - 'local variables and return statement.');
|
| + 'local variables and return statement.');
|
| return result;
|
| }
|
| // prepare to return an assigned variable
|
| @@ -616,11 +605,10 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| sb.write(variable.displayName);
|
| sb.write('\n');
|
| }
|
| - result.addFatalError(
|
| - format(
|
| - 'Ambiguous return value: Selected block contains more than one '
|
| - 'assignment to local variables. Affected variables are:\n\n{0}',
|
| - sb.toString().trim()));
|
| + result.addFatalError(format(
|
| + 'Ambiguous return value: Selected block contains more than one '
|
| + 'assignment to local variables. Affected variables are:\n\n{0}',
|
| + sb.toString().trim()));
|
| }
|
| // done
|
| return result;
|
| @@ -686,11 +674,9 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| void _prepareNames() {
|
| names.clear();
|
| if (_selectionExpression != null) {
|
| - names.addAll(
|
| - getVariableNameSuggestionsForExpression(
|
| - _selectionExpression.staticType,
|
| - _selectionExpression,
|
| - _excludedNames));
|
| + names.addAll(getVariableNameSuggestionsForExpression(
|
| + _selectionExpression.staticType, _selectionExpression,
|
| + _excludedNames));
|
| }
|
| }
|
|
|
| @@ -722,7 +708,6 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl implements
|
| }
|
| }
|
|
|
| -
|
| /**
|
| * [SelectionAnalyzer] for [ExtractMethodRefactoringImpl].
|
| */
|
| @@ -741,7 +726,7 @@ class _ExtractMethodAnalyzer extends StatementAnalyzer {
|
| super.handleSelectionEndsIn(node);
|
| invalidSelection(
|
| 'The selection does not cover a set of statements or an expression. '
|
| - 'Extend selection to a valid range.');
|
| + 'Extend selection to a valid range.');
|
| }
|
|
|
| @override
|
| @@ -749,8 +734,7 @@ class _ExtractMethodAnalyzer extends StatementAnalyzer {
|
| super.visitAssignmentExpression(node);
|
| Expression lhs = node.leftHandSide;
|
| if (_isFirstSelectedNode(lhs)) {
|
| - invalidSelection(
|
| - 'Cannot extract the left-hand side of an assignment.',
|
| + invalidSelection('Cannot extract the left-hand side of an assignment.',
|
| newLocation_fromNode(lhs));
|
| }
|
| return null;
|
| @@ -760,10 +744,8 @@ class _ExtractMethodAnalyzer extends StatementAnalyzer {
|
| Object visitConstructorInitializer(ConstructorInitializer node) {
|
| super.visitConstructorInitializer(node);
|
| if (_isFirstSelectedNode(node)) {
|
| - invalidSelection(
|
| - 'Cannot extract a constructor initializer. '
|
| - 'Select expression part of initializer.',
|
| - newLocation_fromNode(node));
|
| + invalidSelection('Cannot extract a constructor initializer. '
|
| + 'Select expression part of initializer.', newLocation_fromNode(node));
|
| }
|
| return null;
|
| }
|
| @@ -815,10 +797,8 @@ class _ExtractMethodAnalyzer extends StatementAnalyzer {
|
| Object visitVariableDeclaration(VariableDeclaration node) {
|
| super.visitVariableDeclaration(node);
|
| if (_isFirstSelectedNode(node)) {
|
| - invalidSelection(
|
| - 'Cannot extract a variable declaration fragment. '
|
| - 'Select whole declaration statement.',
|
| - newLocation_fromNode(node));
|
| + invalidSelection('Cannot extract a variable declaration fragment. '
|
| + 'Select whole declaration statement.', newLocation_fromNode(node));
|
| }
|
| return null;
|
| }
|
| @@ -838,7 +818,6 @@ class _ExtractMethodAnalyzer extends StatementAnalyzer {
|
| bool _isFirstSelectedNode(AstNode node) => identical(firstSelectedNode, node);
|
| }
|
|
|
| -
|
| class _GetSourcePatternVisitor extends GeneralizingAstVisitor {
|
| final SourceRange partRange;
|
| final _SourcePattern pattern;
|
| @@ -865,18 +844,13 @@ class _GetSourcePatternVisitor extends GeneralizingAstVisitor {
|
| patternName = '__refVar${pattern.originalToPatternNames.length}';
|
| pattern.originalToPatternNames[originalName] = patternName;
|
| }
|
| - replaceEdits.add(
|
| - new SourceEdit(
|
| - nodeRange.offset - partRange.offset,
|
| - nodeRange.length,
|
| - patternName));
|
| + replaceEdits.add(new SourceEdit(nodeRange.offset - partRange.offset,
|
| + nodeRange.length, patternName));
|
| }
|
| }
|
| }
|
| }
|
|
|
| -
|
| -
|
| class _HasMethodInvocationVisitor extends RecursiveAstVisitor {
|
| bool result = false;
|
|
|
| @@ -886,13 +860,11 @@ class _HasMethodInvocationVisitor extends RecursiveAstVisitor {
|
| }
|
| }
|
|
|
| -
|
| class _HasReturnStatementVisitor extends RecursiveAstVisitor {
|
| bool hasReturn = false;
|
|
|
| @override
|
| - visitBlockFunctionBody(BlockFunctionBody node) {
|
| - }
|
| + visitBlockFunctionBody(BlockFunctionBody node) {}
|
|
|
| @override
|
| visitReturnStatement(ReturnStatement node) {
|
| @@ -900,7 +872,6 @@ class _HasReturnStatementVisitor extends RecursiveAstVisitor {
|
| }
|
| }
|
|
|
| -
|
| class _InitializeOccurrencesVisitor extends GeneralizingAstVisitor<Object> {
|
| final ExtractMethodRefactoringImpl ref;
|
| final _SourcePattern selectionPattern;
|
| @@ -908,8 +879,8 @@ class _InitializeOccurrencesVisitor extends GeneralizingAstVisitor<Object> {
|
|
|
| bool forceStatic = false;
|
|
|
| - _InitializeOccurrencesVisitor(this.ref, this.selectionPattern,
|
| - this.patternToSelectionName);
|
| + _InitializeOccurrencesVisitor(
|
| + this.ref, this.selectionPattern, this.patternToSelectionName);
|
|
|
| @override
|
| Object visitBlock(Block node) {
|
| @@ -994,8 +965,7 @@ class _InitializeOccurrencesVisitor extends GeneralizingAstVisitor<Object> {
|
| int beginStatementIndex = 0;
|
| int selectionCount = ref._selectionStatements.length;
|
| while (beginStatementIndex + selectionCount <= statements.length) {
|
| - SourceRange nodeRange = rangeStartEnd(
|
| - statements[beginStatementIndex],
|
| + SourceRange nodeRange = rangeStartEnd(statements[beginStatementIndex],
|
| statements[beginStatementIndex + selectionCount - 1]);
|
| bool found = _tryToFindOccurrence(nodeRange);
|
| // next statement
|
| @@ -1036,10 +1006,8 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<Object> {
|
| DartType parameterType = node.bestType;
|
| String parameterTypeCode = ref._getTypeCode(parameterType);
|
| parameter = new RefactoringMethodParameter(
|
| - RefactoringMethodParameterKind.REQUIRED,
|
| - parameterTypeCode,
|
| - variableName,
|
| - id: variableName);
|
| + RefactoringMethodParameterKind.REQUIRED, parameterTypeCode,
|
| + variableName, id: variableName);
|
| ref._parameters.add(parameter);
|
| ref._parametersMap[variableName] = parameter;
|
| }
|
| @@ -1063,7 +1031,6 @@ class _InitializeParametersVisitor extends GeneralizingAstVisitor<Object> {
|
| }
|
| }
|
|
|
| -
|
| class _IsUsedAfterSelectionVisitor extends GeneralizingAstVisitor {
|
| final ExtractMethodRefactoringImpl ref;
|
| final VariableElement element;
|
| @@ -1083,7 +1050,6 @@ class _IsUsedAfterSelectionVisitor extends GeneralizingAstVisitor {
|
| }
|
| }
|
|
|
| -
|
| /**
|
| * Description of a single occurrence of the selected expression or set of
|
| * statements.
|
| @@ -1097,7 +1063,6 @@ class _Occurrence {
|
| _Occurrence(this.range, this.isSelection);
|
| }
|
|
|
| -
|
| class _ResetCanCreateGetterVisitor extends RecursiveAstVisitor {
|
| final ExtractMethodRefactoringImpl ref;
|
|
|
| @@ -1128,13 +1093,11 @@ class _ResetCanCreateGetterVisitor extends RecursiveAstVisitor {
|
| }
|
| }
|
|
|
| -
|
| class _ReturnTypeComputer extends RecursiveAstVisitor {
|
| DartType returnType;
|
|
|
| @override
|
| - visitBlockFunctionBody(BlockFunctionBody node) {
|
| - }
|
| + visitBlockFunctionBody(BlockFunctionBody node) {}
|
|
|
| @override
|
| visitReturnStatement(ReturnStatement node) {
|
| @@ -1161,7 +1124,6 @@ class _ReturnTypeComputer extends RecursiveAstVisitor {
|
| }
|
| }
|
|
|
| -
|
| /**
|
| * Generalized version of some source, in which references to the specific
|
| * variables are replaced with pattern variables, with back mapping from the
|
|
|