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

Side by Side Diff: pkg/analyzer_plugin/test/utilities/completion/replacement_range.dart

Issue 2937093002: Refactor: Removed ReplacementRange from test and ReplacementRange from src/. Copied contents of tes… (Closed)
Patch Set: Created 3 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'package:analyzer/dart/ast/ast.dart';
6 import 'package:analyzer/dart/ast/standard_ast_factory.dart';
7 import 'package:analyzer/dart/ast/token.dart';
8 import 'package:analyzer/src/dart/ast/token.dart';
9 import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart' ;
10
11 /**
12 * Utility class for computing the code completion replacement range.
13 */
14 class ReplacementRange {
15 // Copied from analysis_server/lib/src/services/completion/dart/completion_man ager.dart
16 int offset;
17 int length;
18
19 ReplacementRange(this.offset, this.length);
20
21 factory ReplacementRange.compute(int requestOffset, CompletionTarget target) {
22 bool isKeywordOrIdentifier(Token token) =>
23 token.type.isKeyword || token.type == TokenType.IDENTIFIER;
24
25 //TODO(danrubel) Ideally this needs to be pushed down into the contributors
26 // but that implies that each suggestion can have a different replacement
27 // offset/length which would mean an API change.
28
29 var entity = target.entity;
30 Token token = entity is AstNode ? entity.beginToken : entity;
31 // TODO(brianwilkerson) If this class is every needed outside of tests, move
32 // the code below into RangeFactory and use a SourceRange rather than a
33 // ReplacementRange.
34 if (token != null && requestOffset < token.offset) {
35 token = token.previous;
36 }
37 if (token != null) {
38 if (requestOffset == token.offset && !isKeywordOrIdentifier(token)) {
39 // If the insertion point is at the beginning of the current token
40 // and the current token is not an identifier
41 // then check the previous token to see if it should be replaced
42 token = token.previous;
43 }
44 if (token != null && isKeywordOrIdentifier(token)) {
45 if (token.offset <= requestOffset && requestOffset <= token.end) {
46 // Replacement range for typical identifier completion
47 return new ReplacementRange(token.offset, token.length);
48 }
49 }
50 if (token is StringToken) {
51 SimpleStringLiteral uri =
52 astFactory.simpleStringLiteral(token, token.lexeme);
53 Keyword keyword = token.previous?.keyword;
54 if (keyword == Keyword.IMPORT ||
55 keyword == Keyword.EXPORT ||
56 keyword == Keyword.PART) {
57 int start = uri.contentsOffset;
58 var end = uri.contentsEnd;
59 if (start <= requestOffset && requestOffset <= end) {
60 // Replacement range for import URI
61 return new ReplacementRange(start, end - start);
62 }
63 }
64 }
65 }
66 return new ReplacementRange(requestOffset, 0);
67 }
68 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698