mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 00:39:49 +00:00
Remove ReplacementRange
R=danrubel@google.com, maxkim@google.com Review-Url: https://codereview.chromium.org/2946573003 .
This commit is contained in:
parent
fe368f956c
commit
eafc070479
|
@ -3,9 +3,12 @@
|
|||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
|
||||
import 'package:analyzer/dart/ast/token.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/src/dart/ast/token.dart';
|
||||
import 'package:analyzer/src/generated/source.dart';
|
||||
import 'package:analyzer/src/generated/utilities_dart.dart';
|
||||
|
||||
/**
|
||||
|
@ -251,6 +254,51 @@ class CompletionTarget {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a source range that represents the region of text that should be
|
||||
* replaced when a suggestion based on this target is selected, given that the
|
||||
* completion was requested at the given [requestOffset].
|
||||
*/
|
||||
SourceRange computeReplacementRange(int requestOffset) {
|
||||
bool isKeywordOrIdentifier(Token token) =>
|
||||
token.type.isKeyword || token.type == TokenType.IDENTIFIER;
|
||||
|
||||
Token token = entity is AstNode ? (entity as AstNode).beginToken : entity;
|
||||
if (token != null && requestOffset < token.offset) {
|
||||
token = token.previous;
|
||||
}
|
||||
if (token != null) {
|
||||
if (requestOffset == token.offset && !isKeywordOrIdentifier(token)) {
|
||||
// If the insertion point is at the beginning of the current token
|
||||
// and the current token is not an identifier
|
||||
// then check the previous token to see if it should be replaced
|
||||
token = token.previous;
|
||||
}
|
||||
if (token != null && isKeywordOrIdentifier(token)) {
|
||||
if (token.offset <= requestOffset && requestOffset <= token.end) {
|
||||
// Replacement range for typical identifier completion
|
||||
return new SourceRange(token.offset, token.length);
|
||||
}
|
||||
}
|
||||
if (token is StringToken) {
|
||||
SimpleStringLiteral uri =
|
||||
astFactory.simpleStringLiteral(token, token.lexeme);
|
||||
Keyword keyword = token.previous?.keyword;
|
||||
if (keyword == Keyword.IMPORT ||
|
||||
keyword == Keyword.EXPORT ||
|
||||
keyword == Keyword.PART) {
|
||||
int start = uri.contentsOffset;
|
||||
int end = uri.contentsEnd;
|
||||
if (start <= requestOffset && requestOffset <= end) {
|
||||
// Replacement range for import URI
|
||||
return new SourceRange(start, end - start);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new SourceRange(requestOffset, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return `true` if the target is a functional argument in an argument list.
|
||||
* The target [AstNode] hierarchy *must* be resolved for this to work.
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
|
||||
import 'package:analyzer/dart/ast/token.dart';
|
||||
import 'package:analyzer/src/dart/ast/token.dart';
|
||||
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
|
||||
|
||||
/**
|
||||
* Utility class for computing the code completion replacement range.
|
||||
*/
|
||||
class ReplacementRange {
|
||||
// Copied from analysis_server/lib/src/services/completion/dart/completion_manager.dart
|
||||
int offset;
|
||||
int length;
|
||||
|
||||
ReplacementRange(this.offset, this.length);
|
||||
|
||||
factory ReplacementRange.compute(int requestOffset, CompletionTarget target) {
|
||||
bool isKeywordOrIdentifier(Token token) =>
|
||||
token.type.isKeyword || token.type == TokenType.IDENTIFIER;
|
||||
|
||||
//TODO(danrubel) Ideally this needs to be pushed down into the contributors
|
||||
// but that implies that each suggestion can have a different replacement
|
||||
// offset/length which would mean an API change.
|
||||
|
||||
var entity = target.entity;
|
||||
Token token = entity is AstNode ? entity.beginToken : entity;
|
||||
// TODO(brianwilkerson) If this class is every needed outside of tests, move
|
||||
// the code below into RangeFactory and use a SourceRange rather than a
|
||||
// ReplacementRange.
|
||||
if (token != null && requestOffset < token.offset) {
|
||||
token = token.previous;
|
||||
}
|
||||
if (token != null) {
|
||||
if (requestOffset == token.offset && !isKeywordOrIdentifier(token)) {
|
||||
// If the insertion point is at the beginning of the current token
|
||||
// and the current token is not an identifier
|
||||
// then check the previous token to see if it should be replaced
|
||||
token = token.previous;
|
||||
}
|
||||
if (token != null && isKeywordOrIdentifier(token)) {
|
||||
if (token.offset <= requestOffset && requestOffset <= token.end) {
|
||||
// Replacement range for typical identifier completion
|
||||
return new ReplacementRange(token.offset, token.length);
|
||||
}
|
||||
}
|
||||
if (token is StringToken) {
|
||||
SimpleStringLiteral uri =
|
||||
astFactory.simpleStringLiteral(token, token.lexeme);
|
||||
Keyword keyword = token.previous?.keyword;
|
||||
if (keyword == Keyword.IMPORT ||
|
||||
keyword == Keyword.EXPORT ||
|
||||
keyword == Keyword.PART) {
|
||||
int start = uri.contentsOffset;
|
||||
var end = uri.contentsEnd;
|
||||
if (start <= requestOffset && requestOffset <= end) {
|
||||
// Replacement range for import URI
|
||||
return new ReplacementRange(start, end - start);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ReplacementRange(requestOffset, 0);
|
||||
}
|
||||
}
|
|
@ -13,7 +13,6 @@ import 'package:analyzer_plugin/src/utilities/completion/completion_core.dart';
|
|||
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
|
||||
import 'package:analyzer_plugin/utilities/completion/completion_core.dart';
|
||||
import 'package:analyzer_plugin/utilities/completion/relevance.dart';
|
||||
import 'package:analyzer_plugin/utilities/completion/replacement_range.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../../support/abstract_context.dart';
|
||||
|
@ -463,7 +462,7 @@ abstract class DartCompletionContributorTest extends AbstractContextTest {
|
|||
|
||||
CompletionTarget target =
|
||||
new CompletionTarget.forOffset(request.result.unit, request.offset);
|
||||
var range = new ReplacementRange.compute(request.offset, target);
|
||||
var range = target.computeReplacementRange(request.offset);
|
||||
replacementOffset = range.offset;
|
||||
replacementLength = range.length;
|
||||
|
||||
|
|
Loading…
Reference in a new issue