5
5
import 'package:analysis_server/src/protocol/protocol_internal.dart' ;
6
6
import 'package:analysis_server/src/protocol_server.dart' as protocol;
7
7
import 'package:analysis_server/src/services/completion/dart/keyword_contributor.dart' ;
8
+ import 'package:analyzer/dart/analysis/results.dart' ;
8
9
import 'package:analyzer/dart/ast/ast.dart' ;
9
10
import 'package:analyzer/dart/ast/syntactic_entity.dart' ;
10
11
import 'package:analyzer/dart/ast/token.dart' ;
11
12
import 'package:analyzer/dart/ast/visitor.dart' ;
12
13
import 'package:analyzer/dart/element/element.dart' as element;
14
+ import 'package:analyzer/source/line_info.dart' ;
13
15
14
16
class ExpectedCompletion {
15
17
final String _filePath;
@@ -122,11 +124,12 @@ class ExpectedCompletion {
122
124
}
123
125
124
126
class ExpectedCompletionsVisitor extends RecursiveAstVisitor <void > {
125
- final List <ExpectedCompletion > expectedCompletions;
127
+ /// The result of resolving the file being visited.
128
+ final ResolvedUnitResult result;
126
129
127
- final String filePath;
128
-
129
- late CompilationUnit _enclosingCompilationUnit ;
130
+ /// The completions that are expected to be produced in the file being
131
+ /// visited.
132
+ final List < ExpectedCompletion > expectedCompletions = [] ;
130
133
131
134
/// This boolean is set to enable whether or not we should assert that some
132
135
/// found keyword in Dart syntax should be in the completion set returned from
@@ -141,22 +144,22 @@ class ExpectedCompletionsVisitor extends RecursiveAstVisitor<void> {
141
144
/// comment don't yield an error like Dart syntax mistakes would yield.
142
145
final bool _doExpectCommentRefs = false ;
143
146
144
- ExpectedCompletionsVisitor (this .filePath)
145
- : expectedCompletions = < ExpectedCompletion > [];
147
+ ExpectedCompletionsVisitor (this .result);
148
+
149
+ /// Return the path of the file that is being visited.
150
+ String get filePath => result.path;
151
+
152
+ /// Return the line info for the file that is being visited.
153
+ LineInfo get lineInfo => result.lineInfo;
146
154
147
155
void safelyRecordEntity (SyntacticEntity ? entity,
148
156
{protocol.CompletionSuggestionKind ? kind,
149
157
protocol.ElementKind ? elementKind}) {
150
158
// Only record if this entity is not null, has a length, etc.
151
- if (entity != null && entity.offset > 0 && entity.length > 0 ) {
152
- // Compute the line number at this offset
153
- var lineNumber = _enclosingCompilationUnit.lineInfo!
154
- .getLocation (entity.offset)
155
- .lineNumber;
156
-
157
- var columnNumber = _enclosingCompilationUnit.lineInfo!
158
- .getLocation (entity.offset)
159
- .columnNumber;
159
+ if (entity != null && entity.offset >= 0 && entity.length > 0 ) {
160
+ var location = lineInfo.getLocation (entity.offset);
161
+ var lineNumber = location.lineNumber;
162
+ var columnNumber = location.columnNumber;
160
163
161
164
bool isKeyword () => kind == protocol.CompletionSuggestionKind .KEYWORD ;
162
165
@@ -314,12 +317,6 @@ class ExpectedCompletionsVisitor extends RecursiveAstVisitor<void> {
314
317
super .visitClassTypeAlias (node);
315
318
}
316
319
317
- @override
318
- void visitCompilationUnit (CompilationUnit node) {
319
- _enclosingCompilationUnit = node;
320
- super .visitCompilationUnit (node);
321
- }
322
-
323
320
@override
324
321
void visitConfiguration (Configuration node) {
325
322
safelyRecordKeywordCompletion (node.ifKeyword);
0 commit comments