Skip to content

Commit

Permalink
Make 'Inline Method' refactoring work with the new analysis driver.
Browse files Browse the repository at this point in the history
R=brianwilkerson@google.com
BUG=

Review URL: https://codereview.chromium.org/2540793003 .
  • Loading branch information
scheglov committed Nov 30, 2016
1 parent 667fc8d commit 0f9a78f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 5 additions & 1 deletion pkg/analysis_server/lib/src/edit/edit_domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,11 @@ class _RefactoringManager {
CompilationUnit unit = await server.getResolvedCompilationUnit(file);
if (unit != null) {
_resetOnAnalysisStarted();
refactoring = new InlineMethodRefactoring(searchEngine, unit, offset);
refactoring =
new InlineMethodRefactoring(searchEngine, (Element element) async {
String elementPath = element.source.fullName;
return await server.getResolvedCompilationUnit(elementPath);
}, unit, offset);
}
}
if (kind == RefactoringKind.MOVE_FILE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,19 @@ Set<String> _getNamesConflictingAt(AstNode node) {
return result;
}

/**
* Completes with the resolved [CompilationUnit] that contains the [element].
*/
typedef Future<CompilationUnit> GetResolvedUnitContainingElement(
Element element);

/**
* [InlineMethodRefactoring] implementation.
*/
class InlineMethodRefactoringImpl extends RefactoringImpl
implements InlineMethodRefactoring {
final SearchEngine searchEngine;
final GetResolvedUnitContainingElement getResolvedUnit;
final CompilationUnit unit;
final int offset;
CorrectionUtils utils;
Expand All @@ -218,7 +225,8 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
List<_ReferenceProcessor> _referenceProcessors = [];
Set<FunctionBody> _alreadyMadeAsync = new Set<FunctionBody>();

InlineMethodRefactoringImpl(this.searchEngine, this.unit, this.offset) {
InlineMethodRefactoringImpl(
this.searchEngine, this.getResolvedUnit, this.unit, this.offset) {
utils = new CorrectionUtils(unit);
}

Expand Down Expand Up @@ -279,7 +287,7 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
Future<RefactoringStatus> checkInitialConditions() async {
RefactoringStatus result = new RefactoringStatus();
// prepare method information
result.addStatus(_prepareMethod());
result.addStatus(await _prepareMethod());
if (result.hasFatalError) {
return new Future<RefactoringStatus>.value(result);
}
Expand All @@ -301,6 +309,7 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
_referenceProcessors.clear();
for (SearchMatch reference in references) {
_ReferenceProcessor processor = new _ReferenceProcessor(this, reference);
await processor.init();
_referenceProcessors.add(processor);
}
return result;
Expand All @@ -327,7 +336,7 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
/**
* Initializes [_methodElement] and related fields.
*/
RefactoringStatus _prepareMethod() {
Future<RefactoringStatus> _prepareMethod() async {
_methodElement = null;
_methodParameters = null;
_methodBody = null;
Expand All @@ -352,7 +361,7 @@ class InlineMethodRefactoringImpl extends RefactoringImpl
}
_methodElement = element as ExecutableElement;
_isAccessor = element is PropertyAccessorElement;
_methodUnit = element.unit;
_methodUnit = await getResolvedUnit(element);
_methodUtils = new CorrectionUtils(_methodUnit);
// class member
bool isClassMember = element.enclosingElement is ClassElement;
Expand Down Expand Up @@ -435,17 +444,20 @@ class _ParameterOccurrence {
*/
class _ReferenceProcessor {
final InlineMethodRefactoringImpl ref;
final SearchMatch reference;

Element refElement;
CorrectionUtils _refUtils;
SimpleIdentifier _node;
SourceRange _refLineRange;
String _refPrefix;

_ReferenceProcessor(this.ref, SearchMatch reference) {
_ReferenceProcessor(this.ref, this.reference);

Future<Null> init() async {
refElement = reference.element;
// prepare CorrectionUtils
CompilationUnit refUnit = refElement.unit;
CompilationUnit refUnit = await ref.getResolvedUnit(refElement);
_refUtils = new CorrectionUtils(refUnit);
// prepare node and environment
_node = _refUtils.findNode(reference.sourceRange.offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ abstract class InlineMethodRefactoring implements Refactoring {
* Returns a new [InlineMethodRefactoring] instance.
*/
factory InlineMethodRefactoring(
SearchEngine searchEngine, CompilationUnit unit, int offset) {
return new InlineMethodRefactoringImpl(searchEngine, unit, offset);
SearchEngine searchEngine,
GetResolvedUnitContainingElement getResolvedUnit,
CompilationUnit unit,
int offset) {
return new InlineMethodRefactoringImpl(
searchEngine, getResolvedUnit, unit, offset);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,7 @@ main(bool p, bool p2, bool p3) {

void _createRefactoring(String search) {
int offset = findOffset(search);
refactoring = new InlineMethodRefactoring(searchEngine, testUnit, offset);
refactoring = new InlineMethodRefactoring(
searchEngine, (element) async => element.unit, testUnit, offset);
}
}

0 comments on commit 0f9a78f

Please sign in to comment.