Files
sdk/pkg/analyzer_plugin/test/utilities/completion/completion_target_test.dart
T
Mike Fairhurst f22cdd8def Revert "Change completionTarget.forOffset to accept & wrap dangling AstNodes."
This reverts commit fbfe5691cb.

Reason for revert: Subtle backwards incompatibility, with no obvious win-win fix.

Original change's description:
> Change completionTarget.forOffset to accept & wrap dangling AstNodes.
>
> Deprecate the entryPoint parameter, but still accept it in place of what
> used to be (positionally) compilationUnit.
>
> Detect if we have a compilation unit or not; and in the case we don't,
> add a new protection by asserting that it has no parent (its the root of
> the dangling tree).
>
> Its more of an implementation detail that completion contributors don't
> work well on dangling nodes, so fix it up for clients rather than
> expecting them to do it themselves.
>
> Change-Id: I3e454734e5b515f4e536f7229d541ca8c6aed60c
> Reviewed-on: https://dart-review.googlesource.com/52645
> Reviewed-by: Paul Berry <paulberry@google.com>
> Commit-Queue: Mike Fairhurst <mfairhurst@google.com>

Change-Id: I64a6689f7862fb2d256b348491c79c58ee0fe1a9
Reviewed-on: https://dart-review.googlesource.com/52962
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
2018-04-27 21:23:06 +00:00

58 lines
2.5 KiB
Dart

// Copyright (c) 2018, 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'
show SyntheticBeginToken, SyntheticToken;
import 'package:analyzer/src/dart/scanner/reader.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(CompletionTargetTest);
});
}
@reflectiveTest
class CompletionTargetTest {
test_danglingExpressionCompletionIsValid() {
// Test that users can parse dangling expressions of dart and autocomplete
// them without crash/with the correct offset information.
final snippet = wrapForCompliance(parseDanglingDart('identifier'));
final completionTarget =
// ignore: deprecated_member_use
new CompletionTarget.forOffset(null, 1, entryPoint: snippet);
expect(completionTarget.offset, 1);
final replacementRange = completionTarget.computeReplacementRange(1);
expect(replacementRange.offset, 0);
expect(replacementRange.length, 'identifier'.length);
}
/// Parse a dangling expression (no parent node). The angular plugin, for
/// instance, does this.
Expression parseDanglingDart(String code) {
final reader = new CharSequenceReader(code);
final scanner = new Scanner(null, reader, null);
return new Parser(null, null).parseExpression(scanner.tokenize());
}
Expression wrapForCompliance(Expression expression) {
// TODO(mfairhurst) This should be performed for clients or the need should
// be dropped. It's a fairly valid invariant that all autocompletion target
// expressions should have parents, and one we can enforce via synthetics.
// But clients should not be doing this ideally.
return astFactory.parenthesizedExpression(
new SyntheticBeginToken(TokenType.OPEN_PAREN, expression.offset)
..next = expression.beginToken,
expression,
new SyntheticToken(TokenType.CLOSE_PAREN, expression.end));
}
}