From 318e84238945289dbaa5a6a6a8f359fd4a9db127 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 13 Oct 2016 13:29:36 -0700 Subject: [PATCH] Extract appending nodes to the list into a helper method. R=brianwilkerson@google.com, paulberry@google.com BUG= Review URL: https://codereview.chromium.org/2417053002 . --- pkg/analyzer/lib/src/dart/sdk/patch.dart | 35 ++++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/sdk/patch.dart b/pkg/analyzer/lib/src/dart/sdk/patch.dart index e6475c3a028..02b497cf392 100644 --- a/pkg/analyzer/lib/src/dart/sdk/patch.dart +++ b/pkg/analyzer/lib/src/dart/sdk/patch.dart @@ -197,14 +197,9 @@ class SdkPatcher { patchMember.offset); } } - // Append new top-level declarations. - Token lastToken = baseClass.endToken.previous; - for (ClassMember newMember in membersToAppend) { - newMember.endToken.setNext(lastToken.next); - lastToken.setNext(newMember.beginToken); - baseClass.members.add(newMember); - lastToken = newMember.endToken; - } + // Append new class members. + _appendToNodeList( + baseClass.members, membersToAppend, baseClass.leftBracket); } void _patchDirectives( @@ -279,13 +274,8 @@ class SdkPatcher { } } // Append new top-level declarations. - Token lastToken = baseUnit.endToken.previous; - for (CompilationUnitMember newDeclaration in declarationsToAppend) { - newDeclaration.endToken.setNext(lastToken.next); - lastToken.setNext(newDeclaration.beginToken); - baseUnit.declarations.add(newDeclaration); - lastToken = newDeclaration.endToken; - } + _appendToNodeList(baseUnit.declarations, declarationsToAppend, + baseUnit.endToken.previous); } /** @@ -309,6 +299,21 @@ class SdkPatcher { return unit; } + /** + * Append [newNodes] to the given [nodes] and attach new tokens to the end + * token of the last [nodes] items, or, if it is empty, to [defaultPrevToken]. + */ + static void _appendToNodeList( + NodeList nodes, List newNodes, Token defaultPrevToken) { + Token prevToken = nodes.endToken ?? defaultPrevToken; + for (AstNode newNode in newNodes) { + newNode.endToken.setNext(prevToken.next); + prevToken.setNext(newNode.beginToken); + nodes.add(newNode); + prevToken = newNode.endToken; + } + } + /** * Return `true` if [metadata] has the `@patch` annotation. */