Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/swift/Syntax/SyntaxCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "swift/Syntax/Syntax.h"

#include <iterator>

namespace swift {
namespace syntax {

Expand Down Expand Up @@ -173,8 +175,11 @@ class SyntaxCollection : public Syntax {

/// Return a new collection with the element removed at index i.
SyntaxCollection<CollectionKind, Element> removing(size_t i) const {
auto NewLayout = getRaw()->Layout;
NewLayout.erase(NewLayout.begin() + i);
assert(i <= size());
std::vector<RC<RawSyntax>> NewLayout = getRaw()->getLayout();
auto iterator = NewLayout.begin();
std::advance(iterator, i);
NewLayout.erase(iterator);
auto Raw = RawSyntax::make(CollectionKind, NewLayout, getRaw()->getPresence());
return Data->replaceSelf<SyntaxCollection<CollectionKind, Element>>(Raw);
}
Expand Down
17 changes: 17 additions & 0 deletions unittests/Syntax/SyntaxCollectionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,20 @@ TEST(SyntaxCollectionTests, Iteration) {
List.print(OS);
ASSERT_EQ(OS.str().str(), IteratedOS.str().str());
}

TEST(SyntaxCollectionTests, Removing) {
auto Arg = getCannedArgument();
auto List = SyntaxFactory::makeBlankFunctionCallArgumentList()
.appending(Arg)
.appending(Arg.withLabel(SyntaxFactory::makeIdentifier("first", {}, {})))
.appending(Arg)
.removing(1);

ASSERT_EQ(List.size(), static_cast<size_t>(2));

SmallString<48> Scratch;
llvm::raw_svector_ostream OS(Scratch);
List.print(OS);
ASSERT_EQ(OS.str().str(), "x: foo, x: foo, ");
}