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

Unified Diff: pkg/analyzer_plugin/lib/utilities/range_factory.dart

Issue 2872813002: Add support for creating source ranges (Closed)
Patch Set: Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer_plugin/test/support/abstract_single_unit.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer_plugin/lib/utilities/range_factory.dart
diff --git a/pkg/analyzer_plugin/lib/utilities/range_factory.dart b/pkg/analyzer_plugin/lib/utilities/range_factory.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a875abf3f09f2d0c34b734ead7ddaf9024b68e70
--- /dev/null
+++ b/pkg/analyzer_plugin/lib/utilities/range_factory.dart
@@ -0,0 +1,118 @@
+// 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/syntactic_entity.dart';
+import 'package:analyzer/dart/ast/token.dart';
+import 'package:analyzer/dart/element/element.dart';
+import 'package:analyzer/error/error.dart';
+import 'package:analyzer/src/generated/source.dart';
+
+/**
+ * An instance of [RangeFactory] made available for convenience.
+ */
+final RangeFactory range = new RangeFactory();
+
+/**
+ * A factory used to create instances of [SourceRange] based on various
+ * syntactic and semantic entities.
+ */
+class RangeFactory {
+ /**
+ * Return a source range that covers the name of the given [element].
+ */
+ SourceRange elementName(Element element) {
+ return new SourceRange(element.nameOffset, element.nameLength);
+ }
+
+ /**
+ * Return a source range that starts at the end of [leftEntity] and ends at
+ * the end of [rightEntity].
+ */
+ SourceRange endEnd(SyntacticEntity leftEntity, SyntacticEntity rightEntity) {
+ int offset = leftEntity.end;
+ int length = rightEntity.end - offset;
+ return new SourceRange(offset, length);
+ }
+
+ /**
+ * Return a source range that starts at the end of [entity] and has the given
+ * [length].
+ */
+ SourceRange endLength(SyntacticEntity entity, int length) {
+ return new SourceRange(entity.end, length);
+ }
+
+ /**
+ * Return a source range that starts at the end of [leftEntity] and ends at
+ * the start of [rightEntity].
+ */
+ SourceRange endStart(
+ SyntacticEntity leftEntity, SyntacticEntity rightEntity) {
+ int offset = leftEntity.end;
+ int length = rightEntity.offset - offset;
+ return new SourceRange(offset, length);
+ }
+
+ /**
+ * Return a source range that covers the same range as the given [error].
+ */
+ SourceRange error(AnalysisError error) {
+ return new SourceRange(error.offset, error.length);
+ }
+
+ /**
+ * Return a source range that covers the same range as the given [node].
+ */
+ SourceRange node(AstNode node) {
+ return new SourceRange(node.offset, node.length);
+ }
+
+ /**
+ * Return a source range that covers all of the given [nodes] (that is, from
+ * the start of the first node to the end of the last node.
+ */
+ SourceRange nodes(List<AstNode> nodes) {
+ if (nodes.isEmpty) {
+ return new SourceRange(0, 0);
+ }
+ return startEnd(nodes.first, nodes.last);
+ }
+
+ /**
+ * Return a source range that starts at the start of [leftEntity] and ends at
+ * the end of [rightEntity].
+ */
+ SourceRange startEnd(
+ SyntacticEntity leftEntity, SyntacticEntity rightEntity) {
+ int offset = leftEntity.offset;
+ int length = rightEntity.end - offset;
+ return new SourceRange(offset, length);
+ }
+
+ /**
+ * Return a source range that starts at the start of [entity] and has a length of [length].
+ */
+ SourceRange startLength(SyntacticEntity entity, int length) {
+ return new SourceRange(entity.offset, length);
+ }
+
+ /**
+ * Return a source range that starts at the start of [leftEntity] and ends at
+ * the start of [rightEntity].
+ */
+ SourceRange startStart(
+ SyntacticEntity leftEntity, SyntacticEntity rightEntity) {
+ int offset = leftEntity.offset;
+ int length = rightEntity.offset - offset;
+ return new SourceRange(offset, length);
+ }
+
+ /**
+ * Return a source range that covers the same range as the given [token].
+ */
+ SourceRange token(Token token) {
+ return new SourceRange(token.offset, token.length);
+ }
+}
« no previous file with comments | « no previous file | pkg/analyzer_plugin/test/support/abstract_single_unit.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698