Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 import 'package:analyzer/src/generated/ast.dart'; | |
| 2 import 'package:analyzer/src/generated/scanner.dart'; | |
| 3 | |
| 4 /** | |
| 5 * A CompletionTarget represents an edge in the parse tree which connects an | |
| 6 * AST node (representing the [context] of the completion) to one of its | |
| 7 * children (the [entity], which represents the place in the parse tree where | |
| 8 * the newly completed text will be inserted). | |
| 9 * | |
| 10 * To illustrate, consider the following snippet of code, and its associated | |
| 11 * parse tree. (T's represent tokens, N's represent AST nodes. Some trivial | |
| 12 * AST nodes are not shown). | |
| 13 * | |
| 14 * ___N (function declaration) | |
| 15 * / \ | |
| 16 * / __N_______ (function body) | |
| 17 * / / |a \ | |
| 18 * / / N______ \ (statement) | |
| 19 * / / / \ \ | |
| 20 * / / N____ \ \ (assignment expression) | |
| 21 * | / /| \ \ \ | |
| 22 * . | / | _N___ \ | ("as" expression) | |
| 23 * . | | | / | \c | | | |
| 24 * . | N | N |b N | | (simple identifiers) | |
| 25 * | | | | | | | | | |
| 26 * T T T T T T T T | |
| 27 * m() { foo = bar as Baz; } | |
| 28 * | |
| 29 * The Completion target is usually placed as high in the tree as possible so | |
| 30 * that we can produce the most meaningful completions with minimal effort. | |
| 31 * For instance, if the cursor is inside the identifier "foo", the completion | |
| 32 * target will be the edge marked "a", so that we will produce all completions | |
| 33 * that could possibly start a statement, even those which would conflict with | |
| 34 * the current parse (such as the keyword "for", which begins a "for" | |
| 35 * statement). As a consequence of this, the [entity] will usually not be the | |
| 36 * first child of the [context] node. | |
| 37 * | |
| 38 * Note that the [context] is always an AST node, but the [entity] may not be. | |
| 39 * For instance, if the cursor is inside the keyword "as", the completion | |
| 40 * target will be the edge marked "b", so the [context] is the token "as". | |
|
danrubel
2014/12/20 05:22:57
Should the end of this sentence be "... so the [en
Paul Berry
2014/12/20 15:43:24
Yes, thanks. Fixed.
| |
| 41 * | |
| 42 * If the cursor is between tokens, the completion target is usually associated | |
| 43 * with the token that follows the cursor (since that's the token that will be | |
| 44 * displaced when the new text is inserted). For example, if the cursor is | |
| 45 * after the "{" character, then the completion target will be the edge marked | |
| 46 * "a", just as it is if the cursor is inside the identifier "foo". However | |
| 47 * there is one exception: if the cursor is at the rightmost edge of a keyword | |
| 48 * or identifier, then the completion target is associated with that token, | |
| 49 * since any further letters typed will change the meaning of the identifier or | |
| 50 * keyword, rather than creating a new token. So for instance, if the cursor | |
| 51 * is just after the "s" of "as", the completion target will be the edge marked | |
| 52 * "b", but if the cursor target is after the first space following "as", then | |
| 53 * the completion target will be the edge marked "c". | |
| 54 * | |
| 55 * If the file is empty, or the cursor is after all the text in the file, then | |
| 56 * there may be no edge in the parse tree which is appropriate to act as the | |
| 57 * completion target; in this case, [entity] is set to null and [context] is | |
| 58 * set to the CompilationUnit. | |
| 59 */ | |
| 60 class CompletionTarget { | |
| 61 /** | |
| 62 * The context in which the completion is occurring. This is the AST node | |
| 63 * which is a direct parent of [entity]. | |
| 64 */ | |
| 65 final AstNode context; | |
|
danrubel
2014/12/20 05:22:57
Since "context" already has a strong connotation a
Paul Berry
2014/12/20 15:43:24
Good idea. Renamed to "containingNode".
| |
| 66 | |
| 67 /** | |
| 68 * The entity which the completed text will replace (or which will be | |
| 69 * displaced once the completed text is inserted). This may be an AstNode or | |
| 70 * a Token, or it may be null if the cursor is after all tokens in the file. | |
| 71 * | |
| 72 * Usually, the entity won't be the first child of the [context] (this is a | |
| 73 * consequence of placing the completion target as high in the tree as | |
| 74 * possible). However, there is one exception: when the cursor is inside of | |
| 75 * a multi-character token which is not a keyword or identifier (e.g. a | |
| 76 * comment, or a token like "+=", the entity will be always be the token. | |
| 77 */ | |
| 78 final Object entity; | |
| 79 | |
| 80 /** | |
| 81 * Compute the appropriate [CompletionTarget] for the given [offset] within | |
| 82 * the [compilationUnit]. | |
| 83 */ | |
| 84 factory CompletionTarget.forOffset(CompilationUnit compilationUnit, | |
| 85 int offset) { | |
| 86 // The precise algorithm is as follows. We perform a depth-first search of | |
| 87 // all edges in the parse tree (both those that point to AST nodes and | |
| 88 // those that point to tokens), visiting parents before children. The | |
| 89 // first edge which points to an entity satisfying either _isCandidateToken | |
| 90 // or _isCandidateNode is the completion target. If no edge is found that | |
| 91 // satisfies these two predicates, then we set the completion target entity | |
| 92 // to null and the context to the compilationUnit. | |
| 93 // | |
| 94 // Note that if a token is not a candidate target, then none of the tokens | |
| 95 // that precede it are candidate targets either. Therefore any entity | |
| 96 // whose last token is not a candidate target can be skipped. This lets us | |
| 97 // prune the search to the point where no recursion is necessary; at each | |
| 98 // step in the process we know exactly which child node we need to proceed | |
| 99 // to. | |
| 100 AstNode containingNode = compilationUnit; | |
| 101 outerLoop: while (true) { | |
| 102 if (containingNode is Comment) { | |
| 103 // Comments are handled specially: we descend into any CommentReference | |
| 104 // child node that contains the cursor offset. | |
| 105 Comment comment = containingNode; | |
| 106 for (CommentReference commentReference in comment.references) { | |
| 107 if (commentReference.offset <= offset && | |
| 108 offset <= commentReference.end) { | |
| 109 containingNode = commentReference; | |
| 110 continue outerLoop; | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 for (var entity in containingNode.childEntities) { | |
| 115 if (entity is Token) { | |
| 116 if (_isCandidateToken(entity, offset)) { | |
| 117 // Target found. | |
| 118 return new CompletionTarget._(containingNode, entity); | |
| 119 } else { | |
| 120 // Since entity is a token, we don't need to look inside it; just | |
| 121 // proceed to the next entity. | |
| 122 continue; | |
| 123 } | |
| 124 } else if (entity is AstNode) { | |
| 125 // If the last token in the node isn't a candidate target, then | |
| 126 // neither the node nor any of its descendants can possibly be the | |
| 127 // completion target, so we can skip the node entirely. | |
| 128 if (!_isCandidateToken(entity.endToken, offset)) { | |
| 129 continue; | |
| 130 } | |
| 131 | |
| 132 // If the node is a candidate target, then we are done. | |
| 133 if (_isCandidateNode(entity, offset)) { | |
| 134 return new CompletionTarget._(containingNode, entity); | |
| 135 } | |
| 136 | |
| 137 // Otherwise, the completion target is somewhere inside the entity, | |
| 138 // so we need to jump to the start of the outer loop to examine its | |
| 139 // contents. | |
| 140 containingNode = entity; | |
| 141 continue outerLoop; | |
| 142 } else { | |
| 143 // Unexpected entity found (all entities in a parse tree should be | |
| 144 // AST nodes or tokens). | |
| 145 assert(false); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 // No completion target found. It should only be possible to reach here | |
| 150 // the first time through the outer loop (since we only jump to the start | |
| 151 // of the outer loop after determining that the completion target is | |
| 152 // inside an entity). We can check that assumption by verifying that | |
| 153 // containingNode is still the compilationUnit. | |
| 154 assert(identical(containingNode, compilationUnit)); | |
| 155 | |
| 156 // Since no completion target was found, we set the completion target | |
| 157 // entity to null and use the compilationUnit as the parent. | |
| 158 return new CompletionTarget._(compilationUnit, null); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 /** | |
| 163 * Create a [CompletionTarget] holding the given [context] and [entity]. | |
| 164 */ | |
| 165 CompletionTarget._(this.context, this.entity); | |
| 166 | |
| 167 /** | |
| 168 * Determine whether [node] could possibly be the [entity] for a | |
| 169 * [CompletionTarget] associated with the given [offset]. | |
| 170 */ | |
| 171 static bool _isCandidateNode(AstNode node, int offset) { | |
| 172 // If the node's first token is a keyword or identifier, then the node is a | |
| 173 // candidate entity if its first token is. | |
| 174 Token beginToken = node.beginToken; | |
| 175 if (beginToken.type == TokenType.KEYWORD || | |
| 176 beginToken.type == TokenType.IDENTIFIER) { | |
| 177 return _isCandidateToken(beginToken, offset); | |
| 178 } | |
| 179 | |
| 180 // Otherwise, the node is a candidate entity only if the offset is before | |
| 181 // the beginning of the node. This ensures that completions within a token | |
| 182 // (e.g. inside a literal string or inside a comment) are evaluated within | |
| 183 // the context of the token itself. | |
| 184 return offset <= node.offset; | |
| 185 } | |
| 186 | |
| 187 /** | |
| 188 * Determine whether [token] could possibly be the [entity] for a | |
| 189 * [CompletionTarget] associated with the given [offset]. | |
| 190 */ | |
| 191 static bool _isCandidateToken(Token token, int offset) { | |
| 192 // A token is considered a candidate entity if the cursor offset is (a) | |
| 193 // before the start of the token, (b) within the token, (c) at the end of | |
| 194 // the token and the token is a keyword or identifier, or (d) at the | |
| 195 // location of the token and the token is zero length. | |
| 196 if (offset < token.end) { | |
| 197 return true; | |
| 198 } else if (offset == token.end) { | |
| 199 return token.type == TokenType.KEYWORD || | |
| 200 token.type == TokenType.IDENTIFIER || | |
| 201 token.length == 0; | |
| 202 } else { | |
| 203 return false; | |
| 204 } | |
| 205 } | |
| 206 } | |
| OLD | NEW |