Skip to content

Commit cbd62cb

Browse files
authored
IDE: Expose a utility to collect argument label ranges from either tuple or paren exprs. (#8982)
1 parent 0543f24 commit cbd62cb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/swift/IDE/Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ class SourceEditOutputConsumer : public SourceEditConsumer {
397397
~SourceEditOutputConsumer();
398398
void accept(SourceManager &SM, RegionType RegionType, ArrayRef<Replacement> Replacements) override;
399399
};
400+
401+
// Get the ranges of argument labels from an Arg, either tuple or paren.
402+
std::vector<CharSourceRange> getCallArgLabelRanges(SourceManager &SM, Expr *Arg);
400403
} // namespace ide
401404
} // namespace swift
402405

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,27 @@ void swift::ide::getLocationInfo(const ValueDecl *VD,
912912
DeclarationLoc, Filename);
913913
}
914914
}
915+
916+
std::vector<CharSourceRange> swift::ide::
917+
getCallArgLabelRanges(SourceManager &SM, Expr *Arg) {
918+
std::vector<CharSourceRange> Ranges;
919+
if (TupleExpr *TE = dyn_cast<TupleExpr>(Arg)) {
920+
size_t ElemIndex = 0;
921+
for(Expr *Elem: TE->getElements()) {
922+
SourceLoc LabelStart(Elem->getStartLoc());
923+
SourceLoc LabelEnd(LabelStart);
924+
925+
auto NameIdentifier = TE->getElementName(ElemIndex);
926+
if (!NameIdentifier.empty()) {
927+
LabelStart = TE->getElementNameLoc(ElemIndex);
928+
}
929+
Ranges.push_back(CharSourceRange(SM, LabelStart, LabelEnd));
930+
++ElemIndex;
931+
}
932+
} else if (ParenExpr *PE = dyn_cast<ParenExpr>(Arg)) {
933+
if (PE->getSubExpr())
934+
Ranges.push_back(CharSourceRange(PE->getSubExpr()->getStartLoc(), 0));
935+
}
936+
937+
return Ranges;
938+
}

0 commit comments

Comments
 (0)