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

Unified Diff: pkg/analysis_services/lib/src/completion/dart_completion_manager.dart

Issue 467233003: refactor and cleanup code completion (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merge Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_services/lib/src/completion/dart_completion_manager.dart
diff --git a/pkg/analysis_services/lib/src/completion/dart_completion_manager.dart b/pkg/analysis_services/lib/src/completion/dart_completion_manager.dart
index 516379083f937c006e8076e3adfdd880a6a776e1..e25a5f03ad2a9a4250385279c4f2e86b6ffb343d 100644
--- a/pkg/analysis_services/lib/src/completion/dart_completion_manager.dart
+++ b/pkg/analysis_services/lib/src/completion/dart_completion_manager.dart
@@ -6,12 +6,12 @@ library test.services.completion.dart;
import 'dart:async';
-import 'package:analysis_services/completion/completion_computer.dart';
+import 'package:analysis_services/completion/completion_manager.dart';
import 'package:analysis_services/completion/completion_suggestion.dart';
import 'package:analysis_services/search/search_engine.dart';
import 'package:analysis_services/src/completion/imported_type_computer.dart';
-import 'package:analysis_services/src/completion/keyword_computer.dart';
import 'package:analysis_services/src/completion/invocation_computer.dart';
+import 'package:analysis_services/src/completion/keyword_computer.dart';
import 'package:analysis_services/src/completion/local_computer.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';
@@ -19,18 +19,42 @@ import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
/**
+ * The base class for computing code completion suggestions.
+ */
+abstract class DartCompletionComputer {
+ /**
+ * Computes the initial set of [CompletionSuggestion]s based on
+ * the given completion context. The compilation unit and completion node
+ * in the given completion context may not be resolved.
+ * This method should execute quickly and not block waiting for any analysis.
+ * Returns `true` if the computer's work is complete
+ * or `false` if [computeFull] should be called to complete the work.
+ */
+ bool computeFast(DartCompletionRequest request);
+
+ /**
+ * Computes the complete set of [CompletionSuggestion]s based on
+ * the given completion context. The compilation unit and completion node
+ * in the given completion context are resolved.
+ * Returns `true` if the receiver modified the list of suggestions.
+ */
+ Future<bool> computeFull(DartCompletionRequest request);
+}
+
+/**
* Manages code completion for a given Dart file completion request.
*/
class DartCompletionManager extends CompletionManager {
final AnalysisContext context;
final Source source;
final int offset;
- final SearchEngine searchEngine;
- final List<CompletionSuggestion> suggestions = [];
- List<CompletionComputer> computers;
+ DartCompletionRequest request;
+ List<DartCompletionComputer> computers;
- DartCompletionManager(this.context, this.source, this.offset,
- this.searchEngine);
+ DartCompletionManager(this.context, SearchEngine searchEngine, this.source,
+ this.offset) {
+ request = new DartCompletionRequest(context, searchEngine, source, offset);
+ }
@override
void compute() {
@@ -47,8 +71,9 @@ class DartCompletionManager extends CompletionManager {
*/
void computeFast() {
CompilationUnit unit = context.parseCompilationUnit(source);
- AstNode node = new NodeLocator.con1(offset).searchWithin(unit);
- computers.removeWhere((c) => c.computeFast(unit, node, suggestions));
+ request.unit = unit;
+ request.node = new NodeLocator.con1(offset).searchWithin(unit);
+ computers.removeWhere((DartCompletionComputer c) => c.computeFast(request));
sendResults(computers.isEmpty);
}
@@ -62,10 +87,11 @@ class DartCompletionManager extends CompletionManager {
sendResults(true);
return;
}
- AstNode node = new NodeLocator.con1(offset).searchWithin(unit);
+ request.unit = unit;
+ request.node = new NodeLocator.con1(offset).searchWithin(unit);
int count = computers.length;
computers.forEach((c) {
- c.computeFull(unit, node, suggestions).then((bool changed) {
+ c.computeFull(request).then((bool changed) {
var last = --count == 0;
if (changed || last) {
sendResults(last);
@@ -86,32 +112,28 @@ class DartCompletionManager extends CompletionManager {
new ImportedTypeComputer(),
new InvocationComputer()];
}
- computers.forEach((CompletionComputer c) {
- c.context = context;
- c.source = source;
- c.offset = offset;
- c.searchEngine = searchEngine;
- });
}
/**
* Send the current list of suggestions to the client.
*/
void sendResults(bool last) {
- controller.add(new CompletionResult(offset, 0, suggestions, last));
+ controller.add(
+ new CompletionResult(request.offset, 0, request.suggestions, last));
if (last) {
controller.close();
}
}
/**
- * Wait for analysis to be complete and return the resolved unit
- * or `null` if the unit could not be resolved.
+ * Return a future that completes when analysis is complete.
+ * Return `true` if the compilation unit is be resolved.
*/
Future<CompilationUnit> waitForAnalysis() {
LibraryElement library = context.getLibraryElement(source);
if (library != null) {
- var unit = context.getResolvedCompilationUnit(source, library);
+ CompilationUnit unit =
+ context.getResolvedCompilationUnit(source, library);
if (unit != null) {
return new Future.value(unit);
}
@@ -120,3 +142,50 @@ class DartCompletionManager extends CompletionManager {
return new Future(waitForAnalysis);
}
}
+
+/**
+ * The context in which the completion is requested.
+ */
+class DartCompletionRequest {
+ /**
+ * The analysis context in which the completion is requested.
+ */
+ final AnalysisContext context;
+
+ /**
+ * The search engine for use when building suggestions.
+ */
+ final SearchEngine searchEngine;
+
+ /**
+ * The source in which the completion is requested.
+ */
+ final Source source;
+
+ /**
+ * The offset within the source at which the completion is requested.
+ */
+ final int offset;
+
+ /**
+ * The compilation unit in which the completion was requested. This unit
+ * may or may not be resolved when [DartCompletionComputer.computeFast]
+ * is called but is resolved when [DartCompletionComputer.computeFull].
+ */
+ CompilationUnit unit;
+
+ /**
+ * The node in which the completion occurred. This node
+ * may or may not be resolved when [DartCompletionComputer.computeFast]
+ * is called but is resolved when [DartCompletionComputer.computeFull].
+ */
+ AstNode node;
+
+ /**
+ * The list of suggestions to be sent to the client.
+ */
+ final List<CompletionSuggestion> suggestions = [];
+
+ DartCompletionRequest(this.context, this.searchEngine, this.source,
+ this.offset);
+}

Powered by Google App Engine
This is Rietveld 408576698