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

Side by Side Diff: pkg/analyzer_plugin/lib/src/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 int offset;
16 int length;
17
18 ReplacementRange(this.offset, this.length);
19
20 factory ReplacementRange.compute(int requestOffset, CompletionTarget target) {
21 bool isKeywordOrIdentifier(Token token) =>
22 token.type.isKeyword || token.type == TokenType.IDENTIFIER;
23
24 //TODO(danrubel) Ideally this needs to be pushed down into the contributors
25 // but that implies that each suggestion can have a different
26 // replacement offsent/length which would mean an API change
27
28 var entity = target.entity;
29 Token token = entity is AstNode ? entity.beginToken : entity;
30 if (token != null && requestOffset < token.offset) {
31 token = token.previous;
32 }
33 if (token != null) {
34 if (requestOffset == token.offset && !isKeywordOrIdentifier(token)) {
35 // If the insertion point is at the beginning of the current token
36 // and the current token is not an identifier
37 // then check the previous token to see if it should be replaced
38 token = token.previous;
39 }
40 if (token != null && isKeywordOrIdentifier(token)) {
41 if (token.offset <= requestOffset && requestOffset <= token.end) {
42 // Replacement range for typical identifier completion
43 return new ReplacementRange(token.offset, token.length);
44 }
45 }
46 if (token is StringToken) {
47 SimpleStringLiteral uri =
48 astFactory.simpleStringLiteral(token, token.lexeme);
49 Keyword keyword = token.previous?.keyword;
50 if (keyword == Keyword.IMPORT ||
51 keyword == Keyword.EXPORT ||
52 keyword == Keyword.PART) {
53 int start = uri.contentsOffset;
54 var end = uri.contentsEnd;
55 if (start <= requestOffset && requestOffset <= end) {
56 // Replacement range for import URI
57 return new ReplacementRange(start, end - start);
58 }
59 }
60 }
61 }
62 return new ReplacementRange(requestOffset, 0);
63 }
64 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698